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.

IndicScriptProcessor.java 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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.lang.reflect.Constructor;
  20. import java.lang.reflect.InvocationTargetException;
  21. import java.util.Collections;
  22. import java.util.HashMap;
  23. import java.util.HashSet;
  24. import java.util.Map;
  25. import java.util.Set;
  26. import java.util.Vector;
  27. import org.apache.commons.logging.Log;
  28. import org.apache.commons.logging.LogFactory;
  29. import org.apache.fop.complexscripts.fonts.GlyphTable;
  30. import org.apache.fop.complexscripts.util.CharAssociation;
  31. import org.apache.fop.complexscripts.util.CharScript;
  32. import org.apache.fop.complexscripts.util.GlyphContextTester;
  33. import org.apache.fop.complexscripts.util.GlyphSequence;
  34. import org.apache.fop.complexscripts.util.ScriptContextTester;
  35. // CSOFF: LineLengthCheck
  36. /**
  37. * <p>The <code>IndicScriptProcessor</code> class implements a script processor for
  38. * performing glyph substitution and positioning operations on content associated with the Indic script.</p>
  39. *
  40. * <p>This work was originally authored by Glenn Adams (gadams@apache.org).</p>
  41. */
  42. public class IndicScriptProcessor extends DefaultScriptProcessor {
  43. /** logging instance */
  44. private static final Log log = LogFactory.getLog(IndicScriptProcessor.class);
  45. /** required features to use for substitutions */
  46. private static final String[] GSUB_REQ_FEATURES =
  47. {
  48. "abvf", // above base forms
  49. "abvs", // above base substitutions
  50. "akhn", // akhand
  51. "blwf", // below base forms
  52. "blws", // below base substitutions
  53. "ccmp", // glyph composition/decomposition
  54. "cjct", // conjunct forms
  55. "clig", // contextual ligatures
  56. "half", // half forms
  57. "haln", // halant forms
  58. "locl", // localized forms
  59. "nukt", // nukta forms
  60. "pref", // pre-base forms
  61. "pres", // pre-base substitutions
  62. "pstf", // post-base forms
  63. "psts", // post-base substitutions
  64. "rkrf", // rakar forms
  65. "rphf", // reph form
  66. "vatu" // vattu variants
  67. };
  68. /** optional features to use for substitutions */
  69. private static final String[] GSUB_OPT_FEATURES =
  70. {
  71. "afrc", // alternative fractions
  72. "calt", // contextual alternatives
  73. "dlig" // discretionary ligatures
  74. };
  75. /** required features to use for positioning */
  76. private static final String[] GPOS_REQ_FEATURES =
  77. {
  78. "abvm", // above base marks
  79. "blwm", // below base marks
  80. "dist", // distance (adjustment)
  81. "kern" // kerning
  82. };
  83. /** required features to use for positioning */
  84. private static final String[] GPOS_OPT_FEATURES =
  85. {
  86. };
  87. private static class SubstitutionScriptContextTester implements ScriptContextTester {
  88. private static Map<String, GlyphContextTester> testerMap = new HashMap<String, GlyphContextTester>();
  89. public GlyphContextTester getTester(String feature) {
  90. return testerMap.get(feature);
  91. }
  92. }
  93. private static class PositioningScriptContextTester implements ScriptContextTester {
  94. private static Map<String, GlyphContextTester> testerMap = new HashMap<String, GlyphContextTester>();
  95. public GlyphContextTester getTester(String feature) {
  96. return testerMap.get(feature);
  97. }
  98. }
  99. /**
  100. * Make script specific flavor of Indic script processor.
  101. * @param script tag
  102. * @return script processor instance
  103. */
  104. public static ScriptProcessor makeProcessor(String script) {
  105. switch (CharScript.scriptCodeFromTag(script)) {
  106. case CharScript.SCRIPT_DEVANAGARI:
  107. case CharScript.SCRIPT_DEVANAGARI_2:
  108. return new DevanagariScriptProcessor(script);
  109. case CharScript.SCRIPT_GUJARATI:
  110. case CharScript.SCRIPT_GUJARATI_2:
  111. return new GujaratiScriptProcessor(script);
  112. case CharScript.SCRIPT_GURMUKHI:
  113. case CharScript.SCRIPT_GURMUKHI_2:
  114. return new GurmukhiScriptProcessor(script);
  115. case CharScript.SCRIPT_TAMIL:
  116. case CharScript.SCRIPT_TAMIL_2:
  117. return new TamilScriptProcessor(script);
  118. case CharScript.SCRIPT_KHMER:
  119. return new KhmerScriptProcessor(script);
  120. // [TBD] implement other script processors
  121. default:
  122. return new IndicScriptProcessor(script);
  123. }
  124. }
  125. private final ScriptContextTester subContextTester;
  126. private final ScriptContextTester posContextTester;
  127. IndicScriptProcessor(String script) {
  128. super(script);
  129. this.subContextTester = new SubstitutionScriptContextTester();
  130. this.posContextTester = new PositioningScriptContextTester();
  131. }
  132. /** {@inheritDoc} */
  133. public String[] getSubstitutionFeatures() {
  134. return GSUB_REQ_FEATURES;
  135. }
  136. /** {@inheritDoc} */
  137. public String[] getOptionalSubstitutionFeatures() {
  138. return GSUB_OPT_FEATURES;
  139. }
  140. /** {@inheritDoc} */
  141. public ScriptContextTester getSubstitutionContextTester() {
  142. return subContextTester;
  143. }
  144. /** {@inheritDoc} */
  145. public String[] getPositioningFeatures() {
  146. return GPOS_REQ_FEATURES;
  147. }
  148. /** {@inheritDoc} */
  149. public String[] getOptionalPositioningFeatures() {
  150. return GPOS_OPT_FEATURES;
  151. }
  152. /** {@inheritDoc} */
  153. public ScriptContextTester getPositioningContextTester() {
  154. return posContextTester;
  155. }
  156. /** {@inheritDoc} */
  157. @Override
  158. public GlyphSequence substitute(GlyphSequence gs, String script, String language, GlyphTable.UseSpec[] usa, ScriptContextTester sct) {
  159. assert usa != null;
  160. // 1. syllabize
  161. GlyphSequence[] sa = syllabize(gs, script, language);
  162. // 2. process each syllable
  163. for (int i = 0, n = sa.length; i < n; i++) {
  164. GlyphSequence s = sa [ i ];
  165. // apply basic shaping subs
  166. for (GlyphTable.UseSpec us : usa) {
  167. if (isBasicShapingUse(us)) {
  168. s.setPredications(true);
  169. s = us.substitute(s, script, language, sct);
  170. }
  171. }
  172. // reorder pre-base matra
  173. s = reorderPreBaseMatra(s);
  174. // reorder reph
  175. s = reorderReph(s);
  176. // apply presentation subs
  177. for (GlyphTable.UseSpec us : usa) {
  178. if (isPresentationUse(us)) {
  179. s.setPredications(true);
  180. s = us.substitute(s, script, language, sct);
  181. }
  182. }
  183. // record result
  184. sa [ i ] = s;
  185. }
  186. // 3. return reassembled substituted syllables
  187. return unsyllabize(gs, sa);
  188. }
  189. /**
  190. * Get script specific syllabizer class.
  191. * @return a syllabizer class object or null
  192. */
  193. protected Class<? extends Syllabizer> getSyllabizerClass() {
  194. return null;
  195. }
  196. private GlyphSequence[] syllabize(GlyphSequence gs, String script, String language) {
  197. return Syllabizer.getSyllabizer(script, language, getSyllabizerClass()).syllabize(gs);
  198. }
  199. private GlyphSequence unsyllabize(GlyphSequence gs, GlyphSequence[] sa) {
  200. return GlyphSequence.join(gs, sa);
  201. }
  202. private static Set<String> basicShapingFeatures;
  203. private static final String[] BASIC_SHAPING_FEATURE_STRINGS = {
  204. "abvf",
  205. "akhn",
  206. "blwf",
  207. "cjct",
  208. "half",
  209. "locl",
  210. "nukt",
  211. "pref",
  212. "pstf",
  213. "rkrf",
  214. "rphf",
  215. "vatu",
  216. "ccmp"
  217. };
  218. static {
  219. basicShapingFeatures = new HashSet<String>();
  220. Collections.addAll(basicShapingFeatures, BASIC_SHAPING_FEATURE_STRINGS);
  221. }
  222. private boolean isBasicShapingUse(GlyphTable.UseSpec us) {
  223. assert us != null;
  224. if (basicShapingFeatures != null) {
  225. return basicShapingFeatures.contains(us.getFeature());
  226. } else {
  227. return false;
  228. }
  229. }
  230. private static Set<String> presentationFeatures;
  231. private static final String[] PRESENTATION_FEATURE_STRINGS = {
  232. "abvs",
  233. "blws",
  234. "calt",
  235. "haln",
  236. "pres",
  237. "psts",
  238. "clig"
  239. };
  240. static {
  241. presentationFeatures = new HashSet<String>();
  242. Collections.addAll(presentationFeatures, PRESENTATION_FEATURE_STRINGS);
  243. }
  244. private boolean isPresentationUse(GlyphTable.UseSpec us) {
  245. assert us != null;
  246. if (presentationFeatures != null) {
  247. return presentationFeatures.contains(us.getFeature());
  248. } else {
  249. return false;
  250. }
  251. }
  252. private GlyphSequence reorderPreBaseMatra(GlyphSequence gs) {
  253. int source;
  254. if ((source = findPreBaseMatra(gs)) >= 0) {
  255. int target;
  256. if ((target = findPreBaseMatraTarget(gs, source)) >= 0) {
  257. if (target != source) {
  258. gs = reorder(gs, source, target);
  259. }
  260. }
  261. }
  262. return gs;
  263. }
  264. /**
  265. * Find pre-base matra in sequence.
  266. * @param gs input sequence
  267. * @return index of pre-base matra or -1 if not found
  268. */
  269. protected int findPreBaseMatra(GlyphSequence gs) {
  270. return -1;
  271. }
  272. /**
  273. * Find pre-base matra target in sequence.
  274. * @param gs input sequence
  275. * @param source index of pre-base matra
  276. * @return index of pre-base matra target or -1
  277. */
  278. protected int findPreBaseMatraTarget(GlyphSequence gs, int source) {
  279. return -1;
  280. }
  281. private GlyphSequence reorderReph(GlyphSequence gs) {
  282. int source;
  283. if ((source = findReph(gs)) >= 0) {
  284. int target;
  285. if ((target = findRephTarget(gs, source)) >= 0) {
  286. if (target != source) {
  287. gs = reorder(gs, source, target);
  288. }
  289. }
  290. }
  291. return gs;
  292. }
  293. /**
  294. * Find reph in sequence.
  295. * @param gs input sequence
  296. * @return index of reph or -1 if not found
  297. */
  298. protected int findReph(GlyphSequence gs) {
  299. return -1;
  300. }
  301. /**
  302. * Find reph target in sequence.
  303. * @param gs input sequence
  304. * @param source index of reph
  305. * @return index of reph target or -1
  306. */
  307. protected int findRephTarget(GlyphSequence gs, int source) {
  308. return -1;
  309. }
  310. private GlyphSequence reorder(GlyphSequence gs, int source, int target) {
  311. return GlyphSequence.reorder(gs, source, 1, target);
  312. }
  313. /** {@inheritDoc} */
  314. @Override
  315. public boolean position(GlyphSequence gs, String script, String language, int fontSize, GlyphTable.UseSpec[] usa, int[] widths, int[][] adjustments, ScriptContextTester sct) {
  316. boolean adjusted = super.position(gs, script, language, fontSize, usa, widths, adjustments, sct);
  317. return adjusted;
  318. }
  319. /** Abstract syllabizer. */
  320. protected abstract static class Syllabizer implements Comparable {
  321. private String script;
  322. private String language;
  323. Syllabizer(String script, String language) {
  324. this.script = script;
  325. this.language = language;
  326. }
  327. /**
  328. * Subdivide glyph sequence GS into syllabic segments each represented by a distinct
  329. * output glyph sequence.
  330. * @param gs input glyph sequence
  331. * @return segmented syllabic glyph sequences
  332. */
  333. abstract GlyphSequence[] syllabize(GlyphSequence gs);
  334. /** {@inheritDoc} */
  335. public int hashCode() {
  336. int hc = 0;
  337. hc = 7 * hc + (hc ^ script.hashCode());
  338. hc = 11 * hc + (hc ^ language.hashCode());
  339. return hc;
  340. }
  341. /** {@inheritDoc} */
  342. public boolean equals(Object o) {
  343. if (o instanceof Syllabizer) {
  344. Syllabizer s = (Syllabizer) o;
  345. if (!s.script.equals(script)) {
  346. return false;
  347. } else {
  348. return s.language.equals(language);
  349. }
  350. } else {
  351. return false;
  352. }
  353. }
  354. /** {@inheritDoc} */
  355. public int compareTo(Object o) {
  356. int d;
  357. if (o instanceof Syllabizer) {
  358. Syllabizer s = (Syllabizer) o;
  359. if ((d = script.compareTo(s.script)) == 0) {
  360. d = language.compareTo(s.language);
  361. }
  362. } else {
  363. d = -1;
  364. }
  365. return d;
  366. }
  367. private static Map<String, Syllabizer> syllabizers = new HashMap<String, Syllabizer>();
  368. static Syllabizer getSyllabizer(String script, String language, Class<? extends Syllabizer> syllabizerClass) {
  369. String sid = makeSyllabizerId(script, language);
  370. Syllabizer s = syllabizers.get(sid);
  371. if (s == null) {
  372. if ((syllabizerClass == null) || ((s = makeSyllabizer(script, language, syllabizerClass)) == null)) {
  373. log.warn("No syllabizer available for script '" + script + "', language '" + language + "', using default Indic syllabizer.");
  374. s = new DefaultSyllabizer(script, language);
  375. }
  376. syllabizers.put(sid, s);
  377. }
  378. return s;
  379. }
  380. static String makeSyllabizerId(String script, String language) {
  381. return script + ":" + language;
  382. }
  383. static Syllabizer makeSyllabizer(String script, String language, Class<? extends Syllabizer> syllabizerClass) {
  384. Syllabizer s;
  385. try {
  386. Constructor<? extends Syllabizer> cf = syllabizerClass.getDeclaredConstructor(new Class[] { String.class, String.class });
  387. s = (Syllabizer) cf.newInstance(script, language);
  388. } catch (NoSuchMethodException e) {
  389. s = null;
  390. } catch (InstantiationException e) {
  391. s = null;
  392. } catch (IllegalAccessException e) {
  393. s = null;
  394. } catch (InvocationTargetException e) {
  395. s = null;
  396. }
  397. return s;
  398. }
  399. }
  400. /** Default syllabizer. */
  401. protected static class DefaultSyllabizer extends Syllabizer {
  402. DefaultSyllabizer(String script, String language) {
  403. super(script, language);
  404. }
  405. /** {@inheritDoc} */
  406. @Override
  407. GlyphSequence[] syllabize(GlyphSequence gs) {
  408. int[] ca = gs.getCharacterArray(false);
  409. int nc = gs.getCharacterCount();
  410. if (nc == 0) {
  411. return new GlyphSequence[] { gs };
  412. } else {
  413. return segmentize(gs, segmentize(ca, nc));
  414. }
  415. }
  416. /**
  417. * Construct array of segements from original character array (associated with original glyph sequence)
  418. * @param ca input character sequence
  419. * @param nc number of characters in sequence
  420. * @return array of syllable segments
  421. */
  422. protected Segment[] segmentize(int[] ca, int nc) {
  423. Vector<Segment> sv = new Vector<Segment>(nc);
  424. for (int s = 0, e = nc; s < e; ) {
  425. int i;
  426. if ((i = findStartOfSyllable(ca, s, e)) < e) {
  427. if (s < i) {
  428. // from s to i is non-syllable segment
  429. sv.add(new Segment(s, i, Segment.OTHER));
  430. }
  431. s = i; // move s to start of syllable
  432. } else {
  433. if (s < e) {
  434. // from s to e is non-syllable segment
  435. sv.add(new Segment(s, e, Segment.OTHER));
  436. }
  437. s = e; // move s to end of input sequence
  438. }
  439. if ((i = findEndOfSyllable(ca, s, e)) > s) {
  440. if (s < i) {
  441. // from s to i is syllable segment
  442. sv.add(new Segment(s, i, Segment.SYLLABLE));
  443. }
  444. s = i; // move s to end of syllable
  445. } else {
  446. if (s < e) {
  447. // from s to e is non-syllable segment
  448. sv.add(new Segment(s, e, Segment.OTHER));
  449. }
  450. s = e; // move s to end of input sequence
  451. }
  452. }
  453. return sv.toArray(new Segment [ sv.size() ]);
  454. }
  455. /**
  456. * Construct array of glyph sequences from original glyph sequence and segment array.
  457. * @param gs original input glyph sequence
  458. * @param sa segment array
  459. * @return array of glyph sequences each belonging to an (ordered) segment in SA
  460. */
  461. protected GlyphSequence[] segmentize(GlyphSequence gs, Segment[] sa) {
  462. int ng = gs.getGlyphCount();
  463. int[] ga = gs.getGlyphArray(false);
  464. CharAssociation[] aa = gs.getAssociations(0, -1);
  465. Vector<GlyphSequence> nsv = new Vector<GlyphSequence>();
  466. for (Segment s : sa) {
  467. Vector<Integer> ngv = new Vector<Integer>(ng);
  468. Vector<CharAssociation> nav = new Vector<CharAssociation>(ng);
  469. for (int j = 0; j < ng; j++) {
  470. CharAssociation ca = aa[j];
  471. if (ca.contained(s.getOffset(), s.getCount())) {
  472. ngv.add(ga[j]);
  473. nav.add(ca);
  474. }
  475. }
  476. if (ngv.size() > 0) {
  477. nsv.add(new GlyphSequence(gs, null, toIntArray(ngv), null, null, nav.toArray(new CharAssociation[nav.size()]), null));
  478. }
  479. }
  480. if (nsv.size() > 0) {
  481. return nsv.toArray(new GlyphSequence [ nsv.size() ]);
  482. } else {
  483. return new GlyphSequence[] { gs };
  484. }
  485. }
  486. /**
  487. * Find start of syllable in character array, starting at S, ending at E.
  488. * @param ca character array
  489. * @param s start index
  490. * @param e end index
  491. * @return index of start or E if no start found
  492. */
  493. protected int findStartOfSyllable(int[] ca, int s, int e) {
  494. return e;
  495. }
  496. /**
  497. * Find end of syllable in character array, starting at S, ending at E.
  498. * @param ca character array
  499. * @param s start index
  500. * @param e end index
  501. * @return index of start or S if no end found
  502. */
  503. protected int findEndOfSyllable(int[] ca, int s, int e) {
  504. return s;
  505. }
  506. private static int[] toIntArray(Vector<Integer> iv) {
  507. int ni = iv.size();
  508. int[] ia = new int [ iv.size() ];
  509. for (int i = 0, n = ni; i < n; i++) {
  510. ia [ i ] = (int) iv.get(i);
  511. }
  512. return ia;
  513. }
  514. }
  515. /** Syllabic segment. */
  516. protected static class Segment {
  517. static final int OTHER = 0; // other (non-syllable) characters
  518. static final int SYLLABLE = 1; // (orthographic) syllable
  519. private int start;
  520. private int end;
  521. private int type;
  522. Segment(int start, int end, int type) {
  523. this.start = start;
  524. this.end = end;
  525. this.type = type;
  526. }
  527. int getStart() {
  528. return start;
  529. }
  530. int getEnd() {
  531. return end;
  532. }
  533. int getOffset() {
  534. return start;
  535. }
  536. int getCount() {
  537. return end - start;
  538. }
  539. int getType() {
  540. return type;
  541. }
  542. }
  543. }