瀏覽代碼

Minor optimization of SkFont creation

MarcinZiabek 3 年之前
父節點
當前提交
d97fa2cef1
共有 3 個文件被更改,包括 11 次插入7 次删除
  1. 6 0
      QuestPDF/Drawing/FontManager.cs
  2. 4 6
      QuestPDF/Drawing/TextShaper.cs
  3. 1 1
      QuestPDF/Elements/Text/Items/TextBlockSpan.cs

+ 6 - 0
QuestPDF/Drawing/FontManager.cs

@@ -17,6 +17,7 @@ namespace QuestPDF.Drawing
         private static ConcurrentDictionary<object, SKPaint> FontPaints = new();
         private static ConcurrentDictionary<object, SKPaint> FontPaints = new();
         private static ConcurrentDictionary<string, SKPaint> ColorPaints = new();
         private static ConcurrentDictionary<string, SKPaint> ColorPaints = new();
         private static ConcurrentDictionary<object, Font> ShaperFonts = new();
         private static ConcurrentDictionary<object, Font> ShaperFonts = new();
+        private static ConcurrentDictionary<object, SKFont> Fonts = new();
         private static ConcurrentDictionary<object, TextShaper> TextShapers = new();
         private static ConcurrentDictionary<object, TextShaper> TextShapers = new();
 
 
         private static void RegisterFontType(SKData fontData, string? customName = null)
         private static void RegisterFontType(SKData fontData, string? customName = null)
@@ -154,5 +155,10 @@ namespace QuestPDF.Drawing
         {
         {
             return TextShapers.GetOrAdd(style.PaintKey, _ => new TextShaper(style));
             return TextShapers.GetOrAdd(style.PaintKey, _ => new TextShaper(style));
         }
         }
+        
+        internal static SKFont FoFont(this TextStyle style)
+        {
+            return Fonts.GetOrAdd(style.PaintKey, _ => style.ToPaint().ToFont());
+        }
     }
     }
 }
 }

+ 4 - 6
QuestPDF/Drawing/TextShaper.cs

@@ -53,7 +53,7 @@ namespace QuestPDF.Drawing
                 yOffset += glyphPositions[i].YAdvance * scaleY;
                 yOffset += glyphPositions[i].YAdvance * scaleY;
             }
             }
             
             
-            return new TextShapingResult(Paint, glyphs);
+            return new TextShapingResult(glyphs);
         }
         }
         
         
         void PopulateBufferWithText(Buffer buffer, string text)
         void PopulateBufferWithText(Buffer buffer, string text)
@@ -89,12 +89,10 @@ namespace QuestPDF.Drawing
     
     
     internal class TextShapingResult
     internal class TextShapingResult
     {
     {
-        private SKPaint Paint { get; }
         public ShapedGlyph[] Glyphs { get; }
         public ShapedGlyph[] Glyphs { get; }
 
 
-        public TextShapingResult(SKPaint paint, ShapedGlyph[] glyphs)
+        public TextShapingResult(ShapedGlyph[] glyphs)
         {
         {
-            Paint = paint;
             Glyphs = glyphs;
             Glyphs = glyphs;
         }
         }
 
 
@@ -124,14 +122,14 @@ namespace QuestPDF.Drawing
             return end.Position.X - start.Position.X + end.Width;
             return end.Position.X - start.Position.X + end.Width;
         }
         }
         
         
-        public DrawTextCommand? PositionText(int startIndex, int endIndex)
+        public DrawTextCommand? PositionText(int startIndex, int endIndex, TextStyle textStyle)
         {
         {
             if (Glyphs.Length == 0)
             if (Glyphs.Length == 0)
                 return null;
                 return null;
             
             
             using var skTextBlobBuilder = new SKTextBlobBuilder();
             using var skTextBlobBuilder = new SKTextBlobBuilder();
             
             
-            var positionedRunBuffer = skTextBlobBuilder.AllocatePositionedRun(Paint.ToFont(), endIndex - startIndex + 1);
+            var positionedRunBuffer = skTextBlobBuilder.AllocatePositionedRun(textStyle.FoFont(), endIndex - startIndex + 1);
             var glyphSpan = positionedRunBuffer.GetGlyphSpan();
             var glyphSpan = positionedRunBuffer.GetGlyphSpan();
             var positionSpan = positionedRunBuffer.GetPositionSpan();
             var positionSpan = positionedRunBuffer.GetPositionSpan();
                 
                 

+ 1 - 1
QuestPDF/Elements/Text/Items/TextBlockSpan.cs

@@ -139,7 +139,7 @@ namespace QuestPDF.Elements.Text.Items
 
 
             var glyphOffsetY = GetGlyphOffset();
             var glyphOffsetY = GetGlyphOffset();
             
             
-            var textDrawingCommand = TextShapingResult.PositionText(request.StartIndex, request.EndIndex);
+            var textDrawingCommand = TextShapingResult.PositionText(request.StartIndex, request.EndIndex, Style);
             
             
             request.Canvas.DrawRectangle(new Position(0, request.TotalAscent), new Size(request.TextSize.Width, request.TextSize.Height), Style.BackgroundColor);
             request.Canvas.DrawRectangle(new Position(0, request.TotalAscent), new Size(request.TextSize.Width, request.TextSize.Height), Style.BackgroundColor);