1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
|
/**
* ===========================================
* LibLayout : a free Java layouting library
* ===========================================
*
* Project Info: http://reporting.pentaho.org/liblayout/
*
* (C) Copyright 2006-2007, by Pentaho Corporation and Contributors.
*
* This library is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
* either version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* ------------
* $Id: InfiniteMinorAxisLayoutStep.java 6489 2008-11-28 14:53:40Z tmorgner $
* ------------
* (C) Copyright 2006-2007, by Pentaho Corporation.
*/
package org.jfree.layouting.renderer.process;
import org.jfree.layouting.input.style.keys.text.TextAlign;
import org.jfree.layouting.input.style.values.CSSValue;
import org.jfree.layouting.renderer.border.RenderLength;
import org.jfree.layouting.renderer.model.BlockRenderBox;
import org.jfree.layouting.renderer.model.FinishedRenderNode;
import org.jfree.layouting.renderer.model.InlineRenderBox;
import org.jfree.layouting.renderer.model.ParagraphPoolBox;
import org.jfree.layouting.renderer.model.ParagraphRenderBox;
import org.jfree.layouting.renderer.model.PlaceholderRenderNode;
import org.jfree.layouting.renderer.model.RenderBox;
import org.jfree.layouting.renderer.model.RenderNode;
import org.jfree.layouting.renderer.model.RenderableReplacedContent;
import org.jfree.layouting.renderer.model.RenderableText;
import org.jfree.layouting.renderer.model.SpacerRenderNode;
import org.jfree.layouting.renderer.model.StaticBoxLayoutProperties;
import org.jfree.layouting.renderer.model.ComputedLayoutProperties;
import org.jfree.layouting.renderer.model.page.LogicalPageBox;
import org.jfree.layouting.renderer.model.page.PageGrid;
import org.jfree.layouting.renderer.model.table.TableCellRenderBox;
import org.jfree.layouting.renderer.model.table.TableRenderBox;
import org.jfree.layouting.renderer.model.table.cols.TableColumn;
import org.jfree.layouting.renderer.model.table.cols.TableColumnModel;
import org.jfree.layouting.renderer.process.layoutrules.EndSequenceElement;
import org.jfree.layouting.renderer.process.layoutrules.InlineBoxSequenceElement;
import org.jfree.layouting.renderer.process.layoutrules.InlineNodeSequenceElement;
import org.jfree.layouting.renderer.process.layoutrules.InlineSequenceElement;
import org.jfree.layouting.renderer.process.layoutrules.ReplacedContentSequenceElement;
import org.jfree.layouting.renderer.process.layoutrules.SpacerSequenceElement;
import org.jfree.layouting.renderer.process.layoutrules.StartSequenceElement;
import org.jfree.layouting.renderer.process.layoutrules.TextSequenceElement;
import org.pentaho.reporting.libraries.base.util.DebugLog;
/**
* This process-step computes the effective layout, but it does not take
* horizontal pagebreaks into account. (It has to deal with vertical breaks, as
* they affect the text layout.)
* <p/>
* This processing step does not ajust anything on the vertical axis. Vertical
* alignment is handled in a second step.
*
* @author Thomas Morgner
*/
public class InfiniteMinorAxisLayoutStep
extends IterateVisualProcessStep
{
private ParagraphBreakState breakState;
private PageGrid pageGrid;
private RenderBox continuedElement;
public InfiniteMinorAxisLayoutStep()
{
}
public void compute(final LogicalPageBox root)
{
breakState = null;
pageGrid = root.getPageGrid();
startProcessing(root);
pageGrid = null;
breakState = null;
}
/**
* Continues processing. The renderbox must have a valid x-layout (that is: X,
* content-X1, content-X2 and Width)
*
* @param parent
* @param box
*/
public void continueComputation(final PageGrid pageGrid,
final RenderBox box)
{
if (box.getContentAreaX2() == 0 || box.getWidth() == 0)
{
throw new IllegalStateException("Box must be layouted a bit ..");
}
this.pageGrid = pageGrid;
this.breakState = null;
this.continuedElement = box;
startProcessing(box);
this.continuedElement = null;
this.pageGrid = null;
this.breakState = null;
}
/**
* The whole computation is only done for exactly one nesting level of
* paragraphs. If we encounter an inline-block or inline-table, we handle them
* as a single element.
*
* @param box
* @return
*/
protected boolean startBlockLevelBox(final RenderBox box)
{
// first, compute the position. The position is global, not relative to a
// parent or so. Therefore a child has no connection to the parent's
// effective position, when it is painted.
if (breakState == null)
{
if (box instanceof ParagraphRenderBox)
{
computeContentArea(box);
final ParagraphRenderBox paragraphBox = (ParagraphRenderBox) box;
if (continuedElement == null)
{
final BlockRenderBox lineboxContainer = paragraphBox.getLineboxContainer();
final boolean unchanged =
lineboxContainer.getChangeTracker() == paragraphBox.getMinorLayoutAge();
if (unchanged)
{
return false;
}
}
paragraphBox.clearLayout();
breakState = new ParagraphBreakState(paragraphBox);
}
else if (box instanceof TableCellRenderBox)
{
// todo: Cannot be done unless the inner paragraphs have been layouted once ..
computeCellArea((TableCellRenderBox) box);
}
else
{
computeContentArea(box);
if (box instanceof TableRenderBox)
{
computeCellPositions((TableRenderBox) box);
}
}
return true;
}
if (breakState.isSuspended() == false)
{
// The break-state exists only while we are inside of an paragraph
// and suspend can only happen on inline elements.
// A block-element inside a paragraph cannot be (and if it does, it is
// a bug)
throw new IllegalStateException("This cannot be.");
}
// this way or another - we are suspended now. So there is no need to look
// at the children anymore ..
return false;
}
protected void finishBlockLevelBox(final RenderBox box)
{
// Todo: maybe it would be very wise if we dont extend the box later on.
// Heck, lets see whether this causes great pain ...
verifyContentWidth(box);
if (breakState != null)
{
final Object suspender = breakState.getSuspendItem();
if (box.getInstanceId() == suspender)
{
breakState.setSuspendItem(null);
return;
}
if (suspender != null)
{
return;
}
if (box instanceof ParagraphRenderBox)
{
// finally update the change tracker ..
final ParagraphRenderBox paraBox = (ParagraphRenderBox) box;
paraBox.setMinorLayoutAge(paraBox.getLineboxContainer().getChangeTracker());
breakState = null;
}
}
}
private void computeCellPositions(final TableRenderBox tableBox)
{
// Todo: Compute the effective cell positions ...
// In a perfect world, we would start to take pagebreaks into account
// and would try to distribute the table columns in a way, so that they
// do not cross inner page boundaries.
// But for the first release, our world is not perfect!
final TableColumnModel model = tableBox.getColumnModel();
long position = 0;
final int columnCount = model.getColumnCount();
for (int i = 0; i < columnCount; i++)
{
final TableColumn column = model.getColumn(i);
column.setEffectiveCellPosition(position);
position += column.getEffectiveSize() + model.getBorderSpacing();
}
}
private void computeCellArea(final TableCellRenderBox cellRenderBox)
{
// This is slightly different for table cells ...
final int columnIndex = cellRenderBox.getColumnIndex();
final TableRenderBox table = cellRenderBox.getTable();
final TableColumnModel columnModel = table.getColumnModel();
final TableColumn column = columnModel.getColumn(columnIndex);
long effectiveSize = column.getEffectiveSize();
final int colSpan = cellRenderBox.getColSpan();
if (colSpan > 1)
{
for (int i = 1; i < colSpan; i++)
{
final TableColumn spannedColumn = columnModel.getColumn(columnIndex + i);
effectiveSize += spannedColumn.getEffectiveSize();
effectiveSize += columnModel.getBorderSpacing();
}
}
final long effectiveCellPosition = column.getEffectiveCellPosition();
cellRenderBox.setX(effectiveCellPosition);
cellRenderBox.setWidth(effectiveSize);
final ComputedLayoutProperties blp = cellRenderBox.getComputedLayoutProperties();
// next, compute the width ...
long leftPadding = blp.getBorderLeft();
leftPadding += blp.getPaddingLeft();
cellRenderBox.setContentAreaX1(effectiveCellPosition + leftPadding);
long rightPadding = blp.getBorderRight();
rightPadding += blp.getPaddingRight();
final long contentWidth = effectiveSize - rightPadding;
cellRenderBox.setContentAreaX2(effectiveCellPosition + contentWidth);
}
/**
* Computes the effective content area. The content area is the space that can
* be used by any of the childs of the given box.
* <p/>
* InlineBoxes get computed in the alignment processor.
*
* @param box the block render box for which we compute the content area
*/
private void computeContentArea(final RenderBox box)
{
if (box == continuedElement)
{
return;
}
final ComputedLayoutProperties blp = box.getComputedLayoutProperties();
final long x = computeX(box) + blp.getMarginLeft();
box.setX(x);
// next, compute the width ...
long leftPadding = blp.getBorderLeft();
leftPadding += blp.getPaddingLeft();
box.setContentAreaX1(x + leftPadding);
final RenderLength computedWidth =
box.getComputedLayoutProperties().getComputedWidth();
if (computedWidth == RenderLength.AUTO)
{
final RenderBox parent = box.getParent();
if (parent instanceof BlockRenderBox)
{
long rightPadding = blp.getMarginRight();
rightPadding += blp.getBorderRight();
rightPadding += blp.getPaddingRight();
final BlockRenderBox blockParent = (BlockRenderBox) parent;
box.setContentAreaX2(blockParent.getContentAreaX2() - rightPadding);
}
else
{
// A block level element that sits inside an inline element
box.setContentAreaX2(x + leftPadding + box.getMinimumChunkWidth());
}
}
else
{
final long contentWidth = computedWidth.resolve(0);
box.setContentAreaX2(x + leftPadding + contentWidth);
}
}
private long computeX(final RenderNode node)
{
final RenderBox parent = node.getParent();
if (parent == null)
{
return 0;
}
return parent.getContentAreaX1();
}
/**
* Verifies the content width and produces the effective box width.
*
* @param box
*/
private void verifyContentWidth(final RenderBox box)
{
final long x = box.getX();
final long contentEnd = box.getContentAreaX2();
final ComputedLayoutProperties blp = box.getComputedLayoutProperties();
final long boxEnd = contentEnd + blp.getBorderRight() + blp.getPaddingRight();
box.setWidth(boxEnd - x);
}
protected boolean startInlineLevelBox(final RenderBox box)
{
if (breakState == null)
{
// ignore .. should not happen anyway ..
return true;
}
if (breakState.isSuspended())
{
return false;
}
if (box instanceof InlineRenderBox)
{
breakState.add(new StartSequenceElement((InlineRenderBox) box));
return true;
}
computeContentArea(box);
breakState.add(new InlineBoxSequenceElement(box));
breakState.setSuspendItem(box.getInstanceId());
return false;
}
protected void finishInlineLevelBox(final RenderBox box)
{
if (breakState == null)
{
return;
}
if (breakState.getSuspendItem() == box.getInstanceId())
{
// stop being suspended.
breakState.setSuspendItem(null);
return;
}
if (box instanceof InlineRenderBox)
{
breakState.add(new EndSequenceElement((InlineRenderBox) box));
return;
}
final Object suspender = breakState.getSuspendItem();
if (box.getInstanceId() == suspender)
{
breakState.setSuspendItem(null);
return;
}
if (suspender != null)
{
return;
}
if (box instanceof ParagraphRenderBox)
{
throw new IllegalStateException("This cannot be.");
}
}
protected void processInlineLevelNode(final RenderNode node)
{
if (breakState == null || breakState.isSuspended())
{
return;
}
if (node instanceof FinishedRenderNode)
{
final FinishedRenderNode finNode = (FinishedRenderNode) node;
node.setWidth(finNode.getLayoutedWidth());
}
if (node instanceof RenderableText)
{
breakState.add(new TextSequenceElement((RenderableText) node));
}
else if (node instanceof RenderableReplacedContent)
{
breakState.add
(new ReplacedContentSequenceElement((RenderableReplacedContent) node));
}
else if (node instanceof PlaceholderRenderNode)
{
breakState.add(new InlineNodeSequenceElement(node));
}
else if (node instanceof SpacerRenderNode)
{
if (breakState.isContainsContent())
{
breakState.add(new SpacerSequenceElement((SpacerRenderNode) node));
}
}
else
{
breakState.add(new InlineNodeSequenceElement(node));
}
}
protected void processBlockLevelNode(final RenderNode node)
{
// This could be anything, text, or an image.
final long x = computeX(node);
node.setX(x);
if (node instanceof FinishedRenderNode)
{
final FinishedRenderNode finNode = (FinishedRenderNode) node;
node.setWidth(finNode.getLayoutedWidth());
}
else
{
node.setWidth
(node.getComputedLayoutProperties().getComputedWidth().resolve(0));
}
}
protected void processParagraphChilds(final ParagraphRenderBox box)
{
final BlockRenderBox lineboxContainer = box.getLineboxContainer();
RenderNode node = lineboxContainer.getVisibleFirst();
while (node != null)
{
// all childs of the linebox container must be inline boxes. They
// represent the lines in the paragraph. Any other element here is
// a error that must be reported
if (node instanceof ParagraphPoolBox == false)
{
throw new IllegalStateException("Expected ParagraphPoolBox elements.");
}
final InlineRenderBox inlineRenderBox = (InlineRenderBox) node;
startLine(inlineRenderBox);
processBoxChilds(inlineRenderBox);
finishLine(inlineRenderBox);
node = node.getVisibleNext();
}
}
protected void startLine(final InlineRenderBox inlineRenderBox)
{
if (breakState == null)
{
return;
}
if (breakState.isSuspended())
{
return;
}
breakState.clear();
breakState.add(new StartSequenceElement(inlineRenderBox));
}
protected void finishLine(final InlineRenderBox inlineRenderBox)
{
if (breakState == null || breakState.isSuspended())
{
return;
}
breakState.add(new EndSequenceElement(inlineRenderBox));
final ParagraphRenderBox paragraph = breakState.getParagraph();
final CSSValue lastLineAlignment = paragraph.getLastLineAlignment();
final CSSValue textAlignment = paragraph.getTextAlignment();
// Todo: Start the layouting on primary sequence ...
// This aligns all direct childs. Once that is finished, we have to
// check, whether possibly existing inner-paragraphs are still valid
// or whether moving them violated any of the inner-pagebreak constraints.
final TextAlignmentProcessor processor = create(textAlignment, lastLineAlignment);
final InlineSequenceElement[] sequence = breakState.getSequence();
if (sequence.length == 0)
{
return;
}
final long lineStart = paragraph.getContentAreaX1();
final long lineEnd = paragraph.getContentAreaX2();
if (lineEnd - lineStart <= 0)
{
final long minimumChunkWidth = paragraph.getMinimumChunkWidth();
processor.initialize(sequence,
lineStart, lineStart + minimumChunkWidth, pageGrid);
DebugLog.log("Auto-Corrected zero-width linebox.");
}
else
{
processor.initialize(sequence, lineStart, lineEnd, pageGrid);
}
while (processor.hasNext())
{
final RenderNode linebox = processor.next();
if (linebox instanceof ParagraphPoolBox == false)
{
throw new NullPointerException("Line must not be null");
}
paragraph.addGeneratedChild(linebox);
}
}
protected TextAlignmentProcessor create(final CSSValue alignment,
final CSSValue lastLine)
{
if (TextAlign.CENTER.equals(alignment))
{
return new CenterAlignmentProcessor();
}
else if (TextAlign.RIGHT.equals(alignment))
{
return new RightAlignmentProcessor();
}
return new LeftAlignmentProcessor();
}
}
|