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.

OTFSubSetFileTestCase.java 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  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.DataInputStream;
  21. import java.io.IOException;
  22. import java.util.ArrayList;
  23. import java.util.Arrays;
  24. import java.util.Collections;
  25. import java.util.HashMap;
  26. import java.util.LinkedHashMap;
  27. import java.util.List;
  28. import java.util.Map;
  29. import org.junit.Assert;
  30. import org.junit.Before;
  31. import org.junit.Test;
  32. import static org.junit.Assert.assertEquals;
  33. import static org.junit.Assert.assertTrue;
  34. import static org.mockito.Matchers.any;
  35. import static org.mockito.Mockito.mock;
  36. import static org.mockito.Mockito.when;
  37. import org.apache.fontbox.cff.CFFFont;
  38. import org.apache.fontbox.cff.CFFParser;
  39. import org.apache.fop.fonts.MultiByteFont;
  40. import org.apache.fop.fonts.cff.CFFDataReader;
  41. import org.apache.fop.fonts.cff.CFFDataReader.CFFIndexData;
  42. import org.apache.fop.fonts.cff.CFFDataReader.DICTEntry;
  43. import org.apache.fop.fonts.truetype.OTFSubSetFile.BytesNumber;
  44. public class OTFSubSetFileTestCase extends OTFFileTestCase {
  45. private Map<Integer, Integer> glyphs = new HashMap<Integer, Integer>();
  46. /**
  47. * Initialises the test by creating the font subset. A CFFDataReader is
  48. * also created based on the subset data for use in the tests.
  49. * @throws IOException
  50. */
  51. @Before
  52. public void setUp() throws Exception {
  53. super.setUp();
  54. for (int i = 0; i < 256; i++) {
  55. glyphs.put(i, i);
  56. }
  57. }
  58. private CFFDataReader getCFFReaderSourceSans() throws IOException {
  59. byte[] sourceSansData = getSourceSansSubset().getFontSubset();
  60. return new CFFDataReader(sourceSansData);
  61. }
  62. private OTFSubSetFile getSourceSansSubset() throws IOException {
  63. OTFSubSetFile sourceSansSubset = new OTFSubSetFile();
  64. sourceSansSubset.readFont(sourceSansReader, "SourceSansProBold", null, glyphs);
  65. return sourceSansSubset;
  66. }
  67. /**
  68. * Validates the CharString data against the original font
  69. * @throws IOException
  70. */
  71. @Test
  72. public void testCharStringIndex() throws IOException {
  73. CFFDataReader cffReaderSourceSans = getCFFReaderSourceSans();
  74. assertEquals(256, cffReaderSourceSans.getCharStringIndex().getNumObjects());
  75. assertTrue(checkCorrectOffsets(cffReaderSourceSans.getCharStringIndex()));
  76. validateCharStrings(cffReaderSourceSans, getSourceSansSubset().getCFFReader());
  77. }
  78. /**
  79. * Checks the index data to ensure that the offsets are valid
  80. * @param indexData The index data to check
  81. * @return Returns true if it is found to be valid
  82. */
  83. private boolean checkCorrectOffsets(CFFIndexData indexData) {
  84. int last = 0;
  85. for (int i = 0; i < indexData.getOffsets().length; i++) {
  86. if (indexData.getOffsets()[i] < last) {
  87. return false;
  88. }
  89. last = indexData.getOffsets()[i];
  90. }
  91. return true;
  92. }
  93. /**
  94. * Validates the subset font CharString data by comparing it with the original.
  95. * @param subsetCFF The subset CFFDataReader containing the CharString data
  96. * @param origCFF The original CFFDataReader containing the CharString data
  97. * @throws IOException
  98. */
  99. private void validateCharStrings(CFFDataReader subsetCFF, CFFDataReader origCFF)
  100. throws IOException {
  101. CFFFont sourceSansOriginal = sourceSansProBold.fileFont;
  102. CFFIndexData charStrings = subsetCFF.getCharStringIndex();
  103. List<byte[]> origCharStringData = sourceSansOriginal.getCharStringBytes();
  104. for (int i = 0; i < charStrings.getNumObjects(); i++) {
  105. byte[] origCharData = origCharStringData.get(i);
  106. byte[] charData = charStrings.getValue(i);
  107. List<BytesNumber> origOperands = getFullCharString(new Context(), origCharData, origCFF);
  108. List<BytesNumber> subsetOperands = getFullCharString(new Context(), charData, subsetCFF);
  109. for (int j = 0; j < origOperands.size(); j++) {
  110. assertTrue(origOperands.get(j).equals(subsetOperands.get(j)));
  111. }
  112. }
  113. }
  114. static class Context {
  115. private ArrayList<BytesNumber> operands = new ArrayList<BytesNumber>();
  116. private ArrayList<BytesNumber> stack = new ArrayList<BytesNumber>();
  117. private int hstemCount;
  118. private int vstemCount;
  119. private int lastOp = -1;
  120. private int maskLength = -1;
  121. public void pushOperand(BytesNumber v) {
  122. operands.add(v);
  123. if (v instanceof Operator) {
  124. if (v.getNumber() != 11 && v.getNumber() != 12) {
  125. lastOp = v.getNumber();
  126. }
  127. } else {
  128. stack.add(v);
  129. }
  130. }
  131. public BytesNumber popOperand() {
  132. operands.remove(operands.size() - 1);
  133. return stack.remove(stack.size() - 1);
  134. }
  135. public BytesNumber lastOperand() {
  136. return operands.get(operands.size() - 1);
  137. }
  138. public void clearStack() {
  139. stack.clear();
  140. }
  141. public int getMaskLength() {
  142. // The number of data bytes for mask is exactly the number needed, one
  143. // bit per hint, to reference the number of stem hints declared
  144. // at the beginning of the charstring program.
  145. if (maskLength > 0) {
  146. return maskLength;
  147. }
  148. return 1 + (hstemCount + vstemCount - 1) / 8;
  149. }
  150. public List<BytesNumber> getFullOperandsList() {
  151. return operands;
  152. }
  153. public void countHstem() {
  154. // hstem(hm) operator
  155. hstemCount += stack.size() / 2;
  156. clearStack();
  157. }
  158. public void countVstem() {
  159. // vstem(hm) operator
  160. vstemCount += stack.size() / 2;
  161. clearStack();
  162. }
  163. public int calcMaskLength() {
  164. if (lastOp == 1 || lastOp == 18) {
  165. //If hstem and vstem hints are both declared at the beginning of
  166. //a charstring, and this sequence is followed directly by the
  167. //hintmask or cntrmask operators, the vstem hint operator need
  168. //not be included.
  169. vstemCount += stack.size() / 2;
  170. }
  171. clearStack();
  172. return getMaskLength();
  173. }
  174. }
  175. /**
  176. * Recursively reads and constructs the full CharString for comparison
  177. * @param data The original byte data of the CharString
  178. * @param cffData The CFFDataReader containing the subroutine indexes
  179. * @return Returns a list of parsed operands and operators
  180. * @throws IOException
  181. */
  182. private List<BytesNumber> getFullCharString(Context context, byte[] data, CFFDataReader cffData)
  183. throws IOException {
  184. CFFIndexData localIndexSubr = cffData.getLocalIndexSubr();
  185. CFFIndexData globalIndexSubr = cffData.getGlobalIndexSubr();
  186. boolean hasLocalSubroutines = localIndexSubr != null && localIndexSubr.getNumObjects() > 0;
  187. boolean hasGlobalSubroutines = globalIndexSubr != null && globalIndexSubr.getNumObjects() > 0;
  188. for (int dataPos = 0; dataPos < data.length; dataPos++) {
  189. int b0 = data[dataPos] & 0xff;
  190. if (b0 == 10 && hasLocalSubroutines) {
  191. int subrNumber = getSubrNumber(localIndexSubr.getNumObjects(),
  192. context.popOperand().getNumber());
  193. byte[] subr = localIndexSubr.getValue(subrNumber);
  194. getFullCharString(context, subr, cffData);
  195. } else if (b0 == 29 && hasGlobalSubroutines) {
  196. int subrNumber = getSubrNumber(globalIndexSubr.getNumObjects(),
  197. context.popOperand().getNumber());
  198. byte[] subr = globalIndexSubr.getValue(subrNumber);
  199. getFullCharString(context, subr, cffData);
  200. } else if ((b0 >= 0 && b0 <= 27) || (b0 >= 29 && b0 <= 31)) {
  201. int size = 1;
  202. int b1 = -1;
  203. if (b0 == 12) {
  204. b1 = data[dataPos++] & 0xff;
  205. size = 2;
  206. } else if (b0 == 1 || b0 == 18) {
  207. context.countHstem();
  208. } else if (b0 == 3 || b0 == 23) {
  209. context.countVstem();
  210. } else if (b0 == 19 || b0 == 20) {
  211. int length = context.calcMaskLength();
  212. dataPos += length;
  213. size = length + 1;
  214. }
  215. context.pushOperand(new Operator(b0, size, getOperatorName(b0, b1)));
  216. } else if (b0 == 28 || (b0 >= 32 && b0 <= 255)) {
  217. context.pushOperand(readNumber(b0, data, dataPos));
  218. dataPos += context.lastOperand().getNumBytes() - 1;
  219. }
  220. }
  221. return context.getFullOperandsList();
  222. }
  223. /**
  224. * Parses a number from one or more bytes
  225. * @param b0 The first byte to identify how to interpret the number
  226. * @param input The original byte data containing the number
  227. * @param curPos The current position of the number
  228. * @return Returns the number
  229. * @throws IOException
  230. */
  231. private BytesNumber readNumber(int b0, byte[] input, int curPos) throws IOException {
  232. if (b0 == 28) {
  233. int b1 = input[curPos + 1] & 0xff;
  234. int b2 = input[curPos + 2] & 0xff;
  235. return new BytesNumber((int) (short) (b1 << 8 | b2), 3);
  236. } else if (b0 >= 32 && b0 <= 246) {
  237. return new BytesNumber(b0 - 139, 1);
  238. } else if (b0 >= 247 && b0 <= 250) {
  239. int b1 = input[curPos + 1] & 0xff;
  240. return new BytesNumber((b0 - 247) * 256 + b1 + 108, 2);
  241. } else if (b0 >= 251 && b0 <= 254) {
  242. int b1 = input[curPos + 1] & 0xff;
  243. return new BytesNumber(-(b0 - 251) * 256 - b1 - 108, 2);
  244. } else if (b0 == 255) {
  245. int b1 = input[curPos + 1] & 0xff;
  246. int b2 = input[curPos + 2] & 0xff;
  247. int b3 = input[curPos + 3] & 0xff;
  248. int b4 = input[curPos + 4] & 0xff;
  249. return new BytesNumber((b1 << 24 | b2 << 16 | b3 << 8 | b4), 5);
  250. } else {
  251. throw new IllegalArgumentException();
  252. }
  253. }
  254. /**
  255. * Gets the subroutine number according to the number of subroutines
  256. * and the provided operand.
  257. * @param numSubroutines The number of subroutines used to calculate the
  258. * subroutine reference.
  259. * @param operand The operand for the subroutine
  260. * @return Returns the calculated subroutine number
  261. */
  262. private int getSubrNumber(int numSubroutines, int operand) {
  263. int bias = getBias(numSubroutines);
  264. return bias + operand;
  265. }
  266. /**
  267. * Gets the bias give the number of subroutines. This is used in the
  268. * calculation to determine a subroutine's number
  269. * @param subrCount The number of subroutines for a given index
  270. * @return Returns the bias value
  271. */
  272. private int getBias(int subrCount) {
  273. if (subrCount < 1240) {
  274. return 107;
  275. } else if (subrCount < 33900) {
  276. return 1131;
  277. } else {
  278. return 32768;
  279. }
  280. }
  281. /**
  282. * A class representing an operator from the CharString data
  283. */
  284. private class Operator extends BytesNumber {
  285. private String opName = "";
  286. Operator(int number, int numBytes, String opName) {
  287. super(number, numBytes);
  288. this.opName = opName;
  289. }
  290. public String toString() {
  291. return String.format("[%s]", opName);
  292. }
  293. }
  294. /**
  295. * Gets the identifying name for the given operator. This is primarily
  296. * used for debugging purposes. See the Type 2 CharString Format specification
  297. * document (Technical Note #5177) Appendix A (Command Codes).
  298. * @param operator The operator code
  299. * @param operatorB The second byte of the operator
  300. * @return Returns the operator name.
  301. */
  302. private String getOperatorName(int operator, int operatorB) {
  303. switch (operator) {
  304. case 0: return "Reserved";
  305. case 1: return "hstem";
  306. case 2: return "Reserved";
  307. case 3: return "vstem";
  308. case 4: return "vmoveto";
  309. case 5: return "rlineto";
  310. case 6: return "hlineto";
  311. case 7: return "vlineto";
  312. case 8: return "rrcurveto";
  313. case 9: return "Reserved";
  314. case 10: return "callsubr";
  315. case 11: return "return";
  316. case 12: return getDoubleOpName(operatorB);
  317. case 13: return "Reserved";
  318. case 14: return "enchar";
  319. case 15:
  320. case 16:
  321. case 17: return "Reserved";
  322. case 18: return "hstemhm";
  323. case 19: return "hintmask";
  324. case 20: return "cntrmask";
  325. case 21: return "rmoveto";
  326. case 22: return "hmoveto";
  327. case 23: return "vstemhm";
  328. case 24: return "rcurveline";
  329. case 25: return "rlinecurve";
  330. case 26: return "vvcurveto";
  331. case 27: return "hhcurveto";
  332. case 28: return "shortint";
  333. case 29: return "callgsubr";
  334. case 30: return "vhcurveto";
  335. case 31: return "hvcurveto";
  336. default: return "Unknown";
  337. }
  338. }
  339. /**
  340. * Gets the name of a double byte operator code
  341. * @param operator The second byte of the operator
  342. * @return Returns the name
  343. */
  344. private String getDoubleOpName(int operator) {
  345. switch (operator) {
  346. case 0:
  347. case 1:
  348. case 2: return "Reserved";
  349. case 3: return "and";
  350. case 4: return "or";
  351. case 5: return "not";
  352. case 6:
  353. case 7:
  354. case 8: return "Reserved";
  355. case 9: return "abs";
  356. case 10: return "add";
  357. case 11: return "sub";
  358. case 12: return "div";
  359. case 13: return "Reserved";
  360. case 14: return "neg";
  361. case 15: return "eq";
  362. case 16:
  363. case 17: return "Reserved";
  364. case 18: return "drop";
  365. case 19: return "Reserved";
  366. case 20: return "put";
  367. case 21: return "get";
  368. case 22: return "ifelse";
  369. case 23: return "random";
  370. case 24: return "mul";
  371. case 25: return "Reserved";
  372. case 26: return "sqrt";
  373. case 27: return "dup";
  374. case 28: return "exch";
  375. case 29: return "index";
  376. case 30: return "roll";
  377. case 31:
  378. case 32:
  379. case 33: return "Reserved";
  380. case 34: return "hflex";
  381. case 35: return "flex";
  382. case 36: return "hflex1";
  383. case 37: return "flex1";
  384. case 38: return "Reserved";
  385. default: return "Unknown";
  386. }
  387. }
  388. /**
  389. * Validates the String index data and size
  390. * @throws IOException
  391. */
  392. @Test
  393. public void testStringIndex() throws IOException {
  394. CFFDataReader cffReaderSourceSans = getCFFReaderSourceSans();
  395. assertEquals(164, cffReaderSourceSans.getStringIndex().getNumObjects());
  396. assertTrue(checkCorrectOffsets(cffReaderSourceSans.getStringIndex()));
  397. assertEquals("Amacron", new String(cffReaderSourceSans.getStringIndex().getValue(5)));
  398. assertEquals("Edotaccent", new String(cffReaderSourceSans.getStringIndex().getValue(32)));
  399. assertEquals("uni0122", new String(cffReaderSourceSans.getStringIndex().getValue(45)));
  400. }
  401. /**
  402. * Validates the Top Dict data
  403. * @throws IOException
  404. */
  405. @Test
  406. public void testTopDictData() throws IOException {
  407. CFFDataReader cffReaderSourceSans = getCFFReaderSourceSans();
  408. Map<String, DICTEntry> topDictEntries = cffReaderSourceSans.parseDictData(
  409. cffReaderSourceSans.getTopDictIndex().getData());
  410. assertEquals(10, topDictEntries.size());
  411. }
  412. @Test
  413. public void testFDSelect() throws IOException {
  414. Assert.assertEquals(getSubset(1).length, 42);
  415. Assert.assertEquals(getSubset(2).length, 49);
  416. }
  417. private byte[] getSubset(final int opLen) throws IOException {
  418. FontFileReader reader = sourceSansReader;
  419. OTFSubSetFile otfSubSetFile = new MyOTFSubSetFile(opLen);
  420. otfSubSetFile.readFont(reader, "StandardOpenType", null, new HashMap<Integer, Integer>());
  421. return otfSubSetFile.getFontSubset();
  422. }
  423. @Test
  424. public void testOffsets() throws IOException {
  425. StringBuilder sb = new StringBuilder();
  426. for (int i = 0; i < 2048; i++) {
  427. sb.append("SourceSansProBold");
  428. }
  429. OTFSubSetFile otfSubSetFile = new OTFSubSetFile();
  430. otfSubSetFile.readFont(sourceSansReader, sb.toString(), null, glyphs);
  431. new CFFParser().parse(otfSubSetFile.getFontSubset());
  432. }
  433. @Test
  434. public void testCharset() throws IOException {
  435. FontFileReader reader = sourceSansReader;
  436. MyOTFSubSetFile otfSubSetFile = new MyOTFSubSetFile(1);
  437. otfSubSetFile.readFont(reader, "StandardOpenType", null, new HashMap<Integer, Integer>());
  438. ByteArrayInputStream is = new ByteArrayInputStream(otfSubSetFile.getFontSubset());
  439. is.skip(otfSubSetFile.charsetOffset);
  440. Assert.assertEquals(is.read(), 2);
  441. }
  442. class MyOTFSubSetFile extends OTFSubSetFile {
  443. int charsetOffset;
  444. int opLen;
  445. MyOTFSubSetFile(int opLen) throws IOException {
  446. super();
  447. this.opLen = opLen;
  448. }
  449. protected void createCFF() throws IOException {
  450. cffReader = mock(CFFDataReader.class);
  451. when(cffReader.getHeader()).thenReturn(new byte[0]);
  452. when(cffReader.getTopDictIndex()).thenReturn(new CFFDataReader().new CFFIndexData() {
  453. public byte[] getByteData() throws IOException {
  454. return new byte[] {0, 0, 1};
  455. }
  456. });
  457. LinkedHashMap<String, DICTEntry> map = new LinkedHashMap<String, DICTEntry>();
  458. DICTEntry dict = new DICTEntry();
  459. dict.setOperands(Collections.<Number>singletonList(1));
  460. map.put("charset", dict);
  461. map.put("CharStrings", dict);
  462. when((cffReader.getTopDictEntries())).thenReturn(map);
  463. CFFDataReader.Format3FDSelect fdSelect = new CFFDataReader().new Format3FDSelect();
  464. fdSelect.setRanges(new HashMap<Integer, Integer>());
  465. when(cffReader.getFDSelect()).thenReturn(fdSelect);
  466. cffReader.getTopDictEntries().get("CharStrings").setOperandLength(opLen);
  467. super.createCFF();
  468. }
  469. protected void updateFixedOffsets(Map<String, DICTEntry> topDICT, Offsets offsets) throws IOException {
  470. this.charsetOffset = offsets.charset;
  471. super.updateFixedOffsets(topDICT, offsets);
  472. }
  473. }
  474. @Test
  475. public void testResizeOfOperand() throws IOException {
  476. OTFSubSetFile otfSubSetFile = new OTFSubSetFile() {
  477. protected void writeFDSelect() {
  478. super.writeFDSelect();
  479. writeBytes(new byte[1024 * 100]);
  480. }
  481. };
  482. otfSubSetFile.readFont(sourceSansReader, "StandardOpenType", null, glyphs);
  483. byte[] fontSubset = otfSubSetFile.getFontSubset();
  484. CFFDataReader cffReader = new CFFDataReader(fontSubset);
  485. assertEquals(cffReader.getTopDictEntries().get("CharStrings").getOperandLength(), 5);
  486. assertEquals(cffReader.getTopDictEntries().get("CharStrings").getByteData().length, 6);
  487. }
  488. @Test
  489. public void testFDArraySize() throws IOException {
  490. OTFSubSetFileFDArraySize otfSubSetFileFDArraySize = new OTFSubSetFileFDArraySize();
  491. otfSubSetFileFDArraySize.readFont(sourceSansReader, "StandardOpenType", null, glyphs);
  492. byte[] fontSubset = otfSubSetFileFDArraySize.getFontSubset();
  493. DataInputStream dis = new DataInputStream(new ByteArrayInputStream(fontSubset));
  494. dis.skipBytes(otfSubSetFileFDArraySize.offset);
  495. Assert.assertEquals(dis.readUnsignedShort(), otfSubSetFileFDArraySize.fdFontCount);
  496. Assert.assertEquals(dis.readByte(), 2);
  497. }
  498. static class OTFSubSetFileFDArraySize extends OTFSubSetFile {
  499. int offset;
  500. int fdFontCount = 128;
  501. OTFSubSetFileFDArraySize() throws IOException {
  502. super();
  503. }
  504. protected void createCFF() throws IOException {
  505. super.createCFF();
  506. writeFDArray(new ArrayList<Integer>(), new ArrayList<Integer>(), new ArrayList<Integer>());
  507. }
  508. protected int writeFDArray(List<Integer> uniqueNewRefs, List<Integer> privateDictOffsets,
  509. List<Integer> fontNameSIDs) throws IOException {
  510. List<CFFDataReader.FontDict> fdFonts = cffReader.getFDFonts();
  511. CFFDataReader.FontDict fdFont = cffReader.new FontDict() {
  512. public byte[] getByteData() throws IOException {
  513. return new byte[128];
  514. }
  515. };
  516. cffReader = makeCFFDataReader();
  517. when(cffReader.getFDFonts()).thenReturn(fdFonts);
  518. fdFonts.clear();
  519. uniqueNewRefs.clear();
  520. privateDictOffsets.clear();
  521. fontNameSIDs.clear();
  522. for (int i = 0; i < fdFontCount; i++) {
  523. fdFonts.add(fdFont);
  524. uniqueNewRefs.add(i);
  525. privateDictOffsets.add(i);
  526. fontNameSIDs.add(i);
  527. }
  528. offset = super.writeFDArray(uniqueNewRefs, privateDictOffsets, fontNameSIDs);
  529. return offset;
  530. }
  531. }
  532. @Test
  533. public void testOrderOfEntries() throws IOException {
  534. OTFSubSetFileEntryOrder otfSubSetFile = getFont(3, 2);
  535. assertTrue(otfSubSetFile.offsets.fdArray < otfSubSetFile.offsets.charString);
  536. otfSubSetFile = getFont(2, 3);
  537. assertTrue(otfSubSetFile.offsets.fdArray > otfSubSetFile.offsets.charString);
  538. }
  539. private OTFSubSetFileEntryOrder getFont(int csLen, int fdLen) throws IOException {
  540. glyphs.clear();
  541. OTFSubSetFileEntryOrder otfSubSetFile = new OTFSubSetFileEntryOrder(csLen, fdLen);
  542. otfSubSetFile.readFont(sourceSansReader, "StandardOpenType", null, glyphs);
  543. return otfSubSetFile;
  544. }
  545. static class OTFSubSetFileEntryOrder extends OTFSubSetFile {
  546. Offsets offsets;
  547. int csLen;
  548. int fdLen;
  549. OTFSubSetFileEntryOrder(int csLen, int fdLen) throws IOException {
  550. super();
  551. this.csLen = csLen;
  552. this.fdLen = fdLen;
  553. }
  554. protected void createCFF() throws IOException {
  555. cffReader = makeCFFDataReader();
  556. LinkedHashMap<String, DICTEntry> topDict = new LinkedHashMap<String, DICTEntry>();
  557. DICTEntry entry = new DICTEntry();
  558. entry.setOperands(Collections.<Number>singletonList(0));
  559. topDict.put("charset", entry);
  560. entry.setOperandLength(csLen);
  561. topDict.put("CharStrings", entry);
  562. entry = new DICTEntry();
  563. entry.setOperandLength(fdLen);
  564. topDict.put("FDArray", entry);
  565. when(cffReader.getTopDictEntries()).thenReturn(topDict);
  566. super.createCFF();
  567. }
  568. protected void updateCIDOffsets(Offsets offsets) throws IOException {
  569. super.updateCIDOffsets(offsets);
  570. this.offsets = offsets;
  571. }
  572. }
  573. private static CFFDataReader makeCFFDataReader() throws IOException {
  574. CFFDataReader cffReader = mock(CFFDataReader.class);
  575. when(cffReader.getHeader()).thenReturn(new byte[0]);
  576. when(cffReader.getTopDictIndex()).thenReturn(cffReader.new CFFIndexData() {
  577. public byte[] getByteData() throws IOException {
  578. return new byte[]{0, 0, 1};
  579. }
  580. });
  581. CFFDataReader.Format3FDSelect fdSelect = cffReader.new Format3FDSelect();
  582. fdSelect.setRanges(new HashMap<Integer, Integer>());
  583. when(cffReader.getFDSelect()).thenReturn(fdSelect);
  584. CFFDataReader.FontDict fd = mock(CFFDataReader.FontDict.class);
  585. when(fd.getPrivateDictData()).thenReturn(new byte[0]);
  586. when(cffReader.getFDFonts()).thenReturn(Collections.singletonList(fd));
  587. LinkedHashMap<String, DICTEntry> map = new LinkedHashMap<String, DICTEntry>();
  588. DICTEntry e = new DICTEntry();
  589. e.setOffset(1);
  590. e.setOperandLengths(Arrays.asList(0, 0));
  591. e.setOperandLength(2);
  592. map.put("FontName", e);
  593. map.put("Private", e);
  594. map.put("Subrs", e);
  595. when(cffReader.parseDictData(any(byte[].class))).thenReturn(map);
  596. return cffReader;
  597. }
  598. @Test
  599. public void testWriteCIDDictsAndSubrs() throws IOException {
  600. OTFSubSetFile subSetFile = new OTFSubSetFile() {
  601. public void readFont(FontFileReader in, String embeddedName, MultiByteFont mbFont) throws IOException {
  602. cffReader = makeCFFDataReader();
  603. fdSubrs = new ArrayList<List<byte[]>>();
  604. fdSubrs.add(new ArrayList<byte[]>());
  605. writeCIDDictsAndSubrs(Collections.singletonList(0));
  606. }
  607. };
  608. subSetFile.readFont(null, null, (MultiByteFont) null);
  609. ByteArrayInputStream is = new ByteArrayInputStream(subSetFile.getFontSubset());
  610. is.skip(1);
  611. Assert.assertEquals(is.read(), 247);
  612. Assert.assertEquals(is.read(), 0);
  613. final int sizeOfPrivateDictByteData = 108;
  614. is.skip(sizeOfPrivateDictByteData - 3);
  615. is.skip(2); //start index
  616. Assert.assertEquals(is.read(), 1);
  617. }
  618. @Test
  619. public void testResizeOfOperand2() throws IOException {
  620. OTFSubSetFile otfSubSetFile = new OTFSubSetFile() {
  621. void readFont(FontFileReader in, String embeddedName, MultiByteFont mbFont,
  622. Map<Integer, Integer> usedGlyphs) throws IOException {
  623. cffReader = makeCFFDataReader();
  624. LinkedHashMap<String, DICTEntry> topDict = new LinkedHashMap<String, DICTEntry>();
  625. DICTEntry entry = new DICTEntry();
  626. entry.setOperandLength(1);
  627. entry.setOperator(new int[0]);
  628. entry.setOperands(Collections.<Number>singletonList(0));
  629. topDict.put("version", entry);
  630. when(cffReader.getTopDictEntries()).thenReturn(topDict);
  631. writeTopDICT();
  632. }
  633. };
  634. otfSubSetFile.readFont(sourceSansReader, "StandardOpenType", null, glyphs);
  635. ByteArrayInputStream fontSubset = new ByteArrayInputStream(otfSubSetFile.getFontSubset());
  636. fontSubset.skip(5);
  637. Assert.assertEquals(fontSubset.read(), 248);
  638. Assert.assertEquals(fontSubset.read(), (byte)(390 - 108));
  639. }
  640. }