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.

TTFSubSetFile.java 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  1. /*
  2. * $Id$
  3. * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
  4. * For details on use and redistribution please refer to the
  5. * LICENSE file included with these sources.
  6. */
  7. package org.apache.fop.fonts;
  8. import java.io.*;
  9. import java.util.Iterator;
  10. import java.util.HashMap;
  11. import java.util.ArrayList;
  12. /**
  13. * Reads a TrueType file and generates a subset
  14. * That can be used to embed a TrueType CID font
  15. * TrueType tables needed for embedded CID fonts are:
  16. * "head", "hhea", "loca", "maxp", "cvt ", "prep", "glyf", "hmtx" and "fpgm"
  17. * The TrueType spec can be found at the Microsoft
  18. * Typography site: http://www.microsoft.com/truetype/
  19. */
  20. public class TTFSubSetFile extends TTFFile {
  21. byte[] output = null;
  22. int realSize = 0;
  23. int currentPos = 0;
  24. /*
  25. * Offsets in name table to be filled out by table.
  26. * The offsets are to the checkSum field
  27. */
  28. int cvtDirOffset = 0;
  29. int fpgmDirOffset = 0;
  30. int glyfDirOffset = 0;
  31. int headDirOffset = 0;
  32. int hheaDirOffset = 0;
  33. int hmtxDirOffset = 0;
  34. int locaDirOffset = 0;
  35. int maxpDirOffset = 0;
  36. int prepDirOffset = 0;
  37. int checkSumAdjustmentOffset = 0;
  38. int locaOffset = 0;
  39. /**
  40. * Initalize the output array
  41. */
  42. private void init(int size) {
  43. output = new byte[size];
  44. realSize = 0;
  45. currentPos = 0;
  46. // createDirectory()
  47. }
  48. /**
  49. * Create the directory table
  50. */
  51. private void createDirectory() {
  52. int numTables = 9;
  53. // Create the TrueType header
  54. writeByte((byte)0);
  55. writeByte((byte)1);
  56. writeByte((byte)0);
  57. writeByte((byte)0);
  58. realSize += 4;
  59. writeUShort(numTables);
  60. realSize += 2;
  61. // Create searchRange, entrySelector and rangeShift
  62. int maxPow = maxPow2(numTables);
  63. int searchRange = maxPow * 16;
  64. writeUShort(searchRange);
  65. realSize += 2;
  66. writeUShort(maxPow);
  67. realSize += 2;
  68. writeUShort((numTables * 16) - searchRange);
  69. realSize += 2;
  70. // Create space for the table entries
  71. writeString("cvt ");
  72. cvtDirOffset = currentPos;
  73. currentPos += 12;
  74. realSize += 16;
  75. writeString("fpgm");
  76. fpgmDirOffset = currentPos;
  77. currentPos += 12;
  78. realSize += 16;
  79. writeString("glyf");
  80. glyfDirOffset = currentPos;
  81. currentPos += 12;
  82. realSize += 16;
  83. writeString("head");
  84. headDirOffset = currentPos;
  85. currentPos += 12;
  86. realSize += 16;
  87. writeString("hhea");
  88. hheaDirOffset = currentPos;
  89. currentPos += 12;
  90. realSize += 16;
  91. writeString("hmtx");
  92. hmtxDirOffset = currentPos;
  93. currentPos += 12;
  94. realSize += 16;
  95. writeString("loca");
  96. locaDirOffset = currentPos;
  97. currentPos += 12;
  98. realSize += 16;
  99. writeString("maxp");
  100. maxpDirOffset = currentPos;
  101. currentPos += 12;
  102. realSize += 16;
  103. writeString("prep");
  104. prepDirOffset = currentPos;
  105. currentPos += 12;
  106. realSize += 16;
  107. }
  108. /**
  109. * Copy the cvt table as is from original font to subset font
  110. */
  111. private void createCvt(FontFileReader in) throws IOException {
  112. TTFDirTabEntry entry = (TTFDirTabEntry)dirTabs.get("cvt ");
  113. if (entry != null) {
  114. pad4();
  115. seek_tab(in, "cvt ", 0);
  116. System.arraycopy(in.getBytes((int)entry.offset, (int)entry.length),
  117. 0, output, currentPos, (int)entry.length);
  118. int checksum = getCheckSum(currentPos, (int)entry.length);
  119. writeULong(cvtDirOffset, checksum);
  120. writeULong(cvtDirOffset + 4, currentPos);
  121. writeULong(cvtDirOffset + 8, (int)entry.length);
  122. currentPos += (int)entry.length;
  123. realSize += (int)entry.length;
  124. } else {
  125. throw new IOException("Can't find cvt table");
  126. }
  127. }
  128. /**
  129. * Copy the fpgm table as is from original font to subset font
  130. */
  131. private void createFpgm(FontFileReader in) throws IOException {
  132. TTFDirTabEntry entry = (TTFDirTabEntry)dirTabs.get("fpgm");
  133. if (entry != null) {
  134. pad4();
  135. seek_tab(in, "fpgm", 0);
  136. System.arraycopy(in.getBytes((int)entry.offset, (int)entry.length),
  137. 0, output, currentPos, (int)entry.length);
  138. int checksum = getCheckSum(currentPos, (int)entry.length);
  139. writeULong(fpgmDirOffset, checksum);
  140. writeULong(fpgmDirOffset + 4, currentPos);
  141. writeULong(fpgmDirOffset + 8, (int)entry.length);
  142. currentPos += (int)entry.length;
  143. realSize += (int)entry.length;
  144. } else {
  145. throw new IOException("Can't find fpgm table");
  146. }
  147. }
  148. /**
  149. * Create an empty loca table without updating checksum
  150. */
  151. private void createLoca(int size) throws IOException {
  152. pad4();
  153. locaOffset = currentPos;
  154. writeULong(locaDirOffset + 4, currentPos);
  155. writeULong(locaDirOffset + 8, size * 4 + 4);
  156. currentPos += size * 4 + 4;
  157. realSize += size * 4 + 4;
  158. }
  159. /**
  160. * Copy the maxp table as is from original font to subset font
  161. * and set num glyphs to size
  162. */
  163. private void createMaxp(FontFileReader in, int size) throws IOException {
  164. TTFDirTabEntry entry = (TTFDirTabEntry)dirTabs.get("maxp");
  165. if (entry != null) {
  166. pad4();
  167. seek_tab(in, "maxp", 0);
  168. System.arraycopy(in.getBytes((int)entry.offset, (int)entry.length),
  169. 0, output, currentPos, (int)entry.length);
  170. writeUShort(currentPos + 4, size);
  171. int checksum = getCheckSum(currentPos, (int)entry.length);
  172. writeULong(maxpDirOffset, checksum);
  173. writeULong(maxpDirOffset + 4, currentPos);
  174. writeULong(maxpDirOffset + 8, (int)entry.length);
  175. currentPos += (int)entry.length;
  176. realSize += (int)entry.length;
  177. } else {
  178. throw new IOException("Can't find maxp table");
  179. }
  180. }
  181. /**
  182. * Copy the prep table as is from original font to subset font
  183. */
  184. private void createPrep(FontFileReader in) throws IOException {
  185. TTFDirTabEntry entry = (TTFDirTabEntry)dirTabs.get("prep");
  186. if (entry != null) {
  187. pad4();
  188. seek_tab(in, "prep", 0);
  189. System.arraycopy(in.getBytes((int)entry.offset, (int)entry.length),
  190. 0, output, currentPos, (int)entry.length);
  191. int checksum = getCheckSum(currentPos, (int)entry.length);
  192. writeULong(prepDirOffset, checksum);
  193. writeULong(prepDirOffset + 4, currentPos);
  194. writeULong(prepDirOffset + 8, (int)entry.length);
  195. currentPos += (int)entry.length;
  196. realSize += (int)entry.length;
  197. } else {
  198. throw new IOException("Can't find prep table");
  199. }
  200. }
  201. /**
  202. * Copy the hhea table as is from original font to subset font
  203. * and fill in size of hmtx table
  204. */
  205. private void createHhea(FontFileReader in, int size) throws IOException {
  206. TTFDirTabEntry entry = (TTFDirTabEntry)dirTabs.get("hhea");
  207. if (entry != null) {
  208. pad4();
  209. seek_tab(in, "hhea", 0);
  210. System.arraycopy(in.getBytes((int)entry.offset, (int)entry.length),
  211. 0, output, currentPos, (int)entry.length);
  212. writeUShort((int)entry.length + currentPos - 2, size);
  213. int checksum = getCheckSum(currentPos, (int)entry.length);
  214. writeULong(hheaDirOffset, checksum);
  215. writeULong(hheaDirOffset + 4, currentPos);
  216. writeULong(hheaDirOffset + 8, (int)entry.length);
  217. currentPos += (int)entry.length;
  218. realSize += (int)entry.length;
  219. } else {
  220. throw new IOException("Can't find hhea table");
  221. }
  222. }
  223. /**
  224. * Copy the head table as is from original font to subset font
  225. * and set indexToLocaFormat to long and set
  226. * checkSumAdjustment to 0, store offset to checkSumAdjustment
  227. * in checkSumAdjustmentOffset
  228. */
  229. private void createHead(FontFileReader in) throws IOException {
  230. TTFDirTabEntry entry = (TTFDirTabEntry)dirTabs.get("head");
  231. if (entry != null) {
  232. pad4();
  233. seek_tab(in, "head", 0);
  234. System.arraycopy(in.getBytes((int)entry.offset, (int)entry.length),
  235. 0, output, currentPos, (int)entry.length);
  236. checkSumAdjustmentOffset = currentPos + 8;
  237. output[currentPos + 8] = 0; // Set checkSumAdjustment to 0
  238. output[currentPos + 9] = 0;
  239. output[currentPos + 10] = 0;
  240. output[currentPos + 11] = 0;
  241. output[currentPos + 50] = 0; // long locaformat
  242. output[currentPos + 51] = 1; // long locaformat
  243. int checksum = getCheckSum(currentPos, (int)entry.length);
  244. writeULong(headDirOffset, checksum);
  245. writeULong(headDirOffset + 4, currentPos);
  246. writeULong(headDirOffset + 8, (int)entry.length);
  247. currentPos += (int)entry.length;
  248. realSize += (int)entry.length;
  249. } else {
  250. throw new IOException("Can't find head table");
  251. }
  252. }
  253. /**
  254. * Create the glyf table and fill in loca table
  255. */
  256. private void createGlyf(FontFileReader in,
  257. HashMap glyphs) throws IOException {
  258. TTFDirTabEntry entry = (TTFDirTabEntry)dirTabs.get("glyf");
  259. int size = 0;
  260. int start = 0;
  261. int endOffset = 0; // Store this as the last loca
  262. if (entry != null) {
  263. pad4();
  264. start = currentPos;
  265. for (Iterator e = glyphs.keySet().iterator(); e.hasNext(); ) {
  266. int glyphLength = 0;
  267. Integer origIndex = (Integer)e.next();
  268. Integer subsetIndex = (Integer)glyphs.get(origIndex);
  269. int nextOffset = 0;
  270. if (origIndex.intValue() >= (mtx_tab.length - 1))
  271. nextOffset = (int)lastLoca;
  272. else
  273. nextOffset =
  274. (int)mtx_tab[origIndex.intValue() + 1].offset;
  275. glyphLength = nextOffset
  276. - (int)mtx_tab[origIndex.intValue()].offset;
  277. // Copy glyph
  278. System.arraycopy(in.getBytes((int)entry.offset + (int)mtx_tab[origIndex.intValue()].offset, glyphLength),
  279. 0, output, currentPos, glyphLength);
  280. // Update loca table
  281. writeULong(locaOffset + subsetIndex.intValue() * 4,
  282. currentPos - start);
  283. if ((currentPos - start + glyphLength) > endOffset)
  284. endOffset = (currentPos - start + glyphLength);
  285. currentPos += glyphLength;
  286. realSize += glyphLength;
  287. }
  288. size = currentPos - start;
  289. int checksum = getCheckSum(start, size);
  290. writeULong(glyfDirOffset, checksum);
  291. writeULong(glyfDirOffset + 4, start);
  292. writeULong(glyfDirOffset + 8, size);
  293. currentPos += 12;
  294. realSize += 12;
  295. // Update loca checksum and last loca index
  296. writeULong(locaOffset + glyphs.size() * 4, endOffset);
  297. checksum = getCheckSum(locaOffset, glyphs.size() * 4 + 4);
  298. writeULong(locaDirOffset, checksum);
  299. } else {
  300. throw new IOException("Can't find glyf table");
  301. }
  302. }
  303. /**
  304. * Create the hmtx table by copying metrics from original
  305. * font to subset font. The glyphs hashtable contains an
  306. * Integer key and Integer value that maps the original
  307. * metric (key) to the subset metric (value)
  308. */
  309. private void createHmtx(FontFileReader in,
  310. HashMap glyphs) throws IOException {
  311. TTFDirTabEntry entry = (TTFDirTabEntry)dirTabs.get("hmtx");
  312. int longHorMetricSize = glyphs.size() * 2;
  313. int leftSideBearingSize = glyphs.size() * 2;
  314. int hmtxSize = longHorMetricSize + leftSideBearingSize;
  315. if (entry != null) {
  316. pad4();
  317. int offset = (int)entry.offset;
  318. for (Iterator e = glyphs.keySet().iterator(); e.hasNext(); ) {
  319. Integer origIndex = (Integer)e.next();
  320. Integer subsetIndex = (Integer)glyphs.get(origIndex);
  321. writeUShort(currentPos + subsetIndex.intValue() * 4,
  322. mtx_tab[origIndex.intValue()].wx);
  323. writeUShort(currentPos + subsetIndex.intValue() * 4 + 2,
  324. mtx_tab[origIndex.intValue()].lsb);
  325. }
  326. int checksum = getCheckSum(currentPos, hmtxSize);
  327. writeULong(hmtxDirOffset, checksum);
  328. writeULong(hmtxDirOffset + 4, currentPos);
  329. writeULong(hmtxDirOffset + 8, hmtxSize);
  330. currentPos += hmtxSize;
  331. realSize += hmtxSize;
  332. } else {
  333. throw new IOException("Can't find hmtx table");
  334. }
  335. }
  336. /**
  337. * Returns a List containing the glyph itself plus all glyphs
  338. * that this composite glyph uses
  339. */
  340. private ArrayList getIncludedGlyphs(FontFileReader in, int glyphOffset,
  341. Integer glyphIdx) throws IOException {
  342. ArrayList ret = new ArrayList();
  343. ret.add(glyphIdx);
  344. int offset = glyphOffset + (int)mtx_tab[glyphIdx.intValue()].offset
  345. + 10;
  346. Integer compositeIdx = null;
  347. int flags = 0;
  348. boolean moreComposites = true;
  349. while (moreComposites) {
  350. flags = in.readTTFUShort(offset);
  351. compositeIdx = new Integer(in.readTTFUShort(offset + 2));
  352. ret.add(compositeIdx);
  353. offset += 4;
  354. if ((flags & 1) > 0) {
  355. // ARG_1_AND_ARG_2_ARE_WORDS
  356. offset += 4;
  357. } else {
  358. offset += 2;
  359. }
  360. if ((flags & 8) > 0)
  361. offset += 2; // WE_HAVE_A_SCALE
  362. else if ((flags & 64) > 0)
  363. offset += 4; // WE_HAVE_AN_X_AND_Y_SCALE
  364. else if ((flags & 128) > 0)
  365. offset += 8; // WE_HAVE_A_TWO_BY_TWO
  366. if ((flags & 32) > 0)
  367. moreComposites = true;
  368. else
  369. moreComposites = false;
  370. }
  371. return ret;
  372. }
  373. /**
  374. * Rewrite all compositepointers in glyphindex glyphIdx
  375. *
  376. */
  377. private void remapComposite(FontFileReader in, HashMap glyphs,
  378. int glyphOffset,
  379. Integer glyphIdx) throws IOException {
  380. int offset = glyphOffset + (int)mtx_tab[glyphIdx.intValue()].offset
  381. + 10;
  382. Integer compositeIdx = null;
  383. int flags = 0;
  384. boolean moreComposites = true;
  385. while (moreComposites) {
  386. flags = in.readTTFUShort(offset);
  387. compositeIdx = new Integer(in.readTTFUShort(offset + 2));
  388. Integer newIdx = (Integer)glyphs.get(compositeIdx);
  389. if (newIdx == null) {
  390. // This errormessage would look much better
  391. // if the fontname was printed to
  392. //log.error("An embedded font "
  393. // + "contains bad glyph data. "
  394. // + "Characters might not display "
  395. // + "correctly.");
  396. moreComposites = false;
  397. continue;
  398. }
  399. in.writeTTFUShort(offset + 2, newIdx.intValue());
  400. offset += 4;
  401. if ((flags & 1) > 0) {
  402. // ARG_1_AND_ARG_2_ARE_WORDS
  403. offset += 4;
  404. } else {
  405. offset += 2;
  406. }
  407. if ((flags & 8) > 0) {
  408. offset += 2; // WE_HAVE_A_SCALE
  409. } else if ((flags & 64) > 0) {
  410. offset += 4; // WE_HAVE_AN_X_AND_Y_SCALE
  411. } else if ((flags & 128) > 0) {
  412. offset += 8; // WE_HAVE_A_TWO_BY_TWO
  413. }
  414. if ((flags & 32) > 0)
  415. moreComposites = true;
  416. else
  417. moreComposites = false;
  418. }
  419. }
  420. /**
  421. * Scan all the original glyphs for composite glyphs and add those glyphs
  422. * to the glyphmapping also rewrite the composite glyph pointers to the new
  423. * mapping
  424. */
  425. private void scanGlyphs(FontFileReader in,
  426. HashMap glyphs) throws IOException {
  427. TTFDirTabEntry entry = (TTFDirTabEntry)dirTabs.get("glyf");
  428. HashMap newComposites = null;
  429. HashMap allComposites = new HashMap();
  430. int newIndex = glyphs.size();
  431. if (entry != null) {
  432. while (newComposites == null || newComposites.size() > 0) {
  433. // Inefficient to iterate through all glyphs
  434. newComposites = new HashMap();
  435. for (Iterator e = glyphs.keySet().iterator(); e.hasNext(); ) {
  436. Integer origIndex = (Integer)e.next();
  437. if (in.readTTFShort(entry.offset
  438. + mtx_tab[origIndex.intValue()].offset) < 0) {
  439. // origIndex is a composite glyph
  440. allComposites.put(origIndex, glyphs.get(origIndex));
  441. ArrayList composites =
  442. getIncludedGlyphs(in, (int)entry.offset,
  443. origIndex);
  444. // Iterate through all composites pointed to
  445. // by this composite and check if they exists
  446. // in the glyphs map, add them if not.
  447. for (Iterator cps = composites.iterator();
  448. cps.hasNext(); ) {
  449. Integer cIdx = (Integer)cps.next();
  450. if (glyphs.get(cIdx) == null
  451. && newComposites.get(cIdx) == null) {
  452. newComposites.put(cIdx,
  453. new Integer(newIndex));
  454. newIndex++;
  455. }
  456. }
  457. }
  458. }
  459. // Add composites to glyphs
  460. for (Iterator m = newComposites.keySet().iterator();
  461. m.hasNext(); ) {
  462. Integer im = (Integer)m.next();
  463. glyphs.put(im, newComposites.get(im));
  464. }
  465. }
  466. // Iterate through all composites to remap their composite index
  467. for (Iterator ce = allComposites.keySet().iterator();
  468. ce.hasNext(); ) {
  469. remapComposite(in, glyphs, (int)entry.offset,
  470. (Integer)ce.next());
  471. }
  472. } else {
  473. throw new IOException("Can't find glyf table");
  474. }
  475. }
  476. /**
  477. * glyphs has old index as (Integer) key and new index
  478. * as (Integer) value
  479. */
  480. public byte[] readFont(FontFileReader in, String name,
  481. HashMap glyphs) throws IOException {
  482. /*
  483. * Check if TrueType collection, and that the name
  484. * exists in the collection
  485. */
  486. if (!checkTTC(in, name, false))
  487. throw new IOException("Failed to read font");
  488. output = new byte[in.getFileSize()];
  489. readDirTabs(in);
  490. readFontHeader(in);
  491. getNumGlyphs(in);
  492. readHorizontalHeader(in);
  493. readHorizontalMetrics(in);
  494. readIndexToLocation(in);
  495. scanGlyphs(in, glyphs);
  496. createDirectory(); // Create the TrueType header and directory
  497. createHead(in);
  498. createHhea(in, glyphs.size()); // Create the hhea table
  499. createHmtx(in, glyphs); // Create hmtx table
  500. createMaxp(in, glyphs.size()); // copy the maxp table
  501. try {
  502. createCvt(in); // copy the cvt table
  503. } catch (IOException ex) {
  504. // Cvt is optional (only required for OpenType (MS) fonts)
  505. //log.error("TrueType warning: " + ex.getMessage());
  506. }
  507. try {
  508. createFpgm(in); // copy fpgm table
  509. } catch (IOException ex) {
  510. // Fpgm is optional (only required for OpenType (MS) fonts)
  511. //log.error("TrueType warning: " + ex.getMessage());
  512. }
  513. try {
  514. createPrep(in); // copy prep table
  515. } catch (IOException ex) {
  516. // Prep is optional (only required for OpenType (MS) fonts)
  517. //log.error("TrueType warning: " + ex.getMessage());
  518. }
  519. try {
  520. createLoca(glyphs.size()); // create empty loca table
  521. } catch (IOException ex) {
  522. // Loca is optional (only required for OpenType (MS) fonts)
  523. //log.error("TrueType warning: " + ex.getMessage());
  524. }
  525. try {
  526. createGlyf(in, glyphs);
  527. } catch (IOException ex) {
  528. // Glyf is optional (only required for OpenType (MS) fonts)
  529. //log.error("TrueType warning: " + ex.getMessage());
  530. }
  531. pad4();
  532. createCheckSumAdjustment();
  533. byte[] ret = new byte[realSize];
  534. System.arraycopy(output, 0, ret, 0, realSize);
  535. return ret;
  536. }
  537. /**
  538. * writes a ISO-8859-1 string at the currentPosition
  539. * updates currentPosition but not realSize
  540. * @return number of bytes written
  541. */
  542. private int writeString(String str) {
  543. int length = 0;
  544. try {
  545. byte[] buf = str.getBytes("ISO-8859-1");
  546. System.arraycopy(buf, 0, output, currentPos, buf.length);
  547. length = buf.length;
  548. currentPos += length;
  549. } catch (Exception e) {
  550. // This should never happen!
  551. }
  552. return length;
  553. }
  554. /**
  555. * Appends a byte to the output array,
  556. * updates currentPost but not realSize
  557. */
  558. private void writeByte(byte b) {
  559. output[currentPos++] = b;
  560. }
  561. /**
  562. * Appends a USHORT to the output array,
  563. * updates currentPost but not realSize
  564. */
  565. private void writeUShort(int s) {
  566. byte b1 = (byte)((s >> 8) & 0xff);
  567. byte b2 = (byte)(s & 0xff);
  568. writeByte(b1);
  569. writeByte(b2);
  570. }
  571. /**
  572. * Appends a USHORT to the output array,
  573. * at the given position without changing currentPos
  574. */
  575. private void writeUShort(int pos, int s) {
  576. byte b1 = (byte)((s >> 8) & 0xff);
  577. byte b2 = (byte)(s & 0xff);
  578. output[pos] = b1;
  579. output[pos + 1] = b2;
  580. }
  581. /**
  582. * Appends a ULONG to the output array,
  583. * updates currentPos but not realSize
  584. */
  585. private void writeULong(int s) {
  586. byte b1 = (byte)((s >> 24) & 0xff);
  587. byte b2 = (byte)((s >> 16) & 0xff);
  588. byte b3 = (byte)((s >> 8) & 0xff);
  589. byte b4 = (byte)(s & 0xff);
  590. writeByte(b1);
  591. writeByte(b2);
  592. writeByte(b3);
  593. writeByte(b4);
  594. }
  595. /**
  596. * Appends a ULONG to the output array,
  597. * at the given position without changing currentPos
  598. */
  599. private void writeULong(int pos, int s) {
  600. byte b1 = (byte)((s >> 24) & 0xff);
  601. byte b2 = (byte)((s >> 16) & 0xff);
  602. byte b3 = (byte)((s >> 8) & 0xff);
  603. byte b4 = (byte)(s & 0xff);
  604. output[pos] = b1;
  605. output[pos + 1] = b2;
  606. output[pos + 2] = b3;
  607. output[pos + 3] = b4;
  608. }
  609. /**
  610. * Read a signed short value at given position
  611. */
  612. private short readShort(int pos) {
  613. int ret = readUShort(pos);
  614. return (short)ret;
  615. }
  616. /**
  617. * Read a unsigned short value at given position
  618. */
  619. private int readUShort(int pos) {
  620. int ret = (int)output[pos];
  621. if (ret < 0)
  622. ret += 256;
  623. ret = ret << 8;
  624. if ((int)output[pos + 1] < 0) {
  625. ret |= (int)output[pos + 1] + 256;
  626. } else
  627. ret |= (int)output[pos + 1];
  628. return ret;
  629. }
  630. /**
  631. * Create a padding in the fontfile to align
  632. * on a 4-byte boundary
  633. */
  634. private void pad4() {
  635. int padSize = currentPos % 4;
  636. for (int i = 0; i < padSize; i++) {
  637. output[currentPos++] = 0;
  638. realSize++;
  639. }
  640. }
  641. /**
  642. * Returns the maximum power of 2 <= max
  643. */
  644. private int maxPow2(int max) {
  645. int i = 0;
  646. while (Math.pow(2, (double)i) < max)
  647. i++;
  648. return (i - 1);
  649. }
  650. private int log2(int num) {
  651. return (int)(Math.log((double)num) / Math.log(2));
  652. }
  653. private int getCheckSum(int start, int size) {
  654. return (int)getLongCheckSum(start, size);
  655. }
  656. private long getLongCheckSum(int start, int size) {
  657. // All the tables here are aligned on four byte boundaries
  658. // Add remainder to size if it's not a multiple of 4
  659. int remainder = size % 4;
  660. if (remainder != 0)
  661. size += remainder;
  662. long sum = 0;
  663. for (int i = 0; i < size; i += 4) {
  664. int l = (int)(output[start + i] << 24);
  665. l += (int)(output[start + i + 1] << 16);
  666. l += (int)(output[start + i + 2] << 16);
  667. l += (int)(output[start + i + 3] << 16);
  668. sum += l;
  669. if (sum > 0xffffffff)
  670. sum = sum - 0xffffffff;
  671. }
  672. return sum;
  673. }
  674. private void createCheckSumAdjustment() {
  675. long sum = getLongCheckSum(0, realSize);
  676. int checksum = (int)(0xb1b0afba - sum);
  677. writeULong(checkSumAdjustmentOffset, checksum);
  678. }
  679. }