From 689c127cd91fe9bc0255eaec040bef112f74c779 Mon Sep 17 00:00:00 2001 From: BobLd <38405645+BobLd@users.noreply.github.com> Date: Thu, 8 Aug 2024 13:53:24 +0100 Subject: [PATCH] Get correct text orientation when base line points are equal and fix #741 --- src/UglyToad.PdfPig/Content/Letter.cs | 45 +++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/src/UglyToad.PdfPig/Content/Letter.cs b/src/UglyToad.PdfPig/Content/Letter.cs index 3ee0222f..9d998499 100644 --- a/src/UglyToad.PdfPig/Content/Letter.cs +++ b/src/UglyToad.PdfPig/Content/Letter.cs @@ -137,8 +137,14 @@ private TextOrientation GetTextOrientation() { - if (System.Math.Abs(StartBaseLine.Y - EndBaseLine.Y) < 10e-5) + if (Math.Abs(StartBaseLine.Y - EndBaseLine.Y) < 10e-5) { + if (Math.Abs(StartBaseLine.X - EndBaseLine.X) < 10e-5) + { + // Start and End point are the same + return GetTextOrientationRot(); + } + if (StartBaseLine.X > EndBaseLine.X) { return TextOrientation.Rotate180; @@ -147,8 +153,14 @@ return TextOrientation.Horizontal; } - if (System.Math.Abs(StartBaseLine.X - EndBaseLine.X) < 10e-5) + if (Math.Abs(StartBaseLine.X - EndBaseLine.X) < 10e-5) { + if (Math.Abs(StartBaseLine.Y - EndBaseLine.Y) < 10e-5) + { + // Start and End point are the same + return GetTextOrientationRot(); + } + if (StartBaseLine.Y > EndBaseLine.Y) { return TextOrientation.Rotate90; @@ -160,6 +172,35 @@ return TextOrientation.Other; } + private TextOrientation GetTextOrientationRot() + { + double rotation = GlyphRectangle.Rotation; + int rotationInt = (int)Math.Round(rotation, MidpointRounding.AwayFromZero); + + if (Math.Abs(rotation - rotationInt) >= 10e-5) + { + return TextOrientation.Other; + } + + switch (rotationInt) + { + case 0: + return TextOrientation.Horizontal; + + case -90: + return TextOrientation.Rotate90; + + case 180: + case -180: + return TextOrientation.Rotate180; + + case 90: + return TextOrientation.Rotate270; + } + + throw new Exception($"Could not find TextOrientation for rotation '{rotation}'."); + } + /// /// Produces a string representation of the letter and its position. ///