Skip to content

Commit

Permalink
Use singleton instances of encodings
Browse files Browse the repository at this point in the history
  • Loading branch information
dmester committed Jul 9, 2023
1 parent 6b198eb commit e93ec7f
Show file tree
Hide file tree
Showing 15 changed files with 33 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/PdfToSvg/DocumentModel/PdfStreamDebugProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public string Content
{
using (var stream = streamInfo.OpenDecoded(CancellationToken.None))
{
var encoding = new PdfDocEncoding();
var encoding = SingleByteEncoding.PdfDoc;
var buffer = new byte[8 * 1024];
var read = stream.Read(buffer, 0, buffer.Length);

Expand Down
2 changes: 1 addition & 1 deletion src/PdfToSvg/DocumentModel/PdfString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public override string ToString()
return Encoding.BigEndianUnicode.GetString(data, 2, data.Length - 2);
}

return new PdfDocEncoding().GetString(data);
return SingleByteEncoding.PdfDoc.GetString(data);
}

public string ToString(Encoding encoding) => encoding.GetString(data);
Expand Down
6 changes: 3 additions & 3 deletions src/PdfToSvg/Encodings/EncodingFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ internal static class EncodingFactory
{
if (encodingName == Names.WinAnsiEncoding)
{
return new WinAnsiEncoding();
return SingleByteEncoding.WinAnsi;
}

if (encodingName == Names.MacExpertEncoding)
{
return new MacExpertEncoding();
return SingleByteEncoding.MacExpert;
}

if (encodingName == Names.MacRomanEncoding)
{
return new MacRomanEncoding();
return SingleByteEncoding.MacRoman;
}
}
else if (definition is PdfDictionary encodingDict)
Expand Down
12 changes: 12 additions & 0 deletions src/PdfToSvg/Encodings/SingleByteEncoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ internal class SingleByteEncoding : Encoding
private Lazy<Dictionary<string, byte>> fromUnicode;
private Lazy<int> maxUnicodeLength;

private static SingleByteEncoding? standard;
private static SingleByteEncoding? pdfDoc;
private static SingleByteEncoding? winAnsi;
private static SingleByteEncoding? macExpert;
private static SingleByteEncoding? macRoman;

public static SingleByteEncoding Standard => standard ??= new StandardEncoding();
public static SingleByteEncoding PdfDoc => pdfDoc ??= new PdfDocEncoding();
public static SingleByteEncoding WinAnsi => winAnsi ??= new WinAnsiEncoding();
public static SingleByteEncoding MacExpert => macExpert ??= new MacExpertEncoding();
public static SingleByteEncoding MacRoman => macRoman ??= new MacRomanEncoding();

internal SingleByteEncoding(string?[] toUnicode, string?[] toGlyphName)
{
if (toUnicode == null) throw new ArgumentNullException(nameof(toUnicode));
Expand Down
2 changes: 1 addition & 1 deletion src/PdfToSvg/Fonts/BaseFont.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ protected virtual void OnInit(CancellationToken cancellationToken)
}

// Encoding
var baseEncoding = (isSymbolic ? openTypeFontEncoding : null) ?? new StandardEncoding();
var baseEncoding = (isSymbolic ? openTypeFontEncoding : null) ?? SingleByteEncoding.Standard;
var encodingDefinition = fontDict.GetValueOrDefault(Names.Encoding);
pdfFontEncoding = EncodingFactory.Create(encodingDefinition, baseEncoding);

Expand Down
2 changes: 1 addition & 1 deletion src/PdfToSvg/Fonts/CompactFonts/CompactFont.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public CompactFont(CompactFontSet fontSet)

public List<int> CharSet { get; } = new();

public SingleByteEncoding Encoding { get; set; } = new StandardEncoding();
public SingleByteEncoding Encoding { get; set; } = SingleByteEncoding.Standard;

public List<CompactFontGlyph> Glyphs { get; } = new();

Expand Down
6 changes: 3 additions & 3 deletions src/PdfToSvg/Fonts/CompactFonts/CompactFontParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,12 @@ private SingleByteEncoding ReadEncoding(CompactFont font, int offsetOrId)

if (isCidFont || offsetOrId <= 0)
{
return new StandardEncoding();
return SingleByteEncoding.Standard;
}

if (offsetOrId == 1)
{
return new MacExpertEncoding();
return SingleByteEncoding.MacExpert;
}

