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

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