diff --git a/src/UglyToad.PdfPig/PdfFonts/Composite/Type0Font.cs b/src/UglyToad.PdfPig/PdfFonts/Composite/Type0Font.cs index a4ba68fe..3d0a4e39 100644 --- a/src/UglyToad.PdfPig/PdfFonts/Composite/Type0Font.cs +++ b/src/UglyToad.PdfPig/PdfFonts/Composite/Type0Font.cs @@ -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 boundingBoxCache + = new Dictionary(); 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)