morganthomas / balance.js

WIP. Document layout optimization engine. Successor to Yoga.
Other
1 stars 0 forks source link

Simplify line packing algorithm #4

Open morganthomas opened 7 years ago

morganthomas commented 7 years ago

The current line packing algorithm lets you lay out a series of velements with arbitrarily complex layout problems. Simplify this by having three categories of boxes: rigid, elastic, and fill. The following rules apply.

Corresponding changes will be required to the paragraph velement.

morganthomas commented 7 years ago

Here's a draft of the new line packing algorithm.

Take boxes and their optimal lengths, considering a fill box to have length 0, until you have more than the line length. You have up to three breakpoints to consider: the box the line length falls inside in this lineup, and the boxes right before and after that box.

Compute the badness and optimal layouts of each of these possible lines. Throw out any non-feasible ones. Update your list of threads accordingly. Prune the threads if necessary. Continue until all your threads are complete, and select the best one.

The badness of a line with no fill box is simply:

|elastic box stretch/shrink ratio - 1|

The badness of a line with a fill box is always zero.

morganthomas commented 7 years ago

Here's how to compute the stretch/shrink ratio in a line with no fill box.

morganthomas commented 7 years ago

The badness of a solution is the average of the badnesses of the lines (not the sum).

morganthomas commented 7 years ago

Actually I'm not sure what difference it makes whether you use the average or the sum. Let's use the sum for now, as the simplest option.