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
|
/**
* ===========================================
* 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: InfiniteMajorAxisLayoutStep.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.renderer.border.RenderLength;
import org.jfree.layouting.renderer.model.BlockRenderBox;
import org.jfree.layouting.renderer.model.ComputedLayoutProperties;
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.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.page.LogicalPageBox;
import org.jfree.layouting.renderer.model.table.TableRowRenderBox;
import org.jfree.layouting.renderer.process.valign.BoxAlignContext;
import org.jfree.layouting.renderer.process.valign.InlineBlockAlignContext;
import org.jfree.layouting.renderer.process.valign.NodeAlignContext;
import org.jfree.layouting.renderer.process.valign.ReplacedContentAlignContext;
import org.jfree.layouting.renderer.process.valign.TextElementAlignContext;
import org.jfree.layouting.renderer.process.valign.VerticalAlignmentProcessor;
import org.pentaho.reporting.libraries.base.util.FastStack;
/**
* This process-step computes the vertical alignment and corrects the
* y-positions whereever needed.
* <p/>
* This will only work, if the minor-axis step has been executed.
* Executing this class eats 23% of the current layouting time.
*
* @author Thomas Morgner
*/
public class InfiniteMajorAxisLayoutStep
extends IterateVisualProcessStep
{
public static class ParagraphBreakState
{
private Object suspendItem;
private FastStack contexts;
private ParagraphRenderBox paragraph;
public ParagraphBreakState(final ParagraphRenderBox paragraph)
{
if (paragraph == null)
{
throw new NullPointerException();
}
this.paragraph = paragraph;
this.contexts = new FastStack();
}
public ParagraphRenderBox getParagraph()
{
return paragraph;
}
public Object getSuspendItem()
{
return suspendItem;
}
public void setSuspendItem(final Object suspendItem)
{
this.suspendItem = suspendItem;
}
public boolean isSuspended()
{
return suspendItem != null;
}
public BoxAlignContext getCurrentLine()
{
if (contexts.isEmpty())
{
return null;
}
return (BoxAlignContext) contexts.peek();
}
public void openContext (final BoxAlignContext context)
{
if (contexts.isEmpty() == false)
{
final BoxAlignContext boxAlignContext =
(BoxAlignContext) contexts.peek();
boxAlignContext.addChild(context);
}
contexts.push (context);
}
public BoxAlignContext closeContext()
{
return (BoxAlignContext) contexts.pop();
}
}
private ParagraphBreakState breakState;
private RenderBox continuedElement;
public InfiniteMajorAxisLayoutStep()
{
}
public void compute(final LogicalPageBox pageBox)
{
this.breakState = null;
this.continuedElement = null;
startProcessing(pageBox);
this.breakState = null;
this.continuedElement = null;
}
/**
* Continues processing. The renderbox must have a valid x-layout (that is: X,
* content-X1, content-X2 and Width)
*
* @param box
*/
public void continueComputation(final RenderBox box)
{
if (box.getContentAreaX2() == 0 || box.getWidth() == 0)
{
throw new IllegalStateException("Box must be layouted a bit ..");
}
this.breakState = null;
this.continuedElement = box;
startProcessing(box);
this.continuedElement = null;
}
protected boolean startBlockLevelBox(final RenderBox box)
{
if (box.isIgnorableForRendering())
{
return false;
}
// 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 (box != continuedElement)
{
computeYPosition(box);
// We have an valid y position now
// we do not recompute the y-position of the continued element yet as we
// would not have all context-information here (and it had been done in
// the calling step anyway)
}
if (breakState == null)
{
if (box instanceof ParagraphRenderBox)
{
final ParagraphRenderBox paragraphBox = (ParagraphRenderBox) box;
// We cant cache that ... the shift operations later would misbehave
// One way around would be to at least store the layouted offsets
// (which should be immutable as long as the line did not change its
// contents) and to reapply them on each run. This is cheaper than
// having to compute the whole v-align for the whole line.
breakState = new ParagraphBreakState(paragraphBox);
}
return true;
}
// No breakstate and not being suspended? Why this?
if (breakState.isSuspended() == false)
{
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;
}
private void computeYPosition(final RenderNode node)
{
final long marginTop = node.getEffectiveMarginTop();
// The y-position of a box depends on the parent.
final RenderBox parent = node.getParent();
// A table row is something special. Although it is a block box,
// it layouts its children from left to right
if (parent instanceof TableRowRenderBox)
{
// Node is a table-cell ..
node.setDirty(true);
node.setY(parent.getY());
}
// If the box's parent is a block box ..
else if (parent instanceof BlockRenderBox)
{
final RenderNode prev = node.getVisiblePrev();
if (prev != null)
{
// we have a silbling. Position yourself directly below your silbling ..
node.setDirty(true);
node.setY(marginTop + prev.getY() + prev.getHeight());
}
else
{
final ComputedLayoutProperties blp = parent.getComputedLayoutProperties();
final long insetTop = (blp.getBorderTop() + blp.getPaddingTop());
node.setDirty(true);
node.setY(marginTop + insetTop + parent.getY());
}
}
// The parent is a inline box.
else if (parent != null)
{
final ComputedLayoutProperties blp = parent.getComputedLayoutProperties();
final long insetTop = (blp.getBorderTop() + blp.getPaddingTop());
node.setDirty(true);
node.setY(marginTop + insetTop + parent.getY());
}
else
{
// there's no parent ..
node.setDirty(true);
node.setY(marginTop);
}
}
protected void finishBlockLevelBox(final RenderBox box)
{
// Check the height. Set the height.
final ComputedLayoutProperties clp = box.getComputedLayoutProperties();
final RenderLength computedWidth = clp.getComputedWidth();
final RenderLength preferredHeight = box.getBoxDefinition().getPreferredHeight();
final long computedHeight =
preferredHeight.resolve(computedWidth.resolve(0));
final ComputedLayoutProperties blp = box.getComputedLayoutProperties();
final long insetBottom = blp.getBorderBottom() + blp.getPaddingBottom();
final RenderNode lastChildNode = box.getLastChild();
if (lastChildNode != null)
{
// grab the node's y2
final long childY2 = lastChildNode.getY() + lastChildNode.getHeight() +
lastChildNode.getEffectiveMarginBottom();
final long effectiveHeight = (childY2 - box.getY()) + insetBottom;
final long height = Math.max(effectiveHeight, computedHeight);
box.setHeight(height);
}
else
{
final long insetTop = blp.getBorderTop() + blp.getBorderTop();
box.setHeight(Math.max(computedHeight, insetTop + insetBottom));
}
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.setMajorLayoutAge(paraBox.getLineboxContainer().getChangeTracker());
breakState = null;
}
}
}
protected boolean startInlineLevelBox(final RenderBox box)
{
// todo: Inline level boxes may have margins ...
computeYPosition(box);
computeBaselineInfo(box);
if (breakState == null)
{
// ignore .. should not happen anyway ..
return true;
}
if (breakState.isSuspended())
{
return false;
}
if (box instanceof InlineRenderBox)
{
breakState.openContext(new BoxAlignContext(box));
return true;
}
breakState.getCurrentLine().addChild(new InlineBlockAlignContext(box));
breakState.setSuspendItem(box.getInstanceId());
return false;
}
private void computeBaselineInfo(final RenderBox box)
{
RenderNode node = box.getVisibleFirst();
while (node != null)
{
if (node instanceof RenderableText)
{
// grab the baseline info from there ...
final RenderableText text = (RenderableText) node;
box.setBaselineInfo(text.getBaselineInfo());
break;
}
node = node.getVisibleNext();
}
if (box.getBaselineInfo() == null)
{
// If we have no baseline info here, ask the parent. If that one has none
// either, then we cant do anything about it.
box.setBaselineInfo(box.getNominalBaselineInfo());
}
}
protected void finishInlineLevelBox(final RenderBox box)
{
if (breakState == null)
{
return;
}
if (box instanceof InlineRenderBox)
{
breakState.closeContext();
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)
{
computeYPosition(node);
if (breakState == null || breakState.isSuspended())
{
return;
}
if (node instanceof RenderableText)
{
breakState.getCurrentLine().addChild
(new TextElementAlignContext((RenderableText) node));
}
else if (node instanceof RenderableReplacedContent)
{
breakState.getCurrentLine().addChild
(new ReplacedContentAlignContext((RenderableReplacedContent) node));
}
else if (node instanceof SpacerRenderNode)
{
breakState.getCurrentLine().addChild(new NodeAlignContext(node));
}
else
{
breakState.getCurrentLine().addChild(new NodeAlignContext(node));
}
}
protected void processBlockLevelNode(final RenderNode node)
{
// This could be anything, text, or an image.
computeYPosition(node);
if (node instanceof FinishedRenderNode)
{
final FinishedRenderNode fnode = (FinishedRenderNode) node;
node.setHeight(fnode.getLayoutedHeight());
}
// Tables can have spacer nodes in weird positions. Actually it is less
// expensive to filter them here than to kill them earlier.
// Heck, given infinite time and resources, I will filter them earlier ..
// throw new IllegalStateException
// ("Block Level nodes are somewhat illegal: " + node);
// node.setHeight(node.getNodeLayoutProperties().getMaximumBoxHeight());
}
protected void processParagraphChilds(final ParagraphRenderBox box)
{
// Process the direct childs of the paragraph
// Each direct child represents a line ..
RenderNode node = box.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("Encountered " + node.getClass());
}
final ParagraphPoolBox inlineRenderBox = (ParagraphPoolBox) node;
startLine(inlineRenderBox);
processBoxChilds(inlineRenderBox);
finishLine(inlineRenderBox);
node = node.getVisibleNext();
}
}
protected void startLine(final ParagraphPoolBox box)
{
computeYPosition(box);
if (breakState == null)
{
return;
}
if (breakState.isSuspended())
{
return;
}
breakState.openContext(new BoxAlignContext(box));
}
protected void finishLine(final ParagraphPoolBox inlineRenderBox)
{
if (breakState == null || breakState.isSuspended())
{
return;
}
final BoxAlignContext boxAlignContext = breakState.closeContext();
// 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 VerticalAlignmentProcessor processor = new VerticalAlignmentProcessor();
final ComputedLayoutProperties blp = inlineRenderBox.getComputedLayoutProperties();
final long insetTop = (blp.getBorderTop() + blp.getPaddingTop());
final long contentAreaY1 = inlineRenderBox.getY() + insetTop;
final RenderLength lineHeight = inlineRenderBox.getLineHeight();
final ComputedLayoutProperties clp = inlineRenderBox.getComputedLayoutProperties();
final RenderLength bcw = clp.getBlockContextWidth();
processor.align (boxAlignContext, contentAreaY1,
lineHeight.resolve(bcw.resolve(0)));
}
protected void finishOtherBox(final RenderBox box)
{
if (breakState != null)
{
final Object suspender = breakState.getSuspendItem();
if (box.getInstanceId() == suspender)
{
breakState.setSuspendItem(null);
}
}
}
}
|