|
5 | 5 | using System.Runtime.CompilerServices; |
6 | 6 | using SixLabors.Fonts; |
7 | 7 | using SixLabors.Fonts.Rendering; |
| 8 | +using SixLabors.Fonts.Unicode; |
8 | 9 | using SixLabors.ImageSharp.Drawing.Processing.Processors.Drawing; |
9 | 10 | using SixLabors.ImageSharp.Drawing.Shapes.Rasterization; |
10 | 11 | using SixLabors.ImageSharp.Drawing.Text; |
@@ -41,12 +42,12 @@ internal sealed partial class RichTextGlyphRenderer : BaseGlyphBuilder, IDisposa |
41 | 42 | // - Provide a good accuracy (smaller than 0.2% image difference compared to the non-caching variant) |
42 | 43 | // - Cache hit ratio above 60% |
43 | 44 | private const float AccuracyMultiple = 8; |
44 | | - private readonly Dictionary<(GlyphRendererParameters Glyph, RectangleF Bounds), List<GlyphRenderData>> glyphCache = []; |
| 45 | + private readonly Dictionary<CacheKey, List<GlyphRenderData>> glyphCache = []; |
45 | 46 | private int cacheReadIndex; |
46 | 47 |
|
47 | 48 | private bool rasterizationRequired; |
48 | 49 | private readonly bool noCache; |
49 | | - private (GlyphRendererParameters Glyph, RectangleF Bounds) currentCacheKey; |
| 50 | + private CacheKey currentCacheKey; |
50 | 51 |
|
51 | 52 | public RichTextGlyphRenderer( |
52 | 53 | RichTextOptions textOptions, |
@@ -128,7 +129,7 @@ protected override void BeginGlyph(in FontRectangle bounds, in GlyphRendererPara |
128 | 129 | MathF.Round(currentBounds.Width * AccuracyMultiple) / AccuracyMultiple, |
129 | 130 | MathF.Round(currentBounds.Height * AccuracyMultiple) / AccuracyMultiple); |
130 | 131 |
|
131 | | - this.currentCacheKey = (parameters, new RectangleF(subPixelLocation, subPixelSize)); |
| 132 | + this.currentCacheKey = CacheKey.FromParameters(parameters, new RectangleF(subPixelLocation, subPixelSize)); |
132 | 133 | if (this.glyphCache.ContainsKey(this.currentCacheKey)) |
133 | 134 | { |
134 | 135 | // We have already drawn the glyph vectors. |
@@ -588,7 +589,7 @@ private void Dispose(bool disposing) |
588 | 589 | { |
589 | 590 | if (disposing) |
590 | 591 | { |
591 | | - foreach (KeyValuePair<(GlyphRendererParameters Glyph, RectangleF Bounds), List<GlyphRenderData>> kv in this.glyphCache) |
| 592 | + foreach (KeyValuePair<CacheKey, List<GlyphRenderData>> kv in this.glyphCache) |
592 | 593 | { |
593 | 594 | foreach (GlyphRenderData data in kv.Value) |
594 | 595 | { |
@@ -624,4 +625,49 @@ public readonly void Dispose() |
624 | 625 | this.OutlineMap?.Dispose(); |
625 | 626 | } |
626 | 627 | } |
| 628 | + |
| 629 | + private readonly struct CacheKey |
| 630 | + { |
| 631 | + public string Font { get; init; } |
| 632 | + |
| 633 | + public GlyphColor GlyphColor { get; init; } |
| 634 | + |
| 635 | + public GlyphType GlyphType { get; init; } |
| 636 | + |
| 637 | + public FontStyle FontStyle { get; init; } |
| 638 | + |
| 639 | + public ushort GlyphId { get; init; } |
| 640 | + |
| 641 | + public ushort CompositeGlyphId { get; init; } |
| 642 | + |
| 643 | + public CodePoint CodePoint { get; init; } |
| 644 | + |
| 645 | + public float PointSize { get; init; } |
| 646 | + |
| 647 | + public float Dpi { get; init; } |
| 648 | + |
| 649 | + public GlyphLayoutMode LayoutMode { get; init; } |
| 650 | + |
| 651 | + public TextRun TextRun { get; init; } |
| 652 | + |
| 653 | + public RectangleF Bounds { get; init; } |
| 654 | + |
| 655 | + public static CacheKey FromParameters(in GlyphRendererParameters parameters, RectangleF bounds) |
| 656 | + => new() |
| 657 | + { |
| 658 | + // Our caching does not need the grapheme index as that is only relevant to the text layout. |
| 659 | + Font = parameters.Font, |
| 660 | + GlyphColor = parameters.GlyphColor, |
| 661 | + GlyphType = parameters.GlyphType, |
| 662 | + FontStyle = parameters.FontStyle, |
| 663 | + GlyphId = parameters.GlyphId, |
| 664 | + CompositeGlyphId = parameters.CompositeGlyphId, |
| 665 | + CodePoint = parameters.CodePoint, |
| 666 | + PointSize = parameters.PointSize, |
| 667 | + Dpi = parameters.Dpi, |
| 668 | + LayoutMode = parameters.LayoutMode, |
| 669 | + TextRun = parameters.TextRun, |
| 670 | + Bounds = bounds |
| 671 | + }; |
| 672 | + } |
627 | 673 | } |
0 commit comments