#6 add debugging code for inspecting svgs of generated cff glyphs fix small bugs

This commit is contained in:
Eliot Jones
2018-11-17 16:23:17 +00:00
parent 4d18a2478d
commit 936e17b723
5 changed files with 75 additions and 5 deletions

View File

@@ -16,7 +16,10 @@
[Fact] [Fact]
public void CanReadContent() public void CanReadContent()
{ {
using (var document = PdfDocument.Open(GetFilename())) using (var document = PdfDocument.Open(GetFilename(), new ParsingOptions
{
UseLenientParsing = false
}))
{ {
var page = document.GetPage(1); var page = document.GetPage(1);

View File

@@ -42,6 +42,8 @@
public PdfPoint CurrentLocation { get; set; } = new PdfPoint(0, 0); public PdfPoint CurrentLocation { get; set; } = new PdfPoint(0, 0);
public decimal? Width { get; set; }
public void AddRelativeHorizontalLine(decimal dx) public void AddRelativeHorizontalLine(decimal dx)
{ {
AddRelativeLine(dx, 0); AddRelativeLine(dx, 0);

View File

@@ -332,6 +332,8 @@
ctx.AddRelativeBezierCurve(dx1, dy1, dx2, dy2, 0, dy3); ctx.AddRelativeBezierCurve(dx1, dy1, dx2, dy2, 0, dy3);
} }
ctx.Stack.Clear();
}) })
}, },
{ 27, new LazyType2Command("hhcurveto", ctx => { 27, new LazyType2Command("hhcurveto", ctx =>

View File

@@ -43,16 +43,69 @@
return glyph; return glyph;
} }
private static CharacterPath Run(CommandSequence sequence) public static CharacterPath Run(CommandSequence sequence)
{ {
var context = new Type2BuildCharContext(); var context = new Type2BuildCharContext();
var hasRunStackClearingCommand = false;
foreach (var command in sequence.Commands) foreach (var command in sequence.Commands)
{ {
//command.Match(x => context.Stack.Push(x), command.Match(x => context.Stack.Push(x),
// x => x.Run(context)); x =>
{
if (!hasRunStackClearingCommand)
{
/*
* The first stack-clearing operator, which must be one of hstem, hstemhm, vstem, vstemhm, cntrmask, hintmask, hmoveto, vmoveto,
* rmoveto, or endchar, takes an additional argument — the width (as described earlier), which may be expressed as zero or one numeric argument.
*/
hasRunStackClearingCommand = true;
switch (x.Name)
{
case "hstem":
case "hstemhm":
case "vstemhm":
case "vstem":
{
var oddArgCount = context.Stack.Length % 2 != 0;
if (oddArgCount)
{
context.Width = context.Stack.PopBottom();
}
break;
}
case "hmoveto":
case "vmoveto":
SetWidthFromArgumentsIfPresent(context, 1);
break;
case "rmoveto":
SetWidthFromArgumentsIfPresent(context, 2);
break;
case "cntrmask":
case "hintmask":
case "endchar:":
SetWidthFromArgumentsIfPresent(context, 0);
break;
default:
hasRunStackClearingCommand = false;
break;
}
}
x.Run(context);
});
} }
throw new NotImplementedException(); return context.Path;
}
private static void SetWidthFromArgumentsIfPresent(Type2BuildCharContext context, int expectedArgumentLength)
{
if (context.Stack.Length > expectedArgumentLength)
{
context.Width = context.Stack.PopBottom();
}
} }
public class CommandSequence public class CommandSequence

View File

@@ -2,6 +2,7 @@
{ {
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Linq; using System.Linq;
using Charsets; using Charsets;
using CharStrings; using CharStrings;
@@ -147,6 +148,15 @@
throw new ArgumentOutOfRangeException($"Unexpected CharString type in CFF font: {topDictionary.CharStringType}."); throw new ArgumentOutOfRangeException($"Unexpected CharString type in CFF font: {topDictionary.CharStringType}.");
} }
if (Debugger.IsAttached)
{
foreach (var pair in charStrings.CharStrings)
{
var path = Type2CharStrings.Run(pair.Value);
var svg = path.ToFullSvg();
}
}
return new CompactFontFormatFont(topDictionary, privateDictionary, charset, Union<Type1CharStrings, Type2CharStrings>.Two(charStrings)); return new CompactFontFormatFont(topDictionary, privateDictionary, charset, Union<Type1CharStrings, Type2CharStrings>.Two(charStrings));
} }