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.

ScriptProcessor.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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;
  19. import java.util.Arrays;
  20. import java.util.HashMap;
  21. import java.util.List;
  22. import java.util.Map;
  23. import org.apache.fop.complexscripts.fonts.GlyphDefinitionTable;
  24. import org.apache.fop.complexscripts.fonts.GlyphPositioningTable;
  25. import org.apache.fop.complexscripts.fonts.GlyphSubstitutionTable;
  26. import org.apache.fop.complexscripts.fonts.GlyphTable;
  27. import org.apache.fop.complexscripts.util.CharScript;
  28. import org.apache.fop.complexscripts.util.GlyphSequence;
  29. import org.apache.fop.complexscripts.util.ScriptContextTester;
  30. import org.apache.fop.fonts.MultiByteFont;
  31. // CSOFF: LineLengthCheck
  32. /**
  33. * <p>Abstract script processor base class for which an implementation of the substitution and positioning methods
  34. * must be supplied.</p>
  35. *
  36. * <p>This work was originally authored by Glenn Adams (gadams@apache.org).</p>
  37. */
  38. public abstract class ScriptProcessor {
  39. private final String script;
  40. private final Map<AssembledLookupsKey, GlyphTable.UseSpec[]> assembledLookups;
  41. /**
  42. * Instantiate a script processor.
  43. * @param script a script identifier
  44. */
  45. protected ScriptProcessor(String script) {
  46. if ((script == null) || (script.length() == 0)) {
  47. throw new IllegalArgumentException("script must be non-empty string");
  48. } else {
  49. this.script = script;
  50. this.assembledLookups = new HashMap<AssembledLookupsKey, GlyphTable.UseSpec[]>();
  51. }
  52. }
  53. /** @return script identifier */
  54. public final String getScript() {
  55. return script;
  56. }
  57. /**
  58. * Obtain script specific required substitution features.
  59. * @return array of suppported substitution features or null
  60. */
  61. public abstract String[] getSubstitutionFeatures();
  62. /**
  63. * Obtain script specific optional substitution features.
  64. * @return array of suppported substitution features or null
  65. */
  66. public String[] getOptionalSubstitutionFeatures() {
  67. return new String[0];
  68. }
  69. /**
  70. * Obtain script specific substitution context tester.
  71. * @return substitution context tester or null
  72. */
  73. public abstract ScriptContextTester getSubstitutionContextTester();
  74. /**
  75. * Perform substitution processing using a specific set of lookup tables.
  76. * @param gsub the glyph substitution table that applies
  77. * @param gs an input glyph sequence
  78. * @param script a script identifier
  79. * @param language a language identifier
  80. * @param lookups a mapping from lookup specifications to glyph subtables to use for substitution processing
  81. * @return the substituted (output) glyph sequence
  82. */
  83. public final GlyphSequence substitute(GlyphSubstitutionTable gsub, GlyphSequence gs, String script, String language,
  84. Map<GlyphTable.LookupSpec, List<GlyphTable.LookupTable>> lookups) {
  85. return substitute(gs, script, language, assembleLookups(gsub, getSubstitutionFeatures(), lookups), getSubstitutionContextTester());
  86. }
  87. /**
  88. * Perform substitution processing using a specific set of ordered glyph table use specifications.
  89. * @param gs an input glyph sequence
  90. * @param script a script identifier
  91. * @param language a language identifier
  92. * @param usa an ordered array of glyph table use specs
  93. * @param sct a script specific context tester (or null)
  94. * @return the substituted (output) glyph sequence
  95. */
  96. public GlyphSequence substitute(GlyphSequence gs, String script, String language, GlyphTable.UseSpec[] usa, ScriptContextTester sct) {
  97. assert usa != null;
  98. for (GlyphTable.UseSpec us : usa) {
  99. gs = us.substitute(gs, script, language, sct);
  100. }
  101. return gs;
  102. }
  103. /**
  104. * Reorder combining marks in glyph sequence so that they precede (within the sequence) the base
  105. * character to which they are applied. N.B. In the case of RTL segments, marks are not reordered by this,
  106. * method since when the segment is reversed by BIDI processing, marks are automatically reordered to precede
  107. * their base glyph.
  108. * @param gdef the glyph definition table that applies
  109. * @param gs an input glyph sequence
  110. * @param unscaledWidths associated unscaled advance widths (also reordered)
  111. * @param gpa associated glyph position adjustments (also reordered)
  112. * @param script a script identifier
  113. * @param language a language identifier
  114. * @return the reordered (output) glyph sequence
  115. */
  116. public GlyphSequence reorderCombiningMarks(GlyphDefinitionTable gdef, GlyphSequence gs, int[] unscaledWidths, int[][] gpa, String script, String language) {
  117. return gs;
  118. }
  119. /**
  120. * Obtain script specific required positioning features.
  121. * @return array of suppported positioning features or null
  122. */
  123. public abstract String[] getPositioningFeatures();
  124. /**
  125. * Obtain script specific optional positioning features.
  126. * @return array of suppported positioning features or null
  127. */
  128. public String[] getOptionalPositioningFeatures() {
  129. return new String[0];
  130. }
  131. /**
  132. * Obtain script specific positioning context tester.
  133. * @return positioning context tester or null
  134. */
  135. public abstract ScriptContextTester getPositioningContextTester();
  136. /**
  137. * Perform positioning processing using a specific set of lookup tables.
  138. * @param gpos the glyph positioning table that applies
  139. * @param gs an input glyph sequence
  140. * @param script a script identifier
  141. * @param language a language identifier
  142. * @param fontSize size in device units
  143. * @param lookups a mapping from lookup specifications to glyph subtables to use for positioning processing
  144. * @param widths array of default advancements for each glyph
  145. * @param adjustments accumulated adjustments array (sequence) of 4-tuples of placement [PX,PY] and advance [AX,AY] adjustments, in that order,
  146. * with one 4-tuple for each element of glyph sequence
  147. * @return true if some adjustment is not zero; otherwise, false
  148. */
  149. public final boolean position(GlyphPositioningTable gpos, GlyphSequence gs, String script, String language, int fontSize,
  150. Map<GlyphTable.LookupSpec, List<GlyphTable.LookupTable>> lookups, int[] widths, int[][] adjustments) {
  151. return position(gs, script, language, fontSize, assembleLookups(gpos, getPositioningFeatures(), lookups), widths, adjustments, getPositioningContextTester());
  152. }
  153. /**
  154. * Perform positioning processing using a specific set of ordered glyph table use specifications.
  155. * @param gs an input glyph sequence
  156. * @param script a script identifier
  157. * @param language a language identifier
  158. * @param fontSize size in device units
  159. * @param usa an ordered array of glyph table use specs
  160. * @param widths array of default advancements for each glyph in font
  161. * @param adjustments accumulated adjustments array (sequence) of 4-tuples of placement [PX,PY] and advance [AX,AY] adjustments, in that order,
  162. * with one 4-tuple for each element of glyph sequence
  163. * @param sct a script specific context tester (or null)
  164. * @return true if some adjustment is not zero; otherwise, false
  165. */
  166. public boolean position(GlyphSequence gs, String script, String language, int fontSize, GlyphTable.UseSpec[] usa, int[] widths, int[][] adjustments, ScriptContextTester sct) {
  167. assert usa != null;
  168. boolean adjusted = false;
  169. for (GlyphTable.UseSpec us : usa) {
  170. if (us.position(gs, script, language, fontSize, widths, adjustments, sct)) {
  171. adjusted = true;
  172. }
  173. }
  174. return adjusted;
  175. }
  176. /**
  177. * Assemble ordered array of lookup table use specifications according to the specified features and candidate lookups,
  178. * where the order of the array is in accordance to the order of the applicable lookup list.
  179. * @param table the governing glyph table
  180. * @param features array of feature identifiers to apply
  181. * @param lookups a mapping from lookup specifications to lists of look tables from which to select lookup tables according to the specified features
  182. * @return ordered array of assembled lookup table use specifications
  183. */
  184. public final GlyphTable.UseSpec[] assembleLookups(GlyphTable table, String[] features,
  185. Map<GlyphTable.LookupSpec, List<GlyphTable.LookupTable>> lookups) {
  186. AssembledLookupsKey key = new AssembledLookupsKey(table, features, lookups);
  187. GlyphTable.UseSpec[] usa;
  188. if ((usa = assembledLookupsGet(key)) != null) {
  189. return usa;
  190. } else {
  191. return assembledLookupsPut(key, table.assembleLookups(features, lookups));
  192. }
  193. }
  194. private GlyphTable.UseSpec[] assembledLookupsGet(AssembledLookupsKey key) {
  195. return assembledLookups.get(key);
  196. }
  197. private GlyphTable.UseSpec[] assembledLookupsPut(AssembledLookupsKey key, GlyphTable.UseSpec[] usa) {
  198. assembledLookups.put(key, usa);
  199. return usa;
  200. }
  201. /**
  202. * Obtain script processor instance associated with specified script.
  203. * @param script a script identifier
  204. * @return a script processor instance or null if none found
  205. */
  206. public static synchronized ScriptProcessor getInstance(String script, Map<String, ScriptProcessor> processors) {
  207. ScriptProcessor sp = null;
  208. assert processors != null;
  209. if ((sp = processors.get(script)) == null) {
  210. processors.put(script, sp = createProcessor(script));
  211. }
  212. return sp;
  213. }
  214. // [TBD] - rework to provide more configurable binding between script name and script processor constructor
  215. private static ScriptProcessor createProcessor(String script) {
  216. ScriptProcessor sp = null;
  217. int sc = CharScript.scriptCodeFromTag(script);
  218. if (sc == CharScript.SCRIPT_ARABIC) {
  219. sp = new ArabicScriptProcessor(script);
  220. } else if (CharScript.isIndicScript(sc)) {
  221. sp = IndicScriptProcessor.makeProcessor(script);
  222. } else {
  223. sp = new DefaultScriptProcessor(script);
  224. }
  225. return sp;
  226. }
  227. private static class AssembledLookupsKey {
  228. private final GlyphTable table;
  229. private final String[] features;
  230. private final Map<GlyphTable.LookupSpec, List<GlyphTable.LookupTable>> lookups;
  231. AssembledLookupsKey(GlyphTable table, String[] features, Map<GlyphTable.LookupSpec, List<GlyphTable.LookupTable>> lookups) {
  232. this.table = table;
  233. this.features = features;
  234. this.lookups = lookups;
  235. }
  236. /** {@inheritDoc} */
  237. public int hashCode() {
  238. int hc = 0;
  239. hc = 7 * hc + (hc ^ table.hashCode());
  240. hc = 11 * hc + (hc ^ Arrays.hashCode(features));
  241. hc = 17 * hc + (hc ^ lookups.hashCode());
  242. return hc;
  243. }
  244. /** {@inheritDoc} */
  245. public boolean equals(Object o) {
  246. if (o instanceof AssembledLookupsKey) {
  247. AssembledLookupsKey k = (AssembledLookupsKey) o;
  248. if (!table.equals(k.table)) {
  249. return false;
  250. } else if (!Arrays.equals(features, k.features)) {
  251. return false;
  252. } else {
  253. return lookups.equals(k.lookups);
  254. }
  255. } else {
  256. return false;
  257. }
  258. }
  259. }
  260. public CharSequence preProcess(CharSequence charSequence, MultiByteFont font, List associations) {
  261. return charSequence;
  262. }
  263. }