Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add font width test case #17

Merged
merged 1 commit into from
Jun 8, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package de.rototor.pdfbox.graphics2d;

import org.junit.Test;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType0Font;

import java.awt.*;
import java.io.IOException;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

public class FontWidthDiscrepancyTest extends PdfBoxGraphics2DTestBase {
@Test
public void testAnonioFontWidth() throws IOException, FontFormatException
{
final PDDocument doc = new PDDocument();
final String testString = "MMMMMMMMMMMMMMMMMMMMMM";
final float fontSize = 20f;
final Font antonioRegular = Font
.createFont(Font.TRUETYPE_FONT,
PdfBoxGraphics2dTest.class.getResourceAsStream("antonio/Antonio-Regular.ttf"))
.deriveFont(fontSize);
final PDFont pdFont = PDType0Font
.load(doc, PdfBoxGraphics2dTest.class.getResourceAsStream("antonio/Antonio-Regular.ttf"));

final Graphics2D gfx = new PdfBoxGraphics2D(doc, 400, 400);

float pdfWidth = pdFont.getStringWidth(testString) / 1000 * fontSize;
float gfxWidth = gfx.getFontMetrics(antonioRegular).stringWidth(testString);

gfx.dispose();

assertEquals(pdfWidth, gfxWidth, 1.0);

}

}