From bb4c6f2f1e2ffd275797d75eee4d53853d316b89 Mon Sep 17 00:00:00 2001 From: Eliot Jones Date: Sun, 21 May 2023 19:13:14 +0100 Subject: [PATCH] add capacity to glyph list dictionaries --- src/UglyToad.PdfPig.Fonts/GlyphList.cs | 2 +- src/UglyToad.PdfPig.Fonts/GlyphListFactory.cs | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/UglyToad.PdfPig.Fonts/GlyphList.cs b/src/UglyToad.PdfPig.Fonts/GlyphList.cs index 3871f078..af1ba736 100644 --- a/src/UglyToad.PdfPig.Fonts/GlyphList.cs +++ b/src/UglyToad.PdfPig.Fonts/GlyphList.cs @@ -43,7 +43,7 @@ { nameToUnicode = namesToUnicode; - var unicodeToNameTemp = new Dictionary(); + var unicodeToNameTemp = new Dictionary(namesToUnicode.Count); foreach (var pair in namesToUnicode) { diff --git a/src/UglyToad.PdfPig.Fonts/GlyphListFactory.cs b/src/UglyToad.PdfPig.Fonts/GlyphListFactory.cs index 72e8f7d8..a5e66982 100644 --- a/src/UglyToad.PdfPig.Fonts/GlyphListFactory.cs +++ b/src/UglyToad.PdfPig.Fonts/GlyphListFactory.cs @@ -18,19 +18,31 @@ throw new ArgumentException($"No embedded glyph list resource was found with the name {listName}."); } + int? capacity = null; + // Prevent too much wasted memory capacity for Adobe GlyphList + if (string.Equals("glyphlist", listName, StringComparison.OrdinalIgnoreCase)) + { + capacity = 4300; + } - return Read(resource); + return ReadInternal(resource, capacity); } } public static GlyphList Read(Stream stream) + { + return ReadInternal(stream); + } + + private static GlyphList ReadInternal(Stream stream, int? defaultDictionaryCapacity = 0) { if (stream == null) { throw new ArgumentNullException(nameof(stream)); } - var result = new Dictionary(); + var result = defaultDictionaryCapacity.HasValue ? new Dictionary(defaultDictionaryCapacity.Value) + : new Dictionary(); using (var reader = new StreamReader(stream)) {