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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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.IOException;
  20. import java.util.ArrayList;
  21. import java.util.Arrays;
  22. import java.util.HashMap;
  23. import java.util.List;
  24. import java.util.Map;
  25. import org.junit.Before;
  26. import org.junit.Test;
  27. import static org.junit.Assert.assertEquals;
  28. import static org.junit.Assert.assertTrue;
  29. import org.apache.fontbox.cff.CFFFont;
  30. import org.apache.fop.fonts.cff.CFFDataReader;
  31. import org.apache.fop.fonts.cff.CFFDataReader.CFFIndexData;
  32. import org.apache.fop.fonts.cff.CFFDataReader.DICTEntry;
  33. import org.apache.fop.fonts.truetype.OTFSubSetFile.BytesNumber;
  34. public class OTFSubSetFileTestCase extends OTFFileTestCase {
  35. CFFDataReader cffReaderSourceSans;
  36. private OTFSubSetFile sourceSansSubset;
  37. private byte[] sourceSansData;
  38. CFFDataReader cffReaderHeitiStd;
  39. /**
  40. * Initialises the test by creating the font subset. A CFFDataReader is
  41. * also created based on the subset data for use in the tests.
  42. * @throws IOException
  43. */
  44. @Before
  45. public void setUp() throws Exception {
  46. super.setUp();
  47. Map<Integer, Integer> glyphs = new HashMap<Integer, Integer>();
  48. for (int i = 0; i < 256; i++) {
  49. glyphs.put(i, i);
  50. }
  51. sourceSansSubset = new OTFSubSetFile();
  52. String sourceSansHeader = OFFontLoader.readHeader(sourceSansReader);
  53. sourceSansSubset.readFont(sourceSansReader, "SourceSansProBold", sourceSansHeader, glyphs);
  54. sourceSansData = sourceSansSubset.getFontSubset();
  55. cffReaderSourceSans = new CFFDataReader(sourceSansData);
  56. }
  57. /**
  58. * Validates the CharString data against the original font
  59. * @throws IOException
  60. */
  61. @Test
  62. public void testCharStringIndex() throws IOException {
  63. assertEquals(256, cffReaderSourceSans.getCharStringIndex().getNumObjects());
  64. assertTrue(checkCorrectOffsets(cffReaderSourceSans.getCharStringIndex()));
  65. validateCharStrings(cffReaderSourceSans, sourceSansSubset.getCFFReader());
  66. }
  67. /**
  68. * Checks the index data to ensure that the offsets are valid
  69. * @param indexData The index data to check
  70. * @return Returns true if it is found to be valid
  71. */
  72. private boolean checkCorrectOffsets(CFFIndexData indexData) {
  73. int last = 0;
  74. for (int i = 0; i < indexData.getOffsets().length; i++) {
  75. if (indexData.getOffsets()[i] < last) {
  76. return false;
  77. }
  78. last = indexData.getOffsets()[i];
  79. }
  80. return true;
  81. }
  82. /**
  83. * Validates the subset font CharString data by comparing it with the original.
  84. * @param subsetCFF The subset CFFDataReader containing the CharString data
  85. * @param origCFF The original CFFDataReader containing the CharString data
  86. * @throws IOException
  87. */
  88. private void validateCharStrings(CFFDataReader subsetCFF, CFFDataReader origCFF)
  89. throws IOException {
  90. CFFFont sourceSansOriginal = sourceSansProBold.fileFont;
  91. CFFIndexData charStrings = subsetCFF.getCharStringIndex();
  92. Map<String, byte[]> origCharStringData = sourceSansOriginal.getCharStringsDict();
  93. for (int i = 0; i < charStrings.getNumObjects(); i++) {
  94. byte[] origCharData = origCharStringData.get(origCharStringData.keySet().toArray(
  95. new String[0])[i]);
  96. byte[] charData = charStrings.getValue(i);
  97. List<BytesNumber> origOperands = getFullCharString(origCharData, origCFF);
  98. List<BytesNumber> subsetOperands = getFullCharString(charData, subsetCFF);
  99. for (int j = 0; j < origOperands.size(); j++) {
  100. assertTrue(origOperands.get(j).equals(subsetOperands.get(j)));
  101. }
  102. }
  103. }
  104. /**
  105. * Recursively reads and constructs the full CharString for comparison
  106. * @param data The original byte data of the CharString
  107. * @param cffData The CFFDataReader containing the subroutine indexes
  108. * @return Returns a list of parsed operands and operators
  109. * @throws IOException
  110. */
  111. private List<BytesNumber> getFullCharString(byte[] data, CFFDataReader cffData) throws IOException {
  112. CFFIndexData localIndexSubr = cffData.getLocalIndexSubr();
  113. CFFIndexData globalIndexSubr = cffData.getGlobalIndexSubr();
  114. boolean hasLocalSubroutines = localIndexSubr != null && localIndexSubr.getNumObjects() > 0;
  115. boolean hasGlobalSubroutines = globalIndexSubr != null && globalIndexSubr.getNumObjects() > 0;
  116. ArrayList<BytesNumber> operands = new ArrayList<BytesNumber>();
  117. for (int dataPos = 0; dataPos < data.length; dataPos++) {
  118. int b0 = data[dataPos] & 0xff;
  119. if (b0 == 10 && hasLocalSubroutines) {
  120. int subrNumber = getSubrNumber(localIndexSubr.getNumObjects(),
  121. operands.get(operands.size() - 1).getNumber());
  122. byte[] subr = localIndexSubr.getValue(subrNumber);
  123. List<BytesNumber> subrOperands = getFullCharString(subr, cffData);
  124. operands = mergeOperands(operands, subrOperands);
  125. } else if (b0 == 29 && hasGlobalSubroutines) {
  126. int subrNumber = getSubrNumber(globalIndexSubr.getNumObjects(),
  127. operands.get(operands.size() - 1).getNumber());
  128. byte[] subr = globalIndexSubr.getValue(subrNumber);
  129. ArrayList<BytesNumber> subrOperands = (ArrayList<BytesNumber>)getFullCharString(subr, cffData);
  130. operands = mergeOperands(operands, subrOperands);
  131. } else if ((b0 >= 0 && b0 <= 27) || (b0 >= 29 && b0 <= 31)) {
  132. int size = 1;
  133. int b1 = -1;
  134. if (b0 == 12) {
  135. b1 = data[dataPos++] & 0xff;
  136. size = 2;
  137. }
  138. if (b0 == 19 || b0 == 20) {
  139. dataPos += 1;
  140. size = 2;
  141. }
  142. operands.add(new Operator(b0, size, getOperatorName(b0, b1)));
  143. } else if (b0 == 28 || (b0 >= 32 && b0 <= 255)) {
  144. operands.add(readNumber(b0, data, dataPos));
  145. dataPos += operands.get(operands.size() - 1).getNumBytes() - 1;
  146. }
  147. }
  148. return operands;
  149. }
  150. /**
  151. * Merges two lists of operands. This is typically used to merge the CharString
  152. * data with that of a parsed and referenced subroutine.
  153. * @param charString The parsed CharString data so far
  154. * @param subroutine The parsed elements from a subroutine
  155. * @return Returns a merged list of both CharString and subroutine elements.
  156. */
  157. private ArrayList<BytesNumber> mergeOperands(List<BytesNumber> charString,
  158. List<BytesNumber> subroutine) {
  159. BytesNumber[] charStringOperands = charString.toArray(new BytesNumber[0]);
  160. BytesNumber[] subroutineOperands = subroutine.toArray(new BytesNumber[0]);
  161. BytesNumber[] mergeData = new BytesNumber[charStringOperands.length - 1
  162. + subroutineOperands.length - 1];
  163. System.arraycopy(charStringOperands, 0, mergeData, 0, charStringOperands.length - 1);
  164. System.arraycopy(subroutineOperands, 0, mergeData, charStringOperands.length - 1,
  165. subroutineOperands.length - 1);
  166. ArrayList<BytesNumber> hello = new ArrayList<BytesNumber>();
  167. hello.addAll(Arrays.asList(mergeData));
  168. return hello;
  169. }
  170. /**
  171. * Parses a number from one or more bytes
  172. * @param b0 The first byte to identify how to interpret the number
  173. * @param input The original byte data containing the number
  174. * @param curPos The current position of the number
  175. * @return Returns the number
  176. * @throws IOException
  177. */
  178. private BytesNumber readNumber(int b0, byte[] input, int curPos) throws IOException {
  179. if (b0 == 28) {
  180. int b1 = input[curPos + 1] & 0xff;
  181. int b2 = input[curPos + 2] & 0xff;
  182. return new BytesNumber(Integer.valueOf((short) (b1 << 8 | b2)), 3);
  183. } else if (b0 >= 32 && b0 <= 246) {
  184. return new BytesNumber(Integer.valueOf(b0 - 139), 1);
  185. } else if (b0 >= 247 && b0 <= 250) {
  186. int b1 = input[curPos + 1] & 0xff;
  187. return new BytesNumber(Integer.valueOf((b0 - 247) * 256 + b1 + 108), 2);
  188. } else if (b0 >= 251 && b0 <= 254) {
  189. int b1 = input[curPos + 1] & 0xff;
  190. return new BytesNumber(Integer.valueOf(-(b0 - 251) * 256 - b1 - 108), 2);
  191. } else if (b0 == 255) {
  192. int b1 = input[curPos + 1] & 0xff;
  193. int b2 = input[curPos + 2] & 0xff;
  194. return new BytesNumber(Integer.valueOf((short)(b1 << 8 | b2)), 5);
  195. } else {
  196. throw new IllegalArgumentException();
  197. }
  198. }
  199. /**
  200. * Gets the subroutine number according to the number of subroutines
  201. * and the provided operand.
  202. * @param numSubroutines The number of subroutines used to calculate the
  203. * subroutine reference.
  204. * @param operand The operand for the subroutine
  205. * @return Returns the calculated subroutine number
  206. */
  207. private int getSubrNumber(int numSubroutines, int operand) {
  208. int bias = getBias(numSubroutines);
  209. return bias + operand;
  210. }
  211. /**
  212. * Gets the bias give the number of subroutines. This is used in the
  213. * calculation to determine a subroutine's number
  214. * @param subrCount The number of subroutines for a given index
  215. * @return Returns the bias value
  216. */
  217. private int getBias(int subrCount) {
  218. if (subrCount < 1240) {
  219. return 107;
  220. } else if (subrCount < 33900) {
  221. return 1131;
  222. } else {
  223. return 32768;
  224. }
  225. }
  226. /**
  227. * A class representing an operator from the CharString data
  228. */
  229. private class Operator extends BytesNumber {
  230. private String opName = "";
  231. public Operator(int number, int numBytes, String opName) {
  232. super(number, numBytes);
  233. this.opName = opName;
  234. }
  235. public String toString() {
  236. return String.format("[%s]", opName);
  237. }
  238. }
  239. /**
  240. * Gets the identifying name for the given operator. This is primarily
  241. * used for debugging purposes. See the Type 2 CharString Format specification
  242. * document (Technical Note #5177) Appendix A (Command Codes).
  243. * @param operator The operator code
  244. * @param codeb The second byte of the operator
  245. * @return Returns the operator name.
  246. */
  247. private String getOperatorName(int operator, int operatorB) {
  248. switch (operator) {
  249. case 0: return "Reserved";
  250. case 1: return "hstem";
  251. case 2: return "Reserved";
  252. case 3: return "vstem";
  253. case 4: return "vmoveto";
  254. case 5: return "rlineto";
  255. case 6: return "hlineto";
  256. case 7: return "vlineto";
  257. case 8: return "rrcurveto";
  258. case 9: return "Reserved";
  259. case 10: return "callsubr";
  260. case 11: return "return";
  261. case 12: return getDoubleOpName(operatorB);
  262. case 13: return "Reserved";
  263. case 14: return "enchar";
  264. case 15:
  265. case 16:
  266. case 17: return "Reserved";
  267. case 18: return "hstemhm";
  268. case 19: return "hintmask";
  269. case 20: return "cntrmask";
  270. case 21: return "rmoveto";
  271. case 22: return "hmoveto";
  272. case 23: return "vstemhm";
  273. case 24: return "rcurveline";
  274. case 25: return "rlinecurve";
  275. case 26: return "vvcurveto";
  276. case 27: return "hhcurveto";
  277. case 28: return "shortint";
  278. case 29: return "callgsubr";
  279. case 30: return "vhcurveto";
  280. case 31: return "hvcurveto";
  281. default: return "Unknown";
  282. }
  283. }
  284. /**
  285. * Gets the name of a double byte operator code
  286. * @param operator The second byte of the operator
  287. * @return Returns the name
  288. */
  289. private String getDoubleOpName(int operator) {
  290. switch (operator) {
  291. case 0:
  292. case 1:
  293. case 2: return "Reserved";
  294. case 3: return "and";
  295. case 4: return "or";
  296. case 5: return "not";
  297. case 6:
  298. case 7:
  299. case 8: return "Reserved";
  300. case 9: return "abs";
  301. case 10: return "add";
  302. case 11: return "sub";
  303. case 12: return "div";
  304. case 13: return "Reserved";
  305. case 14: return "neg";
  306. case 15: return "eq";
  307. case 16:
  308. case 17: return "Reserved";
  309. case 18: return "drop";
  310. case 19: return "Reserved";
  311. case 20: return "put";
  312. case 21: return "get";
  313. case 22: return "ifelse";
  314. case 23: return "random";
  315. case 24: return "mul";
  316. case 25: return "Reserved";
  317. case 26: return "sqrt";
  318. case 27: return "dup";
  319. case 28: return "exch";
  320. case 29: return "index";
  321. case 30: return "roll";
  322. case 31:
  323. case 32:
  324. case 33: return "Reserved";
  325. case 34: return "hflex";
  326. case 35: return "flex";
  327. case 36: return "hflex1";
  328. case 37: return "flex1";
  329. case 38: return "Reserved";
  330. default: return "Unknown";
  331. }
  332. }
  333. /**
  334. * Validates the String index data and size
  335. * @throws IOException
  336. */
  337. @Test
  338. public void testStringIndex() throws IOException {
  339. assertEquals(164, cffReaderSourceSans.getStringIndex().getNumObjects());
  340. assertTrue(checkCorrectOffsets(cffReaderSourceSans.getStringIndex()));
  341. assertEquals("Amacron", new String(cffReaderSourceSans.getStringIndex().getValue(5)));
  342. assertEquals("Edotaccent", new String(cffReaderSourceSans.getStringIndex().getValue(32)));
  343. assertEquals("uni0122", new String(cffReaderSourceSans.getStringIndex().getValue(45)));
  344. }
  345. /**
  346. * Validates the Top Dict data
  347. * @throws IOException
  348. */
  349. @Test
  350. public void testTopDictData() throws IOException {
  351. Map<String, DICTEntry> topDictEntries = cffReaderSourceSans.parseDictData(
  352. cffReaderSourceSans.getTopDictIndex().getData());
  353. assertEquals(10, topDictEntries.size());
  354. }
  355. }