Skip to content

Commit

Permalink
#47 Testcase for transformed fonts.
Browse files Browse the repository at this point in the history
  • Loading branch information
rototor committed Jan 8, 2023
1 parent c7eb8e6 commit 1195e62
Showing 1 changed file with 34 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.awt.*;
import java.awt.font.TextAttribute;
import java.awt.geom.AffineTransform;
import java.io.IOException;
import java.text.AttributedString;

Expand All @@ -13,7 +14,7 @@ public class FontTest extends PdfBoxGraphics2DTestBase
public void testAntonioFont() throws IOException, FontFormatException
{
final Font antonioRegular = Font.createFont(Font.TRUETYPE_FONT,
PdfBoxGraphics2dTest.class.getResourceAsStream("antonio/Antonio-Regular.ttf"))
PdfBoxGraphics2dTest.class.getResourceAsStream("antonio/Antonio-Regular.ttf"))
.deriveFont(15f);
exportGraphic("fonts", "antonio", new GraphicsExporter()
{
Expand All @@ -31,7 +32,7 @@ public void draw(Graphics2D gfx) throws IOException, FontFormatException
public void testStyledAttributeIterator() throws IOException, FontFormatException
{
final Font antonioRegular = Font.createFont(Font.TRUETYPE_FONT,
PdfBoxGraphics2dTest.class.getResourceAsStream("antonio/Antonio-Regular.ttf"))
PdfBoxGraphics2dTest.class.getResourceAsStream("antonio/Antonio-Regular.ttf"))
.deriveFont(15f);
exportGraphic("fonts", "attributed_text", new GraphicsExporter()
{
Expand Down Expand Up @@ -59,7 +60,7 @@ public void draw(Graphics2D gfx) throws IOException, FontFormatException

Font font = new Font("SansSerif", Font.PLAIN, 12);
Font font2 = Font.createFont(Font.TRUETYPE_FONT,
PdfBoxGraphics2dTest.class.getResourceAsStream("DejaVuSerifCondensed.ttf"))
PdfBoxGraphics2dTest.class.getResourceAsStream("DejaVuSerifCondensed.ttf"))
.deriveFont(13f);
str.addAttribute(TextAttribute.FONT, font);
gfx.drawString(str.getIterator(), 10, 100);
Expand All @@ -68,4 +69,34 @@ public void draw(Graphics2D gfx) throws IOException, FontFormatException
}
});
}

@Test
public void testTransformedFont() throws IOException, FontFormatException
{
final Font antonioRegular = Font.createFont(Font.TRUETYPE_FONT,
PdfBoxGraphics2dTest.class.getResourceAsStream("antonio/Antonio-Regular.ttf"))
.deriveFont(15f);
exportGraphic("fonts", "transformed", new GraphicsExporter()
{
@Override
public void draw(Graphics2D gfx) throws IOException, FontFormatException
{
AffineTransform affineTransform = antonioRegular.getTransform();
affineTransform.rotate(Math.toRadians(-90), 0, 0);
Font rotatedFont = antonioRegular.deriveFont(affineTransform);
gfx.setColor(Color.BLACK);
gfx.setFont(rotatedFont);
gfx.drawString("Some sample text", 50, 150);

AffineTransform saveTF = gfx.getTransform();
AffineTransform at = AffineTransform.getTranslateInstance(100, 150);
at.rotate(Math.toRadians(-90), 0, 0);
gfx.setColor(Color.RED);
gfx.setFont(antonioRegular);
gfx.setTransform(at);
gfx.drawString("Some sample text", 0, 0);
gfx.setTransform(saveTF);
}
});
}
}

0 comments on commit 1195e62

Please sign in to comment.