mirror of
https://github.com/UglyToad/PdfPig.git
synced 2025-09-19 10:47:56 +08:00
inline transformation code and cache afm strings
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
namespace UglyToad.PdfPig.Fonts.AdobeFontMetrics
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace UglyToad.PdfPig.Fonts.AdobeFontMetrics
|
||||
{
|
||||
using System;
|
||||
using System.Globalization;
|
||||
@@ -11,6 +13,9 @@
|
||||
/// </summary>
|
||||
public static class AdobeFontMetricsParser
|
||||
{
|
||||
private static readonly object Locker = new object();
|
||||
private static readonly Dictionary<string, string> CharacterNames = new Dictionary<string, string>();
|
||||
|
||||
/// <summary>
|
||||
/// This is a comment in a AFM file.
|
||||
/// </summary>
|
||||
@@ -603,7 +608,18 @@
|
||||
}
|
||||
case CharmetricsN:
|
||||
{
|
||||
metric.Name = parts[1];
|
||||
lock (Locker)
|
||||
{
|
||||
var name = parts[1];
|
||||
|
||||
if (!CharacterNames.TryGetValue(name, out var cached))
|
||||
{
|
||||
cached = name;
|
||||
CharacterNames[name] = cached;
|
||||
}
|
||||
|
||||
metric.Name = cached;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case CharmetricsB:
|
||||
|
@@ -3,7 +3,7 @@
|
||||
/// <summary>
|
||||
/// The meaning of the metric sets field.
|
||||
/// </summary>
|
||||
public enum AdobeFontMetricsWritingDirections
|
||||
public enum AdobeFontMetricsWritingDirections : byte
|
||||
{
|
||||
/// <summary>
|
||||
/// Writing direction 0 only.
|
||||
|
@@ -33,8 +33,8 @@
|
||||
{
|
||||
private static readonly HashSet<string> Standard14Names = new HashSet<string>();
|
||||
private static readonly Dictionary<string, string> Standard14Mapping = new Dictionary<string, string>(34);
|
||||
private static readonly Dictionary<string, AdobeFontMetrics> Standard14AfmMap = new Dictionary<string, AdobeFontMetrics>(34);
|
||||
private static readonly Dictionary<Standard14Font, AdobeFontMetrics> Standard14AfmTypeMap = new Dictionary<Standard14Font, AdobeFontMetrics>(14);
|
||||
private static readonly Dictionary<Standard14Font, string> BuilderTypesToNames = new Dictionary<Standard14Font, string>(14);
|
||||
private static readonly Dictionary<string, AdobeFontMetrics> Standard14Cache = new Dictionary<string, AdobeFontMetrics>(34);
|
||||
|
||||
static Standard14()
|
||||
{
|
||||
@@ -88,9 +88,14 @@
|
||||
Standard14Names.Add(fontName);
|
||||
Standard14Mapping.Add(fontName, afmName);
|
||||
|
||||
if (Standard14AfmMap.TryGetValue(afmName, out var metrics))
|
||||
if (type.HasValue)
|
||||
{
|
||||
Standard14AfmMap[fontName] = metrics;
|
||||
BuilderTypesToNames[type.Value] = afmName;
|
||||
}
|
||||
|
||||
if (Standard14Cache.TryGetValue(afmName, out var metrics))
|
||||
{
|
||||
Standard14Cache[fontName] = metrics;
|
||||
}
|
||||
|
||||
try
|
||||
@@ -112,11 +117,7 @@
|
||||
bytes = new ByteArrayInputBytes(memory.ToArray());
|
||||
}
|
||||
|
||||
Standard14AfmMap[fontName] = AdobeFontMetricsParser.Parse(bytes, true);
|
||||
if (type.HasValue)
|
||||
{
|
||||
Standard14AfmTypeMap[type.Value] = Standard14AfmMap[fontName];
|
||||
}
|
||||
Standard14Cache[fontName] = AdobeFontMetricsParser.Parse(bytes, true);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -130,7 +131,7 @@
|
||||
/// </summary>
|
||||
public static AdobeFontMetrics GetAdobeFontMetrics(string baseName)
|
||||
{
|
||||
Standard14AfmMap.TryGetValue(baseName, out var metrics);
|
||||
Standard14Cache.TryGetValue(baseName, out var metrics);
|
||||
|
||||
return metrics;
|
||||
}
|
||||
@@ -140,7 +141,7 @@
|
||||
/// </summary>
|
||||
public static AdobeFontMetrics GetAdobeFontMetrics(Standard14Font fontType)
|
||||
{
|
||||
return Standard14AfmTypeMap[fontType];
|
||||
return Standard14Cache[BuilderTypesToNames[fontType]];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
Reference in New Issue
Block a user