-
Notifications
You must be signed in to change notification settings - Fork 7
/
flex.go
827 lines (755 loc) · 20.9 KB
/
flex.go
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
// Referenced code: https://github.com/golang/exp/blob/master/shiny/widget/flex/flex.go
package furex
import (
"fmt"
"image"
"math"
)
// Direction is the direction in which flex items are laid out
type Direction uint8
const (
Row Direction = iota
Column
)
func (d Direction) String() string {
switch d {
case Row:
return "row"
case Column:
return "column"
default:
return fmt.Sprintf("unknown direction: %d", d)
}
}
// Justify aligns items along the main axis.
type Justify uint8
const (
JustifyStart Justify = iota // pack to start of line
JustifyEnd // pack to end of line
JustifyCenter // pack to center of line
JustifySpaceBetween // even spacing
JustifySpaceAround // even spacing, half-size on each end
)
func (f Justify) String() string {
switch f {
case JustifyStart:
return "flex-start"
case JustifyEnd:
return "flex-end"
case JustifyCenter:
return "center"
case JustifySpaceBetween:
return "space-between"
case JustifySpaceAround:
return "space-around"
default:
return fmt.Sprintf("unknown justify: %d", f)
}
}
// FlexAlign represents align of flex children
type FlexAlign int
const (
FlexCenter FlexAlign = iota
FlexStart
FlexEnd
FlexSpaceBetween
)
func (f FlexAlign) String() string {
switch f {
case FlexCenter:
return "center"
case FlexStart:
return "flex-start"
case FlexEnd:
return "flex-end"
case FlexSpaceBetween:
return "space-between"
default:
return fmt.Sprintf("unknown flex-align: %d", f)
}
}
// AlignItem aligns items along the cross axis.
type AlignItem uint8
const (
AlignItemStretch AlignItem = iota
AlignItemStart
AlignItemEnd
AlignItemCenter
)
func (f AlignItem) String() string {
switch f {
case AlignItemStretch:
return "stretch"
case AlignItemStart:
return "flex-start"
case AlignItemEnd:
return "flex-end"
case AlignItemCenter:
return "center"
default:
return fmt.Sprintf("unknown align-item: %d", f)
}
}
// FlexWrap controls whether the container is single- or multi-line,
// and the direction in which the lines are laid out.
type FlexWrap uint8
const (
NoWrap FlexWrap = iota
Wrap
WrapReverse
)
func (f FlexWrap) String() string {
switch f {
case NoWrap:
return "nowrap"
case Wrap:
return "wrap"
case WrapReverse:
return "wrap-reverse"
default:
return fmt.Sprintf("unknown flex-wrap: %d", f)
}
}
// AlignContent is the 'align-content' property.
// It aligns container lines when there is extra space on the cross-axis.
type AlignContent uint8
const (
AlignContentStart AlignContent = iota
AlignContentEnd
AlignContentCenter
AlignContentSpaceBetween
AlignContentSpaceAround
AlignContentStretch
)
func (f AlignContent) String() string {
switch f {
case AlignContentStart:
return "start"
case AlignContentEnd:
return "end"
case AlignContentCenter:
return "center"
case AlignContentSpaceBetween:
return "space-between"
case AlignContentSpaceAround:
return "space-around"
case AlignContentStretch:
return "stretch"
}
return fmt.Sprintf("unknown align-content: %d", f)
}
// Position is the 'position' property
type Position uint8
const (
PositionStatic Position = iota
PositionAbsolute
)
func (p Position) String() string {
switch p {
case PositionStatic:
return "static"
case PositionAbsolute:
return "absolute"
}
return fmt.Sprintf("unknown position: %d", p)
}
// Display is the 'display' property
type Display uint8
const (
DisplayFlex Display = iota
DisplayNone
)
func (d Display) String() string {
switch d {
case DisplayFlex:
return "flex"
case DisplayNone:
return "none"
}
return fmt.Sprintf("unknown display: %d", d)
}
type flexEmbed struct {
*View
}
// layout is the main routine that implements a subset of flexbox layout
// https://www.w3.org/TR/css-flexbox-1/#layout-algorithm
func (f *flexEmbed) layout(width, height int, container *containerEmbed) {
// 9.2. Line Length Determination
// Determine the available main and cross space for the flex items.
containerMainSize := float64(f.mainSize(width, height))
containerCrossSize := float64(f.crossSize(width, height))
// Determine the flex base size and hypothetical main size of each item:
var children []element
for _, c := range container.children {
if c.item.Display == DisplayNone {
continue
}
if c.item.Position == PositionAbsolute {
x := container.frame.Min.X
if c.item.Left != 0 {
x = container.frame.Min.X + c.item.Left
} else if c.item.Right != nil {
x = container.frame.Max.X - *c.item.Right - c.item.Width
}
y := container.frame.Min.Y
if c.item.Top != 0 {
y = container.frame.Min.Y + c.item.Top
} else if c.item.Bottom != nil {
y = container.frame.Max.Y - *c.item.Bottom - c.item.Height
}
c.bounds = image.Rect(x, y, x+c.item.Width, y+c.item.Height)
c.item.frame = c.bounds
c.absolute = true
continue
}
c.absolute = false
children = append(children, element{
widthInPct: c.item.WidthInPct,
heightInPct: c.item.HeightInPct,
flexBaseSize: float64(f.flexBaseSize(c)),
node: c,
})
}
// Depending on the flex container direction, apply calculation for width and height in percent.
switch f.Direction {
case Row:
// Calculate the remaining width after taking out the fixed width items.
remFree := width
for _, c := range children {
remFree -= (c.node.item.Width + c.node.item.MarginLeft + c.node.item.MarginRight)
}
// If there is remaining space, distribute it among the flexible items.
if remFree > 0 {
for i, c := range children {
if c.widthInPct > 0 {
v := float64(width) * c.widthInPct / 100.
children[i].node.item.calculatedWidth = int(math.Min(v, float64(remFree)))
children[i].flexBaseSize = float64(f.flexBaseSize(children[i].node))
}
}
}
// If the container is a row, calculate the height of each item.
for _, c := range children {
if c.heightInPct > 0 {
// Calculate the new width based on the item's width percentage.
c.node.item.calculatedHeight = int(float64(height) * c.node.item.HeightInPct / 100)
}
}
case Column:
// Calculate the remaining height after taking out the fixed width items.
remFree := height
for _, c := range children {
remFree -= (c.node.item.Height + c.node.item.MarginTop + c.node.item.MarginBottom)
}
// If there is remaining space, distribute it among the flexible items.
if remFree > 0 {
for i, c := range children {
if c.heightInPct > 0 {
v := float64(height) * c.heightInPct / 100.
children[i].node.item.calculatedHeight = int(math.Min(v, float64(remFree)))
children[i].flexBaseSize = float64(f.flexBaseSize(children[i].node))
}
}
}
// If the container is a column, calculate the width of each item.
for _, c := range children {
if c.widthInPct > 0 {
// Calculate the new width based on the item's width percentage.
c.node.item.calculatedWidth = int(float64(width) * c.node.item.WidthInPct / 100)
}
}
default:
panic(fmt.Sprint("flex: bad direction ", f.Direction))
}
// §9.3. Main Size Determination
// Collect flex items into flex lines
var lines []flexLine
if f.Wrap == NoWrap {
// Single line
line := flexLine{child: make([]*element, len(children))}
for i := range children {
child := &children[i]
child.mainMargin = f.mainMargin(child.node)
line.child[i] = child
line.mainSize += child.flexBaseSize +
(child.mainMargin[0] + child.mainMargin[1])
}
lines = []flexLine{line}
} else {
// Multi line
var line flexLine
for i := range children {
child := &children[i]
child.mainMargin = f.mainMargin(child.node)
// hypotheticalMainSize = flexBaseSize + main margin
hypotheticalMainSize := child.flexBaseSize +
(child.mainMargin[0] + child.mainMargin[1])
if line.mainSize > 0 && line.mainSize+hypotheticalMainSize > containerMainSize {
lines = append(lines, line)
line = flexLine{}
}
line.child = append(line.child, child)
line.mainSize += hypotheticalMainSize
}
if len(line.child) > 0 || len(children) == 0 {
lines = append(lines, line)
}
}
// §9.3.6 resolve flexible lengths (details in section §9.7)
for l := range lines {
line := &lines[l]
grow := line.mainSize < containerMainSize // §9.7.1
// §9.7.2 freeze inflexible children.
for _, child := range line.child {
mainSize := float64(f.mainSize(child.node.item.width(), child.node.item.height()))
if grow {
if child.node.item.Grow == 0 {
child.frozen = true
child.mainSize = mainSize
}
} else {
if child.node.item.Shrink == 0 {
child.frozen = true
child.mainSize = mainSize
}
}
}
// §9.7.3 calculate initial free space
freeSpace := float64(f.mainSize(width, height))
for _, child := range line.child {
freeSpace -= (float64(f.flexBaseSize(child.node)) +
(child.mainMargin[0] + child.mainMargin[1]))
}
// §9.7.4 flex loop
for {
// Check for flexible items.
allFrozen := true
for _, child := range line.child {
if !child.frozen {
allFrozen = false
break
}
}
if allFrozen {
break
}
// Calculate remaining free space.
remFreeSpace := float64(f.mainSize(width, height))
unfrozenFlexFactor := 0.0
for _, child := range line.child {
mainMargin := child.mainMargin[0] + child.mainMargin[1]
if child.frozen {
remFreeSpace -= (child.mainSize + mainMargin)
} else {
remFreeSpace -= (float64(f.mainSize(child.node.item.width(), child.node.item.height())) + mainMargin)
if grow {
unfrozenFlexFactor += child.node.item.Grow
} else {
unfrozenFlexFactor += child.node.item.Shrink
}
}
}
if unfrozenFlexFactor < 1 {
p := freeSpace * unfrozenFlexFactor
if math.Abs(p) < math.Abs(remFreeSpace) {
remFreeSpace = p
}
}
// Distribute free space proportional to flex factors.
if grow {
for _, child := range line.child {
if child.frozen {
continue
}
r := child.node.item.Grow / unfrozenFlexFactor
child.mainSize = float64(f.mainSize(
child.node.item.width(), child.node.item.height(),
)) + r*remFreeSpace
}
} else {
sumScaledShrinkFactor := 0.0
for _, child := range line.child {
if child.frozen {
continue
}
scaledShrinkFactor := float64(f.mainSize(
child.node.item.width(), child.node.item.height(),
)) * child.node.item.Shrink
sumScaledShrinkFactor += scaledShrinkFactor
}
for _, child := range line.child {
if child.frozen {
continue
}
scaledShrinkFactor := float64(f.mainSize(
child.node.item.width(), child.node.item.height(),
)) * child.node.item.Shrink
r := float64(scaledShrinkFactor) / sumScaledShrinkFactor
child.mainSize = float64(f.mainSize(
child.node.item.width(), child.node.item.height(),
)) - r*math.Abs(float64(remFreeSpace))
}
}
for _, child := range line.child {
child.frozen = true
}
}
}
// §9.4. Cross Size Determination
// Determine the hypothetical cross size of each item
for l := range lines {
for _, c := range lines[l].child {
c.crossMargin = f.crossMargin(c.node)
c.crossSize = float64(
f.crossSize(c.node.item.width(), c.node.item.height()),
)
}
}
// §9.4.8 Calculate the cross size of each flex line.
if len(lines) == 1 {
// Single line
lines[0].crossSize = containerCrossSize
} else {
// Multi line
for l := range lines {
line := &lines[l]
max := 0.0
for _, child := range line.child {
if child.crossSize > max {
max = child.crossSize +
(child.crossMargin[0] + child.crossMargin[1])
}
}
line.crossSize = max
}
}
off := 0.0
for l := range lines {
line := &lines[l]
line.crossOffset = off
off += line.crossSize
}
// §9.4.9 align-content: stretch
remCrossSize := containerCrossSize - off
if f.AlignContent == AlignContentStretch && remCrossSize > 0 {
add := remCrossSize / float64(len(lines))
for l := range lines {
line := &lines[l]
line.crossOffset += float64(l) * add
line.crossSize += add
}
}
// §9.4.11 align-item: stretch
for l := range lines {
line := &lines[l]
for _, child := range line.child {
if f.AlignItems == AlignItemStretch &&
!f.isCrossSizeFixed(child.node.item) &&
child.crossSize < line.crossSize {
crossMargin := child.crossMargin[0] + child.crossMargin[1]
child.crossSize = line.crossSize - crossMargin
}
}
}
// §9.5. Main-Axis Alignment
for l := range lines {
line := &lines[l]
total := 0.0
for _, child := range line.child {
total += child.mainSize +
(child.mainMargin[0] + child.mainMargin[1])
}
remFree := containerMainSize - total
off, spacing := 0.0, 0.0
switch f.Justify {
case JustifyStart:
case JustifyEnd:
off = remFree
case JustifyCenter:
off = remFree / 2
case JustifySpaceBetween:
spacing = remFree / float64(len(line.child)-1)
case JustifySpaceAround:
spacing = remFree / float64(len(line.child))
off = spacing / 2
}
for _, child := range line.child {
child.mainOffset = off + (child.mainMargin[0])
off += spacing + child.mainSize +
(child.mainMargin[0] + child.mainMargin[1])
}
}
// §9.6. Cross axis alignment
for l := range lines {
line := &lines[l]
for _, child := range line.child {
child.crossOffset = line.crossOffset + (child.crossMargin[0])
if child.crossSize == line.crossSize {
continue
}
diff := line.crossSize - child.crossSize -
(child.crossMargin[0] + child.crossMargin[1])
switch f.AlignItems {
case AlignItemStart:
// already laid out correctly
case AlignItemEnd:
child.crossOffset = line.crossOffset + diff +
(child.crossMargin[0])
case AlignItemCenter:
child.crossOffset = line.crossOffset + diff/2 +
(child.crossMargin[0])
}
}
}
// §9.6.15 determine container cross size used
crossSize := lines[len(lines)-1].crossOffset + lines[len(lines)-1].crossSize
remFree := containerCrossSize - crossSize
// §9.6.16 align flex lines, 'align-content'.
if remFree > 0 {
spacing, off := 0.0, 0.0
switch f.AlignContent {
case AlignContentStart:
// already laid out correctly
case AlignContentEnd:
off = remFree
case AlignContentCenter:
off = remFree / 2
case AlignContentSpaceBetween:
spacing = remFree / float64(len(lines)-1)
case AlignContentSpaceAround:
spacing = remFree / float64(len(lines))
off = spacing / 2
}
if f.AlignContent != AlignContentStart {
for l := range lines {
line := &lines[l]
line.crossOffset += off
for _, child := range line.child {
child.crossOffset += off
}
off += spacing
}
}
}
// §9.9.1. Flex Container Intrinsic Main Sizes
intrinsicMainSize := 0.0
for _, line := range lines {
largestMaxContentFlexFraction := -math.MaxFloat64
for _, child := range line.child {
// 1. Calculate the max-content flex fraction for each item.
flexBaseSize := child.flexBaseSize
maxContentSize := child.mainSize
var maxContentFlexFraction float64
if maxContentSize > flexBaseSize {
// Positive free space, divide by flex grow factor (floored at 1).
flexGrowFactor := math.Max(1, child.node.item.Grow)
maxContentFlexFraction = (maxContentSize - flexBaseSize) / flexGrowFactor
} else {
// Negative free space, divide by scaled flex shrink factor (floored at 1).
flexShrinkFactor := math.Max(1, child.node.item.Shrink)
maxContentFlexFraction = (flexBaseSize - maxContentSize) / (flexBaseSize * flexShrinkFactor)
}
child.maxContentFlexFraction = maxContentFlexFraction
if maxContentFlexFraction > largestMaxContentFlexFraction {
largestMaxContentFlexFraction = maxContentFlexFraction
}
}
// 2. Add each item’s flex base size to the product of its flex grow/shrink factor and the largest max-content flex fraction.
for _, child := range line.child {
var newMainSize float64
if largestMaxContentFlexFraction > 0 {
newMainSize = child.flexBaseSize + (child.node.item.Grow * largestMaxContentFlexFraction)
} else {
newMainSize = child.flexBaseSize - (child.node.item.Shrink * child.flexBaseSize * largestMaxContentFlexFraction)
}
child.mainSize = newMainSize
}
// 3. Determine line size and update intrinsicMainSize.
lineSize := 0.0
for _, child := range line.child {
lineSize += child.mainSize
}
if lineSize > intrinsicMainSize {
intrinsicMainSize = lineSize
}
}
f.setMainSize(int(intrinsicMainSize))
// §9.9.2. Flex Container Intrinsic Cross Sizes
// The min-content/max-content cross size of a single-line flex container
// is the largest min-content contribution/max-content contribution (respectively)
// of its flex items.
intrinsicCrossSize := 0.0
for _, line := range lines {
if intrinsicCrossSize < line.crossOffset+line.crossSize {
intrinsicCrossSize = line.crossOffset + line.crossSize
}
min := math.Inf(1)
max := -1.
for _, child := range line.child {
if child.crossOffset < min {
min = child.crossOffset
}
if max == -1 || child.crossOffset+child.crossSize > max {
max = child.crossOffset + child.crossSize
}
}
if intrinsicCrossSize < max-min {
intrinsicCrossSize = max - min
}
}
f.setCrossSize(int(intrinsicCrossSize))
// TODO: Calculate min-content/max-content cross size for multi-line flex container.
// For a multi-line flex container, the min-content/max-content cross size is
// the sum of the flex line cross sizes resulting from sizing the flex container
// under a cross-axis min-content constraint/max-content constraint (respectively).
// However, if the flex container is flex-flow: column wrap;, then it’s sized
// by first finding the largest min-content/max-content cross-size contribution
// among the flex items (respectively), then using that size as the available
// space in the cross axis for each of the flex items during layout.
// Layout complete. Update children position
for l := range lines {
line := &lines[l]
for _, child := range line.child {
switch f.Direction {
case Row:
child.node.bounds = image.Rect(
round(child.mainOffset),
round(child.crossOffset),
round(child.mainOffset+child.mainSize),
round(child.crossOffset+child.crossSize))
child.node.item.setFrame(child.node.bounds.Add(f.frame.Min))
case Column:
child.node.bounds = image.Rect(
round(child.crossOffset),
round(child.mainOffset),
round(child.crossOffset+child.crossSize),
round(child.mainOffset+child.mainSize))
child.node.item.setFrame(child.node.bounds.Add(f.frame.Min))
default:
panic(fmt.Sprint("flex: bad direction ", f.Direction))
}
}
}
}
type element struct {
node *child
flexBaseSize float64
mainSize float64
mainOffset float64
mainMargin []float64
crossSize float64
crossOffset float64
crossMargin []float64
frozen bool
maxContentFlexFraction float64
widthInPct float64
heightInPct float64
}
type flexLine struct {
mainSize float64
crossSize float64
crossOffset float64
child []*element
}
func (f *flexEmbed) mainSize(x, y int) int {
switch f.Direction {
case Row:
return x
case Column:
return y
default:
panic(fmt.Sprint("flex: bad direction ", f.Direction))
}
}
func (f *flexEmbed) setCrossSize(v int) {
switch f.Direction {
case Row:
f.calculatedHeight = v
case Column:
f.calculatedWidth = v
default:
panic(fmt.Sprint("flex: bad direction ", f.Direction))
}
}
func (f *flexEmbed) setMainSize(v int) {
switch f.Direction {
case Row:
f.calculatedWidth = v
case Column:
f.calculatedHeight = v
default:
panic(fmt.Sprint("flex: bad direction ", f.Direction))
}
}
func (f *flexEmbed) isCrossSizeFixed(v *View) bool {
switch f.Direction {
case Row:
return v.isHeightFixed()
case Column:
return v.isWidthFixed()
default:
panic(fmt.Sprint("flex: bad direction ", f.Direction))
}
}
func (f *flexEmbed) crossSize(x, y int) int {
switch f.Direction {
case Row:
return y
case Column:
return x
default:
panic(fmt.Sprint("flex: bad direction ", f.Direction))
}
}
func (f *flexEmbed) mainMargin(c *child) []float64 {
switch f.Direction {
case Row:
return []float64{
float64(c.item.MarginLeft),
float64(c.item.MarginRight)}
case Column:
return []float64{
float64(c.item.MarginTop),
float64(c.item.MarginBottom)}
default:
panic("unreachable")
}
}
func (f *flexEmbed) crossMargin(c *child) []float64 {
switch f.Direction {
case Row:
return []float64{
float64(c.item.MarginTop),
float64(c.item.MarginBottom)}
case Column:
return []float64{
float64(c.item.MarginLeft),
float64(c.item.MarginRight)}
default:
panic("unreachable")
}
}
func (f *flexEmbed) flexBaseSize(c *child) int {
w := c.item.Width
if w == 0 {
w = c.item.calculatedWidth
}
h := c.item.Height
if h == 0 {
h = c.item.calculatedHeight
}
return f.mainSize(w, h)
}
func (f *flexEmbed) clampSize(size, width, height int) int {
minSize := f.mainSize(width, height)
if minSize > size {
size = minSize
}
if size < 0 {
return 0
}
return size
}
func round(f float64) int {
return int(math.Floor(f + .5))
}