use tryget rather than lambdas for union type

avoid the allocations caused by lambda expressions for performance reasons.
This commit is contained in:
Eliot Jones
2020-02-28 16:02:20 +00:00
parent 4d911fb9d1
commit 4442a69a97
9 changed files with 141 additions and 55 deletions

View File

@@ -48,21 +48,26 @@
var defaultWidthX = GetDefaultWidthX(characterName);
var nominalWidthX = GetNominalWidthX(characterName);
var result = CharStrings.Match(x => throw new NotImplementedException("Type 1 CharStrings in a CFF font are currently unsupported."),
x =>
{
var glyph = x.Generate(characterName, (double)defaultWidthX, (double)nominalWidthX);
var rectangle = glyph.Path.GetBoundingRectangle();
if (rectangle.HasValue)
{
return rectangle;
}
if (CharStrings.TryGetFirst(out var _))
{
throw new NotImplementedException("Type 1 CharStrings in a CFF font are currently unsupported.");
}
var defaultBoundingBox = TopDictionary.FontBoundingBox;
return new PdfRectangle(0, 0, glyph.Width.GetValueOrDefault(), defaultBoundingBox.Height);
});
if (!CharStrings.TryGetSecond(out var type2CharStrings))
{
return null;
}
var glyph = type2CharStrings.Generate(characterName, (double)defaultWidthX, (double)nominalWidthX);
var rectangle = glyph.Path.GetBoundingRectangle();
if (rectangle.HasValue)
{
return rectangle;
}
var defaultBoundingBox = TopDictionary.FontBoundingBox;
return new PdfRectangle(0, 0, glyph.Width.GetValueOrDefault(), defaultBoundingBox.Height);
return result;
}
/// <summary>
@@ -88,8 +93,8 @@
public IReadOnlyList<CompactFontFormatPrivateDictionary> PrivateDictionaries { get; }
public ICompactFontFormatFdSelect FdSelect { get; }
public CompactFontFormatCidFont(CompactFontFormatTopLevelDictionary topDictionary, CompactFontFormatPrivateDictionary privateDictionary,
ICompactFontFormatCharset charset,
public CompactFontFormatCidFont(CompactFontFormatTopLevelDictionary topDictionary, CompactFontFormatPrivateDictionary privateDictionary,
ICompactFontFormatCharset charset,
Union<Type1CharStrings, Type2CharStrings> charStrings,
IReadOnlyList<CompactFontFormatTopLevelDictionary> fontDictionaries,
IReadOnlyList<CompactFontFormatPrivateDictionary> privateDictionaries,

View File

@@ -2,7 +2,6 @@
{
using System;
using System.Collections.Generic;
using System.Linq;
using Core;
/// <summary>
@@ -21,6 +20,11 @@
/// </summary>
public IReadOnlyDictionary<string, CompactFontFormatFont> Fonts { get; }
/// <summary>
/// The first font contained in the collection.
/// </summary>
public CompactFontFormatFont FirstFont { get; }
/// <summary>
/// Create a new <see cref="CompactFontFormatFontCollection"/>.
/// </summary>
@@ -30,6 +34,11 @@
{
Header = header;
Fonts = fontSet ?? throw new ArgumentNullException(nameof(fontSet));
foreach (var pair in fontSet)
{
FirstFont = pair.Value;
break;
}
}
/// <summary>
@@ -50,7 +59,7 @@
/// </summary>
public PdfRectangle? GetCharacterBoundingBox(string characterName)
{
return Fonts.First().Value.GetCharacterBoundingBox(characterName);
return FirstFont.GetCharacterBoundingBox(characterName);
}
/// <summary>
@@ -58,7 +67,7 @@
/// </summary>
public string GetCharacterName(int characterCode)
{
var font = Fonts.First().Value;
var font = FirstFont;
if (font.Encoding != null)
{

View File

@@ -23,8 +23,14 @@
foreach (var command in subroutine.Commands)
{
command.Match(x => context.Stack.Push(x),
x => x.Run(context));
if (command.TryGetFirst(out var num))
{
context.Stack.Push(num);
}
else if (command.TryGetSecond(out var lazyCommand))
{
lazyCommand.Run(context);
}
}
}
}

View File

@@ -92,8 +92,14 @@
foreach (var command in sequence.Commands)
{
command.Match(x => context.Stack.Push(x),
x => x.Run(context));
if (command.TryGetFirst(out var num))
{
context.Stack.Push(num);
}
else if (command.TryGetSecond(out var lazyCommand))
{
lazyCommand.Run(context);
}
}
return context.Path;