@@ -59,6 +59,18 @@ public sealed class DrawingCanvas<TPixel> : DrawingCanvas
5959 /// </summary>
6060 private readonly Stack < DrawingCanvasState > savedStates = new ( ) ;
6161
62+ // Per-canvas glyph-outline cache: when enabled, hoists RichTextGlyphRenderer's per-glyph
63+ // outline cache from per-DrawText-call scope up to the whole canvas, so a glyph outline
64+ // built once is reused by every DrawText call on this canvas (across a frame's many text
65+ // runs) instead of being rebuilt for every run on a text-heavy page. Lazily created and only
66+ // used when sharing is opted in via Configuration.SetSharedGlyphCache; see shareGlyphCache.
67+ private Dictionary < RichTextGlyphRenderer . CacheKey , List < RichTextGlyphRenderer . GlyphRenderData > > ? sharedGlyphCacheStore ;
68+
69+ // Whether this canvas shares its glyph-outline cache across DrawText calls. Resolved once at
70+ // construction from the configuration (Configuration.SetSharedGlyphCache); defaults to false,
71+ // in which case each DrawText uses RichTextGlyphRenderer's private per-call cache.
72+ private readonly bool shareGlyphCache ;
73+
6274 /// <summary>
6375 /// Initializes a new instance of the <see cref="DrawingCanvas{TPixel}"/> class.
6476 /// </summary>
@@ -149,6 +161,7 @@ private DrawingCanvas(
149161 this . targetFrame = targetFrame ;
150162 this . batcher = batcher ;
151163 this . ownsBatcher = ownsBatcher ;
164+ this . shareGlyphCache = configuration . GetSharedGlyphCache ( ) ;
152165
153166 // Canvas coordinates are local to the current frame; origin stays at (0,0).
154167 this . Bounds = new Rectangle ( 0 , 0 , targetFrame . Bounds . Width , targetFrame . Bounds . Height ) ;
@@ -437,6 +450,11 @@ public override void DrawText(
437450 this . DrawTextCore ( textOptions , text , path , brush , pen ) ;
438451 }
439452
453+ // Returns the per-canvas shared glyph-outline cache (lazily created) when sharing is enabled,
454+ // or null so the renderer falls back to its private per-call cache (the historical default).
455+ private Dictionary < RichTextGlyphRenderer . CacheKey , List < RichTextGlyphRenderer . GlyphRenderData > > ? ResolveGlyphCache ( )
456+ => this . shareGlyphCache ? this . sharedGlyphCacheStore ??= [ ] : null ;
457+
440458 private void DrawTextCore (
441459 RichTextOptions textOptions ,
442460 ReadOnlySpan < char > text ,
@@ -457,7 +475,7 @@ private void DrawTextCore(
457475 EnsureTextPaint ( brush , pen ) ;
458476
459477 RichTextOptions configuredOptions = ConfigureTextOptions ( textOptions , path , out IPath ? configuredPath ) ;
460- using RichTextGlyphRenderer glyphRenderer = new ( effectiveOptions , configuredPath , pen , brush ) ;
478+ using RichTextGlyphRenderer glyphRenderer = new ( effectiveOptions , configuredPath , pen , brush , this . ResolveGlyphCache ( ) ) ;
461479 TextRenderer renderer = new ( glyphRenderer ) ;
462480 renderer . RenderText ( text , configuredOptions ) ;
463481
@@ -487,7 +505,7 @@ public override void DrawText(
487505 effectiveOptions . ShapeOptions ,
488506 Matrix4x4 . CreateTranslation ( location . X , location . Y , 0 ) * effectiveOptions . Transform ) ;
489507
490- using RichTextGlyphRenderer glyphRenderer = new ( placedOptions , path : null , pen , brush ) ;
508+ using RichTextGlyphRenderer glyphRenderer = new ( placedOptions , path : null , pen , brush , this . ResolveGlyphCache ( ) ) ;
491509 textBlock . RenderTo ( glyphRenderer , wrappingLength ) ;
492510
493511 this . DrawTextOperations ( glyphRenderer . DrawingOperations , placedOptions , state . ClipPaths ) ;
@@ -509,7 +527,7 @@ public override void DrawText(
509527 DrawingCanvasState state = this . ResolveState ( ) ;
510528 DrawingOptions effectiveOptions = state . Options ;
511529
512- using RichTextGlyphRenderer glyphRenderer = new ( effectiveOptions , path , pen , brush ) ;
530+ using RichTextGlyphRenderer glyphRenderer = new ( effectiveOptions , path , pen , brush , this . ResolveGlyphCache ( ) ) ;
513531 textBlock . RenderTo ( glyphRenderer , wrappingLength ) ;
514532
515533 this . DrawTextOperations ( glyphRenderer . DrawingOperations , effectiveOptions , state . ClipPaths ) ;
@@ -537,7 +555,7 @@ public override void DrawText(
537555 effectiveOptions . ShapeOptions ,
538556 Matrix4x4 . CreateTranslation ( location . X , location . Y , 0 ) * effectiveOptions . Transform ) ;
539557
540- using RichTextGlyphRenderer glyphRenderer = new ( placedOptions , path : null , pen , brush ) ;
558+ using RichTextGlyphRenderer glyphRenderer = new ( placedOptions , path : null , pen , brush , this . ResolveGlyphCache ( ) ) ;
541559 lineLayout . RenderTo ( glyphRenderer ) ;
542560
543561 this . DrawTextOperations ( glyphRenderer . DrawingOperations , placedOptions , state . ClipPaths ) ;
@@ -558,7 +576,7 @@ public override void DrawText(
558576 DrawingCanvasState state = this . ResolveState ( ) ;
559577 DrawingOptions effectiveOptions = state . Options ;
560578
561- using RichTextGlyphRenderer glyphRenderer = new ( effectiveOptions , path , pen , brush ) ;
579+ using RichTextGlyphRenderer glyphRenderer = new ( effectiveOptions , path , pen , brush , this . ResolveGlyphCache ( ) ) ;
562580 lineLayout . RenderTo ( glyphRenderer ) ;
563581
564582 this . DrawTextOperations ( glyphRenderer . DrawingOperations , effectiveOptions , state . ClipPaths ) ;
@@ -1144,6 +1162,9 @@ public override void Dispose()
11441162 this . DisposePendingImageResources ( ) ;
11451163 }
11461164
1165+ // Release the per-canvas shared glyph-outline cache.
1166+ this . sharedGlyphCacheStore = null ;
1167+
11471168 this . isDisposed = true ;
11481169 }
11491170 }
0 commit comments