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
(** Computes size using styles and measure functions *)
open Geometry
open Tree
(** Compute the size of a leaf node (node with no children) *)
let compute_leaf_layout ~(inputs : Layout_input.t) ~(style : Style.t)
~(resolve_calc_value : Style.calc_resolver)
~(measure_function :
float option size -> Available_space.t size -> float size) :
Layout_output.t =
let known_dimensions = Layout_input.known_dimensions inputs in
let parent_size = Layout_input.parent_size inputs in
let available_space = Layout_input.available_space inputs in
let sizing_mode = Layout_input.sizing_mode inputs in
let run_mode = Layout_input.run_mode inputs in
let parent_inline_size = parent_size.width in
let margin =
Style.margin style
|> Rect.map (fun lpa ->
Style.Length_percentage_auto.resolve_or_zero lpa parent_inline_size
resolve_calc_value)
in
let padding =
Style.padding style
|> Rect.map (fun lp ->
Style.Length_percentage.resolve_or_zero lp parent_inline_size
resolve_calc_value)
in
let border =
Style.border style
|> Rect.map (fun lp ->
Style.Length_percentage.resolve_or_zero lp parent_inline_size
resolve_calc_value)
in
let padding_border = Rect.add padding border in
let pb_sum = Rect.sum_axes padding_border in
let box_sizing_adjustment =
if Style.box_sizing style = Style.Box_sizing.Content_box then pb_sum
else Size.zero
in
let node_size, node_min_size, node_max_size, aspect_ratio =
match sizing_mode with
| Sizing_mode.Content_size -> (known_dimensions, Size.none, Size.none, None)
| Sizing_mode.Inherent_size ->
let aspect_ratio = Style.aspect_ratio style in
let style_size =
Style.size style |> fun dims ->
Size.
{
width =
Style.Dimension.maybe_resolve dims.width parent_size.width
resolve_calc_value;
height =
Style.Dimension.maybe_resolve dims.height parent_size.height
resolve_calc_value;
}
|> Size.apply_aspect_ratio aspect_ratio
|> Size.maybe_add box_sizing_adjustment
in
let style_min_size =
Style.min_size style |> fun dims ->
Size.
{
width =
Style.Dimension.maybe_resolve dims.width parent_size.width
resolve_calc_value;
height =
Style.Dimension.maybe_resolve dims.height parent_size.height
resolve_calc_value;
}
|> Size.apply_aspect_ratio aspect_ratio
|> Size.maybe_add box_sizing_adjustment
in
let style_max_size =
Style.max_size style |> fun dims ->
Size.
{
width =
Style.Dimension.maybe_resolve dims.width parent_size.width
resolve_calc_value;
height =
Style.Dimension.maybe_resolve dims.height parent_size.height
resolve_calc_value;
}
|> Size.maybe_add box_sizing_adjustment
in
let node_size = Size.choose_first known_dimensions style_size in
(node_size, style_min_size, style_max_size, aspect_ratio)
in
let scrollbar_gutter =
let overflow = Style.overflow style in
Point.{ x = overflow.y; y = overflow.x }
|> Point.map (function
| Style.Overflow.Scroll -> Style.scrollbar_width style
| _ -> 0.0)
in
let content_box_inset =
let open Rect in
{
padding_border with
right = padding_border.right +. scrollbar_gutter.x;
bottom = padding_border.bottom +. scrollbar_gutter.y;
}
in
let has_styles_preventing_being_collapsed_through =
(match Style.display style with Style.Display.Block -> false | _ -> true)
|| Style.Overflow.is_container (Style.overflow style).x
|| Style.Overflow.is_container (Style.overflow style).y
|| Style.position style = Style.Position.Absolute
|| padding.top > 0.0 || padding.bottom > 0.0 || border.top > 0.0
|| border.bottom > 0.0
|| (match node_size.height with Some h when h > 0.0 -> true | _ -> false)
||
match node_min_size.height with
| Some h when h > 0.0 -> true
| _ -> false
in
match
(run_mode, has_styles_preventing_being_collapsed_through, node_size)
with
| Run_mode.Compute_size, true, { width = Some width; height = Some height } ->
let size =
Size.{ width; height } |> fun s ->
Size.clamp node_min_size node_max_size s |> fun s -> Size.max s pb_sum
in
Layout_output.make ~size ~content_size:Size.zero
~first_baselines:Point.none ~top_margin:Collapsible_margin_set.zero
~bottom_margin:Collapsible_margin_set.zero
~margins_can_collapse_through:false
| _ ->
let available_space_computed =
let compute_axis known avail_space_axis margin_sum node_size_axis
node_min_size_axis node_max_size_axis inset_sum =
let open Available_space in
(match known with Some v -> of_float v | None -> avail_space_axis)
|> fun space ->
sub_or_zero space (Some margin_sum)
|> fun space ->
set_or_self space known
|> fun space ->
set_or_self space node_size_axis
|> fun space ->
min_or_self space node_max_size_axis
|> fun space ->
map_definite_value space (fun size ->
let clamped =
match (node_min_size_axis, node_max_size_axis) with
| Some min, Some max -> Float.max min (Float.min max size)
| Some min, None -> Float.max min size
| None, Some max -> Float.min max size
| None, None -> size
in
clamped -. inset_sum)
in
let width =
compute_axis known_dimensions.width available_space.width
(Rect.horizontal_axis_sum margin)
node_size.width node_min_size.width node_max_size.width
(Rect.horizontal_axis_sum content_box_inset)
in
let height =
compute_axis known_dimensions.height available_space.height
(Rect.vertical_axis_sum margin)
node_size.height node_min_size.height node_max_size.height
(Rect.vertical_axis_sum content_box_inset)
in
Size.{ width; height }
in
let measured_size =
let known_for_measure =
match run_mode with
| Run_mode.Compute_size -> known_dimensions
| Run_mode.Perform_layout -> Size.none
| Run_mode.Perform_hidden_layout ->
invalid_arg "Leaf node cannot be measured in hidden layout mode"
in
measure_function known_for_measure available_space_computed
in
let clamped_size =
let chosen = Size.choose_first known_dimensions node_size in
let measured_with_inset =
Size.add measured_size (Rect.sum_axes content_box_inset)
in
let base_size =
match (chosen.width, chosen.height) with
| Some w, Some h -> Size.{ width = w; height = h }
| Some w, None ->
Size.{ width = w; height = measured_with_inset.height }
| None, Some h ->
Size.{ width = measured_with_inset.width; height = h }
| None, None -> measured_with_inset
in
Size.clamp node_min_size node_max_size base_size
in
let size =
match aspect_ratio with
| Some ratio ->
let aspect_height = clamped_size.width /. ratio in
Size.
{
width = clamped_size.width;
height = Float.max clamped_size.height aspect_height;
}
| None -> clamped_size
in
let size = Size.max size pb_sum in
Layout_output.make ~size
~content_size:(Size.add measured_size (Rect.sum_axes padding))
~first_baselines:Point.none ~top_margin:Collapsible_margin_set.zero
~bottom_margin:Collapsible_margin_set.zero
~margins_can_collapse_through:
((not has_styles_preventing_being_collapsed_through)
&& size.height = 0.0 && measured_size.height = 0.0)