@@ -62,8 +62,8 @@ public sealed class DrawingCanvas<TPixel> : DrawingCanvas
6262 // Per-canvas glyph-outline cache: hoists RichTextGlyphRenderer's per-glyph outline cache from
6363 // per-DrawText-call scope up to the whole canvas, so a glyph outline built once is reused by
6464 // every DrawText call on this canvas (across a frame's many text runs) instead of being
65- // rebuilt for every run on a text-heavy page. Lazily created on first text draw.
66- private Dictionary < RichTextGlyphRenderer . CacheKey , List < RichTextGlyphRenderer . GlyphRenderData > > ? glyphCache ;
65+ // rebuilt for every run on a text-heavy page.
66+ private readonly Dictionary < RichTextGlyphRenderer . CacheKey , List < RichTextGlyphRenderer . GlyphRenderData > > glyphCache = [ ] ;
6767
6868 /// <summary>
6969 /// Initializes a new instance of the <see cref="DrawingCanvas{TPixel}"/> class.
@@ -443,10 +443,6 @@ public override void DrawText(
443443 this . DrawTextCore ( textOptions , text , path , brush , pen ) ;
444444 }
445445
446- // Returns the per-canvas glyph-outline cache, lazily creating it on first use.
447- private Dictionary < RichTextGlyphRenderer . CacheKey , List < RichTextGlyphRenderer . GlyphRenderData > > ResolveGlyphCache ( )
448- => this . glyphCache ??= [ ] ;
449-
450446 private void DrawTextCore (
451447 RichTextOptions textOptions ,
452448 ReadOnlySpan < char > text ,
@@ -467,7 +463,7 @@ private void DrawTextCore(
467463 EnsureTextPaint ( brush , pen ) ;
468464
469465 RichTextOptions configuredOptions = ConfigureTextOptions ( textOptions , path , out IPath ? configuredPath ) ;
470- using RichTextGlyphRenderer glyphRenderer = new ( effectiveOptions , configuredPath , pen , brush , this . ResolveGlyphCache ( ) ) ;
466+ using RichTextGlyphRenderer glyphRenderer = new ( effectiveOptions , configuredPath , pen , brush , this . glyphCache ) ;
471467 TextRenderer renderer = new ( glyphRenderer ) ;
472468 renderer . RenderText ( text , configuredOptions ) ;
473469
@@ -497,7 +493,7 @@ public override void DrawText(
497493 effectiveOptions . ShapeOptions ,
498494 Matrix4x4 . CreateTranslation ( location . X , location . Y , 0 ) * effectiveOptions . Transform ) ;
499495
500- using RichTextGlyphRenderer glyphRenderer = new ( placedOptions , path : null , pen , brush , this . ResolveGlyphCache ( ) ) ;
496+ using RichTextGlyphRenderer glyphRenderer = new ( placedOptions , path : null , pen , brush , this . glyphCache ) ;
501497 textBlock . RenderTo ( glyphRenderer , wrappingLength ) ;
502498
503499 this . DrawTextOperations ( glyphRenderer . DrawingOperations , placedOptions , state . ClipPaths ) ;
@@ -519,7 +515,7 @@ public override void DrawText(
519515 DrawingCanvasState state = this . ResolveState ( ) ;
520516 DrawingOptions effectiveOptions = state . Options ;
521517
522- using RichTextGlyphRenderer glyphRenderer = new ( effectiveOptions , path , pen , brush , this . ResolveGlyphCache ( ) ) ;
518+ using RichTextGlyphRenderer glyphRenderer = new ( effectiveOptions , path , pen , brush , this . glyphCache ) ;
523519 textBlock . RenderTo ( glyphRenderer , wrappingLength ) ;
524520
525521 this . DrawTextOperations ( glyphRenderer . DrawingOperations , effectiveOptions , state . ClipPaths ) ;
@@ -547,7 +543,7 @@ public override void DrawText(
547543 effectiveOptions . ShapeOptions ,
548544 Matrix4x4 . CreateTranslation ( location . X , location . Y , 0 ) * effectiveOptions . Transform ) ;
549545
550- using RichTextGlyphRenderer glyphRenderer = new ( placedOptions , path : null , pen , brush , this . ResolveGlyphCache ( ) ) ;
546+ using RichTextGlyphRenderer glyphRenderer = new ( placedOptions , path : null , pen , brush , this . glyphCache ) ;
551547 lineLayout . RenderTo ( glyphRenderer ) ;
552548
553549 this . DrawTextOperations ( glyphRenderer . DrawingOperations , placedOptions , state . ClipPaths ) ;
@@ -568,7 +564,7 @@ public override void DrawText(
568564 DrawingCanvasState state = this . ResolveState ( ) ;
569565 DrawingOptions effectiveOptions = state . Options ;
570566
571- using RichTextGlyphRenderer glyphRenderer = new ( effectiveOptions , path , pen , brush , this . ResolveGlyphCache ( ) ) ;
567+ using RichTextGlyphRenderer glyphRenderer = new ( effectiveOptions , path , pen , brush , this . glyphCache ) ;
572568 lineLayout . RenderTo ( glyphRenderer ) ;
573569
574570 this . DrawTextOperations ( glyphRenderer . DrawingOperations , effectiveOptions , state . ClipPaths ) ;
@@ -1155,7 +1151,7 @@ public override void Dispose()
11551151 }
11561152
11571153 // Release the per-canvas glyph-outline cache.
1158- this . glyphCache = null ;
1154+ this . glyphCache . Clear ( ) ;
11591155
11601156 this . isDisposed = true ;
11611157 }
0 commit comments