Fix Type 3 font height and fix #500 (#585)

This commit is contained in:
BobLd
2023-03-26 19:20:20 +01:00
committed by GitHub
parent 58b3394d01
commit 93a2f318aa
4 changed files with 19 additions and 10 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 221 KiB

View File

@@ -1,9 +1,9 @@
namespace UglyToad.PdfPig.Tests.Integration.VisualVerification namespace UglyToad.PdfPig.Tests.Integration.VisualVerification
{ {
using PdfPig.Core;
using System; using System;
using System.Drawing; using System.Drawing;
using System.IO; using System.IO;
using PdfPig.Core;
using Xunit; using Xunit;
public class GenerateLetterBoundingBoxImages public class GenerateLetterBoundingBoxImages
@@ -21,6 +21,7 @@
private const string SinglePage270ClockwiseRotation = "SinglePage270ClockwiseRotation - from PdfPig"; private const string SinglePage270ClockwiseRotation = "SinglePage270ClockwiseRotation - from PdfPig";
private const string SPARCv9ArchitectureManual = "SPARC - v9 Architecture Manual"; private const string SPARCv9ArchitectureManual = "SPARC - v9 Architecture Manual";
private const string CroppedAndRotatedFile = "cropped-and-rotated"; private const string CroppedAndRotatedFile = "cropped-and-rotated";
private const string MOZILLA_10372_2File = "MOZILLA-10372-2";
private static string GetFilename(string name) private static string GetFilename(string name)
{ {
@@ -130,14 +131,22 @@
Run(CroppedAndRotatedFile, 205); Run(CroppedAndRotatedFile, 205);
} }
private static void Run(string file, int imageHeight = 792) [Fact]
public void MOZILLA_10372_2Test()
{
Run(MOZILLA_10372_2File, 1584, 7);
}
private static void Run(string file, int imageHeight = 792, int pageNo = 1)
{ {
var pdfFileName = GetFilename(file); var pdfFileName = GetFilename(file);
using (var document = PdfDocument.Open(pdfFileName)) using (var document = PdfDocument.Open(pdfFileName))
using (var image = GetCorrespondingImage(pdfFileName)) using (var image = GetCorrespondingImage(pdfFileName))
{ {
var page = document.GetPage(1); var page = document.GetPage(pageNo);
double scale = imageHeight / page.Height;
var violetPen = new Pen(Color.BlueViolet, 1); var violetPen = new Pen(Color.BlueViolet, 1);
var redPen = new Pen(Color.Crimson, 1); var redPen = new Pen(Color.Crimson, 1);
@@ -148,17 +157,17 @@
{ {
foreach (var word in page.GetWords()) foreach (var word in page.GetWords())
{ {
DrawRectangle(word.BoundingBox, graphics, redPen, imageHeight); DrawRectangle(word.BoundingBox, graphics, redPen, imageHeight, scale);
} }
foreach (var letter in page.Letters) foreach (var letter in page.Letters)
{ {
DrawRectangle(letter.GlyphRectangle, graphics, violetPen, imageHeight); DrawRectangle(letter.GlyphRectangle, graphics, violetPen, imageHeight, scale);
} }
foreach (var annotation in page.ExperimentalAccess.GetAnnotations()) foreach (var annotation in page.ExperimentalAccess.GetAnnotations())
{ {
DrawRectangle(annotation.Rectangle, graphics, bluePen, imageHeight); DrawRectangle(annotation.Rectangle, graphics, bluePen, imageHeight, scale);
} }
var imageName = $"{file}.jpg"; var imageName = $"{file}.jpg";
@@ -176,16 +185,16 @@
} }
private static void DrawRectangle(PdfRectangle rectangle, Graphics graphics, Pen pen, private static void DrawRectangle(PdfRectangle rectangle, Graphics graphics, Pen pen,
int imageHeight) int imageHeight, double scale)
{ {
int GetY(PdfPoint p) int GetY(PdfPoint p)
{ {
return imageHeight - (int) p.Y; return imageHeight - (int)(p.Y * scale);
} }
Point GetPoint(PdfPoint p) Point GetPoint(PdfPoint p)
{ {
return new Point((int)p.X, GetY(p)); return new Point((int)(p.X * scale), GetY(p));
} }
graphics.DrawLine(pen, GetPoint(rectangle.BottomLeft), GetPoint(rectangle.BottomRight)); graphics.DrawLine(pen, GetPoint(rectangle.BottomLeft), GetPoint(rectangle.BottomRight));

View File

@@ -81,7 +81,7 @@
throw new InvalidFontFormatException($"The character code was not contained in the widths array: {characterCode}."); throw new InvalidFontFormatException($"The character code was not contained in the widths array: {characterCode}.");
} }
return new PdfRectangle(0, 0, widths[characterCode - firstChar], 0); return new PdfRectangle(0, 0, widths[characterCode - firstChar], boundingBox.Height);
} }
public TransformationMatrix GetFontMatrix() public TransformationMatrix GetFontMatrix()