0.2.0-dev Download ZIP
661 lines ·23.5 KB Raw
| 1 | namespace EvilEngine.Html; |
| 2 | |
| 3 | internal sealed partial class Flow |
| 4 | { |
| 5 | private static Grid BuildGrid(TableBox table, double spacing) |
| 6 | { |
| 7 | var grid = new Grid(spacing); |
| 8 | var columns = 0; |
| 9 | foreach (var group in table.Groups) |
| 10 | { |
| 11 | if (group.Rows.Count == 0) |
| 12 | continue; |
| 13 | var start = grid.Rows.Count; |
| 14 | var end = start + group.Rows.Count; |
| 15 | var taken = new HashSet<(int, int)>(); |
| 16 | for (var row = start; row < end; row++) |
| 17 | { |
| 18 | grid.Rows.Add(group.Rows[row - start]); |
| 19 | var column = 0; |
| 20 | foreach (var cell in group.Rows[row - start].Cells) |
| 21 | { |
| 22 | while (taken.Contains((row, column))) |
| 23 | column++; |
| 24 | var rowSpan = cell.RowSpan == 0 || row + cell.RowSpan > end ? end - row : cell.RowSpan; |
| 25 | for (var spanned = row; spanned < row + rowSpan; spanned++) |
| 26 | for (var across = column; across < column + cell.ColSpan; across++) |
| 27 | taken.Add((spanned, across)); |
| 28 | grid.Slots.Add(new Slot(cell, row, column, rowSpan, cell.ColSpan)); |
| 29 | column += cell.ColSpan; |
| 30 | columns = Math.Max(columns, column); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | grid.Groups.Add((start, end)); |
| 35 | } |
| 36 | |
| 37 | grid.Columns = [.. Enumerable.Range(0, columns).Select(_ => new Column())]; |
| 38 | foreach (var slot in grid.Slots) |
| 39 | grid.Columns[slot.Col].Originates = true; |
| 40 | return grid; |
| 41 | } |
| 42 | |
| 43 | private CellInfo Info(TableCell cell) |
| 44 | { |
| 45 | var style = cell.Box.Style; |
| 46 | var (min, pref) = Intrinsic(cell.Box); |
| 47 | var (percent, specified) = (0.0, false); |
| 48 | if (style.Width is { Percent: false } width) |
| 49 | { |
| 50 | specified = true; |
| 51 | pref = Math.Max(width.Value, min); |
| 52 | } |
| 53 | else if (style.Width is { Percent: true } share) |
| 54 | { |
| 55 | percent = share.Value / 100; |
| 56 | } |
| 57 | |
| 58 | var edges = style.Padding.Left + style.Padding.Right + style.Border.Left + style.Border.Right; |
| 59 | return new CellInfo(min + edges, pref + edges, percent, specified); |
| 60 | } |
| 61 | |
| 62 | private void ColumnIntrinsics(Grid grid) |
| 63 | { |
| 64 | var infos = grid.Slots.ToDictionary(slot => slot, slot => Info(slot.Cell)); |
| 65 | foreach (var slot in grid.Slots.Where(slot => slot.ColSpan == 1)) |
| 66 | { |
| 67 | var info = infos[slot]; |
| 68 | var column = grid.Columns[slot.Col]; |
| 69 | column.AddCoords(info.Min, info.Pref, info.Specified); |
| 70 | column.Percent = Math.Max(column.Percent, info.Percent); |
| 71 | } |
| 72 | |
| 73 | foreach (var span in grid.Slots.Where(slot => slot.ColSpan > 1).GroupBy(slot => slot.ColSpan) |
| 74 | .OrderBy(span => span.Key)) |
| 75 | { |
| 76 | foreach (var slot in span) |
| 77 | { |
| 78 | var info = infos[slot]; |
| 79 | if (info.Percent > 0) |
| 80 | DistributePercent(grid, info.Percent, slot.Col, slot.ColSpan); |
| 81 | Distribute(grid, info.Min, slot.Col, slot.ColSpan, Target.Min, info.Specified); |
| 82 | Distribute(grid, info.Pref, slot.Col, slot.ColSpan, Target.Pref, info.Specified); |
| 83 | } |
| 84 | |
| 85 | foreach (var column in grid.Columns) |
| 86 | column.AccumulateSpan(); |
| 87 | } |
| 88 | |
| 89 | var used = 0.0; |
| 90 | foreach (var column in grid.Columns) |
| 91 | { |
| 92 | column.Percent = Math.Min(column.Percent, 1 - used); |
| 93 | used += column.Percent; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | private static (double Min, double Pref, double PrefExpand) ColumnTotals(Grid grid) |
| 98 | { |
| 99 | double min = 0, pref = 0, smallPercentPref = 0, nonPercentPref = 0, percentTotal = 0; |
| 100 | var add = grid.Spacing; |
| 101 | for (var index = 0; index < grid.Columns.Length; index++) |
| 102 | { |
| 103 | var column = grid.Columns[index]; |
| 104 | if (grid.SpacingBefore(index)) |
| 105 | add += grid.Spacing; |
| 106 | min += column.Min; |
| 107 | pref += column.Pref; |
| 108 | if (column.Percent > 0) |
| 109 | { |
| 110 | smallPercentPref = Math.Max(smallPercentPref, column.Pref / column.Percent); |
| 111 | percentTotal += column.Percent; |
| 112 | } |
| 113 | else |
| 114 | { |
| 115 | nonPercentPref += column.Pref; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | var expand = Math.Max(pref, smallPercentPref); |
| 120 | if (percentTotal >= 1 - 1e-6) |
| 121 | { |
| 122 | if (nonPercentPref > 0) |
| 123 | expand = double.PositiveInfinity; |
| 124 | } |
| 125 | else |
| 126 | { |
| 127 | expand = Math.Max(expand, nonPercentPref / (1 - percentTotal)); |
| 128 | } |
| 129 | |
| 130 | if (grid.Columns.Length > 0) |
| 131 | { |
| 132 | min += add; |
| 133 | pref += add; |
| 134 | expand += add; |
| 135 | } |
| 136 | |
| 137 | return (min, pref, expand); |
| 138 | } |
| 139 | |
| 140 | private static void DistributePercent(Grid grid, double percent, int first, int count) |
| 141 | { |
| 142 | var (nonPercentCount, nonPercentPref) = (0, 0.0); |
| 143 | for (var index = first; index < first + count; index++) |
| 144 | { |
| 145 | var column = grid.Columns[index]; |
| 146 | if (column.Percent == 0) |
| 147 | { |
| 148 | nonPercentPref += column.Pref; |
| 149 | if (column.Originates) |
| 150 | nonPercentCount++; |
| 151 | } |
| 152 | else |
| 153 | { |
| 154 | percent -= column.Percent; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | if (percent <= 0 || nonPercentCount == 0) |
| 159 | return; |
| 160 | var byPref = nonPercentPref > 0; |
| 161 | for (var index = first; index < first + count; index++) |
| 162 | { |
| 163 | var column = grid.Columns[index]; |
| 164 | if (column.Percent != 0) |
| 165 | continue; |
| 166 | var allocated = byPref ? percent * (column.Pref / nonPercentPref) |
| 167 | : column.Originates ? percent / nonPercentCount |
| 168 | : 0; |
| 169 | column.SpanPercent = Math.Max(column.SpanPercent, allocated); |
| 170 | percent -= allocated; |
| 171 | nonPercentPref -= column.Pref; |
| 172 | if (column.Originates) |
| 173 | nonPercentCount--; |
| 174 | if (percent <= 1e-9) |
| 175 | return; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | private static double Au(double value) |
| 180 | { |
| 181 | return Math.Round(value * 60, MidpointRounding.AwayFromZero) / 60; |
| 182 | } |
| 183 | |
| 184 | private static double AuFloor(double value) |
| 185 | { |
| 186 | return Math.Floor(value * 60 + 1e-6) / 60; |
| 187 | } |
| 188 | |
| 189 | private static void Distribute(Grid grid, double size, int first, int count, Target target, bool spanSpecified) |
| 190 | { |
| 191 | for (var index = first + 1; index < first + count; index++) |
| 192 | if (grid.SpacingBefore(index)) |
| 193 | size -= grid.Spacing; |
| 194 | if (target == Target.Final) |
| 195 | size -= 2 * grid.Spacing; |
| 196 | |
| 197 | double guessMin = 0, |
| 198 | guessMinPct = 0, |
| 199 | guessMinSpec = 0, |
| 200 | guessPref = 0, |
| 201 | flexPref = 0, |
| 202 | fixedPref = 0, |
| 203 | percentTotal = 0; |
| 204 | var zeroColumns = 0; |
| 205 | for (var index = first; index < first + count; index++) |
| 206 | { |
| 207 | var column = grid.Columns[index]; |
| 208 | guessMin += column.Min; |
| 209 | if (column.Percent != 0) |
| 210 | { |
| 211 | percentTotal += column.Percent; |
| 212 | var value = Math.Max(AuFloor(size * column.Percent), column.Min); |
| 213 | guessMinPct += value; |
| 214 | guessPref += value; |
| 215 | } |
| 216 | else |
| 217 | { |
| 218 | guessPref += column.Pref; |
| 219 | guessMinPct += column.Min; |
| 220 | if (column.Specified) |
| 221 | { |
| 222 | guessMinSpec += Math.Max(column.Pref - column.Min, 0); |
| 223 | fixedPref += column.Pref; |
| 224 | } |
| 225 | else if (column.Pref == 0) |
| 226 | { |
| 227 | if (column.Originates) |
| 228 | zeroColumns++; |
| 229 | } |
| 230 | else |
| 231 | { |
| 232 | flexPref += column.Pref; |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | guessMinSpec += guessMinPct; |
| 238 | |
| 239 | Spread spread; |
| 240 | double space, basis; |
| 241 | if (size < guessPref) |
| 242 | { |
| 243 | if (target != Target.Final && size <= guessMin) |
| 244 | return; |
| 245 | if (size < guessMinPct) |
| 246 | (spread, space, basis) = (Spread.PctSmall, size - guessMin, guessMinPct - guessMin); |
| 247 | else if (size < guessMinSpec) |
| 248 | (spread, space, basis) = (Spread.FixedSmall, size - guessMinPct, guessMinSpec - guessMinPct); |
| 249 | else |
| 250 | (spread, space, basis) = (Spread.FlexSmall, size - guessMinSpec, guessPref - guessMinSpec); |
| 251 | } |
| 252 | else |
| 253 | { |
| 254 | space = size - guessPref; |
| 255 | (spread, basis) = flexPref > 0 ? (Spread.FlexLarge, flexPref) |
| 256 | : zeroColumns > 0 ? (Spread.FlexLargeZero, zeroColumns) |
| 257 | : fixedPref > 0 ? (Spread.FixedLarge, fixedPref) |
| 258 | : percentTotal > 0 ? (Spread.PctLarge, percentTotal) |
| 259 | : (Spread.AllLarge, count); |
| 260 | } |
| 261 | |
| 262 | double Share() |
| 263 | { |
| 264 | return basis == 0 ? 0 : space / basis; |
| 265 | } |
| 266 | |
| 267 | for (var index = first; index < first + count; index++) |
| 268 | { |
| 269 | var column = grid.Columns[index]; |
| 270 | var percent = column.Percent; |
| 271 | var width = percent != 0 ? Math.Max(AuFloor(size * percent), column.Min) : column.Pref; |
| 272 | var before = width; |
| 273 | switch (spread) |
| 274 | { |
| 275 | case Spread.PctSmall: |
| 276 | width = before = column.Min; |
| 277 | if (percent != 0 && AuFloor(size * percent) - width is > 0 and var pctMinusMin) |
| 278 | { |
| 279 | var share = Share(); |
| 280 | basis -= pctMinusMin; |
| 281 | width += Au(pctMinusMin * share); |
| 282 | } |
| 283 | |
| 284 | break; |
| 285 | case Spread.FixedSmall when percent == 0: |
| 286 | if (column.Specified) |
| 287 | { |
| 288 | var prefMinusMin = width - column.Min; |
| 289 | width = before = column.Min; |
| 290 | if (prefMinusMin != 0) |
| 291 | { |
| 292 | var share = Share(); |
| 293 | basis -= prefMinusMin; |
| 294 | width += Au(prefMinusMin * share); |
| 295 | } |
| 296 | } |
| 297 | else |
| 298 | { |
| 299 | width = before = column.Min; |
| 300 | } |
| 301 | |
| 302 | break; |
| 303 | case Spread.FlexSmall when percent == 0 && !column.Specified: |
| 304 | { |
| 305 | var prefMinusMin = Math.Max(width - column.Min, 0); |
| 306 | width = before = column.Min; |
| 307 | if (prefMinusMin != 0) |
| 308 | { |
| 309 | var share = Share(); |
| 310 | basis -= prefMinusMin; |
| 311 | width += Au(prefMinusMin * share); |
| 312 | } |
| 313 | |
| 314 | break; |
| 315 | } |
| 316 | case Spread.FlexLarge when percent == 0 && !column.Specified && width != 0: |
| 317 | case Spread.FixedLarge when percent == 0 && width != 0: |
| 318 | { |
| 319 | var share = Share(); |
| 320 | basis -= width; |
| 321 | width += Au(width * share); |
| 322 | break; |
| 323 | } |
| 324 | case Spread.FlexLargeZero when percent == 0 && !column.Specified && column.Originates: |
| 325 | width += Au(Share()); |
| 326 | basis--; |
| 327 | break; |
| 328 | case Spread.PctLarge when percent != 0: |
| 329 | { |
| 330 | var share = Share(); |
| 331 | width += Au(percent * share); |
| 332 | basis -= percent; |
| 333 | break; |
| 334 | } |
| 335 | case Spread.AllLarge: |
| 336 | width += Au(Share()); |
| 337 | basis--; |
| 338 | break; |
| 339 | } |
| 340 | |
| 341 | space -= width - before; |
| 342 | switch (target) |
| 343 | { |
| 344 | case Target.Min: |
| 345 | column.AddSpanCoords(width, width, spanSpecified); |
| 346 | break; |
| 347 | case Target.Pref: |
| 348 | column.AddSpanCoords(0, width, spanSpecified); |
| 349 | break; |
| 350 | default: |
| 351 | column.Final = width; |
| 352 | break; |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | private (double Min, double Pref) TableIntrinsic(BlockBox box) |
| 358 | { |
| 359 | var grid = BuildGrid(box.Table!, box.Style.BorderSpacing); |
| 360 | ColumnIntrinsics(grid); |
| 361 | var (min, pref, _) = ColumnTotals(grid); |
| 362 | var edges = box.Style.Border.Left + box.Style.Border.Right; |
| 363 | min += edges; |
| 364 | pref = Math.Max(pref + edges, min); |
| 365 | foreach (var caption in box.Table!.Captions) |
| 366 | { |
| 367 | var captionMin = Outer(caption).Min; |
| 368 | min = Math.Max(min, captionMin); |
| 369 | pref = Math.Max(pref, captionMin); |
| 370 | } |
| 371 | |
| 372 | return (min, pref); |
| 373 | } |
| 374 | |
| 375 | private void Table(BlockBox box, double x, double width) |
| 376 | { |
| 377 | var style = box.Style; |
| 378 | var (margin, border) = (style.Margin, style.Border); |
| 379 | AddMargin(margin.Top); |
| 380 | FlushMargins(); |
| 381 | if (box.Element is { Id.Length: > 0 } element) |
| 382 | Page.Anchors.TryAdd(element, Y); |
| 383 | |
| 384 | var grid = BuildGrid(box.Table!, style.BorderSpacing); |
| 385 | ColumnIntrinsics(grid); |
| 386 | var (min, _, expand) = ColumnTotals(grid); |
| 387 | var available = Math.Max(0, width - margin.Left - margin.Right); |
| 388 | var edges = border.Left + border.Right; |
| 389 | var contentWidth = style.Width is { } specified |
| 390 | ? Math.Max(specified.Resolve(width) - edges, min) |
| 391 | : min > available - edges |
| 392 | ? min |
| 393 | : Math.Min(expand, available - edges); |
| 394 | contentWidth = Math.Max(0, contentWidth); |
| 395 | var tableWidth = contentWidth + edges; |
| 396 | var tableX = x + margin.Left + (style.CenteredByMargins ? Math.Max(0, (available - tableWidth) / 2) : 0); |
| 397 | |
| 398 | foreach (var caption in box.Table!.Captions) |
| 399 | { |
| 400 | var captionStyle = caption.Style; |
| 401 | var (padding, captionBorder) = (captionStyle.Padding, captionStyle.Border); |
| 402 | var laid = Isolate(caption, |
| 403 | Math.Max(0, tableWidth - padding.Left - padding.Right - captionBorder.Left - captionBorder.Right)); |
| 404 | if (caption.Element is { Id.Length: > 0 } captionElement) |
| 405 | Page.Anchors.TryAdd(captionElement, Y); |
| 406 | Page.Paste(laid.Page, tableX + captionBorder.Left + padding.Left, Y + captionBorder.Top + padding.Top); |
| 407 | var height = laid.Height + padding.Top + padding.Bottom + captionBorder.Top + captionBorder.Bottom; |
| 408 | Page.Border(tableX, Y, tableWidth, height, captionStyle); |
| 409 | Y += height; |
| 410 | } |
| 411 | |
| 412 | var columns = grid.Columns; |
| 413 | if (columns.Length > 0) |
| 414 | Distribute(grid, contentWidth, 0, columns.Length, Target.Final, false); |
| 415 | var columnX = new double[columns.Length]; |
| 416 | var cursor = tableX + border.Left; |
| 417 | for (var index = 0; index < columns.Length; index++) |
| 418 | { |
| 419 | if (grid.SpacingBefore(index)) |
| 420 | cursor += grid.Spacing; |
| 421 | columnX[index] = cursor; |
| 422 | cursor += columns[index].Final; |
| 423 | } |
| 424 | |
| 425 | var cells = new Dictionary<Slot, LaidCell>(); |
| 426 | foreach (var slot in grid.Slots) |
| 427 | { |
| 428 | var cellStyle = slot.Cell.Box.Style; |
| 429 | var (padding, cellBorder) = (cellStyle.Padding, cellStyle.Border); |
| 430 | var inner = Math.Max(0, |
| 431 | grid.SpanWidth(slot) - padding.Left - padding.Right - cellBorder.Left - cellBorder.Right); |
| 432 | var content = Isolate(slot.Cell.Box, inner); |
| 433 | var contentHeight = content.Height; |
| 434 | if (cellStyle.Height is { Percent: false } fixedHeight) |
| 435 | contentHeight = Math.Max(contentHeight, fixedHeight.Value); |
| 436 | var height = contentHeight + padding.Top + padding.Bottom + cellBorder.Top + cellBorder.Bottom; |
| 437 | double? ascent = content.Empty ? null : cellBorder.Top + padding.Top + (content.Baseline ?? content.Height); |
| 438 | cells[slot] = new LaidCell(content, height, ascent); |
| 439 | } |
| 440 | |
| 441 | var rowCount = grid.Rows.Count; |
| 442 | var heights = new double[rowCount]; |
| 443 | var ascents = new double[rowCount]; |
| 444 | var descents = new double[rowCount]; |
| 445 | foreach (var slot in grid.Slots) |
| 446 | { |
| 447 | var cell = cells[slot]; |
| 448 | if (slot.RowSpan == 1) |
| 449 | heights[slot.Row] = Math.Max(heights[slot.Row], cell.Height); |
| 450 | if (slot.Cell.Box.Style.VerticalAlign == VerticalAlign.Baseline && cell.Ascent is { } ascent) |
| 451 | { |
| 452 | ascents[slot.Row] = Math.Max(ascents[slot.Row], ascent); |
| 453 | if (slot.RowSpan == 1) |
| 454 | descents[slot.Row] = Math.Max(descents[slot.Row], cell.Height - ascent); |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | for (var row = 0; row < rowCount; row++) |
| 459 | heights[row] = Math.Max(heights[row], ascents[row] + descents[row]); |
| 460 | foreach (var (start, end) in grid.Groups) |
| 461 | SpreadRowSpans(grid, start, end, heights, cells); |
| 462 | |
| 463 | var top = Y; |
| 464 | var rowY = new double[rowCount]; |
| 465 | var bottom = top + border.Top; |
| 466 | for (var row = 0; row < rowCount; row++) |
| 467 | { |
| 468 | bottom += grid.Spacing; |
| 469 | rowY[row] = bottom; |
| 470 | bottom += heights[row]; |
| 471 | if (grid.Rows[row].Element.Id is { Length: > 0 }) |
| 472 | Page.Anchors.TryAdd(grid.Rows[row].Element, rowY[row]); |
| 473 | } |
| 474 | |
| 475 | if (rowCount > 0) |
| 476 | bottom += grid.Spacing; |
| 477 | bottom += border.Bottom; |
| 478 | Page.Border(tableX, top, tableWidth, bottom - top, style); |
| 479 | |
| 480 | foreach (var slot in grid.Slots) |
| 481 | { |
| 482 | var cell = cells[slot]; |
| 483 | var cellStyle = slot.Cell.Box.Style; |
| 484 | var (padding, cellBorder) = (cellStyle.Padding, cellStyle.Border); |
| 485 | var cellX = columnX[slot.Col]; |
| 486 | var cellY = rowY[slot.Row]; |
| 487 | var cellHeight = (slot.RowSpan - 1) * grid.Spacing; |
| 488 | for (var row = slot.Row; row < slot.Row + slot.RowSpan; row++) |
| 489 | cellHeight += heights[row]; |
| 490 | Page.Border(cellX, cellY, grid.SpanWidth(slot), cellHeight, cellStyle); |
| 491 | if (slot.Cell.Box.Element is { Id.Length: > 0 } cellElement) |
| 492 | Page.Anchors.TryAdd(cellElement, cellY); |
| 493 | |
| 494 | var paddingBox = cellHeight - cellBorder.Top - cellBorder.Bottom; |
| 495 | var kid = cell.Content.Height + padding.Top + padding.Bottom; |
| 496 | var offset = cellStyle.VerticalAlign switch |
| 497 | { |
| 498 | VerticalAlign.Top => 0, |
| 499 | VerticalAlign.Bottom => paddingBox - kid, |
| 500 | VerticalAlign.Baseline when cell.Ascent is { } ascent => ascents[slot.Row] - ascent, |
| 501 | VerticalAlign.Baseline => 0, |
| 502 | _ => (paddingBox - kid) / 2 |
| 503 | }; |
| 504 | Page.Paste(cell.Content.Page, cellX + cellBorder.Left + padding.Left, |
| 505 | cellY + cellBorder.Top + padding.Top + Math.Max(0, offset)); |
| 506 | } |
| 507 | |
| 508 | Y = bottom; |
| 509 | AddMargin(margin.Bottom); |
| 510 | } |
| 511 | |
| 512 | private static void SpreadRowSpans(Grid grid, int start, int end, double[] heights, |
| 513 | Dictionary<Slot, LaidCell> cells) |
| 514 | { |
| 515 | var special = new bool[heights.Length]; |
| 516 | for (var row = start; row < end; row++) |
| 517 | { |
| 518 | var touching = grid.Slots.Count(slot => slot.Row <= row && row < slot.Row + slot.RowSpan); |
| 519 | special[row] = touching > 1 && !grid.Slots.Any(slot => slot.Row == row && slot.RowSpan == 1); |
| 520 | } |
| 521 | |
| 522 | foreach (var slot in grid.Slots) |
| 523 | { |
| 524 | if (slot.Row < start || slot.Row >= end || slot.RowSpan <= 1) |
| 525 | continue; |
| 526 | var (row, span) = (slot.Row, slot.RowSpan); |
| 527 | var spanned = 0.0; |
| 528 | var specials = 0; |
| 529 | for (var index = row; index < row + span; index++) |
| 530 | { |
| 531 | spanned += heights[index]; |
| 532 | if (special[index]) |
| 533 | specials++; |
| 534 | } |
| 535 | |
| 536 | var area = spanned + (span - 1) * grid.Spacing; |
| 537 | var height = cells[slot].Height; |
| 538 | if (area >= height) |
| 539 | continue; |
| 540 | |
| 541 | var extra = height - area; |
| 542 | var used = 0.0; |
| 543 | if (specials == 0) |
| 544 | { |
| 545 | if (spanned > 0) |
| 546 | for (var index = span - 1; index >= 0; index--) |
| 547 | { |
| 548 | var share = index == 0 ? extra - used : extra * (heights[row + index] / spanned); |
| 549 | share = Math.Min(share, extra - used); |
| 550 | heights[row + index] += share; |
| 551 | used += share; |
| 552 | if (used >= extra) |
| 553 | break; |
| 554 | } |
| 555 | else |
| 556 | heights[row + span - 1] += extra; |
| 557 | } |
| 558 | else |
| 559 | { |
| 560 | for (var index = span - 1; index >= 0; index--) |
| 561 | { |
| 562 | if (!special[row + index]) |
| 563 | continue; |
| 564 | var share = specials == 1 ? extra - used : extra / specials; |
| 565 | share = Math.Min(share, extra - used); |
| 566 | heights[row + index] += share; |
| 567 | used += share; |
| 568 | if (used >= extra) |
| 569 | break; |
| 570 | } |
| 571 | } |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | private sealed class Column |
| 576 | { |
| 577 | public double Min, Pref, Percent, SpanMin, SpanPref, SpanPercent, Final; |
| 578 | public bool Specified, Originates; |
| 579 | |
| 580 | public void AddCoords(double min, double pref, bool specified) |
| 581 | { |
| 582 | if (specified && !Specified) |
| 583 | { |
| 584 | Pref = Min; |
| 585 | Specified = true; |
| 586 | } |
| 587 | |
| 588 | if (!specified && Specified) |
| 589 | pref = min; |
| 590 | Min = Math.Max(Min, min); |
| 591 | Pref = Math.Max(Pref, pref); |
| 592 | } |
| 593 | |
| 594 | public void AddSpanCoords(double min, double pref, bool specified) |
| 595 | { |
| 596 | if (!specified && Specified) |
| 597 | pref = min; |
| 598 | SpanMin = Math.Max(SpanMin, min); |
| 599 | SpanPref = Math.Max(SpanPref, pref); |
| 600 | } |
| 601 | |
| 602 | public void AccumulateSpan() |
| 603 | { |
| 604 | AddCoords(SpanMin, SpanPref, Specified); |
| 605 | Percent = Math.Max(Percent, SpanPercent); |
| 606 | SpanMin = SpanPref = SpanPercent = 0; |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | private sealed record Slot(TableCell Cell, int Row, int Col, int RowSpan, int ColSpan); |
| 611 | |
| 612 | private sealed record CellInfo(double Min, double Pref, double Percent, bool Specified); |
| 613 | |
| 614 | private sealed record LaidCell(Isolated Content, double Height, double? Ascent); |
| 615 | |
| 616 | private sealed class Grid(double spacing) |
| 617 | { |
| 618 | public readonly List<(int Start, int End)> Groups = []; |
| 619 | public readonly List<TableRow> Rows = []; |
| 620 | public readonly List<Slot> Slots = []; |
| 621 | public readonly double Spacing = spacing; |
| 622 | public Column[] Columns = []; |
| 623 | |
| 624 | public bool SpacingBefore(int column) |
| 625 | { |
| 626 | return column == 0 || Columns[column].Originates; |
| 627 | } |
| 628 | |
| 629 | public double SpanWidth(Slot slot) |
| 630 | { |
| 631 | var width = 0.0; |
| 632 | for (var column = slot.Col; column < slot.Col + slot.ColSpan; column++) |
| 633 | { |
| 634 | if (column > slot.Col && SpacingBefore(column)) |
| 635 | width += Spacing; |
| 636 | width += Columns[column].Final; |
| 637 | } |
| 638 | |
| 639 | return width; |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | private enum Target |
| 644 | { |
| 645 | Min, |
| 646 | Pref, |
| 647 | Final |
| 648 | } |
| 649 | |
| 650 | private enum Spread |
| 651 | { |
| 652 | PctSmall, |
| 653 | FixedSmall, |
| 654 | FlexSmall, |
| 655 | FlexLarge, |
| 656 | FlexLargeZero, |
| 657 | FixedLarge, |
| 658 | PctLarge, |
| 659 | AllLarge |
| 660 | } |
| 661 | } |