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.

TTFFileTest.java 15KB

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