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.

ArabicWordFormsTestCase.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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.complexscripts.scripts.arabic;
  19. import java.io.File;
  20. import java.io.FileInputStream;
  21. import java.io.FileNotFoundException;
  22. import java.io.FilenameFilter;
  23. import java.io.IOException;
  24. import java.io.ObjectInputStream;
  25. import java.nio.IntBuffer;
  26. import java.util.List;
  27. import org.junit.Test;
  28. import static org.junit.Assert.assertEquals;
  29. import static org.junit.Assert.assertTrue;
  30. import static org.junit.Assert.fail;
  31. import org.apache.commons.io.IOUtils;
  32. import org.apache.fop.complexscripts.fonts.GlyphPositioningTable;
  33. import org.apache.fop.complexscripts.fonts.GlyphSubstitutionTable;
  34. import org.apache.fop.complexscripts.fonts.ttx.TTXFile;
  35. import org.apache.fop.complexscripts.util.GlyphSequence;
  36. // CSOFF: LineLength
  37. /**
  38. * Tests for functionality related to the arabic script.
  39. */
  40. public class ArabicWordFormsTestCase implements ArabicWordFormsConstants {
  41. @Test
  42. public void testArabicWordForms() {
  43. for (String sfn : SRC_FILES) {
  44. try {
  45. processWordForms(new File(DAT_FILES_DIR));
  46. } catch (Exception e) {
  47. fail(e.getMessage());
  48. }
  49. }
  50. }
  51. private void processWordForms(File dfd) {
  52. String[] files = listWordFormFiles(dfd);
  53. for (String fn : files) {
  54. File dff = new File(dfd, fn);
  55. processWordForms(dff.getAbsolutePath());
  56. }
  57. }
  58. private String[] listWordFormFiles(File dfd) {
  59. return dfd.list(new FilenameFilter() {
  60. public boolean accept(File f, String name) {
  61. return hasPrefixFrom(name, SRC_FILES) && hasExtension(name, WF_FILE_DAT_EXT);
  62. }
  63. private boolean hasPrefixFrom(String name, String[] prefixes) {
  64. for (String p : prefixes) {
  65. if (name.startsWith(p)) {
  66. return true;
  67. }
  68. }
  69. return false;
  70. }
  71. private boolean hasExtension(String name, String extension) {
  72. return name.endsWith("." + extension);
  73. }
  74. });
  75. }
  76. private void processWordForms(String dpn) {
  77. FileInputStream fis = null;
  78. try {
  79. fis = new FileInputStream(dpn);
  80. ObjectInputStream ois = new ObjectInputStream(fis);
  81. List<Object[]> data = (List<Object[]>) ois.readObject();
  82. if (data != null) {
  83. processWordForms(data);
  84. }
  85. ois.close();
  86. } catch (FileNotFoundException e) {
  87. throw new RuntimeException(e.getMessage(), e);
  88. } catch (IOException e) {
  89. throw new RuntimeException(e.getMessage(), e);
  90. } catch (Exception e) {
  91. throw new RuntimeException(e.getMessage(), e);
  92. } finally {
  93. IOUtils.closeQuietly(fis);
  94. }
  95. }
  96. private void processWordForms(List<Object[]> data) {
  97. assert data != null;
  98. assert data.size() > 0;
  99. String script = null;
  100. String language = null;
  101. String tfn = null;
  102. TTXFile tf = null;
  103. GlyphSubstitutionTable gsub = null;
  104. GlyphPositioningTable gpos = null;
  105. int[] widths = null;
  106. for (Object[] d : data) {
  107. if (script == null) {
  108. assert d.length >= 4;
  109. script = (String) d[0];
  110. language = (String) d[1];
  111. tfn = (String) d[3];
  112. tf = TTXFile.getFromCache(TTX_FONTS_DIR + File.separator + tfn);
  113. assertTrue(tf != null);
  114. gsub = tf.getGSUB();
  115. assertTrue(gsub != null);
  116. gpos = tf.getGPOS();
  117. assertTrue(gpos != null);
  118. widths = tf.getWidths();
  119. assertTrue(widths != null);
  120. } else {
  121. assert tf != null;
  122. assert gsub != null;
  123. assert gpos != null;
  124. assert tfn != null;
  125. assert d.length >= 4;
  126. String wf = (String) d[0];
  127. int[] iga = (int[]) d[1];
  128. int[] oga = (int[]) d[2];
  129. int[][] paa = (int[][]) d[3];
  130. GlyphSequence tigs = tf.mapCharsToGlyphs(wf);
  131. assertSameGlyphs(iga, getGlyphs(tigs), "input glyphs", wf, tfn);
  132. GlyphSequence togs = gsub.substitute(tigs, script, language);
  133. assertSameGlyphs(oga, getGlyphs(togs), "output glyphs", wf, tfn);
  134. int[][] tpaa = new int [ togs.getGlyphCount() ] [ 4 ];
  135. if (gpos.position(togs, script, language, 1000, widths, tpaa)) {
  136. assertSameAdjustments(paa, tpaa, wf, tfn);
  137. } else if (paa != null) {
  138. assertEquals("unequal adjustment count, word form(" + wf + "), font (" + tfn + ")", paa.length, 0);
  139. }
  140. }
  141. }
  142. }
  143. private void assertSameGlyphs(int[] expected, int[] actual, String label, String wf, String tfn) {
  144. assertEquals(label + ": unequal glyph count, word form(" + wf + "), font (" + tfn + ")", expected.length, actual.length);
  145. for (int i = 0, n = expected.length; i < n; i++) {
  146. int e = expected[i];
  147. int a = actual[i];
  148. assertEquals(label + ": unequal glyphs[" + i + "], word form(" + wf + "), font (" + tfn + ")", e, a);
  149. }
  150. }
  151. private void assertSameAdjustments(int[][] expected, int[][] actual, String wf, String tfn) {
  152. assertEquals("unequal adjustment count, word form(" + wf + "), font (" + tfn + ")", expected.length, actual.length);
  153. for (int i = 0, n = expected.length; i < n; i++) {
  154. int[] ea = expected[i];
  155. int[] aa = actual[i];
  156. assertEquals("bad adjustments length, word form(" + wf + "), font (" + tfn + ")", ea.length, aa.length);
  157. for (int k = 0; k < 4; k++) {
  158. int e = ea[k];
  159. int a = aa[k];
  160. assertEquals("unequal adjustment[" + i + "][" + k + "], word form(" + wf + "), font (" + tfn + ")", e, a);
  161. }
  162. }
  163. }
  164. private static int[] getGlyphs(GlyphSequence gs) {
  165. IntBuffer gb = gs.getGlyphs();
  166. int[] ga = new int [ gb.limit() ];
  167. gb.rewind();
  168. gb.get(ga);
  169. return ga;
  170. }
  171. }