mirror of
https://github.com/UglyToad/PdfPig.git
synced 2026-03-10 00:23:29 +08:00
test coverage for parsing glyph lists and fix bug with octal conversion
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
namespace UglyToad.Pdf.Tests.Fonts.Encodings
|
||||
{
|
||||
using System;
|
||||
using System.IO;
|
||||
using Pdf.Fonts.Encodings;
|
||||
using Xunit;
|
||||
|
||||
@@ -14,5 +16,49 @@
|
||||
|
||||
Assert.Equal("H", h);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MissingResourceNameThrows()
|
||||
{
|
||||
Action action = () => GlyphListFactory.Get("missing resource");
|
||||
|
||||
Assert.Throws<ArgumentException>(action);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SkipsBlankLine()
|
||||
{
|
||||
var input = @"# comment
|
||||
|
||||
one;0031";
|
||||
using (var stream = new MemoryStream(System.Text.Encoding.ASCII.GetBytes(input)))
|
||||
{
|
||||
var result = GlyphListFactory.Read(stream);
|
||||
|
||||
Assert.Equal("1", result.NameToUnicode("one"));
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadNullThrows()
|
||||
{
|
||||
Action action = () => GlyphListFactory.Read(null);
|
||||
|
||||
Assert.Throws<ArgumentNullException>(action);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InvalidFormatThrows()
|
||||
{
|
||||
|
||||
var input = @"one;0031
|
||||
twelve;";
|
||||
using (var stream = new MemoryStream(System.Text.Encoding.ASCII.GetBytes(input)))
|
||||
{
|
||||
Action action = () => GlyphListFactory.Read(stream);
|
||||
|
||||
Assert.Throws<InvalidOperationException>(action);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
16
src/UglyToad.Pdf.Tests/Util/OctalHelpersTests.cs
Normal file
16
src/UglyToad.Pdf.Tests/Util/OctalHelpersTests.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
namespace UglyToad.Pdf.Tests.Util
|
||||
{
|
||||
using Pdf.Util;
|
||||
using Xunit;
|
||||
|
||||
public class OctalHelpersTests
|
||||
{
|
||||
[Fact]
|
||||
public void CorrectlyConverts()
|
||||
{
|
||||
var result = OctalHelpers.FromOctalInt(110);
|
||||
|
||||
Assert.Equal(72, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,16 +9,30 @@
|
||||
{
|
||||
public static GlyphList Get(string listName)
|
||||
{
|
||||
var result = new Dictionary<string, string>();
|
||||
|
||||
using (var resource = typeof(GlyphListFactory).Assembly.GetManifestResourceStream($"UglyToad.Pdf.Resources.GlyphList.{listName}"))
|
||||
using (var resource =
|
||||
typeof(GlyphListFactory).Assembly.GetManifestResourceStream(
|
||||
$"UglyToad.Pdf.Resources.GlyphList.{listName}"))
|
||||
{
|
||||
if (resource == null)
|
||||
{
|
||||
throw new ArgumentException($"No embedded glyph list resource was found with the name {listName}.");
|
||||
}
|
||||
|
||||
using (var reader = new StreamReader(resource))
|
||||
|
||||
return Read(resource);
|
||||
}
|
||||
}
|
||||
|
||||
public static GlyphList Read(Stream stream)
|
||||
{
|
||||
if (stream == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(stream));
|
||||
}
|
||||
|
||||
var result = new Dictionary<string, string>();
|
||||
|
||||
using (var reader = new StreamReader(stream))
|
||||
{
|
||||
while (!reader.EndOfStream)
|
||||
{
|
||||
@@ -34,7 +48,7 @@
|
||||
continue;
|
||||
}
|
||||
|
||||
var parts = line.Split(new[] {';'});
|
||||
var parts = line.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
if (parts.Length != 2)
|
||||
{
|
||||
@@ -57,7 +71,6 @@
|
||||
result[key] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new GlyphList(result);
|
||||
}
|
||||
|
||||
@@ -65,7 +65,9 @@
|
||||
var encodedCharacterName = encoding.GetName(characterCode);
|
||||
|
||||
// Look up the character name in the Adobe Glyph List.
|
||||
value = GlyphList.AdobeGlyphList.NameToUnicode(encodedCharacterName);
|
||||
|
||||
return true;
|
||||
|
||||
if (!ToUnicode.CanMapToUnicode)
|
||||
{
|
||||
|
||||
@@ -52,9 +52,9 @@
|
||||
var str = input.ToString();
|
||||
|
||||
int sum = 0;
|
||||
for (var i = str.Length - 1; i >= 0; i--)
|
||||
for (var i = 0; i < str.Length; i++)
|
||||
{
|
||||
var part = str[i].CharacterToShort();
|
||||
var part = str[str.Length - 1 - i].CharacterToShort();
|
||||
|
||||
sum += part * QuickPower(8, i);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user