-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
231 lines (182 loc) · 6.46 KB
/
example.py
File metadata and controls
231 lines (182 loc) · 6.46 KB
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
import math
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent / "src"))
from yoga import ( # noqa: E402
YGAlign,
YGConfigFree,
YGConfigNew,
YGDirection,
YGFlexDirection,
YGJustify,
YGMeasureMode,
YGNodeCalculateLayout,
YGNodeFreeRecursive,
YGNodeGetContext,
YGNodeInsertChild,
YGNodeLayoutGetHeight,
YGNodeLayoutGetLeft,
YGNodeLayoutGetTop,
YGNodeLayoutGetWidth,
YGNodeNew,
YGNodeNewWithConfig,
YGNodeSetContext,
YGNodeSetMeasureFunc,
YGNodeStyleGetFlexDirection,
YGNodeStyleSetAlignItems,
YGNodeStyleSetFlexDirection,
YGNodeStyleSetFlexGrow,
YGNodeStyleSetFlexWrap,
YGNodeStyleSetHeight,
YGNodeStyleSetJustifyContent,
YGNodeStyleSetPositionType,
YGNodeStyleSetWidth,
YGPositionType,
YGSize,
YGWrap,
)
def assert_layout(node, left, top, width, height):
actual = (
YGNodeLayoutGetLeft(node),
YGNodeLayoutGetTop(node),
YGNodeLayoutGetWidth(node),
YGNodeLayoutGetHeight(node),
)
expected = (left, top, width, height)
if actual != expected:
raise AssertionError(f"layout mismatch: actual={actual}, expected={expected}")
def longest_word_width(text, width_per_char):
max_length = 0
current_length = 0
for char in text:
if char == " ":
max_length = max(max_length, current_length)
current_length = 0
else:
current_length += 1
return max(max_length, current_length) * width_per_char
def calculate_height(text, measured_width, width_per_char, height_per_char):
if len(text) * width_per_char <= measured_width:
return height_per_char
words = text.split(" ")
lines = 1
current_line_length = 0
for word in words:
word_width = len(word) * width_per_char
if word_width > measured_width:
if current_line_length > 0:
lines += 1
lines += 1
current_line_length = 0
elif current_line_length + word_width <= measured_width:
current_line_length += word_width + width_per_char
else:
lines += 1
current_line_length = word_width + width_per_char
return (lines - 1 if current_line_length == 0 else lines) * height_per_char
def intrinsic_size_measure(node, width, width_mode, height, height_mode):
inner_text = YGNodeGetContext(node)
height_per_char = 10
width_per_char = 10
if width_mode == YGMeasureMode.YGMeasureModeExactly:
measured_width = width
elif width_mode == YGMeasureMode.YGMeasureModeAtMost:
measured_width = min(len(inner_text) * width_per_char, width)
else:
measured_width = len(inner_text) * width_per_char
constrained_width = (
measured_width
if YGNodeStyleGetFlexDirection(node) == YGFlexDirection.YGFlexDirectionColumn
else max(longest_word_width(inner_text, width_per_char), measured_width)
)
if height_mode == YGMeasureMode.YGMeasureModeExactly:
measured_height = height
elif height_mode == YGMeasureMode.YGMeasureModeAtMost:
measured_height = min(
calculate_height(inner_text, constrained_width, width_per_char, height_per_char),
height,
)
else:
measured_height = calculate_height(
inner_text, constrained_width, width_per_char, height_per_char
)
return YGSize(measured_width, measured_height)
def example_flex_row():
root = YGNodeNew()
left = YGNodeNew()
right = YGNodeNew()
YGNodeStyleSetWidth(root, 300)
YGNodeStyleSetHeight(root, 100)
YGNodeStyleSetFlexDirection(root, YGFlexDirection.YGFlexDirectionRow)
YGNodeStyleSetWidth(left, 50)
YGNodeStyleSetHeight(left, 100)
YGNodeInsertChild(root, left, 0)
YGNodeStyleSetFlexGrow(right, 1)
YGNodeInsertChild(root, right, 1)
YGNodeCalculateLayout(root, math.nan, math.nan, YGDirection.YGDirectionLTR)
assert_layout(root, 0, 0, 300, 100)
assert_layout(left, 0, 0, 50, 100)
assert_layout(right, 50, 0, 250, 100)
YGNodeFreeRecursive(root)
def example_wrap():
root = YGNodeNew()
children = [YGNodeNew() for _ in range(5)]
YGNodeStyleSetWidth(root, 140)
YGNodeStyleSetHeight(root, 120)
YGNodeStyleSetFlexDirection(root, YGFlexDirection.YGFlexDirectionRow)
YGNodeStyleSetFlexWrap(root, YGWrap.YGWrapWrap)
for index, child in enumerate(children):
YGNodeStyleSetWidth(child, 50)
YGNodeStyleSetHeight(child, 10)
YGNodeInsertChild(root, child, index)
YGNodeCalculateLayout(root, math.nan, math.nan, YGDirection.YGDirectionLTR)
expected = [
(0, 0, 50, 10),
(50, 0, 50, 10),
(0, 10, 50, 10),
(50, 10, 50, 10),
(0, 20, 50, 10),
]
for child, layout in zip(children, expected):
assert_layout(child, *layout)
YGNodeFreeRecursive(root)
def example_absolute():
root = YGNodeNew()
child = YGNodeNew()
YGNodeStyleSetPositionType(root, YGPositionType.YGPositionTypeAbsolute)
YGNodeStyleSetWidth(root, 200)
YGNodeStyleSetHeight(root, 200)
YGNodeStyleSetJustifyContent(root, YGJustify.YGJustifyCenter)
YGNodeStyleSetAlignItems(root, YGAlign.YGAlignCenter)
YGNodeStyleSetPositionType(child, YGPositionType.YGPositionTypeAbsolute)
YGNodeStyleSetWidth(child, 50)
YGNodeStyleSetHeight(child, 40)
YGNodeInsertChild(root, child, 0)
YGNodeCalculateLayout(root, math.nan, math.nan, YGDirection.YGDirectionLTR)
assert_layout(root, 0, 0, 200, 200)
assert_layout(child, 75, 80, 50, 40)
YGNodeFreeRecursive(root)
def example_measure():
config = YGConfigNew()
root = YGNodeNewWithConfig(config)
text = YGNodeNewWithConfig(config)
YGNodeStyleSetPositionType(root, YGPositionType.YGPositionTypeAbsolute)
YGNodeStyleSetWidth(root, 2000)
YGNodeStyleSetHeight(root, 2000)
YGNodeStyleSetAlignItems(root, YGAlign.YGAlignFlexStart)
YGNodeStyleSetFlexDirection(text, YGFlexDirection.YGFlexDirectionRow)
YGNodeSetContext(text, "Lorem ipsum")
YGNodeSetMeasureFunc(text, intrinsic_size_measure)
YGNodeInsertChild(root, text, 0)
YGNodeCalculateLayout(root, math.nan, math.nan, YGDirection.YGDirectionLTR)
assert_layout(text, 0, 0, 110, 10)
YGNodeFreeRecursive(root)
YGConfigFree(config)
def main():
example_flex_row()
example_wrap()
example_absolute()
example_measure()
print("example.py: OK")
if __name__ == "__main__":
main()