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.5KB

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