|
7 | 7 | import org.junit.jupiter.api.Assertions; |
8 | 8 | import org.junit.jupiter.api.Test; |
9 | 9 |
|
10 | | -import java.io.File; |
11 | | -import java.io.FileOutputStream; |
12 | | -import java.io.IOException; |
13 | | -import java.io.InputStream; |
| 10 | +import java.io.*; |
14 | 11 |
|
15 | 12 | /* |
16 | 13 | * Proof of Concept to handle special characters like U+2002 ('enspace') |
|
26 | 23 | @Slf4j |
27 | 24 | class TestSpecialCharacters { |
28 | 25 |
|
29 | | - private static final String TEST_LINE = "Row with special character : ."; // test line with U+2002 ('enspace') |
| 26 | + private static final String TEST_LINE = "Row with special character : %s."; // test line with U+2002 ('enspace') |
| 27 | + |
| 28 | + private static final String[] SPECIAL_CHARACTERS = { |
| 29 | + "\u2002", // 'enspace' |
| 30 | + "\u2010", // 'hyphentwo' |
| 31 | + "\u2033", // 'second' |
| 32 | + "\u03BC", // 'mugreek' |
| 33 | + "\u039C", // 'Mu' |
| 34 | + "\u2212", // 'minus' |
| 35 | + "\u0141" , // 'Lslash' |
| 36 | + "\u2103", // 'centigrade' |
| 37 | + }; |
30 | 38 |
|
31 | 39 | @Test |
32 | 40 | void testWithPdfBoxFontHelvetica() throws IOException { |
33 | | - try (FileOutputStream fos = new FileOutputStream( "target/test_font_from_pdfbox.pdf" )) { |
34 | | - SpecialCharacters sp = new SpecialCharacters(); |
35 | | - Assertions.assertThrows( IllegalArgumentException.class, () -> { |
36 | | - sp.generatePDF( fos, PDType1Font.HELVETICA, TEST_LINE ); |
37 | | - }); |
| 41 | + SpecialCharacters sp = new SpecialCharacters(); |
| 42 | + for ( int k=0; k<SPECIAL_CHARACTERS.length; k++ ) { |
| 43 | + String special = SPECIAL_CHARACTERS[k]; |
| 44 | + String line = String.format( TEST_LINE, special ); |
| 45 | + try (ByteArrayOutputStream os = new ByteArrayOutputStream()) { |
| 46 | + log.info( "testWithPdfBoxFontHelvetica index:{}, char:{}, line:{}", k, special, line ); |
| 47 | + Assertions.assertThrows(IllegalArgumentException.class, () -> { |
| 48 | + sp.generatePDF(os, PDType1Font.HELVETICA, line); |
| 49 | + }); |
| 50 | + } |
38 | 51 | } |
39 | | - |
40 | 52 | } |
41 | 53 |
|
42 | 54 | @Test |
43 | 55 | void testWithExternalHelvetica() throws IOException { |
44 | | - File pdfFile = new File( "target/test_font_external.pdf" ); |
45 | | - log.info( "pdfFile: {}, delete: {}", pdfFile, pdfFile.delete() ); |
46 | | - Assertions.assertFalse( pdfFile.exists() ); |
47 | | - try ( InputStream fontIS = ClassHelper.loadFromClassLoader( "font/Helvetica.ttf" ); |
48 | | - FileOutputStream fos = new FileOutputStream( pdfFile ) ) { |
| 56 | + // some characters are still not available on all font, for instance in this example : |
| 57 | + // "\uFFFD", // '.notdef' |
| 58 | + // "\u25AA", // 'H18543' |
| 59 | + SpecialCharacters sp = new SpecialCharacters(); |
| 60 | + for ( int k=0; k<SPECIAL_CHARACTERS.length; k++ ) { |
| 61 | + String special = SPECIAL_CHARACTERS[k]; |
| 62 | + String line = String.format( TEST_LINE, special ); |
| 63 | + File pdfFile = new File( String.format( "target/test_font_external_%s.pdf", k ) ); |
| 64 | + log.info( "pdfFile: {}, delete: {}", pdfFile, pdfFile.delete() ); |
| 65 | + Assertions.assertFalse( pdfFile.exists() ); |
| 66 | + try (InputStream fontIS = ClassHelper.loadFromClassLoader( "font/Helvetica.ttf" ); |
| 67 | + OutputStream os = new FileOutputStream( pdfFile ) ) { |
| 68 | + log.info( "testWithExternalHelvetica index:{}, char:{}, line:{}", k, special, line ); |
| 69 | + sp.generatePDF(os, fontIS, line); |
| 70 | + } |
| 71 | + Assertions.assertTrue( pdfFile.exists() ); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void testWithPdfBoxFontHelveticaNormalLine() throws IOException { |
| 77 | + try (ByteArrayOutputStream os = new ByteArrayOutputStream()) { |
49 | 78 | SpecialCharacters sp = new SpecialCharacters(); |
50 | | - sp.generatePDF( fos, fontIS, TEST_LINE ); |
| 79 | + sp.generatePDF( os, PDType1Font.HELVETICA, "Normal line" ); |
| 80 | + Assertions.assertNotEquals( 0, os.toByteArray().length ); |
51 | 81 | } |
52 | | - Assertions.assertTrue( pdfFile.exists() ); |
53 | 82 | } |
54 | 83 |
|
55 | 84 | } |
0 commit comments