diff --git a/src/UglyToad.PdfPig/Content/Letter.cs b/src/UglyToad.PdfPig/Content/Letter.cs index 3ee0222f7..9d9984999 100644 --- a/src/UglyToad.PdfPig/Content/Letter.cs +++ b/src/UglyToad.PdfPig/Content/Letter.cs @@ -137,8 +137,14 @@ public Letter(string value, PdfRectangle glyphRectangle, 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 @@ private TextOrientation GetTextOrientation() 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 @@ private TextOrientation GetTextOrientation() 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. ///