Skip to content

Commit

Permalink
Get correct text orientation when base line points are equal and fix #…
Browse files Browse the repository at this point in the history
  • Loading branch information
BobLd committed Sep 29, 2024
1 parent 4845f43 commit 689c127
Showing 1 changed file with 43 additions and 2 deletions.
45 changes: 43 additions & 2 deletions src/UglyToad.PdfPig/Content/Letter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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}'.");
}

/// <summary>
/// Produces a string representation of the letter and its position.
/// </summary>
Expand Down

0 comments on commit 689c127

Please sign in to comment.