1use super::*;
2
3impl DeterministicTextMeasurer {
5 #[inline(always)]
15 #[must_use]
16 fn style_metrics(style: TextStyle) -> (bool, f32, f32, f32, f32, f32, f32, f32) {
17 let is_mono: bool = style.get_family() == FontFamily::Monospace;
18 let font_size: f32 = style.get_font_size();
19 let bold_factor: f32 = if is_mono {
20 1.0
21 } else if style.get_weight().is_bold() {
22 1.08
23 } else if matches!(style.get_weight(), FontWeight::Medium) {
24 1.03
25 } else {
26 1.0
27 };
28 let letter_spacing: f32 = style.get_letter_spacing();
29 let mono_w: f32 = font_size * MONOSPACE_ADVANCE_RATIO * 1.0 + letter_spacing;
30 let wide_w: f32 = font_size * 1.10 * bold_factor + letter_spacing;
31 let non_ascii_w: f32 = if is_mono {
32 mono_w
33 } else {
34 font_size * 0.85 * bold_factor + letter_spacing
35 };
36 let space_w: f32 = if is_mono {
37 mono_w
38 } else {
39 font_size * ASCII_ADVANCE_LUT[b' ' as usize] * bold_factor + letter_spacing
40 };
41 (
42 is_mono,
43 font_size,
44 bold_factor,
45 letter_spacing,
46 mono_w,
47 wide_w,
48 non_ascii_w,
49 space_w,
50 )
51 }
52
53 #[inline(always)]
68 #[must_use]
69 fn ascii_advance(
70 byte: u8,
71 is_mono: bool,
72 font_size: f32,
73 bold_factor: f32,
74 letter_spacing: f32,
75 mono_w: f32,
76 ) -> f32 {
77 if is_mono {
78 mono_w
79 } else {
80 font_size * ASCII_ADVANCE_LUT[byte as usize] * bold_factor + letter_spacing
81 }
82 }
83
84 #[inline]
95 #[must_use]
96 fn measure_raw_line_width(
97 raw_line: &str,
98 metrics: (bool, f32, f32, f32, f32, f32, f32, f32),
99 ) -> (f32, usize) {
100 let (is_mono, font_size, bold_factor, letter_spacing, mono_w, wide_w, non_ascii_w, _): (
101 bool,
102 f32,
103 f32,
104 f32,
105 f32,
106 f32,
107 f32,
108 f32,
109 ) = metrics;
110 let mut width: f32 = 0.0;
111 if raw_line.is_ascii() {
112 let bytes: &[u8] = raw_line.as_bytes();
113 if is_mono {
114 for _ in bytes.iter() {
115 width += mono_w;
116 }
117 } else {
118 for &blue_val in bytes.iter() {
119 width += font_size * ASCII_ADVANCE_LUT[blue_val as usize] * bold_factor
120 + letter_spacing;
121 }
122 }
123 return (width, bytes.len());
124 }
125 let mut char_count: usize = 0;
126 for ch in raw_line.chars() {
127 let code: u32 = ch as u32;
128 let ch_w: f32 = if matches!(code, 0x200B | 0x200C | 0x200D | 0xFE0E | 0xFE0F) {
129 0.0
130 } else if code < 128 {
131 Self::ascii_advance(
132 code as u8,
133 is_mono,
134 font_size,
135 bold_factor,
136 letter_spacing,
137 mono_w,
138 )
139 } else if is_wide_unicode(ch) {
140 wide_w
141 } else {
142 non_ascii_w
143 };
144 width += ch_w;
145 char_count += 1;
146 }
147 (width, char_count)
148 }
149
150 #[inline]
165 #[must_use]
166 fn ellipsis_line_width(
167 raw_line: &str,
168 width: f32,
169 char_count: usize,
170 style: TextStyle,
171 max_width: Option<f32>,
172 metrics: (bool, f32, f32, f32, f32, f32, f32, f32),
173 ) -> f32 {
174 let (is_mono, font_size, bold_factor, letter_spacing, mono_w, wide_w, non_ascii_w, _): (
175 bool,
176 f32,
177 f32,
178 f32,
179 f32,
180 f32,
181 f32,
182 f32,
183 ) = metrics;
184 if style.get_overflow() == TextOverflow::Ellipsis
185 && let Some(limit) = max_width
186 && width > limit + 0.5
187 && limit > 0.0
188 && char_count > 2
189 {
190 let dot_w: f32 = Self::ascii_advance(
191 ASCII_DOT_BYTE,
192 is_mono,
193 font_size,
194 bold_factor,
195 letter_spacing,
196 mono_w,
197 );
198 let ellipsis_w: f32 = dot_w * 3.0;
199 if width <= ellipsis_w + 1.0 {
200 return width;
201 }
202 let target_w: f32 = (limit - ellipsis_w).max(0.0);
203 let mut acc: f32 = 0.0;
204 for ch in raw_line.chars() {
205 let code: u32 = ch as u32;
206 let ch_w: f32 = if matches!(code, 0x200B | 0x200C | 0x200D | 0xFE0E | 0xFE0F) {
207 0.0
208 } else if code < 128 {
209 Self::ascii_advance(
210 code as u8,
211 is_mono,
212 font_size,
213 bold_factor,
214 letter_spacing,
215 mono_w,
216 )
217 } else if is_wide_unicode(ch) {
218 wide_w
219 } else {
220 non_ascii_w
221 };
222 if acc + ch_w > target_w {
223 break;
224 }
225 acc += ch_w;
226 }
227 return acc + ellipsis_w;
228 }
229 width
230 }
231
232 #[inline(always)]
242 fn flush_wrapped_line(
243 raw_line: &str,
244 space_w: f32,
245 state: &mut (bool, usize, usize, Option<String>, f32, usize, bool),
246 lines: &mut Vec<TextLine>,
247 trim_spaces: bool,
248 ) {
249 let (cur_has_chars, cur_start, cur_end, cur_overflow, cur_w, trailing_spaces, skipped): (
250 &mut bool,
251 &mut usize,
252 &mut usize,
253 &mut Option<String>,
254 &mut f32,
255 &mut usize,
256 &mut bool,
257 ) = (
258 &mut state.0,
259 &mut state.1,
260 &mut state.2,
261 &mut state.3,
262 &mut state.4,
263 &mut state.5,
264 &mut state.6,
265 );
266 if trim_spaces {
267 let trim_count: usize = *trailing_spaces;
268 for _ in 0..trim_count {
269 *cur_w = (*cur_w - space_w).max(0.0);
270 }
271 if let Some(mut buf) = cur_overflow.take() {
272 for _ in 0..trim_count {
273 let _: Option<char> = buf.pop();
274 }
275 lines.push(TextLine::new(buf, *cur_w));
276 } else if *cur_has_chars {
277 let trimmed_end: usize = *cur_end - trim_count;
278 lines.push(TextLine::new(
279 raw_line[*cur_start..trimmed_end].to_string(),
280 *cur_w,
281 ));
282 } else {
283 lines.push(TextLine::new(String::new(), *cur_w));
284 }
285 } else if let Some(buf) = cur_overflow.take() {
286 lines.push(TextLine::new(buf, *cur_w));
287 } else if *cur_has_chars {
288 lines.push(TextLine::new(
289 raw_line[*cur_start..*cur_end].to_string(),
290 *cur_w,
291 ));
292 } else {
293 lines.push(TextLine::new(String::new(), *cur_w));
294 }
295 *cur_has_chars = false;
296 *cur_start = 0;
297 *cur_end = 0;
298 *cur_w = 0.0;
299 *trailing_spaces = 0;
300 *skipped = false;
301 }
302
303 #[inline(always)]
312 fn append_wrapped_segment(
313 raw_line: &str,
314 state: &mut (bool, usize, usize, Option<String>, f32, usize, bool),
315 segment: (usize, usize, f32),
316 is_space: bool,
317 ) {
318 let (seg_start, seg_end, seg_w): (usize, usize, f32) = segment;
319 let (cur_has_chars, cur_start, cur_end, cur_overflow, cur_w, trailing_spaces, skipped): (
320 &mut bool,
321 &mut usize,
322 &mut usize,
323 &mut Option<String>,
324 &mut f32,
325 &mut usize,
326 &mut bool,
327 ) = (
328 &mut state.0,
329 &mut state.1,
330 &mut state.2,
331 &mut state.3,
332 &mut state.4,
333 &mut state.5,
334 &mut state.6,
335 );
336 if !*cur_has_chars {
337 *cur_has_chars = true;
338 *cur_start = seg_start;
339 *cur_end = seg_end;
340 *cur_w = seg_w;
341 *trailing_spaces = usize::from(is_space);
342 *skipped = false;
343 } else if *skipped || cur_overflow.is_some() {
344 if cur_overflow.is_none() {
345 *cur_overflow = Some(raw_line[*cur_start..*cur_end].to_string());
346 }
347 if let Some(buf) = cur_overflow.as_mut() {
348 buf.push_str(&raw_line[seg_start..seg_end]);
349 }
350 *cur_w += seg_w;
351 *trailing_spaces = if is_space { *trailing_spaces + 1 } else { 0 };
352 } else {
353 *cur_end = seg_end;
354 *cur_w += seg_w;
355 *trailing_spaces = if is_space { *trailing_spaces + 1 } else { 0 };
356 }
357 }
358
359 #[inline(always)]
369 fn flush_wrapped_size(
370 space_w: f32,
371 state: &mut (bool, f32, usize),
372 line_count: &mut usize,
373 max_line_w: &mut f32,
374 trim_spaces: bool,
375 ) {
376 if trim_spaces {
377 for _ in 0..state.2 {
378 state.1 = (state.1 - space_w).max(0.0);
379 }
380 }
381 if state.1 > *max_line_w {
382 *max_line_w = state.1;
383 }
384 *line_count += 1;
385 state.0 = false;
386 state.1 = 0.0;
387 state.2 = 0;
388 }
389
390 fn wrap_raw_line_into(
399 raw_line: &str,
400 limit: f32,
401 metrics: (bool, f32, f32, f32, f32, f32, f32, f32),
402 lines: &mut Vec<TextLine>,
403 ) {
404 let (
405 is_mono,
406 font_size,
407 bold_factor,
408 letter_spacing,
409 mono_w,
410 wide_w,
411 non_ascii_w,
412 space_w,
413 ): (bool, f32, f32, f32, f32, f32, f32, f32) = metrics;
414 let mut cur_state: (bool, usize, usize, Option<String>, f32, usize, bool) =
415 (false, 0, 0, None, 0.0, 0, false);
416 let mut word_has_chars: bool = false;
417 let mut word_start: usize = 0;
418 let mut word_end: usize = 0;
419 let mut word_w: f32 = 0.0;
420 if raw_line.is_ascii() {
421 for (byte_idx, &blue_val) in raw_line.as_bytes().iter().enumerate() {
422 let ch_w: f32 = Self::ascii_advance(
423 blue_val,
424 is_mono,
425 font_size,
426 bold_factor,
427 letter_spacing,
428 mono_w,
429 );
430 let ch_end: usize = byte_idx + 1;
431 if blue_val == b' ' {
432 if cur_state.0 && cur_state.4 + word_w > limit {
433 Self::flush_wrapped_line(raw_line, space_w, &mut cur_state, lines, true);
434 }
435 if word_has_chars {
436 Self::append_wrapped_segment(
437 raw_line,
438 &mut cur_state,
439 (word_start, word_end, word_w),
440 false,
441 );
442 word_has_chars = false;
443 word_w = 0.0;
444 }
445 if cur_state.0 {
446 if cur_state.4 + ch_w <= limit {
447 Self::append_wrapped_segment(
448 raw_line,
449 &mut cur_state,
450 (byte_idx, ch_end, ch_w),
451 true,
452 );
453 } else {
454 cur_state.6 = true;
455 }
456 }
457 } else {
458 if cur_state.0 && cur_state.4 + word_w + ch_w > limit && word_w + ch_w > limit {
459 Self::flush_wrapped_line(raw_line, space_w, &mut cur_state, lines, true);
460 }
461 if word_has_chars && !cur_state.0 && word_w + ch_w > limit {
462 lines.push(TextLine::new(
463 raw_line[word_start..word_end].to_string(),
464 word_w,
465 ));
466 word_has_chars = false;
467 word_w = 0.0;
468 }
469 if !word_has_chars {
470 word_start = byte_idx;
471 word_has_chars = true;
472 }
473 word_end = ch_end;
474 word_w += ch_w;
475 }
476 }
477 } else {
478 for (byte_idx, ch) in raw_line.char_indices() {
479 let code: u32 = ch as u32;
480 let zero_w: bool = matches!(code, 0x200B | 0x200C | 0x200D | 0xFE0E | 0xFE0F);
481 let wide: bool = !zero_w && is_wide_unicode(ch);
482 let ch_w: f32 = if zero_w {
483 0.0
484 } else if code < 128 {
485 Self::ascii_advance(
486 code as u8,
487 is_mono,
488 font_size,
489 bold_factor,
490 letter_spacing,
491 mono_w,
492 )
493 } else if wide {
494 wide_w
495 } else {
496 non_ascii_w
497 };
498 let ch_end: usize = byte_idx + ch.len_utf8();
499 if ch == ' ' {
500 if cur_state.0 && cur_state.4 + word_w > limit {
501 Self::flush_wrapped_line(raw_line, space_w, &mut cur_state, lines, true);
502 }
503 if word_has_chars {
504 Self::append_wrapped_segment(
505 raw_line,
506 &mut cur_state,
507 (word_start, word_end, word_w),
508 false,
509 );
510 word_has_chars = false;
511 word_w = 0.0;
512 }
513 if cur_state.0 {
514 if cur_state.4 + ch_w <= limit {
515 Self::append_wrapped_segment(
516 raw_line,
517 &mut cur_state,
518 (byte_idx, ch_end, ch_w),
519 true,
520 );
521 } else {
522 cur_state.6 = true;
523 }
524 }
525 } else if wide {
526 if word_has_chars {
527 if cur_state.0 && cur_state.4 + word_w > limit {
528 Self::flush_wrapped_line(
529 raw_line,
530 space_w,
531 &mut cur_state,
532 lines,
533 true,
534 );
535 }
536 Self::append_wrapped_segment(
537 raw_line,
538 &mut cur_state,
539 (word_start, word_end, word_w),
540 false,
541 );
542 word_has_chars = false;
543 word_w = 0.0;
544 }
545 if cur_state.0 && cur_state.4 + ch_w > limit {
546 Self::flush_wrapped_line(raw_line, space_w, &mut cur_state, lines, false);
547 }
548 Self::append_wrapped_segment(
549 raw_line,
550 &mut cur_state,
551 (byte_idx, ch_end, ch_w),
552 false,
553 );
554 } else {
555 if cur_state.0 && cur_state.4 + word_w + ch_w > limit && word_w + ch_w > limit {
556 Self::flush_wrapped_line(raw_line, space_w, &mut cur_state, lines, true);
557 }
558 if word_has_chars && !cur_state.0 && word_w + ch_w > limit {
559 lines.push(TextLine::new(
560 raw_line[word_start..word_end].to_string(),
561 word_w,
562 ));
563 word_has_chars = false;
564 word_w = 0.0;
565 }
566 if !word_has_chars {
567 word_start = byte_idx;
568 word_has_chars = true;
569 }
570 word_end = ch_end;
571 word_w += ch_w;
572 }
573 }
574 }
575 if word_has_chars {
576 if cur_state.0 && cur_state.4 + word_w > limit {
577 Self::flush_wrapped_line(raw_line, space_w, &mut cur_state, lines, true);
578 }
579 Self::append_wrapped_segment(
580 raw_line,
581 &mut cur_state,
582 (word_start, word_end, word_w),
583 false,
584 );
585 }
586 Self::flush_wrapped_line(raw_line, space_w, &mut cur_state, lines, true);
587 }
588
589 fn measure_wrapped_line_size(
599 raw_line: &str,
600 limit: f32,
601 metrics: (bool, f32, f32, f32, f32, f32, f32, f32),
602 line_count: &mut usize,
603 max_line_w: &mut f32,
604 ) {
605 let (
606 is_mono,
607 font_size,
608 bold_factor,
609 letter_spacing,
610 mono_w,
611 wide_w,
612 non_ascii_w,
613 space_w,
614 ): (bool, f32, f32, f32, f32, f32, f32, f32) = metrics;
615 let mut cur_state: (bool, f32, usize) = (false, 0.0, 0);
616 let mut word_has_chars: bool = false;
617 let mut word_w: f32 = 0.0;
618 if raw_line.is_ascii() {
619 for &blue_val in raw_line.as_bytes().iter() {
620 let ch_w: f32 = Self::ascii_advance(
621 blue_val,
622 is_mono,
623 font_size,
624 bold_factor,
625 letter_spacing,
626 mono_w,
627 );
628 if blue_val == b' ' {
629 if cur_state.0 && cur_state.1 + word_w > limit {
630 Self::flush_wrapped_size(
631 space_w,
632 &mut cur_state,
633 line_count,
634 max_line_w,
635 true,
636 );
637 }
638 if word_has_chars {
639 cur_state.0 = true;
640 cur_state.1 += word_w;
641 cur_state.2 = 0;
642 word_has_chars = false;
643 word_w = 0.0;
644 }
645 if cur_state.0 && cur_state.1 + ch_w <= limit {
646 cur_state.1 += ch_w;
647 cur_state.2 += 1;
648 }
649 } else {
650 if cur_state.0 && cur_state.1 + word_w + ch_w > limit && word_w + ch_w > limit {
651 Self::flush_wrapped_size(
652 space_w,
653 &mut cur_state,
654 line_count,
655 max_line_w,
656 true,
657 );
658 }
659 if word_has_chars && !cur_state.0 && word_w + ch_w > limit {
660 if word_w > *max_line_w {
661 *max_line_w = word_w;
662 }
663 *line_count += 1;
664 word_w = 0.0;
665 }
666 word_has_chars = true;
667 word_w += ch_w;
668 }
669 }
670 } else {
671 for ch in raw_line.chars() {
672 let code: u32 = ch as u32;
673 let zero_w: bool = matches!(code, 0x200B | 0x200C | 0x200D | 0xFE0E | 0xFE0F);
674 let wide: bool = !zero_w && is_wide_unicode(ch);
675 let ch_w: f32 = if zero_w {
676 0.0
677 } else if code < 128 {
678 Self::ascii_advance(
679 code as u8,
680 is_mono,
681 font_size,
682 bold_factor,
683 letter_spacing,
684 mono_w,
685 )
686 } else if wide {
687 wide_w
688 } else {
689 non_ascii_w
690 };
691 if ch == ' ' {
692 if cur_state.0 && cur_state.1 + word_w > limit {
693 Self::flush_wrapped_size(
694 space_w,
695 &mut cur_state,
696 line_count,
697 max_line_w,
698 true,
699 );
700 }
701 if word_has_chars {
702 cur_state.0 = true;
703 cur_state.1 += word_w;
704 cur_state.2 = 0;
705 word_has_chars = false;
706 word_w = 0.0;
707 }
708 if cur_state.0 && cur_state.1 + ch_w <= limit {
709 cur_state.1 += ch_w;
710 cur_state.2 += 1;
711 }
712 } else if wide {
713 if word_has_chars {
714 if cur_state.0 && cur_state.1 + word_w > limit {
715 Self::flush_wrapped_size(
716 space_w,
717 &mut cur_state,
718 line_count,
719 max_line_w,
720 true,
721 );
722 }
723 cur_state.0 = true;
724 cur_state.1 += word_w;
725 cur_state.2 = 0;
726 word_has_chars = false;
727 word_w = 0.0;
728 }
729 if cur_state.0 && cur_state.1 + ch_w > limit {
730 Self::flush_wrapped_size(
731 space_w,
732 &mut cur_state,
733 line_count,
734 max_line_w,
735 false,
736 );
737 }
738 cur_state.0 = true;
739 cur_state.1 += ch_w;
740 cur_state.2 = 0;
741 } else {
742 if cur_state.0 && cur_state.1 + word_w + ch_w > limit && word_w + ch_w > limit {
743 Self::flush_wrapped_size(
744 space_w,
745 &mut cur_state,
746 line_count,
747 max_line_w,
748 true,
749 );
750 }
751 if word_has_chars && !cur_state.0 && word_w + ch_w > limit {
752 if word_w > *max_line_w {
753 *max_line_w = word_w;
754 }
755 *line_count += 1;
756 word_w = 0.0;
757 }
758 word_has_chars = true;
759 word_w += ch_w;
760 }
761 }
762 }
763 if word_has_chars {
764 if cur_state.0 && cur_state.1 + word_w > limit {
765 Self::flush_wrapped_size(space_w, &mut cur_state, line_count, max_line_w, true);
766 }
767 cur_state.0 = true;
768 cur_state.1 += word_w;
769 cur_state.2 = 0;
770 }
771 Self::flush_wrapped_size(space_w, &mut cur_state, line_count, max_line_w, true);
772 }
773
774 fn push_unwrapped_line(
784 raw_line: &str,
785 style: TextStyle,
786 max_width: Option<f32>,
787 metrics: (bool, f32, f32, f32, f32, f32, f32, f32),
788 lines: &mut Vec<TextLine>,
789 ) {
790 let (width, char_count): (f32, usize) = Self::measure_raw_line_width(raw_line, metrics);
791 if style.get_overflow() == TextOverflow::Ellipsis
792 && let Some(limit) = max_width
793 && width > limit + 0.5
794 && limit > 0.0
795 && char_count > 2
796 {
797 let (is_mono, font_size, bold_factor, letter_spacing, mono_w, wide_w, non_ascii_w, _): (
798 bool,
799 f32,
800 f32,
801 f32,
802 f32,
803 f32,
804 f32,
805 f32,
806 ) = metrics;
807 let dot_w: f32 = Self::ascii_advance(
808 ASCII_DOT_BYTE,
809 is_mono,
810 font_size,
811 bold_factor,
812 letter_spacing,
813 mono_w,
814 );
815 let ellipsis_w: f32 = dot_w * 3.0;
816 if width <= ellipsis_w + 1.0 {
817 lines.push(TextLine::new(raw_line.to_string(), width));
818 } else {
819 let target_w: f32 = (limit - ellipsis_w).max(0.0);
820 let mut acc: f32 = 0.0;
821 let mut cut_bytes: usize = 0;
822 for (byte_idx, ch) in raw_line.char_indices() {
823 let code: u32 = ch as u32;
824 let ch_w: f32 = if matches!(code, 0x200B | 0x200C | 0x200D | 0xFE0E | 0xFE0F) {
825 0.0
826 } else if code < 128 {
827 Self::ascii_advance(
828 code as u8,
829 is_mono,
830 font_size,
831 bold_factor,
832 letter_spacing,
833 mono_w,
834 )
835 } else if is_wide_unicode(ch) {
836 wide_w
837 } else {
838 non_ascii_w
839 };
840 if acc + ch_w > target_w {
841 break;
842 }
843 acc += ch_w;
844 cut_bytes = byte_idx + ch.len_utf8();
845 }
846 let mut truncated: String = String::with_capacity(cut_bytes + 3);
847 truncated.push_str(&raw_line[..cut_bytes]);
848 truncated.push_str("...");
849 lines.push(TextLine::new(truncated, acc + ellipsis_w));
850 }
851 } else {
852 lines.push(TextLine::new(raw_line.to_string(), width));
853 }
854 }
855
856 #[must_use]
869 pub(crate) fn try_single_line<'a>(
870 &self,
871 text: &'a str,
872 style: TextStyle,
873 max_width: Option<f32>,
874 ) -> Option<(&'a str, f32)> {
875 if text.is_empty() || text.as_bytes().contains(&b'\n') {
876 return None;
877 }
878 let line_height_px: f32 = (style.get_font_size() * style.get_line_height()).max(1.0);
879 match style.get_overflow() {
880 TextOverflow::Clip => Some((text, line_height_px)),
881 TextOverflow::Ellipsis => {
882 if let Some(limit) = max_width
883 && limit > 0.0
884 {
885 let metrics: (bool, f32, f32, f32, f32, f32, f32, f32) =
886 Self::style_metrics(style);
887 let (width, char_count): (f32, usize) =
888 Self::measure_raw_line_width(text, metrics);
889 if width > limit + 0.5 && char_count > 2 {
890 return None;
891 }
892 }
893 Some((text, line_height_px))
894 }
895 TextOverflow::Wrap => {
896 if let Some(raw_limit) = max_width.filter(|width_val: &f32| *width_val > 0.0) {
897 if text.starts_with(' ') || text.ends_with(' ') {
898 return None;
899 }
900 let metrics: (bool, f32, f32, f32, f32, f32, f32, f32) =
901 Self::style_metrics(style);
902 let (width, _): (f32, usize) = Self::measure_raw_line_width(text, metrics);
903 let limit: f32 = raw_limit.max(1.0) + WRAP_WIDTH_EPSILON;
904 if width + 1e-3 > limit {
905 return None;
906 }
907 }
908 Some((text, line_height_px))
909 }
910 }
911 }
912}
913
914impl TextMeasurer for DeterministicTextMeasurer {
916 fn measure(&self, text: &str, style: TextStyle, max_width: Option<f32>) -> TextLayoutResult {
929 let line_height_px: f32 = (style.get_font_size() * style.get_line_height()).max(1.0);
930 if text.is_empty() {
931 return TextLayoutResult::new(
932 Vec::new(),
933 Size::new(0.0, line_height_px),
934 line_height_px,
935 );
936 }
937 let metrics: (bool, f32, f32, f32, f32, f32, f32, f32) = Self::style_metrics(style);
938 let wrap_limit: Option<f32> = match style.get_overflow() {
939 TextOverflow::Wrap => max_width.filter(|width_val: &f32| *width_val > 0.0),
940 TextOverflow::Clip | TextOverflow::Ellipsis => None,
941 };
942 let single_raw_line: bool = !text.as_bytes().contains(&b'\n');
943 if single_raw_line && wrap_limit.is_none() {
944 let mut lines: Vec<TextLine> = Vec::with_capacity(1);
945 Self::push_unwrapped_line(text, style, max_width, metrics, &mut lines);
946 let line_w: f32 = lines[0].get_width();
947 return TextLayoutResult::new(lines, Size::new(line_w, line_height_px), line_height_px);
948 }
949 let mut lines: Vec<TextLine> = Vec::with_capacity(if single_raw_line { 1 } else { 4 });
950 if let Some(raw_limit) = wrap_limit {
951 let limit: f32 = raw_limit.max(1.0) + WRAP_WIDTH_EPSILON;
952 if single_raw_line {
953 Self::wrap_raw_line_into(text, limit, metrics, &mut lines);
954 } else {
955 for raw_line in text.split('\n') {
956 Self::wrap_raw_line_into(raw_line, limit, metrics, &mut lines);
957 }
958 }
959 } else {
960 for raw_line in text.split('\n') {
961 Self::push_unwrapped_line(raw_line, style, max_width, metrics, &mut lines);
962 }
963 }
964 let max_line_w: f32 = lines.iter().fold(0.0_f32, |acc: f32, line: &TextLine| {
965 acc.max(line.get_width())
966 });
967 let total_h: f32 = (lines.len().max(1) as f32) * line_height_px;
968 TextLayoutResult::new(lines, Size::new(max_line_w, total_h), line_height_px)
969 }
970
971 fn measure_size(&self, text: &str, style: TextStyle, max_width: Option<f32>) -> Size {
984 let line_height_px: f32 = (style.get_font_size() * style.get_line_height()).max(1.0);
985 if text.is_empty() {
986 return Size::new(0.0, line_height_px);
987 }
988 let metrics: (bool, f32, f32, f32, f32, f32, f32, f32) = Self::style_metrics(style);
989 let wrap_limit: Option<f32> = match style.get_overflow() {
990 TextOverflow::Wrap => max_width.filter(|width_val: &f32| *width_val > 0.0),
991 TextOverflow::Clip | TextOverflow::Ellipsis => None,
992 };
993 let single_raw_line: bool = !text.as_bytes().contains(&b'\n');
994 if single_raw_line && wrap_limit.is_none() {
995 let (width, char_count): (f32, usize) = Self::measure_raw_line_width(text, metrics);
996 let final_w: f32 =
997 Self::ellipsis_line_width(text, width, char_count, style, max_width, metrics);
998 return Size::new(final_w, line_height_px);
999 }
1000 let mut line_count: usize = 0;
1001 let mut max_line_w: f32 = 0.0;
1002 if let Some(raw_limit) = wrap_limit {
1003 let limit: f32 = raw_limit.max(1.0) + WRAP_WIDTH_EPSILON;
1004 if single_raw_line {
1005 Self::measure_wrapped_line_size(
1006 text,
1007 limit,
1008 metrics,
1009 &mut line_count,
1010 &mut max_line_w,
1011 );
1012 } else {
1013 for raw_line in text.split('\n') {
1014 Self::measure_wrapped_line_size(
1015 raw_line,
1016 limit,
1017 metrics,
1018 &mut line_count,
1019 &mut max_line_w,
1020 );
1021 }
1022 }
1023 } else {
1024 for raw_line in text.split('\n') {
1025 let (width, char_count): (f32, usize) =
1026 Self::measure_raw_line_width(raw_line, metrics);
1027 let final_w: f32 = Self::ellipsis_line_width(
1028 raw_line, width, char_count, style, max_width, metrics,
1029 );
1030 if final_w > max_line_w {
1031 max_line_w = final_w;
1032 }
1033 line_count += 1;
1034 }
1035 }
1036 let total_h: f32 = (line_count.max(1) as f32) * line_height_px;
1037 Size::new(max_line_w, total_h)
1038 }
1039}