@@ -24,7 +24,6 @@ internal sealed class RichTextGlyphRenderer : BaseGlyphBuilder, IColorGlyphRende
2424 private const byte RenderOrderOutline = 1 ;
2525 private const byte RenderOrderDecoration = 2 ;
2626
27- private readonly RichTextOptions textOptions ;
2827 private readonly DrawingOptions drawingOptions ;
2928 private readonly MemoryAllocator memoryAllocator ;
3029 private readonly Pen defaultPen ;
@@ -40,7 +39,7 @@ internal sealed class RichTextGlyphRenderer : BaseGlyphBuilder, IColorGlyphRende
4039 private TextDecorationDetails ? currentUnderline ;
4140 private TextDecorationDetails ? currentStrikout ;
4241 private TextDecorationDetails ? currentOverline ;
43- private bool currentDecorationsRotated ;
42+ private bool currentDecorationRotated ;
4443
4544 // Just enough accuracy to allow for 1/8 px differences which later are accumulated while rendering,
4645 // but do not grow into full px offsets.
@@ -61,7 +60,6 @@ public RichTextGlyphRenderer(
6160 Brush brush )
6261 : base ( drawingOptions . Transform )
6362 {
64- this . textOptions = textOptions ;
6563 this . drawingOptions = drawingOptions ;
6664 this . memoryAllocator = memoryAllocator ;
6765 this . defaultPen = pen ;
@@ -102,7 +100,7 @@ protected override void BeginText(in FontRectangle bounds)
102100 protected override void BeginGlyph ( in FontRectangle bounds , in GlyphRendererParameters parameters )
103101 {
104102 this . currentColor = null ;
105- this . currentDecorationsRotated = parameters . LayoutMode . IsVertical ( ) || parameters . LayoutMode . IsVerticalMixed ( ) ;
103+ this . currentDecorationRotated = parameters . LayoutMode . IsVertical ( ) || parameters . LayoutMode . IsVerticalMixed ( ) ;
106104 this . currentTextRun = parameters . TextRun ;
107105 if ( parameters . TextRun is RichTextRun drawingRun )
108106 {
@@ -225,19 +223,14 @@ public override void SetDecoration(TextDecorations textDecorations, Vector2 star
225223 {
226224 thickness = pen . StrokeWidth ;
227225 }
228-
229- // Center the line at the given position.
230- bool rotated = this . currentDecorationsRotated ;
231- Vector2 pad = rotated ? new ( thickness * .5F , 0 ) : new ( 0 , thickness * .5F ) ;
232- Vector2 a = start - pad ;
233- Vector2 b = start + pad ;
234- Vector2 d = end - pad ;
235- thickness = rotated ? b . X - a . X : b . Y - a . Y ;
236-
237- pen ??= new SolidPen ( this . currentBrush ?? this . defaultBrush , thickness ) ;
226+ else
227+ {
228+ pen = new SolidPen ( this . currentBrush ?? this . defaultBrush , thickness ) ;
229+ }
238230
239231 // Drawing is always centered around the point so we need to offset by half.
240232 Vector2 offset = Vector2 . Zero ;
233+ bool rotated = this . currentDecorationRotated ;
241234 if ( textDecorations == TextDecorations . Overline )
242235 {
243236 // CSS overline is drawn above the position, so we need to move it up.
@@ -249,8 +242,7 @@ public override void SetDecoration(TextDecorations textDecorations, Vector2 star
249242 offset = rotated ? new ( - ( thickness * .5F ) , 0 ) : new ( 0 , thickness * .5F ) ;
250243 }
251244
252- // Clamp the line to whole pixels
253- this . AppendDecoration ( ref targetDecoration , ClampToPixel ( a + offset ) , ClampToPixel ( d + offset ) , pen , ( float ) Math . Truncate ( thickness ) ) ;
245+ this . AppendDecoration ( ref targetDecoration , start + offset , end + offset , pen , thickness , rotated ) ;
254246 }
255247
256248 protected override void EndGlyph ( )
@@ -425,24 +417,47 @@ private void FinalizeDecoration(ref TextDecorationDetails? decoration)
425417 }
426418 }
427419
428- private void AppendDecoration ( ref TextDecorationDetails ? decoration , Vector2 start , Vector2 end , Pen pen , float thickness )
420+ private void AppendDecoration (
421+ ref TextDecorationDetails ? decoration ,
422+ Vector2 start ,
423+ Vector2 end ,
424+ Pen pen ,
425+ float thickness ,
426+ bool rotated )
429427 {
430428 if ( decoration != null )
431429 {
432430 // TODO: This only works well if we are not trying to follow a path.
433431 if ( this . path is null )
434432 {
435433 // Let's try and expand it first.
436- if ( thickness == decoration . Value . Thickness
437- && decoration . Value . End . Y == start . Y
438- && ( decoration . Value . End . X + 1 ) >= start . X
434+ if ( rotated )
435+ {
436+ if ( thickness == decoration . Value . Thickness
437+ && decoration . Value . End . Y + 1 >= start . Y
438+ && decoration . Value . End . X == start . X
439439 && decoration . Value . Pen . Equals ( pen ) )
440+ {
441+ // Expand the line
442+ start = decoration . Value . Start ;
443+
444+ // If this is null finalize does nothing.
445+ decoration = null ;
446+ }
447+ }
448+ else
440449 {
441- // Expand the line
442- start = decoration . Value . Start ;
450+ if ( thickness == decoration . Value . Thickness
451+ && decoration . Value . End . Y == start . Y
452+ && decoration . Value . End . X + 1 >= start . X
453+ && decoration . Value . Pen . Equals ( pen ) )
454+ {
455+ // Expand the line
456+ start = decoration . Value . Start ;
443457
444- // If this is null finalize does nothing.
445- decoration = null ;
458+ // If this is null finalize does nothing.
459+ decoration = null ;
460+ }
446461 }
447462 }
448463 }
0 commit comments