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
|
namespace UglyToad.Pdf.Tests.Fonts.Encodings
|
||||||
{
|
{
|
||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
using Pdf.Fonts.Encodings;
|
using Pdf.Fonts.Encodings;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
@@ -14,5 +16,49 @@
|
|||||||
|
|
||||||
Assert.Equal("H", h);
|
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,53 +9,66 @@
|
|||||||
{
|
{
|
||||||
public static GlyphList Get(string listName)
|
public static GlyphList Get(string listName)
|
||||||
{
|
{
|
||||||
var result = new Dictionary<string, string>();
|
using (var resource =
|
||||||
|
typeof(GlyphListFactory).Assembly.GetManifestResourceStream(
|
||||||
using (var resource = typeof(GlyphListFactory).Assembly.GetManifestResourceStream($"UglyToad.Pdf.Resources.GlyphList.{listName}"))
|
$"UglyToad.Pdf.Resources.GlyphList.{listName}"))
|
||||||
{
|
{
|
||||||
if (resource == null)
|
if (resource == null)
|
||||||
{
|
{
|
||||||
throw new ArgumentException($"No embedded glyph list resource was found with the name {listName}.");
|
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();
|
continue;
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -65,7 +65,9 @@
|
|||||||
var encodedCharacterName = encoding.GetName(characterCode);
|
var encodedCharacterName = encoding.GetName(characterCode);
|
||||||
|
|
||||||
// Look up the character name in the Adobe Glyph List.
|
// Look up the character name in the Adobe Glyph List.
|
||||||
|
value = GlyphList.AdobeGlyphList.NameToUnicode(encodedCharacterName);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
|
||||||
if (!ToUnicode.CanMapToUnicode)
|
if (!ToUnicode.CanMapToUnicode)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -52,9 +52,9 @@
|
|||||||
var str = input.ToString();
|
var str = input.ToString();
|
||||||
|
|
||||||
int sum = 0;
|
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);
|
sum += part * QuickPower(8, i);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user