test coverage for parsing glyph lists and fix bug with octal conversion

This commit is contained in:
Eliot Jones
2018-01-01 18:39:18 +00:00
parent d7b9a9d559
commit d03c04cca1
5 changed files with 117 additions and 40 deletions

View File

@@ -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);
}
}
}
}

View 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);
}
}
}

View File

@@ -9,53 +9,66 @@
{
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)
{
while (!reader.EndOfStream)
var line = reader.ReadLine();
if (string.IsNullOrWhiteSpace(line))
{
var line = reader.ReadLine();
if (string.IsNullOrWhiteSpace(line))
{
continue;
}
if (line[0] == '#')
{
continue;
}
var parts = line.Split(new[] {';'});
if (parts.Length != 2)
{
throw new InvalidOperationException(
$"The line in the glyph list did not match the expected format. Line was: {line}");
}
var key = parts[0];
var values = parts[1].Split(' ');
var value = string.Empty;
foreach (var s in values)
{
var code = int.Parse(s, NumberStyles.HexNumber);
value += char.ConvertFromUtf32(code);
}
result[key] = value;
continue;
}
if (line[0] == '#')
{
continue;
}
var parts = line.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length != 2)
{
throw new InvalidOperationException(
$"The line in the glyph list did not match the expected format. Line was: {line}");
}
var key = parts[0];
var values = parts[1].Split(' ');
var value = string.Empty;
foreach (var s in values)
{
var code = int.Parse(s, NumberStyles.HexNumber);
value += char.ConvertFromUtf32(code);
}
result[key] = value;
}
}

View File

@@ -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)
{

View File

@@ -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);
}