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

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