Skip to content

Commit 26ef970

Browse files
author
ArthurHub
committed
Fix RTL text rendering (שלום,)
1 parent e90f8c1 commit 26ef970

7 files changed

Lines changed: 102 additions & 40 deletions

File tree

‎Source/HtmlRenderer/Dom/CssBox.cs‎

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,39 +1248,43 @@ protected void PaintBackground(IGraphics g, RectangleF rect, bool isFirst, bool
12481248
/// <param name="offset">the current scroll offset to offset the words</param>
12491249
private void PaintWords(IGraphics g, PointF offset)
12501250
{
1251-
foreach (var word in Words)
1251+
if( Width.Length > 0 )
12521252
{
1253-
var wordPoint = new PointF(word.Left + offset.X, word.Top + offset.Y);
1254-
if (word.Selected)
1253+
var isRtl = Direction == CssConstants.Rtl;
1254+
foreach (var word in Words)
12551255
{
1256-
// handle paint selected word background and with partial word selection
1257-
var wordLine = DomUtils.GetCssLineBoxByWord(word);
1258-
var left = word.SelectedStartOffset > -1 ? word.SelectedStartOffset : (wordLine.Words[0] != word && word.HasSpaceBefore ? -ActualWordSpacing : 0);
1259-
var padWordRight = word.HasSpaceAfter && !wordLine.IsLastSelectedWord(word);
1260-
var width = word.SelectedEndOffset > -1 ? word.SelectedEndOffset : word.Width + (padWordRight ? ActualWordSpacing : 0);
1261-
var rect = new RectangleF(word.Left + offset.X + left, word.Top + offset.Y, width - left, wordLine.LineHeight);
1256+
var wordPoint = new PointF(word.Left + offset.X, word.Top + offset.Y);
1257+
if (word.Selected)
1258+
{
1259+
// handle paint selected word background and with partial word selection
1260+
var wordLine = DomUtils.GetCssLineBoxByWord(word);
1261+
var left = word.SelectedStartOffset > -1 ? word.SelectedStartOffset : (wordLine.Words[0] != word && word.HasSpaceBefore ? -ActualWordSpacing : 0);
1262+
var padWordRight = word.HasSpaceAfter && !wordLine.IsLastSelectedWord(word);
1263+
var width = word.SelectedEndOffset > -1 ? word.SelectedEndOffset : word.Width + (padWordRight ? ActualWordSpacing : 0);
1264+
var rect = new RectangleF(word.Left + offset.X + left, word.Top + offset.Y, width - left, wordLine.LineHeight);
12621265

1263-
g.FillRectangle(GetSelectionBackBrush(false), rect.X, rect.Y, rect.Width, rect.Height);
1266+
g.FillRectangle(GetSelectionBackBrush(false), rect.X, rect.Y, rect.Width, rect.Height);
12641267

1265-
if (HtmlContainer.SelectionForeColor != System.Drawing.Color.Empty && (word.SelectedStartOffset > 0 || word.SelectedEndIndexOffset > -1))
1266-
{
1267-
var orgClip = g.GetClip();
1268-
g.SetClip(rect, CombineMode.Exclude);
1269-
g.DrawString(word.Text, ActualFont, ActualColor, wordPoint, new SizeF(word.Width, word.Height));
1270-
g.SetClip(rect);
1271-
g.DrawString(word.Text, ActualFont, GetSelectionForeBrush(), wordPoint, new SizeF(word.Width, word.Height));
1272-
g.SetClip(orgClip);
1268+
if (HtmlContainer.SelectionForeColor != System.Drawing.Color.Empty && (word.SelectedStartOffset > 0 || word.SelectedEndIndexOffset > -1))
1269+
{
1270+
var orgClip = g.GetClip();
1271+
g.SetClip(rect, CombineMode.Exclude);
1272+
g.DrawString(word.Text, ActualFont, ActualColor, wordPoint, new SizeF(word.Width, word.Height), isRtl);
1273+
g.SetClip(rect);
1274+
g.DrawString(word.Text, ActualFont, GetSelectionForeBrush(), wordPoint, new SizeF(word.Width, word.Height), isRtl);
1275+
g.SetClip(orgClip);
1276+
}
1277+
else
1278+
{
1279+
g.DrawString(word.Text, ActualFont, GetSelectionForeBrush(), wordPoint, new SizeF(word.Width, word.Height), isRtl);
1280+
}
12731281
}
12741282
else
12751283
{
1276-
g.DrawString(word.Text, ActualFont, GetSelectionForeBrush(), wordPoint, new SizeF(word.Width, word.Height));
1284+
//g.DrawRectangle(Pens.Red, wordPoint.X, wordPoint.Y, word.Width - 1, word.Height - 1);
1285+
g.DrawString(word.Text, ActualFont, ActualColor, wordPoint, new SizeF(word.Width, word.Height), isRtl);
12771286
}
12781287
}
1279-
else
1280-
{
1281-
//g.DrawRectangle(Pens.Red, wordPoint.X, wordPoint.Y, word.Width - 1, word.Height - 1);
1282-
g.DrawString(word.Text, ActualFont, ActualColor, wordPoint, new SizeF(word.Width, word.Height));
1283-
}
12841288
}
12851289
}
12861290

‎Source/HtmlRenderer/Dom/CssBoxFrame.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ private void DrawTitle(IGraphics g, Rectangle rect)
511511
sf.FormatFlags = StringFormatFlags.NoWrap;
512512
sf.Trimming = StringTrimming.EllipsisCharacter;
513513
var titleRect = new RectangleF(rect.Left + 3, rect.Top + 3, rect.Width - 6, rect.Height - 6);
514-
g.DrawString(_videoTitle, font, System.Drawing.Color.WhiteSmoke, titleRect.Location, SizeF.Empty);
514+
g.DrawString(_videoTitle, font, System.Drawing.Color.WhiteSmoke, titleRect.Location, SizeF.Empty, false);
515515
}
516516
}
517517
}

