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.

CommonBorderPaddingBackground.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /*
  2. * Copyright 2004-2005 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.fo.properties;
  18. import org.apache.fop.apps.FOUserAgent;
  19. import org.apache.fop.datatypes.ColorType;
  20. import org.apache.fop.datatypes.Length;
  21. import org.apache.fop.datatypes.PercentBaseContext;
  22. import org.apache.fop.fo.Constants;
  23. import org.apache.fop.fo.FObj;
  24. import org.apache.fop.fo.PropertyList;
  25. import org.apache.fop.fo.expr.PropertyException;
  26. import org.apache.fop.image.FopImage;
  27. import org.apache.fop.image.ImageFactory;
  28. /**
  29. * Stores all common border and padding properties.
  30. * See Sec. 7.7 of the XSL-FO Standard.
  31. */
  32. public class CommonBorderPaddingBackground implements Cloneable {
  33. /**
  34. * The "background-attachment" property.
  35. */
  36. public int backgroundAttachment;
  37. /**
  38. * The "background-color" property.
  39. */
  40. public ColorType backgroundColor;
  41. /**
  42. * The "background-image" property.
  43. */
  44. public String backgroundImage;
  45. /**
  46. * The "background-repeat" property.
  47. */
  48. public int backgroundRepeat;
  49. /**
  50. * The "background-position-horizontal" property.
  51. */
  52. public Length backgroundPositionHorizontal;
  53. /**
  54. * The "background-position-vertical" property.
  55. */
  56. public Length backgroundPositionVertical;
  57. private FopImage fopimage;
  58. /** the "before" edge */
  59. public static final int BEFORE = 0;
  60. /** the "after" edge */
  61. public static final int AFTER = 1;
  62. /** the "start" edge */
  63. public static final int START = 2;
  64. /** the "end" edge */
  65. public static final int END = 3;
  66. public static class BorderInfo implements Cloneable {
  67. private int mStyle; // Enum for border style
  68. private ColorType mColor; // Border color
  69. private CondLengthProperty mWidth;
  70. BorderInfo(int style, CondLengthProperty width, ColorType color) {
  71. mStyle = style;
  72. mWidth = width;
  73. mColor = color;
  74. }
  75. public int getStyle() {
  76. return this.mStyle;
  77. }
  78. public ColorType getColor() {
  79. return this.mColor;
  80. }
  81. public CondLengthProperty getWidth() {
  82. return this.mWidth;
  83. }
  84. public int getRetainedWidth() {
  85. if ((mStyle == Constants.EN_NONE)
  86. || (mStyle == Constants.EN_HIDDEN)) {
  87. return 0;
  88. } else {
  89. return mWidth.getLengthValue();
  90. }
  91. }
  92. /** @see java.lang.Object#toString() */
  93. public String toString() {
  94. StringBuffer sb = new StringBuffer("BorderInfo");
  95. sb.append(" {");
  96. sb.append(mStyle);
  97. sb.append(", ");
  98. sb.append(mColor);
  99. sb.append(", ");
  100. sb.append(mWidth);
  101. sb.append("}");
  102. return sb.toString();
  103. }
  104. }
  105. private BorderInfo[] borderInfo = new BorderInfo[4];
  106. private CondLengthProperty[] padding = new CondLengthProperty[4];
  107. /**
  108. * Construct a CommonBorderPaddingBackground object.
  109. */
  110. public CommonBorderPaddingBackground() {
  111. }
  112. /**
  113. * Construct a CommonBorderPaddingBackground object.
  114. * @param pList The PropertyList to get properties from.
  115. * @param fobj The FO to create this instance for.
  116. * @throws PropertyException if there's an error while binding the properties
  117. */
  118. public CommonBorderPaddingBackground(PropertyList pList, FObj fobj) throws PropertyException {
  119. backgroundAttachment = pList.get(Constants.PR_BACKGROUND_ATTACHMENT).getEnum();
  120. backgroundColor = pList.get(Constants.PR_BACKGROUND_COLOR).getColorType();
  121. if (backgroundColor.getAlpha() == 0) {
  122. backgroundColor = null;
  123. }
  124. backgroundImage = pList.get(Constants.PR_BACKGROUND_IMAGE).getString();
  125. if (backgroundImage == null || "none".equals(backgroundImage)) {
  126. backgroundImage = null;
  127. } else {
  128. backgroundRepeat = pList.get(Constants.PR_BACKGROUND_REPEAT).getEnum();
  129. backgroundPositionHorizontal = pList.get(
  130. Constants.PR_BACKGROUND_POSITION_HORIZONTAL).getLength();
  131. backgroundPositionVertical = pList.get(
  132. Constants.PR_BACKGROUND_POSITION_VERTICAL).getLength();
  133. //Additional processing: preload image
  134. String url = ImageFactory.getURL(backgroundImage);
  135. FOUserAgent userAgent = fobj.getUserAgent();
  136. ImageFactory fact = userAgent.getFactory().getImageFactory();
  137. fopimage = fact.getImage(url, userAgent);
  138. if (fopimage == null) {
  139. fobj.getLogger().error("Background image not available: " + backgroundImage);
  140. } else {
  141. // load dimensions
  142. if (!fopimage.load(FopImage.DIMENSIONS)) {
  143. fobj.getLogger().error("Cannot read background image dimensions: "
  144. + backgroundImage);
  145. }
  146. }
  147. //TODO Report to caller so he can decide to throw an exception
  148. }
  149. initBorderInfo(pList, BEFORE,
  150. Constants.PR_BORDER_BEFORE_COLOR,
  151. Constants.PR_BORDER_BEFORE_STYLE,
  152. Constants.PR_BORDER_BEFORE_WIDTH,
  153. Constants.PR_PADDING_BEFORE);
  154. initBorderInfo(pList, AFTER,
  155. Constants.PR_BORDER_AFTER_COLOR,
  156. Constants.PR_BORDER_AFTER_STYLE,
  157. Constants.PR_BORDER_AFTER_WIDTH,
  158. Constants.PR_PADDING_AFTER);
  159. initBorderInfo(pList, START,
  160. Constants.PR_BORDER_START_COLOR,
  161. Constants.PR_BORDER_START_STYLE,
  162. Constants.PR_BORDER_START_WIDTH,
  163. Constants.PR_PADDING_START);
  164. initBorderInfo(pList, END,
  165. Constants.PR_BORDER_END_COLOR,
  166. Constants.PR_BORDER_END_STYLE,
  167. Constants.PR_BORDER_END_WIDTH,
  168. Constants.PR_PADDING_END);
  169. }
  170. private void initBorderInfo(PropertyList pList, int side,
  171. int colorProp, int styleProp, int widthProp, int paddingProp)
  172. throws PropertyException {
  173. padding[side] = pList.get(paddingProp).getCondLength();
  174. // If style = none, force width to 0, don't get Color (spec 7.7.20)
  175. int style = pList.get(styleProp).getEnum();
  176. if (style != Constants.EN_NONE) {
  177. setBorderInfo(new BorderInfo(style,
  178. pList.get(widthProp).getCondLength(),
  179. pList.get(colorProp).getColorType()), side);
  180. }
  181. }
  182. /**
  183. * Sets a border.
  184. * @param info the border information
  185. * @param side the side to apply the info to
  186. */
  187. public void setBorderInfo(BorderInfo info, int side) {
  188. this.borderInfo[side] = info;
  189. }
  190. /**
  191. * @param side the side to retrieve
  192. * @return the border info for a side
  193. */
  194. public BorderInfo getBorderInfo(int side) {
  195. return this.borderInfo[side];
  196. }
  197. /**
  198. * Set padding.
  199. * @param source the padding info to copy from
  200. */
  201. public void setPadding(CommonBorderPaddingBackground source) {
  202. this.padding = source.padding;
  203. }
  204. /**
  205. * @return the background image as a preloaded FopImage, null if there is
  206. * no background image.
  207. */
  208. public FopImage getFopImage() {
  209. return this.fopimage;
  210. }
  211. public int getBorderStartWidth(boolean bDiscard) {
  212. return getBorderWidth(START, bDiscard);
  213. }
  214. public int getBorderEndWidth(boolean bDiscard) {
  215. return getBorderWidth(END, bDiscard);
  216. }
  217. public int getBorderBeforeWidth(boolean bDiscard) {
  218. return getBorderWidth(BEFORE, bDiscard);
  219. }
  220. public int getBorderAfterWidth(boolean bDiscard) {
  221. return getBorderWidth(AFTER, bDiscard);
  222. }
  223. public int getPaddingStart(boolean bDiscard, PercentBaseContext context) {
  224. return getPadding(START, bDiscard, context);
  225. }
  226. public int getPaddingEnd(boolean bDiscard, PercentBaseContext context) {
  227. return getPadding(END, bDiscard, context);
  228. }
  229. public int getPaddingBefore(boolean bDiscard, PercentBaseContext context) {
  230. return getPadding(BEFORE, bDiscard, context);
  231. }
  232. public int getPaddingAfter(boolean bDiscard, PercentBaseContext context) {
  233. return getPadding(AFTER, bDiscard, context);
  234. }
  235. public int getBorderWidth(int side, boolean bDiscard) {
  236. if ((borderInfo[side] == null)
  237. || (borderInfo[side].mStyle == Constants.EN_NONE)
  238. || (borderInfo[side].mStyle == Constants.EN_HIDDEN)
  239. || (bDiscard && borderInfo[side].mWidth.isDiscard())) {
  240. return 0;
  241. } else {
  242. return borderInfo[side].mWidth.getLengthValue();
  243. }
  244. }
  245. public ColorType getBorderColor(int side) {
  246. if (borderInfo[side] != null) {
  247. return borderInfo[side].mColor;
  248. } else {
  249. return null;
  250. }
  251. }
  252. public int getBorderStyle(int side) {
  253. if (borderInfo[side] != null) {
  254. return borderInfo[side].mStyle;
  255. } else {
  256. return Constants.EN_NONE;
  257. }
  258. }
  259. public int getPadding(int side, boolean bDiscard, PercentBaseContext context) {
  260. if ((padding[side] == null) || (bDiscard && padding[side].isDiscard())) {
  261. return 0;
  262. } else {
  263. return padding[side].getLengthValue(context);
  264. }
  265. }
  266. /**
  267. * Returns the CondLengthProperty for the padding on one side.
  268. * @param side the side
  269. * @return the requested CondLengthProperty
  270. */
  271. public CondLengthProperty getPaddingLengthProperty(int side) {
  272. return padding[side];
  273. }
  274. /**
  275. * Return all the border and padding width in the inline progression
  276. * dimension.
  277. * @param bDiscard the discard flag.
  278. * @param context for percentage evaluation.
  279. * @return all the padding and border width.
  280. */
  281. public int getIPPaddingAndBorder(boolean bDiscard, PercentBaseContext context) {
  282. return getPaddingStart(bDiscard, context)
  283. + getPaddingEnd(bDiscard, context)
  284. + getBorderStartWidth(bDiscard)
  285. + getBorderEndWidth(bDiscard);
  286. }
  287. /**
  288. * Return all the border and padding height in the block progression
  289. * dimension.
  290. * @param bDiscard the discard flag.
  291. * @param context for percentage evaluation
  292. * @return all the padding and border height.
  293. */
  294. public int getBPPaddingAndBorder(boolean bDiscard, PercentBaseContext context) {
  295. return getPaddingBefore(bDiscard, context) + getPaddingAfter(bDiscard, context) +
  296. getBorderBeforeWidth(bDiscard) + getBorderAfterWidth(bDiscard);
  297. }
  298. public String toString() {
  299. return "CommonBordersAndPadding (Before, After, Start, End):\n" +
  300. "Borders: (" + getBorderBeforeWidth(false) + ", " + getBorderAfterWidth(false) + ", " +
  301. getBorderStartWidth(false) + ", " + getBorderEndWidth(false) + ")\n" +
  302. "Border Colors: (" + getBorderColor(BEFORE) + ", " + getBorderColor(AFTER) + ", " +
  303. getBorderColor(START) + ", " + getBorderColor(END) + ")\n" +
  304. "Padding: (" + getPaddingBefore(false, null) + ", " + getPaddingAfter(false, null) + ", " +
  305. getPaddingStart(false, null) + ", " + getPaddingEnd(false, null) + ")\n";
  306. }
  307. /**
  308. * @return true if there is any kind of background to be painted
  309. */
  310. public boolean hasBackground() {
  311. return ((backgroundColor != null || getFopImage() != null));
  312. }
  313. /** @return true if border is non-zero. */
  314. public boolean hasBorder() {
  315. return ((getBorderBeforeWidth(false) + getBorderAfterWidth(false)
  316. + getBorderStartWidth(false) + getBorderEndWidth(false)) > 0);
  317. }
  318. /**
  319. * @param context for percentage based evaluation.
  320. * @return true if padding is non-zero.
  321. */
  322. public boolean hasPadding(PercentBaseContext context) {
  323. return ((getPaddingBefore(false, context) + getPaddingAfter(false, context)
  324. + getPaddingStart(false, context) + getPaddingEnd(false, context)) > 0);
  325. }
  326. /** @return true if there are any borders defined. */
  327. public boolean hasBorderInfo() {
  328. return (borderInfo[BEFORE] != null || borderInfo[AFTER] != null
  329. || borderInfo[START] != null || borderInfo[END] != null);
  330. }
  331. }