Skip to content

Commit a7adee1

Browse files
author
ArthurHub
committed
fix right and center align glyph padding (https://htmlrenderer.codeplex.com/discussions/458875)
1 parent 0751bfd commit a7adee1

File tree

2 files changed

+41
-27
lines changed

2 files changed

+41
-27
lines changed

Source/Demo/TestSamples/12.Text.htm

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22
<head>
33
<title>Text</title>
44
<link rel="Stylesheet" href="StyleSheet" />
5+
<style>
6+
.right {
7+
text-align: right;
8+
border-width: 1px;
9+
border-style: solid;
10+
border-color: blue;
11+
}
12+
</style>
513
</head>
614
<body>
715
<h2>Case 1</h2>
@@ -14,10 +22,20 @@ <h2>Text align justify with background colors</h2>
1422
<p style="text-align: justify">
1523
<div>
1624
Lorem ipsum dolor sit amet, <span style="background-color: gold;">consectetur</span> adipisicing
17-
elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua tempor incididunt ut labore et dolore magna aliqua incididunt ut labore et dolore magna aliqua.
25+
elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua tempor incididunt ut labore et dolore magna aliqua incididunt ut labore et dolore magna aliqua.
1826
</div>
1927
</p>
2028
<hr />
29+
<h2>Right align adjusts</h2>
30+
<table style="border-spacing: 0px;">
31+
<tr>
32+
<td class="right">978</td>
33+
</tr>
34+
<tr>
35+
<td class="right">32</td>
36+
</tr>
37+
</table>
38+
<hr />
2139
<h2>Transparent text</h2>
2240
<p style="color: rgba(255,0,0,100)">
2341
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur ornare mollis elit. Integer sagittis. Fusce elementum commodo felis. Vivamus lacinia eleifend libero. Donec

Source/HtmlRenderer/Dom/CssLayoutEngine.cs

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -611,20 +611,21 @@ private static void ApplyCenterAlignment(IGraphics g, CssLineBox line)
611611

612612
CssRect lastWord = line.Words[line.Words.Count - 1];
613613
float right = line.OwnerBox.ActualRight - line.OwnerBox.ActualPaddingRight - line.OwnerBox.ActualBorderRightWidth;
614-
float diff = right - lastWord.Right - lastWord.LeftGlyphPadding - lastWord.OwnerBox.ActualBorderRightWidth - lastWord.OwnerBox.ActualPaddingRight;
614+
float diff = right - lastWord.Right - lastWord.OwnerBox.ActualBorderRightWidth - lastWord.OwnerBox.ActualPaddingRight;
615615
diff /= 2;
616616

617-
if (diff <= 0) return;
618-
619-
foreach (CssRect word in line.Words)
617+
if (diff > 0)
620618
{
621-
word.Left += diff;
622-
}
619+
foreach (CssRect word in line.Words)
620+
{
621+
word.Left += diff;
622+
}
623623

624-
foreach (CssBox b in line.Rectangles.Keys)
625-
{
626-
RectangleF r = b.Rectangles[line];
627-
b.Rectangles[line] = new RectangleF(r.X + diff, r.Y, r.Width, r.Height);
624+
foreach (CssBox b in line.Rectangles.Keys)
625+
{
626+
RectangleF r = b.Rectangles[line];
627+
b.Rectangles[line] = new RectangleF(r.X + diff, r.Y, r.Width, r.Height);
628+
}
628629
}
629630
}
630631

@@ -640,25 +641,20 @@ private static void ApplyRightAlignment(IGraphics g, CssLineBox line)
640641

641642
CssRect lastWord = line.Words[line.Words.Count - 1];
642643
float right = line.OwnerBox.ActualRight - line.OwnerBox.ActualPaddingRight - line.OwnerBox.ActualBorderRightWidth;
643-
float diff = right - lastWord.Right - lastWord.LeftGlyphPadding - lastWord.OwnerBox.ActualBorderRightWidth - lastWord.OwnerBox.ActualPaddingRight;
644-
644+
float diff = right - lastWord.Right - lastWord.OwnerBox.ActualBorderRightWidth - lastWord.OwnerBox.ActualPaddingRight;
645645

646-
if (diff <= 0) return;
647-
648-
//if (line.OwnerBox.Direction == CssConstants.Rtl)
649-
//{
650-
651-
//}
652-
653-
foreach (CssRect word in line.Words)
646+
if (diff > 0)
654647
{
655-
word.Left += diff;
656-
}
648+
foreach (CssRect word in line.Words)
649+
{
650+
word.Left += diff;
651+
}
657652

658-
foreach (CssBox b in line.Rectangles.Keys)
659-
{
660-
RectangleF r = b.Rectangles[line];
661-
b.Rectangles[line] = new RectangleF(r.X + diff, r.Y, r.Width, r.Height);
653+
foreach (CssBox b in line.Rectangles.Keys)
654+
{
655+
RectangleF r = b.Rectangles[line];
656+
b.Rectangles[line] = new RectangleF(r.X + diff, r.Y, r.Width, r.Height);
657+
}
662658
}
663659
}
664660

0 commit comments

Comments
 (0)