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.

GenerateArabicTestData.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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.FileOutputStream;
  23. import java.io.IOException;
  24. import java.io.InputStreamReader;
  25. import java.io.LineNumberReader;
  26. import java.io.ObjectOutputStream;
  27. import java.nio.IntBuffer;
  28. import java.nio.charset.Charset;
  29. import java.util.ArrayList;
  30. import java.util.List;
  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. /**
  36. * Tests for functionality related to the arabic script.
  37. */
  38. public final class GenerateArabicTestData implements ArabicTestConstants {
  39. private GenerateArabicTestData() {
  40. }
  41. public static void main(String[] args) {
  42. boolean compile = false;
  43. boolean help = false;
  44. for (String a : args) {
  45. if (a.equals("-c")) {
  46. compile = true;
  47. }
  48. if (a.equals("-?")) {
  49. help = true;
  50. }
  51. }
  52. if (help) {
  53. help();
  54. } else if (compile) {
  55. compile();
  56. }
  57. }
  58. private static void help() {
  59. StringBuffer sb = new StringBuffer();
  60. sb.append("org.apache.fop.complexscripts.arabic.ArabicTestCase");
  61. sb.append(" [-compile]");
  62. sb.append(" [-?]");
  63. System.out.println(sb.toString());
  64. }
  65. private static void compile() {
  66. for (String sfn : SRC_FILES) {
  67. try {
  68. String spn = SRC_FILES_DIR + File.separator + sfn + "." + WF_FILE_SRC_EXT;
  69. compile(WF_FILE_SCRIPT, WF_FILE_LANGUAGE, spn);
  70. } catch (Exception e) {
  71. System.err.println(e.getMessage());
  72. }
  73. }
  74. }
  75. private static void compile(String script, String language, String spn) {
  76. int fno = 0;
  77. for (String tfn : TTX_FONTS) {
  78. TTXFile tf = TTXFile.getFromCache(TTX_FONTS_DIR + File.separator + tfn);
  79. assert tf != null;
  80. List data = compile(script, language, spn, tfn, tf);
  81. output(makeDataPathName(spn, fno++), data);
  82. }
  83. }
  84. private static List compile(String script, String language, String spn, String tfn, TTXFile tf) {
  85. List<Object[]> data = new ArrayList<Object[]>();
  86. data.add(new Object[] { script, language, spn, tfn });
  87. GlyphSubstitutionTable gsub = tf.getGSUB();
  88. GlyphPositioningTable gpos = tf.getGPOS();
  89. int[] widths = tf.getWidths();
  90. if ((gsub != null) && (gpos != null)) {
  91. FileInputStream fis = null;
  92. try {
  93. fis = new FileInputStream(spn);
  94. if (fis != null) {
  95. LineNumberReader lr = new LineNumberReader(new InputStreamReader(fis, Charset.forName("UTF-8")));
  96. String wf;
  97. while ((wf = lr.readLine()) != null) {
  98. GlyphSequence igs = tf.mapCharsToGlyphs(wf);
  99. GlyphSequence ogs = gsub.substitute(igs, script, language);
  100. int[][] paa = new int [ ogs.getGlyphCount() ] [ 4 ];
  101. if (!gpos.position(ogs, script, language, 1000, widths, paa)) {
  102. paa = null;
  103. }
  104. data.add(new Object[] { wf, getGlyphs(igs), getGlyphs(ogs), paa });
  105. }
  106. lr.close();
  107. }
  108. } catch (FileNotFoundException e) {
  109. throw new RuntimeException(e.getMessage(), e);
  110. } catch (IOException e) {
  111. throw new RuntimeException(e.getMessage(), e);
  112. } catch (Exception e) {
  113. throw new RuntimeException(e.getMessage(), e);
  114. } finally {
  115. if (fis != null) {
  116. try { fis.close(); } catch (Exception e) { /* NOP */ }
  117. }
  118. }
  119. } else {
  120. assert gsub != null;
  121. assert gpos != null;
  122. }
  123. System.err.println("compiled " + (data.size() - 1) + " word forms using font " + tfn);
  124. return data;
  125. }
  126. private static int[] getGlyphs(GlyphSequence gs) {
  127. IntBuffer gb = gs.getGlyphs();
  128. int[] ga = new int [ gb.limit() ];
  129. gb.rewind();
  130. gb.get(ga);
  131. return ga;
  132. }
  133. private static String makeDataPathName(String spn, int fno) {
  134. File f = new File(spn);
  135. return DAT_FILES_DIR + File.separator + stripExtension(f.getName()) + "-f" + fno + "." + WF_FILE_DAT_EXT;
  136. }
  137. private static String stripExtension(String s) {
  138. int i = s.lastIndexOf('.');
  139. if (i >= 0) {
  140. return s.substring(0, i);
  141. } else {
  142. return s;
  143. }
  144. }
  145. private static void output(String dpn, List<Object[]> data) {
  146. FileOutputStream fos = null;
  147. try {
  148. fos = new FileOutputStream(dpn);
  149. if (fos != null) {
  150. ObjectOutputStream oos = new ObjectOutputStream(fos);
  151. oos.writeObject(data);
  152. oos.close();
  153. }
  154. } catch (FileNotFoundException e) {
  155. throw new RuntimeException(e.getMessage(), e);
  156. } catch (IOException e) {
  157. throw new RuntimeException(e.getMessage(), e);
  158. } catch (Exception e) {
  159. throw new RuntimeException(e.getMessage(), e);
  160. } finally {
  161. if (fos != null) {
  162. try { fos.close(); } catch (Exception e) { /* NOP */ }
  163. }
  164. }
  165. }
  166. }