Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

GlyphPositioningState.java 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 org.apache.fop.complexscripts.util.GlyphSequence;
  20. import org.apache.fop.complexscripts.util.ScriptContextTester;
  21. // CSOFF: LineLengthCheck
  22. /**
  23. * <p>The <code>GlyphPositioningState</code> implements an state object used during glyph positioning
  24. * processing.</p>
  25. *
  26. * <p>This work was originally authored by Glenn Adams (gadams@apache.org).</p>
  27. */
  28. public class GlyphPositioningState extends GlyphProcessingState {
  29. /** font size */
  30. private int fontSize;
  31. /** default advancements */
  32. private int[] widths;
  33. /** current adjustments */
  34. private int[][] adjustments;
  35. /** if true, then some adjustment was applied */
  36. private boolean adjusted;
  37. /**
  38. * Construct default (reset) glyph positioning state.
  39. */
  40. public GlyphPositioningState() {
  41. }
  42. /**
  43. * Construct glyph positioning state.
  44. * @param gs input glyph sequence
  45. * @param script script identifier
  46. * @param language language identifier
  47. * @param feature feature identifier
  48. * @param fontSize font size (in micropoints)
  49. * @param widths array of design advancements (in glyph index order)
  50. * @param adjustments positioning adjustments to which positioning is applied
  51. * @param sct script context tester (or null)
  52. */
  53. public GlyphPositioningState(GlyphSequence gs, String script, String language, String feature, int fontSize, int[] widths, int[][] adjustments, ScriptContextTester sct) {
  54. super(gs, script, language, feature, sct);
  55. this.fontSize = fontSize;
  56. this.widths = widths;
  57. this.adjustments = adjustments;
  58. }
  59. /**
  60. * Construct glyph positioning 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 ps existing positioning state to copy from
  63. */
  64. public GlyphPositioningState(GlyphPositioningState ps) {
  65. super(ps);
  66. this.fontSize = ps.fontSize;
  67. this.widths = ps.widths;
  68. this.adjustments = ps.adjustments;
  69. }
  70. /**
  71. * Reset glyph positioning state.
  72. * @param gs input glyph sequence
  73. * @param script script identifier
  74. * @param language language identifier
  75. * @param feature feature identifier
  76. * @param fontSize font size (in micropoints)
  77. * @param widths array of design advancements (in glyph index order)
  78. * @param adjustments positioning adjustments to which positioning is applied
  79. * @param sct script context tester (or null)
  80. */
  81. public GlyphPositioningState reset(GlyphSequence gs, String script, String language, String feature, int fontSize, int[] widths, int[][] adjustments, ScriptContextTester sct) {
  82. super.reset(gs, script, language, feature, sct);
  83. this.fontSize = fontSize;
  84. this.widths = widths;
  85. this.adjustments = adjustments;
  86. this.adjusted = false;
  87. return this;
  88. }
  89. /**
  90. * Obtain design advancement (width) of glyph at specified index.
  91. * @param gi glyph index
  92. * @return design advancement, or zero if glyph index is not present
  93. */
  94. public int getWidth(int gi) {
  95. if ((widths != null) && (gi < widths.length)) {
  96. return widths [ gi ];
  97. } else {
  98. return 0;
  99. }
  100. }
  101. /**
  102. * Perform adjustments at current position index.
  103. * @param v value containing adjustments
  104. * @return true if a non-zero adjustment was made
  105. */
  106. public boolean adjust(GlyphPositioningTable.Value v) {
  107. return adjust(v, 0);
  108. }
  109. /**
  110. * Perform adjustments at specified offset from current position index.
  111. * @param v value containing adjustments
  112. * @param offset from current position index
  113. * @return true if a non-zero adjustment was made
  114. */
  115. public boolean adjust(GlyphPositioningTable.Value v, int offset) {
  116. assert v != null;
  117. if ((index + offset) < indexLast) {
  118. return v.adjust(adjustments [ index + offset ], fontSize);
  119. } else {
  120. throw new IndexOutOfBoundsException();
  121. }
  122. }
  123. /**
  124. * Obtain current adjustments at current position index.
  125. * @return array of adjustments (int[4]) at current position
  126. */
  127. public int[] getAdjustment() {
  128. return getAdjustment(0);
  129. }
  130. /**
  131. * Obtain current adjustments at specified offset from current position index.
  132. * @param offset from current position index
  133. * @return array of adjustments (int[4]) at specified offset
  134. * @throws IndexOutOfBoundsException if offset is invalid
  135. */
  136. public int[] getAdjustment(int offset) throws IndexOutOfBoundsException {
  137. if ((index + offset) < indexLast) {
  138. return adjustments [ index + offset ];
  139. } else {
  140. throw new IndexOutOfBoundsException();
  141. }
  142. }
  143. /**
  144. * Apply positioning subtable to current state at current position (only),
  145. * resulting in the consumption of zero or more input glyphs.
  146. * @param st the glyph positioning subtable to apply
  147. * @return true if subtable applied, or false if it did not (e.g., its
  148. * input coverage table did not match current input context)
  149. */
  150. public boolean apply(GlyphPositioningSubtable st) {
  151. assert st != null;
  152. updateSubtableState(st);
  153. boolean applied = st.position(this);
  154. return applied;
  155. }
  156. /**
  157. * Apply a sequence of matched rule lookups to the <code>nig</code> input glyphs
  158. * starting at the current position. If lookups are non-null and non-empty, then
  159. * all input glyphs specified by <code>nig</code> are consumed irregardless of
  160. * whether any specified lookup applied.
  161. * @param lookups array of matched lookups (or null)
  162. * @param nig number of glyphs in input sequence, starting at current position, to which
  163. * the lookups are to apply, and to be consumed once the application has finished
  164. * @return true if lookups are non-null and non-empty; otherwise, false
  165. */
  166. public boolean apply(GlyphTable.RuleLookup[] lookups, int nig) {
  167. if ((lookups != null) && (lookups.length > 0)) {
  168. // apply each rule lookup to extracted input glyph array
  169. for (int i = 0, n = lookups.length; i < n; i++) {
  170. GlyphTable.RuleLookup l = lookups [ i ];
  171. if (l != null) {
  172. GlyphTable.LookupTable lt = l.getLookup();
  173. if (lt != null) {
  174. // perform positioning on a copy of previous state
  175. GlyphPositioningState ps = new GlyphPositioningState(this);
  176. // apply lookup table positioning
  177. if (lt.position(ps, l.getSequenceIndex())) {
  178. setAdjusted(true);
  179. }
  180. }
  181. }
  182. }
  183. consume(nig);
  184. return true;
  185. } else {
  186. return false;
  187. }
  188. }
  189. /**
  190. * Apply default application semantices; namely, consume one input glyph.
  191. */
  192. public void applyDefault() {
  193. super.applyDefault();
  194. }
  195. /**
  196. * Set adjusted state, used to record effect of non-zero adjustment.
  197. * @param adjusted true if to set adjusted state, otherwise false to
  198. * clear adjusted state
  199. */
  200. public void setAdjusted(boolean adjusted) {
  201. this.adjusted = adjusted;
  202. }
  203. /**
  204. * Get adjusted state.
  205. * @return adjusted true if some non-zero adjustment occurred and
  206. * was recorded by {@link #setAdjusted}; otherwise, false.
  207. */
  208. public boolean getAdjusted() {
  209. return adjusted;
  210. }
  211. }