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 &b in bytes.iter() {
119 width +=
120 font_size * ASCII_ADVANCE_LUT[b as usize] * bold_factor + 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, &b) in raw_line.as_bytes().iter().enumerate() {
422 let ch_w: f32 =
423 Self::ascii_advance(b, is_mono, font_size, bold_factor, letter_spacing, mono_w);
424 let ch_end: usize = byte_idx + 1;
425 if b == b' ' {
426 if cur_state.0 && cur_state.4 + word_w > limit {
427 Self::flush_wrapped_line(raw_line, space_w, &mut cur_state, lines, true);
428 }
429 if word_has_chars {
430 Self::append_wrapped_segment(
431 raw_line,
432 &mut cur_state,
433 (word_start, word_end, word_w),
434 false,
435 );
436 word_has_chars = false;
437 word_w = 0.0;
438 }
439 if cur_state.0 {
440 if cur_state.4 + ch_w <= limit {
441 Self::append_wrapped_segment(
442 raw_line,
443 &mut cur_state,
444 (byte_idx, ch_end, ch_w),
445 true,
446 );
447 } else {
448 cur_state.6 = true;
449 }
450 }
451 } else {
452 if cur_state.0 && cur_state.4 + word_w + ch_w > limit && word_w + ch_w > limit {
453 Self::flush_wrapped_line(raw_line, space_w, &mut cur_state, lines, true);
454 }
455 if word_has_chars && !cur_state.0 && word_w + ch_w > limit {
456 lines.push(TextLine::new(
457 raw_line[word_start..word_end].to_string(),
458 word_w,
459 ));
460 word_has_chars = false;
461 word_w = 0.0;
462 }
463 if !word_has_chars {
464 word_start = byte_idx;
465 word_has_chars = true;
466 }
467 word_end = ch_end;
468 word_w += ch_w;
469 }
470 }
471 } else {
472 for (byte_idx, ch) in raw_line.char_indices() {
473 let code: u32 = ch as u32;
474 let zero_w: bool = matches!(code, 0x200B | 0x200C | 0x200D | 0xFE0E | 0xFE0F);
475 let wide: bool = !zero_w && is_wide_unicode(ch);
476 let ch_w: f32 = if zero_w {
477 0.0
478 } else if code < 128 {
479 Self::ascii_advance(
480 code as u8,
481 is_mono,
482 font_size,
483 bold_factor,
484 letter_spacing,
485 mono_w,
486 )
487 } else if wide {
488 wide_w
489 } else {
490 non_ascii_w
491 };
492 let ch_end: usize = byte_idx + ch.len_utf8();
493 if ch == ' ' {
494 if cur_state.0 && cur_state.4 + word_w > limit {
495 Self::flush_wrapped_line(raw_line, space_w, &mut cur_state, lines, true);
496 }
497 if word_has_chars {
498 Self::append_wrapped_segment(
499 raw_line,
500 &mut cur_state,
501 (word_start, word_end, word_w),
502 false,
503 );
504 word_has_chars = false;
505 word_w = 0.0;
506 }
507 if cur_state.0 {
508 if cur_state.4 + ch_w <= limit {
509 Self::append_wrapped_segment(
510 raw_line,
511 &mut cur_state,
512 (byte_idx, ch_end, ch_w),
513 true,
514 );
515 } else {
516 cur_state.6 = true;
517 }
518 }
519 } else if wide {
520 if word_has_chars {
521 if cur_state.0 && cur_state.4 + word_w > limit {
522 Self::flush_wrapped_line(
523 raw_line,
524 space_w,
525 &mut cur_state,
526 lines,
527 true,
528 );
529 }
530 Self::append_wrapped_segment(
531 raw_line,
532 &mut cur_state,
533 (word_start, word_end, word_w),
534 false,
535 );
536 word_has_chars = false;
537 word_w = 0.0;
538 }
539 if cur_state.0 && cur_state.4 + ch_w > limit {
540 Self::flush_wrapped_line(raw_line, space_w, &mut cur_state, lines, false);
541 }
542 Self::append_wrapped_segment(
543 raw_line,
544 &mut cur_state,
545 (byte_idx, ch_end, ch_w),
546 false,
547 );
548 } else {
549 if cur_state.0 && cur_state.4 + word_w + ch_w > limit && word_w + ch_w > limit {
550 Self::flush_wrapped_line(raw_line, space_w, &mut cur_state, lines, true);
551 }
552 if word_has_chars && !cur_state.0 && word_w + ch_w > limit {
553 lines.push(TextLine::new(
554 raw_line[word_start..word_end].to_string(),
555 word_w,
556 ));
557 word_has_chars = false;
558 word_w = 0.0;
559 }
560 if !word_has_chars {
561 word_start = byte_idx;
562 word_has_chars = true;
563 }
564 word_end = ch_end;
565 word_w += ch_w;
566 }
567 }
568 }
569 if word_has_chars {
570 if cur_state.0 && cur_state.4 + word_w > limit {
571 Self::flush_wrapped_line(raw_line, space_w, &mut cur_state, lines, true);
572 }
573 Self::append_wrapped_segment(
574 raw_line,
575 &mut cur_state,
576 (word_start, word_end, word_w),
577 false,
578 );
579 }
580 Self::flush_wrapped_line(raw_line, space_w, &mut cur_state, lines, true);
581 }
582
583 fn measure_wrapped_line_size(
593 raw_line: &str,
594 limit: f32,
595 metrics: (bool, f32, f32, f32, f32, f32, f32, f32),
596 line_count: &mut usize,
597 max_line_w: &mut f32,
598 ) {
599 let (
600 is_mono,
601 font_size,
602 bold_factor,
603 letter_spacing,
604 mono_w,
605 wide_w,
606 non_ascii_w,
607 space_w,
608 ): (bool, f32, f32, f32, f32, f32, f32, f32) = metrics;
609 let mut cur_state: (bool, f32, usize) = (false, 0.0, 0);
610 let mut word_has_chars: bool = false;
611 let mut word_w: f32 = 0.0;
612 if raw_line.is_ascii() {
613 for &b in raw_line.as_bytes().iter() {
614 let ch_w: f32 =
615 Self::ascii_advance(b, is_mono, font_size, bold_factor, letter_spacing, mono_w);
616 if b == b' ' {
617 if cur_state.0 && cur_state.1 + word_w > limit {
618 Self::flush_wrapped_size(
619 space_w,
620 &mut cur_state,
621 line_count,
622 max_line_w,
623 true,
624 );
625 }
626 if word_has_chars {
627 cur_state.0 = true;
628 cur_state.1 += word_w;
629 cur_state.2 = 0;
630 word_has_chars = false;
631 word_w = 0.0;
632 }
633 if cur_state.0 && cur_state.1 + ch_w <= limit {
634 cur_state.1 += ch_w;
635 cur_state.2 += 1;
636 }
637 } else {
638 if cur_state.0 && cur_state.1 + word_w + ch_w > limit && word_w + ch_w > limit {
639 Self::flush_wrapped_size(
640 space_w,
641 &mut cur_state,
642 line_count,
643 max_line_w,
644 true,
645 );
646 }
647 if word_has_chars && !cur_state.0 && word_w + ch_w > limit {
648 if word_w > *max_line_w {
649 *max_line_w = word_w;
650 }
651 *line_count += 1;
652 word_w = 0.0;
653 }
654 word_has_chars = true;
655 word_w += ch_w;
656 }
657 }
658 } else {
659 for ch in raw_line.chars() {
660 let code: u32 = ch as u32;
661 let zero_w: bool = matches!(code, 0x200B | 0x200C | 0x200D | 0xFE0E | 0xFE0F);
662 let wide: bool = !zero_w && is_wide_unicode(ch);
663 let ch_w: f32 = if zero_w {
664 0.0
665 } else if code < 128 {
666 Self::ascii_advance(
667 code as u8,
668 is_mono,
669 font_size,
670 bold_factor,
671 letter_spacing,
672 mono_w,
673 )
674 } else if wide {
675 wide_w
676 } else {
677 non_ascii_w
678 };
679 if ch == ' ' {
680 if cur_state.0 && cur_state.1 + word_w > limit {
681 Self::flush_wrapped_size(
682 space_w,
683 &mut cur_state,
684 line_count,
685 max_line_w,
686 true,
687 );
688 }
689 if word_has_chars {
690 cur_state.0 = true;
691 cur_state.1 += word_w;
692 cur_state.2 = 0;
693 word_has_chars = false;
694 word_w = 0.0;
695 }
696 if cur_state.0 && cur_state.1 + ch_w <= limit {
697 cur_state.1 += ch_w;
698 cur_state.2 += 1;
699 }
700 } else if wide {
701 if word_has_chars {
702 if cur_state.0 && cur_state.1 + word_w > limit {
703 Self::flush_wrapped_size(
704 space_w,
705 &mut cur_state,
706 line_count,
707 max_line_w,
708 true,
709 );
710 }
711 cur_state.0 = true;
712 cur_state.1 += word_w;
713 cur_state.2 = 0;
714 word_has_chars = false;
715 word_w = 0.0;
716 }
717 if cur_state.0 && cur_state.1 + ch_w > limit {
718 Self::flush_wrapped_size(
719 space_w,
720 &mut cur_state,
721 line_count,
722 max_line_w,
723 false,
724 );
725 }
726 cur_state.0 = true;
727 cur_state.1 += ch_w;
728 cur_state.2 = 0;
729 } else {
730 if cur_state.0 && cur_state.1 + word_w + ch_w > limit && word_w + ch_w > limit {
731 Self::flush_wrapped_size(
732 space_w,
733 &mut cur_state,
734 line_count,
735 max_line_w,
736 true,
737 );
738 }
739 if word_has_chars && !cur_state.0 && word_w + ch_w > limit {
740 if word_w > *max_line_w {
741 *max_line_w = word_w;
742 }
743 *line_count += 1;
744 word_w = 0.0;
745 }
746 word_has_chars = true;
747 word_w += ch_w;
748 }
749 }
750 }
751 if word_has_chars {
752 if cur_state.0 && cur_state.1 + word_w > limit {
753 Self::flush_wrapped_size(space_w, &mut cur_state, line_count, max_line_w, true);
754 }
755 cur_state.0 = true;
756 cur_state.1 += word_w;
757 cur_state.2 = 0;
758 }
759 Self::flush_wrapped_size(space_w, &mut cur_state, line_count, max_line_w, true);
760 }
761
762 fn push_unwrapped_line(
772 raw_line: &str,
773 style: TextStyle,
774 max_width: Option<f32>,
775 metrics: (bool, f32, f32, f32, f32, f32, f32, f32),
776 lines: &mut Vec<TextLine>,
777 ) {
778 let (width, char_count): (f32, usize) = Self::measure_raw_line_width(raw_line, metrics);
779 if style.get_overflow() == TextOverflow::Ellipsis
780 && let Some(limit) = max_width
781 && width > limit + 0.5
782 && limit > 0.0
783 && char_count > 2
784 {
785 let (is_mono, font_size, bold_factor, letter_spacing, mono_w, wide_w, non_ascii_w, _): (
786 bool,
787 f32,
788 f32,
789 f32,
790 f32,
791 f32,
792 f32,
793 f32,
794 ) = metrics;
795 let dot_w: f32 = Self::ascii_advance(
796 ASCII_DOT_BYTE,
797 is_mono,
798 font_size,
799 bold_factor,
800 letter_spacing,
801 mono_w,
802 );
803 let ellipsis_w: f32 = dot_w * 3.0;
804 if width <= ellipsis_w + 1.0 {
805 lines.push(TextLine::new(raw_line.to_string(), width));
806 } else {
807 let target_w: f32 = (limit - ellipsis_w).max(0.0);
808 let mut acc: f32 = 0.0;
809 let mut cut_bytes: usize = 0;
810 for (byte_idx, ch) in raw_line.char_indices() {
811 let code: u32 = ch as u32;
812 let ch_w: f32 = if matches!(code, 0x200B | 0x200C | 0x200D | 0xFE0E | 0xFE0F) {
813 0.0
814 } else if code < 128 {
815 Self::ascii_advance(
816 code as u8,
817 is_mono,
818 font_size,
819 bold_factor,
820 letter_spacing,
821 mono_w,
822 )
823 } else if is_wide_unicode(ch) {
824 wide_w
825 } else {
826 non_ascii_w
827 };
828 if acc + ch_w > target_w {
829 break;
830 }
831 acc += ch_w;
832 cut_bytes = byte_idx + ch.len_utf8();
833 }
834 let mut truncated: String = String::with_capacity(cut_bytes + 3);
835 truncated.push_str(&raw_line[..cut_bytes]);
836 truncated.push_str("...");
837 lines.push(TextLine::new(truncated, acc + ellipsis_w));
838 }
839 } else {
840 lines.push(TextLine::new(raw_line.to_string(), width));
841 }
842 }
843
844 #[must_use]
857 pub(crate) fn try_single_line<'a>(
858 &self,
859 text: &'a str,
860 style: TextStyle,
861 max_width: Option<f32>,
862 ) -> Option<(&'a str, f32)> {
863 if text.is_empty() || text.as_bytes().contains(&b'\n') {
864 return None;
865 }
866 let line_height_px: f32 = (style.get_font_size() * style.get_line_height()).max(1.0);
867 match style.get_overflow() {
868 TextOverflow::Clip => Some((text, line_height_px)),
869 TextOverflow::Ellipsis => {
870 if let Some(limit) = max_width
871 && limit > 0.0
872 {
873 let metrics: (bool, f32, f32, f32, f32, f32, f32, f32) =
874 Self::style_metrics(style);
875 let (width, char_count): (f32, usize) =
876 Self::measure_raw_line_width(text, metrics);
877 if width > limit + 0.5 && char_count > 2 {
878 return None;
879 }
880 }
881 Some((text, line_height_px))
882 }
883 TextOverflow::Wrap => {
884 if let Some(raw_limit) = max_width.filter(|w: &f32| *w > 0.0) {
885 if text.starts_with(' ') || text.ends_with(' ') {
886 return None;
887 }
888 let metrics: (bool, f32, f32, f32, f32, f32, f32, f32) =
889 Self::style_metrics(style);
890 let (width, _): (f32, usize) = Self::measure_raw_line_width(text, metrics);
891 let limit: f32 = raw_limit.max(1.0) + WRAP_WIDTH_EPSILON;
892 if width + 1e-3 > limit {
893 return None;
894 }
895 }
896 Some((text, line_height_px))
897 }
898 }
899 }
900}
901
902impl TextMeasurer for DeterministicTextMeasurer {
904 fn measure(&self, text: &str, style: TextStyle, max_width: Option<f32>) -> TextLayoutResult {
917 let line_height_px: f32 = (style.get_font_size() * style.get_line_height()).max(1.0);
918 if text.is_empty() {
919 return TextLayoutResult::new(
920 Vec::new(),
921 Size::new(0.0, line_height_px),
922 line_height_px,
923 );
924 }
925 let metrics: (bool, f32, f32, f32, f32, f32, f32, f32) = Self::style_metrics(style);
926 let wrap_limit: Option<f32> = match style.get_overflow() {
927 TextOverflow::Wrap => max_width.filter(|w: &f32| *w > 0.0),
928 TextOverflow::Clip | TextOverflow::Ellipsis => None,
929 };
930 let single_raw_line: bool = !text.as_bytes().contains(&b'\n');
931 if single_raw_line && wrap_limit.is_none() {
932 let mut lines: Vec<TextLine> = Vec::with_capacity(1);
933 Self::push_unwrapped_line(text, style, max_width, metrics, &mut lines);
934 let line_w: f32 = lines[0].get_width();
935 return TextLayoutResult::new(lines, Size::new(line_w, line_height_px), line_height_px);
936 }
937 let mut lines: Vec<TextLine> = Vec::with_capacity(if single_raw_line { 1 } else { 4 });
938 if let Some(raw_limit) = wrap_limit {
939 let limit: f32 = raw_limit.max(1.0) + WRAP_WIDTH_EPSILON;
940 if single_raw_line {
941 Self::wrap_raw_line_into(text, limit, metrics, &mut lines);
942 } else {
943 for raw_line in text.split('\n') {
944 Self::wrap_raw_line_into(raw_line, limit, metrics, &mut lines);
945 }
946 }
947 } else {
948 for raw_line in text.split('\n') {
949 Self::push_unwrapped_line(raw_line, style, max_width, metrics, &mut lines);
950 }
951 }
952 let max_line_w: f32 = lines.iter().fold(0.0_f32, |acc: f32, line: &TextLine| {
953 acc.max(line.get_width())
954 });
955 let total_h: f32 = (lines.len().max(1) as f32) * line_height_px;
956 TextLayoutResult::new(lines, Size::new(max_line_w, total_h), line_height_px)
957 }
958
959 fn measure_size(&self, text: &str, style: TextStyle, max_width: Option<f32>) -> Size {
972 let line_height_px: f32 = (style.get_font_size() * style.get_line_height()).max(1.0);
973 if text.is_empty() {
974 return Size::new(0.0, line_height_px);
975 }
976 let metrics: (bool, f32, f32, f32, f32, f32, f32, f32) = Self::style_metrics(style);
977 let wrap_limit: Option<f32> = match style.get_overflow() {
978 TextOverflow::Wrap => max_width.filter(|w: &f32| *w > 0.0),
979 TextOverflow::Clip | TextOverflow::Ellipsis => None,
980 };
981 let single_raw_line: bool = !text.as_bytes().contains(&b'\n');
982 if single_raw_line && wrap_limit.is_none() {
983 let (width, char_count): (f32, usize) = Self::measure_raw_line_width(text, metrics);
984 let final_w: f32 =
985 Self::ellipsis_line_width(text, width, char_count, style, max_width, metrics);
986 return Size::new(final_w, line_height_px);
987 }
988 let mut line_count: usize = 0;
989 let mut max_line_w: f32 = 0.0;
990 if let Some(raw_limit) = wrap_limit {
991 let limit: f32 = raw_limit.max(1.0) + WRAP_WIDTH_EPSILON;
992 if single_raw_line {
993 Self::measure_wrapped_line_size(
994 text,
995 limit,
996 metrics,
997 &mut line_count,
998 &mut max_line_w,
999 );
1000 } else {
1001 for raw_line in text.split('\n') {
1002 Self::measure_wrapped_line_size(
1003 raw_line,
1004 limit,
1005 metrics,
1006 &mut line_count,
1007 &mut max_line_w,
1008 );
1009 }
1010 }
1011 } else {
1012 for raw_line in text.split('\n') {
1013 let (width, char_count): (f32, usize) =
1014 Self::measure_raw_line_width(raw_line, metrics);
1015 let final_w: f32 = Self::ellipsis_line_width(
1016 raw_line, width, char_count, style, max_width, metrics,
1017 );
1018 if final_w > max_line_w {
1019 max_line_w = final_w;
1020 }
1021 line_count += 1;
1022 }
1023 }
1024 let total_h: f32 = (line_count.max(1) as f32) * line_height_px;
1025 Size::new(max_line_w, total_h)
1026 }
1027}