Fix glyph bounding box for Type3 font with zero height (#610)

* For Type3 font with a zero width/height bounding box, set it to a sensible
default using the font matrix. This ensures the letter bounding boxes will
not have height 0.

* Also added a test to check for non-zero height in the sample Type3 PDF

* Prevent division by zero error

---------

Co-authored-by: mvantzet <mark@radialsg.com>
This commit is contained in:
mvantzet
2023-04-19 09:55:34 +02:00
committed by GitHub
parent 2d72d62c59
commit 147b8997cc
5 changed files with 2364 additions and 0 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 70 KiB

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,24 @@
namespace UglyToad.PdfPig.Tests.Integration
{
using Xunit;
public class Type3FontTests
{
private static string GetFilename()
{
return IntegrationHelpers.GetDocumentPath("type3-font-zero-height.pdf");
}
[Fact]
public void HasLetterWidthsAndHeights()
{
using (var document = PdfDocument.Open(GetFilename()))
{
var page = document.GetPage(1);
Assert.Contains(page.Letters, x => x.GlyphRectangle.Width != 0);
Assert.Contains(page.Letters, x => x.GlyphRectangle.Height != 0);
}
}
}
}

View File

@@ -22,6 +22,7 @@
private const string SPARCv9ArchitectureManual = "SPARC - v9 Architecture Manual";
private const string CroppedAndRotatedFile = "cropped-and-rotated";
private const string MOZILLA_10372_2File = "MOZILLA-10372-2";
private const string Type3FontZeroHeight = "type3-font-zero-height";
private static string GetFilename(string name)
{
@@ -137,6 +138,12 @@
Run(MOZILLA_10372_2File, 1584, 7);
}
[Fact]
public void Type3FontZeroHeightTest()
{
Run(Type3FontZeroHeight, 1255);
}
private static void Run(string file, int imageHeight = 792, int pageNo = 1)
{
var pdfFileName = GetFilename(file);

View File

@@ -31,6 +31,12 @@
var fontMatrix = GetFontMatrix(dictionary);
if (boundingBox.Left == 0 && boundingBox.Bottom == 0 && boundingBox.Height == 0 && boundingBox.Width == 0
&& fontMatrix.A != 0 && fontMatrix.D != 0)
{
boundingBox = new PdfRectangle(0, 0, 1 / fontMatrix.A, 1 / fontMatrix.D);
}
var firstCharacter = FontDictionaryAccessHelper.GetFirstCharacter(dictionary);
var lastCharacter = FontDictionaryAccessHelper.GetLastCharacter(dictionary);
var widths = FontDictionaryAccessHelper.GetWidths(scanner, dictionary);