Minor optimization of SkFont creation

This commit is contained in:
MarcinZiabek
2022-05-23 01:18:21 +02:00
parent 6d122c784d
commit d97fa2cef1
3 changed files with 11 additions and 7 deletions
+6
View File
@@ -17,6 +17,7 @@ namespace QuestPDF.Drawing
private static ConcurrentDictionary<object, SKPaint> FontPaints = new();
private static ConcurrentDictionary<string, SKPaint> ColorPaints = new();
private static ConcurrentDictionary<object, Font> ShaperFonts = new();
private static ConcurrentDictionary<object, SKFont> Fonts = new();
private static ConcurrentDictionary<object, TextShaper> TextShapers = new();
private static void RegisterFontType(SKData fontData, string? customName = null)
@@ -154,5 +155,10 @@ namespace QuestPDF.Drawing
{
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
View File
@@ -53,7 +53,7 @@ namespace QuestPDF.Drawing
yOffset += glyphPositions[i].YAdvance * scaleY;
}
return new TextShapingResult(Paint, glyphs);
return new TextShapingResult(glyphs);
}
void PopulateBufferWithText(Buffer buffer, string text)
@@ -89,12 +89,10 @@ namespace QuestPDF.Drawing
internal class TextShapingResult
{
private SKPaint Paint { get; }
public ShapedGlyph[] Glyphs { get; }
public TextShapingResult(SKPaint paint, ShapedGlyph[] glyphs)
public TextShapingResult(ShapedGlyph[] glyphs)
{
Paint = paint;
Glyphs = glyphs;
}
@@ -124,14 +122,14 @@ namespace QuestPDF.Drawing
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)
return null;
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 positionSpan = positionedRunBuffer.GetPositionSpan();
@@ -139,7 +139,7 @@ namespace QuestPDF.Elements.Text.Items
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);