Skip to content

Commit

Permalink
Merge pull request #36 from jonmccracken-wf/fixRotatedStrokeWidth
Browse files Browse the repository at this point in the history
Fix stroke width in for lines in rotated transform spaces
  • Loading branch information
rototor committed Apr 21, 2022
2 parents ba65fd5 + 56f1540 commit bcf8b8f
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import java.awt.geom.Line2D;
import java.awt.geom.NoninvertibleTransformException;
import java.awt.geom.Path2D;
import java.awt.geom.Point2D;
import java.awt.geom.PathIterator;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
Expand Down Expand Up @@ -287,7 +288,7 @@ public PDResources getResources()

/**
* *AFTER* you have disposed() this Graphics2D you can access the XForm
*
*
* @return the PDFormXObject which resulted in this graphics
*/
@SuppressWarnings("WeakerAccess")
Expand Down Expand Up @@ -530,16 +531,18 @@ private void applyStroke(Stroke strokeToApply) throws IOException
}

AffineTransform tf = getCurrentEffectiveTransform();
float lineWidth = calculateTransformedLength(basicStroke.getLineWidth(), tf);

contentStream.setLineWidth(lineWidth);

double scaleX = tf.getScaleX();
contentStream.setLineWidth((float) Math.abs(basicStroke.getLineWidth() * scaleX));
float[] dashArray = basicStroke.getDashArray();
if (dashArray != null)
{
for (int i = 0; i < dashArray.length; i++)
dashArray[i] = (float) Math.abs(dashArray[i] * scaleX);
dashArray[i] = calculateTransformedLength(dashArray[i], tf);

contentStream.setLineDashPattern(dashArray,
(float) Math.abs(basicStroke.getDashPhase() * scaleX));
calculateTransformedLength(basicStroke.getDashPhase(), tf));
}
}
else if (strokeToApply != null)
Expand All @@ -549,6 +552,15 @@ else if (strokeToApply != null)
}
}

private float calculateTransformedLength(float length, AffineTransform tf) {
// Represent stroke width as a horizontal line from origin to basicStroke.LineWidth.
Point2D.Float lengthVector = new Point2D.Float(length, 0);
// Apply the current transform to the horizontal line.
tf.deltaTransform(lengthVector, lengthVector);
// Calculate the length of the transformed line. This is the new, adjusted length.
return (float) Math.sqrt(lengthVector.x * lengthVector.x + lengthVector.y * lengthVector.y);
}

private AffineTransform getCurrentEffectiveTransform()
{
AffineTransform tf = new AffineTransform();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ public void testSVGs() throws IOException
renderSVG("watermark.svg", 0.4);
}

@Test
public void testRotatedStrokes() throws IOException
{
renderSVG("strokeRotation.svg", 0.55);
renderSVG("dashedStrokeRotation.svg", 0.55);
}

@Test
public void renderFailureCases() throws IOException
{
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit bcf8b8f

Please sign in to comment.