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.

TTFFile.java 45KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342
  1. /*
  2. * Copyright 1999-2004,2006 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.fonts.truetype;
  18. import java.io.IOException;
  19. import java.util.Iterator;
  20. import java.util.Map;
  21. import java.util.List;
  22. import org.apache.commons.logging.Log;
  23. import org.apache.commons.logging.LogFactory;
  24. import org.apache.fop.fonts.Glyphs;
  25. /**
  26. * Reads a TrueType file or a TrueType Collection.
  27. * The TrueType spec can be found at the Microsoft.
  28. * Typography site: http://www.microsoft.com/truetype/
  29. */
  30. public class TTFFile {
  31. static final byte NTABS = 24;
  32. static final int NMACGLYPHS = 258;
  33. static final int MAX_CHAR_CODE = 255;
  34. static final int ENC_BUF_SIZE = 1024;
  35. /** Set to true to get even more debug output than with level DEBUG */
  36. public static final boolean TRACE_ENABLED = false;
  37. private String encoding = "WinAnsiEncoding"; // Default encoding
  38. private short firstChar = 0;
  39. private boolean isEmbeddable = true;
  40. private boolean hasSerifs = true;
  41. /**
  42. * Table directory
  43. */
  44. protected Map dirTabs;
  45. private Map kerningTab; // for CIDs
  46. private Map ansiKerningTab; // For winAnsiEncoding
  47. private List cmaps;
  48. private List unicodeMapping;
  49. private int upem; // unitsPerEm from "head" table
  50. private int nhmtx; // Number of horizontal metrics
  51. private int postFormat;
  52. private int locaFormat;
  53. /**
  54. * Offset to last loca
  55. */
  56. protected long lastLoca = 0;
  57. private int numberOfGlyphs; // Number of glyphs in font (read from "maxp" table)
  58. private int nmGlyphs; // Used in fixWidths - remove?
  59. /**
  60. * Contains glyph data
  61. */
  62. protected TTFMtxEntry[] mtxTab; // Contains glyph data
  63. private int[] mtxEncoded = null;
  64. private String fontName = "";
  65. private String fullName = "";
  66. private String notice = "";
  67. private String familyName = "";
  68. private String subFamilyName = "";
  69. private long italicAngle = 0;
  70. private long isFixedPitch = 0;
  71. private int fontBBox1 = 0;
  72. private int fontBBox2 = 0;
  73. private int fontBBox3 = 0;
  74. private int fontBBox4 = 0;
  75. private int capHeight = 0;
  76. private int underlinePosition = 0;
  77. private int underlineThickness = 0;
  78. private int xHeight = 0;
  79. private int ascender = 0;
  80. private int descender = 0;
  81. private short lastChar = 0;
  82. private int[] ansiWidth;
  83. private Map ansiIndex;
  84. private TTFDirTabEntry currentDirTab;
  85. /**
  86. * logging instance
  87. */
  88. protected Log log = LogFactory.getLog(TTFFile.class);
  89. /**
  90. * Position inputstream to position indicated
  91. * in the dirtab offset + offset
  92. */
  93. void seekTab(FontFileReader in, String name,
  94. long offset) throws IOException {
  95. TTFDirTabEntry dt = (TTFDirTabEntry)dirTabs.get(name);
  96. if (dt == null) {
  97. log.error("Dirtab " + name + " not found.");
  98. } else {
  99. in.seekSet(dt.getOffset() + offset);
  100. this.currentDirTab = dt;
  101. }
  102. }
  103. /**
  104. * Convert from truetype unit to pdf unit based on the
  105. * unitsPerEm field in the "head" table
  106. * @param n truetype unit
  107. * @return pdf unit
  108. */
  109. public int convertTTFUnit2PDFUnit(int n) {
  110. int ret;
  111. if (n < 0) {
  112. long rest1 = n % upem;
  113. long storrest = 1000 * rest1;
  114. long ledd2 = (storrest != 0 ? rest1 / storrest : 0);
  115. ret = -((-1000 * n) / upem - (int)ledd2);
  116. } else {
  117. ret = (n / upem) * 1000 + ((n % upem) * 1000) / upem;
  118. }
  119. return ret;
  120. }
  121. /**
  122. * Read the cmap table,
  123. * return false if the table is not present or only unsupported
  124. * tables are present. Currently only unicode cmaps are supported.
  125. * Set the unicodeIndex in the TTFMtxEntries and fills in the
  126. * cmaps vector.
  127. */
  128. private boolean readCMAP(FontFileReader in) throws IOException {
  129. unicodeMapping = new java.util.ArrayList();
  130. //Read CMAP table and correct mtxTab.index
  131. int mtxPtr = 0;
  132. seekTab(in, "cmap", 2);
  133. int numCMap = in.readTTFUShort(); // Number of cmap subtables
  134. long cmapUniOffset = 0;
  135. log.info(numCMap + " cmap tables");
  136. //Read offset for all tables. We are only interested in the unicode table
  137. for (int i = 0; i < numCMap; i++) {
  138. int cmapPID = in.readTTFUShort();
  139. int cmapEID = in.readTTFUShort();
  140. long cmapOffset = in.readTTFULong();
  141. log.debug("Platform ID: " + cmapPID + " Encoding: " + cmapEID);
  142. if (cmapPID == 3 && cmapEID == 1) {
  143. cmapUniOffset = cmapOffset;
  144. }
  145. }
  146. if (cmapUniOffset <= 0) {
  147. log.fatal("Unicode cmap table not present");
  148. log.fatal("Unsupported format: Aborting");
  149. return false;
  150. }
  151. // Read unicode cmap
  152. seekTab(in, "cmap", cmapUniOffset);
  153. int cmapFormat = in.readTTFUShort();
  154. /*int cmap_length =*/ in.readTTFUShort(); //skip cmap length
  155. log.info("CMAP format: " + cmapFormat);
  156. if (cmapFormat == 4) {
  157. in.skip(2); // Skip version number
  158. int cmapSegCountX2 = in.readTTFUShort();
  159. int cmapSearchRange = in.readTTFUShort();
  160. int cmapEntrySelector = in.readTTFUShort();
  161. int cmapRangeShift = in.readTTFUShort();
  162. if (log.isDebugEnabled()) {
  163. log.debug("segCountX2 : " + cmapSegCountX2);
  164. log.debug("searchRange : " + cmapSearchRange);
  165. log.debug("entrySelector: " + cmapEntrySelector);
  166. log.debug("rangeShift : " + cmapRangeShift);
  167. }
  168. int[] cmapEndCounts = new int[cmapSegCountX2 / 2];
  169. int[] cmapStartCounts = new int[cmapSegCountX2 / 2];
  170. int[] cmapDeltas = new int[cmapSegCountX2 / 2];
  171. int[] cmapRangeOffsets = new int[cmapSegCountX2 / 2];
  172. for (int i = 0; i < (cmapSegCountX2 / 2); i++) {
  173. cmapEndCounts[i] = in.readTTFUShort();
  174. }
  175. in.skip(2); // Skip reservedPad
  176. for (int i = 0; i < (cmapSegCountX2 / 2); i++) {
  177. cmapStartCounts[i] = in.readTTFUShort();
  178. }
  179. for (int i = 0; i < (cmapSegCountX2 / 2); i++) {
  180. cmapDeltas[i] = in.readTTFShort();
  181. }
  182. //int startRangeOffset = in.getCurrentPos();
  183. for (int i = 0; i < (cmapSegCountX2 / 2); i++) {
  184. cmapRangeOffsets[i] = in.readTTFUShort();
  185. }
  186. int glyphIdArrayOffset = in.getCurrentPos();
  187. // Insert the unicode id for the glyphs in mtxTab
  188. // and fill in the cmaps ArrayList
  189. for (int i = 0; i < cmapStartCounts.length; i++) {
  190. log.debug(i + ": " + cmapStartCounts[i]
  191. + " - " + cmapEndCounts[i]);
  192. for (int j = cmapStartCounts[i]; j <= cmapEndCounts[i]; j++) {
  193. // Update lastChar
  194. if (j < 256 && j > lastChar) {
  195. lastChar = (short)j;
  196. }
  197. if (mtxPtr < mtxTab.length) {
  198. int glyphIdx;
  199. // the last character 65535 = .notdef
  200. // may have a range offset
  201. if (cmapRangeOffsets[i] != 0 && j != 65535) {
  202. int glyphOffset = glyphIdArrayOffset
  203. + ((cmapRangeOffsets[i] / 2)
  204. + (j - cmapStartCounts[i])
  205. + (i)
  206. - cmapSegCountX2 / 2) * 2;
  207. in.seekSet(glyphOffset);
  208. glyphIdx = (in.readTTFUShort() + cmapDeltas[i])
  209. & 0xffff;
  210. unicodeMapping.add(new UnicodeMapping(glyphIdx, j));
  211. mtxTab[glyphIdx].getUnicodeIndex().add(new Integer(j));
  212. // Also add winAnsiWidth
  213. List v = (List)ansiIndex.get(new Integer(j));
  214. if (v != null) {
  215. Iterator e = v.listIterator();
  216. while (e.hasNext()) {
  217. Integer aIdx = (Integer)e.next();
  218. ansiWidth[aIdx.intValue()]
  219. = mtxTab[glyphIdx].getWx();
  220. log.debug("Added width "
  221. + mtxTab[glyphIdx].getWx()
  222. + " uni: " + j
  223. + " ansi: " + aIdx.intValue());
  224. }
  225. }
  226. log.debug("Idx: "
  227. + glyphIdx
  228. + " Delta: " + cmapDeltas[i]
  229. + " Unicode: " + j
  230. + " name: " + mtxTab[glyphIdx].getName());
  231. } else {
  232. glyphIdx = (j + cmapDeltas[i]) & 0xffff;
  233. if (glyphIdx < mtxTab.length) {
  234. mtxTab[glyphIdx].getUnicodeIndex().add(new Integer(j));
  235. } else {
  236. log.debug("Glyph " + glyphIdx
  237. + " out of range: "
  238. + mtxTab.length);
  239. }
  240. unicodeMapping.add(new UnicodeMapping(glyphIdx, j));
  241. if (glyphIdx < mtxTab.length) {
  242. mtxTab[glyphIdx].getUnicodeIndex().add(new Integer(j));
  243. } else {
  244. log.debug("Glyph " + glyphIdx
  245. + " out of range: "
  246. + mtxTab.length);
  247. }
  248. // Also add winAnsiWidth
  249. List v = (List)ansiIndex.get(new Integer(j));
  250. if (v != null) {
  251. Iterator e = v.listIterator();
  252. while (e.hasNext()) {
  253. Integer aIdx = (Integer)e.next();
  254. ansiWidth[aIdx.intValue()] = mtxTab[glyphIdx].getWx();
  255. }
  256. }
  257. //getLogger().debug("IIdx: " +
  258. // mtxPtr +
  259. // " Delta: " + cmap_deltas[i] +
  260. // " Unicode: " + j +
  261. // " name: " +
  262. // mtxTab[(j+cmap_deltas[i]) & 0xffff].name);
  263. }
  264. if (glyphIdx < mtxTab.length) {
  265. if (mtxTab[glyphIdx].getUnicodeIndex().size() < 2) {
  266. mtxPtr++;
  267. }
  268. }
  269. }
  270. }
  271. }
  272. }
  273. return true;
  274. }
  275. /**
  276. * Print first char/last char
  277. */
  278. private void printMaxMin() {
  279. int min = 255;
  280. int max = 0;
  281. for (int i = 0; i < mtxTab.length; i++) {
  282. if (mtxTab[i].getIndex() < min) {
  283. min = mtxTab[i].getIndex();
  284. }
  285. if (mtxTab[i].getIndex() > max) {
  286. max = mtxTab[i].getIndex();
  287. }
  288. }
  289. log.info("Min: " + min);
  290. log.info("Max: " + max);
  291. }
  292. /**
  293. * Reads the font using a FontFileReader.
  294. *
  295. * @param in The FontFileReader to use
  296. * @throws IOException In case of an I/O problem
  297. */
  298. public void readFont(FontFileReader in) throws IOException {
  299. readFont(in, (String)null);
  300. }
  301. /**
  302. * initialize the ansiWidths array (for winAnsiEncoding)
  303. * and fill with the missingwidth
  304. */
  305. private void initAnsiWidths() {
  306. ansiWidth = new int[256];
  307. for (int i = 0; i < 256; i++) {
  308. ansiWidth[i] = mtxTab[0].getWx();
  309. }
  310. // Create an index hash to the ansiWidth
  311. // Can't just index the winAnsiEncoding when inserting widths
  312. // same char (eg bullet) is repeated more than one place
  313. ansiIndex = new java.util.HashMap();
  314. for (int i = 32; i < Glyphs.WINANSI_ENCODING.length; i++) {
  315. Integer ansi = new Integer(i);
  316. Integer uni = new Integer((int)Glyphs.WINANSI_ENCODING[i]);
  317. List v = (List)ansiIndex.get(uni);
  318. if (v == null) {
  319. v = new java.util.ArrayList();
  320. ansiIndex.put(uni, v);
  321. }
  322. v.add(ansi);
  323. }
  324. }
  325. /**
  326. * Read the font data.
  327. * If the fontfile is a TrueType Collection (.ttc file)
  328. * the name of the font to read data for must be supplied,
  329. * else the name is ignored.
  330. *
  331. * @param in The FontFileReader to use
  332. * @param name The name of the font
  333. * @return boolean Returns true if the font is valid
  334. * @throws IOException In case of an I/O problem
  335. */
  336. public boolean readFont(FontFileReader in, String name) throws IOException {
  337. /*
  338. * Check if TrueType collection, and that the name
  339. * exists in the collection
  340. */
  341. if (!checkTTC(in, name)) {
  342. if (name == null) {
  343. throw new IllegalArgumentException(
  344. "For TrueType collection you must specify which font "
  345. + "to select (-ttcname)");
  346. } else {
  347. throw new IOException(
  348. "Name does not exist in the TrueType collection: " + name);
  349. }
  350. }
  351. readDirTabs(in);
  352. readFontHeader(in);
  353. getNumGlyphs(in);
  354. log.info("Number of glyphs in font: " + numberOfGlyphs);
  355. readHorizontalHeader(in);
  356. readHorizontalMetrics(in);
  357. initAnsiWidths();
  358. readPostScript(in);
  359. readOS2(in);
  360. readIndexToLocation(in);
  361. readGlyf(in);
  362. readName(in);
  363. boolean pcltFound = readPCLT(in);
  364. // Read cmap table and fill in ansiwidths
  365. boolean valid = readCMAP(in);
  366. if (!valid) {
  367. return false;
  368. }
  369. // Create cmaps for bfentries
  370. createCMaps();
  371. // print_max_min();
  372. readKerning(in);
  373. if (!pcltFound) {
  374. guessPCLTValuesFromBBox();
  375. }
  376. return true;
  377. }
  378. private void createCMaps() {
  379. cmaps = new java.util.ArrayList();
  380. TTFCmapEntry tce = new TTFCmapEntry();
  381. Iterator e = unicodeMapping.listIterator();
  382. UnicodeMapping um = (UnicodeMapping)e.next();
  383. UnicodeMapping lastMapping = um;
  384. tce.setUnicodeStart(um.getUnicodeIndex());
  385. tce.setGlyphStartIndex(um.getGlyphIndex());
  386. while (e.hasNext()) {
  387. um = (UnicodeMapping)e.next();
  388. if (((lastMapping.getUnicodeIndex() + 1) != um.getUnicodeIndex())
  389. || ((lastMapping.getGlyphIndex() + 1) != um.getGlyphIndex())) {
  390. tce.setUnicodeEnd(lastMapping.getUnicodeIndex());
  391. cmaps.add(tce);
  392. tce = new TTFCmapEntry();
  393. tce.setUnicodeStart(um.getUnicodeIndex());
  394. tce.setGlyphStartIndex(um.getGlyphIndex());
  395. }
  396. lastMapping = um;
  397. }
  398. tce.setUnicodeEnd(um.getUnicodeIndex());
  399. cmaps.add(tce);
  400. }
  401. /**
  402. * Returns the Windows name of the font.
  403. * @return String The Windows name
  404. */
  405. public String getWindowsName() {
  406. return familyName + "," + subFamilyName;
  407. }
  408. /**
  409. * Returns the PostScript name of the font.
  410. * @return String The PostScript name
  411. */
  412. public String getPostScriptName() {
  413. if ("Regular".equals(subFamilyName) || "Roman".equals(subFamilyName)) {
  414. return familyName;
  415. } else {
  416. return familyName + "," + subFamilyName;
  417. }
  418. }
  419. /**
  420. * Returns the font family name of the font.
  421. * @return String The family name
  422. */
  423. public String getFamilyName() {
  424. return familyName;
  425. }
  426. /**
  427. * Returns the name of the character set used.
  428. * @return String The caracter set
  429. */
  430. public String getCharSetName() {
  431. return encoding;
  432. }
  433. /**
  434. * Returns the CapHeight attribute of the font.
  435. * @return int The CapHeight
  436. */
  437. public int getCapHeight() {
  438. return (int)convertTTFUnit2PDFUnit(capHeight);
  439. }
  440. /**
  441. * Returns the XHeight attribute of the font.
  442. * @return int The XHeight
  443. */
  444. public int getXHeight() {
  445. return (int)convertTTFUnit2PDFUnit(xHeight);
  446. }
  447. /**
  448. * Returns the Flags attribute of the font.
  449. * @return int The Flags
  450. */
  451. public int getFlags() {
  452. int flags = 32; // Use Adobe Standard charset
  453. if (italicAngle != 0) {
  454. flags = flags | 64;
  455. }
  456. if (isFixedPitch != 0) {
  457. flags = flags | 2;
  458. }
  459. if (hasSerifs) {
  460. flags = flags | 1;
  461. }
  462. return flags;
  463. }
  464. /**
  465. * Returns the StemV attribute of the font.
  466. * @return String The StemV
  467. */
  468. public String getStemV() {
  469. return "0";
  470. }
  471. /**
  472. * Returns the ItalicAngle attribute of the font.
  473. * @return String The ItalicAngle
  474. */
  475. public String getItalicAngle() {
  476. String ia = Short.toString((short)(italicAngle / 0x10000));
  477. // This is the correct italic angle, however only int italic
  478. // angles are supported at the moment so this is commented out.
  479. /*
  480. * if ((italicAngle % 0x10000) > 0 )
  481. * ia=ia+(comma+Short.toString((short)((short)((italicAngle % 0x10000)*1000)/0x10000)));
  482. */
  483. return ia;
  484. }
  485. /**
  486. * Returns the font bounding box.
  487. * @return int[] The font bbox
  488. */
  489. public int[] getFontBBox() {
  490. final int[] fbb = new int[4];
  491. fbb[0] = (int)convertTTFUnit2PDFUnit(fontBBox1);
  492. fbb[1] = (int)convertTTFUnit2PDFUnit(fontBBox2);
  493. fbb[2] = (int)convertTTFUnit2PDFUnit(fontBBox3);
  494. fbb[3] = (int)convertTTFUnit2PDFUnit(fontBBox4);
  495. return fbb;
  496. }
  497. /**
  498. * Returns the LowerCaseAscent attribute of the font.
  499. * @return int The LowerCaseAscent
  500. */
  501. public int getLowerCaseAscent() {
  502. return (int)convertTTFUnit2PDFUnit(ascender);
  503. }
  504. /**
  505. * Returns the LowerCaseDescent attribute of the font.
  506. * @return int The LowerCaseDescent
  507. */
  508. public int getLowerCaseDescent() {
  509. return (int)convertTTFUnit2PDFUnit(descender);
  510. }
  511. /**
  512. * Returns the index of the last character, but this is for WinAnsiEncoding
  513. * only, so the last char is < 256.
  514. * @return short Index of the last character (<256)
  515. */
  516. public short getLastChar() {
  517. return lastChar;
  518. }
  519. /**
  520. * Returns the index of the first character.
  521. * @return short Index of the first character
  522. */
  523. public short getFirstChar() {
  524. return firstChar;
  525. }
  526. /**
  527. * Returns an array of character widths.
  528. * @return int[] The character widths
  529. */
  530. public int[] getWidths() {
  531. int[] wx = new int[mtxTab.length];
  532. for (int i = 0; i < wx.length; i++) {
  533. wx[i] = (int)convertTTFUnit2PDFUnit(mtxTab[i].getWx());
  534. }
  535. return wx;
  536. }
  537. /**
  538. * Returns the width of a given character.
  539. * @param idx Index of the character
  540. * @return int Standard width
  541. */
  542. public int getCharWidth(int idx) {
  543. return (int)convertTTFUnit2PDFUnit(ansiWidth[idx]);
  544. }
  545. /**
  546. * Returns the kerning table.
  547. * @return Map The kerning table
  548. */
  549. public Map getKerning() {
  550. return kerningTab;
  551. }
  552. /**
  553. * Returns the ANSI kerning table.
  554. * @return Map The ANSI kerning table
  555. */
  556. public Map getAnsiKerning() {
  557. return ansiKerningTab;
  558. }
  559. /**
  560. * Indicates if the font may be embedded.
  561. * @return boolean True if it may be embedded
  562. */
  563. public boolean isEmbeddable() {
  564. return isEmbeddable;
  565. }
  566. /**
  567. * Read Table Directory from the current position in the
  568. * FontFileReader and fill the global HashMap dirTabs
  569. * with the table name (String) as key and a TTFDirTabEntry
  570. * as value.
  571. * @param in FontFileReader to read the table directory from
  572. * @throws IOException in case of an I/O problem
  573. */
  574. protected void readDirTabs(FontFileReader in) throws IOException {
  575. in.skip(4); // TTF_FIXED_SIZE
  576. int ntabs = in.readTTFUShort();
  577. in.skip(6); // 3xTTF_USHORT_SIZE
  578. dirTabs = new java.util.HashMap();
  579. TTFDirTabEntry[] pd = new TTFDirTabEntry[ntabs];
  580. log.debug("Reading " + ntabs + " dir tables");
  581. for (int i = 0; i < ntabs; i++) {
  582. pd[i] = new TTFDirTabEntry();
  583. dirTabs.put(pd[i].read(in), pd[i]);
  584. }
  585. }
  586. /**
  587. * Read the "head" table, this reads the bounding box and
  588. * sets the upem (unitsPerEM) variable
  589. * @param in FontFileReader to read the header from
  590. * @throws IOException in case of an I/O problem
  591. */
  592. protected void readFontHeader(FontFileReader in) throws IOException {
  593. seekTab(in, "head", 2 * 4 + 2 * 4 + 2);
  594. upem = in.readTTFUShort();
  595. in.skip(16);
  596. fontBBox1 = in.readTTFShort();
  597. fontBBox2 = in.readTTFShort();
  598. fontBBox3 = in.readTTFShort();
  599. fontBBox4 = in.readTTFShort();
  600. in.skip(2 + 2 + 2);
  601. locaFormat = in.readTTFShort();
  602. }
  603. /**
  604. * Read the number of glyphs from the "maxp" table
  605. * @param in FontFileReader to read the number of glyphs from
  606. * @throws IOException in case of an I/O problem
  607. */
  608. protected void getNumGlyphs(FontFileReader in) throws IOException {
  609. seekTab(in, "maxp", 4);
  610. numberOfGlyphs = in.readTTFUShort();
  611. }
  612. /**
  613. * Read the "hhea" table to find the ascender and descender and
  614. * size of "hmtx" table, as a fixed size font might have only
  615. * one width.
  616. * @param in FontFileReader to read the hhea table from
  617. * @throws IOException in case of an I/O problem
  618. */
  619. protected void readHorizontalHeader(FontFileReader in)
  620. throws IOException {
  621. seekTab(in, "hhea", 4);
  622. ascender = in.readTTFShort(); // Use sTypoAscender in "OS/2" table?
  623. descender = in.readTTFShort(); // Use sTypoDescender in "OS/2" table?
  624. in.skip(2 + 2 + 3 * 2 + 8 * 2);
  625. nhmtx = in.readTTFUShort();
  626. log.debug("Number of horizontal metrics: " + nhmtx);
  627. //Check OS/2 table for ascender/descender if necessary
  628. if (ascender == 0 || descender == 0) {
  629. seekTab(in, "OS/2", 68);
  630. if (this.currentDirTab.getLength() >= 78) {
  631. ascender = in.readTTFShort(); //sTypoAscender
  632. descender = in.readTTFShort(); //sTypoDescender
  633. }
  634. }
  635. }
  636. /**
  637. * Read "hmtx" table and put the horizontal metrics
  638. * in the mtxTab array. If the number of metrics is less
  639. * than the number of glyphs (eg fixed size fonts), extend
  640. * the mtxTab array and fill in the missing widths
  641. * @param in FontFileReader to read the hmtx table from
  642. * @throws IOException in case of an I/O problem
  643. */
  644. protected void readHorizontalMetrics(FontFileReader in)
  645. throws IOException {
  646. seekTab(in, "hmtx", 0);
  647. int mtxSize = Math.max(numberOfGlyphs, nhmtx);
  648. mtxTab = new TTFMtxEntry[mtxSize];
  649. if (TRACE_ENABLED) {
  650. log.debug("*** Widths array: \n");
  651. }
  652. for (int i = 0; i < mtxSize; i++) {
  653. mtxTab[i] = new TTFMtxEntry();
  654. }
  655. for (int i = 0; i < nhmtx; i++) {
  656. mtxTab[i].setWx(in.readTTFUShort());
  657. mtxTab[i].setLsb(in.readTTFUShort());
  658. if (TRACE_ENABLED) {
  659. if (log.isDebugEnabled()) {
  660. log.debug(" width[" + i + "] = "
  661. + convertTTFUnit2PDFUnit(mtxTab[i].getWx()) + ";");
  662. }
  663. }
  664. }
  665. if (nhmtx < mtxSize) {
  666. // Fill in the missing widths
  667. int lastWidth = mtxTab[nhmtx - 1].getWx();
  668. for (int i = nhmtx; i < mtxSize; i++) {
  669. mtxTab[i].setWx(lastWidth);
  670. mtxTab[i].setLsb(in.readTTFUShort());
  671. }
  672. }
  673. }
  674. /**
  675. * Read the "post" table
  676. * containing the PostScript names of the glyphs.
  677. */
  678. private final void readPostScript(FontFileReader in) throws IOException {
  679. seekTab(in, "post", 0);
  680. postFormat = in.readTTFLong();
  681. italicAngle = in.readTTFULong();
  682. underlinePosition = in.readTTFShort();
  683. underlineThickness = in.readTTFShort();
  684. isFixedPitch = in.readTTFULong();
  685. //Skip memory usage values
  686. in.skip(4 * 4);
  687. log.debug("PostScript format: " + postFormat);
  688. switch (postFormat) {
  689. case 0x00010000:
  690. log.debug("PostScript format 1");
  691. for (int i = 0; i < Glyphs.MAC_GLYPH_NAMES.length; i++) {
  692. mtxTab[i].setName(Glyphs.MAC_GLYPH_NAMES[i]);
  693. }
  694. break;
  695. case 0x00020000:
  696. log.debug("PostScript format 2");
  697. int numGlyphStrings = 0;
  698. // Read Number of Glyphs
  699. int l = in.readTTFUShort();
  700. // Read indexes
  701. for (int i = 0; i < l; i++) {
  702. mtxTab[i].setIndex(in.readTTFUShort());
  703. if (mtxTab[i].getIndex() > 257) {
  704. //Index is not in the Macintosh standard set
  705. numGlyphStrings++;
  706. }
  707. if (log.isDebugEnabled()) {
  708. log.debug("PostScript index: " + mtxTab[i].getIndexAsString());
  709. }
  710. }
  711. // firstChar=minIndex;
  712. String[] psGlyphsBuffer = new String[numGlyphStrings];
  713. if (log.isDebugEnabled()) {
  714. log.debug("Reading " + numGlyphStrings
  715. + " glyphnames, that are not in the standard Macintosh"
  716. + " set. Total number of glyphs=" + l);
  717. }
  718. for (int i = 0; i < psGlyphsBuffer.length; i++) {
  719. psGlyphsBuffer[i] = in.readTTFString(in.readTTFUByte());
  720. }
  721. //Set glyph names
  722. for (int i = 0; i < l; i++) {
  723. if (mtxTab[i].getIndex() < NMACGLYPHS) {
  724. mtxTab[i].setName(Glyphs.MAC_GLYPH_NAMES[mtxTab[i].getIndex()]);
  725. } else {
  726. if (!mtxTab[i].isIndexReserved()) {
  727. int k = mtxTab[i].getIndex() - NMACGLYPHS;
  728. if (log.isDebugEnabled()) {
  729. log.debug(k + " i=" + i + " mtx=" + mtxTab.length
  730. + " ps=" + psGlyphsBuffer.length);
  731. }
  732. mtxTab[i].setName(psGlyphsBuffer[k]);
  733. }
  734. }
  735. }
  736. break;
  737. case 0x00030000:
  738. // PostScript format 3 contains no glyph names
  739. log.debug("PostScript format 3");
  740. break;
  741. default:
  742. log.error("Unknown PostScript format: " + postFormat);
  743. }
  744. }
  745. /**
  746. * Read the "OS/2" table
  747. */
  748. private final void readOS2(FontFileReader in) throws IOException {
  749. // Check if font is embeddable
  750. if (dirTabs.get("OS/2") != null) {
  751. seekTab(in, "OS/2", 2 * 4);
  752. int fsType = in.readTTFUShort();
  753. if (fsType == 2) {
  754. isEmbeddable = false;
  755. } else {
  756. isEmbeddable = true;
  757. }
  758. } else {
  759. isEmbeddable = true;
  760. }
  761. }
  762. /**
  763. * Read the "loca" table.
  764. * @param in FontFileReader to read from
  765. * @throws IOException In case of a I/O problem
  766. */
  767. protected final void readIndexToLocation(FontFileReader in)
  768. throws IOException {
  769. seekTab(in, "loca", 0);
  770. for (int i = 0; i < numberOfGlyphs; i++) {
  771. mtxTab[i].setOffset(locaFormat == 1 ? in.readTTFULong()
  772. : (in.readTTFUShort() << 1));
  773. }
  774. lastLoca = (locaFormat == 1 ? in.readTTFULong()
  775. : (in.readTTFUShort() << 1));
  776. }
  777. /**
  778. * Read the "glyf" table to find the bounding boxes.
  779. * @param in FontFileReader to read from
  780. * @throws IOException In case of a I/O problem
  781. */
  782. private final void readGlyf(FontFileReader in) throws IOException {
  783. TTFDirTabEntry dirTab = (TTFDirTabEntry)dirTabs.get("glyf");
  784. for (int i = 0; i < (numberOfGlyphs - 1); i++) {
  785. if (mtxTab[i].getOffset() != mtxTab[i + 1].getOffset()) {
  786. in.seekSet(dirTab.getOffset() + mtxTab[i].getOffset());
  787. in.skip(2);
  788. final int[] bbox = {
  789. in.readTTFShort(),
  790. in.readTTFShort(),
  791. in.readTTFShort(),
  792. in.readTTFShort()};
  793. mtxTab[i].setBoundingBox(bbox);
  794. } else {
  795. mtxTab[i].setBoundingBox(mtxTab[0].getBoundingBox());
  796. }
  797. }
  798. long n = ((TTFDirTabEntry)dirTabs.get("glyf")).getOffset();
  799. for (int i = 0; i < numberOfGlyphs; i++) {
  800. if ((i + 1) >= mtxTab.length
  801. || mtxTab[i].getOffset() != mtxTab[i + 1].getOffset()) {
  802. in.seekSet(n + mtxTab[i].getOffset());
  803. in.skip(2);
  804. final int[] bbox = {
  805. in.readTTFShort(),
  806. in.readTTFShort(),
  807. in.readTTFShort(),
  808. in.readTTFShort()};
  809. mtxTab[i].setBoundingBox(bbox);
  810. } else {
  811. /**@todo Verify that this is correct, looks like a copy/paste bug (jm)*/
  812. final int bbox0 = mtxTab[0].getBoundingBox()[0];
  813. final int[] bbox = {bbox0, bbox0, bbox0, bbox0};
  814. mtxTab[i].setBoundingBox(bbox);
  815. /* Original code
  816. mtxTab[i].bbox[0] = mtxTab[0].bbox[0];
  817. mtxTab[i].bbox[1] = mtxTab[0].bbox[0];
  818. mtxTab[i].bbox[2] = mtxTab[0].bbox[0];
  819. mtxTab[i].bbox[3] = mtxTab[0].bbox[0]; */
  820. }
  821. log.debug(mtxTab[i].toString(this));
  822. }
  823. }
  824. /**
  825. * Read the "name" table.
  826. * @param in FontFileReader to read from
  827. * @throws IOException In case of a I/O problem
  828. */
  829. private final void readName(FontFileReader in) throws IOException {
  830. seekTab(in, "name", 2);
  831. int i = in.getCurrentPos();
  832. int n = in.readTTFUShort();
  833. int j = in.readTTFUShort() + i - 2;
  834. i += 2 * 2;
  835. while (n-- > 0) {
  836. // getLogger().debug("Iteration: " + n);
  837. in.seekSet(i);
  838. final int platformID = in.readTTFUShort();
  839. final int encodingID = in.readTTFUShort();
  840. final int languageID = in.readTTFUShort();
  841. int k = in.readTTFUShort();
  842. int l = in.readTTFUShort();
  843. if (((platformID == 1 || platformID == 3)
  844. && (encodingID == 0 || encodingID == 1))
  845. && (k == 1 || k == 2 || k == 0 || k == 4 || k == 6)) {
  846. in.seekSet(j + in.readTTFUShort());
  847. String txt = in.readTTFString(l);
  848. log.debug(platformID + " "
  849. + encodingID + " "
  850. + languageID + " "
  851. + k + " " + txt);
  852. switch (k) {
  853. case 0:
  854. notice = txt;
  855. break;
  856. case 1:
  857. familyName = txt;
  858. break;
  859. case 2:
  860. subFamilyName = txt;
  861. break;
  862. case 4:
  863. fullName = txt;
  864. break;
  865. case 6:
  866. fontName = txt;
  867. break;
  868. }
  869. if (!notice.equals("")
  870. && !fullName.equals("")
  871. && !fontName.equals("")
  872. && !familyName.equals("")
  873. && !subFamilyName.equals("")) {
  874. break;
  875. }
  876. }
  877. i += 6 * 2;
  878. }
  879. }
  880. /**
  881. * Read the "PCLT" table to find xHeight and capHeight.
  882. * @param in FontFileReader to read from
  883. * @throws IOException In case of a I/O problem
  884. */
  885. private final boolean readPCLT(FontFileReader in) throws IOException {
  886. TTFDirTabEntry dirTab = (TTFDirTabEntry)dirTabs.get("PCLT");
  887. if (dirTab != null) {
  888. in.seekSet(dirTab.getOffset() + 4 + 4 + 2);
  889. xHeight = in.readTTFUShort();
  890. in.skip(2 * 2);
  891. capHeight = in.readTTFUShort();
  892. in.skip(2 + 16 + 8 + 6 + 1 + 1);
  893. int serifStyle = in.readTTFUByte();
  894. serifStyle = serifStyle >> 6;
  895. serifStyle = serifStyle & 3;
  896. if (serifStyle == 1) {
  897. hasSerifs = false;
  898. } else {
  899. hasSerifs = true;
  900. }
  901. return true;
  902. } else {
  903. return false;
  904. }
  905. }
  906. private void guessPCLTValuesFromBBox() {
  907. // Approximate capHeight from height of "H"
  908. // It's most unlikly that a font misses the PCLT table
  909. // This also assumes that postscriptnames exists ("H")
  910. // Should look it up int the cmap (that wouldn't help
  911. // for charsets without H anyway...)
  912. // Same for xHeight with the letter "x"
  913. boolean capHeightFound = false;
  914. boolean xHeightFound = false;
  915. for (int i = 0; i < mtxTab.length; i++) {
  916. if ("H".equals(mtxTab[i].getName())) {
  917. capHeight = mtxTab[i].getBoundingBox()[3] - mtxTab[i].getBoundingBox()[1];
  918. capHeightFound = true;
  919. } else if ("x".equals(mtxTab[i].getName())) {
  920. xHeight = mtxTab[i].getBoundingBox()[3] - mtxTab[i].getBoundingBox()[1];
  921. xHeightFound = true;
  922. } else {
  923. // OpenType Fonts with a version 3.0 "post" table don't have glyph names.
  924. // Use Unicode indices instead.
  925. List unicodeIndex = mtxTab[i].getUnicodeIndex();
  926. if (unicodeIndex.size() > 0) {
  927. //Only the first index is used
  928. char ch = (char)((Integer)unicodeIndex.get(0)).intValue();
  929. if (ch == 'H') {
  930. capHeight = mtxTab[i].getBoundingBox()[3] - mtxTab[i].getBoundingBox()[1];
  931. capHeightFound = true;
  932. } else if (ch == 'x') {
  933. xHeight = mtxTab[i].getBoundingBox()[3] - mtxTab[i].getBoundingBox()[1];
  934. xHeightFound = true;
  935. }
  936. }
  937. }
  938. }
  939. if (!capHeightFound) {
  940. log.warn("capHeight value could not be determined. The font may not work as expected.");
  941. }
  942. if (!xHeightFound) {
  943. log.warn("xHeight value could not be determined. The font may not work as expected.");
  944. }
  945. }
  946. /**
  947. * Read the kerning table, create a table for both CIDs and
  948. * winAnsiEncoding.
  949. * @param in FontFileReader to read from
  950. * @throws IOException In case of a I/O problem
  951. */
  952. private final void readKerning(FontFileReader in) throws IOException {
  953. // Read kerning
  954. kerningTab = new java.util.HashMap();
  955. ansiKerningTab = new java.util.HashMap();
  956. TTFDirTabEntry dirTab = (TTFDirTabEntry)dirTabs.get("kern");
  957. if (dirTab != null) {
  958. seekTab(in, "kern", 2);
  959. for (int n = in.readTTFUShort(); n > 0; n--) {
  960. in.skip(2 * 2);
  961. int k = in.readTTFUShort();
  962. if (!((k & 1) != 0) || (k & 2) != 0 || (k & 4) != 0) {
  963. return;
  964. }
  965. if ((k >> 8) != 0) {
  966. continue;
  967. }
  968. k = in.readTTFUShort();
  969. in.skip(3 * 2);
  970. while (k-- > 0) {
  971. int i = in.readTTFUShort();
  972. int j = in.readTTFUShort();
  973. int kpx = in.readTTFShort();
  974. if (kpx != 0) {
  975. // CID table
  976. Integer iObj = new Integer(i);
  977. Map adjTab = (Map)kerningTab.get(iObj);
  978. if (adjTab == null) {
  979. adjTab = new java.util.HashMap();
  980. }
  981. adjTab.put(new Integer(j),
  982. new Integer((int)convertTTFUnit2PDFUnit(kpx)));
  983. kerningTab.put(iObj, adjTab);
  984. }
  985. }
  986. }
  987. // getLogger().debug(kerningTab.toString());
  988. // Create winAnsiEncoded kerning table
  989. Iterator ae = kerningTab.keySet().iterator();
  990. while (ae.hasNext()) {
  991. Integer cidKey = (Integer)ae.next();
  992. Map akpx = new java.util.HashMap();
  993. Map ckpx = (Map)kerningTab.get(cidKey);
  994. Iterator aee = ckpx.keySet().iterator();
  995. while (aee.hasNext()) {
  996. Integer cidKey2 = (Integer)aee.next();
  997. Integer kern = (Integer)ckpx.get(cidKey2);
  998. Iterator uniMap = mtxTab[cidKey2.intValue()].getUnicodeIndex().listIterator();
  999. while (uniMap.hasNext()) {
  1000. Integer unicodeKey = (Integer)uniMap.next();
  1001. Integer[] ansiKeys = unicodeToWinAnsi(unicodeKey.intValue());
  1002. for (int u = 0; u < ansiKeys.length; u++) {
  1003. akpx.put(ansiKeys[u], kern);
  1004. }
  1005. }
  1006. }
  1007. if (akpx.size() > 0) {
  1008. Iterator uniMap = mtxTab[cidKey.intValue()].getUnicodeIndex().listIterator();
  1009. while (uniMap.hasNext()) {
  1010. Integer unicodeKey = (Integer)uniMap.next();
  1011. Integer[] ansiKeys = unicodeToWinAnsi(unicodeKey.intValue());
  1012. for (int u = 0; u < ansiKeys.length; u++) {
  1013. ansiKerningTab.put(ansiKeys[u], akpx);
  1014. }
  1015. }
  1016. }
  1017. }
  1018. }
  1019. }
  1020. /**
  1021. * Return a List with TTFCmapEntry.
  1022. * @return A list of TTFCmapEntry objects
  1023. */
  1024. public List getCMaps() {
  1025. return cmaps;
  1026. }
  1027. /**
  1028. * Check if this is a TrueType collection and that the given
  1029. * name exists in the collection.
  1030. * If it does, set offset in fontfile to the beginning of
  1031. * the Table Directory for that font.
  1032. * @param in FontFileReader to read from
  1033. * @param name The name to check
  1034. * @return True if not collection or font name present, false otherwise
  1035. * @throws IOException In case of an I/O problem
  1036. */
  1037. protected final boolean checkTTC(FontFileReader in, String name) throws IOException {
  1038. String tag = in.readTTFString(4);
  1039. if ("ttcf".equals(tag)) {
  1040. // This is a TrueType Collection
  1041. in.skip(4);
  1042. // Read directory offsets
  1043. int numDirectories = (int)in.readTTFULong();
  1044. // int numDirectories=in.readTTFUShort();
  1045. long[] dirOffsets = new long[numDirectories];
  1046. for (int i = 0; i < numDirectories; i++) {
  1047. dirOffsets[i] = in.readTTFULong();
  1048. }
  1049. log.info("This is a TrueType collection file with "
  1050. + numDirectories + " fonts");
  1051. log.info("Containing the following fonts: ");
  1052. // Read all the directories and name tables to check
  1053. // If the font exists - this is a bit ugly, but...
  1054. boolean found = false;
  1055. // Iterate through all name tables even if font
  1056. // Is found, just to show all the names
  1057. long dirTabOffset = 0;
  1058. for (int i = 0; (i < numDirectories); i++) {
  1059. in.seekSet(dirOffsets[i]);
  1060. readDirTabs(in);
  1061. readName(in);
  1062. if (fullName.equals(name)) {
  1063. found = true;
  1064. dirTabOffset = dirOffsets[i];
  1065. log.info(fullName + " <-- selected");
  1066. } else {
  1067. log.info(fullName);
  1068. }
  1069. // Reset names
  1070. notice = "";
  1071. fullName = "";
  1072. familyName = "";
  1073. fontName = "";
  1074. subFamilyName = "";
  1075. }
  1076. in.seekSet(dirTabOffset);
  1077. return found;
  1078. } else {
  1079. in.seekSet(0);
  1080. return true;
  1081. }
  1082. }
  1083. /*
  1084. * Helper classes, they are not very efficient, but that really
  1085. * doesn't matter...
  1086. */
  1087. private Integer[] unicodeToWinAnsi(int unicode) {
  1088. List ret = new java.util.ArrayList();
  1089. for (int i = 32; i < Glyphs.WINANSI_ENCODING.length; i++) {
  1090. if (unicode == Glyphs.WINANSI_ENCODING[i]) {
  1091. ret.add(new Integer(i));
  1092. }
  1093. }
  1094. return (Integer[])ret.toArray(new Integer[0]);
  1095. }
  1096. /**
  1097. * Dumps a few informational values to System.out.
  1098. */
  1099. public void printStuff() {
  1100. System.out.println("Font name: " + fontName);
  1101. System.out.println("Full name: " + fullName);
  1102. System.out.println("Family name: " + familyName);
  1103. System.out.println("Subfamily name: " + subFamilyName);
  1104. System.out.println("Notice: " + notice);
  1105. System.out.println("xHeight: " + (int)convertTTFUnit2PDFUnit(xHeight));
  1106. System.out.println("capheight: " + (int)convertTTFUnit2PDFUnit(capHeight));
  1107. int italic = (int)(italicAngle >> 16);
  1108. System.out.println("Italic: " + italic);
  1109. System.out.print("ItalicAngle: " + (short)(italicAngle / 0x10000));
  1110. if ((italicAngle % 0x10000) > 0) {
  1111. System.out.print("."
  1112. + (short)((italicAngle % 0x10000) * 1000)
  1113. / 0x10000);
  1114. }
  1115. System.out.println();
  1116. System.out.println("Ascender: " + convertTTFUnit2PDFUnit(ascender));
  1117. System.out.println("Descender: " + convertTTFUnit2PDFUnit(descender));
  1118. System.out.println("FontBBox: [" + (int)convertTTFUnit2PDFUnit(fontBBox1)
  1119. + " " + (int)convertTTFUnit2PDFUnit(fontBBox2) + " "
  1120. + (int)convertTTFUnit2PDFUnit(fontBBox3) + " "
  1121. + (int)convertTTFUnit2PDFUnit(fontBBox4) + "]");
  1122. }
  1123. /**
  1124. * Static main method to get info about a TrueType font.
  1125. * @param args The command line arguments
  1126. */
  1127. public static void main(String[] args) {
  1128. try {
  1129. TTFFile ttfFile = new TTFFile();
  1130. FontFileReader reader = new FontFileReader(args[0]);
  1131. String name = null;
  1132. if (args.length >= 2) {
  1133. name = args[1];
  1134. }
  1135. ttfFile.readFont(reader, name);
  1136. ttfFile.printStuff();
  1137. } catch (IOException ioe) {
  1138. System.err.println("Problem reading font: " + ioe.toString());
  1139. ioe.printStackTrace(System.err);
  1140. }
  1141. }
  1142. }
  1143. /**
  1144. * Key-value helper class
  1145. */
  1146. class UnicodeMapping {
  1147. private int unicodeIndex;
  1148. private int glyphIndex;
  1149. UnicodeMapping(int glyphIndex, int unicodeIndex) {
  1150. this.unicodeIndex = unicodeIndex;
  1151. this.glyphIndex = glyphIndex;
  1152. }
  1153. /**
  1154. * Returns the glyphIndex.
  1155. * @return the glyph index
  1156. */
  1157. public int getGlyphIndex() {
  1158. return glyphIndex;
  1159. }
  1160. /**
  1161. * Returns the unicodeIndex.
  1162. * @return the Unicode index
  1163. */
  1164. public int getUnicodeIndex() {
  1165. return unicodeIndex;
  1166. }
  1167. }