You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TTFFileTestCase.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.fonts.truetype;
  19. import java.io.IOException;
  20. import java.util.Map;
  21. import org.junit.Test;
  22. import static org.junit.Assert.assertEquals;
  23. import static org.junit.Assert.assertTrue;
  24. import static org.junit.Assert.fail;
  25. import org.apache.fop.fonts.truetype.TTFFile.PostScriptVersion;
  26. /**
  27. * Class for testing org.apache.fop.fonts.truetype.TTFFile
  28. */
  29. public class TTFFileTestCase {
  30. // We only want to initialize the FontFileReader once (for performance reasons)
  31. /** The truetype font file (DejaVuLGCSerif) */
  32. protected final TTFFile dejavuTTFFile;
  33. /** The FontFileReader for ttfFile (DejaVuLGCSerif) */
  34. protected final FontFileReader dejavuReader;
  35. /** The truetype font file (DroidSansMono) */
  36. protected final TTFFile droidmonoTTFFile;
  37. /** The FontFileReader for ttfFile (DroidSansMono) */
  38. protected final FontFileReader droidmonoReader;
  39. /**
  40. * Constructor initialises FileFontReader to
  41. * @throws IOException exception
  42. */
  43. public TTFFileTestCase() throws IOException {
  44. dejavuTTFFile = new TTFFile();
  45. dejavuReader = new FontFileReader("test/resources/fonts/ttf/DejaVuLGCSerif.ttf");
  46. dejavuTTFFile.readFont(dejavuReader);
  47. droidmonoTTFFile = new TTFFile();
  48. droidmonoReader = new FontFileReader("test/resources/fonts/ttf/DroidSansMono.ttf");
  49. droidmonoTTFFile.readFont(droidmonoReader);
  50. }
  51. /**
  52. * Test convertTTFUnit2PDFUnit() - The units per em retrieved reading the HEAD table from
  53. * the font file. (DroidSansMono has the same units per em as DejaVu so no point testing it)
  54. */
  55. @Test
  56. public void testConvertTTFUnit2PDFUnit() {
  57. // DejaVu has 2048 units per em (PDF works in millipts, thus the 1000)
  58. // test rational number
  59. assertEquals(1000, dejavuTTFFile.convertTTFUnit2PDFUnit(2048));
  60. // test smallest case, this should = 0.488 (round down to 0)
  61. assertEquals(0, dejavuTTFFile.convertTTFUnit2PDFUnit(1));
  62. // this should round up, but since it's millipts...
  63. assertEquals(0, dejavuTTFFile.convertTTFUnit2PDFUnit(2));
  64. // ensure behaviour is the same for negative numbers
  65. assertEquals(0, dejavuTTFFile.convertTTFUnit2PDFUnit(-0));
  66. assertEquals(-1000, dejavuTTFFile.convertTTFUnit2PDFUnit(-2048));
  67. assertEquals(0, dejavuTTFFile.convertTTFUnit2PDFUnit(-1));
  68. assertEquals(0, dejavuTTFFile.convertTTFUnit2PDFUnit(-2));
  69. }
  70. /**
  71. * Test checkTTC()
  72. * @throws IOException exception
  73. */
  74. @Test
  75. public void testCheckTTC() throws IOException {
  76. // DejaVu is not a TTC, thus this returns true
  77. assertTrue(dejavuTTFFile.checkTTC(""));
  78. assertTrue(droidmonoTTFFile.checkTTC(""));
  79. /*
  80. * Cannot reasonably test the rest of this method without an actual truetype collection
  81. * because all methods in FontFileReader are "final" and thus mocking isn't possible.
  82. */
  83. }
  84. /**
  85. * Test getAnsiKerning() - Tests values retrieved from the kern table in the font file.
  86. */
  87. @Test
  88. public void testGetAnsiKerning() {
  89. Map<Integer, Map<Integer, Integer>> ansiKerning = dejavuTTFFile.getKerning();
  90. if (ansiKerning.isEmpty()) {
  91. fail();
  92. }
  93. Integer k1 = ansiKerning.get(Integer.valueOf('A')).get(
  94. Integer.valueOf('T'));
  95. assertEquals(dejavuTTFFile.convertTTFUnit2PDFUnit(-112), k1.intValue());
  96. Integer k2 = ansiKerning.get(Integer.valueOf('Y')).get(Integer.valueOf('u'));
  97. assertEquals(dejavuTTFFile.convertTTFUnit2PDFUnit(-178), k2.intValue());
  98. // DroidSansMono doens't have kerning (it's mono-spaced)
  99. ansiKerning = droidmonoTTFFile.getAnsiKerning();
  100. if (!ansiKerning.isEmpty()) {
  101. fail("DroidSansMono shouldn't have any kerning data.");
  102. }
  103. }
  104. /**
  105. * Test getCapHeight - there are several paths to test:
  106. * 1) The PCLT table (if present)
  107. * 2) The yMax (3rd) value, for the bounding box, for 'H' in the glyf table.
  108. * if not the above:
  109. * 3) The caps height in the OS/2 table
  110. * Tests values retrieved from analysing the font file.
  111. */
  112. @Test
  113. public void testGetCapHeight() {
  114. // DejaVu doesn't have the PCLT table and so these have to be guessed
  115. // The height is approximated to be the height of the "H" which for
  116. // Deja = 1493 TTFunits
  117. assertEquals(dejavuTTFFile.convertTTFUnit2PDFUnit(1493), dejavuTTFFile.getCapHeight());
  118. // DroidSansMono doesn't have a PCLT table either
  119. // height of "H" = 1462
  120. assertEquals(droidmonoTTFFile.convertTTFUnit2PDFUnit(1462),
  121. droidmonoTTFFile.getCapHeight());
  122. }
  123. /**
  124. * Test getCharSetName() - check that it returns "WinAnsiEncoding".
  125. */
  126. @Test
  127. public void testGetCharSetName() {
  128. assertTrue("WinAnsiEncoding".equals(dejavuTTFFile.getCharSetName()));
  129. assertTrue("WinAnsiEncoding".equals(droidmonoTTFFile.getCharSetName()));
  130. }
  131. /**
  132. * Test getCharWidth() - Test values retrieved from the metrics in the glyf table in
  133. * the font file.
  134. */
  135. @Test
  136. public void testGetCharWidth() {
  137. // Arbitrarily test a few values:
  138. // The width of "H" (Unicode index 0x0048) is 1786
  139. assertEquals(dejavuTTFFile.convertTTFUnit2PDFUnit(1786), dejavuTTFFile.getCharWidth(0x48));
  140. // The width of "i" (unicode index 0x0069) is 655 TTFunits
  141. assertEquals(dejavuTTFFile.convertTTFUnit2PDFUnit(655), dejavuTTFFile.getCharWidth(0x69));
  142. // final check, "!" (unicode index 0x0021) is 823 TTFunits
  143. assertEquals(dejavuTTFFile.convertTTFUnit2PDFUnit(823), dejavuTTFFile.getCharWidth(0x21));
  144. // All the glyphs should be the same width in DroidSansMono (mono-spaced)
  145. int charWidth = droidmonoTTFFile.convertTTFUnit2PDFUnit(1229);
  146. for (int i = 0; i < 255; i++) {
  147. assertEquals(charWidth, droidmonoTTFFile.getCharWidth(i));
  148. }
  149. }
  150. /**
  151. * TODO: add implementation to this test
  152. */
  153. public void testGetCMaps() {
  154. }
  155. /**
  156. * Test getFamilyNames() - Test value retrieved from the name table in the font file.
  157. */
  158. @Test
  159. public void testGetFamilyNames() {
  160. assertEquals(1, dejavuTTFFile.getFamilyNames().size());
  161. for (String name : dejavuTTFFile.getFamilyNames()) {
  162. assertEquals("DejaVu LGC Serif", name);
  163. }
  164. assertEquals(1, droidmonoTTFFile.getFamilyNames().size());
  165. for (String name : droidmonoTTFFile.getFamilyNames()) {
  166. assertEquals("Droid Sans Mono", name);
  167. }
  168. }
  169. /**
  170. * Test getFirstChar() - TODO: implement a more intelligent test here.
  171. */
  172. @Test
  173. public void testGetFirstChar() {
  174. // Not really sure how to test this intelligently
  175. assertEquals(0, dejavuTTFFile.getFirstChar());
  176. assertEquals(0, droidmonoTTFFile.getFirstChar());
  177. }
  178. /**
  179. * Test getFlags() - Test values retrieved from the POST table in the font file.
  180. */
  181. @Test
  182. public void testGetFlags() {
  183. /* DejaVu flags are:
  184. * italic angle = 0
  185. * fixed pitch = 0
  186. * has serifs = true (default value; this font doesn't have a PCLT table)
  187. */
  188. int flags = dejavuTTFFile.getFlags();
  189. assertEquals(0, flags & 64); // Italics angle = 0
  190. assertEquals(32, flags & 32); // Adobe standard charset
  191. assertEquals(0, flags & 2); // fixed pitch = 0
  192. assertEquals(1, flags & 1); // has serifs = 1 (true)
  193. /*
  194. * Droid flags are:
  195. * italic angle = 0
  196. * fixed pitch = 1
  197. * has serifs = true (default value; this font doesn't have a PCLT table)
  198. */
  199. flags = droidmonoTTFFile.getFlags();
  200. assertEquals(0, flags & 64);
  201. assertEquals(32, flags & 32);
  202. assertEquals(2, flags & 2);
  203. assertEquals(1, flags & 1);
  204. }
  205. /**
  206. * Test getFontBBox() - Test values retrieved from values in the HEAD table in the font file.
  207. */
  208. @Test
  209. public void testGetFontBBox() {
  210. int[] bBox = dejavuTTFFile.getFontBBox();
  211. /*
  212. * The head table has the following values(DejaVu):
  213. * xmin = -1576, ymin = -710, xmax = 3439, ymax = 2544
  214. */
  215. assertEquals(dejavuTTFFile.convertTTFUnit2PDFUnit(-1576), bBox[0]);
  216. assertEquals(dejavuTTFFile.convertTTFUnit2PDFUnit(-710), bBox[1]);
  217. assertEquals(dejavuTTFFile.convertTTFUnit2PDFUnit(3439), bBox[2]);
  218. assertEquals(dejavuTTFFile.convertTTFUnit2PDFUnit(2544), bBox[3]);
  219. /*
  220. * The head table has the following values (DroidSansMono):
  221. * xmin = -312, ymin= -555, xmax = 1315, ymax = 2163
  222. */
  223. bBox = droidmonoTTFFile.getFontBBox();
  224. assertEquals(droidmonoTTFFile.convertTTFUnit2PDFUnit(-312), bBox[0]);
  225. assertEquals(droidmonoTTFFile.convertTTFUnit2PDFUnit(-555), bBox[1]);
  226. assertEquals(droidmonoTTFFile.convertTTFUnit2PDFUnit(1315), bBox[2]);
  227. assertEquals(droidmonoTTFFile.convertTTFUnit2PDFUnit(2163), bBox[3]);
  228. }
  229. /**
  230. * Test getFullName() - Test value retrieved from the name table in the font file.
  231. */
  232. @Test
  233. public void testGetFullName() {
  234. assertEquals("DejaVu LGC Serif", dejavuTTFFile.getFullName());
  235. assertEquals("Droid Sans Mono", droidmonoTTFFile.getFullName());
  236. }
  237. /**
  238. * Test getGlyphName - Test value retrieved from the POST table in the font file.
  239. */
  240. @Test
  241. public void testGetGlyphName() {
  242. assertEquals("H", dejavuTTFFile.getGlyphName(43));
  243. assertEquals("H", droidmonoTTFFile.getGlyphName(43));
  244. }
  245. /**
  246. * Test getItalicAngle() - Test value retrieved from the POST table in the font file.
  247. */
  248. @Test
  249. public void testGetItalicAngle() {
  250. assertEquals("0", dejavuTTFFile.getItalicAngle());
  251. assertEquals("0", droidmonoTTFFile.getItalicAngle());
  252. }
  253. /**
  254. * Test getKerning() - Test values retrieved from the kern table in the font file.
  255. */
  256. @Test
  257. public void testGetKerning() {
  258. Map<Integer, Map<Integer, Integer>> kerning = dejavuTTFFile.getKerning();
  259. if (kerning.isEmpty()) {
  260. fail();
  261. }
  262. Integer k1 = kerning.get(Integer.valueOf('A')).get(Integer.valueOf('T'));
  263. assertEquals(dejavuTTFFile.convertTTFUnit2PDFUnit(-112), k1.intValue());
  264. Integer k2 = kerning.get(Integer.valueOf('K')).get(Integer.valueOf('u'));
  265. assertEquals(dejavuTTFFile.convertTTFUnit2PDFUnit(-45), k2.intValue());
  266. // DroidSansMono has no kerning data (mono-spaced)
  267. kerning = droidmonoTTFFile.getKerning();
  268. if (!kerning.isEmpty()) {
  269. fail("DroidSansMono shouldn't have any kerning data");
  270. }
  271. }
  272. /**
  273. * Test lastChar() - TODO: implement a more intelligent test
  274. */
  275. @Test
  276. public void testLastChar() {
  277. assertEquals(0xff, dejavuTTFFile.getLastChar());
  278. assertEquals(0xff, droidmonoTTFFile.getLastChar());
  279. }
  280. /**
  281. * Test getLowerCaseAscent() - There are several paths to test:
  282. * 1) The values in the HHEA table (see code)
  283. * 2) Fall back to values from the OS/2 table
  284. * Test values retrieved from the font file.
  285. */
  286. @Test
  287. public void testGetLowerCaseAscent() {
  288. assertEquals(dejavuTTFFile.convertTTFUnit2PDFUnit(1556),
  289. dejavuTTFFile.getLowerCaseAscent());
  290. // Curiously the same value
  291. assertEquals(droidmonoTTFFile.convertTTFUnit2PDFUnit(1556),
  292. droidmonoTTFFile.getLowerCaseAscent());
  293. }
  294. /**
  295. * Test getPostScriptName() - Test values retrieved from the post table in the font file.
  296. */
  297. @Test
  298. public void testGetPostScriptName() {
  299. assertEquals(PostScriptVersion.V2, dejavuTTFFile.getPostScriptVersion());
  300. assertEquals(PostScriptVersion.V2, droidmonoTTFFile.getPostScriptVersion());
  301. }
  302. /**
  303. * Test getStemV() - Undefined.
  304. */
  305. @Test
  306. public void testGetStemV() {
  307. // Undefined
  308. assertEquals("0", dejavuTTFFile.getStemV());
  309. assertEquals("0", droidmonoTTFFile.getStemV());
  310. }
  311. /**
  312. * Test getSubFamilyName() - Test values retrieved from the name table in the font file.
  313. */
  314. @Test
  315. public void testGetSubFamilyName() {
  316. assertEquals("Book", dejavuTTFFile.getSubFamilyName());
  317. assertEquals("Regular", droidmonoTTFFile.getSubFamilyName());
  318. }
  319. /**
  320. * Test getTTCnames() - TODO: add implementation with TTC font.
  321. */
  322. public void testGetTTCnames() {
  323. // Can't test with with DejaVu since it's not a TrueType Collection
  324. }
  325. /**
  326. * Test getWeightClass() - Test value retrieved from the OS/2 table in the font file.
  327. */
  328. @Test
  329. public void testGetWeightClass() {
  330. // Retrieved from OS/2 table
  331. assertEquals(400, dejavuTTFFile.getWeightClass());
  332. assertEquals(400, droidmonoTTFFile.getWeightClass());
  333. }
  334. /**
  335. * Test getWidths() - Test values retrieved from the hmtx table in the font file.
  336. */
  337. @Test
  338. public void testGetWidths() {
  339. int[] widths = dejavuTTFFile.getWidths();
  340. // using the width of 'A' index = 36
  341. assertEquals(dejavuTTFFile.convertTTFUnit2PDFUnit(1479), widths[36]);
  342. // using the width of '|' index = 95
  343. assertEquals(dejavuTTFFile.convertTTFUnit2PDFUnit(690), widths[95]);
  344. widths = droidmonoTTFFile.getWidths();
  345. // DroidSansMono should have all widths the same size (mono-spaced)
  346. int width = droidmonoTTFFile.convertTTFUnit2PDFUnit(1229);
  347. for (int i = 0; i < 255; i++) {
  348. assertEquals(width, widths[i]);
  349. }
  350. }
  351. /**
  352. * Test getXHeight() - There are several paths to test:
  353. * 1) The PCLT table (if available)
  354. * 2) The yMax for the bounding box for 'x' in the glyf table.
  355. * Fall back:
  356. * 3) The xheight in the OS/2 table.
  357. */
  358. @Test
  359. public void testGetXHeight() {
  360. // Since there's no PCLT table, the height of 'x' is used for both DejaVu and DroidSansMono
  361. assertEquals(dejavuTTFFile.convertTTFUnit2PDFUnit(1064), dejavuTTFFile.getXHeight());
  362. assertEquals(droidmonoTTFFile.convertTTFUnit2PDFUnit(1098), droidmonoTTFFile.getXHeight());
  363. }
  364. /**
  365. * Test isCFF() - TODO: add test for a CFF font.
  366. */
  367. @Test
  368. public void testIsCFF() {
  369. // Neither DejaVu nor DroidSansMono are a compact format font
  370. assertEquals(false, dejavuTTFFile.isCFF());
  371. assertEquals(false, droidmonoTTFFile.isCFF());
  372. }
  373. /**
  374. * Test isEmbeddable() - Test value retrieved from the OS/2 table in the font file.
  375. */
  376. @Test
  377. public void testIsEmbeddable() {
  378. // Dejavu and DroidSansMono are both embeddable
  379. assertEquals(true, dejavuTTFFile.isEmbeddable());
  380. assertEquals(true, droidmonoTTFFile.isEmbeddable());
  381. }
  382. /**
  383. * Test readFont() - Add implementation if necessary.
  384. */
  385. public void testReadFont() {
  386. // I'm pretty sure we've tested this with all the other tests
  387. }
  388. }