Skip to content
0.2.0-dev Download ZIP
88 lines ·3.7 KB Raw
1namespace EvilEngine.Html;
2
3internal sealed partial class Flow
4{
5 private void Fieldset(BlockBox box, double x, double width)
6 {
7 var legend = box.Legend!;
8 var style = box.Style;
9 var (margin, padding, border) = (style.Margin, style.Padding, style.Border);
10 AddMargin(margin.Top);
11 FlushMargins();
12 var top = Y;
13 if (box.Element is { Id.Length: > 0 } element)
14 Page.Anchors.TryAdd(element, top);
15
16 var borderX = x + margin.Left;
17 var borderWidth = Math.Max(0, width - margin.Left - margin.Right);
18 var contentX = borderX + border.Left + padding.Left;
19 var contentWidth = Math.Max(0, borderWidth - border.Left - border.Right - padding.Left - padding.Right);
20
21 var legendStyle = legend.Style;
22 var (legendMargin, legendPadding, legendBorder) = (legendStyle.Margin, legendStyle.Padding, legendStyle.Border);
23 var (min, max) = Outer(legend);
24 var outerWidth = Math.Min(Math.Max(min, contentWidth), max);
25 var boxWidth = Math.Max(0, outerWidth - legendMargin.Left - legendMargin.Right);
26 var innerWidth = Math.Max(0,
27 boxWidth - legendBorder.Left - legendBorder.Right - legendPadding.Left - legendPadding.Right);
28 var laid = Isolate(legend, innerWidth);
29 var boxHeight = legendBorder.Top + legendPadding.Top + laid.Height + legendPadding.Bottom + legendBorder.Bottom;
30 var outerHeight = legendMargin.Top + boxHeight + legendMargin.Bottom;
31
32 var legendX = contentX;
33 if (contentWidth > outerWidth)
34 legendX += LegendAlign(legend) switch
35 {
36 TextAlign.Center => (contentWidth - outerWidth) / 2,
37 TextAlign.Right => contentWidth - outerWidth,
38 _ => 0
39 };
40 var legendY = top;
41 var space = 0.0;
42 if (outerHeight > border.Top)
43 {
44 space = outerHeight - border.Top;
45 }
46 else
47 {
48 var off = (border.Top - boxHeight) / 2 - legendMargin.Top;
49 if (off > 0)
50 legendY += off - Math.Max(0, off + outerHeight - border.Top);
51 }
52
53 var legendBox = new Box(legendX + legendMargin.Left, legendY + legendMargin.Top, boxWidth, boxHeight);
54 if (legendBorder != default && legendStyle.BorderStyle != BorderStyle.None)
55 Page.Border(legendBox.X, legendBox.Y, legendBox.Width, legendBox.Height, legendStyle);
56 Page.Paste(laid.Page, legendBox.X + legendBorder.Left + legendPadding.Left,
57 legendBox.Y + legendBorder.Top + legendPadding.Top);
58 if (legend.Element is { Id.Length: > 0 } legendElement)
59 Page.Anchors.TryAdd(legendElement, legendBox.Y);
60
61 Y = top + border.Top + space + padding.Top;
62 Content(box, contentX, contentWidth);
63 FlushMargins();
64 Y += padding.Bottom + border.Bottom;
65
66 if (border != default && style.BorderStyle != BorderStyle.None)
67 {
68 var borderTop = top;
69 var visual = legendMargin.Top + boxHeight / 2 - border.Top / 2;
70 if (visual > 0 && outerHeight > border.Top)
71 borderTop += visual - Math.Max(0, visual + border.Top - outerHeight);
72 var clip = legendBox.Height < border.Top ? legendBox with { Y = top, Height = border.Top } : legendBox;
73 Page.Border(borderX, borderTop, borderWidth, Y - borderTop, style, clip);
74 }
75
76 AddMargin(margin.Bottom);
77 }
78
79 private static TextAlign LegendAlign(BlockBox legend)
80 {
81 return legend.Element?.GetAttribute("align")?.Trim().ToLowerInvariant() switch
82 {
83 "center" => TextAlign.Center,
84 "right" => TextAlign.Right,
85 _ => TextAlign.Start
86 };
87 }
88}