mirror of
https://github.com/UglyToad/PdfPig.git
synced 2025-10-14 10:55:04 +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:
@@ -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 =>
|
||||
{
|
||||
var name = encoding.GetName(characterCode);
|
||||
return x.GetCharacterBoundingBox(name);
|
||||
},
|
||||
x =>
|
||||
{
|
||||
var first = x.Fonts.First().Value;
|
||||
PdfRectangle? rect = null;
|
||||
if (fontProgram.TryGetFirst(out var t1Font))
|
||||
{
|
||||
var name = encoding.GetName(characterCode);
|
||||
rect = t1Font.GetCharacterBoundingBox(name);
|
||||
}
|
||||
else if (fontProgram.TryGetSecond(out var cffFont))
|
||||
{
|
||||
var first = cffFont.FirstFont;
|
||||
string characterName;
|
||||
if (encoding != null)
|
||||
{
|
||||
@@ -178,11 +192,12 @@
|
||||
}
|
||||
else
|
||||
{
|
||||
characterName = x.GetCharacterName(characterCode);
|
||||
characterName = cffFont.GetCharacterName(characterCode);
|
||||
}
|
||||
|
||||
return first.GetCharacterBoundingBox(characterName);
|
||||
});
|
||||
rect = first.GetCharacterBoundingBox(characterName);
|
||||
}
|
||||
|
||||
|
||||
if (!rect.HasValue)
|
||||
{
|
||||
|
Reference in New Issue
Block a user