}
}
// now, build the line and update the array ..
final ArrayList pendingElements = new ArrayList();
final FastStack contexts = new FastStack();
RenderBox firstBox = null;
RenderBox box = null;
for (int i = 0; i < lastPosition; i++)
{
final InlineSequenceElement element = sequenceElements[i];
if (element instanceof EndSequenceElement)
{
contexts.pop();
final long boxX2 = (elementPositions[i] + elementDimensions[i]);
//noinspection ConstantConditions
box.setWidth(boxX2 - box.getX());
if (contexts.isEmpty())
{
box = null;
}
else
{
final RenderNode node = box;
box = (RenderBox) contexts.peek();
box.addGeneratedChild(node);
}
continue;
}
if (element instanceof StartSequenceElement)
{
final RenderNode node = element.getNode();
box = (RenderBox) node.derive(false);
box.setX(elementPositions[i]);
contexts.push(box);
if (firstBox == null)
{
firstBox = box;
}
continue;
}
if (box == null)
{
throw new IllegalStateException("Invalid sequence: " +
"Cannot have elements before we open the box context.");
}
// Content element: Perform a deep-derive, so that we preserve the
// possibly existing sub-nodes.
final RenderNode child = element.getNode().derive(true);
child.setX(elementPositions[i]);
child.setWidth(elementDimensions[i]);
if (box.isPreserveSpace() != false)
{
box.addGeneratedChild(child);
continue;
}
if (child.isIgnorableForRendering())
{
pendingElements.add(child);
}
else
{
for (int j = 0; j < pendingElements.size(); j++)
{
final RenderNode node = (RenderNode) pendingElements.get(j);
box.addGeneratedChild(node);
}
pendingElements.clear();
box.addGeneratedChild(child);
}
}
// Remove all spacers and other non printable content that might
// look ugly at the beginning of a new line ..
for (; lastPosition < sequenceFill; lastPosition++)
{
final RenderNode node = sequenceElements[lastPosition].getNode();
if (node.isDiscardable() == false)
{
break;
}
}
// If there are open contexts, then add the split-result to the new line
// and update the width of the current line
final int openContexts = contexts.size();
for (int i = 0; i < openContexts; i++)
{
final RenderBox renderBox = (RenderBox) contexts.get(i);
renderBox.setWidth(getEndOfLine() - box.getX());
final InlineRenderBox rightBox = (InlineRenderBox)
renderBox.split(RenderNode.HORIZONTAL_AXIS);
sequenceElements[i] = new StartSequenceElement(rightBox);