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.

CIDSubsetTestCase.java 6.5KB

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.fonts;
  19. import java.util.Arrays;
  20. import java.util.BitSet;
  21. import java.util.HashMap;
  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 static org.mockito.Mockito.mock;
  28. import static org.mockito.Mockito.when;
  29. import org.apache.fop.util.CharUtilities;
  30. public class CIDSubsetTestCase {
  31. /** The surrogate pair is expected to be in the end of the string. Change it carefully. */
  32. private static final String TEXT = "Hello CIDSubset \uD83D\uDCA9";
  33. private CIDSubset cidSub;
  34. private BitSet bs;
  35. private int[] codepoints;
  36. private int[] widths;
  37. private Map<Integer, Integer> glyphToSelector;
  38. private Map<Integer, Integer> charToSelector;
  39. private HashMap<Integer, Integer> charToGlyph;
  40. @Before
  41. public void setup() {
  42. bs = new BitSet();
  43. glyphToSelector = new HashMap<Integer, Integer>();
  44. charToSelector = new HashMap<Integer, Integer>();
  45. charToGlyph = new HashMap<Integer, Integer>();
  46. codepoints = new int[TEXT.length() - 1]; // skip one char because of surrogate pair
  47. bs.set(0); // .notdef
  48. int glyphIdx = 0;
  49. for (int i = 0; i < TEXT.length(); i++) {
  50. int cp = TEXT.codePointAt(i);
  51. i += CharUtilities.incrementIfNonBMP(cp);
  52. codepoints[glyphIdx] = cp;
  53. glyphIdx++;
  54. // Assign glyphIdx for each character
  55. // glyphIndex 0 is reserved for .notdef
  56. if (!charToGlyph.containsKey(cp)) {
  57. charToGlyph.put(cp, glyphIdx);
  58. bs.set(glyphIdx);
  59. }
  60. }
  61. // fill widths up to max glyph index + 1 for .notdef
  62. widths = new int[glyphIdx + 1];
  63. for (int i = 0; i < widths.length; i++) {
  64. widths[i] = 100 * i;
  65. }
  66. MultiByteFont mbFont = mock(MultiByteFont.class);
  67. when(mbFont.getGlyphIndices()).thenReturn(bs);
  68. when(mbFont.getWidths()).thenReturn(widths);
  69. cidSub = new CIDSubset(mbFont);
  70. for (int i = 0; i < codepoints.length; i++) {
  71. int codepoint = codepoints[i];
  72. int glyphIndex = charToGlyph.get(codepoint);
  73. int subsetCharSelector = cidSub.mapCodePoint(glyphIndex, codepoint);
  74. glyphToSelector.put(glyphIndex, subsetCharSelector);
  75. charToSelector.put(codepoint, subsetCharSelector);
  76. }
  77. }
  78. @Test
  79. public void testGetOriginalGlyphIndex() {
  80. // index 5 exists
  81. int codepoint = (int) TEXT.charAt(0);
  82. int subsetCharSelector = charToSelector.get(codepoint);
  83. int originalIdx = charToGlyph.get(codepoint);
  84. assertEquals(originalIdx, cidSub.getOriginalGlyphIndex(subsetCharSelector));
  85. }
  86. @Test
  87. public void testGetUnicode() {
  88. int bmpCodepoint = codepoints[5];
  89. int nonBmpCodepoint = codepoints[codepoints.length - 1];
  90. assertEquals(bmpCodepoint, cidSub.getUnicode(charToSelector.get(bmpCodepoint)));
  91. assertEquals(nonBmpCodepoint, cidSub.getUnicode(charToSelector.get(nonBmpCodepoint)));
  92. // not exist
  93. assertEquals(CharUtilities.NOT_A_CHARACTER, cidSub.getUnicode(-1));
  94. }
  95. @Test
  96. public void testMapChar() {
  97. for (Map.Entry<Integer, Integer> entry : glyphToSelector.entrySet()) {
  98. int glyphIndex = entry.getKey();
  99. int subsetCharSelector = entry.getValue();
  100. // the value of codepoint is not relevant for the purpose of this test: safe to take a random value.
  101. int codepoint = 'a';
  102. assertEquals(subsetCharSelector, cidSub.mapChar(glyphIndex, (char) codepoint));
  103. }
  104. }
  105. @Test
  106. public void testMapCodePoint() {
  107. for (Map.Entry<Integer, Integer> entry : glyphToSelector.entrySet()) {
  108. int glyphIndex = entry.getKey();
  109. int subsetCharSelector = entry.getValue();
  110. // the value of codepoint is not relevant for the purpose of this test: safe to take a random value.
  111. int codepoint = 'a';
  112. assertEquals(subsetCharSelector, cidSub.mapCodePoint(glyphIndex, codepoint));
  113. }
  114. }
  115. @Test
  116. public void testGetGlyphs() {
  117. Map<Integer, Integer> fontGlyphs = cidSub.getGlyphs();
  118. for (Integer key : fontGlyphs.keySet()) {
  119. if (key == 0) {
  120. // the entry 0 -> 0 is set in the CIDSubset constructor
  121. assertEquals(0, fontGlyphs.get(key).intValue());
  122. continue;
  123. }
  124. assertEquals(glyphToSelector.get(key), fontGlyphs.get(key));
  125. }
  126. assertEquals(glyphToSelector.size() + 1, fontGlyphs.size());
  127. }
  128. @Test
  129. public void testGetChars() {
  130. char[] chars = cidSub.getChars();
  131. char[] expected = TEXT.toCharArray();
  132. Arrays.sort(chars);
  133. Arrays.sort(expected);
  134. // checks if the returned arrays contains all the expected chars
  135. for (char c : expected) {
  136. assertTrue(Arrays.binarySearch(chars, c) >= 0);
  137. }
  138. // checks if the returned array do not contains unexpected chars
  139. for (char c : chars) {
  140. if (c == CharUtilities.NOT_A_CHARACTER) {
  141. continue;
  142. }
  143. assertTrue(Arrays.binarySearch(expected, c) >= 0);
  144. }
  145. }
  146. @Test
  147. public void testGetNumberOfGlyphs() {
  148. // +1 because of .notdef
  149. assertEquals(glyphToSelector.size() + 1, cidSub.getNumberOfGlyphs());
  150. }
  151. @Test
  152. public void testGetGlyphIndices() {
  153. assertEquals(bs, cidSub.getGlyphIndices());
  154. }
  155. @Test
  156. public void testGetWidths() {
  157. Arrays.sort(widths);
  158. for (int width : cidSub.getWidths()) {
  159. assertTrue(Arrays.binarySearch(widths, width) >= 0);
  160. }
  161. }
  162. }