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.

ArabicTestCase.java 7.1KB

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