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.

ScaledBaselineTable.java 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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.layoutmgr.inline;
  19. import org.apache.fop.fo.Constants;
  20. import org.apache.fop.traits.WritingMode;
  21. /**
  22. * The FOP specific incarnation of the XSL-FO scaled baseline table.
  23. * All baseline tables are scaled to the font size of the font they
  24. * apply to. This class uses a coordinate system with its origin
  25. * where the dominant baseline intersects the start edge of the box.
  26. * All measurements are in mpt.
  27. */
  28. final class ScaledBaselineTable {
  29. private static final float HANGING_BASELINE_FACTOR = 0.8f;
  30. private static final float MATHEMATICAL_BASELINE_FACTOR = 0.5f;
  31. private final int altitude;
  32. private final int depth;
  33. private final int xHeight;
  34. private final int dominantBaselineIdentifier;
  35. private final WritingMode writingMode;
  36. private final int dominantBaselineOffset;
  37. private int beforeEdgeOffset;
  38. private int afterEdgeOffset;
  39. /**
  40. *
  41. * Creates a new instance of BasicScaledBaselineTable for the given
  42. * altitude, depth, xHeight, baseline and writing mode.
  43. * @param altitude the height of the box or the font ascender
  44. * @param depth the font descender or 0
  45. * @param xHeight the font xHeight
  46. * @param dominantBaselineIdentifier the dominant baseline given as an integer constant
  47. * @param writingMode the writing mode given as an integer constant
  48. */
  49. ScaledBaselineTable(int altitude,
  50. int depth,
  51. int xHeight,
  52. int dominantBaselineIdentifier,
  53. WritingMode writingMode) {
  54. this.altitude = altitude;
  55. this.depth = depth;
  56. this.xHeight = xHeight;
  57. this.dominantBaselineIdentifier = dominantBaselineIdentifier;
  58. this.writingMode = writingMode;
  59. this.dominantBaselineOffset = getBaselineDefaultOffset(this.dominantBaselineIdentifier);
  60. this.beforeEdgeOffset = altitude - dominantBaselineOffset;
  61. this.afterEdgeOffset = depth - dominantBaselineOffset;
  62. }
  63. /**
  64. * Return the dominant baseline for this baseline table.
  65. * @return the dominant baseline
  66. */
  67. int getDominantBaselineIdentifier() {
  68. return this.dominantBaselineIdentifier;
  69. }
  70. /**
  71. * Return the writing mode for this baseline table.
  72. * @return the writing mode
  73. */
  74. WritingMode getWritingMode() {
  75. return this.writingMode;
  76. }
  77. /**
  78. * Return the offset of the given baseline from the dominant baseline.
  79. *
  80. * @param baselineIdentifier a baseline identifier
  81. * @return the offset from the dominant baseline
  82. */
  83. int getBaseline(int baselineIdentifier) {
  84. int offset = 0;
  85. if (!isHorizontalWritingMode()) {
  86. switch (baselineIdentifier) {
  87. case Constants.EN_TOP:
  88. case Constants.EN_TEXT_TOP:
  89. case Constants.EN_TEXT_BOTTOM:
  90. case Constants.EN_BOTTOM:
  91. throw new IllegalArgumentException("Baseline " + baselineIdentifier
  92. + " only supported for horizontal writing modes");
  93. default: // TODO
  94. }
  95. }
  96. switch (baselineIdentifier) {
  97. case Constants.EN_TOP: // fall through
  98. case Constants.EN_BEFORE_EDGE:
  99. offset = beforeEdgeOffset;
  100. break;
  101. case Constants.EN_TEXT_TOP:
  102. case Constants.EN_TEXT_BEFORE_EDGE:
  103. case Constants.EN_HANGING:
  104. case Constants.EN_CENTRAL:
  105. case Constants.EN_MIDDLE:
  106. case Constants.EN_MATHEMATICAL:
  107. case Constants.EN_ALPHABETIC:
  108. case Constants.EN_IDEOGRAPHIC:
  109. case Constants.EN_TEXT_BOTTOM:
  110. case Constants.EN_TEXT_AFTER_EDGE:
  111. offset = getBaselineDefaultOffset(baselineIdentifier) - dominantBaselineOffset;
  112. break;
  113. case Constants.EN_BOTTOM: // fall through
  114. case Constants.EN_AFTER_EDGE:
  115. offset = afterEdgeOffset;
  116. break;
  117. default: throw new IllegalArgumentException(String.valueOf(baselineIdentifier));
  118. }
  119. return offset;
  120. }
  121. private boolean isHorizontalWritingMode() {
  122. return writingMode.isHorizontal();
  123. }
  124. /**
  125. * Return the baseline offset measured from the font's default
  126. * baseline for the given baseline.
  127. * @param baselineIdentifier the baseline identifier
  128. * @return the baseline offset
  129. */
  130. private int getBaselineDefaultOffset(int baselineIdentifier) {
  131. int offset = 0;
  132. switch (baselineIdentifier) {
  133. case Constants.EN_TEXT_BEFORE_EDGE:
  134. offset = altitude;
  135. break;
  136. case Constants.EN_HANGING:
  137. offset = Math.round(altitude * HANGING_BASELINE_FACTOR);
  138. break;
  139. case Constants.EN_CENTRAL:
  140. offset = (altitude - depth) / 2 + depth;
  141. break;
  142. case Constants.EN_MIDDLE:
  143. offset = xHeight / 2;
  144. break;
  145. case Constants.EN_MATHEMATICAL:
  146. offset = Math.round(altitude * MATHEMATICAL_BASELINE_FACTOR);
  147. break;
  148. case Constants.EN_ALPHABETIC:
  149. offset = 0;
  150. break;
  151. case Constants.EN_IDEOGRAPHIC: // Fall through
  152. case Constants.EN_TEXT_AFTER_EDGE:
  153. offset = depth;
  154. break;
  155. default: throw new IllegalArgumentException(String.valueOf(baselineIdentifier));
  156. }
  157. return offset;
  158. }
  159. /**
  160. * Sets the position of the before and after baselines.
  161. * This is usually only done for line areas. For other
  162. * areas the position of the before and after baselines
  163. * are fixed when the table is constructed.
  164. * @param beforeBaseline the offset of the before-edge baseline from the dominant baseline
  165. * @param afterBaseline the offset of the after-edge baseline from the dominant baseline
  166. */
  167. void setBeforeAndAfterBaselines(int beforeBaseline, int afterBaseline) {
  168. beforeEdgeOffset = beforeBaseline;
  169. afterEdgeOffset = afterBaseline;
  170. }
  171. /**
  172. * Return a new baseline table for the given baseline based
  173. * on the current baseline table.
  174. * @param baselineIdentifier the baseline identifer
  175. * @return a new baseline with the new baseline
  176. */
  177. ScaledBaselineTable deriveScaledBaselineTable(int baselineIdentifier) {
  178. ScaledBaselineTable bac = new ScaledBaselineTable(altitude, depth, xHeight,
  179. baselineIdentifier, this.writingMode);
  180. return bac;
  181. }
  182. }