mirror of
https://github.com/UglyToad/PdfPig.git
synced 2025-10-15 19:54:52 +08:00
add very hacky parsing for type 1 font files in order to read the encoding
This commit is contained in:
@@ -11,6 +11,13 @@
|
||||
public static readonly OperatorToken EndObject = new OperatorToken("endobj");
|
||||
public static readonly OperatorToken StartStream = new OperatorToken("stream");
|
||||
public static readonly OperatorToken EndStream = new OperatorToken("endstream");
|
||||
public static readonly OperatorToken Eexec = new OperatorToken("eexec");
|
||||
public static readonly OperatorToken Def = new OperatorToken("def");
|
||||
public static readonly OperatorToken Dict = new OperatorToken("dict");
|
||||
public static readonly OperatorToken Readonly = new OperatorToken("readonly");
|
||||
public static readonly OperatorToken Dup = new OperatorToken("dup");
|
||||
public static readonly OperatorToken For = new OperatorToken("for");
|
||||
public static readonly OperatorToken Put = new OperatorToken("put");
|
||||
|
||||
public string Data { get; }
|
||||
|
||||
@@ -39,6 +46,20 @@
|
||||
return StartStream;
|
||||
case "endstream":
|
||||
return EndStream;
|
||||
case "eexec":
|
||||
return Eexec;
|
||||
case "def":
|
||||
return Def;
|
||||
case "dict":
|
||||
return Dict;
|
||||
case "readonly":
|
||||
return Readonly;
|
||||
case "dup":
|
||||
return Dup;
|
||||
case "for":
|
||||
return For;
|
||||
case "put":
|
||||
return Put;
|
||||
default:
|
||||
return new OperatorToken(data);
|
||||
}
|
||||
|
76
src/UglyToad.PdfPig/Tokenization/Type1ArrayTokenizer.cs
Normal file
76
src/UglyToad.PdfPig/Tokenization/Type1ArrayTokenizer.cs
Normal file
@@ -0,0 +1,76 @@
|
||||
namespace UglyToad.PdfPig.Tokenization
|
||||
{
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
using IO;
|
||||
using Tokens;
|
||||
|
||||
internal class Type1ArrayTokenizer : ITokenizer
|
||||
{
|
||||
public bool ReadsNextByte { get; } = false;
|
||||
|
||||
public bool TryTokenize(byte currentByte, IInputBytes inputBytes, out IToken token)
|
||||
{
|
||||
token = null;
|
||||
|
||||
if (currentByte != '{')
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var builder = new StringBuilder();
|
||||
|
||||
while (inputBytes.MoveNext())
|
||||
{
|
||||
if (inputBytes.CurrentByte == '}')
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
builder.Append((char) inputBytes.CurrentByte);
|
||||
}
|
||||
|
||||
var parts = builder.ToString().Split(new[] {" "}, StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
var tokens = new List<IToken>();
|
||||
|
||||
foreach (var part in parts)
|
||||
{
|
||||
if (char.IsNumber(part[0]) || part[0] == '-')
|
||||
{
|
||||
if (decimal.TryParse(part, NumberStyles.AllowLeadingSign, null, out var value))
|
||||
{
|
||||
tokens.Add(new NumericToken(value));
|
||||
}
|
||||
else
|
||||
{
|
||||
tokens.Add(OperatorToken.Create(part));
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (part[0] == '/')
|
||||
{
|
||||
tokens.Add(new NameToken(part.Substring(1)));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (part[0] == '(' && part[part.Length - 1] == ')')
|
||||
{
|
||||
tokens.Add(new StringToken(part));
|
||||
continue;
|
||||
}
|
||||
|
||||
tokens.Add(OperatorToken.Create(part));
|
||||
|
||||
}
|
||||
|
||||
token = new ArrayToken(tokens);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
42
src/UglyToad.PdfPig/Tokenization/Type1NameTokenizer.cs
Normal file
42
src/UglyToad.PdfPig/Tokenization/Type1NameTokenizer.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
namespace UglyToad.PdfPig.Tokenization
|
||||
{
|
||||
using System.Text;
|
||||
using IO;
|
||||
using Parser.Parts;
|
||||
using Tokens;
|
||||
|
||||
internal class Type1NameTokenizer : ITokenizer
|
||||
{
|
||||
public bool ReadsNextByte { get; } = true;
|
||||
|
||||
public bool TryTokenize(byte currentByte, IInputBytes inputBytes, out IToken token)
|
||||
{
|
||||
token = null;
|
||||
|
||||
if (currentByte != '/')
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var builder = new StringBuilder();
|
||||
while (inputBytes.MoveNext())
|
||||
{
|
||||
if (ReadHelper.IsWhitespace(inputBytes.CurrentByte)
|
||||
|| inputBytes.CurrentByte == '{'
|
||||
|| inputBytes.CurrentByte == '<'
|
||||
|| inputBytes.CurrentByte == '/'
|
||||
|| inputBytes.CurrentByte == '['
|
||||
|| inputBytes.CurrentByte == '(')
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
builder.Append((char)inputBytes.CurrentByte);
|
||||
}
|
||||
|
||||
token = new NameToken(builder.ToString());
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user