namespace EvilEngine.Html; internal sealed partial class Flow { private void Fieldset(BlockBox box, double x, double width) { var legend = box.Legend!; var style = box.Style; var (margin, padding, border) = (style.Margin, style.Padding, style.Border); AddMargin(margin.Top); FlushMargins(); var top = Y; if (box.Element is { Id.Length: > 0 } element) Page.Anchors.TryAdd(element, top); var borderX = x + margin.Left; var borderWidth = Math.Max(0, width - margin.Left - margin.Right); var contentX = borderX + border.Left + padding.Left; var contentWidth = Math.Max(0, borderWidth - border.Left - border.Right - padding.Left - padding.Right); var legendStyle = legend.Style; var (legendMargin, legendPadding, legendBorder) = (legendStyle.Margin, legendStyle.Padding, legendStyle.Border); var (min, max) = Outer(legend); var outerWidth = Math.Min(Math.Max(min, contentWidth), max); var boxWidth = Math.Max(0, outerWidth - legendMargin.Left - legendMargin.Right); var innerWidth = Math.Max(0, boxWidth - legendBorder.Left - legendBorder.Right - legendPadding.Left - legendPadding.Right); var laid = Isolate(legend, innerWidth); var boxHeight = legendBorder.Top + legendPadding.Top + laid.Height + legendPadding.Bottom + legendBorder.Bottom; var outerHeight = legendMargin.Top + boxHeight + legendMargin.Bottom; var legendX = contentX; if (contentWidth > outerWidth) legendX += LegendAlign(legend) switch { TextAlign.Center => (contentWidth - outerWidth) / 2, TextAlign.Right => contentWidth - outerWidth, _ => 0 }; var legendY = top; var space = 0.0; if (outerHeight > border.Top) { space = outerHeight - border.Top; } else { var off = (border.Top - boxHeight) / 2 - legendMargin.Top; if (off > 0) legendY += off - Math.Max(0, off + outerHeight - border.Top); } var legendBox = new Box(legendX + legendMargin.Left, legendY + legendMargin.Top, boxWidth, boxHeight); if (legendBorder != default && legendStyle.BorderStyle != BorderStyle.None) Page.Border(legendBox.X, legendBox.Y, legendBox.Width, legendBox.Height, legendStyle); Page.Paste(laid.Page, legendBox.X + legendBorder.Left + legendPadding.Left, legendBox.Y + legendBorder.Top + legendPadding.Top); if (legend.Element is { Id.Length: > 0 } legendElement) Page.Anchors.TryAdd(legendElement, legendBox.Y); Y = top + border.Top + space + padding.Top; Content(box, contentX, contentWidth); FlushMargins(); Y += padding.Bottom + border.Bottom; if (border != default && style.BorderStyle != BorderStyle.None) { var borderTop = top; var visual = legendMargin.Top + boxHeight / 2 - border.Top / 2; if (visual > 0 && outerHeight > border.Top) borderTop += visual - Math.Max(0, visual + border.Top - outerHeight); var clip = legendBox.Height < border.Top ? legendBox with { Y = top, Height = border.Top } : legendBox; Page.Border(borderX, borderTop, borderWidth, Y - borderTop, style, clip); } AddMargin(margin.Bottom); } private static TextAlign LegendAlign(BlockBox legend) { return legend.Element?.GetAttribute("align")?.Trim().ToLowerInvariant() switch { "center" => TextAlign.Center, "right" => TextAlign.Right, _ => TextAlign.Start }; } }