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.1KB

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