您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

TraitSetter.java 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.layoutmgr;
  18. import org.apache.fop.fo.properties.CommonBorderAndPadding;
  19. import org.apache.fop.traits.BorderProps;
  20. import org.apache.fop.area.Area;
  21. import org.apache.fop.area.Trait;
  22. import org.apache.fop.fo.properties.CommonBackground;
  23. import org.apache.fop.fo.properties.CommonMarginBlock;
  24. /**
  25. * This is a helper class used for setting common traits on areas.
  26. */
  27. public class TraitSetter {
  28. /**
  29. * Sets border and padding traits on areas.
  30. * @param area area to set the traits on
  31. * @param bpProps border and padding properties
  32. */
  33. public static void setBorderPaddingTraits(Area area,
  34. CommonBorderAndPadding bpProps, boolean bNotFirst, boolean bNotLast) {
  35. int iBP;
  36. iBP = bpProps.getPadding(CommonBorderAndPadding.START, bNotFirst);
  37. if (iBP > 0) {
  38. //area.addTrait(new Trait(Trait.PADDING_START, new Integer(iBP)));
  39. area.addTrait(Trait.PADDING_START, new Integer(iBP));
  40. }
  41. iBP = bpProps.getPadding(CommonBorderAndPadding.END, bNotLast);
  42. if (iBP > 0) {
  43. //area.addTrait(new Trait(Trait.PADDING_END, new Integer(iBP)));
  44. area.addTrait(Trait.PADDING_END, new Integer(iBP));
  45. }
  46. iBP = bpProps.getPadding(CommonBorderAndPadding.BEFORE, false);
  47. if (iBP > 0) {
  48. // area.addTrait(new Trait(Trait.PADDING_BEFORE, new Integer(iBP)));
  49. area.addTrait(Trait.PADDING_BEFORE, new Integer(iBP));
  50. }
  51. iBP = bpProps.getPadding(CommonBorderAndPadding.AFTER, false);
  52. if (iBP > 0) {
  53. //area.addTrait(new Trait(Trait.PADDING_AFTER, new Integer(iBP)));
  54. area.addTrait(Trait.PADDING_AFTER, new Integer(iBP));
  55. }
  56. addBorderTrait(area, bpProps, bNotFirst,
  57. CommonBorderAndPadding.START, Trait.BORDER_START);
  58. addBorderTrait(area, bpProps, bNotLast, CommonBorderAndPadding.END,
  59. Trait.BORDER_END);
  60. addBorderTrait(area, bpProps, false, CommonBorderAndPadding.BEFORE,
  61. Trait.BORDER_BEFORE);
  62. addBorderTrait(area, bpProps, false, CommonBorderAndPadding.AFTER,
  63. Trait.BORDER_AFTER);
  64. }
  65. /**
  66. * Sets border traits on an area.
  67. * @param area area to set the traits on
  68. * @param bpProps border and padding properties
  69. */
  70. private static void addBorderTrait(Area area,
  71. CommonBorderAndPadding bpProps,
  72. boolean bDiscard, int iSide,
  73. Object oTrait) {
  74. int iBP = bpProps.getBorderWidth(iSide, bDiscard);
  75. if (iBP > 0) {
  76. // area.addTrait(new Trait(oTrait,
  77. // new BorderProps(bpProps.getBorderStyle(iSide),
  78. // iBP,
  79. // bpProps.getBorderColor(iSide))));
  80. area.addTrait(oTrait,
  81. new BorderProps(bpProps.getBorderStyle(iSide),
  82. iBP, bpProps.getBorderColor(iSide)));
  83. }
  84. }
  85. /**
  86. * Add borders to an area.
  87. * Layout managers that create areas with borders can use this to
  88. * add the borders to the area.
  89. * @param curBlock area to set the traits on
  90. * @param bordProps border properties
  91. */
  92. public static void addBorders(Area curBlock, CommonBorderAndPadding bordProps) {
  93. BorderProps bps = getBorderProps(bordProps, CommonBorderAndPadding.BEFORE);
  94. if (bps.width != 0) {
  95. curBlock.addTrait(Trait.BORDER_BEFORE, bps);
  96. }
  97. bps = getBorderProps(bordProps, CommonBorderAndPadding.AFTER);
  98. if (bps.width != 0) {
  99. curBlock.addTrait(Trait.BORDER_AFTER, bps);
  100. }
  101. bps = getBorderProps(bordProps, CommonBorderAndPadding.START);
  102. if (bps.width != 0) {
  103. curBlock.addTrait(Trait.BORDER_START, bps);
  104. }
  105. bps = getBorderProps(bordProps, CommonBorderAndPadding.END);
  106. if (bps.width != 0) {
  107. curBlock.addTrait(Trait.BORDER_END, bps);
  108. }
  109. int padding = bordProps.getPadding(CommonBorderAndPadding.START, false);
  110. if (padding != 0) {
  111. curBlock.addTrait(Trait.PADDING_START, new java.lang.Integer(padding));
  112. }
  113. padding = bordProps.getPadding(CommonBorderAndPadding.END, false);
  114. if (padding != 0) {
  115. curBlock.addTrait(Trait.PADDING_END, new java.lang.Integer(padding));
  116. }
  117. padding = bordProps.getPadding(CommonBorderAndPadding.BEFORE, false);
  118. if (padding != 0) {
  119. curBlock.addTrait(Trait.PADDING_BEFORE, new java.lang.Integer(padding));
  120. }
  121. padding = bordProps.getPadding(CommonBorderAndPadding.AFTER, false);
  122. if (padding != 0) {
  123. curBlock.addTrait(Trait.PADDING_AFTER, new java.lang.Integer(padding));
  124. }
  125. }
  126. private static BorderProps getBorderProps(CommonBorderAndPadding bordProps, int side) {
  127. BorderProps bps;
  128. bps = new BorderProps(bordProps.getBorderStyle(side),
  129. bordProps.getBorderWidth(side, false),
  130. bordProps.getBorderColor(side));
  131. return bps;
  132. }
  133. /**
  134. * Add background to an area.
  135. * Layout managers that create areas with a background can use this to
  136. * add the background to the area.
  137. * @param curBlock the current block
  138. * @param backProps the background properties
  139. */
  140. public static void addBackground(Area curBlock, CommonBackground backProps) {
  141. Trait.Background back = new Trait.Background();
  142. back.setColor(backProps.backColor);
  143. if (backProps.backImage != null) {
  144. back.setURL(backProps.backImage);
  145. back.setRepeat(backProps.backRepeat);
  146. if (backProps.backPosHorizontal != null) {
  147. back.setHoriz(backProps.backPosHorizontal.getValue());
  148. }
  149. if (backProps.backPosVertical != null) {
  150. back.setVertical(backProps.backPosVertical.getValue());
  151. }
  152. }
  153. if (back.getColor() != null || back.getURL() != null) {
  154. curBlock.addTrait(Trait.BACKGROUND, back);
  155. }
  156. }
  157. /**
  158. * Add space to a block area.
  159. * Layout managers that create block areas can use this to add space
  160. * outside of the border rectangle to the area.
  161. * @param curBlock the current block.
  162. * @param marginProps the margin properties.
  163. */
  164. public static void addMargins(Area curBlock,
  165. CommonBorderAndPadding bpProps,
  166. CommonMarginBlock marginProps) {
  167. int spaceStart = marginProps.startIndent -
  168. bpProps.getBorderStartWidth(false) -
  169. bpProps.getPaddingStart(false);
  170. if (spaceStart != 0) {
  171. curBlock.addTrait(Trait.SPACE_START, new Integer(spaceStart));
  172. }
  173. int spaceEnd = marginProps.endIndent -
  174. bpProps.getBorderEndWidth(false) -
  175. bpProps.getPaddingEnd(false);
  176. if (spaceEnd != 0) {
  177. curBlock.addTrait(Trait.SPACE_END, new Integer(spaceEnd));
  178. }
  179. }
  180. }