@@ -13,8 +13,7 @@ pub use float_pigment_forest::Len;
13
13
use float_pigment_forest:: { layout:: LayoutPosition , node:: Length , * } ;
14
14
use float_pigment_layout:: DefLength ;
15
15
use float_pigment_mlp:: {
16
- context:: Context ,
17
- context:: Parse ,
16
+ context:: { Context , Parse } ,
18
17
node:: { attribute:: Attribute , NodeType } ,
19
18
} ;
20
19
@@ -98,6 +97,125 @@ impl Default for TestCtx {
98
97
}
99
98
}
100
99
100
+ #[ inline( always) ]
101
+ fn is_block_tag ( tag : & str ) -> bool {
102
+ tag == "div" || tag == "view"
103
+ }
104
+
105
+ #[ inline( always) ]
106
+ fn is_measure_text_slot ( tag : & str ) -> bool {
107
+ tag == "text-slot"
108
+ }
109
+ #[ derive( Debug , Default , Clone ) ]
110
+ struct TextInfo {
111
+ font_size : f32 ,
112
+ // raw_text: String
113
+ text_len : usize ,
114
+ }
115
+
116
+ impl TextInfo {
117
+ fn measure (
118
+ & self ,
119
+ min_width : Len ,
120
+ min_height : Len ,
121
+ max_width : Len ,
122
+ max_height : Len ,
123
+ max_content_width : Len ,
124
+ max_content_height : Len ,
125
+ ) -> Size < Len > {
126
+ let text_len = self . text_len ;
127
+ let text_width = self . font_size * text_len as f32 ;
128
+ let max_w = max_width. min ( max_content_width) ;
129
+ let max_h = max_height. min ( max_content_height) ;
130
+ let measured_width;
131
+ let measured_height;
132
+ if text_width <= max_w {
133
+ // single line
134
+ measured_width = Len :: from_f32 ( text_width) ;
135
+ measured_height = Len :: from_f32 ( self . font_size ) ;
136
+ } else {
137
+ // multi line
138
+ let mut row_count = ( max_w. to_f32 ( ) / self . font_size ) . floor ( ) ;
139
+ if row_count < 1. {
140
+ row_count = 1. ;
141
+ }
142
+ let col_count = ( text_len as f32 / row_count) . ceil ( ) ;
143
+ measured_width = Len :: from_f32 ( ( row_count * self . font_size ) as i32 as f32 ) ;
144
+ measured_height = Len :: from_f32 ( ( col_count * self . font_size ) as i32 as f32 ) ;
145
+ }
146
+ println ! (
147
+ "text_info: {:?}, width: {:?} ~ {:?}, height: {:?} ~ {:?}, max_content_width: {:?}, max_content_height: {:?}, measured_width: {:?}, measured_height: {:?}" ,
148
+ self ,
149
+ min_width,
150
+ max_width,
151
+ min_height,
152
+ max_height,
153
+ max_content_width,
154
+ max_content_height,
155
+ measured_width,
156
+ measured_height,
157
+ ) ;
158
+ Size :: new ( measured_width, measured_height. min ( max_h) )
159
+ }
160
+ }
161
+
162
+ struct TextInfoBuilder ( TextInfo ) ;
163
+
164
+ impl TextInfoBuilder {
165
+ fn new ( ) -> Self {
166
+ Self ( TextInfo {
167
+ font_size : 16. ,
168
+ // raw_text: String::new(),
169
+ text_len : 0 ,
170
+ } )
171
+ }
172
+ // fn with_text(mut self, text: String) -> Self {
173
+ // self.0.raw_text = text;
174
+ // self
175
+ // }
176
+ fn with_text_len ( mut self , text_len : usize ) -> Self {
177
+ self . 0 . text_len = text_len;
178
+ self
179
+ }
180
+ fn with_font_size ( mut self , font_size : f32 ) -> Self {
181
+ self . 0 . font_size = font_size;
182
+ self
183
+ }
184
+ fn build ( self ) -> TextInfo {
185
+ self . 0
186
+ }
187
+ }
188
+
189
+ #[ inline( always) ]
190
+ fn prepare_measure_node ( node : * mut Node , text_info : TextInfo ) {
191
+ let node = unsafe { & mut * node } ;
192
+ node. set_measure_func ( Some ( Box :: new (
193
+ move |_,
194
+ max_width,
195
+ _,
196
+ max_height,
197
+ _,
198
+ min_width,
199
+ min_height,
200
+ max_content_width,
201
+ max_content_height| {
202
+ text_info. measure (
203
+ min_width,
204
+ min_height,
205
+ max_width,
206
+ max_height,
207
+ max_content_width,
208
+ max_content_height,
209
+ )
210
+ } ,
211
+ ) ) ) ;
212
+ unsafe {
213
+ node. set_display ( Display :: Inline ) ;
214
+ node. set_baseline_func ( Some ( Box :: new ( |_, _, _| Len :: from_f32 ( 16. ) ) ) ) ;
215
+ node. set_node_type ( float_pigment_forest:: NodeType :: Text ) ;
216
+ }
217
+ }
218
+
101
219
impl TestCtx {
102
220
pub fn new ( ) -> Self {
103
221
Self {
@@ -241,50 +359,45 @@ impl TestCtx {
241
359
parent_node_props : Option < & NodeProperties > ,
242
360
) -> NodePtr {
243
361
match cur {
244
- NodeType :: Text ( _t ) => {
362
+ NodeType :: Text ( t ) => {
245
363
let node = Node :: new_ptr ( ) ;
246
- ( * node) . set_display ( Display :: Inline ) ;
247
- // TODO
248
- ( * node) . set_measure_func ( Some ( Box :: new (
249
- |_,
250
- max_width,
251
- _,
252
- max_height,
253
- _,
254
- min_width,
255
- min_height,
256
- max_content_width,
257
- max_content_height| {
258
- println ! (
259
- "width: {:?} ~ {:?}, height: {:?} ~ {:?}, max_content_width: {:?}, max_content_height: {:?}" ,
260
- min_width,
261
- max_width,
262
- min_height,
263
- max_height,
264
- max_content_width,
265
- max_content_height
266
- ) ;
267
- Size :: new ( Len :: from_f32 ( 32. ) , Len :: from_f32 ( 16. ) )
268
- } ,
269
- ) ) ) ;
270
- ( * node) . set_baseline_func ( Some ( Box :: new ( |_, _, _| Len :: from_f32 ( 16. ) ) ) ) ;
271
- ( * node) . set_node_type ( float_pigment_forest:: NodeType :: Text ) ;
272
-
364
+ let text_info = TextInfoBuilder :: new ( ) . with_text_len ( t. text ( ) . len ( ) ) . build ( ) ;
365
+ prepare_measure_node ( node, text_info) ;
273
366
node
274
367
}
275
368
NodeType :: Element ( e) => {
276
369
let node = Node :: new_ptr ( ) ;
277
- match e. tag ( ) {
278
- "div" => { }
279
- _ => ( * node ) . set_display ( float_pigment_css :: typing :: Display :: Inline ) ,
280
- } ;
370
+ if ! is_block_tag ( e. tag ( ) ) {
371
+ ( * node ) . set_display ( float_pigment_css :: typing :: Display :: Inline ) ;
372
+ }
373
+
281
374
let mut node_props = NodeProperties :: new ( parent_node_props) ;
282
375
if let Some ( style) = e. attributes ( ) . get ( "style" ) {
283
376
unsafe {
284
377
TestCtx :: set_style ( & * node, & style, & mut node_props, parent_node_props) ;
285
378
}
286
379
}
287
380
self . set_expect_layout_pos ( node, e. attributes ( ) ) ;
381
+
382
+ if is_measure_text_slot ( e. tag ( ) ) {
383
+ let node = Node :: new_ptr ( ) ;
384
+ let text_len = e
385
+ . attributes ( )
386
+ . get ( "len" )
387
+ . map ( |v| v. parse :: < usize > ( ) . unwrap ( ) )
388
+ . unwrap_or ( 0 ) ;
389
+ let font_size = e
390
+ . attributes ( )
391
+ . get ( "fontSize" )
392
+ . map ( |v| v. parse :: < f32 > ( ) . unwrap ( ) )
393
+ . unwrap_or ( 16. ) ;
394
+ let text_info = TextInfoBuilder :: new ( )
395
+ . with_text_len ( text_len)
396
+ . with_font_size ( font_size)
397
+ . build ( ) ;
398
+ prepare_measure_node ( node, text_info) ;
399
+ }
400
+
288
401
e. children_mut ( ) . iter ( ) . for_each ( |item| {
289
402
let child = self . create_node_recursive ( item, Some ( & node_props) ) ;
290
403
( * node) . append_child ( child) ;
0 commit comments