Skip to content

Commit

Permalink
rectpacking: Fix micro layout for rectpacking including content align…
Browse files Browse the repository at this point in the history
…ment and label placement. (#997)

* rectpacking: Do node micro layout before and after layout.

Previously it was only done after because whitespace elimination might
change node sizes. However, if the node size cannot fit the label, we
need to do this before the layout too.

* rectpacking: Corrected comments for minsize processors.

* Fixes #989. Content alignment for rectpacking.

Translate inner content based on the real width and the width of the
parent.
  • Loading branch information
soerendomroes authored Feb 13, 2024
1 parent 0fa97fe commit c6f44e8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.eclipse.elk.core.alg.ILayoutProcessor;
import org.eclipse.elk.core.alg.LayoutProcessorConfiguration;
import org.eclipse.elk.core.math.ElkPadding;
import org.eclipse.elk.core.math.KVector;
import org.eclipse.elk.core.options.CoreOptions;
import org.eclipse.elk.core.util.BasicProgressMonitor;
import org.eclipse.elk.core.util.BoxLayoutProvider;
Expand Down Expand Up @@ -58,6 +59,11 @@ public void layout(final ElkNode layoutGraph, final IElkProgressMonitor progress
boolean tryBox = layoutGraph.getProperty(RectPackingOptions.TRYBOX);
List<ElkNode> rectangles = layoutGraph.getChildren();

// if requested, compute nodes's dimensions, place node labels, ports, port labels, etc.
if (!layoutGraph.getProperty(RectPackingOptions.OMIT_NODE_MICRO_LAYOUT)) {
NodeMicroLayout.forGraph(layoutGraph).execute();
}

// Check whether regions are stackable and do box layout instead.
boolean stackable = false;
if (tryBox && rectangles.size() >= 3) {
Expand Down Expand Up @@ -119,6 +125,20 @@ public void layout(final ElkNode layoutGraph, final IElkProgressMonitor progress
progressMonitor.logGraph(layoutGraph, slotIndex + "-Finished");
}
// elkjs-exclude-end

// Content alignment
double realWidth = 0;
double realHeight = 0;
for (ElkNode rect : rectangles) {
realWidth = Math.max(realWidth, rect.getX() + rect.getWidth());
realHeight = Math.max(realHeight, rect.getY() + rect.getHeight());
}

ElkUtil.translate(layoutGraph,
new KVector(
layoutGraph.getProperty(InternalProperties.DRAWING_WIDTH),
layoutGraph.getProperty(InternalProperties.DRAWING_HEIGHT)),
new KVector(realWidth, realHeight));

// Final touch.
applyPadding(rectangles, padding);
Expand All @@ -129,7 +149,7 @@ public void layout(final ElkNode layoutGraph, final IElkProgressMonitor progress
layoutGraph.getProperty(InternalProperties.DRAWING_HEIGHT) + padding.getVertical(), false, true);
}

// if requested, compute nodes's dimensions, place node labels, ports, port labels, etc.
// Do micro layout again since the whitspace elimination and other things might have changed node sizes.
if (!layoutGraph.getProperty(RectPackingOptions.OMIT_NODE_MICRO_LAYOUT)) {
NodeMicroLayout.forGraph(layoutGraph).execute();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import org.eclipse.elk.graph.ElkNode;

/**
* Sorts all child nodes by their desired position.
* Sets a target width at least as high as the minimum width of the parent
*
* <dl>
* <dt>Precondition:</dt>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import org.eclipse.elk.graph.ElkNode;

/**
* Sorts all child nodes by their desired position.
* Sets the minimum width and height of the parent since this influences decisions based on the aspect ratio.
*
* <dl>
* <dt>Precondition:</dt>
Expand Down

0 comments on commit c6f44e8

Please sign in to comment.