Skip to content

Commit

Permalink
Improve PdfFont#getDescent and PdfFont#getAscent return float instead…
Browse files Browse the repository at this point in the history
… of int

DEVSIX-6048
  • Loading branch information
introfog committed Mar 2, 2023
1 parent c742920 commit c4ecd12
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 27 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
18 changes: 9 additions & 9 deletions kernel/src/main/java/com/itextpdf/kernel/font/PdfFont.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ This file is part of the iText (R) project.
import com.itextpdf.io.font.otf.Glyph;
import com.itextpdf.io.font.otf.GlyphLine;
import com.itextpdf.io.util.TextUtil;
import com.itextpdf.kernel.exceptions.PdfException;
import com.itextpdf.kernel.exceptions.KernelExceptionMessageConstant;
import com.itextpdf.kernel.exceptions.PdfException;
import com.itextpdf.kernel.pdf.PdfDictionary;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfName;
Expand Down Expand Up @@ -281,7 +281,7 @@ public float getWidth(String text, float fontSize) {
*
* @return the descent in points
*/
public int getDescent(String text, float fontSize) {
public float getDescent(String text, float fontSize) {
int min = 0;
for (int k = 0; k < text.length(); ++k) {
int ch;
Expand All @@ -301,7 +301,7 @@ public int getDescent(String text, float fontSize) {
}
}
}
return (int) FontProgram.convertTextSpaceToGlyphSpace(min * fontSize);
return FontProgram.convertTextSpaceToGlyphSpace(min * fontSize);
}

/**
Expand All @@ -313,7 +313,7 @@ public int getDescent(String text, float fontSize) {
*
* @return the descent in points
*/
public int getDescent(int unicode, float fontSize) {
public float getDescent(int unicode, float fontSize) {
int min = 0;
Glyph glyph = getGlyph(unicode);
if (glyph == null) {
Expand All @@ -326,7 +326,7 @@ public int getDescent(int unicode, float fontSize) {
min = getFontProgram().getFontMetrics().getTypoDescender();
}

return (int) FontProgram.convertTextSpaceToGlyphSpace(min * fontSize);
return FontProgram.convertTextSpaceToGlyphSpace(min * fontSize);
}

/**
Expand All @@ -338,7 +338,7 @@ public int getDescent(int unicode, float fontSize) {
*
* @return the ascent in points
*/
public int getAscent(String text, float fontSize) {
public float getAscent(String text, float fontSize) {
int max = 0;
for (int k = 0; k < text.length(); ++k) {
int ch;
Expand All @@ -359,7 +359,7 @@ public int getAscent(String text, float fontSize) {
}
}

return (int) FontProgram.convertTextSpaceToGlyphSpace(max * fontSize);
return FontProgram.convertTextSpaceToGlyphSpace(max * fontSize);
}

/**
Expand All @@ -371,7 +371,7 @@ public int getAscent(String text, float fontSize) {
*
* @return the ascent in points
*/
public int getAscent(int unicode, float fontSize) {
public float getAscent(int unicode, float fontSize) {
int max = 0;
Glyph glyph = getGlyph(unicode);
if (glyph == null) {
Expand All @@ -384,7 +384,7 @@ public int getAscent(int unicode, float fontSize) {
max = getFontProgram().getFontMetrics().getTypoAscender();
}

return (int) FontProgram.convertTextSpaceToGlyphSpace(max * fontSize);
return FontProgram.convertTextSpaceToGlyphSpace(max * fontSize);
}

public FontProgram getFontProgram() {
Expand Down
36 changes: 18 additions & 18 deletions kernel/src/test/java/com/itextpdf/kernel/font/PdfFontUnitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -284,31 +284,31 @@ public void getDescentOfGlyphTest() {
TestFont font = new TestFont();

int expectedDescent = font.getGlyph(TestFont.SIMPLE_GLYPH).getBbox()[1];
int expectedValue = (int) (expectedDescent * FONT_SIZE / (double) FontProgram.UNITS_NORMALIZATION);
Assert.assertEquals(expectedValue, font.getDescent(TestFont.SIMPLE_GLYPH, FONT_SIZE));
float expectedValue = (float) (expectedDescent * FONT_SIZE / (float) FontProgram.UNITS_NORMALIZATION);
Assert.assertEquals(expectedValue, font.getDescent(TestFont.SIMPLE_GLYPH, FONT_SIZE), 0.1);
}

@Test
public void descentCannotBePositiveTest() {
TestFont font = new TestFont();

Assert.assertEquals(0, font.getDescent(TestFont.SIMPLE_GLYPH_WITH_POSITIVE_DESCENT, 50));
Assert.assertEquals(0, font.getDescent(TestFont.SIMPLE_GLYPH_WITH_POSITIVE_DESCENT, 50), 0.1);
}

@Test
public void getDescentOfUnknownGlyphTest() {
TestFont font = new TestFont();

Assert.assertEquals(0, font.getDescent(111, 50));
Assert.assertEquals(0, font.getDescent(111, 50), 0.1);
}

@Test
public void getDescentOfGlyphWithoutBBoxTest() {
TestFont font = new TestFont();
font.setFontProgram(new TestFontProgram());

int expectedValue = (int) (FONT_METRICS_DESCENT * FONT_SIZE / (double) FontProgram.UNITS_NORMALIZATION);
Assert.assertEquals(expectedValue, font.getDescent(TestFont.SIMPLE_GLYPH_WITHOUT_BBOX, FONT_SIZE));
float expectedValue = (float) (FONT_METRICS_DESCENT * FONT_SIZE / (double) FontProgram.UNITS_NORMALIZATION);
Assert.assertEquals(expectedValue, font.getDescent(TestFont.SIMPLE_GLYPH_WITHOUT_BBOX, FONT_SIZE), 0.1);
}

@Test
Expand All @@ -322,8 +322,8 @@ public void getDescentOfTextTest() {
String textAsString = new String(text);
int expectedMinDescent = Math.min(font.getGlyph(TestFont.SIMPLE_GLYPH).getBbox()[1],
font.getGlyph(TestFont.COMPLEX_GLYPH).getBbox()[1]);
int expectedValue = (int) (expectedMinDescent * FONT_SIZE / (double) FontProgram.UNITS_NORMALIZATION);
Assert.assertEquals(expectedValue, font.getDescent(textAsString, FONT_SIZE));
float expectedValue = (float) (expectedMinDescent * FONT_SIZE / (double) FontProgram.UNITS_NORMALIZATION);
Assert.assertEquals(expectedValue, font.getDescent(textAsString, FONT_SIZE), 0.1);
}

@Test
Expand All @@ -337,26 +337,26 @@ public void getDescentOfTextWithGlyphWithoutBBoxTest() {
String textAsString = new String(text);
int expectedMinDescent = Math.min(font.getGlyph(TestFont.SIMPLE_GLYPH).getBbox()[1],
FONT_METRICS_DESCENT);
int expectedValue = (int) (expectedMinDescent * FONT_SIZE / (double) FontProgram.UNITS_NORMALIZATION);
Assert.assertEquals(expectedValue, font.getDescent(textAsString, FONT_SIZE));
float expectedValue = (float) (expectedMinDescent * FONT_SIZE / (double) FontProgram.UNITS_NORMALIZATION);
Assert.assertEquals(expectedValue, font.getDescent(textAsString, FONT_SIZE), 0.1);
}

@Test
public void getAscentOfGlyphTest() {
TestFont font = new TestFont();

int expectedAscent = font.getGlyph(TestFont.SIMPLE_GLYPH).getBbox()[3];
int expectedValue = (int) (expectedAscent * FONT_SIZE / (double) FontProgram.UNITS_NORMALIZATION);
Assert.assertEquals(expectedValue, font.getAscent(TestFont.SIMPLE_GLYPH, FONT_SIZE));
float expectedValue = (float) (expectedAscent * FONT_SIZE / (double) FontProgram.UNITS_NORMALIZATION);
Assert.assertEquals(expectedValue, font.getAscent(TestFont.SIMPLE_GLYPH, FONT_SIZE), 0.1);
}

@Test
public void getAscentOfGlyphWithoutBBoxTest() {
TestFont font = new TestFont();
font.setFontProgram(new TestFontProgram());

int expectedValue = (int) (FONT_METRICS_ASCENT * FONT_SIZE / (double) FontProgram.UNITS_NORMALIZATION);
Assert.assertEquals(expectedValue, font.getAscent(TestFont.SIMPLE_GLYPH_WITHOUT_BBOX, FONT_SIZE));
float expectedValue = (float) (FONT_METRICS_ASCENT * FONT_SIZE / (double) FontProgram.UNITS_NORMALIZATION);
Assert.assertEquals(expectedValue, font.getAscent(TestFont.SIMPLE_GLYPH_WITHOUT_BBOX, FONT_SIZE), 0.1);
}

@Test
Expand All @@ -371,8 +371,8 @@ public void getAscentOfTextTest() {
int expectedMaxAscent = Math.max(
font.getGlyph(TestFont.SIMPLE_GLYPH).getBbox()[3],
font.getGlyph(TestFont.COMPLEX_GLYPH).getBbox()[3]);
int expectedValue = (int) (expectedMaxAscent * FONT_SIZE / (double) FontProgram.UNITS_NORMALIZATION);
Assert.assertEquals(expectedValue, font.getAscent(textAsString, FONT_SIZE));
float expectedValue = (float) (expectedMaxAscent * FONT_SIZE / (double) FontProgram.UNITS_NORMALIZATION);
Assert.assertEquals(expectedValue, font.getAscent(textAsString, FONT_SIZE), 0.1);
}

@Test
Expand All @@ -387,8 +387,8 @@ public void getAscentOfTextWithGlyphWithoutBBoxTest() {
int expectedMaxAscent = Math.max(
font.getGlyph(TestFont.SIMPLE_GLYPH).getBbox()[3],
FONT_METRICS_ASCENT);
int expectedValue = (int) (expectedMaxAscent * FONT_SIZE / (double) FontProgram.UNITS_NORMALIZATION);
Assert.assertEquals(expectedValue, font.getAscent(textAsString, FONT_SIZE));
float expectedValue = (float) (expectedMaxAscent * FONT_SIZE / (double) FontProgram.UNITS_NORMALIZATION);
Assert.assertEquals(expectedValue, font.getAscent(textAsString, FONT_SIZE), 0.1);
}

@Test
Expand Down

0 comments on commit c4ecd12

Please sign in to comment.