Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

OTFSubSetFileTestCase.java 29KB

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