mirror of
https://github.com/UglyToad/PdfPig.git
synced 2026-03-10 00:23:29 +08:00
Compare commits
2 Commits
nullabilit
...
debug-enco
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0370c51371 | ||
|
|
c634ec3aa2 |
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@
|
|||||||
{
|
{
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using Core;
|
||||||
using Fonts;
|
using Fonts;
|
||||||
using Fonts.Encodings;
|
using Fonts.Encodings;
|
||||||
using PdfPig.Parser.Parts;
|
using PdfPig.Parser.Parts;
|
||||||
@@ -47,13 +48,22 @@
|
|||||||
|
|
||||||
return WinAnsiEncoding.Instance;
|
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)
|
private Encoding ReadEncodingDictionary(DictionaryToken encodingDictionary, Encoding fontEncoding)
|
||||||
|
|||||||
Reference in New Issue
Block a user