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

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