‎Source/HtmlRenderer/HtmlLabel.cs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -429,11 +429,11 @@ protected override CreateParams CreateParams
429429
switch( _borderStyle )
430430
{
431431
case BorderStyle.FixedSingle:
432-
createParams.Style |= Win32Utils.WS_BORDER;
432+
createParams.Style |= Win32Utils.WsBorder;
433433
break;
434434

435435
case BorderStyle.Fixed3D:
436-
createParams.ExStyle |= Win32Utils.WS_EX_CLIENTEDGE;
436+
createParams.ExStyle |= Win32Utils.WsExClientEdge;
437437
break;
438438
}
439439

@@ -633,12 +633,12 @@ protected virtual void OnRefresh(HtmlRefreshEventArgs e)
633633
[DebuggerStepThrough]
634634
protected override void WndProc(ref Message m)
635635
{
636-
if( _useSystemCursors && m.Msg == Win32Utils.WM_SETCURSOR && Cursor == Cursors.Hand )
636+
if( _useSystemCursors && m.Msg == Win32Utils.WmSetCursor && Cursor == Cursors.Hand )
637637
{
638638
try
639639
{
640640
// Replace .NET's hand cursor with the OS cursor
641-
Win32Utils.SetCursor(Win32Utils.LoadCursor(0, Win32Utils.IDC_HAND));
641+
Win32Utils.SetCursor(Win32Utils.LoadCursor(0, Win32Utils.IdcHand));
642642
m.Result = IntPtr.Zero;
643643
return;
644644
}

‎Source/HtmlRenderer/HtmlPanel.cs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -391,11 +391,11 @@ protected override CreateParams CreateParams
391391
switch (_borderStyle)
392392
{
393393
case BorderStyle.FixedSingle:
394-
createParams.Style |= Win32Utils.WS_BORDER;
394+
createParams.Style |= Win32Utils.WsBorder;
395395
break;
396396

397397
case BorderStyle.Fixed3D:
398-
createParams.ExStyle |= Win32Utils.WS_EX_CLIENTEDGE;
398+
createParams.ExStyle |= Win32Utils.WsExClientEdge;
399399
break;
400400
}
401401

@@ -665,12 +665,12 @@ protected override bool IsInputKey(Keys keyData)
665665
[DebuggerStepThrough]
666666
protected override void WndProc(ref Message m)
667667
{
668-
if (_useSystemCursors && m.Msg == Win32Utils.WM_SETCURSOR && Cursor == Cursors.Hand)
668+
if (_useSystemCursors && m.Msg == Win32Utils.WmSetCursor && Cursor == Cursors.Hand)
669669
{
670670
try
671671
{
672672
// Replace .NET's hand cursor with the OS cursor
673-
Win32Utils.SetCursor(Win32Utils.LoadCursor(0, Win32Utils.IDC_HAND));
673+
Win32Utils.SetCursor(Win32Utils.LoadCursor(0, Win32Utils.IdcHand));
674674
m.Result = IntPtr.Zero;
675675
return;
676676
}

‎Source/HtmlRenderer/IGraphics.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ public interface IGraphics : IDisposable
5555
/// <param name="color">the text color to set</param>
5656
/// <param name="point">the location to start string draw (top-left)</param>
5757
/// <param name="size">used to know the size of the rendered text for transparent text support</param>
58-
void DrawString(String str, Font font, Color color, PointF point, SizeF size);
58+
/// <param name="rtl">is to render the string right-to-left (true - RTL, false - LTR)</param>
59+
void DrawString(String str, Font font, Color color, PointF point, SizeF size, bool rtl);
5960

6061
/// <summary>
6162
/// Draws a line connecting the two points specified by the coordinate pairs.

‎Source/HtmlRenderer/Utils/Win32Utils.cs‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ namespace HtmlRenderer.Utils
2121
/// </summary>
2222
internal static class Win32Utils
2323
{
24-
public const int WS_BORDER = 0x00800000;
24+
public const int WsBorder = 0x00800000;
2525

26-
public const int WS_EX_CLIENTEDGE = 0x200;
26+
public const int WsExClientEdge = 0x200;
2727

2828
/// <summary>
2929
/// Const for BitBlt copy raster-operation code.
@@ -35,9 +35,17 @@ internal static class Win32Utils
3535
/// </summary>
3636
public const int BitBltPaint = 0x00EE0086;
3737

38-
public const int WM_SETCURSOR = 0x20;
38+
public const int WmSetCursor = 0x20;
3939

40-
public const int IDC_HAND = 32649;
40+
public const int IdcHand = 32649;
41+
42+
public const int TextAlignDefault = 0;
43+
44+
public const int TextAlignRtl = 256;
45+
46+
public const int TextAlignBaseline = 24;
47+
48+
public const int TextAlignBaselineRtl = 256 + 24;
4149

4250
[DllImport("user32.dll")]
4351
public static extern int SetCursor(int hCursor);
@@ -125,6 +133,9 @@ public static Rectangle GetWindowRectangle(IntPtr handle)
125133
[DllImport("User32.dll")]
126134
public static extern bool MoveWindow(IntPtr handle, int x, int y, int width, int height, bool redraw);
127135

136+
[DllImport("gdi32.dll")]
137+
public static extern int SetTextAlign(IntPtr hdc, uint fMode);
138+
128139
[DllImport("gdi32.dll")]
129140
public static extern int SetBkMode(IntPtr hdc, int mode);
130141

‎Source/HtmlRenderer/WinGraphics.cs‎

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ internal sealed class WinGraphics : IGraphics
4444
/// </summary>
4545
private static readonly StringFormat _stringFormat;
4646

47+
/// <summary>
48+
/// The string format to use for rendering strings for GDI+ text rendering
49+
/// </summary>
50+
private static readonly StringFormat _stringFormat2;
51+
4752
/// <summary>
4853
/// The wrapped WinForms graphics object
4954
/// </summary>
@@ -59,15 +64,23 @@ internal sealed class WinGraphics : IGraphics
5964
/// </summary>
6065
private IntPtr _hdc;
6166

67+
/// <summary>
68+
/// If text alignment was set to RTL
69+
/// </summary>
70+
private bool _setRtl;
71+
6272
#endregion
6373

74+
6475
/// <summary>
6576
/// Init static resources.
6677
/// </summary>
6778
static WinGraphics()
6879
{
6980
_stringFormat = new StringFormat(StringFormat.GenericTypographic);
7081
_stringFormat.FormatFlags = StringFormatFlags.NoClip | StringFormatFlags.MeasureTrailingSpaces;
82+
83+
_stringFormat2 = new StringFormat(StringFormat.GenericTypographic);
7184
}
7285

7386
/// <summary>
@@ -191,25 +204,29 @@ public Size MeasureString(string str, Font font, float maxWidth, out int charFit
191204
/// <param name="color">the text color to set</param>
192205
/// <param name="point">the location to start string draw (top-left)</param>
193206
/// <param name="size">used to know the size of the rendered text for transparent text support</param>
194-
public void DrawString(String str, Font font, Color color, PointF point, SizeF size)
207+
/// <param name="rtl">is to render the string right-to-left (true - RTL, false - LTR)</param>
208+
public void DrawString(String str, Font font, Color color, PointF point, SizeF size, bool rtl)
195209
{
196210
if( _useGdiPlusTextRendering )
197211
{
198212
ReleaseHdc();
199-
_g.DrawString(str, font, RenderUtils.GetSolidBrush(color), (int)Math.Round(point.X), (int)Math.Round(point.Y), StringFormat.GenericTypographic);
213+
SetRtlAlign(rtl);
214+
_g.DrawString(str, font, RenderUtils.GetSolidBrush(color), (int)Math.Round(point.X) + ( rtl ? size.Width : 0 ), (int)Math.Round(point.Y), _stringFormat2);
200215
}
201216
else
202217
{
203218
if( color.A == 255 )
204219
{
205220
SetFont(font);
206221
SetTextColor(color);
222+
SetRtlAlign(rtl);
207223

208224
Win32Utils.TextOut(_hdc, (int)Math.Round(point.X), (int)Math.Round(point.Y), str, str.Length);
209225
}
210226
else
211227
{
212228
InitHdc();
229+
SetRtlAlign(rtl);
213230
DrawTransparentText(_hdc, str, font, new Point((int)Math.Round(point.X), (int)Math.Round(point.Y)), Size.Round(size), color);
214231
}
215232
}
@@ -221,6 +238,9 @@ public void DrawString(String str, Font font, Color color, PointF point, SizeF s
221238
public void Dispose()
222239
{
223240
ReleaseHdc();
241+
242+
if(_useGdiPlusTextRendering && _setRtl)
243+
_stringFormat2.FormatFlags ^= StringFormatFlags.DirectionRightToLeft;
224244
}
225245

226246

@@ -340,6 +360,7 @@ private void InitHdc()
340360
var clip = _g.Clip.GetHrgn(_g);
341361

342362
_hdc = _g.GetHdc();
363+
_setRtl = false;
343364
Win32Utils.SetBkMode(_hdc, 1);
344365

345366
Win32Utils.SelectClipRgn(_hdc, clip);
@@ -381,6 +402,31 @@ private void SetTextColor(Color color)
381402
Win32Utils.SetTextColor(_hdc, rgb);
382403
}
383404

405+
/// <summary>
406+
/// Change text align to Left-to-Right or Right-to-Left if required.
407+
/// </summary>
408+
private void SetRtlAlign(bool rtl)
409+
{
410+
if( _setRtl )
411+
{
412+
if( !rtl )
413+
{
414+
if( _useGdiPlusTextRendering )
415+
_stringFormat2.FormatFlags ^= StringFormatFlags.DirectionRightToLeft;
416+
else
417+
Win32Utils.SetTextAlign(_hdc, Win32Utils.TextAlignDefault);
418+
}
419+
}
420+
else if( rtl )
421+
{
422+
if( _useGdiPlusTextRendering )
423+
_stringFormat2.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
424+
else
425+
Win32Utils.SetTextAlign(_hdc, Win32Utils.TextAlignRtl);
426+
}
427+
_setRtl = rtl;
428+
}
429+
384430
/// <summary>
385431
/// Special draw logic to draw transparent text using GDI.<br/>
386432
/// 1. Create in-memory DC<br/>

0 commit comments

Comments
 (0)