cache bounding boxes for composite fonts

cached the bounding box for a specific character code value for type 0 (composite) fonts to improve performance.
This commit is contained in:
Eliot Jones
2020-02-28 16:36:06 +00:00
parent 4442a69a97
commit b7a86f482f

View File

@@ -1,6 +1,7 @@
namespace UglyToad.PdfPig.PdfFonts.Composite
{
using System;
using System.Collections.Generic;
using CidFonts;
using Cmap;
using Core;
@@ -16,6 +17,8 @@
private readonly CMap ucs2CMap;
// ReSharper disable once NotAccessedField.Local
private readonly bool isChineseJapaneseOrKorean;
private readonly Dictionary<int, CharacterBoundingBox> boundingBoxCache
= new Dictionary<int, CharacterBoundingBox>();
public NameToken Name => BaseFont;
@@ -84,6 +87,11 @@
public CharacterBoundingBox GetBoundingBox(int characterCode)
{
if (boundingBoxCache.TryGetValue(characterCode, out var cached))
{
return cached;
}
var matrix = GetFontMatrix();
var boundingBox = GetBoundingBoxInGlyphSpace(characterCode);
@@ -96,7 +104,11 @@
var advanceWidth = matrix.TransformX(width);
return new CharacterBoundingBox(boundingBox, advanceWidth);
var result = new CharacterBoundingBox(boundingBox, advanceWidth);
boundingBoxCache[characterCode] = result;
return result;
}
public PdfRectangle GetBoundingBoxInGlyphSpace(int characterCode)