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);