Compare commits

...

2 Commits

Author SHA1 Message Date
Eliot Jones
0370c51371 add a test for #208 2020-09-03 15:53:27 +01:00
Eliot Jones
c634ec3aa2 add more informative exceptions when reading encoding fails 2020-09-03 14:36:42 +01:00
2 changed files with 69 additions and 3 deletions

View File

@@ -0,0 +1,56 @@
namespace UglyToad.PdfPig.Tests.Fonts.Parser
{
using System.Text.RegularExpressions;
using PdfFonts.Parser;
using PdfPig.Core;
using PdfPig.Encryption;
using PdfPig.Tokenization.Scanner;
using PdfPig.Tokens;
using Xunit;
public class EncodingReaderTests
{
[Fact]
public void GetWinAnsiFromNameObject()
{
const string input = @"
1 0 obj
<< /Type /Font /Encoding 12 0 R /Name /WindowsFont1>>
endobj
12 0 obj
/WinAnsiEncoding
endobj";
var scanner = GetScanner(input);
var reader = new EncodingReader(scanner);
var fontDictionary = scanner.Get(new IndirectReference(1, 0));
var encoding = reader.Read(fontDictionary.Data as DictionaryToken);
Assert.NotNull(encoding);
}
private static PdfTokenScanner GetScanner(string s)
{
var input = StringBytesTestConverter.Convert(s, false);
var locationProvider = new TestObjectLocationProvider();
var regex = new Regex(@"\n(\d+)\s(\d+)\sobj");
foreach (Match match in regex.Matches(s))
{
var objN = match.Groups[1].Value;
var gen = match.Groups[2].Value;
locationProvider.Offsets[new IndirectReference(int.Parse(objN), int.Parse(gen))] = match.Index + 1;
}
return new PdfTokenScanner(input.Bytes, locationProvider,
new TestFilterProvider(), NoOpEncryptionHandler.Instance);
}
}
}

View File

@@ -2,6 +2,7 @@
{
using System;
using System.Collections.Generic;
using Core;
using Fonts;
using Fonts.Encodings;
using PdfPig.Parser.Parts;
@@ -47,13 +48,22 @@
return WinAnsiEncoding.Instance;
}
throw new InvalidOperationException($"Did not recognize named font: {name} in {fontDictionary}.");
}
DictionaryToken encodingDictionary = DirectObjectFinder.Get<DictionaryToken>(baseEncodingObject, pdfScanner);
try
{
DictionaryToken encodingDictionary = DirectObjectFinder.Get<DictionaryToken>(baseEncodingObject, pdfScanner);
var encoding = ReadEncodingDictionary(encodingDictionary, fontEncoding);
var encoding = ReadEncodingDictionary(encodingDictionary, fontEncoding);
return encoding;
return encoding;
}
catch (Exception ex)
{
throw new PdfDocumentFormatException($"Failed to locate encoding in {fontDictionary}.", ex);
}
}
private Encoding ReadEncodingDictionary(DictionaryToken encodingDictionary, Encoding fontEncoding)