balance static method

  1. @visibleForTesting
({double height, List<int> starts}) balance(
  1. List<double> heights,
  2. List<double> spaces,
  3. double end,
  4. int n, {
  5. List<bool>? lines = const [],
  6. int orphans = 2,
  7. int widows = 2,
})

heights set in at most n columns, balanced (see MultiColumn): how tall the columns are, and the first piece of each. lines is null where every piece is a line of one paragraph.

Implementation

@visibleForTesting
static ({double height, List<int> starts}) balance(
  List<double> heights,
  List<double> spaces,
  double end,
  int n, {
  List<bool>? lines = const [],
  int orphans = 2,
  int widows = 2,
}) {
  if (heights.isEmpty) return (height: 0.0, starts: const <int>[]);
  final plan = _Plan(heights, spaces, lines, math.max(1, orphans), math.max(1, widows));
  var total = math.max(0.0, end), tallest = 0.0;
  for (var i = 0; i < heights.length; i++) {
    total += plan.space(i) + heights[i];
    tallest = math.max(tallest, heights[i]);
  }
  var height = math.max(total / math.max(1, n), tallest);
  // Each pass keeps at least one more piece up in a column it broke
  // before, so the passes are bounded by the pieces; the bound is only a
  // guard against a height that does not grow by a representable step.
  for (var pass = 0; pass <= heights.length + 1; pass++) {
    final (starts, short) = plan.fill(height);
    if (starts.length <= n || !short.isFinite) return (height: height, starts: starts);
    height += short;
  }
  return (height: height, starts: plan.fill(height).$1);
}