11// Copyright (c) Six Labors.
22// Licensed under the Six Labors Split License.
33
4- #nullable disable
5-
64using System . Numerics ;
75using System . Runtime . CompilerServices ;
86using SixLabors . Fonts ;
@@ -24,15 +22,15 @@ internal sealed class RichTextGlyphRenderer : BaseGlyphBuilder, IColorGlyphRende
2422
2523 private readonly DrawingOptions drawingOptions ;
2624 private readonly MemoryAllocator memoryAllocator ;
27- private readonly Pen defaultPen ;
28- private readonly Brush defaultBrush ;
29- private readonly IPathInternals path ;
25+ private readonly Pen ? defaultPen ;
26+ private readonly Brush ? defaultBrush ;
27+ private readonly IPathInternals ? path ;
3028 private bool isDisposed ;
3129
3230 private readonly Dictionary < Color , Brush > brushLookup = new ( ) ;
33- private TextRun currentTextRun ;
34- private Brush currentBrush ;
35- private Pen currentPen ;
31+ private TextRun ? currentTextRun ;
32+ private Brush ? currentBrush ;
33+ private Pen ? currentPen ;
3634 private Color ? currentColor ;
3735 private TextDecorationDetails ? currentUnderline ;
3836 private TextDecorationDetails ? currentStrikeout ;
@@ -54,8 +52,8 @@ public RichTextGlyphRenderer(
5452 RichTextOptions textOptions ,
5553 DrawingOptions drawingOptions ,
5654 MemoryAllocator memoryAllocator ,
57- Pen pen ,
58- Brush brush )
55+ Pen ? pen ,
56+ Brush ? brush )
5957 : base ( drawingOptions . Transform )
6058 {
6159 this . drawingOptions = drawingOptions ;
@@ -116,7 +114,7 @@ protected override void BeginGlyph(in FontRectangle bounds, in GlyphRendererPara
116114 // Create a cache entry for the glyph.
117115 // We need to apply the default transform to the bounds to get the correct size
118116 // for comparison with future glyphs. We can use this cached glyph anywhere in the text block.
119- var currentBounds = RectangleF . Transform (
117+ RectangleF currentBounds = RectangleF . Transform (
120118 new RectangleF ( bounds . Location , new ( bounds . Width , bounds . Height ) ) ,
121119 this . drawingOptions . Transform ) ;
122120
@@ -150,8 +148,8 @@ public void SetColor(GlyphColor color)
150148
151149 public override TextDecorations EnabledDecorations ( )
152150 {
153- TextRun run = this . currentTextRun ;
154- TextDecorations decorations = run . TextDecorations ;
151+ TextRun ? run = this . currentTextRun ;
152+ TextDecorations decorations = run ? . TextDecorations ?? TextDecorations . None ;
155153
156154 if ( this . currentTextRun is RichTextRun drawingRun )
157155 {
@@ -199,7 +197,7 @@ public override void SetDecoration(TextDecorations textDecorations, Vector2 star
199197 return ;
200198 }
201199
202- Pen pen = null ;
200+ Pen ? pen = null ;
203201 if ( this . currentTextRun is RichTextRun drawingRun )
204202 {
205203 if ( textDecorations == TextDecorations . Strikeout )
@@ -224,8 +222,9 @@ public override void SetDecoration(TextDecorations textDecorations, Vector2 star
224222 else
225223 {
226224 // Clamp the thickness to whole pixels.
225+ // Brush cannot be null if pen is null.
227226 thickness = MathF . Max ( 1F , MathF . Round ( thickness ) ) ;
228- pen = new SolidPen ( this . currentBrush ?? this . defaultBrush , thickness ) ;
227+ pen = new SolidPen ( ( this . currentBrush ?? this . defaultBrush ) ! , thickness ) ;
229228 }
230229
231230 // Drawing is always centered around the point so we need to offset by half.
@@ -275,7 +274,7 @@ protected override void EndGlyph()
275274 renderFill = true ;
276275 if ( this . currentColor . HasValue )
277276 {
278- if ( this . brushLookup . TryGetValue ( this . currentColor . Value , out Brush brush ) )
277+ if ( this . brushLookup . TryGetValue ( this . currentColor . Value , out Brush ? brush ) )
279278 {
280279 this . currentBrush = brush ;
281280 }
@@ -309,7 +308,7 @@ protected override void EndGlyph()
309308
310309 if ( renderOutline )
311310 {
312- path = this . currentPen . GeneratePath ( path ) ;
311+ path = this . currentPen ! . GeneratePath ( path ) ;
313312 renderData . OutlineMap = this . Render ( path ) ;
314313 }
315314
@@ -361,7 +360,7 @@ protected override void EndGlyph()
361360 {
362361 RenderLocation = renderLocation ,
363362 Map = renderData . FillMap ,
364- Brush = this . currentBrush ,
363+ Brush = this . currentBrush ! ,
365364 RenderPass = RenderOrderFill
366365 } ) ;
367366 }
@@ -372,7 +371,7 @@ protected override void EndGlyph()
372371 {
373372 RenderLocation = renderLocation ,
374373 Map = renderData . OutlineMap ,
375- Brush = this . currentPen ? . StrokeFill ?? this . currentBrush ,
374+ Brush = this . currentPen ? . StrokeFill ?? this . currentBrush ! ,
376375 RenderPass = RenderOrderOutline
377376 } ) ;
378377 }
@@ -538,7 +537,7 @@ private Buffer2D<float> Render(IPath path)
538537 // Take the path inside the path builder, scan thing and generate a Buffer2D representing the glyph.
539538 Buffer2D < float > buffer = this . memoryAllocator . Allocate2D < float > ( size . Width , size . Height , AllocationOptions . Clean ) ;
540539
541- var scanner = PolygonScanner . Create (
540+ PolygonScanner scanner = PolygonScanner . Create (
542541 offsetPath ,
543542 0 ,
544543 size . Height ,
0 commit comments