reader.Position = offsetOrId;
Expand Down Expand Up @@ -351,7 +351,7 @@ private void ReplaceSeacChars(CompactFont font)
if (seac != null)
{
var content = glyph.CharString.Content;
var standardEncoding = new StandardEncoding();
var standardEncoding = SingleByteEncoding.Standard;

var acharValue = standardEncoding.GetString(new byte[] { (byte)seac.Achar });
var bcharValue = standardEncoding.GetString(new byte[] { (byte)seac.Bchar });
Expand Down
2 changes: 1 addition & 1 deletion src/PdfToSvg/Fonts/OpenType/Tables/CffTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void Write(OpenTypeWriter writer)
// encoding has no meaning in an OpenType font.
foreach (var font in Content.Fonts)
{
font.Encoding = new StandardEncoding();
font.Encoding = SingleByteEncoding.Standard;
}

var data = CompactFontBuilder.Build(Content);
Expand Down
2 changes: 1 addition & 1 deletion src/PdfToSvg/Fonts/TrueTypeFont.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected override IEnumerable<CharInfo> GetChars()
// ISO 32000-2 section 9.6.5.4
var cmaps = openTypeFont?.CMaps ?? ArrayUtils.Empty<OpenTypeCMap>();
var postGlyphNames = GetGlyphNameLookup();
var encoding = pdfFontEncoding ?? new StandardEncoding();
var encoding = pdfFontEncoding ?? SingleByteEncoding.Standard;

var chars = Enumerable.Empty<CharInfo>();

Expand Down
2 changes: 1 addition & 1 deletion src/PdfToSvg/Fonts/Type1/Type1Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public Type1Parser(byte[] data)
{
if (startLexeme.Value.ToString() == "StandardEncoding")
{
return new StandardEncoding();
return SingleByteEncoding.Standard;
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/PdfToSvg/Fonts/Type1Font.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private Dictionary<string, uint> GetPostGlyphIndexLookup()
protected override IEnumerable<CharInfo> GetChars()
{
// ISO 32000-2 section 9.6.5.2
var encoding = pdfFontEncoding ?? openTypeFontEncoding ?? new StandardEncoding();
var encoding = pdfFontEncoding ?? openTypeFontEncoding ?? SingleByteEncoding.Standard;

var postGlyphIndexes = GetPostGlyphIndexLookup();
var cmap = openTypeFont?.CMaps.OrderByPriority().FirstOrDefault();
Expand Down
2 changes: 1 addition & 1 deletion src/PdfToSvg/Fonts/Type3Font.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private void PopulateOpenType(CancellationToken cancellationToken)
var transform = FontMatrix * Matrix.Scale(TargetEmSize, TargetEmSize);

var charProcs = fontDict.GetDictionaryOrEmpty(Names.CharProcs);
var encoding = pdfFontEncoding ?? new StandardEncoding();
var encoding = pdfFontEncoding ?? SingleByteEncoding.Standard;

// Handle glyphs
var glyphs = new List<CompactFontGlyph>();
Expand Down
2 changes: 1 addition & 1 deletion src/PdfToSvg/Security/KeyAlgorithms.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private static void PadPassword(byte[] output, string password)
#endif

// Convert codepage encoding to PDFDocEncoding
var encoding = new PdfDocEncoding();
var encoding = SingleByteEncoding.PdfDoc;
var decodeCharCount = Math.Min(PaddedPasswordLength, password.Length);
var passwordLength = encoding.GetBytes(password, 0, decodeCharCount, output, 0);

Expand Down
2 changes: 1 addition & 1 deletion tests/PdfToSvg.Tests/Encodings/CustomEncodingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void DecodeText()
39, new PdfName("Ebreve"),
128, new PdfName("Ncaron"), new PdfName("Wcircumflex"),
} },
}, new StandardEncoding());
}, SingleByteEncoding.Standard);

var str = decoder.GetString(new byte[] { 39, 128, 129, 198 });
var bytes = decoder.GetBytes("ĔŇŴÆ");
Expand Down
8 changes: 4 additions & 4 deletions tests/PdfToSvg.Tests/Encodings/EncodingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void WinAnsi()
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!#¤%&/()=?" +
"ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ";

var winAnsi = new WinAnsiEncoding();
var winAnsi = SingleByteEncoding.WinAnsi;
var net1252 = Encoding.GetEncoding(1252);

Assert.AreEqual(source, net1252.GetString(winAnsi.GetBytes(source)));
Expand All @@ -41,7 +41,7 @@ public void MacRoman()
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!#¤%&/()=?" +
"ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÑÒÓÔÕÖØÙÚÛÜßàáâãäåæçèéêëìíîïñòóôõö÷øùúûüÿ";

var macRoman = new MacRomanEncoding();
var macRoman = SingleByteEncoding.MacRoman;

Assert.AreEqual(source, macRoman.GetString(macRoman.GetBytes(source)));
Assert.AreEqual("ÊËÎ", macRoman.GetString(new byte[] { 0xe6, 0xe8, 0xeb }));
Expand All @@ -53,7 +53,7 @@ public void Standard()
{
var source = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!#¤%&/()=?";

var standard = new StandardEncoding();
var standard = SingleByteEncoding.Standard;

Assert.AreEqual(source, standard.GetString(standard.GetBytes(source)));
Assert.AreEqual("ÆŁæ", standard.GetString(new byte[] { 0xe1, 0xe8, 0xf1 }));
Expand All @@ -67,7 +67,7 @@ public void PdfDocEncoding()
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!#¤%&/()=?" +
"ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÑÒÓÔÕÖ×ØÙÚÛÜßàáâãäåæçèéêëìíîïñòóôõö÷øùúûüÿ";

var pdfDoc = new PdfDocEncoding();
var pdfDoc = SingleByteEncoding.PdfDoc;

Assert.AreEqual(source, pdfDoc.GetString(pdfDoc.GetBytes(source)));
Assert.AreEqual("œž¥", pdfDoc.GetString(new byte[] { 0x9c, 0x9e, 0xa5 }));
Expand Down

0 comments on commit e93ec7f

Please sign in to comment.