-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathexternal.rs
More file actions
287 lines (264 loc) · 8.93 KB
/
external.rs
File metadata and controls
287 lines (264 loc) · 8.93 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
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
use crate::{convert_node_ref_to_ptr, ffi::MeasureMode};
use float_pigment_css::length_num::LengthNum;
use float_pigment_layout::Size;
#[cfg(not(debug_assertions))]
use crate::ffi::{
ExternalDirtyCallback, ExternalGetBaseline, ExternalMeasure, ExternalResolveCalc,
};
use crate::{Len, Node};
#[cfg(debug_assertions)]
use mock_external::{
ExternalDirtyCallback, ExternalGetBaseline, ExternalMeasure, ExternalResolveCalc,
};
fn to_f32_with_max_to_infinity(v: Len) -> f32 {
if v == Len::MAX {
f32::INFINITY
} else {
v.to_f32()
}
}
pub(crate) fn get_baseline_impl(node: &Node, width: Len, height: Len) -> Option<Len> {
#[allow(unused_unsafe)]
unsafe {
let baseline = ExternalGetBaseline(
convert_node_ref_to_ptr(node) as crate::ffi::NodePtr,
width.to_f32(),
height.to_f32(),
);
Some(Len::from_f32(baseline))
}
}
pub(crate) fn dirty_callback_impl(node: &Node) {
#[allow(unused_unsafe)]
unsafe {
ExternalDirtyCallback(convert_node_ref_to_ptr(node) as crate::ffi::NodePtr)
};
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn measure_impl(
node: &Node,
max_width: Len,
width_mode: MeasureMode,
max_height: Len,
height_mode: MeasureMode,
min_width: Len,
min_height: Len,
max_content_width: Len,
max_content_height: Len,
) -> Option<Size<Len>> {
#[allow(unused_unsafe)]
unsafe {
let size = ExternalMeasure(
convert_node_ref_to_ptr(node) as crate::ffi::NodePtr,
to_f32_with_max_to_infinity(max_width),
width_mode,
to_f32_with_max_to_infinity(max_height),
height_mode,
min_width.to_f32(),
min_height.to_f32(),
to_f32_with_max_to_infinity(max_content_width),
to_f32_with_max_to_infinity(max_content_height),
);
Some(Size::new(
Len::from_f32(size.width),
Len::from_f32(size.height),
))
}
}
pub(crate) fn resolve_calc_impl(node: &Node, calc_handle: i32, owner: Len) -> Len {
#[allow(unused_unsafe)]
unsafe {
Len::from_f32(ExternalResolveCalc(
convert_node_ref_to_ptr(node) as crate::ffi::NodePtr,
calc_handle,
owner.to_f32(),
))
}
}
#[cfg(debug_assertions)]
pub mod mock_external {
#[derive(Debug, Default, Clone)]
pub struct TextInfo {
pub(crate) font_size: f32,
// raw_text: String
pub(crate) text_len: usize,
}
impl TextInfo {
pub(crate) fn measure(
&self,
min_width: f32,
min_height: f32,
max_width: f32,
max_height: f32,
max_content_width: f32,
max_content_height: f32,
) -> (f32, f32) {
let text_len = self.text_len;
let text_width = self.font_size * text_len as f32;
let max_w = max_width.min(max_content_width);
let max_h = max_height.min(max_content_height);
let measured_width;
let measured_height;
if text_width <= max_w {
// single line
measured_width = text_width;
measured_height = self.font_size;
} else {
// multi line
let mut row_count = (max_w.to_f32() / self.font_size).floor();
if row_count < 1. {
row_count = 1.;
}
let col_count = (text_len as f32 / row_count).ceil();
measured_width = (row_count * self.font_size) as i32 as f32;
measured_height = (col_count * self.font_size) as i32 as f32;
}
println!(
"text_info: {self:?}, width: {min_width:?} ~ {max_width:?}, height: {min_height:?} ~ {max_height:?}, max_content_width: {max_content_width:?}, max_content_height: {max_content_height:?}, measured_width: {measured_width:?}, measured_height: {measured_height:?}"
);
(measured_width, measured_height.min(max_h))
}
}
pub struct TextInfoBuilder(TextInfo);
impl Default for TextInfoBuilder {
fn default() -> Self {
Self::new()
}
}
impl TextInfoBuilder {
pub fn new() -> Self {
Self(TextInfo {
font_size: 16.,
// raw_text: String::new(),
text_len: 0,
})
}
pub fn with_text_len(mut self, text_len: usize) -> Self {
self.0.text_len = text_len;
self
}
pub fn with_font_size(mut self, font_size: f32) -> Self {
self.0.font_size = font_size;
self
}
pub fn set_font_size(&mut self, font_size: f32) {
self.0.font_size = font_size;
}
pub fn build(self) -> TextInfo {
self.0
}
}
use std::{cell::RefCell, collections::HashMap};
use float_pigment_css::length_num::LengthNum;
use crate::{ffi::MeasureMode, get_ref_from_node_ptr, Node, NodePtr};
pub enum MeasureType {
Text(TextInfo),
SpecifiedSize((f32, f32)),
#[allow(clippy::type_complexity)]
Custom(
Box<
dyn Fn(&Node, f32, MeasureMode, f32, MeasureMode, f32, f32, f32, f32) -> (f32, f32),
>,
),
}
thread_local! {
static COLLECTION: RefCell<HashMap<*mut Node, MeasureType>> = RefCell::new(HashMap::new());
}
pub fn set_node_measure_type(node: *mut Node, measure_type: MeasureType) {
COLLECTION.with(|collection| {
let mut collection = collection.borrow_mut();
collection.insert(node, measure_type);
});
}
#[allow(non_snake_case)]
pub(crate) fn ExternalGetBaseline(_node: *mut (), _width: f32, height: f32) -> f32 {
let mut res = 0.;
COLLECTION.with(|collection| {
let collection = collection.borrow();
if let Some(measure_type) = collection.get(&(_node as *mut Node)) {
match measure_type {
MeasureType::Text(text_info) => {
res = text_info.font_size;
}
_ => res = height,
}
}
});
res
}
#[allow(clippy::too_many_arguments)]
#[allow(non_snake_case)]
pub(crate) fn ExternalMeasure(
node: *mut (),
max_width: f32,
_width_mode: MeasureMode,
max_height: f32,
_height_mode: MeasureMode,
min_width: f32,
min_height: f32,
max_content_width: f32,
max_content_height: f32,
) -> crate::ffi::Size {
let mut res = crate::ffi::Size {
width: 0.,
height: 0.,
};
COLLECTION.with(|collection| {
let collection = collection.borrow();
if let Some(measure_type) = collection.get(&(node as *mut Node)) {
match measure_type {
MeasureType::Text(text_info) => {
let (width, height) = text_info.measure(
min_width,
min_height,
max_width,
max_height,
max_content_width,
max_content_height,
);
res = crate::ffi::Size { width, height };
}
MeasureType::SpecifiedSize((width, height)) => {
res = crate::ffi::Size {
width: *width,
height: *height,
};
}
MeasureType::Custom(func) => {
let (width, height) = func(
unsafe { get_ref_from_node_ptr(node as NodePtr) },
max_width,
_width_mode,
max_height,
_height_mode,
min_width,
min_height,
max_content_width,
max_content_height,
);
res = crate::ffi::Size { width, height }
}
}
}
});
res
}
#[allow(non_snake_case)]
pub(crate) fn ExternalResolveCalc(_node: *mut (), _calc_handle: i32, _owner: f32) -> f32 {
todo!()
}
#[allow(non_snake_case)]
pub(crate) fn ExternalDirtyCallback(node: *mut ()) {
use crate::DumpNode;
println!("trigger dirty callback of Node: {}", unsafe {
get_ref_from_node_ptr(node as *mut Node).dump_to_html(
crate::DumpOptions {
recursive: false,
layout: true,
style: crate::DumpStyleMode::None,
},
0,
)
});
}
}