fix bounding boxes for type 0 truetype fonts

This commit is contained in:
Eliot Jones
2018-04-16 23:58:27 +01:00
parent 53f693bc77
commit 8d591ca913
5 changed files with 14 additions and 5 deletions

View File

@@ -13,5 +13,7 @@
bool TryGetBoundingBox(int characterCode, Func<int, int> characterIdentifierToGlyphIndex, out PdfRectangle boundingBox);
bool TryGetBoundingAdvancedWidth(int characterCode, out decimal width);
int GetFontMatrixMultiplier();
}
}

View File

@@ -42,7 +42,7 @@
this.cidToGid = cidToGid;
// TODO: This should maybe take units per em into account?
var scale = 1 / 1000m;
var scale = 1 / (decimal)(fontProgram?.GetFontMatrixMultiplier() ?? 1000);
FontMatrix = TransformationMatrix.FromValues(scale, 0, 0, scale, 0, 0);
}

View File

@@ -30,8 +30,6 @@
public bool IsVertical => CMap.WritingMode == WritingMode.Vertical;
private readonly TransformationMatrix fontMatrix = TransformationMatrix.FromValues(0.001m, 0, 0, 0.001m, 0, 0);
public Type0Font(NameToken baseFont, ICidFont cidFont, CMap cmap, CMap toUnicodeCMap)
{
BaseFont = baseFont ?? throw new ArgumentNullException(nameof(baseFont));
@@ -73,7 +71,11 @@
public PdfRectangle GetBoundingBox(int characterCode)
{
return fontMatrix.Transform(GetBoundingBoxInGlyphSpace(characterCode));
var matrix = GetFontMatrix();
var boundingBox = GetBoundingBoxInGlyphSpace(characterCode);
return matrix.Transform(boundingBox);
}
public decimal GetWidth(int characterCode)

View File

@@ -137,7 +137,7 @@ namespace UglyToad.PdfPig.Fonts.Simple
if (font?.HeaderTable != null)
{
scale = font.HeaderTable.UnitsPerEm;
scale = font.GetFontMatrixMultiplier();
}
return TransformationMatrix.FromValues(1m / scale, 0, 0, 1m / scale, 0, 0);

View File

@@ -69,6 +69,11 @@
return TryGetBoundingAdvancedWidthByIndex(index, out width);
}
public int GetFontMatrixMultiplier()
{
return HeaderTable?.UnitsPerEm ?? 1000;
}
private bool TryGetBoundingAdvancedWidthByIndex(int index, out decimal width)
{
width = TableRegister.HorizontalMetricsTable.GetAdvanceWidth(index);