Skip to content
Closed
Show file tree
Hide file tree
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
155 changes: 52 additions & 103 deletions fontbox/src/test/java/org/apache/fontbox/afm/AFMParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,8 @@

package org.apache.fontbox.afm;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import org.apache.fontbox.util.BoundingBox;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
Expand All @@ -31,106 +27,72 @@
import java.util.List;
import java.util.Optional;

import org.apache.fontbox.util.BoundingBox;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
*
* @author Tilman Hausherr
*/
class AFMParserTest
{
class AFMParserTest {

public static final String HELVETICA_AFM = "src/test/resources/afm/Helvetica.afm";

@Test
void testStartFontMetrics() throws IOException
{
try
{
new AFMParser(new ByteArrayInputStream("huhu".getBytes(StandardCharsets.US_ASCII)))
.parse();
fail("The AFMParser should have thrown an IOException because of a missing "
+ AFMParser.START_FONT_METRICS);
}
catch (IOException e)
{
// expected exception
}
void testStartFontMetrics() {
assertThrows(IOException.class,
() -> new AFMParser(new ByteArrayInputStream("huhu".getBytes(StandardCharsets.US_ASCII))).parse(),
"The AFMParser should have thrown an IOException because of a missing " + AFMParser.START_FONT_METRICS);
}

@Test
void testEndFontMetrics() throws IOException
{
AFMParser parser = new AFMParser(
new FileInputStream("src/test/resources/afm/NoEndFontMetrics.afm"));
try
{
parser.parse();
fail("The AFMParser should have thrown an IOException because of a missing "
+ AFMParser.END_FONT_METRICS);
}
catch (IOException e)
{
assertTrue(e.getMessage().contains("Unknown AFM key"));
}
void testEndFontMetrics() throws IOException {
AFMParser parser = new AFMParser(new FileInputStream("src/test/resources/afm/NoEndFontMetrics.afm"));
IOException e = assertThrows(IOException.class, parser::parse,
"The AFMParser should have thrown an IOException because of a missing " + AFMParser.END_FONT_METRICS);
assertTrue(e.getMessage().contains("Unknown AFM key"));
}

@Test
void testMalformedFloat() throws IOException
{
AFMParser parser = new AFMParser(
new FileInputStream("src/test/resources/afm/MalformedFloat.afm"));
try
{
parser.parse();
fail("The AFMParser should have thrown an IOException because of a malformed float value");
}
catch (IOException e)
{
assertTrue(e.getCause() instanceof NumberFormatException);
assertTrue(e.getMessage().contains("4,1ab"));
}
void testMalformedFloat() throws IOException {
AFMParser parser = new AFMParser(new FileInputStream("src/test/resources/afm/MalformedFloat.afm"));
IOException e = assertThrows(IOException.class, parser::parse,
"The AFMParser should have thrown an IOException because of a malformed float value");
assertInstanceOf(NumberFormatException.class, e.getCause());
assertTrue(e.getMessage().contains("4,1ab"));
}

@Test
void testMalformedInteger() throws IOException
{
AFMParser parser = new AFMParser(
new FileInputStream("src/test/resources/afm/MalformedInteger.afm"));
try
{
parser.parse();
fail("The AFMParser should have thrown an IOException because of a malformed int value");
}
catch (IOException e)
{
assertTrue(e.getCause() instanceof NumberFormatException);
assertTrue(e.getMessage().contains("3.4"));
}
void testMalformedInteger() throws IOException {
AFMParser parser = new AFMParser(new FileInputStream("src/test/resources/afm/MalformedInteger.afm"));
IOException e = assertThrows(IOException.class, parser::parse,
"The AFMParser should have thrown an IOException because of a malformed int value");
assertInstanceOf(NumberFormatException.class, e.getCause());
assertTrue(e.getMessage().contains("3.4"));
}

@Test
void testHelveticaFontMetrics() throws IOException
{
AFMParser parser = new AFMParser(
new FileInputStream("src/test/resources/afm/Helvetica.afm"));
void testHelveticaFontMetrics() throws IOException {
AFMParser parser = new AFMParser(new FileInputStream(HELVETICA_AFM));
checkHelveticaFontMetrics(parser.parse());
}

@Test
void testHelveticaCharMetrics() throws IOException
{
AFMParser parser = new AFMParser(
new FileInputStream("src/test/resources/afm/Helvetica.afm"));
void testHelveticaCharMetrics() throws IOException {
AFMParser parser = new AFMParser(new FileInputStream(HELVETICA_AFM));
FontMetrics fontMetrics = parser.parse();

// char metrics
checkHelveticaCharMetrics(fontMetrics.getCharMetrics());
}

@Test
void testHelveticaKernPairs() throws IOException
{
AFMParser parser = new AFMParser(
new FileInputStream("src/test/resources/afm/Helvetica.afm"));
void testHelveticaKernPairs() throws IOException {
AFMParser parser = new AFMParser(new FileInputStream(HELVETICA_AFM));
FontMetrics fontMetrics = parser.parse();

// KernPairs
Expand All @@ -149,29 +111,23 @@ void testHelveticaKernPairs() throws IOException
}

@Test
void testHelveticaFontMetricsReducedDataset() throws IOException
{
AFMParser parser = new AFMParser(
new FileInputStream("src/test/resources/afm/Helvetica.afm"));
void testHelveticaFontMetricsReducedDataset() throws IOException {
AFMParser parser = new AFMParser(new FileInputStream(HELVETICA_AFM));
checkHelveticaFontMetrics(parser.parse(true));
}

@Test
void testHelveticaCharMetricsReducedDataset() throws IOException
{
AFMParser parser = new AFMParser(
new FileInputStream("src/test/resources/afm/Helvetica.afm"));
void testHelveticaCharMetricsReducedDataset() throws IOException {
AFMParser parser = new AFMParser(new FileInputStream(HELVETICA_AFM));
FontMetrics fontMetrics = parser.parse(true);

// char metrics
checkHelveticaCharMetrics(fontMetrics.getCharMetrics());
}

@Test
void testHelveticaKernPairsReducedDataset() throws IOException
{
AFMParser parser = new AFMParser(
new FileInputStream("src/test/resources/afm/Helvetica.afm"));
void testHelveticaKernPairsReducedDataset() throws IOException {
AFMParser parser = new AFMParser(new FileInputStream(HELVETICA_AFM));
FontMetrics fontMetrics = parser.parse(true);

// KernPairs, empty due to reducedDataset == true
Expand All @@ -184,12 +140,10 @@ void testHelveticaKernPairsReducedDataset() throws IOException
assertTrue(fontMetrics.getComposites().isEmpty());
}

private void checkHelveticaCharMetrics(List<CharMetric> charMetrics)
{
private void checkHelveticaCharMetrics(List<CharMetric> charMetrics) {
assertEquals(315, charMetrics.size());
// check "space" metrics
Optional<CharMetric> space = charMetrics.stream()//
.filter(c -> "space".equals(c.getName())).findFirst();
Optional<CharMetric> space = charMetrics.stream().filter(c -> "space".equals(c.getName())).findFirst();
assertTrue(space.isPresent());
CharMetric spaceCharMetric = space.get();
assertEquals(278f, spaceCharMetric.getWx(), 0f);
Expand All @@ -201,8 +155,7 @@ private void checkHelveticaCharMetrics(List<CharMetric> charMetrics)
assertNull(spaceCharMetric.getW1());
assertNull(spaceCharMetric.getVv());
// check "ring" metrics
Optional<CharMetric> ring = charMetrics.stream()//
.filter(c -> "ring".equals(c.getName())).findFirst();
Optional<CharMetric> ring = charMetrics.stream().filter(c -> "ring".equals(c.getName())).findFirst();
assertTrue(ring.isPresent());
CharMetric ringCharMetric = ring.get();
assertEquals(333f, ringCharMetric.getWx(), 0f);
Expand All @@ -215,8 +168,7 @@ private void checkHelveticaCharMetrics(List<CharMetric> charMetrics)
assertNull(ringCharMetric.getVv());
}

private void checkHelveticaFontMetrics(FontMetrics fontMetrics)
{
private void checkHelveticaFontMetrics(FontMetrics fontMetrics) {
assertEquals(4.1f, fontMetrics.getAFMVersion(), 0f);
assertEquals("Helvetica", fontMetrics.getFontName());
assertEquals("Helvetica", fontMetrics.getFullName());
Expand Down Expand Up @@ -254,8 +206,7 @@ private void checkHelveticaFontMetrics(FontMetrics fontMetrics)
assertFalse(fontMetrics.getIsFixedPitch());
}

private void checkBBox(BoundingBox bBox, float lowerX, float lowerY, float upperX, float upperY)
{
private void checkBBox(BoundingBox bBox, float lowerX, float lowerY, float upperX, float upperY) {
assertNotNull(bBox);
assertEquals(lowerX, bBox.getLowerLeftX(), 0f);
assertEquals(lowerY, bBox.getLowerLeftY(), 0f);
Expand All @@ -264,15 +215,13 @@ private void checkBBox(BoundingBox bBox, float lowerX, float lowerY, float upper
}

private void checkKernPair(List<KernPair> kernPairs, String firstKernChar,
String secondKernChar, float x, float y)
{
String secondKernChar, float x, float y) {
Optional<KernPair> kernPair = kernPairs.stream() //
.filter(k -> firstKernChar.equals(k.getFirstKernCharacter())) //
.filter(k -> secondKernChar.equals(k.getSecondKernCharacter())) //
.findFirst();
assertTrue(kernPair.isPresent());
assertEquals(x, kernPair.get().getX(), 0f);
assertEquals(y, kernPair.get().getY(), 0f);

}
}
61 changes: 19 additions & 42 deletions fontbox/src/test/java/org/apache/fontbox/cff/CFFParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
*/
package org.apache.fontbox.cff;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import org.apache.fontbox.util.BoundingBox;
Expand All @@ -40,8 +40,8 @@ class CFFParserTest
@BeforeAll
static void loadCFFFont() throws IOException
{
List<CFFFont> fonts = readFont("target/fonts/SourceSansProBold.otf");
testCFFType1Font = (CFFType1Font) fonts.get(0);
List<CFFFont> fonts = readFont();
testCFFType1Font = (CFFType1Font) fonts.getFirst();
}

@Test
Expand Down Expand Up @@ -106,8 +106,7 @@ void voidEncoding()
{
CFFEncoding encoding = testCFFType1Font.getEncoding();
assertNotNull(encoding, "Encoding must not be null");
assertTrue(encoding instanceof CFFStandardEncoding,
"Encoding is not an instance of CFFStandardEncoding");
assertInstanceOf(CFFStandardEncoding.class, encoding, "Encoding is not an instance of CFFStandardEncoding");
}

@Test
Expand All @@ -117,25 +116,13 @@ void testCharStringBytess()
assertFalse(charStringBytes.isEmpty());
assertEquals(824, testCFFType1Font.getNumCharStrings());
// check some randomly chosen values
assertTrue(
Arrays.equals(new byte[] { -4, 15, 14 }, charStringBytes.get(1)), //
"Other char strings byte values than expected");
assertTrue(
Arrays.equals(new byte[] { 72, 29, -13, 29, -9, -74, -9, 43, 3, 33, 29, 14 },
charStringBytes.get(16)), //
"Other char strings byte values than expected");
assertTrue(
Arrays.equals(new byte[] { -41, 88, 29, -47, -9, 12, 1, -123, 10, 3, 35, 29, -9,
-50, -9, 62, -9, 3, 10, 85, -56, 61, 10 }, charStringBytes.get(195)), //
"Other char strings byte values than expected");
assertTrue(
Arrays.equals(new byte[] { -5, -69, -61, -8, 28, 1, -9, 57, -39, -65, 29, 14 },
charStringBytes.get(525)), //
"Other char strings byte values than expected");
assertTrue(
Arrays.equals(new byte[] { 107, -48, 10, -9, 20, -9, 123, 3, -9, -112, -8, -46, 21,
-10, 115, 10 }, charStringBytes.get(738)), //
"Other char strings byte values than expected");
assertArrayEquals(new byte[]{-4, 15, 14}, charStringBytes.get(1), "Other char strings byte values than expected");
assertArrayEquals(new byte[]{72, 29, -13, 29, -9, -74, -9, 43, 3, 33, 29, 14}, charStringBytes.get(16), "Other char strings byte values than expected");
assertArrayEquals(new byte[]{-41, 88, 29, -47, -9, 12, 1, -123, 10, 3, 35, 29, -9,
-50, -9, 62, -9, 3, 10, 85, -56, 61, 10}, charStringBytes.get(195), "Other char strings byte values than expected");
assertArrayEquals(new byte[]{-5, -69, -61, -8, 28, 1, -9, 57, -39, -65, 29, 14}, charStringBytes.get(525), "Other char strings byte values than expected");
assertArrayEquals(new byte[]{107, -48, 10, -9, 20, -9, 123, 3, -9, -112, -8, -46, 21,
-10, 115, 10}, charStringBytes.get(738), "Other char strings byte values than expected");
}

@Test
Expand All @@ -145,28 +132,19 @@ void testGlobalSubrIndex()
assertFalse(globalSubrIndex.isEmpty());
assertEquals(278, globalSubrIndex.size());
// check some randomly chosen values
assertTrue(
Arrays.equals(new byte[] { 21, -70, -83, -85, -72, -72, 105, -85, 92, 91, 105, 107,
10, -83, -9, 62, 10 }, globalSubrIndex.get(12)), //
"Other global subr index values than expected");
assertTrue(
Arrays.equals(new byte[] { 58, 122, 29, -5, 48, 6, 11 }, globalSubrIndex.get(120)), //
"Other global subr index values than expected");
assertTrue(
Arrays.equals(new byte[] { 68, 80, 29, -45, -9, 16, -8, -92, 119, 11 },
globalSubrIndex.get(253)), //
"Other global subr index values than expected");
assertArrayEquals(new byte[]{21, -70, -83, -85, -72, -72, 105, -85, 92, 91, 105, 107,
10, -83, -9, 62, 10}, globalSubrIndex.get(12), "Other global subr index values than expected");
assertArrayEquals(new byte[]{58, 122, 29, -5, 48, 6, 11}, globalSubrIndex.get(120), "Other global subr index values than expected");
assertArrayEquals(new byte[]{68, 80, 29, -45, -9, 16, -8, -92, 119, 11}, globalSubrIndex.get(253), "Other global subr index values than expected");
}

/**
* PDFBOX-4038: Test whether BlueValues and other delta encoded lists are read correctly. The
* test file is from FOP-2432.
*
* @throws IOException
*/
@Test
void testDeltaLists() throws IOException
{
void testDeltaLists() {
@SuppressWarnings("unchecked")
List<Number> blues = (List<Number>) testCFFType1Font.getPrivateDict().get("BlueValues");

Expand Down Expand Up @@ -201,9 +179,9 @@ void testDeltaLists() throws IOException
new int[]{146, 150}, stemSnapV);
}

private static List<CFFFont> readFont(String filename) throws IOException
private static List<CFFFont> readFont() throws IOException
{
RandomAccessReadBufferedFile randomAccessRead = new RandomAccessReadBufferedFile(filename);
RandomAccessReadBufferedFile randomAccessRead = new RandomAccessReadBufferedFile("target/fonts/SourceSansProBold.otf");
CFFParser parser = new CFFParser();
return parser.parse(randomAccessRead);
}
Expand All @@ -225,5 +203,4 @@ private void assertNumberList(String message, float[] expected, List<Number> fou
assertEquals(expected[i], found.get(i).floatValue(), message);
}
}

}
Loading