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.

GlyfTableTestCase.java 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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.FileInputStream;
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.util.Arrays;
  24. import java.util.HashMap;
  25. import java.util.Map;
  26. import org.junit.Before;
  27. import org.junit.Test;
  28. import static org.junit.Assert.assertTrue;
  29. /**
  30. * Tests {@link GlyfTable}.
  31. */
  32. public class GlyfTableTestCase {
  33. private static final class DirData {
  34. final long offset;
  35. final long length;
  36. DirData(long offset, long length) {
  37. this.offset = offset;
  38. this.length = length;
  39. }
  40. }
  41. private FontFileReader subsetReader;
  42. private long[] glyphOffsets;
  43. private FontFileReader originalFontReader;
  44. @Before
  45. public void setUp() throws IOException {
  46. FileInputStream fontStream = new FileInputStream(
  47. "test/resources/fonts/ttf/DejaVuLGCSerif.ttf");
  48. try {
  49. originalFontReader = new FontFileReader(fontStream);
  50. } finally {
  51. fontStream.close();
  52. }
  53. }
  54. /**
  55. * Tests that composed glyphs are included in the glyph subset if a composite glyph is used.
  56. *
  57. * @throws IOException if an I/O error occurs
  58. */
  59. @Test
  60. public void testPopulateGlyphsWithComposites() throws IOException {
  61. // Glyph 408 -> U+01D8 "uni01D8" this is a composite glyph.
  62. int[] composedIndices = setupTest(408);
  63. int[] expected = new int[composedIndices.length];
  64. expected[1] = 6;
  65. expected[5] = 2;
  66. expected[6] = 4;
  67. assertArrayEquals(expected, composedIndices);
  68. }
  69. /**
  70. * Tests that no glyphs are added if there are no composite glyphs the subset.
  71. *
  72. * @throws IOException if an I/O error occurs
  73. */
  74. @Test
  75. public void testPopulateNoCompositeGlyphs() throws IOException {
  76. int[] composedIndices = setupTest(36, 37, 38); // "A", "B", "C"
  77. int[] expected = new int[composedIndices.length];
  78. // There should be NO composite glyphs
  79. assertArrayEquals(expected, composedIndices);
  80. }
  81. /**
  82. * Tests that glyphs aren't remapped twice if the glyph before a composite glyph has 0-length.
  83. *
  84. * @throws IOException if an I/O error occurs
  85. */
  86. @Test
  87. public void testGlyphsNotRemappedTwice() throws IOException {
  88. int composedGlyph = 12;
  89. // The order of these glyph indices, must NOT be changed! (see javadoc above)
  90. int[] composedIndices = setupTest(1, 2, 3, 16, 2014, 4, 7, 8, 13, 2015, composedGlyph);
  91. // There are 2 composed glyphs within the subset
  92. int[] expected = new int[composedIndices.length];
  93. expected[10] = composedGlyph;
  94. assertArrayEquals(expected, composedIndices);
  95. }
  96. /**
  97. * Tests that the correct glyph is included in the subset, when a composite glyph composed of a
  98. * composite glyph is used.
  99. *
  100. * @throws IOException if an I/O error occurs
  101. */
  102. @Test
  103. public void testSingleRecursionStep() throws IOException {
  104. // Glyph 2077 -> U+283F "uni283F" this is composed of a composite glyph (recursive).
  105. int[] composedIndices = setupTest(2077);
  106. int[] expected = new int[composedIndices.length];
  107. expected[1] = 2;
  108. assertArrayEquals(expected, composedIndices);
  109. }
  110. private int[] setupTest(int... glyphIndices) throws IOException {
  111. Map<Integer, Integer> glyphs = new HashMap<Integer, Integer>();
  112. int index = 0;
  113. glyphs.put(0, index++); // Glyph 0 (.notdef) must ALWAYS be in the subset
  114. for (int glyphIndex : glyphIndices) {
  115. glyphs.put(glyphIndex, index++);
  116. }
  117. setupSubsetReader(glyphs);
  118. readLoca();
  119. return retrieveIndicesOfComposedGlyphs();
  120. }
  121. private void setupSubsetReader(Map<Integer, Integer> glyphs) throws IOException {
  122. TTFSubSetFile fontFile = new TTFSubSetFile();
  123. String header = OFFontLoader.readHeader(subsetReader);
  124. fontFile.readFont(originalFontReader, "Deja", header, glyphs);
  125. byte[] subsetFont = fontFile.getFontSubset();
  126. InputStream intputStream = new ByteArrayInputStream(subsetFont);
  127. subsetReader = new FontFileReader(intputStream);
  128. }
  129. private void readLoca() throws IOException {
  130. DirData loca = getTableData(OFTableName.LOCA.getName());
  131. int numberOfGlyphs = (int) (loca.length - 4) / 4;
  132. glyphOffsets = new long[numberOfGlyphs];
  133. subsetReader.seekSet(loca.offset);
  134. for (int i = 0; i < numberOfGlyphs; i++) {
  135. glyphOffsets[i] = subsetReader.readTTFULong();
  136. }
  137. }
  138. private int[] retrieveIndicesOfComposedGlyphs() throws IOException {
  139. DirData glyf = getTableData(OFTableName.GLYF.getName());
  140. int[] composedGlyphIndices = new int[glyphOffsets.length];
  141. for (int i = 0; i < glyphOffsets.length; i++) {
  142. long glyphOffset = glyphOffsets[i];
  143. if (i != glyphOffsets.length - 1 && glyphOffset == glyphOffsets[i + 1]) {
  144. continue;
  145. }
  146. subsetReader.seekSet(glyf.offset + glyphOffset);
  147. short numberOfContours = subsetReader.readTTFShort();
  148. if (numberOfContours < 0) {
  149. subsetReader.skip(8);
  150. subsetReader.readTTFUShort(); // flags
  151. int glyphIndex = subsetReader.readTTFUShort();
  152. composedGlyphIndices[i] = glyphIndex;
  153. }
  154. }
  155. return composedGlyphIndices;
  156. }
  157. private DirData getTableData(String tableName) throws IOException {
  158. subsetReader.seekSet(0);
  159. subsetReader.skip(12);
  160. String name;
  161. do {
  162. name = subsetReader.readTTFString(4);
  163. subsetReader.skip(4 * 3);
  164. } while (!name.equals(tableName));
  165. subsetReader.skip(-8); // We've found the table, go back to get the data we skipped over
  166. return new DirData(subsetReader.readTTFLong(), subsetReader.readTTFLong());
  167. }
  168. private void assertArrayEquals(int[] expected, int[] actual) {
  169. assertTrue(Arrays.equals(expected, actual));
  170. }
  171. }