1 51 package org.apache.fop.render; 52 53 import org.apache.fop.image.ImageArea; 55 import org.apache.fop.image.FopImage; 56 import org.apache.fop.image.FopImageException; 57 import org.apache.fop.fo.properties.*; 58 import org.apache.fop.layout.*; 59 import org.apache.fop.layout.inline.*; 60 import org.apache.fop.datatypes.*; 61 62 import org.apache.avalon.framework.logger.Logger; 64 65 import java.util.List ; 67 68 72 public abstract class AbstractRenderer implements Renderer { 73 protected Logger log; 74 75 78 protected int currentYPosition = 0; 79 80 83 protected int currentXPosition = 0; 84 85 88 protected int currentAreaContainerXPosition = 0; 89 90 protected IDReferences idReferences; 91 92 public void setLogger(Logger logger) { 93 log = logger; 94 } 95 96 public void renderSpanArea(SpanArea area) { 97 List children = area.getChildren(); 98 for (int i = 0; i < children.size(); i++) { 99 Box b = (Box)children.get(i); 100 b.render(this); } 102 103 } 104 105 protected abstract void doFrame(Area area); 106 107 114 protected void doBackground(Area area, int x, int y, int w, int h) { 115 if (h == 0 || w == 0) 116 return; 117 118 BackgroundProps props = area.getBackground(); 119 if (props == null) 120 return; 121 122 if (props.backColor.alpha() == 0) { 123 this.addFilledRect(x, y, w, -h, props.backColor); 124 } 125 126 if (props.backImage != null) { 129 int imgW; 130 int imgH; 131 try { 132 imgW = props.backImage.getWidth() * 1000; 134 imgH = props.backImage.getHeight() * 1000; 135 } 136 catch (FopImageException fie) { 137 log.error("Error obtaining bg image width and height", fie); 138 return; 139 } 140 141 int dx = x; 142 int dy = y; 143 int endX = x + w; 144 int endY = y - h; 145 int clipW = w % imgW; 146 int clipH = h % imgH; 147 148 boolean repeatX = true; 149 boolean repeatY = true; 150 switch (props.backRepeat) { 151 case BackgroundRepeat.REPEAT: 152 break; 153 154 case BackgroundRepeat.REPEAT_X: 155 repeatY = false; 156 break; 157 158 case BackgroundRepeat.REPEAT_Y: 159 repeatX = false; 160 break; 161 162 case BackgroundRepeat.NO_REPEAT: 163 repeatX = false; 164 repeatY = false; 165 break; 166 167 case BackgroundRepeat.INHERIT: 168 break; 170 171 default: 172 log.error("Ignoring invalid background-repeat property"); 173 } 174 175 FontState fs = area.getFontState(); 176 177 while (dy > endY) { while (dx < endX) { if (dx + imgW <= endX) { 180 if (dy - imgH >= endY) { 182 drawImageScaled(dx, dy, imgW, imgH, 184 props.backImage, fs); 185 } 186 else { 187 drawImageClipped(dx, dy, 189 0, 0, imgW, clipH, 190 props.backImage, fs); 191 } 192 } 193 else { 194 if (dy - imgH >= endY) { 196 drawImageClipped(dx, dy, 198 0, 0, clipW, imgH, 199 props.backImage, fs); 200 } 201 202 else { 203 drawImageClipped(dx, dy, 205 0, 0, clipW, clipH, 206 props.backImage, fs); 207 } 208 } 209 210 if (repeatX) { 211 dx += imgW; 212 } 213 else { 214 break; 215 } 216 } 218 dx = x; 219 220 if (repeatY) { 221 dy -= imgH; 222 } 223 else { 224 break; 225 } 226 } } 228 } 229 230 241 protected abstract void addFilledRect(int x, int y, int w, int h, 242 ColorType col); 243 244 256 protected void drawImage(int x, int y, FopImage image, FontState fs) { 257 int w; 258 int h; 259 try { 260 w = image.getWidth() * 1000; 262 h = image.getHeight() * 1000; 263 } 264 catch (FopImageException e) { 265 log.error("Failed to obtain the image width and height", e); 266 return; 267 } 268 drawImageScaled(x, y, w, h, image, fs); 269 } 270 271 284 protected abstract void drawImageScaled(int x, int y, int w, int h, 285 FopImage image, 286 FontState fs); 287 288 301 protected abstract void drawImageClipped(int x, int y, 302 int clipX, int clipY, 303 int clipW, int clipH, 304 FopImage image, 305 FontState fs); 306 307 312 public void renderImageArea(ImageArea area) { 313 int x = this.currentXPosition + area.getXOffset(); 315 int y = this.currentYPosition; 316 int w = area.getContentWidth(); 317 int h = area.getHeight(); 318 319 this.currentYPosition -= h; 320 321 FopImage img = area.getImage(); 322 323 if (img == null) { 324 log.error("Error while loading image: area.getImage() is null"); 325 } else { 326 drawImageScaled(x, y, w, h, img, area.getFontState()); 327 } 328 329 this.currentXPosition += w; 330 } 331 332 public void renderBodyAreaContainer(BodyAreaContainer area) { 333 int saveY = this.currentYPosition; 334 int saveX = this.currentAreaContainerXPosition; 335 336 if (area.getPosition() == Position.ABSOLUTE) { 337 this.currentYPosition = area.getYPosition(); 339 this.currentAreaContainerXPosition = area.getXPosition(); 340 } else if (area.getPosition() == Position.RELATIVE) { 341 this.currentYPosition -= area.getYPosition(); 342 this.currentAreaContainerXPosition += area.getXPosition(); 343 } 344 345 this.currentXPosition = this.currentAreaContainerXPosition; 346 int rx = this.currentAreaContainerXPosition; 347 int ry = this.currentYPosition; 348 int w = area.getAllocationWidth(); 356 int h = area.getMaxHeight(); 357 358 doBackground(area, rx, ry, w, h); 359 360 renderAreaContainer(area.getBeforeFloatReferenceArea()); 362 renderAreaContainer(area.getFootnoteReferenceArea()); 363 364 List children = area.getMainReferenceArea().getChildren(); 366 for (int i = 0; i < children.size(); i++) { 367 Box b = (Box)children.get(i); 368 b.render(this); } 370 371 if (area.getPosition() != Position.STATIC) { 372 this.currentYPosition = saveY; 373 this.currentAreaContainerXPosition = saveX; 374 } else { 375 this.currentYPosition -= area.getHeight(); 376 } 377 } 378 379 384 public void renderRegionAreaContainer(AreaContainer area) { 385 int saveY = this.currentYPosition; 386 int saveX = this.currentAreaContainerXPosition; 387 388 if (area.getPosition() == Position.ABSOLUTE) { 389 this.currentYPosition = area.getYPosition(); 391 this.currentAreaContainerXPosition = area.getXPosition(); 392 } else if (area.getPosition() == Position.RELATIVE) { 393 this.currentYPosition -= area.getYPosition(); 394 this.currentAreaContainerXPosition += area.getXPosition(); 395 } 396 397 this.currentXPosition = this.currentAreaContainerXPosition; 398 int rx = this.currentAreaContainerXPosition; 399 int ry = this.currentYPosition; 400 int w = area.getAllocationWidth(); 401 int h = area.getMaxHeight(); 402 403 doBackground(area, rx, ry, w, h); 404 405 List children = area.getChildren(); 406 for (int i = 0; i < children.size(); i++) { 407 Box b = (Box)children.get(i); 408 b.render(this); } 410 411 if (area.getPosition() != Position.STATIC) { 412 this.currentYPosition = saveY; 413 this.currentAreaContainerXPosition = saveX; 414 } else { 415 this.currentYPosition -= area.getHeight(); 416 } 417 } 418 419 424 public void renderAreaContainer(AreaContainer area) { 425 426 int saveY = this.currentYPosition; 427 int saveX = this.currentAreaContainerXPosition; 428 429 if (area.getPosition() == Position.ABSOLUTE) { 430 this.currentYPosition = area.getYPosition(); 432 this.currentAreaContainerXPosition = area.getXPosition(); 433 } else if (area.getPosition() == Position.RELATIVE) { 434 this.currentYPosition -= area.getYPosition(); 435 this.currentAreaContainerXPosition += area.getXPosition(); 436 } else if (area.getPosition() == Position.STATIC) { 437 this.currentYPosition -= area.getPaddingTop() 438 + area.getBorderTopWidth(); 439 443 } 444 445 this.currentXPosition = this.currentAreaContainerXPosition; 446 doFrame(area); 447 448 List children = area.getChildren(); 449 for (int i = 0; i < children.size(); i++) { 450 Box b = (Box)children.get(i); 451 b.render(this); 452 } 453 this.currentYPosition = saveY; 455 this.currentAreaContainerXPosition = saveX; 456 if (area.getPosition() == Position.STATIC) { 457 this.currentYPosition -= area.getHeight(); 458 } 459 460 469 } 470 471 476 public void renderBlockArea(BlockArea area) { 477 this.currentYPosition -= (area.getPaddingTop() 480 + area.getBorderTopWidth()); 481 doFrame(area); 482 List children = area.getChildren(); 483 for (int i = 0; i < children.size(); i++) { 484 Box b = (Box)children.get(i); 485 b.render(this); 486 } 487 this.currentYPosition -= (area.getPaddingBottom() 488 + area.getBorderBottomWidth()); 489 } 490 491 496 public void renderLineArea(LineArea area) { 497 int rx = this.currentAreaContainerXPosition + area.getStartIndent(); 498 int ry = this.currentYPosition; 499 int w = area.getContentWidth(); 500 int h = area.getHeight(); 501 502 this.currentYPosition -= area.getPlacementOffset(); 503 this.currentXPosition = rx; 504 505 int bl = this.currentYPosition; 506 507 List children = area.getChildren(); 508 for (int i = 0; i < children.size(); i++) { 509 Box b = (Box)children.get(i); 510 if (b instanceof InlineArea) { 511 InlineArea ia = (InlineArea)b; 512 this.currentYPosition = ry - ia.getYOffset(); 513 } else { 514 this.currentYPosition = ry - area.getPlacementOffset(); 515 } 516 b.render(this); 517 } 518 519 this.currentYPosition = ry - h; 520 this.currentXPosition = rx; 521 } 522 523 528 public void renderRegions(Page page) { 529 page.getBody().render(this); 530 if (page.getBefore() != null) 531 page.getBefore().render(this); 532 if (page.getAfter() != null) 533 page.getAfter().render(this); 534 if (page.getStart() != null) 535 page.getStart().render(this); 536 if (page.getEnd() != null) 537 page.getEnd().render(this); 538 } 539 540 public IDReferences getIDReferences() { 541 return idReferences; 542 } 543 } 544 | Popular Tags |