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.

GlyphSubstitutionState.java 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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.fonts;
  19. import java.nio.IntBuffer;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. import org.apache.fop.complexscripts.util.GlyphSequence;
  23. import org.apache.fop.complexscripts.util.ScriptContextTester;
  24. // CSOFF: LineLengthCheck
  25. /**
  26. * <p>The <code>GlyphSubstitutionState</code> implements an state object used during glyph substitution
  27. * processing.</p>
  28. *
  29. * <p>This work was originally authored by Glenn Adams (gadams@apache.org).</p>
  30. */
  31. public class GlyphSubstitutionState extends GlyphProcessingState {
  32. /** alternates index */
  33. private int[] alternatesIndex;
  34. /** current output glyph sequence */
  35. private IntBuffer ogb;
  36. /** current output glyph to character associations */
  37. private List oal;
  38. /** character association predications */
  39. private boolean predications;
  40. /**
  41. * Construct default (reset) glyph substitution state.
  42. */
  43. public GlyphSubstitutionState() {
  44. }
  45. /**
  46. * Construct glyph substitution state.
  47. * @param gs input glyph sequence
  48. * @param script script identifier
  49. * @param language language identifier
  50. * @param feature feature identifier
  51. * @param sct script context tester (or null)
  52. */
  53. public GlyphSubstitutionState(GlyphSequence gs, String script, String language, String feature, ScriptContextTester sct) {
  54. super(gs, script, language, feature, sct);
  55. this.ogb = IntBuffer.allocate(gs.getGlyphCount());
  56. this.oal = new ArrayList(gs.getGlyphCount());
  57. this.predications = gs.getPredications();
  58. }
  59. /**
  60. * Construct glyph substitution state using an existing state object using shallow copy
  61. * except as follows: input glyph sequence is copied deep except for its characters array.
  62. * @param ss existing positioning state to copy from
  63. */
  64. public GlyphSubstitutionState(GlyphSubstitutionState ss) {
  65. super(ss);
  66. this.ogb = IntBuffer.allocate(indexLast);
  67. this.oal = new ArrayList(indexLast);
  68. }
  69. /**
  70. * Reset glyph substitution state.
  71. * @param gs input glyph sequence
  72. * @param script script identifier
  73. * @param language language identifier
  74. * @param feature feature identifier
  75. * @param sct script context tester (or null)
  76. */
  77. public GlyphSubstitutionState reset(GlyphSequence gs, String script, String language, String feature, ScriptContextTester sct) {
  78. super.reset(gs, script, language, feature, sct);
  79. this.alternatesIndex = null;
  80. this.ogb = IntBuffer.allocate(gs.getGlyphCount());
  81. this.oal = new ArrayList(gs.getGlyphCount());
  82. this.predications = gs.getPredications();
  83. return this;
  84. }
  85. /**
  86. * Set alternates indices.
  87. * @param alternates array of alternates indices ordered by coverage index
  88. */
  89. public void setAlternates(int[] alternates) {
  90. this.alternatesIndex = alternates;
  91. }
  92. /**
  93. * Obtain alternates index associated with specified coverage index. An alternates
  94. * index is used to select among stylistic alternates of a glyph at a particular
  95. * coverage index. This information must be provided by the document itself (in the
  96. * form of an extension attribute value), since a font has no way to determine which
  97. * alternate the user desires.
  98. * @param ci coverage index
  99. * @return an alternates index
  100. */
  101. public int getAlternatesIndex(int ci) {
  102. if (alternatesIndex == null) {
  103. return 0;
  104. } else if ((ci < 0) || (ci > alternatesIndex.length)) {
  105. return 0;
  106. } else {
  107. return alternatesIndex [ ci ];
  108. }
  109. }
  110. /**
  111. * Put (write) glyph into glyph output buffer.
  112. * @param glyph to write
  113. * @param a character association that applies to glyph
  114. * @param predication a predication value to add to association A if predications enabled
  115. */
  116. public void putGlyph(int glyph, GlyphSequence.CharAssociation a, Object predication) {
  117. if (!ogb.hasRemaining()) {
  118. ogb = growBuffer(ogb);
  119. }
  120. ogb.put(glyph);
  121. if (predications && (predication != null)) {
  122. a.setPredication(feature, predication);
  123. }
  124. oal.add(a);
  125. }
  126. /**
  127. * Put (write) array of glyphs into glyph output buffer.
  128. * @param glyphs to write
  129. * @param associations array of character associations that apply to glyphs
  130. * @param predication optional predicaion object to be associated with glyphs' associations
  131. */
  132. public void putGlyphs(int[] glyphs, GlyphSequence.CharAssociation[] associations, Object predication) {
  133. assert glyphs != null;
  134. assert associations != null;
  135. assert associations.length >= glyphs.length;
  136. for (int i = 0, n = glyphs.length; i < n; i++) {
  137. putGlyph(glyphs [ i ], associations [ i ], predication);
  138. }
  139. }
  140. /**
  141. * Obtain output glyph sequence.
  142. * @return newly constructed glyph sequence comprised of original
  143. * characters, output glyphs, and output associations
  144. */
  145. public GlyphSequence getOutput() {
  146. int position = ogb.position();
  147. if (position > 0) {
  148. ogb.limit(position);
  149. ogb.rewind();
  150. return new GlyphSequence(igs.getCharacters(), ogb, oal);
  151. } else {
  152. return igs;
  153. }
  154. }
  155. /**
  156. * Apply substitution subtable to current state at current position (only),
  157. * resulting in the consumption of zero or more input glyphs, and possibly
  158. * replacing the current input glyphs starting at the current position, in
  159. * which case it is possible that indexLast is altered to be either less than
  160. * or greater than its value prior to this application.
  161. * @param st the glyph substitution subtable to apply
  162. * @return true if subtable applied, or false if it did not (e.g., its
  163. * input coverage table did not match current input context)
  164. */
  165. public boolean apply(GlyphSubstitutionSubtable st) {
  166. assert st != null;
  167. updateSubtableState(st);
  168. boolean applied = st.substitute(this);
  169. return applied;
  170. }
  171. /**
  172. * Apply a sequence of matched rule lookups to the <code>nig</code> input glyphs
  173. * starting at the current position. If lookups are non-null and non-empty, then
  174. * all input glyphs specified by <code>nig</code> are consumed irregardless of
  175. * whether any specified lookup applied.
  176. * @param lookups array of matched lookups (or null)
  177. * @param nig number of glyphs in input sequence, starting at current position, to which
  178. * the lookups are to apply, and to be consumed once the application has finished
  179. * @return true if lookups are non-null and non-empty; otherwise, false
  180. */
  181. public boolean apply(GlyphTable.RuleLookup[] lookups, int nig) {
  182. // int nbg = index;
  183. int nlg = indexLast - (index + nig);
  184. int nog = 0;
  185. if ((lookups != null) && (lookups.length > 0)) {
  186. // apply each rule lookup to extracted input glyph array
  187. for (int i = 0, n = lookups.length; i < n; i++) {
  188. GlyphTable.RuleLookup l = lookups [ i ];
  189. if (l != null) {
  190. GlyphTable.LookupTable lt = l.getLookup();
  191. if (lt != null) {
  192. // perform substitution on a copy of previous state
  193. GlyphSubstitutionState ss = new GlyphSubstitutionState(this);
  194. // apply lookup table substitutions
  195. GlyphSequence gs = lt.substitute(ss, l.getSequenceIndex());
  196. // replace current input sequence starting at current position with result
  197. if (replaceInput(0, -1, gs)) {
  198. nog = gs.getGlyphCount() - nlg;
  199. }
  200. }
  201. }
  202. }
  203. // output glyphs and associations
  204. putGlyphs(getGlyphs(0, nog, false, null, null, null), getAssociations(0, nog, false, null, null, null), null);
  205. // consume replaced input glyphs
  206. consume(nog);
  207. return true;
  208. } else {
  209. return false;
  210. }
  211. }
  212. /**
  213. * Apply default application semantices; namely, consume one input glyph,
  214. * writing that glyph (and its association) to the output glyphs (and associations).
  215. */
  216. public void applyDefault() {
  217. super.applyDefault();
  218. int gi = getGlyph();
  219. if (gi != 65535) {
  220. putGlyph(gi, getAssociation(), null);
  221. }
  222. }
  223. private static IntBuffer growBuffer(IntBuffer ib) {
  224. int capacity = ib.capacity();
  225. int capacityNew = capacity * 2;
  226. IntBuffer ibNew = IntBuffer.allocate(capacityNew);
  227. ib.rewind();
  228. return ibNew.put(ib);
  229. }
  230. }