1 17 18 19 20 package org.apache.fop.layoutmgr; 21 22 import org.apache.commons.logging.Log; 23 import org.apache.commons.logging.LogFactory; 24 import org.apache.fop.traits.BorderProps; 25 import org.apache.fop.traits.MinOptMax; 26 import org.apache.fop.area.Area; 27 import org.apache.fop.area.Trait; 28 import org.apache.fop.datatypes.LengthBase; 29 import org.apache.fop.datatypes.PercentBaseContext; 30 import org.apache.fop.datatypes.SimplePercentBaseContext; 31 import org.apache.fop.fo.Constants; 32 import org.apache.fop.fo.properties.CommonMarginBlock; 33 import org.apache.fop.fo.properties.CommonBorderPaddingBackground; 34 import org.apache.fop.fo.properties.CommonTextDecoration; 35 import org.apache.fop.fonts.Font; 36 import org.apache.fop.fonts.FontInfo; 37 import org.apache.fop.fonts.FontTriplet; 38 39 42 public class TraitSetter { 43 44 45 protected static Log log = LogFactory.getLog(TraitSetter.class); 46 47 55 public static void setBorderPaddingTraits(Area area, 56 CommonBorderPaddingBackground bpProps, boolean bNotFirst, boolean bNotLast, 57 PercentBaseContext context) { 58 int iBP; 59 iBP = bpProps.getPadding(CommonBorderPaddingBackground.START, bNotFirst, context); 60 if (iBP > 0) { 61 area.addTrait(Trait.PADDING_START, new Integer (iBP)); 62 } 63 iBP = bpProps.getPadding(CommonBorderPaddingBackground.END, bNotLast, context); 64 if (iBP > 0) { 65 area.addTrait(Trait.PADDING_END, new Integer (iBP)); 66 } 67 iBP = bpProps.getPadding(CommonBorderPaddingBackground.BEFORE, false, context); 68 if (iBP > 0) { 69 area.addTrait(Trait.PADDING_BEFORE, new Integer (iBP)); 70 } 71 iBP = bpProps.getPadding(CommonBorderPaddingBackground.AFTER, false, context); 72 if (iBP > 0) { 73 area.addTrait(Trait.PADDING_AFTER, new Integer (iBP)); 74 } 75 76 addBorderTrait(area, bpProps, bNotFirst, 77 CommonBorderPaddingBackground.START, 78 BorderProps.SEPARATE, Trait.BORDER_START); 79 80 addBorderTrait(area, bpProps, bNotLast, 81 CommonBorderPaddingBackground.END, 82 BorderProps.SEPARATE, Trait.BORDER_END); 83 84 addBorderTrait(area, bpProps, false, 85 CommonBorderPaddingBackground.BEFORE, 86 BorderProps.SEPARATE, Trait.BORDER_BEFORE); 87 88 addBorderTrait(area, bpProps, false, 89 CommonBorderPaddingBackground.AFTER, 90 BorderProps.SEPARATE, Trait.BORDER_AFTER); 91 } 92 93 99 private static void addBorderTrait(Area area, 100 CommonBorderPaddingBackground bpProps, 101 boolean bDiscard, int iSide, int mode, 102 Object oTrait) { 103 int iBP = bpProps.getBorderWidth(iSide, bDiscard); 104 if (iBP > 0) { 105 area.addTrait(oTrait, 106 new BorderProps(bpProps.getBorderStyle(iSide), 107 iBP, bpProps.getBorderColor(iSide), 108 mode)); 109 } 110 } 111 112 121 public static void addBorders(Area area, CommonBorderPaddingBackground bordProps, 122 PercentBaseContext context) { 123 BorderProps bps = getBorderProps(bordProps, CommonBorderPaddingBackground.BEFORE); 124 if (bps != null) { 125 area.addTrait(Trait.BORDER_BEFORE, bps); 126 } 127 bps = getBorderProps(bordProps, CommonBorderPaddingBackground.AFTER); 128 if (bps != null) { 129 area.addTrait(Trait.BORDER_AFTER, bps); 130 } 131 bps = getBorderProps(bordProps, CommonBorderPaddingBackground.START); 132 if (bps != null) { 133 area.addTrait(Trait.BORDER_START, bps); 134 } 135 bps = getBorderProps(bordProps, CommonBorderPaddingBackground.END); 136 if (bps != null) { 137 area.addTrait(Trait.BORDER_END, bps); 138 } 139 140 addPadding(area, bordProps, context); 141 } 142 143 155 public static void addBorders(Area area, CommonBorderPaddingBackground bordProps, 156 boolean discardBefore, boolean discardAfter, 157 boolean discardStart, boolean discardEnd, 158 PercentBaseContext context) { 159 BorderProps bps = getBorderProps(bordProps, CommonBorderPaddingBackground.BEFORE); 160 if (bps != null && !discardBefore) { 161 area.addTrait(Trait.BORDER_BEFORE, bps); 162 } 163 bps = getBorderProps(bordProps, CommonBorderPaddingBackground.AFTER); 164 if (bps != null && !discardAfter) { 165 area.addTrait(Trait.BORDER_AFTER, bps); 166 } 167 bps = getBorderProps(bordProps, CommonBorderPaddingBackground.START); 168 if (bps != null && !discardStart) { 169 area.addTrait(Trait.BORDER_START, bps); 170 } 171 bps = getBorderProps(bordProps, CommonBorderPaddingBackground.END); 172 if (bps != null && !discardEnd) { 173 area.addTrait(Trait.BORDER_END, bps); 174 } 175 } 176 177 187 public static void addCollapsingBorders(Area area, 188 CommonBorderPaddingBackground bordProps, 189 boolean[] outer, 190 PercentBaseContext context) { 191 BorderProps bps = getCollapsingBorderProps(bordProps, 192 CommonBorderPaddingBackground.BEFORE, outer[0]); 193 if (bps != null) { 194 area.addTrait(Trait.BORDER_BEFORE, bps); 195 } 196 bps = getCollapsingBorderProps(bordProps, 197 CommonBorderPaddingBackground.AFTER, outer[1]); 198 if (bps != null) { 199 area.addTrait(Trait.BORDER_AFTER, bps); 200 } 201 bps = getCollapsingBorderProps(bordProps, 202 CommonBorderPaddingBackground.START, outer[2]); 203 if (bps != null) { 204 area.addTrait(Trait.BORDER_START, bps); 205 } 206 bps = getCollapsingBorderProps(bordProps, 207 CommonBorderPaddingBackground.END, outer[3]); 208 if (bps != null) { 209 area.addTrait(Trait.BORDER_END, bps); 210 } 211 212 addPadding(area, bordProps, context); 213 } 214 215 private static void addPadding(Area area, CommonBorderPaddingBackground bordProps, 216 PercentBaseContext context) { 217 addPadding(area, bordProps, false, false, false, false, context); 218 } 219 220 232 public static void addPadding(Area area, CommonBorderPaddingBackground bordProps, 233 boolean discardBefore, boolean discardAfter, 234 boolean discardStart, boolean discardEnd, 235 PercentBaseContext context) { 236 int padding = bordProps.getPadding(CommonBorderPaddingBackground.BEFORE, 237 discardBefore, context); 238 if (padding != 0) { 239 area.addTrait(Trait.PADDING_BEFORE, new java.lang.Integer (padding)); 240 } 241 242 padding = bordProps.getPadding(CommonBorderPaddingBackground.AFTER, 243 discardAfter, context); 244 if (padding != 0) { 245 area.addTrait(Trait.PADDING_AFTER, new java.lang.Integer (padding)); 246 } 247 248 padding = bordProps.getPadding(CommonBorderPaddingBackground.START, 249 discardStart, context); 250 if (padding != 0) { 251 area.addTrait(Trait.PADDING_START, new java.lang.Integer (padding)); 252 } 253 254 padding = bordProps.getPadding(CommonBorderPaddingBackground.END, 255 discardEnd, context); 256 if (padding != 0) { 257 area.addTrait(Trait.PADDING_END, new java.lang.Integer (padding)); 258 } 259 260 } 261 262 private static BorderProps getBorderProps(CommonBorderPaddingBackground bordProps, int side) { 263 int width = bordProps.getBorderWidth(side, false); 264 if (width != 0) { 265 BorderProps bps; 266 bps = new BorderProps(bordProps.getBorderStyle(side), 267 width, 268 bordProps.getBorderColor(side), 269 BorderProps.SEPARATE); 270 return bps; 271 } else { 272 return null; 273 } 274 } 275 276 private static BorderProps getCollapsingBorderProps( 277 CommonBorderPaddingBackground bordProps, int side, boolean outer) { 278 int width = bordProps.getBorderWidth(side, false); 279 if (width != 0) { 280 BorderProps bps; 281 bps = new BorderProps(bordProps.getBorderStyle(side), 282 width, bordProps.getBorderColor(side), 283 (outer ? BorderProps.COLLAPSE_OUTER : BorderProps.COLLAPSE_INNER)); 284 return bps; 285 } else { 286 return null; 287 } 288 } 289 290 299 public static void addBackground(Area area, 300 CommonBorderPaddingBackground backProps, 301 PercentBaseContext context) { 302 if (!backProps.hasBackground()) { 303 return; 304 } 305 Trait.Background back = new Trait.Background(); 306 back.setColor(backProps.backgroundColor); 307 308 if (backProps.getFopImage() != null) { 309 back.setURL(backProps.backgroundImage); 310 back.setFopImage(backProps.getFopImage()); 311 back.setRepeat(backProps.backgroundRepeat); 312 if (backProps.backgroundPositionHorizontal != null) { 313 if (back.getRepeat() == Constants.EN_NOREPEAT 314 || back.getRepeat() == Constants.EN_REPEATY) { 315 if (area.getIPD() > 0) { 316 int width = area.getIPD(); 317 width += backProps.getPaddingStart(false, context); 318 width += backProps.getPaddingEnd(false, context); 319 back.setHoriz(backProps.backgroundPositionHorizontal.getValue( 320 new SimplePercentBaseContext(context, 321 LengthBase.IMAGE_BACKGROUND_POSITION_HORIZONTAL, 322 (width - back.getFopImage().getIntrinsicWidth()) 323 ) 324 )); 325 } else { 326 log.warn("Horizontal background image positioning ignored" 328 + " because the IPD was not set on the area." 329 + " (Yes, it's a bug in FOP)"); 330 } 331 } 332 } 333 if (backProps.backgroundPositionVertical != null) { 334 if (back.getRepeat() == Constants.EN_NOREPEAT 335 || back.getRepeat() == Constants.EN_REPEATX) { 336 if (area.getBPD() > 0) { 337 int height = area.getBPD(); 338 height += backProps.getPaddingBefore(false, context); 339 height += backProps.getPaddingAfter(false, context); 340 back.setVertical(backProps.backgroundPositionVertical.getValue( 341 new SimplePercentBaseContext(context, 342 LengthBase.IMAGE_BACKGROUND_POSITION_VERTICAL, 343 (height - back.getFopImage().getIntrinsicHeight()) 344 ) 345 )); 346 } else { 347 log.warn("Vertical background image positioning ignored" 349 + " because the BPD was not set on the area." 350 + " (Yes, it's a bug in FOP)"); 351 } 352 } 353 } 354 } 355 356 area.addTrait(Trait.BACKGROUND, back); 357 } 358 359 369 public static void addMargins(Area area, 370 CommonBorderPaddingBackground bpProps, 371 int startIndent, int endIndent, 372 PercentBaseContext context) { 373 if (startIndent != 0) { 374 area.addTrait(Trait.START_INDENT, new Integer (startIndent)); 375 } 376 377 int spaceStart = startIndent 378 - bpProps.getBorderStartWidth(false) 379 - bpProps.getPaddingStart(false, context); 380 if (spaceStart != 0) { 381 area.addTrait(Trait.SPACE_START, new Integer (spaceStart)); 382 } 383 384 if (endIndent != 0) { 385 area.addTrait(Trait.END_INDENT, new Integer (endIndent)); 386 } 387 int spaceEnd = endIndent 388 - bpProps.getBorderEndWidth(false) 389 - bpProps.getPaddingEnd(false, context); 390 if (spaceEnd != 0) { 391 area.addTrait(Trait.SPACE_END, new Integer (spaceEnd)); 392 } 393 } 394 395 404 public static void addMargins(Area area, 405 CommonBorderPaddingBackground bpProps, 406 CommonMarginBlock marginProps, 407 PercentBaseContext context) { 408 int startIndent = marginProps.startIndent.getValue(context); 409 int endIndent = marginProps.endIndent.getValue(context); 410 addMargins(area, bpProps, startIndent, endIndent, context); 411 } 412 413 420 public static int getEffectiveSpace(double adjust, MinOptMax space) { 421 if (space == null) { 422 return 0; 423 } 424 int sp = space.opt; 425 if (adjust > 0) { 426 sp = sp + (int)(adjust * (space.max - space.opt)); 427 } else { 428 sp = sp + (int)(adjust * (space.opt - space.min)); 429 } 430 return sp; 431 } 432 433 440 public static void addSpaceBeforeAfter(Area area, double adjust, 441 MinOptMax spaceBefore, MinOptMax spaceAfter) { 442 int space; 443 space = getEffectiveSpace(adjust, spaceBefore); 444 if (space != 0) { 445 area.addTrait(Trait.SPACE_BEFORE, new Integer (space)); 446 } 447 space = getEffectiveSpace(adjust, spaceAfter); 448 if (space != 0) { 449 area.addTrait(Trait.SPACE_AFTER, new Integer (space)); 450 } 451 } 452 453 459 public static void addBreaks(Area area, int breakBefore, int breakAfter) { 460 464 } 465 466 471 public static void addFontTraits(Area area, Font font) { 472 area.addTrait(Trait.FONT, font.getFontTriplet()); 473 area.addTrait(Trait.FONT_SIZE, new Integer (font.getFontSize())); 474 } 475 476 481 public static void addTextDecoration(Area area, CommonTextDecoration deco) { 482 if (deco != null) { 484 if (deco.hasUnderline()) { 485 area.addTrait(Trait.UNDERLINE, Boolean.TRUE); 486 area.addTrait(Trait.UNDERLINE_COLOR, deco.getUnderlineColor()); 487 } 488 if (deco.hasOverline()) { 489 area.addTrait(Trait.OVERLINE, Boolean.TRUE); 490 area.addTrait(Trait.OVERLINE_COLOR, deco.getOverlineColor()); 491 } 492 if (deco.hasLineThrough()) { 493 area.addTrait(Trait.LINETHROUGH, Boolean.TRUE); 494 area.addTrait(Trait.LINETHROUGH_COLOR, deco.getLineThroughColor()); 495 } 496 if (deco.isBlinking()) { 497 area.addTrait(Trait.BLINK, Boolean.TRUE); 498 } 499 } 500 } 501 502 508 public static void setProducerID(Area area, String id) { 509 if (id != null && id.length() > 0) { 510 area.addTrait(Trait.PROD_ID, id); 511 } 512 } 513 } 514 | Popular Tags |