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.

IndentPropertyMaker.java 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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.fo.properties;
  19. import org.apache.fop.datatypes.Numeric;
  20. import org.apache.fop.fo.FONode;
  21. import org.apache.fop.fo.FObj;
  22. import org.apache.fop.fo.PropertyList;
  23. import org.apache.fop.fo.expr.NumericOp;
  24. import org.apache.fop.fo.expr.PropertyException;
  25. /**
  26. * This property maker handles the calculations described in 5.3.2 which
  27. * involves the sizes of the corresponding margin-* properties and the
  28. * padding-* and border-*-width properties.
  29. */
  30. public class IndentPropertyMaker extends CorrespondingPropertyMaker {
  31. /**
  32. * The corresponding padding-* propIds
  33. */
  34. private int[] paddingCorresponding = null;
  35. /**
  36. * The corresponding border-*-width propIds
  37. */
  38. private int[] borderWidthCorresponding = null;
  39. /**
  40. * Create a start-indent or end-indent property maker.
  41. * @param baseMaker the property maker to use
  42. */
  43. public IndentPropertyMaker(PropertyMaker baseMaker) {
  44. super(baseMaker);
  45. }
  46. /**
  47. * Set the corresponding values for the padding-* properties.
  48. * @param paddingCorresponding the corresping propids.
  49. */
  50. public void setPaddingCorresponding(int[] paddingCorresponding) {
  51. this.paddingCorresponding = paddingCorresponding;
  52. }
  53. /**
  54. * Set the corresponding values for the border-*-width properties.
  55. * @param borderWidthCorresponding the corresping propids.
  56. */
  57. public void setBorderWidthCorresponding(int[] borderWidthCorresponding) {
  58. this.borderWidthCorresponding = borderWidthCorresponding;
  59. }
  60. /**
  61. * Calculate the corresponding value for start-indent and end-indent.
  62. * @see CorrespondingPropertyMaker#compute(PropertyList)
  63. */
  64. public Property compute(PropertyList propertyList) throws PropertyException {
  65. if (propertyList.getFObj().getUserAgent()
  66. .isBreakIndentInheritanceOnReferenceAreaBoundary()) {
  67. return computeAlternativeRuleset(propertyList);
  68. } else {
  69. return computeConforming(propertyList);
  70. }
  71. }
  72. /**
  73. * Calculate the corresponding value for start-indent and end-indent.
  74. * @see CorrespondingPropertyMaker#compute(PropertyList)
  75. */
  76. public Property computeConforming(PropertyList propertyList) throws PropertyException {
  77. PropertyList pList = getWMPropertyList(propertyList);
  78. if (pList == null) {
  79. return null;
  80. }
  81. // Calculate the values as described in 5.3.2.
  82. Numeric padding = getCorresponding(paddingCorresponding, propertyList).getNumeric();
  83. Numeric border = getCorresponding(borderWidthCorresponding, propertyList).getNumeric();
  84. int marginProp = pList.getWritingMode(lr_tb, rl_tb, tb_rl);
  85. // Calculate the absolute margin.
  86. if (propertyList.getExplicitOrShorthand(marginProp) == null) {
  87. Property indent = propertyList.getExplicit(baseMaker.propId);
  88. if (indent == null) {
  89. //Neither indent nor margin is specified, use inherited
  90. return null;
  91. } else {
  92. //Use explicit indent directly
  93. return indent;
  94. }
  95. } else {
  96. //Margin is used
  97. Numeric margin = propertyList.get(marginProp).getNumeric();
  98. Numeric v = new FixedLength(0);
  99. if (!propertyList.getFObj().generatesReferenceAreas()) {
  100. // The inherited_value_of([start|end]-indent)
  101. v = NumericOp.addition(v, propertyList.getInherited(baseMaker.propId).getNumeric());
  102. }
  103. // The corresponding absolute margin-[right|left}.
  104. v = NumericOp.addition(v, margin);
  105. v = NumericOp.addition(v, padding);
  106. v = NumericOp.addition(v, border);
  107. return (Property) v;
  108. }
  109. }
  110. private boolean isInherited(PropertyList pList) {
  111. if (pList.getFObj().getUserAgent().isBreakIndentInheritanceOnReferenceAreaBoundary()) {
  112. FONode nd = pList.getFObj().getParent();
  113. return !((nd instanceof FObj) && ((FObj)nd).generatesReferenceAreas());
  114. } else {
  115. return true;
  116. }
  117. }
  118. /**
  119. * Calculate the corresponding value for start-indent and end-indent.
  120. * This method calculates indent following an alternative rule set that
  121. * tries to mimic many commercial solutions that chose to violate the
  122. * XSL specification.
  123. * @see CorrespondingPropertyMaker#compute(PropertyList)
  124. */
  125. public Property computeAlternativeRuleset(PropertyList propertyList) throws PropertyException {
  126. PropertyList pList = getWMPropertyList(propertyList);
  127. if (pList == null) {
  128. return null;
  129. }
  130. // Calculate the values as described in 5.3.2.
  131. Numeric padding = getCorresponding(paddingCorresponding, propertyList).getNumeric();
  132. Numeric border = getCorresponding(borderWidthCorresponding, propertyList).getNumeric();
  133. int marginProp = pList.getWritingMode(lr_tb, rl_tb, tb_rl);
  134. //Determine whether the nearest anscestor indent was specified through
  135. //start-indent|end-indent or through a margin property.
  136. boolean marginNearest = false;
  137. PropertyList pl = propertyList.getParentPropertyList();
  138. while (pl != null) {
  139. if (pl.getExplicit(baseMaker.propId) != null) {
  140. break;
  141. } else if (pl.getExplicitOrShorthand(marginProp) != null) {
  142. marginNearest = true;
  143. break;
  144. }
  145. pl = pl.getParentPropertyList();
  146. }
  147. // Calculate the absolute margin.
  148. if (propertyList.getExplicitOrShorthand(marginProp) == null) {
  149. Property indent = propertyList.getExplicit(baseMaker.propId);
  150. if (indent == null) {
  151. //Neither start-indent nor margin is specified, use inherited
  152. if (isInherited(propertyList) || !marginNearest) {
  153. return null;
  154. } else {
  155. return new FixedLength(0);
  156. }
  157. } else {
  158. return indent;
  159. }
  160. } else {
  161. //Margin is used
  162. Numeric margin = propertyList.get(marginProp).getNumeric();
  163. Numeric v = new FixedLength(0);
  164. if (isInherited(propertyList)) {
  165. // The inherited_value_of([start|end]-indent)
  166. v = NumericOp.addition(v, propertyList.getInherited(baseMaker.propId).getNumeric());
  167. }
  168. // The corresponding absolute margin-[right|left}.
  169. v = NumericOp.addition(v, margin);
  170. v = NumericOp.addition(v, padding);
  171. v = NumericOp.addition(v, border);
  172. return (Property) v;
  173. }
  174. }
  175. private Property getCorresponding(int[] corresponding, PropertyList propertyList)
  176. throws PropertyException {
  177. PropertyList pList = getWMPropertyList(propertyList);
  178. if (pList != null) {
  179. int wmcorr = pList.getWritingMode(corresponding[0], corresponding[1], corresponding[2]);
  180. return propertyList.get(wmcorr);
  181. } else {
  182. return null;
  183. }
  184. }
  185. }