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.

GlyphPositioningSubtable.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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>GlyphPositioningSubtable</code> implements an abstract base of a glyph subtable,
  24. * providing a default implementation of the <code>GlyphPositioning</code> interface.</p>
  25. *
  26. * <p>This work was originally authored by Glenn Adams (gadams@apache.org).</p>
  27. */
  28. public abstract class GlyphPositioningSubtable extends GlyphSubtable implements GlyphPositioning {
  29. private static final GlyphPositioningState STATE = new GlyphPositioningState();
  30. /**
  31. * Instantiate a <code>GlyphPositioningSubtable</code>.
  32. * @param id subtable identifier
  33. * @param sequence subtable sequence
  34. * @param flags subtable flags
  35. * @param format subtable format
  36. * @param coverage subtable coverage table
  37. */
  38. protected GlyphPositioningSubtable(String id, int sequence, int flags, int format, GlyphCoverageTable coverage) {
  39. super(id, sequence, flags, format, coverage);
  40. }
  41. /** {@inheritDoc} */
  42. public int getTableType() {
  43. return GlyphTable.GLYPH_TABLE_TYPE_POSITIONING;
  44. }
  45. /** {@inheritDoc} */
  46. public String getTypeName() {
  47. return GlyphPositioningTable.getLookupTypeName(getType());
  48. }
  49. /** {@inheritDoc} */
  50. public boolean isCompatible(GlyphSubtable subtable) {
  51. return subtable instanceof GlyphPositioningSubtable;
  52. }
  53. /** {@inheritDoc} */
  54. public boolean usesReverseScan() {
  55. return false;
  56. }
  57. /** {@inheritDoc} */
  58. public boolean position(GlyphPositioningState ps) {
  59. return false;
  60. }
  61. /**
  62. * Apply positioning using specified state and subtable array. For each position in input sequence,
  63. * apply subtables in order until some subtable applies or none remain. If no subtable applied or no
  64. * input was consumed for a given position, then apply default action (no adjustments and advance).
  65. * If <code>sequenceIndex</code> is non-negative, then apply subtables only when current position
  66. * matches <code>sequenceIndex</code> in relation to the starting position. Furthermore, upon
  67. * successful application at <code>sequenceIndex</code>, then discontinue processing the remaining
  68. * @param ps positioning state
  69. * @param sta array of subtables to apply
  70. * @param sequenceIndex if non negative, then apply subtables only at specified sequence index
  71. * @return true if a non-zero adjustment occurred
  72. */
  73. public static final boolean position(GlyphPositioningState ps, GlyphPositioningSubtable[] sta, int sequenceIndex) {
  74. int sequenceStart = ps.getPosition();
  75. boolean appliedOneShot = false;
  76. while (ps.hasNext()) {
  77. boolean applied = false;
  78. if (!appliedOneShot && ps.maybeApplicable()) {
  79. for (int i = 0, n = sta.length; !applied && (i < n); i++) {
  80. if (sequenceIndex < 0) {
  81. applied = ps.apply(sta [ i ]);
  82. } else if (ps.getPosition() == (sequenceStart + sequenceIndex)) {
  83. applied = ps.apply(sta [ i ]);
  84. if (applied) {
  85. appliedOneShot = true;
  86. }
  87. }
  88. }
  89. }
  90. if (!applied || !ps.didConsume()) {
  91. ps.applyDefault();
  92. }
  93. ps.next();
  94. }
  95. return ps.getAdjusted();
  96. }
  97. /**
  98. * Apply positioning.
  99. * @param gs input glyph sequence
  100. * @param script tag
  101. * @param language tag
  102. * @param feature tag
  103. * @param fontSize the font size
  104. * @param sta subtable array
  105. * @param widths array
  106. * @param adjustments array (receives output adjustments)
  107. * @param sct script context tester
  108. * @return true if a non-zero adjustment occurred
  109. */
  110. public static final boolean position(GlyphSequence gs, String script, String language, String feature, int fontSize, GlyphPositioningSubtable[] sta, int[] widths, int[][] adjustments, ScriptContextTester sct) {
  111. synchronized (STATE) {
  112. return position(STATE.reset(gs, script, language, feature, fontSize, widths, adjustments, sct), sta, -1);
  113. }
  114. }
  115. }