-
Notifications
You must be signed in to change notification settings - Fork 0
/
BoxTree.cs
4064 lines (3502 loc) · 146 KB
/
BoxTree.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//using HtmlParserSharp;
//using OpenTK;
using SkiaSharpOpenGLBenchmark.css;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Xml;
namespace SkiaSharpOpenGLBenchmark
{
public enum BoxType : byte
{
BOX_BLOCK,
BOX_INLINE_CONTAINER,
BOX_INLINE,
BOX_TABLE,
BOX_TABLE_ROW,
BOX_TABLE_CELL,
BOX_TABLE_ROW_GROUP,
BOX_FLOAT_LEFT,
BOX_FLOAT_RIGHT,
BOX_INLINE_BLOCK,
BOX_BR,
BOX_TEXT,
BOX_INLINE_END,
BOX_NONE,
BOX_FLEX,
BOX_INLINE_FLEX
}
public enum BoxFlags : int
{
NEW_LINE = 1 << 0, // first inline on a new line
STYLE_OWNED = 1 << 1, // style is owned by this box
PRINTED = 1 << 2, // box has already been printed
PRE_STRIP = 1 << 3, // PRE tag needing leading newline stripped
CLONE = 1 << 4, // continuation of previous box from wrapping
MEASURED = 1 << 5, // text box width has been measured
HAS_HEIGHT = 1 << 6, // box has height (perhaps due to children)
MAKE_HEIGHT = 1 << 7, // box causes its own height
NEED_MIN = 1 << 8, // minimum width is required for layout
REPLACE_DIM = 1 << 9, // replaced element has given dimensions
IFRAME = 1 << 10, // box contains an iframe
CONVERT_CHILDREN = 1 << 11, // wanted children converting
IS_REPLACED = 1 << 12 // box is a replaced element
}
// Transient properties for construction of current node
public struct BoxConstructProps
{
// Style from which to inherit, or NULL if none
public ComputedStyle ParentStyle;
// Current link target, or NULL if none
//struct nsurl *href;
// Current frame target, or NULL if none
public string Target;
// Current title attribute, or NULL if none
public string Title;
// Identity of the current block-level container
public Box ContainingBlock;
// Current container for inlines, or NULL if none
// \note If non-NULL, will be the last child of containing_block
public Box InlineContainer;
// Whether the current node is the root of the DOM tree
public bool NodeIsRoot;
};
// Sides of a box
public enum BoxSide : byte
{
TOP = 0,
RIGHT = 1,
BOTTOM = 2,
LEFT = 3
};
// Container for box border details
// box.h:102
public struct BoxBorder
{
public CssBorderStyleEnum Style; // border-style
public Color BorderColor; // border-color value
public int Width; // border-width (pixels)
}
// Node in box tree. All dimensions are in pixels
public class Box
{
// Type of box
public BoxType Type;
// Box flags
public BoxFlags Flags;
// DOM node that generated this box or NULL
public XmlNode Node;
// Computed styles for elements and their pseudo elements.
// NULL on non-element boxes.
public CssSelectResults Styles;
// Style for this box. 0 for INLINE_CONTAINER and
// FLOAT_*. Pointer into a box's 'styles' select results,
// except for implied boxes, where it is a pointer to an
// owned computed style.
public ComputedStyle Style;
// value of id attribute (or name for anchors)
public string Id;
// Next sibling box, or NULL.
public Box Next;
// Previous sibling box, or NULL.
Box Prev;
// First child box, or NULL.
public Box Children;
// Last child box, or NULL.
public Box Last;
// Parent box, or NULL.
public Box Parent;
// INLINE_END box corresponding to this INLINE box, or INLINE
// box corresponding to this INLINE_END box.
public Box InlineEnd;
// First float child box, or NULL. Float boxes are in the tree
// twice, in this list for the block box which defines the
// area for floats, and also in the standard tree given by
// children, next, prev, etc.
public Box float_children;
// Next sibling float box.
public Box next_float;
// If box is a float, points to box's containing block
public Box float_container;
// Level below which subsequent floats must be cleared. This
// is used only for boxes with float_children
int clear_level;
// Level below which floats have been placed.
int cached_place_below_level;
// Coordinate of left padding edge relative to parent box, or
// relative to ancestor that contains this box in
// float_children for FLOAT_.
public int X;
// Coordinate of top padding edge, relative as for x
public int Y;
// Width of content box (excluding padding etc.)
public int Width;
// Height of content box (excluding padding etc.)
public int Height;
/* These four variables determine the maximum extent of a box's
* descendants. They are relative to the x,y coordinates of the box.
*
* Their use depends on the overflow CSS property:
*
* Overflow: Usage:
* visible The content of the box is displayed within these dimensions
* hidden These are ignored. Content is plotted within the box dimensions
* scroll These are used to determine the extent of the scrollable area
* auto As "scroll"
*/
public int DescendantX0; // left edge of descendants
public int DescendantY0; // top edge of descendants
public int DescendantX1; // right edge of descendants
public int DescendantY1; // bottom edge of descendants
// Margin: TOP, RIGHT, BOTTOM, LEFT.
public int[] Margin = new int[4];
// Padding: TOP, RIGHT, BOTTOM, LEFT.
public int[] Padding = new int[4];
// Border: TOP, RIGHT, BOTTOM, LEFT.
public BoxBorder[] Border = new BoxBorder[4];
// Horizontal scroll.
public Scrollbar ScrollX;
// Vertical scroll.
public Scrollbar ScrollY;
// Width of box taking all line breaks (including margins etc). Must be non-negative
int MinWidth;
// Width that would be taken with no line breaks. Must be non-negative
public int MaxWidth;
// Text, or NULL if none. Unterminated.
public string Text;
// Length of text.
public int Length;
// Width of space after current text (depends on font and size).
public int Space;
// Byte offset within a textual representation of this content.
//size_t byte_offset;
// Link, or NULL.
//struct nsurl *href;
// Link target, or NULL.
public string Target;
// Title, or NULL.
public string Title;
// Number of columns for TABLE / TABLE_CELL.
int columns;
// Number of rows for TABLE only.
int rows;
// Start column for TABLE_CELL only.
int start_column;
// Array of table column data for TABLE only.
//struct column *col;
// List item value
int list_value;
// List marker box if this is a list-item, or NULL.
//struct box *list_marker;
// Form control data, or NULL if not a form control.
//struct form_control* gadget;
// (Image)map to use with this object, or NULL if none
//char* usemap;
// Background image for this box, or NULL if none
//struct hlcache_handle *background;
public int Background;
// Object in this box (usually an image), or NULL if none.
//struct hlcache_handle* object;
// Parameters for the object, or NULL.
// struct object_params *object_params;
// Iframe's browser_window, or NULL if none
//struct browser_window *iframe;
// FIXME: Temporary and should be moved away
int SCROLLBAR_WIDTH = 16;
// box_manipulate.c:92 - box_create()
public Box(CssSelectResults styles,
ComputedStyle style,
bool style_owned,
/*struct nsurl */int href,
string target, string title, string id)
{
this.Type = BoxType.BOX_INLINE;
this.Flags = 0; //box->flags = style_owned ? (box->flags | STYLE_OWNED) : box->flags;
Styles = styles;
Style = style;
this.X = this.Y = 0;
this.Width = Int32.MaxValue;
this.Height = 0;
this.DescendantX0 = this.DescendantY0 = 0;
this.DescendantX1 = this.DescendantY1 = 0;
for (int i = 0; i != 4; i++)
{
this.Margin[i] = this.Padding[i] = 0;
this.Border[i].Width = 0;
}
//this.scroll_x = this.scroll_y = NULL;
this.MinWidth = 0;
this.MaxWidth = Int32.MaxValue;
//box->byte_offset = 0;
//box->text = NULL;
//box->length = 0;
Space = 0;
//box->href = (href == NULL) ? NULL : nsurl_ref(href);
//box->target = target;
//box->title = title;
this.columns = 1;
this.rows = 1;
this.start_column = 0;
//box->next = NULL;
//box->prev = NULL;
//box->children = NULL;
this.Last = null;
//box->parent = NULL;
//box->inline_end = NULL;
//box->float_children = NULL;
//box->float_container = NULL;
//box->next_float = NULL;
//box->cached_place_below_level = 0;
//box->list_value = 1;
//box->list_marker = NULL;
//box->col = NULL;
//box->gadget = NULL;
//box->usemap = NULL;
//box->id = id;
Background = 0;
//box->object = NULL;
//box->object_params = NULL;
//box->iframe = NULL;
//box->node = NULL;
ScrollX = new Scrollbar();
ScrollY = new Scrollbar();
}
/**
* mapping from CSS display to box type this table must be in sync
* with libcss' css_display enum
*/
// box_construct.c:93
static public BoxType BoxMap(CssDisplay disp)
{
switch (disp)
{
case CssDisplay.CSS_DISPLAY_INHERIT:
return BoxType.BOX_BLOCK;
case CssDisplay.CSS_DISPLAY_INLINE:
return BoxType.BOX_INLINE;
case CssDisplay.CSS_DISPLAY_BLOCK:
return BoxType.BOX_BLOCK;
case CssDisplay.CSS_DISPLAY_LIST_ITEM:
return BoxType.BOX_BLOCK;
case CssDisplay.CSS_DISPLAY_RUN_IN:
return BoxType.BOX_INLINE;
case CssDisplay.CSS_DISPLAY_INLINE_BLOCK:
return BoxType.BOX_INLINE_BLOCK;
case CssDisplay.CSS_DISPLAY_TABLE:
return BoxType.BOX_TABLE;
case CssDisplay.CSS_DISPLAY_INLINE_TABLE:
return BoxType.BOX_TABLE;
case CssDisplay.CSS_DISPLAY_TABLE_ROW_GROUP:
return BoxType.BOX_TABLE_ROW_GROUP;
case CssDisplay.CSS_DISPLAY_TABLE_HEADER_GROUP:
return BoxType.BOX_TABLE_ROW_GROUP;
case CssDisplay.CSS_DISPLAY_TABLE_FOOTER_GROUP:
return BoxType.BOX_TABLE_ROW_GROUP;
case CssDisplay.CSS_DISPLAY_TABLE_ROW:
return BoxType.BOX_TABLE_ROW;
case CssDisplay.CSS_DISPLAY_TABLE_COLUMN_GROUP:
return BoxType.BOX_NONE;
case CssDisplay.CSS_DISPLAY_TABLE_COLUMN:
return BoxType.BOX_NONE;
case CssDisplay.CSS_DISPLAY_TABLE_CELL:
return BoxType.BOX_TABLE_CELL;
case CssDisplay.CSS_DISPLAY_TABLE_CAPTION:
return BoxType.BOX_INLINE;
case CssDisplay.CSS_DISPLAY_NONE:
return BoxType.BOX_NONE;
default:
Debug.Assert(false); // must never happen
return BoxType.BOX_BLOCK;
}
}
// box_construct.c:153 - box_extract_properties()
static public BoxConstructProps ExtractProperties(XmlNode node, BoxTree bt)
{
BoxConstructProps props = new BoxConstructProps();
props.NodeIsRoot = BoxTree.BoxIsRoot(node);
// Extract properties from containing DOM node
if (props.NodeIsRoot == false)
{
XmlNode current_node = node;
XmlNode parent_node = null;
Box parent_box;
// Find ancestor node containing parent box
while (true)
{
parent_node = current_node.ParentNode;
parent_box = bt.BoxForNode(parent_node);
if (parent_box != null)
{
props.ParentStyle = parent_box.Style;
//props.href = parent_box.href;
props.Target = parent_box.Target;
props.Title = parent_box.Title;
break;
}
else
{
current_node = parent_node;
parent_node = null;
}
}
// Find containing block (may be parent)
while (true)
{
parent_node = current_node.ParentNode;
if (parent_node == null)
break;
var b = bt.BoxForNode(parent_node);
// Children of nodes that created an inline box
// will generate boxes which are attached as
// _siblings_ of the box generated for their
// parent node. Note, however, that we'll still
// use the parent node's styling as the parent
// style, above.
if (b != null && b.Type != BoxType.BOX_INLINE && b.Type != BoxType.BOX_BR)
{
props.ContainingBlock = b;
break;
}
else
{
current_node = parent_node;
parent_node = null;
}
}
}
// Compute current inline container, if any
if (props.ContainingBlock != null &&
props.ContainingBlock.Last != null &&
props.ContainingBlock.Last.Type == BoxType.BOX_INLINE_CONTAINER)
{
props.InlineContainer = props.ContainingBlock.Last;
}
return props;
}
public void AttachToNode(XmlNode n)
{
Node = n;
}
// box_manipulate.c:163 - box_add_child()
public void AddChild(Box child)
{
// parent = this
if (this.Children != null)
{ /* has children already */
this.Last.Next = child;
child.Prev = this.Last;
}
else
{ /* this is the first child */
this.Children = child;
child.Prev = null;
}
this.Last = child;
child.Parent = this;
}
// box_is_first_child()
// box_inspect.h:138
// Check if layout box is a first child.
public bool IsFirstChild()
{
return (Parent == null || this == Parent.Children);
}
#region Layout
// layout_text_indent()
// layout.c:250
/**
* Calculate the text-indent length.
*
* \param style style of block
* \param width width of containing block
* \return length of indent
*/
static int LayoutTextIndent(
CssUnitCtx unit_len_ctx,
ComputedStyle style, int width)
{
Fixed value = Fixed.F_0;
CssUnit unit = CssUnit.CSS_UNIT_PX;
style.ComputedTextIndent(ref value, ref unit);
if (unit == CssUnit.CSS_UNIT_PCT) {
return value.PercentageToInt(width);
} else {
return unit_len_ctx.Len2DevicePx(style, value, unit).ToInt();
}
}
/**
* Determine width of margin, borders, and padding on one side of a box.
*
* \param unit_len_ctx CSS length conversion context for document
* \param style style to measure
* \param side side of box to measure
* \param margin whether margin width is required
* \param border whether border width is required
* \param padding whether padding width is required
* \param fixed increased by sum of fixed margin, border, and padding
* \param frac increased by sum of fractional margin and padding
*/
// layout.c:286
void CalculateMbpWidth(CssUnitCtx unit_len_ctx,
ComputedStyle style,
uint side,
bool margin,
bool border,
bool padding,
ref int fixedPart,
ref double frac)
{
Fixed value = Fixed.F_0;
CssUnit unit = CssUnit.CSS_UNIT_PX;
Debug.Assert(style != null);
// margin
if (margin)
{
CssMarginEnum type;
type = style.ComputedMarginSides[side](ref value, ref unit);
if (type == CssMarginEnum.CSS_MARGIN_SET)
{
if (unit == CssUnit.CSS_UNIT_PCT)
{
frac += (value / Fixed.F_100).ToInt();
}
else
{
fixedPart += unit_len_ctx.Len2DevicePx(style, value, unit).ToInt();
}
}
}
// border
if (border)
{
if (style.ComputedBorderSideStyle[side]() != CssBorderStyleEnum.CSS_BORDER_STYLE_NONE)
{
style.ComputedBorderSideWidth[side](ref value, ref unit);
fixedPart += unit_len_ctx.Len2DevicePx(style, value, unit).ToInt();
}
}
// padding
if (padding)
{
style.ComputedPaddingSide[side](ref value, ref unit);
if (unit == CssUnit.CSS_UNIT_PCT)
{
frac += (value / Fixed.F_100).ToInt();
} else
{
fixedPart += unit_len_ctx.Len2DevicePx(style, value, unit).ToInt();
}
}
}
// layout.c:531
bool HasPercentageMaxWidth()
{
CssUnit unit = CssUnit.CSS_UNIT_PX;
Fixed value = Fixed.F_0;
var type = Style.ComputedMaxWidth(ref value, ref unit);
return ((type == CssMaxWidthEnum.CSS_MAX_WIDTH_SET) && (unit == CssUnit.CSS_UNIT_PCT));
}
/**
* Calculate minimum and maximum width of a line.
*
* \param first a box in an inline container
* \param line_min updated to minimum width of line starting at first
* \param line_max updated to maximum width of line starting at first
* \param first_line true iff this is the first line in the inline container
* \param line_has_height updated to true or false, depending on line
* \param font_func Font functions.
* \return first box in next line, or 0 if no more lines
* \post 0 <= *line_min <= *line_max
*/
// layout.c:562
Box LayoutMinmaxLine(Box first,
ref int line_min,
ref int line_max,
bool first_line,
ref bool line_has_height,
/*const struct gui_layout_table *font_func*/
HtmlContent content)
{
int min = 0, max = 0, width = 0, height, fixedSize;
double frac;
int i, j;
Box b;
Box block;
PlotFontStyle fstyle;
bool no_wrap;
Debug.Assert(first.Parent != null);
Debug.Assert(first.Parent.Parent != null);
Debug.Assert(first.Parent.Parent.Style != null);
block = first.Parent.Parent;
no_wrap = (block.Style.ComputedWhitespace() == CssWhiteSpaceEnum.CSS_WHITE_SPACE_NOWRAP ||
block.Style.ComputedWhitespace() == CssWhiteSpaceEnum.CSS_WHITE_SPACE_PRE);
line_has_height = false;
// corresponds to the pass 1 loop in layout_line()
for (b = first; b != null; b = b.Next)
{
CssWidth wtype;
CssHeightEnum htype;
CssBoxSizingEnum bs;
Fixed value = Fixed.F_0;
CssUnit unit = CssUnit.CSS_UNIT_PX;
Debug.Assert(b.Type == BoxType.BOX_INLINE || b.Type == BoxType.BOX_INLINE_BLOCK ||
b.Type == BoxType.BOX_FLOAT_LEFT ||
b.Type == BoxType.BOX_FLOAT_RIGHT ||
b.Type == BoxType.BOX_BR || b.Type == BoxType.BOX_TEXT ||
b.Type == BoxType.BOX_INLINE_END);
Log.Print(LogChannel.Layout, $"{b.GetHashCode()}: min {min}, max {max}");
if (b.Type == BoxType.BOX_BR) {
b = b.Next;
break;
}
if (b.Type == BoxType.BOX_FLOAT_LEFT || b.Type == BoxType.BOX_FLOAT_RIGHT) {
Debug.Assert(b.Children != null);
if (b.Children.Type == BoxType.BOX_BLOCK)
b.Children.LayoutMinmaxBlock(/*font_func,*/ content);
else
{
//layout_minmax_table(b.Children, font_func, content);
Log.Unimplemented();
}
b.MinWidth = b.Children.MinWidth;
b.MaxWidth = b.Children.MaxWidth;
if (min < b.MinWidth)
min = b.MinWidth;
max += b.MaxWidth;
continue;
}
if (b.Type == BoxType.BOX_INLINE_BLOCK) {
b.LayoutMinmaxBlock(/*font_func,*/ content);
if (min < b.MinWidth)
min = b.MinWidth;
max += b.MaxWidth;
if (((int)b.Flags & (int)BoxFlags.HAS_HEIGHT) != 0)
line_has_height = true;
continue;
}
Debug.Assert(b.Style != null);
fstyle = b.Style.FontPlotStyle(content.UnitLenCtx);
/*if (b.Type == BoxType.BOX_INLINE && !b->object &&
!(b.Flags & REPLACE_DIM) &&
!(b.Flags & IFRAME)) {
fixed = frac = 0;
calculate_mbp_width(&content->unit_len_ctx,
b->style, LEFT, true, true, true,
&fixed, &frac);
if (!b->inline_end)
calculate_mbp_width(&content->unit_len_ctx,
b->style, RIGHT,
true, true, true,
&fixed, &frac);
if (0 < fixed)
max += fixed;
*line_has_height = true;
// \todo update min width, consider fractional extra
} else*/ if (b.Type == BoxType.BOX_INLINE_END)
{
fixedSize = 0;
frac = 0;
CalculateMbpWidth(content.UnitLenCtx,
b.InlineEnd.Style, (uint)BoxSide.RIGHT,
true, true, true,
ref fixedSize, ref frac);
if (0 < fixedSize)
max += fixedSize;
if (b.Next != null)
{
if (b.Space == int.MaxValue)
{
//font_func->width(&fstyle, " ", 1, &b.Space);
Log.Unimplemented("font_func.Width()");
}
max += b.Space;
}
line_has_height = true;
continue;
}
if (/*!b->object &&*/
(((int)b.Flags & (int) BoxFlags.IFRAME) == 0) &&
/*!b.Gadget &&*/ (((int)b.Flags & (int)BoxFlags.REPLACE_DIM) == 0))
{
// inline non-replaced, 10.3.1 and 10.6.1
bool no_wrap_box;
if (string.IsNullOrEmpty(b.Text))
continue;
no_wrap_box = (b.Style.ComputedWhitespace() == CssWhiteSpaceEnum.CSS_WHITE_SPACE_NOWRAP ||
b.Style.ComputedWhitespace() == CssWhiteSpaceEnum.CSS_WHITE_SPACE_PRE);
if (b.Width == int.MaxValue)
{
/** \todo handle errors */
/* If it's a select element, we must use the
* width of the widest option text */
/*
if (b.Parent.Parent.Gadget && b->parent->parent->gadget.Type == GADGET_SELECT)
{
int opt_maxwidth = 0;
struct form_option *o;
for (o = b->parent->parent->gadget->data.select.items; o; o = o.Next) {
int opt_width;
font_func->width(&fstyle,
o.Text,
strlen(o.Text),
&opt_width);
if (opt_maxwidth < opt_width)
opt_maxwidth =opt_width;
}
b->width = opt_maxwidth;
if (nsoption_bool(core_select_menu))
b->width += SCROLLBAR_WIDTH;
} else*/ {
//font_func->width(&fstyle, b.Text, b->length, &b->width);
content.Plot.GetFontWidth(fstyle, b.Text, ref b.Width);
b.Flags |= BoxFlags.MEASURED;
}
}
max += b.Width;
if (b.Next != null)
{
if (b.Space == int.MaxValue)
{
//font_func->width(&fstyle, " ", 1, &b.Space);
content.Plot.GetFontWidth(fstyle, " ", ref b.Space);
}
max += b.Space;
}
if (no_wrap) {
/* Don't wrap due to block style,
* so min is the same as max */
min = max;
} else if (no_wrap_box) {
/* This inline box can't be wrapped,
* for min, consider box's width */
if (min < b.Width)
min = b.Width;
} else if (((int)b.Parent.Flags & (int)BoxFlags.NEED_MIN) != 0)
{
/* If we care what the minimum width is,
* calculate it. (It's only needed if we're
* shrinking-to-fit.) */
/* min = widest single word */
i = 0;
do {
for (j = i; j != b.Length && b.Text[j] != ' '; j++) ;
//font_func->width(&fstyle, b.Text + i, j - i, &width);
Log.Unimplemented("font_func->width()");
if (min < width)
min = width;
i = j + 1;
} while (j != b.Length);
}
line_has_height = true;
continue;
}
/* inline replaced, 10.3.2 and 10.6.2 */
Debug.Assert(b.Style != null);
/* calculate box width */
wtype = b.Style.ComputedWidth(ref value, ref unit);
bs = block.Style.ComputedBoxSizing();
if (wtype == CssWidth.CSS_WIDTH_SET)
{
if (unit == CssUnit.CSS_UNIT_PCT)
{
width = int.MinValue; //AUTO;
} else {
width = content.UnitLenCtx.Len2DevicePx(b.Style, value, unit).ToInt();
if (bs == CssBoxSizingEnum.CSS_BOX_SIZING_BORDER_BOX) {
fixedSize = 0; frac = 0;
CalculateMbpWidth(content.UnitLenCtx,
block.Style, (uint)BoxSide.LEFT,
false, true, true,
ref fixedSize, ref frac);
CalculateMbpWidth(content.UnitLenCtx,
block.Style, (uint)BoxSide.RIGHT,
false, true, true,
ref fixedSize, ref frac);
if (width < fixedSize) {
width = fixedSize;
}
}
if (width < 0)
width = 0;
}
} else {
width = int.MinValue; //AUTO;
}
/* height */
htype = b.Style.ComputedHeight(ref value, ref unit);
if (htype == CssHeightEnum.CSS_HEIGHT_SET) {
height = content.UnitLenCtx.Len2DevicePx(b.Style, value, unit).ToInt();
} else {
height = int.MinValue; //AUTO;
}
/*if (b.Object || (b.Flags & REPLACE_DIM))
{
if (b.Object)
{
int temp_height = height;
layout_get_object_dimensions(b,
&width, &temp_height,
INT_MIN, INT_MAX,
INT_MIN, INT_MAX);
}
fixedSize = 0; frac = 0;
if (bs == CSS_BoxType.BOX_SIZING_BORDER_BOX) {
calculate_mbp_width(&content->unit_len_ctx,
b->style, LEFT,
true, false, false,
&fixedSize, &frac);
calculate_mbp_width(&content->unit_len_ctx,
b->style, RIGHT,
true, false, false,
&fixedSize, &frac);
} else {
calculate_mbp_width(&content->unit_len_ctx,
b->style, LEFT,
true, true, true,
&fixedSize, &frac);
calculate_mbp_width(&content->unit_len_ctx,
b->style, RIGHT,
true, true, true,
&fixedSize, &frac);
}
if (0 < width + fixedSize)
width += fixedSize;
} else if (b.Flags & IFRAME)
{
// TODO: handle percentage widths properl
if (width == int.MinValue) // AUTO
width = 400;
fixedSize = 0; frac = 0;
if (bs == CSS_BoxType.BOX_SIZING_BORDER_BOX) {
calculate_mbp_width(&content->unit_len_ctx,
b->style, LEFT,
true, false, false,
&fixedSize, &frac);
calculate_mbp_width(&content->unit_len_ctx,
b->style, RIGHT,
true, false, false,
&fixedSize, &frac);
} else {
calculate_mbp_width(&content->unit_len_ctx,
b->style, LEFT,
true, true, true,
&fixedSize, &frac);
calculate_mbp_width(&content->unit_len_ctx,
b->style, RIGHT,
true, true, true,
&fifixedSizexed, &frac);
}
if (0 < width + fixedSize)
width += fixedSize;
} else*/
{
// form control with no object
if (width == int.MinValue) // AUTO
width = content.UnitLenCtx.Len2DevicePx(
b.Style,
Fixed.F_1, CssUnit.CSS_UNIT_EM).ToInt();
}
if (min < width && !b.HasPercentageMaxWidth())
min = width;
if (width > 0)
max += width;
line_has_height = true;
}
if (first_line)
{
/* todo: handle percentage values properly */
/* todo: handle text-indent interaction with floats */
int text_indent = LayoutTextIndent(content.UnitLenCtx,
first.Parent.Parent.Style, 100);
min = (min + text_indent < 0) ? 0 : min + text_indent;
max = (max + text_indent < 0) ? 0 : max + text_indent;
}
line_min = min;
line_max = max;
Log.Print(LogChannel.Layout, $"line_min {min}, line_max {max}");
Debug.Assert(b != first);
Debug.Assert(0 <= line_min);
Debug.Assert(line_min <= line_max);
return b;
}
/**
* Calculate minimum and maximum width of an inline container.
*
* \param inline_container box of type INLINE_CONTAINER
* \param[out] has_height set to true if container has height
* \param font_func Font functions.
* \post inline_container->min_width and inline_container->max_width filled in,
* 0 <= inline_container->min_width <= inline_container->max_width
*/
// layout.c:919
public void LayoutMinmaxInlineContainer(ref bool has_height,
//const struct gui_layout_table *font_func,
HtmlContent content)
{
Box child;
int line_min = 0, line_max = 0;
int min = 0, max = 0;
bool first_line = true;
bool line_has_height = false;
Debug.Assert(Type == BoxType.BOX_INLINE_CONTAINER);
// check if the widths have already been calculated
if (MaxWidth != int.MaxValue)
return;
has_height = false;
for (child = Children; child != null; ) {
child = LayoutMinmaxLine(child, ref line_min, ref line_max,
first_line, ref line_has_height, /* font_func,*/ content);
if (min < line_min)
min = line_min;
if (max < line_max)
max = line_max;
first_line = false;
has_height |= line_has_height;