1 17 18 19 20 package org.apache.fop.fo.properties; 21 22 import java.awt.Color ; 23 24 import org.apache.fop.apps.FOUserAgent; 25 import org.apache.fop.datatypes.Length; 26 import org.apache.fop.datatypes.PercentBaseContext; 27 import org.apache.fop.fo.Constants; 28 import org.apache.fop.fo.FObj; 29 import org.apache.fop.fo.PropertyList; 30 import org.apache.fop.fo.expr.PropertyException; 31 import org.apache.fop.image.FopImage; 32 import org.apache.fop.image.ImageFactory; 33 34 38 public class CommonBorderPaddingBackground implements Cloneable { 39 42 public int backgroundAttachment; 43 44 47 public Color backgroundColor; 48 49 52 public String backgroundImage; 53 54 57 public int backgroundRepeat; 58 59 62 public Length backgroundPositionHorizontal; 63 64 67 public Length backgroundPositionVertical; 68 69 70 private FopImage fopimage; 71 72 73 74 public static final int BEFORE = 0; 75 76 public static final int AFTER = 1; 77 78 public static final int START = 2; 79 80 public static final int END = 3; 81 82 public static class BorderInfo implements Cloneable { 83 private int mStyle; private Color mColor; private CondLengthProperty mWidth; 86 87 BorderInfo(int style, CondLengthProperty width, Color color) { 88 mStyle = style; 89 mWidth = width; 90 mColor = color; 91 } 92 93 public int getStyle() { 94 return this.mStyle; 95 } 96 97 public Color getColor() { 98 return this.mColor; 99 } 100 101 public CondLengthProperty getWidth() { 102 return this.mWidth; 103 } 104 105 public int getRetainedWidth() { 106 if ((mStyle == Constants.EN_NONE) 107 || (mStyle == Constants.EN_HIDDEN)) { 108 return 0; 109 } else { 110 return mWidth.getLengthValue(); 111 } 112 } 113 114 115 public String toString() { 116 StringBuffer sb = new StringBuffer ("BorderInfo"); 117 sb.append(" {"); 118 sb.append(mStyle); 119 sb.append(", "); 120 sb.append(mColor); 121 sb.append(", "); 122 sb.append(mWidth); 123 sb.append("}"); 124 return sb.toString(); 125 } 126 } 127 128 private BorderInfo[] borderInfo = new BorderInfo[4]; 129 private CondLengthProperty[] padding = new CondLengthProperty[4]; 130 131 134 public CommonBorderPaddingBackground() { 135 136 } 137 138 144 public CommonBorderPaddingBackground(PropertyList pList, FObj fobj) throws PropertyException { 145 146 backgroundAttachment = pList.get(Constants.PR_BACKGROUND_ATTACHMENT).getEnum(); 147 backgroundColor = pList.get(Constants.PR_BACKGROUND_COLOR).getColor( 148 fobj == null ? null : fobj.getUserAgent()); 149 if (backgroundColor.getAlpha() == 0) { 150 backgroundColor = null; 151 } 152 153 backgroundImage = pList.get(Constants.PR_BACKGROUND_IMAGE).getString(); 154 if (backgroundImage == null || "none".equals(backgroundImage)) { 155 backgroundImage = null; 156 } else { 157 backgroundRepeat = pList.get(Constants.PR_BACKGROUND_REPEAT).getEnum(); 158 backgroundPositionHorizontal = pList.get( 159 Constants.PR_BACKGROUND_POSITION_HORIZONTAL).getLength(); 160 backgroundPositionVertical = pList.get( 161 Constants.PR_BACKGROUND_POSITION_VERTICAL).getLength(); 162 163 String url = ImageFactory.getURL(backgroundImage); 165 FOUserAgent userAgent = fobj.getUserAgent(); 166 ImageFactory fact = userAgent.getFactory().getImageFactory(); 167 fopimage = fact.getImage(url, userAgent); 168 if (fopimage == null) { 169 fobj.getLogger().error("Background image not available: " + backgroundImage); 170 } else { 171 if (!fopimage.load(FopImage.DIMENSIONS)) { 173 fobj.getLogger().error("Cannot read background image dimensions: " 174 + backgroundImage); 175 } 176 } 177 } 179 180 initBorderInfo(pList, BEFORE, 181 Constants.PR_BORDER_BEFORE_COLOR, 182 Constants.PR_BORDER_BEFORE_STYLE, 183 Constants.PR_BORDER_BEFORE_WIDTH, 184 Constants.PR_PADDING_BEFORE); 185 initBorderInfo(pList, AFTER, 186 Constants.PR_BORDER_AFTER_COLOR, 187 Constants.PR_BORDER_AFTER_STYLE, 188 Constants.PR_BORDER_AFTER_WIDTH, 189 Constants.PR_PADDING_AFTER); 190 initBorderInfo(pList, START, 191 Constants.PR_BORDER_START_COLOR, 192 Constants.PR_BORDER_START_STYLE, 193 Constants.PR_BORDER_START_WIDTH, 194 Constants.PR_PADDING_START); 195 initBorderInfo(pList, END, 196 Constants.PR_BORDER_END_COLOR, 197 Constants.PR_BORDER_END_STYLE, 198 Constants.PR_BORDER_END_WIDTH, 199 Constants.PR_PADDING_END); 200 201 } 202 203 private void initBorderInfo(PropertyList pList, int side, 204 int colorProp, int styleProp, int widthProp, int paddingProp) 205 throws PropertyException { 206 padding[side] = pList.get(paddingProp).getCondLength(); 207 int style = pList.get(styleProp).getEnum(); 209 if (style != Constants.EN_NONE) { 210 FOUserAgent ua = (pList == null) 211 ? null 212 : (pList.getFObj() == null ? null : pList.getFObj().getUserAgent()); 213 setBorderInfo(new BorderInfo(style, 214 pList.get(widthProp).getCondLength(), 215 pList.get(colorProp).getColor(ua)), side); 216 } 217 } 218 219 224 public void setBorderInfo(BorderInfo info, int side) { 225 this.borderInfo[side] = info; 226 } 227 228 232 public BorderInfo getBorderInfo(int side) { 233 return this.borderInfo[side]; 234 } 235 236 240 public void setPadding(CommonBorderPaddingBackground source) { 241 this.padding = source.padding; 242 } 243 244 248 public FopImage getFopImage() { 249 return this.fopimage; 250 } 251 252 256 public int getBorderStartWidth(boolean bDiscard) { 257 return getBorderWidth(START, bDiscard); 258 } 259 260 264 public int getBorderEndWidth(boolean bDiscard) { 265 return getBorderWidth(END, bDiscard); 266 } 267 268 272 public int getBorderBeforeWidth(boolean bDiscard) { 273 return getBorderWidth(BEFORE, bDiscard); 274 } 275 276 280 public int getBorderAfterWidth(boolean bDiscard) { 281 return getBorderWidth(AFTER, bDiscard); 282 } 283 284 public int getPaddingStart(boolean bDiscard, PercentBaseContext context) { 285 return getPadding(START, bDiscard, context); 286 } 287 288 public int getPaddingEnd(boolean bDiscard, PercentBaseContext context) { 289 return getPadding(END, bDiscard, context); 290 } 291 292 public int getPaddingBefore(boolean bDiscard, PercentBaseContext context) { 293 return getPadding(BEFORE, bDiscard, context); 294 } 295 296 public int getPaddingAfter(boolean bDiscard, PercentBaseContext context) { 297 return getPadding(AFTER, bDiscard, context); 298 } 299 300 public int getBorderWidth(int side, boolean bDiscard) { 301 if ((borderInfo[side] == null) 302 || (borderInfo[side].mStyle == Constants.EN_NONE) 303 || (borderInfo[side].mStyle == Constants.EN_HIDDEN) 304 || (bDiscard && borderInfo[side].mWidth.isDiscard())) { 305 return 0; 306 } else { 307 return borderInfo[side].mWidth.getLengthValue(); 308 } 309 } 310 311 public Color getBorderColor(int side) { 312 if (borderInfo[side] != null) { 313 return borderInfo[side].getColor(); 314 } else { 315 return null; 316 } 317 } 318 319 public int getBorderStyle(int side) { 320 if (borderInfo[side] != null) { 321 return borderInfo[side].mStyle; 322 } else { 323 return Constants.EN_NONE; 324 } 325 } 326 327 public int getPadding(int side, boolean bDiscard, PercentBaseContext context) { 328 if ((padding[side] == null) || (bDiscard && padding[side].isDiscard())) { 329 return 0; 330 } else { 331 return padding[side].getLengthValue(context); 332 } 333 } 334 335 340 public CondLengthProperty getPaddingLengthProperty(int side) { 341 return padding[side]; 342 } 343 344 351 public int getIPPaddingAndBorder(boolean bDiscard, PercentBaseContext context) { 352 return getPaddingStart(bDiscard, context) 353 + getPaddingEnd(bDiscard, context) 354 + getBorderStartWidth(bDiscard) 355 + getBorderEndWidth(bDiscard); 356 } 357 358 365 public int getBPPaddingAndBorder(boolean bDiscard, PercentBaseContext context) { 366 return getPaddingBefore(bDiscard, context) + getPaddingAfter(bDiscard, context) 367 + getBorderBeforeWidth(bDiscard) + getBorderAfterWidth(bDiscard); 368 } 369 370 371 public String toString() { 372 return "CommonBordersAndPadding (Before, After, Start, End):\n" 373 + "Borders: (" + getBorderBeforeWidth(false) + ", " + getBorderAfterWidth(false) + ", " 374 + getBorderStartWidth(false) + ", " + getBorderEndWidth(false) + ")\n" 375 + "Border Colors: (" + getBorderColor(BEFORE) + ", " + getBorderColor(AFTER) + ", " 376 + getBorderColor(START) + ", " + getBorderColor(END) + ")\n" 377 + "Padding: (" + getPaddingBefore(false, null) + ", " + getPaddingAfter(false, null) 378 + ", " + getPaddingStart(false, null) + ", " + getPaddingEnd(false, null) + ")\n"; 379 } 380 381 384 public boolean hasBackground() { 385 return ((backgroundColor != null || getFopImage() != null)); 386 } 387 388 389 public boolean hasBorder() { 390 return ((getBorderBeforeWidth(false) + getBorderAfterWidth(false) 391 + getBorderStartWidth(false) + getBorderEndWidth(false)) > 0); 392 } 393 394 398 public boolean hasPadding(PercentBaseContext context) { 399 return ((getPaddingBefore(false, context) + getPaddingAfter(false, context) 400 + getPaddingStart(false, context) + getPaddingEnd(false, context)) > 0); 401 } 402 403 404 public boolean hasBorderInfo() { 405 return (borderInfo[BEFORE] != null || borderInfo[AFTER] != null 406 || borderInfo[START] != null || borderInfo[END] != null); 407 } 408 } 409 | Popular Tags |