mirror of
https://github.com/UglyToad/PdfPig.git
synced 2025-09-20 03:17:57 +08:00
use tryget rather than lambdas for union type
avoid the allocations caused by lambda expressions for performance reasons.
This commit is contained in:
@@ -19,6 +19,16 @@ namespace UglyToad.PdfPig.Core
|
||||
/// </summary>
|
||||
public abstract TResult Match<TResult>(Func<A, TResult> first, Func<B, TResult> second);
|
||||
|
||||
/// <summary>
|
||||
/// Get the item if it is of the specific type.
|
||||
/// </summary>
|
||||
public abstract bool TryGetFirst(out A a);
|
||||
|
||||
/// <summary>
|
||||
/// Get the item if it is of the specific type.
|
||||
/// </summary>
|
||||
public abstract bool TryGetSecond(out B b);
|
||||
|
||||
private Union() { }
|
||||
|
||||
/// <summary>
|
||||
@@ -69,6 +79,20 @@ namespace UglyToad.PdfPig.Core
|
||||
return first(Item);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override bool TryGetFirst(out A a)
|
||||
{
|
||||
a = Item;
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override bool TryGetSecond(out B b)
|
||||
{
|
||||
b = default(B);
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string ToString()
|
||||
{
|
||||
@@ -108,6 +132,20 @@ namespace UglyToad.PdfPig.Core
|
||||
return second(Item);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override bool TryGetFirst(out A a)
|
||||
{
|
||||
a = default(A);
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override bool TryGetSecond(out B b)
|
||||
{
|
||||
b = Item;
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string ToString()
|
||||
{
|
||||
|
@@ -48,10 +48,17 @@
|
||||
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 =>
|
||||
if (CharStrings.TryGetFirst(out var _))
|
||||
{
|
||||
var glyph = x.Generate(characterName, (double)defaultWidthX, (double)nominalWidthX);
|
||||
throw new NotImplementedException("Type 1 CharStrings in a CFF font are currently unsupported.");
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
@@ -60,9 +67,7 @@
|
||||
|
||||
var defaultBoundingBox = TopDictionary.FontBoundingBox;
|
||||
return new PdfRectangle(0, 0, glyph.Width.GetValueOrDefault(), defaultBoundingBox.Height);
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@@ -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)
|
||||
{
|
||||
|
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -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;
|
||||
|
@@ -54,10 +54,14 @@
|
||||
{
|
||||
foreach (var image in images)
|
||||
{
|
||||
var result = image.Match<IPdfImage>(x => XObjectFactory.ReadImage(x, pdfScanner, filterProvider, resourceStore),
|
||||
x => x);
|
||||
|
||||
yield return result;
|
||||
if (image.TryGetFirst(out var xObjectContentRecord))
|
||||
{
|
||||
yield return XObjectFactory.ReadImage(xObjectContentRecord, pdfScanner, filterProvider, resourceStore);
|
||||
}
|
||||
else if (image.TryGetSecond(out var inlineImage))
|
||||
{
|
||||
yield return inlineImage;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -71,7 +71,7 @@
|
||||
throw new NotSupportedException("Multiple fonts in a CFF");
|
||||
}
|
||||
#endif
|
||||
return fontCollection.Fonts.First().Value;
|
||||
return fontCollection.FirstFont;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,5 @@
|
||||
namespace UglyToad.PdfPig.PdfFonts.Parser.Handlers
|
||||
{
|
||||
using System.Linq;
|
||||
using Cmap;
|
||||
using Core;
|
||||
using Filters;
|
||||
@@ -101,21 +100,25 @@
|
||||
}
|
||||
}
|
||||
|
||||
Encoding fromFont = font?.Match(x => x.Encoding != null ? new BuiltInEncoding(x.Encoding) : default(Encoding), x =>
|
||||
|
||||
var fromFont = default(Encoding);
|
||||
if (font != null)
|
||||
{
|
||||
if (x.Fonts != null && x.Fonts.Count > 0)
|
||||
if (font.TryGetFirst(out var t1Font))
|
||||
{
|
||||
return x.Fonts.First().Value.Encoding;
|
||||
fromFont = t1Font.Encoding != null ? new BuiltInEncoding(t1Font.Encoding) : default(Encoding);
|
||||
}
|
||||
else if (font.TryGetSecond(out var cffFont))
|
||||
{
|
||||
fromFont = cffFont.FirstFont?.Encoding;
|
||||
}
|
||||
}
|
||||
|
||||
return default(Encoding);
|
||||
});
|
||||
var encoding = encodingReader.Read(dictionary, descriptor, fromFont);
|
||||
|
||||
Encoding encoding = encodingReader.Read(dictionary, descriptor, fromFont);
|
||||
|
||||
if (encoding == null)
|
||||
if (encoding == null && font != null && font.TryGetFirst(out var t1FontReplacment))
|
||||
{
|
||||
font?.Match(x => encoding = new BuiltInEncoding(x.Encoding), _ => { });
|
||||
encoding = new BuiltInEncoding(t1FontReplacment.Encoding);
|
||||
}
|
||||
|
||||
return new Type1FontSimple(name, firstCharacter, lastCharacter, widths, descriptor, encoding, toUnicodeCMap, font);
|
||||
|
@@ -1,7 +1,6 @@
|
||||
namespace UglyToad.PdfPig.PdfFonts.Simple
|
||||
{
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Cmap;
|
||||
using Composite;
|
||||
using Core;
|
||||
@@ -17,6 +16,8 @@
|
||||
/// </summary>
|
||||
internal class Type1FontSimple : IFont
|
||||
{
|
||||
private static readonly TransformationMatrix DefaultTransformationMatrix = TransformationMatrix.FromValues(0.001, 0, 0, 0.001, 0, 0);
|
||||
|
||||
private readonly Dictionary<int, CharacterBoundingBox> cachedBoundingBoxes = new Dictionary<int, CharacterBoundingBox>();
|
||||
|
||||
private readonly int firstChar;
|
||||
@@ -52,8 +53,19 @@
|
||||
this.fontProgram = fontProgram;
|
||||
this.toUnicodeCMap = new ToUnicodeCMap(toUnicodeCMap);
|
||||
|
||||
var matrix = TransformationMatrix.FromValues(0.001, 0, 0, 0.001, 0, 0);
|
||||
fontProgram?.Match(x => matrix = x.FontMatrix, x => { matrix = x.GetFirstTransformationMatrix(); });
|
||||
var matrix = DefaultTransformationMatrix;
|
||||
|
||||
if (fontProgram != null)
|
||||
{
|
||||
if (fontProgram.TryGetFirst(out var t1Font))
|
||||
{
|
||||
matrix = t1Font.FontMatrix;
|
||||
}
|
||||
else if (fontProgram.TryGetSecond(out var cffFont))
|
||||
{
|
||||
matrix = cffFont.GetFirstTransformationMatrix();
|
||||
}
|
||||
}
|
||||
|
||||
fontMatrix = matrix;
|
||||
|
||||
@@ -90,10 +102,11 @@
|
||||
}
|
||||
|
||||
var containsEncoding = false;
|
||||
var capturedValue = default(string);
|
||||
fontProgram.Match(x => { containsEncoding = x.Encoding.TryGetValue(characterCode, out capturedValue); },
|
||||
_ => {});
|
||||
value = capturedValue;
|
||||
if (fontProgram.TryGetFirst(out var t1Font))
|
||||
{
|
||||
containsEncoding = t1Font.Encoding.TryGetValue(characterCode, out value);
|
||||
}
|
||||
|
||||
return containsEncoding;
|
||||
}
|
||||
}
|
||||
@@ -163,14 +176,15 @@
|
||||
return new PdfRectangle(0, 0, widths[characterCode - firstChar], 0);
|
||||
}
|
||||
|
||||
var rect = fontProgram.Match(x =>
|
||||
PdfRectangle? rect = null;
|
||||
if (fontProgram.TryGetFirst(out var t1Font))
|
||||
{
|
||||
var name = encoding.GetName(characterCode);
|
||||
return x.GetCharacterBoundingBox(name);
|
||||
},
|
||||
x =>
|
||||
rect = t1Font.GetCharacterBoundingBox(name);
|
||||
}
|
||||
else if (fontProgram.TryGetSecond(out var cffFont))
|
||||
{
|
||||
var first = x.Fonts.First().Value;
|
||||
var first = cffFont.FirstFont;
|
||||
string characterName;
|
||||
if (encoding != null)
|
||||
{
|
||||
@@ -178,11 +192,12 @@
|
||||
}
|
||||
else
|
||||
{
|
||||
characterName = x.GetCharacterName(characterCode);
|
||||
characterName = cffFont.GetCharacterName(characterCode);
|
||||
}
|
||||
|
||||
rect = first.GetCharacterBoundingBox(characterName);
|
||||
}
|
||||
|
||||
return first.GetCharacterBoundingBox(characterName);
|
||||
});
|
||||
|
||||
if (!rect.HasValue)
|
||||
{
|
||||
|
Reference in New Issue
Block a user