handle extraneous def token in some dictionaries and skip returning glyph bounds if not in font

This commit is contained in:
Eliot Jones
2019-05-19 13:27:38 +01:00
parent e9e376c52a
commit 31d12eb731
4 changed files with 21 additions and 12 deletions

View File

@@ -63,7 +63,7 @@
var builder = new StringBuilder("<!DOCTYPE html><html><head></head><body>"); var builder = new StringBuilder("<!DOCTYPE html><html><head></head><body>");
foreach (var charString in result.CharStrings.CharStrings) foreach (var charString in result.CharStrings.CharStrings)
{ {
var path = result.CharStrings.Generate(charString.Key); Assert.True(result.CharStrings.TryGenerate(charString.Key, out var path));
builder.AppendLine(path.ToFullSvg()); builder.AppendLine(path.ToFullSvg());
} }

View File

@@ -25,27 +25,27 @@
Subroutines = subroutines ?? throw new ArgumentNullException(nameof(subroutines)); Subroutines = subroutines ?? throw new ArgumentNullException(nameof(subroutines));
} }
public PdfPath Generate(string name) public bool TryGenerate(string name, out PdfPath path)
{ {
PdfPath glyph; path = default(PdfPath);
lock (locker) lock (locker)
{ {
if (glyphs.TryGetValue(name, out var result)) if (glyphs.TryGetValue(name, out path))
{ {
return result; return true;
} }
if (!CharStrings.TryGetValue(name, out var sequence)) if (!CharStrings.TryGetValue(name, out var sequence))
{ {
throw new InvalidOperationException($"No charstring sequence with the name /{name} in this font."); return false;
} }
glyph = Run(sequence); path = Run(sequence);
glyphs[name] = glyph; glyphs[name] = path;
} }
return glyph; return true;
} }
private PdfPath Run(CommandSequence sequence) private PdfPath Run(CommandSequence sequence)

View File

@@ -2,7 +2,6 @@
{ {
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using CharStrings; using CharStrings;
using Core; using Core;
using Geometry; using Geometry;
@@ -62,7 +61,11 @@
public PdfRectangle? GetCharacterBoundingBox(string characterName) public PdfRectangle? GetCharacterBoundingBox(string characterName)
{ {
var glyph = CharStrings.Generate(characterName); if (!CharStrings.TryGenerate(characterName, out var glyph))
{
return null;
}
var bbox = glyph.GetBoundingRectangle(); var bbox = glyph.GetBoundingRectangle();
return bbox; return bbox;
@@ -77,7 +80,7 @@
{ {
if (FontMatrix == null || FontMatrix.Data.Count != 6) if (FontMatrix == null || FontMatrix.Data.Count != 6)
{ {
return TransformationMatrix.FromValues(0.001m, 0, 0, 0.001m, 0, 0);; return TransformationMatrix.FromValues(0.001m, 0, 0, 0.001m, 0, 0);
} }
var a = ((NumericToken) FontMatrix.Data[0]).Data; var a = ((NumericToken) FontMatrix.Data[0]).Data;

View File

@@ -94,6 +94,12 @@
result[key] = token; result[key] = token;
} }
// skip def.
if (PeekNext(tokens, i) == OperatorToken.Def)
{
i++;
}
key = null; key = null;
} }