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.

PCLTTFFontReaderTestCase.java 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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.render.pcl.fonts;
  19. import java.io.ByteArrayInputStream;
  20. import java.io.File;
  21. import java.io.FileInputStream;
  22. import java.io.IOException;
  23. import java.util.Arrays;
  24. import java.util.HashMap;
  25. import java.util.List;
  26. import java.util.Map;
  27. import org.junit.Before;
  28. import org.junit.Test;
  29. import static org.junit.Assert.assertArrayEquals;
  30. import static org.junit.Assert.assertEquals;
  31. import static org.mockito.Mockito.mock;
  32. import static org.mockito.Mockito.when;
  33. import org.apache.fop.fonts.CustomFont;
  34. import org.apache.fop.fonts.SingleByteFont;
  35. import org.apache.fop.render.java2d.CustomFontMetricsMapper;
  36. import org.apache.fop.render.pcl.fonts.PCLFontSegment.SegmentID;
  37. import org.apache.fop.render.pcl.fonts.truetype.PCLTTFFontReader;
  38. public class PCLTTFFontReaderTestCase {
  39. private CustomFontMetricsMapper customFont = mock(CustomFontMetricsMapper.class);
  40. private PCLByteWriterUtil byteWriter;
  41. private static final String TEST_FONT_A = "./test/resources/fonts/ttf/DejaVuLGCSerif.ttf";
  42. @Before
  43. public void setUp() {
  44. byteWriter = new PCLByteWriterUtil();
  45. }
  46. @Test
  47. public void verifyFontAData() throws Exception {
  48. CustomFont sbFont = mock(CustomFont.class);
  49. when(sbFont.getInputStream()).thenReturn(new FileInputStream(new File(TEST_FONT_A)));
  50. when(customFont.getRealFont()).thenReturn(sbFont);
  51. SingleByteFont font = mock(SingleByteFont.class);
  52. when(font.getGIDFromChar('h')).thenReturn(104);
  53. when(font.getGIDFromChar('e')).thenReturn(101);
  54. when(font.getGIDFromChar('l')).thenReturn(108);
  55. when(font.getGIDFromChar('o')).thenReturn(111);
  56. PCLTTFFontReader reader = new MockPCLTTFFontReader(customFont, byteWriter);
  57. reader.setFont(font);
  58. verifyFontData(reader);
  59. validateOffsets(reader);
  60. validateFontSegments(reader);
  61. }
  62. /**
  63. * Compares the input font data against a sample of the data read and calculated by the reader. The assertions are
  64. * made against data taken from the TrueType Font Analyzer tool.
  65. * @param reader The reader
  66. */
  67. private void verifyFontData(PCLTTFFontReader reader) {
  68. assertEquals(reader.getCellWidth(), 5015); // Bounding box X2 - X1
  69. assertEquals(reader.getCellHeight(), 3254); // Bounding box Y2 - Y1
  70. assertEquals(reader.getCapHeight(), 0); // OS2Table.capHeight
  71. assertEquals(reader.getFontName(), "DejaVu LGC Serif"); // Full name read by TTFFont object
  72. assertEquals(reader.getFirstCode(), 32); // Always 32 for bound font
  73. assertEquals(reader.getLastCode(), 255); // Always 255 for bound font
  74. // Values that require conversion tables (See PCLTTFFontReader.java)
  75. assertEquals(reader.getStrokeWeight(), 0); // Weight Class 400 (regular) should be equivalent 0
  76. assertEquals(reader.getSerifStyle(), 128); // Serif Style 0 should equal 0
  77. assertEquals(reader.getWidthType(), 0); // Width Class 5 (regular) should be equivalent 0
  78. }
  79. private void validateOffsets(PCLTTFFontReader reader) throws IOException {
  80. // Offsets are stored with their character ID with the array [offset, length]
  81. Map<Integer, int[]> offsets = reader.getCharacterOffsets();
  82. // Test data
  83. int[] charC = {27644, 144}; // Char index = 99
  84. int[] charDollar = {16044, 264}; // Char index = 36
  85. int[] charOne = {17808, 176}; // Char index = 49
  86. int[] charUpperD = {21236, 148}; // Char index = 68
  87. int[] charUpperJ = {22140, 176}; // Char index = 74
  88. assertArrayEquals(offsets.get(99), charC);
  89. assertArrayEquals(offsets.get(36), charDollar);
  90. assertArrayEquals(offsets.get(49), charOne);
  91. assertArrayEquals(offsets.get(68), charUpperD);
  92. assertArrayEquals(offsets.get(74), charUpperJ);
  93. }
  94. /**
  95. * Verifies the font segment data copied originally from the TrueType font. Data was verified using TrueType Font
  96. * Analyzer and PCLParaphernalia tool.
  97. * @param reader The reader
  98. * @throws IOException
  99. */
  100. private void validateFontSegments(PCLTTFFontReader reader) throws IOException {
  101. HashMap<Character, Integer> mappedChars = new HashMap<Character, Integer>();
  102. mappedChars.put('H', 1);
  103. mappedChars.put('e', 1);
  104. mappedChars.put('l', 1);
  105. mappedChars.put('o', 1);
  106. List<PCLFontSegment> segments = reader.getFontSegments(mappedChars);
  107. assertEquals(segments.size(), 5);
  108. for (PCLFontSegment segment : segments) {
  109. if (segment.getIdentifier() == SegmentID.PA) {
  110. // Panose
  111. assertEquals(segment.getData().length, 10);
  112. byte[] panose = {2, 6, 6, 3, 5, 6, 5, 2, 2, 4};
  113. assertArrayEquals(segment.getData(), panose);
  114. } else if (segment.getIdentifier() == SegmentID.GT) {
  115. verifyGlobalTrueTypeData(segment, mappedChars.size());
  116. } else if (segment.getIdentifier() == SegmentID.NULL) {
  117. // Terminating segment
  118. assertEquals(segment.getData().length, 0);
  119. }
  120. }
  121. }
  122. private void verifyGlobalTrueTypeData(PCLFontSegment segment, int mappedCharsSize)
  123. throws IOException {
  124. byte[] ttfData = segment.getData();
  125. int currentPos = 0;
  126. //Version
  127. assertEquals(readInt(new byte[]{ttfData[currentPos++], ttfData[currentPos++]}), 1);
  128. assertEquals(readInt(new byte[]{ttfData[currentPos++], ttfData[currentPos++]}), 0);
  129. //Number of tables
  130. int numTables = readInt(new byte[]{ttfData[currentPos++], ttfData[currentPos++]});
  131. assertEquals(numTables, 8);
  132. //Search range
  133. assertEquals(readInt(new byte[]{ttfData[currentPos++], ttfData[currentPos++]}), 128);
  134. //Entry Selector
  135. assertEquals(readInt(new byte[]{ttfData[currentPos++], ttfData[currentPos++]}), 3);
  136. //Range shift
  137. assertEquals(readInt(new byte[]{ttfData[currentPos++], ttfData[currentPos++]}), 0);
  138. String[] validTags = {"head", "hhea", "hmtx", "maxp", "gdir"};
  139. int matches = 0;
  140. for (int i = 0; i < numTables; i++) {
  141. String tag = readTag(new byte[]{ttfData[currentPos++], ttfData[currentPos++],
  142. ttfData[currentPos++], ttfData[currentPos++]});
  143. if (Arrays.asList(validTags).contains(tag)) {
  144. matches++;
  145. }
  146. if (tag.equals("hmtx")) {
  147. currentPos += 4;
  148. int offset = readLong(new byte[]{ttfData[currentPos++], ttfData[currentPos++],
  149. ttfData[currentPos++], ttfData[currentPos++]});
  150. int length = readLong(new byte[]{ttfData[currentPos++], ttfData[currentPos++],
  151. ttfData[currentPos++], ttfData[currentPos++]});
  152. verifyHmtx(ttfData, offset, length, mappedCharsSize);
  153. } else {
  154. currentPos += 12;
  155. }
  156. }
  157. assertEquals(matches, 5);
  158. }
  159. private void verifyHmtx(byte[] ttfData, int offset, int length, int mappedCharsSize)
  160. throws IOException {
  161. ByteArrayInputStream bais = new ByteArrayInputStream(ttfData);
  162. byte[] subsetHmtx = new byte[length];
  163. bais.skip(offset);
  164. bais.read(subsetHmtx);
  165. assertEquals(subsetHmtx.length, (mappedCharsSize + 32) * 4);
  166. }
  167. private int readInt(byte[] bytes) {
  168. return ((0xFF & bytes[0]) << 8) | (0xFF & bytes[1]);
  169. }
  170. private int readLong(byte[] bytes) {
  171. return ((0xFF & bytes[0]) << 24) | ((0xFF & bytes[1]) << 16) | ((0xFF & bytes[2]) << 8)
  172. | (0xFF & bytes[3]);
  173. }
  174. private String readTag(byte[] tag) {
  175. return new String(tag);
  176. }
  177. }