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.

CFFDataReaderTestCase.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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.cff;
  19. import java.io.IOException;
  20. import java.util.Map;
  21. import java.util.Random;
  22. import org.junit.Before;
  23. import org.junit.Test;
  24. import static org.junit.Assert.assertEquals;
  25. import org.apache.fontbox.cff.CFFDataInput;
  26. import org.apache.fop.fonts.cff.CFFDataReader.CFFIndexData;
  27. import org.apache.fop.fonts.cff.CFFDataReader.DICTEntry;
  28. import org.apache.fop.fonts.truetype.OTFSubSetFile;
  29. public class CFFDataReaderTestCase {
  30. private CFFDataReader cffReader;
  31. /**
  32. * Initializes the CFFDataReader for testing purposes
  33. */
  34. @Before
  35. public void setUp() {
  36. cffReader = new CFFDataReader();
  37. }
  38. /**
  39. * Parses a test dictionary to verify whether the stored data is read correctly.
  40. * @throws IOException
  41. */
  42. @Test
  43. public void parseDictData() throws IOException {
  44. byte[] testDictData = prepareDictData();
  45. Map<String, DICTEntry> testTopDict = cffReader.parseDictData(testDictData);
  46. validateDictData(testTopDict);
  47. }
  48. private byte[] prepareDictData() {
  49. byte[] testDictData = new byte[0];
  50. //Version
  51. testDictData = OTFSubSetFile.concatArray(testDictData, OTFSubSetFile.createNewRef(
  52. 392, new int[] { 0 }, -1));
  53. //Notice
  54. testDictData = OTFSubSetFile.concatArray(testDictData, OTFSubSetFile.createNewRef(
  55. 393, new int[] { 1 }, -1));
  56. //Copyright
  57. testDictData = OTFSubSetFile.concatArray(testDictData, OTFSubSetFile.createNewRef(
  58. 394, new int[] { 12, 0 }, -1));
  59. //FullName
  60. testDictData = OTFSubSetFile.concatArray(testDictData, OTFSubSetFile.createNewRef(
  61. 395, new int[] { 2 }, -1));
  62. //FamilyName
  63. testDictData = OTFSubSetFile.concatArray(testDictData, OTFSubSetFile.createNewRef(
  64. 396, new int[] { 3 }, -1));
  65. //Weight
  66. testDictData = OTFSubSetFile.concatArray(testDictData, OTFSubSetFile.createNewRef(
  67. 397, new int[] { 4 }, -1));
  68. //isFixedPitch (boolean = false)
  69. testDictData = OTFSubSetFile.concatArray(testDictData, OTFSubSetFile.createNewRef(
  70. 0, new int[] { 12, 1 }, -1));
  71. //FontBBox
  72. testDictData = OTFSubSetFile.concatArray(testDictData, OTFSubSetFile.createNewRef(
  73. -50, new int[0], -1));
  74. testDictData = OTFSubSetFile.concatArray(testDictData, OTFSubSetFile.createNewRef(
  75. -40, new int[0], -1));
  76. testDictData = OTFSubSetFile.concatArray(testDictData, OTFSubSetFile.createNewRef(
  77. 100, new int[0], -1));
  78. testDictData = OTFSubSetFile.concatArray(testDictData, OTFSubSetFile.createNewRef(
  79. 120, new int[] { 5 }, -1));
  80. //charset
  81. testDictData = OTFSubSetFile.concatArray(testDictData, OTFSubSetFile.createNewRef(
  82. 1234, new int[] { 15 }, -1));
  83. //CharStrings
  84. testDictData = OTFSubSetFile.concatArray(testDictData, OTFSubSetFile.createNewRef(
  85. 3654, new int[] { 17 }, -1));
  86. //Private
  87. testDictData = OTFSubSetFile.concatArray(testDictData, OTFSubSetFile.createNewRef(
  88. 11454, new int[] { 18 }, -1));
  89. return testDictData;
  90. }
  91. private void validateDictData(Map<String, DICTEntry> dictMap) {
  92. //SID Values (numbers)
  93. assertEquals(dictMap.get("version").getOperands().get(0).intValue(), 392);
  94. assertEquals(dictMap.get("Notice").getOperands().get(0).intValue(), 393);
  95. assertEquals(dictMap.get("Copyright").getOperands().get(0).intValue(), 394);
  96. assertEquals(dictMap.get("FullName").getOperands().get(0).intValue(), 395);
  97. assertEquals(dictMap.get("FamilyName").getOperands().get(0).intValue(), 396);
  98. assertEquals(dictMap.get("Weight").getOperands().get(0).intValue(), 397);
  99. //Boolean comparison
  100. assertEquals(dictMap.get("isFixedPitch").getOperands().get(0).intValue(), 0);
  101. //Array comparison
  102. int[] fontBBox = { -50, -40, 100, 120 };
  103. DICTEntry fontBBoxEntry = dictMap.get("FontBBox");
  104. for (int i = 0; i < fontBBoxEntry.getOperands().size(); i++) {
  105. assertEquals(fontBBoxEntry.getOperands().get(i).intValue(), fontBBox[i]);
  106. }
  107. //Multi-byte offset (number)
  108. assertEquals(dictMap.get("charset").getOperands().get(0).intValue(), 1234);
  109. assertEquals(dictMap.get("CharStrings").getOperands().get(0).intValue(), 3654);
  110. //Larger offset
  111. assertEquals(dictMap.get("Private").getOperands().get(0).intValue(), 11454);
  112. }
  113. /**
  114. * Tests the parsing of an example byte data index structure
  115. * @throws IOException
  116. */
  117. @Test
  118. public void testIndexParsing() throws IOException {
  119. byte[] testIndex = {
  120. 0, 5, //Number of objects
  121. 1, //Offset size
  122. 1, //Offsets...
  123. 5,
  124. 12,
  125. 24,
  126. 27,
  127. 32
  128. };
  129. Random randGen = new Random();
  130. byte[] data = new byte[31];
  131. for (int i = 0; i < data.length; i++) {
  132. data[i] = (byte)randGen.nextInt(255);
  133. }
  134. testIndex = OTFSubSetFile.concatArray(testIndex, data);
  135. CFFIndexData indexData = cffReader.readIndex(new CFFDataInput(testIndex));
  136. assertEquals(indexData.getNumObjects(), 5);
  137. assertEquals(indexData.getOffSize(), 1);
  138. assertEquals(indexData.getOffsets().length, 6);
  139. assertEquals(indexData.getOffsets()[5], 32);
  140. }
  141. }