From b6585292fb3f981ff1bf15059ff0708c78ff41ec Mon Sep 17 00:00:00 2001 From: Eliot Jones Date: Sat, 21 Apr 2018 16:24:37 +0100 Subject: [PATCH] start adding support for reading and applying XObjects --- .../Integration/PigProductionHandbookTests.cs | 12 ++++++++ .../Content/ResourceContainer.cs | 15 ++++++++++ .../Graphics/Operations/InvokeNamedXObject.cs | 28 +++++++++++++++++++ ...ReflectionGraphicsStateOperationFactory.cs | 6 +++- 4 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 src/UglyToad.PdfPig/Graphics/Operations/InvokeNamedXObject.cs diff --git a/src/UglyToad.PdfPig.Tests/Integration/PigProductionHandbookTests.cs b/src/UglyToad.PdfPig.Tests/Integration/PigProductionHandbookTests.cs index ea20bb48..67ec57d8 100644 --- a/src/UglyToad.PdfPig.Tests/Integration/PigProductionHandbookTests.cs +++ b/src/UglyToad.PdfPig.Tests/Integration/PigProductionHandbookTests.cs @@ -24,6 +24,18 @@ } } + [Fact] + public void CanReadPage9() + { + using (var document = PdfDocument.Open(GetFilename())) + { + var page = document.GetPage(9); + + // TODO: This page requires a CFF font parser for Type 1 fonts, see 5.5.1 + Assert.Contains("BreedsNative breeds of pig can be found throughout the country. They are a small body size compared to other exotic and crosses pig types. There name varies from region to region, for example", page.Text); + } + } + [Fact] public void HasCorrectNumberOfPages() { diff --git a/src/UglyToad.PdfPig/Content/ResourceContainer.cs b/src/UglyToad.PdfPig/Content/ResourceContainer.cs index 9a321039..468fe814 100644 --- a/src/UglyToad.PdfPig/Content/ResourceContainer.cs +++ b/src/UglyToad.PdfPig/Content/ResourceContainer.cs @@ -30,6 +30,21 @@ LoadFontDictionary(fontDictionary, isLenientParsing); } + + if (resourceDictionary.TryGet(NameToken.Xobject, out var xobjectBase)) + { + var xobjectDictionary = DirectObjectFinder.Get(xobjectBase, scanner); + + foreach (var pair in xobjectDictionary.Data) + { + if (!(pair.Value is IndirectReferenceToken reference)) + { + throw new InvalidOperationException($"Expected the XObject dictionary value for key /{pair.Key} to be an indirect reference, instead got: {pair.Value}."); + } + + currentResourceState[NameToken.Create(pair.Key)] = reference.Data; + } + } } private void LoadFontDictionary(DictionaryToken fontDictionary, bool isLenientParsing) diff --git a/src/UglyToad.PdfPig/Graphics/Operations/InvokeNamedXObject.cs b/src/UglyToad.PdfPig/Graphics/Operations/InvokeNamedXObject.cs new file mode 100644 index 00000000..3f877ce8 --- /dev/null +++ b/src/UglyToad.PdfPig/Graphics/Operations/InvokeNamedXObject.cs @@ -0,0 +1,28 @@ +namespace UglyToad.PdfPig.Graphics.Operations +{ + using Content; + using Tokenization.Tokens; + + internal class InvokeNamedXObject : IGraphicsStateOperation + { + public const string Symbol = "Do"; + + public string Operator => Symbol; + + public NameToken Name { get; } + + public InvokeNamedXObject(NameToken name) + { + Name = name; + } + + public void Run(IOperationContext operationContext, IResourceStore resourceStore) + { + } + + public override string ToString() + { + return $"{Name} {Symbol}"; + } + } +} \ No newline at end of file diff --git a/src/UglyToad.PdfPig/Graphics/ReflectionGraphicsStateOperationFactory.cs b/src/UglyToad.PdfPig/Graphics/ReflectionGraphicsStateOperationFactory.cs index b7b601fb..04b44cb3 100644 --- a/src/UglyToad.PdfPig/Graphics/ReflectionGraphicsStateOperationFactory.cs +++ b/src/UglyToad.PdfPig/Graphics/ReflectionGraphicsStateOperationFactory.cs @@ -163,7 +163,11 @@ namespace UglyToad.PdfPig.Graphics { if (operands[offset] is NameToken name) { - arguments.Add(name.Data); + arguments.Add(name); + } + else if (operands[offset] is StringToken s) + { + arguments.Add(NameToken.Create(s.Data)); } else {