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 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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.ByteArrayInputStream;
  20. import java.io.ByteArrayOutputStream;
  21. import java.io.DataOutputStream;
  22. import java.io.FileInputStream;
  23. import java.io.IOException;
  24. import java.io.InputStream;
  25. import java.util.HashMap;
  26. import java.util.List;
  27. import java.util.Map;
  28. import org.junit.Test;
  29. import static org.junit.Assert.assertEquals;
  30. import static org.junit.Assert.assertTrue;
  31. import static org.junit.Assert.fail;
  32. import org.apache.fop.fonts.CMapSegment;
  33. import org.apache.fop.fonts.truetype.OpenFont.PostScriptVersion;
  34. /**
  35. * Class for testing org.apache.fop.fonts.truetype.TTFFile
  36. */
  37. public class TTFFileTestCase {
  38. // We only want to initialize the FontFileReader once (for performance reasons)
  39. /** The truetype font file (DejaVuLGCSerif) */
  40. protected final TTFFile dejavuTTFFile;
  41. /** The FontFileReader for ttfFile (DejaVuLGCSerif) */
  42. protected final FontFileReader dejavuReader;
  43. /** The truetype font file (DroidSansMono) */
  44. protected final TTFFile droidmonoTTFFile;
  45. /** The FontFileReader for ttfFile (DroidSansMono) */
  46. protected final FontFileReader droidmonoReader;
  47. /** The truetype font file (AndroidEmoji) fonr non-BMP codepoints */
  48. protected final TTFFile androidEmojiTTFFile;
  49. /** The FontFileReader for ttfFile (AndroidEmoji) */
  50. protected final FontFileReader androidEmojiReader;
  51. /**
  52. * Constructor initialises FileFontReader to
  53. * @throws IOException exception
  54. */
  55. public TTFFileTestCase() throws IOException {
  56. InputStream dejaStream = new FileInputStream("test/resources/fonts/ttf/DejaVuLGCSerif.ttf");
  57. dejavuTTFFile = new TTFFile();
  58. dejavuReader = new FontFileReader(dejaStream);
  59. String dejavuHeader = OFFontLoader.readHeader(dejavuReader);
  60. dejavuTTFFile.readFont(dejavuReader, dejavuHeader);
  61. dejaStream.close();
  62. InputStream droidStream = new FileInputStream("test/resources/fonts/ttf/DroidSansMono.ttf");
  63. droidmonoTTFFile = new TTFFile();
  64. droidmonoReader = new FontFileReader(droidStream);
  65. String droidmonoHeader = OFFontLoader.readHeader(droidmonoReader);
  66. droidmonoTTFFile.readFont(droidmonoReader, droidmonoHeader);
  67. droidStream.close();
  68. InputStream emojiStream = new FileInputStream("test/resources/fonts/ttf/AndroidEmoji.ttf");
  69. androidEmojiTTFFile = new TTFFile();
  70. androidEmojiReader = new FontFileReader(emojiStream);
  71. String androidEmojiHeader = OFFontLoader.readHeader(androidEmojiReader);
  72. androidEmojiTTFFile.readFont(androidEmojiReader, androidEmojiHeader);
  73. emojiStream.close();
  74. }
  75. /**
  76. * Test convertTTFUnit2PDFUnit() - The units per em retrieved reading the HEAD table from
  77. * the font file. (DroidSansMono has the same units per em as DejaVu so no point testing it)
  78. */
  79. @Test
  80. public void testConvertTTFUnit2PDFUnit() {
  81. // DejaVu has 2048 units per em (PDF works in millipts, thus the 1000)
  82. // test rational number
  83. assertEquals(1000, dejavuTTFFile.convertTTFUnit2PDFUnit(2048));
  84. // test smallest case, this should = 0.488 (round down to 0)
  85. assertEquals(0, dejavuTTFFile.convertTTFUnit2PDFUnit(1));
  86. // this should round up, but since it's millipts...
  87. assertEquals(0, dejavuTTFFile.convertTTFUnit2PDFUnit(2));
  88. // ensure behaviour is the same for negative numbers
  89. assertEquals(0, dejavuTTFFile.convertTTFUnit2PDFUnit(-0));
  90. assertEquals(-1000, dejavuTTFFile.convertTTFUnit2PDFUnit(-2048));
  91. assertEquals(0, dejavuTTFFile.convertTTFUnit2PDFUnit(-1));
  92. assertEquals(0, dejavuTTFFile.convertTTFUnit2PDFUnit(-2));
  93. }
  94. /**
  95. * Test checkTTC()
  96. * @throws IOException exception
  97. */
  98. @Test
  99. public void testCheckTTC() throws IOException {
  100. // DejaVu is not a TTC, thus this returns true
  101. String dejavuHeader = OFFontLoader.readHeader(dejavuReader);
  102. assertTrue(dejavuTTFFile.checkTTC(dejavuHeader, ""));
  103. String droidmonoHeader = OFFontLoader.readHeader(droidmonoReader);
  104. assertTrue(droidmonoTTFFile.checkTTC(droidmonoHeader, ""));
  105. /*
  106. * Cannot reasonably test the rest of this method without an actual truetype collection
  107. * because all methods in FontFileReader are "final" and thus mocking isn't possible.
  108. */
  109. }
  110. /**
  111. * Test getAnsiKerning() - Tests values retrieved from the kern table in the font file.
  112. */
  113. @Test
  114. public void testGetAnsiKerning() {
  115. Map<Integer, Map<Integer, Integer>> ansiKerning = dejavuTTFFile.getAnsiKerning();
  116. if (ansiKerning.isEmpty()) {
  117. fail();
  118. }
  119. Integer k1 = ansiKerning.get((int) 'A').get((int) 'T');
  120. assertEquals(dejavuTTFFile.convertTTFUnit2PDFUnit(-112), k1.intValue());
  121. Integer k2 = ansiKerning.get((int) 'Y').get((int) 'u');
  122. assertEquals(dejavuTTFFile.convertTTFUnit2PDFUnit(-178), k2.intValue());
  123. // DroidSansMono doens't have kerning (it's mono-spaced)
  124. ansiKerning = droidmonoTTFFile.getAnsiKerning();
  125. if (!ansiKerning.isEmpty()) {
  126. fail("DroidSansMono shouldn't have any kerning data.");
  127. }
  128. // AndroidEmoji doens't have kerning
  129. ansiKerning = androidEmojiTTFFile.getAnsiKerning();
  130. if (!ansiKerning.isEmpty()) {
  131. fail("AndroidEmoji shouldn't have any kerning data.");
  132. }
  133. }
  134. /**
  135. * Test getCapHeight - there are several paths to test:
  136. * 1) The PCLT table (if present)
  137. * 2) The yMax (3rd) value, for the bounding box, for 'H' in the glyf table.
  138. * if not the above:
  139. * 3) The caps height in the OS/2 table
  140. * Tests values retrieved from analysing the font file.
  141. */
  142. @Test
  143. public void testGetCapHeight() {
  144. // DejaVu doesn't have the PCLT table and so these have to be guessed
  145. // The height is approximated to be the height of the "H" which for
  146. // Deja = 1493 TTFunits
  147. assertEquals(dejavuTTFFile.convertTTFUnit2PDFUnit(1493), dejavuTTFFile.getCapHeight());
  148. // DroidSansMono doesn't have a PCLT table either
  149. // height of "H" = 1462
  150. assertEquals(droidmonoTTFFile.convertTTFUnit2PDFUnit(1462),
  151. droidmonoTTFFile.getCapHeight());
  152. // AndroidEmoji doesn't have a PCLT table either
  153. // height of "H" = 1462
  154. assertEquals(androidEmojiTTFFile.convertTTFUnit2PDFUnit(1462),
  155. androidEmojiTTFFile.getCapHeight());
  156. }
  157. /**
  158. * Test getCharSetName() - check that it returns "WinAnsiEncoding".
  159. */
  160. @Test
  161. public void testGetCharSetName() {
  162. assertTrue("WinAnsiEncoding".equals(dejavuTTFFile.getCharSetName()));
  163. assertTrue("WinAnsiEncoding".equals(droidmonoTTFFile.getCharSetName()));
  164. //Even though this pass I'm not sure whether is what we want
  165. assertTrue("WinAnsiEncoding".equals(androidEmojiTTFFile.getCharSetName()));
  166. }
  167. /**
  168. * Test getCharWidth() - Test values retrieved from the metrics in the glyf table in
  169. * the font file.
  170. */
  171. @Test
  172. public void testGetCharWidth() {
  173. // Arbitrarily test a few values:
  174. // The width of "H" (Unicode index 0x0048) is 1786
  175. assertEquals(dejavuTTFFile.convertTTFUnit2PDFUnit(1786), dejavuTTFFile.getCharWidth(0x48));
  176. // The width of "i" (unicode index 0x0069) is 655 TTFunits
  177. assertEquals(dejavuTTFFile.convertTTFUnit2PDFUnit(655), dejavuTTFFile.getCharWidth(0x69));
  178. // final check, "!" (unicode index 0x0021) is 823 TTFunits
  179. assertEquals(dejavuTTFFile.convertTTFUnit2PDFUnit(823), dejavuTTFFile.getCharWidth(0x21));
  180. // All the glyphs should be the same width in DroidSansMono (mono-spaced)
  181. int charWidth = droidmonoTTFFile.convertTTFUnit2PDFUnit(1229);
  182. for (int i = 0; i < 255; i++) {
  183. assertEquals(charWidth, droidmonoTTFFile.getCharWidth(i));
  184. }
  185. // All the glyphs should be the same width in EmojiAndroid (mono-spaced)
  186. charWidth = androidEmojiTTFFile.convertTTFUnit2PDFUnit(2600);
  187. for (int i = 0; i < 255; i++) {
  188. assertEquals(charWidth, androidEmojiTTFFile.getCharWidth(i));
  189. }
  190. }
  191. /**
  192. * TODO: add implementation to this test
  193. */
  194. @Test
  195. public void testGetCMaps() {
  196. List<CMapSegment> cmaps = androidEmojiTTFFile.getCMaps();
  197. for (CMapSegment seg : cmaps) {
  198. System.out.println(seg.getUnicodeStart() + "-" + seg.getUnicodeEnd() + " -> " + seg.getGlyphStartIndex());
  199. }
  200. }
  201. /**
  202. * Test getFamilyNames() - Test value retrieved from the name table in the font file.
  203. */
  204. @Test
  205. public void testGetFamilyNames() {
  206. assertEquals(1, dejavuTTFFile.getFamilyNames().size());
  207. for (String name : dejavuTTFFile.getFamilyNames()) {
  208. assertEquals("DejaVu LGC Serif", name);
  209. }
  210. assertEquals(1, droidmonoTTFFile.getFamilyNames().size());
  211. for (String name : droidmonoTTFFile.getFamilyNames()) {
  212. assertEquals("Droid Sans Mono", name);
  213. }
  214. assertEquals(1, androidEmojiTTFFile.getFamilyNames().size());
  215. for (String name : androidEmojiTTFFile.getFamilyNames()) {
  216. assertEquals("Android Emoji", name);
  217. }
  218. }
  219. /**
  220. * Test getFirstChar() - TODO: implement a more intelligent test here.
  221. */
  222. @Test
  223. public void testGetFirstChar() {
  224. // Not really sure how to test this intelligently
  225. assertEquals(0, dejavuTTFFile.getFirstChar());
  226. assertEquals(0, droidmonoTTFFile.getFirstChar());
  227. assertEquals(0, androidEmojiTTFFile.getFirstChar());
  228. }
  229. /**
  230. * Test getFlags() - Test values retrieved from the POST table in the font file.
  231. */
  232. @Test
  233. public void testGetFlags() {
  234. /* DejaVu flags are:
  235. * italic angle = 0
  236. * fixed pitch = 0
  237. * has serifs = true (default value; this font doesn't have a PCLT table)
  238. */
  239. int flags = dejavuTTFFile.getFlags();
  240. assertEquals(0, flags & 64); // Italics angle = 0
  241. assertEquals(32, flags & 32); // Adobe standard charset
  242. assertEquals(0, flags & 2); // fixed pitch = 0
  243. assertEquals(1, flags & 1); // has serifs = 1 (true)
  244. /*
  245. * Droid flags are:
  246. * italic angle = 0
  247. * fixed pitch = 1
  248. * has serifs = true (default value; this font doesn't have a PCLT table)
  249. */
  250. flags = droidmonoTTFFile.getFlags();
  251. assertEquals(0, flags & 64);
  252. assertEquals(32, flags & 32);
  253. assertEquals(2, flags & 2);
  254. assertEquals(1, flags & 1);
  255. /*
  256. * Android Emoji flags are:
  257. * italic angle = 0
  258. * fixed pitch = 0
  259. * has serifs = true (default value; this font doesn't have a PCLT table)
  260. */
  261. flags = androidEmojiTTFFile.getFlags();
  262. assertEquals(0, flags & 64);
  263. assertEquals(32, flags & 32);
  264. assertEquals(0, flags & 2);
  265. assertEquals(1, flags & 1);
  266. }
  267. /**
  268. * Test getFontBBox() - Test values retrieved from values in the HEAD table in the font file.
  269. */
  270. @Test
  271. public void testGetFontBBox() {
  272. int[] bBox = dejavuTTFFile.getFontBBox();
  273. /*
  274. * The head table has the following values(DejaVu):
  275. * xmin = -1576, ymin = -710, xmax = 3439, ymax = 2544
  276. */
  277. assertEquals(dejavuTTFFile.convertTTFUnit2PDFUnit(-1576), bBox[0]);
  278. assertEquals(dejavuTTFFile.convertTTFUnit2PDFUnit(-710), bBox[1]);
  279. assertEquals(dejavuTTFFile.convertTTFUnit2PDFUnit(3439), bBox[2]);
  280. assertEquals(dejavuTTFFile.convertTTFUnit2PDFUnit(2544), bBox[3]);
  281. /*
  282. * The head table has the following values (DroidSansMono):
  283. * xmin = -312, ymin= -555, xmax = 1315, ymax = 2163
  284. */
  285. bBox = droidmonoTTFFile.getFontBBox();
  286. assertEquals(droidmonoTTFFile.convertTTFUnit2PDFUnit(-312), bBox[0]);
  287. assertEquals(droidmonoTTFFile.convertTTFUnit2PDFUnit(-555), bBox[1]);
  288. assertEquals(droidmonoTTFFile.convertTTFUnit2PDFUnit(1315), bBox[2]);
  289. assertEquals(droidmonoTTFFile.convertTTFUnit2PDFUnit(2163), bBox[3]);
  290. /*
  291. * The head table has the following values (DroidSansMono):
  292. * xMin = -50, yMin = -733, xMax = 2550, yMax = 2181
  293. */
  294. bBox = androidEmojiTTFFile.getFontBBox();
  295. assertEquals(androidEmojiTTFFile.convertTTFUnit2PDFUnit(-50), bBox[0]);
  296. assertEquals(androidEmojiTTFFile.convertTTFUnit2PDFUnit(-733), bBox[1]);
  297. assertEquals(androidEmojiTTFFile.convertTTFUnit2PDFUnit(2550), bBox[2]);
  298. assertEquals(androidEmojiTTFFile.convertTTFUnit2PDFUnit(2181), bBox[3]);
  299. }
  300. /**
  301. * Test getFullName() - Test value retrieved from the name table in the font file.
  302. */
  303. @Test
  304. public void testGetFullName() {
  305. assertEquals("DejaVu LGC Serif", dejavuTTFFile.getFullName());
  306. assertEquals("Droid Sans Mono", droidmonoTTFFile.getFullName());
  307. assertEquals("Android Emoji", androidEmojiTTFFile.getFullName());
  308. }
  309. /**
  310. * Test getGlyphName - Test value retrieved from the POST table in the font file.
  311. */
  312. @Test
  313. public void testGetGlyphName() {
  314. assertEquals("H", dejavuTTFFile.getGlyphName(43));
  315. assertEquals("H", droidmonoTTFFile.getGlyphName(43));
  316. assertEquals("smileface", androidEmojiTTFFile.getGlyphName(64));
  317. }
  318. /**
  319. * Test getItalicAngle() - Test value retrieved from the POST table in the font file.
  320. */
  321. @Test
  322. public void testGetItalicAngle() {
  323. assertEquals("0", dejavuTTFFile.getItalicAngle());
  324. assertEquals("0", droidmonoTTFFile.getItalicAngle());
  325. assertEquals("0", androidEmojiTTFFile.getItalicAngle());
  326. }
  327. /**
  328. * Test getKerning() - Test values retrieved from the kern table in the font file.
  329. */
  330. @Test
  331. public void testGetKerning() {
  332. Map<Integer, Map<Integer, Integer>> kerning = dejavuTTFFile.getKerning();
  333. if (kerning.isEmpty()) {
  334. fail();
  335. }
  336. Integer k1 = kerning.get((int) 'A').get((int) 'T');
  337. assertEquals(dejavuTTFFile.convertTTFUnit2PDFUnit(-112), k1.intValue());
  338. Integer k2 = kerning.get((int) 'K').get((int) 'u');
  339. assertEquals(dejavuTTFFile.convertTTFUnit2PDFUnit(-45), k2.intValue());
  340. // DroidSansMono has no kerning data (mono-spaced)
  341. kerning = droidmonoTTFFile.getKerning();
  342. if (!kerning.isEmpty()) {
  343. fail("DroidSansMono shouldn't have any kerning data");
  344. }
  345. // AndroidEmoji doens't have kerning
  346. kerning = androidEmojiTTFFile.getKerning();
  347. if (!kerning.isEmpty()) {
  348. fail("AndroidEmoji shouldn't have any kerning data.");
  349. }
  350. }
  351. /**
  352. * Test lastChar() - TODO: implement a more intelligent test
  353. */
  354. @Test
  355. public void testLastChar() {
  356. assertEquals(0xff, dejavuTTFFile.getLastChar());
  357. assertEquals(0xff, droidmonoTTFFile.getLastChar());
  358. assertEquals(0xae, androidEmojiTTFFile.getLastChar()); // Last ASCII mapped char is REGISTERED SIGN
  359. }
  360. /**
  361. * Test getLowerCaseAscent() - There are several paths to test:
  362. * 1) The values in the HHEA table (see code)
  363. * 2) Fall back to values from the OS/2 table
  364. * Test values retrieved from the font file.
  365. */
  366. @Test
  367. public void testGetLowerCaseAscent() {
  368. assertEquals(dejavuTTFFile.convertTTFUnit2PDFUnit(1556),
  369. dejavuTTFFile.getLowerCaseAscent());
  370. // Curiously the same value
  371. assertEquals(droidmonoTTFFile.convertTTFUnit2PDFUnit(1556),
  372. droidmonoTTFFile.getLowerCaseAscent());
  373. //TODO: Nedd to be fixed?
  374. // 0 because the font miss letter glyph that are used in this method to guess the ascender:
  375. // OpenFont.guessVerticalMetricsFromGlyphBBox
  376. assertEquals(androidEmojiTTFFile.convertTTFUnit2PDFUnit(0),
  377. androidEmojiTTFFile.getLowerCaseAscent());
  378. }
  379. /**
  380. * Test getPostScriptName() - Test values retrieved from the post table in the font file.
  381. */
  382. @Test
  383. public void testGetPostScriptName() {
  384. assertEquals(PostScriptVersion.V2, dejavuTTFFile.getPostScriptVersion());
  385. assertEquals(PostScriptVersion.V2, droidmonoTTFFile.getPostScriptVersion());
  386. assertEquals(PostScriptVersion.V2, androidEmojiTTFFile.getPostScriptVersion());
  387. }
  388. /**
  389. * Test getStemV() - Undefined.
  390. */
  391. @Test
  392. public void testGetStemV() {
  393. // Undefined
  394. assertEquals("0", dejavuTTFFile.getStemV());
  395. assertEquals("0", droidmonoTTFFile.getStemV());
  396. assertEquals("0", androidEmojiTTFFile.getStemV());
  397. }
  398. /**
  399. * Test getSubFamilyName() - Test values retrieved from the name table in the font file.
  400. */
  401. @Test
  402. public void testGetSubFamilyName() {
  403. assertEquals("Book", dejavuTTFFile.getSubFamilyName());
  404. assertEquals("Regular", droidmonoTTFFile.getSubFamilyName());
  405. assertEquals("Regular", androidEmojiTTFFile.getSubFamilyName());
  406. }
  407. /**
  408. * Test getTTCnames() - TODO: add implementation with TTC font.
  409. */
  410. public void testGetTTCnames() {
  411. // Can't test with with DejaVu since it's not a TrueType Collection
  412. }
  413. /**
  414. * Test getWeightClass() - Test value retrieved from the OS/2 table in the font file.
  415. */
  416. @Test
  417. public void testGetWeightClass() {
  418. // Retrieved from OS/2 table
  419. assertEquals(400, dejavuTTFFile.getWeightClass());
  420. assertEquals(400, droidmonoTTFFile.getWeightClass());
  421. assertEquals(400, androidEmojiTTFFile.getWeightClass());
  422. }
  423. /**
  424. * Test getWidths() - Test values retrieved from the hmtx table in the font file.
  425. */
  426. @Test
  427. public void testGetWidths() {
  428. int[] widths = dejavuTTFFile.getWidths();
  429. // using the width of 'A' index = 36
  430. assertEquals(dejavuTTFFile.convertTTFUnit2PDFUnit(1479), widths[36]);
  431. // using the width of '|' index = 95
  432. assertEquals(dejavuTTFFile.convertTTFUnit2PDFUnit(690), widths[95]);
  433. // DroidSansMono should have all widths the same size (mono-spaced)
  434. widths = droidmonoTTFFile.getWidths();
  435. int charWidth = droidmonoTTFFile.convertTTFUnit2PDFUnit(1229);
  436. for (OpenFont.UnicodeMapping unicodeMapping : droidmonoTTFFile.unicodeMappings) {
  437. assertEquals(charWidth, widths[unicodeMapping.getGlyphIndex()]);
  438. }
  439. // All the glyphs should be the same width in EmojiAndroid (mono-spaced)
  440. charWidth = androidEmojiTTFFile.convertTTFUnit2PDFUnit(2600);
  441. widths = androidEmojiTTFFile.getWidths();
  442. for (OpenFont.UnicodeMapping unicodeMapping : androidEmojiTTFFile.unicodeMappings) {
  443. assertEquals(charWidth, widths[unicodeMapping.getGlyphIndex()]);
  444. }
  445. }
  446. @Test
  447. public void textUnicodeCoverage() {
  448. int nonBMPcount = 0;
  449. for (OpenFont.UnicodeMapping unicodeMapping : droidmonoTTFFile.unicodeMappings) {
  450. nonBMPcount += unicodeMapping.getUnicodeIndex() > 0xFFFF ? 1 : 0;
  451. }
  452. assertEquals("The font DroidSansMono is supposed to have only BMP codepoints", 0, nonBMPcount);
  453. nonBMPcount = 0;
  454. for (OpenFont.UnicodeMapping unicodeMapping : androidEmojiTTFFile.unicodeMappings) {
  455. nonBMPcount += unicodeMapping.getUnicodeIndex() > 0xFFFF ? 1 : 0;
  456. }
  457. assertTrue("The font AndroidEmoji is supposed to have non-BMP codepoints", 0 != nonBMPcount);
  458. }
  459. /**
  460. * Test getXHeight() - There are several paths to test:
  461. * 1) The PCLT table (if available)
  462. * 2) The yMax for the bounding box for 'x' in the glyf table.
  463. * Fall back:
  464. * 3) The xheight in the OS/2 table.
  465. */
  466. @Test
  467. public void testGetXHeight() {
  468. // Since there's no PCLT table, the height of 'x' is used for both DejaVu and DroidSansMono
  469. assertEquals(dejavuTTFFile.convertTTFUnit2PDFUnit(1064), dejavuTTFFile.getXHeight());
  470. assertEquals(droidmonoTTFFile.convertTTFUnit2PDFUnit(1098), droidmonoTTFFile.getXHeight());
  471. }
  472. /**
  473. * Test isCFF() - TODO: add test for a CFF font.
  474. */
  475. @Test
  476. public void testIsCFF() {
  477. // Neither DejaVu nor DroidSansMono are a compact format font
  478. assertEquals(false, dejavuTTFFile.isCFF());
  479. assertEquals(false, droidmonoTTFFile.isCFF());
  480. assertEquals(false, androidEmojiTTFFile.isCFF());
  481. }
  482. /**
  483. * Test isEmbeddable() - Test value retrieved from the OS/2 table in the font file.
  484. */
  485. @Test
  486. public void testIsEmbeddable() {
  487. // Dejavu and DroidSansMono are both embeddable
  488. assertEquals(true, dejavuTTFFile.isEmbeddable());
  489. assertEquals(true, droidmonoTTFFile.isEmbeddable());
  490. assertEquals(true, androidEmojiTTFFile.isEmbeddable());
  491. }
  492. /** Underline position and thickness. */
  493. @Test
  494. public void testUnderline() {
  495. assertEquals(-63, dejavuTTFFile.getUnderlinePosition());
  496. assertEquals(43, dejavuTTFFile.getUnderlineThickness());
  497. assertEquals(-75, droidmonoTTFFile.getUnderlinePosition());
  498. assertEquals(49, droidmonoTTFFile.getUnderlineThickness());
  499. assertEquals(-75, androidEmojiTTFFile.getUnderlinePosition());
  500. assertEquals(49, androidEmojiTTFFile.getUnderlineThickness());
  501. }
  502. /** Strikeout position and thickness. */
  503. @Test
  504. public void testStrikeout() {
  505. assertEquals(258, dejavuTTFFile.getStrikeoutPosition());
  506. assertEquals(49, dejavuTTFFile.getStrikeoutThickness());
  507. assertEquals(243, droidmonoTTFFile.getStrikeoutPosition());
  508. assertEquals(49, droidmonoTTFFile.getStrikeoutThickness());
  509. assertEquals(122, androidEmojiTTFFile.getStrikeoutPosition());
  510. assertEquals(24, androidEmojiTTFFile.getStrikeoutThickness());
  511. }
  512. /**
  513. * Test readFont() - Add implementation if necessary.
  514. */
  515. public void testReadFont() {
  516. // I'm pretty sure we've tested this with all the other tests
  517. }
  518. @Test
  519. public void testBBox() {
  520. assertEquals(dejavuTTFFile.getBBox(1)[0], 49);
  521. assertEquals(dejavuTTFFile.getBBox(2330).length, 4);
  522. }
  523. @Test
  524. public void testReservedIndex() throws IOException {
  525. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  526. DataOutputStream dos = new DataOutputStream(bos);
  527. dos.write(0);
  528. dos.write(2);
  529. for (int i = 0; i < 31; i++) {
  530. dos.write(0);
  531. }
  532. dos.write(1); //number of glyphs
  533. dos.writeShort(32768); //index value
  534. TTFFile ttfFile = new TTFFile();
  535. ttfFile.dirTabs = new HashMap<OFTableName, OFDirTabEntry>();
  536. ttfFile.fontFile = new FontFileReader(new ByteArrayInputStream(bos.toByteArray()));
  537. ttfFile.mtxTab = new OFMtxEntry[1];
  538. ttfFile.mtxTab[0] = new OFMtxEntry();
  539. ttfFile.readPostScript();
  540. }
  541. }