Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

IndicScriptProcessor.java 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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 (GlyphContextTester) 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 (GlyphContextTester) 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. // [TBD] implement other script processors
  119. default:
  120. return new IndicScriptProcessor(script);
  121. }
  122. }
  123. private final ScriptContextTester subContextTester;
  124. private final ScriptContextTester posContextTester;
  125. IndicScriptProcessor(String script) {
  126. super(script);
  127. this.subContextTester = new SubstitutionScriptContextTester();
  128. this.posContextTester = new PositioningScriptContextTester();
  129. }
  130. /** {@inheritDoc} */
  131. public String[] getSubstitutionFeatures() {
  132. return GSUB_REQ_FEATURES;
  133. }
  134. /** {@inheritDoc} */
  135. public String[] getOptionalSubstitutionFeatures() {
  136. return GSUB_OPT_FEATURES;
  137. }
  138. /** {@inheritDoc} */
  139. public ScriptContextTester getSubstitutionContextTester() {
  140. return subContextTester;
  141. }
  142. /** {@inheritDoc} */
  143. public String[] getPositioningFeatures() {
  144. return GPOS_REQ_FEATURES;
  145. }
  146. /** {@inheritDoc} */
  147. public String[] getOptionalPositioningFeatures() {
  148. return GPOS_OPT_FEATURES;
  149. }
  150. /** {@inheritDoc} */
  151. public ScriptContextTester getPositioningContextTester() {
  152. return posContextTester;
  153. }
  154. /** {@inheritDoc} */
  155. @Override
  156. public GlyphSequence substitute(GlyphSequence gs, String script, String language, GlyphTable.UseSpec[] usa, ScriptContextTester sct) {
  157. assert usa != null;
  158. // 1. syllabize
  159. GlyphSequence[] sa = syllabize(gs, script, language);
  160. // 2. process each syllable
  161. for (int i = 0, n = sa.length; i < n; i++) {
  162. GlyphSequence s = sa [ i ];
  163. // apply basic shaping subs
  164. for (GlyphTable.UseSpec us : usa) {
  165. if (isBasicShapingUse(us)) {
  166. s.setPredications(true);
  167. s = us.substitute(s, script, language, sct);
  168. }
  169. }
  170. // reorder pre-base matra
  171. s = reorderPreBaseMatra(s);
  172. // reorder reph
  173. s = reorderReph(s);
  174. // apply presentation subs
  175. for (GlyphTable.UseSpec us : usa) {
  176. if (isPresentationUse(us)) {
  177. s.setPredications(true);
  178. s = us.substitute(s, script, language, sct);
  179. }
  180. }
  181. // record result
  182. sa [ i ] = s;
  183. }
  184. // 3. return reassembled substituted syllables
  185. return unsyllabize(gs, sa);
  186. }
  187. /**
  188. * Get script specific syllabizer class.
  189. * @return a syllabizer class object or null
  190. */
  191. protected Class<? extends Syllabizer> getSyllabizerClass() {
  192. return null;
  193. }
  194. private GlyphSequence[] syllabize(GlyphSequence gs, String script, String language) {
  195. return Syllabizer.getSyllabizer(script, language, getSyllabizerClass()).syllabize(gs);
  196. }
  197. private GlyphSequence unsyllabize(GlyphSequence gs, GlyphSequence[] sa) {
  198. return GlyphSequence.join(gs, sa);
  199. }
  200. private static Set<String> basicShapingFeatures;
  201. private static final String[] BASIC_SHAPING_FEATURE_STRINGS = {
  202. "abvf",
  203. "akhn",
  204. "blwf",
  205. "cjct",
  206. "half",
  207. "locl",
  208. "nukt",
  209. "pref",
  210. "pstf",
  211. "rkrf",
  212. "rphf",
  213. "vatu",
  214. };
  215. static {
  216. basicShapingFeatures = new HashSet<String>();
  217. Collections.addAll(basicShapingFeatures, BASIC_SHAPING_FEATURE_STRINGS);
  218. }
  219. private boolean isBasicShapingUse(GlyphTable.UseSpec us) {
  220. assert us != null;
  221. if (basicShapingFeatures != null) {
  222. return basicShapingFeatures.contains(us.getFeature());
  223. } else {
  224. return false;
  225. }
  226. }
  227. private static Set<String> presentationFeatures;
  228. private static final String[] PRESENTATION_FEATURE_STRINGS = {
  229. "abvs",
  230. "blws",
  231. "calt",
  232. "haln",
  233. "pres",
  234. "psts",
  235. };
  236. static {
  237. presentationFeatures = new HashSet<String>();
  238. Collections.addAll(presentationFeatures, PRESENTATION_FEATURE_STRINGS);
  239. }
  240. private boolean isPresentationUse(GlyphTable.UseSpec us) {
  241. assert us != null;
  242. if (presentationFeatures != null) {
  243. return presentationFeatures.contains(us.getFeature());
  244. } else {
  245. return false;
  246. }
  247. }
  248. private GlyphSequence reorderPreBaseMatra(GlyphSequence gs) {
  249. int source;
  250. if ((source = findPreBaseMatra(gs)) >= 0) {
  251. int target;
  252. if ((target = findPreBaseMatraTarget(gs, source)) >= 0) {
  253. if (target != source) {
  254. gs = reorder(gs, source, target);
  255. }
  256. }
  257. }
  258. return gs;
  259. }
  260. /**
  261. * Find pre-base matra in sequence.
  262. * @param gs input sequence
  263. * @return index of pre-base matra or -1 if not found
  264. */
  265. protected int findPreBaseMatra(GlyphSequence gs) {
  266. return -1;
  267. }
  268. /**
  269. * Find pre-base matra target in sequence.
  270. * @param gs input sequence
  271. * @param source index of pre-base matra
  272. * @return index of pre-base matra target or -1
  273. */
  274. protected int findPreBaseMatraTarget(GlyphSequence gs, int source) {
  275. return -1;
  276. }
  277. private GlyphSequence reorderReph(GlyphSequence gs) {
  278. int source;
  279. if ((source = findReph(gs)) >= 0) {
  280. int target;
  281. if ((target = findRephTarget(gs, source)) >= 0) {
  282. if (target != source) {
  283. gs = reorder(gs, source, target);
  284. }
  285. }
  286. }
  287. return gs;
  288. }
  289. /**
  290. * Find reph in sequence.
  291. * @param gs input sequence
  292. * @return index of reph or -1 if not found
  293. */
  294. protected int findReph(GlyphSequence gs) {
  295. return -1;
  296. }
  297. /**
  298. * Find reph target in sequence.
  299. * @param gs input sequence
  300. * @param source index of reph
  301. * @return index of reph target or -1
  302. */
  303. protected int findRephTarget(GlyphSequence gs, int source) {
  304. return -1;
  305. }
  306. private GlyphSequence reorder(GlyphSequence gs, int source, int target) {
  307. return GlyphSequence.reorder(gs, source, 1, target);
  308. }
  309. /** {@inheritDoc} */
  310. @Override
  311. public boolean position(GlyphSequence gs, String script, String language, int fontSize, GlyphTable.UseSpec[] usa, int[] widths, int[][] adjustments, ScriptContextTester sct) {
  312. boolean adjusted = super.position(gs, script, language, fontSize, usa, widths, adjustments, sct);
  313. return adjusted;
  314. }
  315. /** Abstract syllabizer. */
  316. protected abstract static class Syllabizer implements Comparable {
  317. private String script;
  318. private String language;
  319. Syllabizer(String script, String language) {
  320. this.script = script;
  321. this.language = language;
  322. }
  323. /**
  324. * Subdivide glyph sequence GS into syllabic segments each represented by a distinct
  325. * output glyph sequence.
  326. * @param gs input glyph sequence
  327. * @return segmented syllabic glyph sequences
  328. */
  329. abstract GlyphSequence[] syllabize(GlyphSequence gs);
  330. /** {@inheritDoc} */
  331. public int hashCode() {
  332. int hc = 0;
  333. hc = 7 * hc + (hc ^ script.hashCode());
  334. hc = 11 * hc + (hc ^ language.hashCode());
  335. return hc;
  336. }
  337. /** {@inheritDoc} */
  338. public boolean equals(Object o) {
  339. if (o instanceof Syllabizer) {
  340. Syllabizer s = (Syllabizer) o;
  341. if (!s.script.equals(script)) {
  342. return false;
  343. } else {
  344. return s.language.equals(language);
  345. }
  346. } else {
  347. return false;
  348. }
  349. }
  350. /** {@inheritDoc} */
  351. public int compareTo(Object o) {
  352. int d;
  353. if (o instanceof Syllabizer) {
  354. Syllabizer s = (Syllabizer) o;
  355. if ((d = script.compareTo(s.script)) == 0) {
  356. d = language.compareTo(s.language);
  357. }
  358. } else {
  359. d = -1;
  360. }
  361. return d;
  362. }
  363. private static Map<String, Syllabizer> syllabizers = new HashMap<String, Syllabizer>();
  364. static Syllabizer getSyllabizer(String script, String language, Class<? extends Syllabizer> syllabizerClass) {
  365. String sid = makeSyllabizerId(script, language);
  366. Syllabizer s = syllabizers.get(sid);
  367. if (s == null) {
  368. if ((syllabizerClass == null) || ((s = makeSyllabizer(script, language, syllabizerClass)) == null)) {
  369. log.warn("No syllabizer available for script '" + script + "', language '" + language + "', using default Indic syllabizer.");
  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)) < e) {
  423. if (s < i) {
  424. // from s to i is non-syllable segment
  425. sv.add(new Segment(s, i, Segment.OTHER));
  426. }
  427. s = i; // move s to start of syllable
  428. } else {
  429. if (s < e) {
  430. // from s to e is non-syllable segment
  431. sv.add(new Segment(s, e, Segment.OTHER));
  432. }
  433. s = e; // move s to end of input sequence
  434. }
  435. if ((i = findEndOfSyllable(ca, s, e)) > s) {
  436. if (s < i) {
  437. // from s to i is syllable segment
  438. sv.add(new Segment(s, i, Segment.SYLLABLE));
  439. }
  440. s = i; // move s to end of syllable
  441. } else {
  442. if (s < e) {
  443. // from s to e is non-syllable segment
  444. sv.add(new Segment(s, e, Segment.OTHER));
  445. }
  446. s = e; // move s to end of input sequence
  447. }
  448. }
  449. return sv.toArray(new Segment [ sv.size() ]);
  450. }
  451. /**
  452. * Construct array of glyph sequences from original glyph sequence and segment array.
  453. * @param gs original input glyph sequence
  454. * @param sa segment array
  455. * @return array of glyph sequences each belonging to an (ordered) segment in SA
  456. */
  457. protected GlyphSequence[] segmentize(GlyphSequence gs, Segment[] sa) {
  458. int ng = gs.getGlyphCount();
  459. int[] ga = gs.getGlyphArray(false);
  460. CharAssociation[] aa = gs.getAssociations(0, -1);
  461. Vector<GlyphSequence> nsv = new Vector<GlyphSequence>();
  462. for (Segment s : sa) {
  463. Vector<Integer> ngv = new Vector<Integer>(ng);
  464. Vector<CharAssociation> nav = new Vector<CharAssociation>(ng);
  465. for (int j = 0; j < ng; j++) {
  466. CharAssociation ca = aa[j];
  467. if (ca.contained(s.getOffset(), s.getCount())) {
  468. ngv.add(ga[j]);
  469. nav.add(ca);
  470. }
  471. }
  472. if (ngv.size() > 0) {
  473. nsv.add(new GlyphSequence(gs, null, toIntArray(ngv), null, null, nav.toArray(new CharAssociation[nav.size()]), null));
  474. }
  475. }
  476. if (nsv.size() > 0) {
  477. return nsv.toArray(new GlyphSequence [ nsv.size() ]);
  478. } else {
  479. return new GlyphSequence[] { gs };
  480. }
  481. }
  482. /**
  483. * Find start of syllable in character array, starting at S, ending at E.
  484. * @param ca character array
  485. * @param s start index
  486. * @param e end index
  487. * @return index of start or E if no start found
  488. */
  489. protected int findStartOfSyllable(int[] ca, int s, int e) {
  490. return e;
  491. }
  492. /**
  493. * Find end of syllable in character array, starting at S, ending at E.
  494. * @param ca character array
  495. * @param s start index
  496. * @param e end index
  497. * @return index of start or S if no end found
  498. */
  499. protected int findEndOfSyllable(int[] ca, int s, int e) {
  500. return s;
  501. }
  502. private static int[] toIntArray(Vector<Integer> iv) {
  503. int ni = iv.size();
  504. int[] ia = new int [ iv.size() ];
  505. for (int i = 0, n = ni; i < n; i++) {
  506. ia [ i ] = (int) iv.get(i);
  507. }
  508. return ia;
  509. }
  510. }
  511. /** Syllabic segment. */
  512. protected static class Segment {
  513. static final int OTHER = 0; // other (non-syllable) characters
  514. static final int SYLLABLE = 1; // (orthographic) syllable
  515. private int start;
  516. private int end;
  517. private int type;
  518. Segment(int start, int end, int type) {
  519. this.start = start;
  520. this.end = end;
  521. this.type = type;
  522. }
  523. int getStart() {
  524. return start;
  525. }
  526. int getEnd() {
  527. return end;
  528. }
  529. int getOffset() {
  530. return start;
  531. }
  532. int getCount() {
  533. return end - start;
  534. }
  535. int getType() {
  536. return type;
  537. }
  538. }
  539. }