fix reading nested array for page content

This commit is contained in:
Eliot Jones
2018-11-15 20:05:15 +00:00
parent 7e1bcf6f64
commit b76cfcee57
2 changed files with 33 additions and 12 deletions

View File

@@ -2,8 +2,6 @@
{ {
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Content; using Content;
using Exceptions; using Exceptions;
using Filters; using Filters;
@@ -63,14 +61,13 @@
{ {
// ignored for now, is it possible? check the spec... // ignored for now, is it possible? check the spec...
} }
else if (contents is ArrayToken array) else if (DirectObjectFinder.TryGet<ArrayToken>(contents, pdfScanner, out var array))
{ {
var bytes = new List<byte>(); var bytes = new List<byte>();
foreach (var item in array.Data) foreach (var item in array.Data)
{ {
var obj = item as IndirectReferenceToken; if (!(item is IndirectReferenceToken obj))
if (obj == null)
{ {
throw new PdfDocumentFormatException($"The contents contained something which was not an indirect reference: {item}."); throw new PdfDocumentFormatException($"The contents contained something which was not an indirect reference: {item}.");
} }
@@ -88,8 +85,7 @@
content = GetContent(bytes, cropBox, userSpaceUnit, isLenientParsing); content = GetContent(bytes, cropBox, userSpaceUnit, isLenientParsing);
} }
else else
{ {
// TODO: this can be an array of stream objects... investigate
var contentStream = DirectObjectFinder.Get<StreamToken>(contents, pdfScanner); var contentStream = DirectObjectFinder.Get<StreamToken>(contents, pdfScanner);
if (contentStream == null) if (contentStream == null)
@@ -109,11 +105,6 @@
private PageContent GetContent(IReadOnlyList<byte> contentBytes, CropBox cropBox, UserSpaceUnit userSpaceUnit, bool isLenientParsing) private PageContent GetContent(IReadOnlyList<byte> contentBytes, CropBox cropBox, UserSpaceUnit userSpaceUnit, bool isLenientParsing)
{ {
if (Debugger.IsAttached)
{
var txt = OtherEncodings.BytesAsLatin1String(contentBytes.ToArray());
}
var operations = pageContentParser.Parse(new ByteArrayInputBytes(contentBytes)); var operations = pageContentParser.Parse(new ByteArrayInputBytes(contentBytes));
var context = new ContentStreamProcessor(cropBox.Bounds, resourceStore, userSpaceUnit, isLenientParsing, pdfScanner, xObjectFactory); var context = new ContentStreamProcessor(cropBox.Bounds, resourceStore, userSpaceUnit, isLenientParsing, pdfScanner, xObjectFactory);

View File

@@ -6,6 +6,36 @@
internal static class DirectObjectFinder internal static class DirectObjectFinder
{ {
public static bool TryGet<T>(IToken token, IPdfTokenScanner scanner, out T tokenResult) where T : IToken
{
tokenResult = default(T);
if (token is T t)
{
tokenResult = t;
return true;
}
if (!(token is IndirectReferenceToken reference))
{
return false;
}
var temp = scanner.Get(reference.Data);
if (temp.Data is T tTemp)
{
tokenResult = tTemp;
return true;
}
if (temp.Data is IndirectReferenceToken nestedReferenceToken)
{
return TryGet(nestedReferenceToken, scanner, out tokenResult);
}
return false;
}
public static T Get<T>(IToken token, IPdfTokenScanner scanner) where T : IToken public static T Get<T>(IToken token, IPdfTokenScanner scanner) where T : IToken
{ {
if (token is T result) if (token is T result)