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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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.HashMap;
  21. import java.util.List;
  22. import java.util.Map;
  23. import org.junit.Before;
  24. import org.junit.Test;
  25. import static org.junit.Assert.assertEquals;
  26. import static org.junit.Assert.assertTrue;
  27. import org.apache.fontbox.cff.CFFDataInput;
  28. import org.apache.fontbox.cff.CFFFont;
  29. import org.apache.fontbox.cff.CFFParser;
  30. import org.apache.fontbox.cff.IndexData;
  31. import org.apache.fontbox.cff.Type2CharStringParser;
  32. import org.apache.fop.fonts.cff.CFFDataReader;
  33. import org.apache.fop.fonts.cff.CFFDataReader.CFFIndexData;
  34. import org.apache.fop.fonts.cff.CFFDataReader.DICTEntry;
  35. public class OTFSubSetFileTestCase extends OTFFileTestCase {
  36. CFFDataReader cffReaderSourceSans;
  37. private OTFSubSetFile sourceSansSubset;
  38. private byte[] sourceSansData;
  39. CFFDataReader cffReaderHeitiStd;
  40. public OTFSubSetFileTestCase() throws IOException {
  41. super();
  42. }
  43. /**
  44. * Initialises the test by creating the font subset. A CFFDataReader is
  45. * also created based on the subset data for use in the tests.
  46. * @throws IOException
  47. */
  48. @Before
  49. public void setUp() throws Exception {
  50. super.setUp();
  51. Map<Integer, Integer> glyphs = new HashMap<Integer, Integer>();
  52. for (int i = 0; i < 256; i++) {
  53. glyphs.put(i, i);
  54. }
  55. sourceSansSubset = new OTFSubSetFile();
  56. String sourceSansHeader = OFFontLoader.readHeader(sourceSansReader);
  57. sourceSansSubset.readFont(sourceSansReader, "SourceSansProBold", sourceSansHeader, glyphs);
  58. sourceSansData = sourceSansSubset.getFontSubset();
  59. cffReaderSourceSans = new CFFDataReader(sourceSansData);
  60. }
  61. /**
  62. * Validates the CharString data against the original font
  63. * @throws IOException
  64. */
  65. @Test
  66. public void testCharStringIndex() throws IOException {
  67. assertEquals(256, cffReaderSourceSans.getCharStringIndex().getNumObjects());
  68. assertTrue(checkCorrectOffsets(cffReaderSourceSans.getCharStringIndex()));
  69. validateCharStrings(cffReaderSourceSans);
  70. }
  71. private boolean checkCorrectOffsets(CFFIndexData indexData) {
  72. int last = 0;
  73. for (int i = 0; i < indexData.getOffsets().length; i++) {
  74. if (indexData.getOffsets()[i] < last) {
  75. return false;
  76. }
  77. last = indexData.getOffsets()[i];
  78. }
  79. return true;
  80. }
  81. private void validateCharStrings(CFFDataReader cffReader) throws IOException {
  82. CFFFont sourceSansOriginal = sourceSansProBold.fileFont;
  83. Map<String, byte[]> origCharStringData = sourceSansOriginal.getCharStringsDict();
  84. IndexData origGlobalIndex = sourceSansOriginal.getGlobalSubrIndex();
  85. IndexData origLocalIndex = sourceSansOriginal.getLocalSubrIndex();
  86. CFFDataInput globalSubrs = new CFFDataInput(cffReader.getGlobalIndexSubr().getByteData());
  87. CFFDataInput localSubrs = new CFFDataInput(cffReader.getLocalIndexSubr().getByteData());
  88. IndexData globalIndexData = CFFParser.readIndexData(globalSubrs);
  89. IndexData localIndexData = CFFParser.readIndexData(localSubrs);
  90. CFFIndexData charStrings = cffReader.getCharStringIndex();
  91. for (int i = 0; i < charStrings.getNumObjects(); i++) {
  92. byte[] charData = charStrings.getValue(i);
  93. Type2CharStringParser parser = new Type2CharStringParser();
  94. byte[] origCharData = origCharStringData.get(origCharStringData.keySet().toArray(
  95. new String[0])[i]);
  96. List<Object> origSeq = parser.parse(origCharData, origGlobalIndex, origLocalIndex);
  97. List<Object> subsetSeq = parser.parse(charData, globalIndexData, localIndexData);
  98. //Validates the subset glyph render routines against the originals
  99. assertEquals(origSeq, subsetSeq);
  100. }
  101. }
  102. /**
  103. * Validates the String index data and size
  104. * @throws IOException
  105. */
  106. @Test
  107. public void testStringIndex() throws IOException {
  108. assertEquals(164, cffReaderSourceSans.getStringIndex().getNumObjects());
  109. assertTrue(checkCorrectOffsets(cffReaderSourceSans.getStringIndex()));
  110. assertEquals("Amacron", new String(cffReaderSourceSans.getStringIndex().getValue(5)));
  111. assertEquals("Edotaccent", new String(cffReaderSourceSans.getStringIndex().getValue(32)));
  112. assertEquals("uni0122", new String(cffReaderSourceSans.getStringIndex().getValue(45)));
  113. }
  114. /**
  115. * Validates the Top Dict data
  116. * @throws IOException
  117. */
  118. @Test
  119. public void testTopDictData() throws IOException {
  120. Map<String, DICTEntry> topDictEntries = cffReaderSourceSans.parseDictData(
  121. cffReaderSourceSans.getTopDictIndex().getData());
  122. assertEquals(10, topDictEntries.size());
  123. }
  124. }