-
Notifications
You must be signed in to change notification settings - Fork 534
Expand file tree
/
Copy pathtext_render.cc
More file actions
938 lines (897 loc) · 37.4 KB
/
Copy pathtext_render.cc
File metadata and controls
938 lines (897 loc) · 37.4 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
// Copyright 2023 The Lynx Authors. All rights reserved.
// Licensed under the Apache License Version 2.0 that can be found in the
// LICENSE file in the root directory of this source tree.
#include "clay/ui/shadow/text_render.h"
#include <algorithm>
#include <cmath>
#include <cstddef>
#include <cstdlib>
#include <limits>
#include <optional>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#include "base/trace/native/trace_event.h"
#include "clay/fml/logging.h"
#include "clay/gfx/geometry/float_rect.h"
#include "clay/public/value.h"
#include "clay/ui/common/attribute_utils.h"
#include "clay/ui/common/isolate.h"
#include "clay/ui/component/inline_image_view.h"
#include "clay/ui/component/text/layout_context.h"
#include "clay/ui/component/text/text_paragraph_builder.h"
#include "clay/ui/component/text/text_style.h"
#include "clay/ui/component/view.h"
#include "clay/ui/rendering/render_container.h"
#include "clay/ui/rendering/render_image.h"
#include "clay/ui/resource/font_collection.h"
#include "clay/ui/shadow/icu_substitute.h"
#include "clay/ui/shadow/inline_image_shadow_node.h"
#include "clay/ui/shadow/inline_text_shadow_node.h"
#include "clay/ui/shadow/inline_truncation_shadow_node.h"
#include "clay/ui/shadow/inline_view_shadow_node.h"
#include "clay/ui/shadow/measure_utils.h"
#include "clay/ui/shadow/raw_text_shadow_node.h"
#include "clay/ui/shadow/text_shadow_node.h"
#if defined(CLAY_ENABLE_TTTEXT)
#include "clay/third_party/txt/src/tttext/paragraph_tt_text.h"
#endif
namespace clay {
namespace utils = attribute_utils;
constexpr float kLayoutTolerance = 1.f;
static constexpr float kDefaultFontSizeInDip = 14.f;
static bool HasInlineTruncationShadowNode(ShadowNode* node) {
if (!node) {
return false;
}
for (auto* child : node->GetChildren()) {
if (child->IsInlineTruncationShadowNode() ||
HasInlineTruncationShadowNode(child)) {
return true;
}
}
return false;
}
static bool InlineTruncationTextBoxFits(
const txt::Paragraph::TextBox& box, double layout_width,
double visible_bottom, double target_line_top, double target_line_bottom,
std::optional<double>& first_box_top, bool require_same_top) {
if (box.rect.Width() <= kLayoutTolerance &&
box.rect.Height() <= kLayoutTolerance) {
return true;
}
if (box.rect.Left() < -kLayoutTolerance ||
box.rect.Right() > layout_width + kLayoutTolerance ||
box.rect.Bottom() < -kLayoutTolerance ||
box.rect.Top() > visible_bottom + kLayoutTolerance ||
box.rect.Bottom() < target_line_top - kLayoutTolerance ||
box.rect.Top() > target_line_bottom + kLayoutTolerance) {
return false;
}
if (require_same_top) {
if (first_box_top.has_value() &&
std::abs(box.rect.Top() - first_box_top.value()) > kLayoutTolerance) {
return false;
}
if (!first_box_top.has_value()) {
first_box_top = box.rect.Top();
}
}
return true;
}
static bool InlineTruncationTextBoxesFit(txt::Paragraph* paragraph,
ShadowNode* node, double layout_width,
double visible_bottom,
double target_line_top,
double target_line_bottom,
std::optional<double>& first_box_top) {
if (!paragraph || !node) {
return false;
}
if (node->IsInlineTextShadowNode()) {
auto* inline_text_node = static_cast<InlineTextShadowNode*>(node);
for (const auto& range : inline_text_node->range_in_paragraph_) {
if (range.end() > range.start()) {
auto last_boxes =
paragraph->GetRectsForRange(range.end() - 1, range.end(),
txt::Paragraph::RectHeightStyle::kTight,
txt::Paragraph::RectWidthStyle::kTight);
if (last_boxes.empty()) {
return false;
}
}
auto boxes = paragraph->GetRectsForRange(
range.start(), range.end(), txt::Paragraph::RectHeightStyle::kTight,
txt::Paragraph::RectWidthStyle::kTight);
for (const auto& box : boxes) {
if (!InlineTruncationTextBoxFits(box, layout_width, visible_bottom,
target_line_top, target_line_bottom,
first_box_top, true)) {
return false;
}
}
}
} else if (node->IsInlineImageShadowNode() ||
node->IsInlineViewShadowNode()) {
size_t start_glyph = 0;
size_t end_glyph = 0;
if (node->IsInlineImageShadowNode()) {
auto* inline_image_node = static_cast<InlineImageShadowNode*>(node);
start_glyph = inline_image_node->StartGlyph();
end_glyph = inline_image_node->EndGlyph();
} else {
auto* inline_view_node = static_cast<InlineViewShadowNode*>(node);
start_glyph = inline_view_node->StartGlyph();
end_glyph = inline_view_node->EndGlyph();
}
if (end_glyph <= start_glyph) {
return false;
}
auto boxes = paragraph->GetRectsForRange(
start_glyph, end_glyph, txt::Paragraph::RectHeightStyle::kTight,
txt::Paragraph::RectWidthStyle::kTight);
if (boxes.empty()) {
return false;
}
for (const auto& box : boxes) {
if (!InlineTruncationTextBoxFits(box, layout_width, visible_bottom,
target_line_top, target_line_bottom,
first_box_top, false)) {
return false;
}
}
}
for (auto* child : node->GetChildren()) {
if (!InlineTruncationTextBoxesFit(paragraph, child, layout_width,
visible_bottom, target_line_top,
target_line_bottom, first_box_top)) {
return false;
}
}
return true;
}
clay::Value TextRender::GetTextInfo(const char* text,
const clay::Value& params) {
const auto& info = utils::GetMap(params);
TextStyle text_style;
auto pixel_ratio = utils::GetInt(utils::GetMapItem(info, "pixelRatio"));
if (pixel_ratio == 0) {
pixel_ratio = 1;
}
auto font_size_str = utils::GetCString(utils::GetMapItem(info, "fontSize"));
char* endptr;
double num = std::strtod(font_size_str.c_str(), &endptr);
if (endptr == font_size_str.c_str()) {
// TODO(ZhuChengCheng) There may be problems here:
// The front end receive this size with unit 'px', which should be the same
// meaning in the css, i.e. measuring precess. thus, the unit here shuld be
// the same as the platform unit, i.e. Android px, iOS pt.
text_style.font_size = kDefaultFontSizeInDip * pixel_ratio;
} else {
text_style.font_size = num * pixel_ratio;
}
double layout_width = std::numeric_limits<double>::max();
auto it_max_width = info.find("maxWidth");
if (it_max_width != info.end()) {
auto layout_width_str = utils::GetCString(it_max_width->second);
layout_width = std::strtod(layout_width_str.c_str(), &endptr);
if (endptr == layout_width_str.c_str()) {
layout_width = std::numeric_limits<double>::max();
}
}
auto it_max_len = info.find("maxLine");
if (it_max_len != info.end()) {
text_style.max_lines = utils::GetDouble(it_max_len->second);
} else {
text_style.max_lines = 1;
}
auto builder = std::make_unique<TextParagraphBuilder>(true, text_style);
builder->AddText(UnicodeUtil::Utf8ToUtf16(text));
auto paragraph = Build(std::move(builder));
paragraph->Layout(layout_width);
auto line_metrics = paragraph->GetLineMetrics();
clay::Value::Array content_array(line_metrics.size());
for (size_t i = 0; i < content_array.size(); i++) {
auto value = clay::Value(
std::string(text + line_metrics[i].start_index,
line_metrics[i].end_index - line_metrics[i].start_index));
content_array[i] = std::move(value);
}
clay::Value::Map ret;
ret.emplace("width", clay::Value(std::min(
layout_width, paragraph->GetMaxIntrinsicWidth())));
ret.emplace("content", clay::Value(std::move(content_array)));
return clay::Value(std::move(ret));
}
void TextRender::MeasureText(const std::string& text, bool show_content,
const std::optional<double>& max_width,
const std::optional<uint32_t>& max_length,
const TextStyle& text_style, float* measured_width,
float* measured_height,
std::vector<std::string>* measured_content) {
auto builder = std::make_unique<TextParagraphBuilder>(true, text_style);
builder->PushStyle(text_style);
LayoutContextText measure_context;
measure_context.SetBuilder(builder.get());
uint32_t max_length_value = max_length.value_or(0);
float text_indent_value = text_style.text_indent.value_or(0.f);
if (max_length_value > 0) {
measure_context.SetMaxLength(max_length_value);
}
if (text_indent_value > 0.f) {
measure_context.SetTextIndent(text_indent_value);
}
measure_context.AddText(UnicodeUtil::Utf8ToUtf16(text),
text_indent_value > 0.f);
auto paragraph = Build(std::move(builder));
double layout_width = max_width.has_value()
? max_width.value()
: std::numeric_limits<double>::max();
paragraph->Layout(layout_width);
if (measured_width) {
*measured_width = std::min(layout_width, paragraph->GetMaxIntrinsicWidth());
}
if (measured_height) {
if (text_style.line_spacing.has_value() &&
paragraph->GetLineMetrics().size() > 0) {
auto line_spacing =
std::max(text_style.line_spacing.value() -
paragraph->GetLineMetrics().begin()->ascent -
paragraph->GetLineMetrics().begin()->descent,
0.0);
*measured_height = std::ceil(paragraph->GetHeight() - line_spacing);
} else {
*measured_height = std::ceil(paragraph->GetHeight());
}
}
if (show_content && measured_content) {
size_t indent = text_indent_value > 0.f ? 1 : 0;
auto line_metrics = paragraph->GetLineMetrics();
if (!line_metrics.empty() && indent) {
auto first_line = line_metrics.begin();
measured_content->emplace_back(
text.c_str() + first_line->start_index,
first_line->end_index - first_line->start_index - indent);
line_metrics.erase(line_metrics.begin());
}
std::unique_ptr<txt::LineMetrics> last_line;
if (!line_metrics.empty() && max_length_value > 0 &&
max_length_value < text.length()) {
last_line = std::make_unique<txt::LineMetrics>(line_metrics.back());
line_metrics.pop_back();
}
for (auto& line_metric : line_metrics) {
measured_content->emplace_back(
text.c_str() + line_metric.start_index - indent,
line_metric.end_index - line_metric.start_index);
}
if (last_line) {
measured_content->emplace_back(
text.c_str() + last_line->start_index - indent,
last_line->end_index - last_line->start_index - 1 /*ellipsis size*/);
}
}
}
TextAlignment TextRender::EffectAlign() {
auto text_align =
measure_node_->text_style_->text_align.value_or(TextAlignment::kStart);
auto text_direction =
measure_node_->text_style_->text_direction.value_or(TextDirection::kLtr);
if (text_align == TextAlignment::kStart) {
return (text_direction == TextDirection::kLtr) ? TextAlignment::kLeft
: TextAlignment::kRight;
} else if (text_align == TextAlignment::kEnd) {
return (text_direction == TextDirection::kLtr) ? TextAlignment::kRight
: TextAlignment::kLeft;
} else {
return text_align;
}
}
void TextRender::Measure(const MeasureConstraint& constraint,
ShadowLayoutContextMeasure* context) {
inline_truncation_hidden_count_ = -1;
BuildTextLayout(constraint, context);
if (constraint.width_mode == TextMeasureMode::kIndefinite &&
measure_node_->text_style_) {
// Do second layout if the width mode is indefinite and the
// alignment is not left. Because in this case, the paragraph_
// won't have the actual line and nothing will be painted.
if (measure_node_->GetResolvedTextAlign() != TextAlignment::kLeft) {
measure_node_->SetNeedSecondLayout(true);
}
}
if (constraint.width_mode == TextMeasureMode::kAtMost &&
context->measured_width_ + kLayoutTolerance < context->layout_width_) {
// Do second layout if actual text width is less than constraints.
// For example, given at most 200px width and actually 100px is needed,
// use 100px to layout again. Otherwise text align will be problem.
// Note: Here maybe some optimizations to avoid second layout.
measure_node_->SetNeedSecondLayout(true);
}
if (cache_paragraph_) {
HandleAutoSize(constraint, context);
HandleInlineTruncation(constraint, context);
}
}
std::vector<LineInfo> TextRender::GetLineInfo() {
auto res = std::vector<LineInfo>{};
if (cache_paragraph_ == nullptr) {
return res;
}
auto line_metrics = cache_paragraph_->GetLineMetrics();
for (auto line_metric : line_metrics) {
res.emplace_back(LineInfo{static_cast<int>(line_metric.start_index),
static_cast<int>(line_metric.end_index), 0});
}
if (res.size() == 0) {
return res;
}
int text_length = static_cast<int>(end_glyph_position_);
if (res.back().end < text_length) {
res.back().ellipsis_count = text_length - res.back().end + 1;
}
if (inline_truncation_hidden_count_ >= 0) {
res.back().ellipsis_count = inline_truncation_hidden_count_;
}
return res;
}
std::unique_ptr<txt::Paragraph> TextRender::LayoutParagraph(
double layout_width) {
TRACE_EVENT("clay", "TextRender::LayoutParagraph");
#ifndef CLAY_ENABLE_TTTEXT
ReprocessAttributeIfNeeded(layout_width);
#endif
auto text_style = measure_node_->text_style_.value();
if (text_style.white_space == WhiteSpace::kNoWrap) {
text_style.max_lines = 1;
if (text_style.overflow != TextOverflow::kEllipsis) {
layout_width = std::numeric_limits<float>::infinity();
}
text_style.text_align = TextAlignment::kLeft;
}
auto raw_text = measure_node_->GetRawText();
if (!text_style.text_direction.has_value() && !raw_text.empty() &&
icu_substitute::IsRTLCharacter(std::u16string(1, raw_text.front()))) {
text_style.text_direction = TextDirection::kRtl;
truncation_direction_ = TextDirection::kRtl;
} else {
truncation_direction_ =
text_style.text_direction.value_or(TextDirection::kLtr);
}
auto builder = std::make_unique<TextParagraphBuilder>(true, text_style);
LayoutContextText context_text;
if (measure_node_->max_length_.has_value()) {
context_text.SetMaxLength(measure_node_->max_length_.value());
}
context_text.SetBuilder(builder.get());
context_text.SetTextIndent(
measure_node_->text_style_->text_indent.value_or(0));
measure_node_->TextLayout(&context_text);
measure_node_->SetInlineEmojiInfo(context_text.TakeInlineEmojiInfo());
auto paragraph = Build(std::move(builder));
TRACE_EVENT("clay", "TextRender::Layout");
#if defined(CLAY_ENABLE_TTTEXT) && (defined(OS_WIN) || defined(OS_MAC))
auto* impl = static_cast<txt::ParagraphTTText*>(paragraph.get());
impl->SetNeedTrimSpace(true);
#endif
paragraph->Layout(layout_width);
end_glyph_position_ = context_text.TextSizeIncludingPlaceholders();
return paragraph;
}
void TextRender::BuildTextLayout(const MeasureConstraint& constraint,
LayoutContext* context) {
TRACE_EVENT("clay", "TextRender::BuildTextLayout");
auto* context_measure = static_cast<ShadowLayoutContextMeasure*>(context);
auto layout_width = context_measure->layout_width_;
const bool force_rebuild = update_flag_ != TextUpdateFlag::kUpdateFlagNone;
const bool should_layout =
force_rebuild || prev_layout_width_ != layout_width;
if (force_rebuild) {
measure_node_->PreLayout(nullptr);
}
std::unique_ptr<txt::Paragraph> paragraph;
if (should_layout) {
if (measure_node_->text_indent_ != 0) {
if (measure_node_->text_indent_use_percent_) {
measure_node_->text_style_->text_indent =
measure_node_->text_indent_ * layout_width;
} else {
measure_node_->text_style_->text_indent = measure_node_->text_indent_;
}
}
if (measure_node_->text_style_->white_space == WhiteSpace::kNoWrap) {
if (measure_node_->text_style_->text_align == TextAlignment::kCenter) {
measure_node_->SetTextPaintAlign(TextAlignment::kCenter);
} else if (measure_node_->GetResolvedTextAlign() ==
TextAlignment::kRight) {
measure_node_->SetTextPaintAlign(TextAlignment::kRight);
} else {
FML_DCHECK(measure_node_->GetResolvedTextAlign() ==
TextAlignment::kLeft);
measure_node_->SetTextPaintAlign(TextAlignment::kLeft);
}
} else {
measure_node_->SetTextPaintAlign(TextAlignment::kLeft);
}
paragraph = LayoutParagraph(layout_width);
cache_paragraph_ = std::move(paragraph);
measured_width_ = std::ceil(cache_paragraph_->GetMaxIntrinsicWidth());
if (measure_node_->text_style_->line_spacing.has_value() &&
cache_paragraph_->GetLineMetrics().size() > 0) {
#ifndef CLAY_ENABLE_TTTEXT
auto line_spacing =
std::max(measure_node_->text_style_->line_spacing.value() -
cache_paragraph_->GetLineMetrics().begin()->ascent -
cache_paragraph_->GetLineMetrics().begin()->descent,
0.0);
measured_height_ =
std::ceil(cache_paragraph_->GetHeight() - line_spacing);
#else
measured_height_ = std::ceil(cache_paragraph_->GetHeight());
#endif
} else {
measured_height_ = std::ceil(cache_paragraph_->GetHeight());
}
}
if (context_measure) {
context_measure->measured_width_ = measured_width_;
context_measure->measured_height_ = measured_height_;
}
update_flag_ = TextUpdateFlag::kUpdateFlagNone;
prev_layout_width_ = layout_width;
}
#ifndef CLAY_ENABLE_TTTEXT
void TextRender::ReprocessAttributeIfNeeded(double layout_width) {
// Determine if the baseline_shift property needs to be set
for (auto* child : measure_node_->GetChildren()) {
if (!child->IsRawTextShadowNode() &&
child->GetVerticalAlign().has_value()) {
if (child->GetVerticalAlign()->type ==
VerticalAlignType::kVerticalAlignLength) {
child->SetBaselineOffset(child->GetVerticalAlign()->length);
continue;
}
TextStyle style = measure_node_->text_style_.value();
// According to lynx's current implementation of the vertical-align
// attribute,
// the largest text among all texts is aligned. Maybe it is more
// reasonable to align the largest text on the same line.
style.font_size = GetMaxFontSize();
auto parent_paragraph = LayoutXCharacter(layout_width, style);
auto line_metrics = parent_paragraph->GetLineMetrics();
if (line_metrics.empty()) {
return;
}
auto parent_run_metrics = line_metrics.front().run_metrics;
if (parent_run_metrics.empty()) {
return;
}
auto box = parent_paragraph->GetRectsForRange(
0, 1, txt::Paragraph::RectHeightStyle::kTight,
txt::Paragraph::RectWidthStyle::kTight);
if (box.empty()) {
return;
}
FontMetrics parent_metrics{
0 - parent_paragraph->GetLineMetrics().front().ascent,
parent_paragraph->GetLineMetrics().front().descent,
parent_run_metrics.begin()->second.font_metrics.fXHeight,
measure_node_->text_style_->line_height.value_or(0.f),
0.f,
static_cast<float>(parent_paragraph->GetHeight()),
box[0].rect.Top(),
box[0].rect.Bottom()};
if (child->IsInlineTextShadowNode()) {
static_cast<InlineTextShadowNode*>(child)->EnsureDefaultStyle();
TextStyle child_text_style =
static_cast<InlineTextShadowNode*>(child)->text_style_.value();
auto child_paragraph = LayoutXCharacter(layout_width, child_text_style);
auto child_line_metrics = child_paragraph->GetLineMetrics();
if (child_line_metrics.empty()) {
continue;
}
auto child_run_metrics = child_line_metrics.front().run_metrics;
if (child_run_metrics.empty()) {
continue;
}
child->SetBaselineOffset(
parent_metrics,
child_run_metrics.begin()->second.font_metrics.fDescent,
child_run_metrics.begin()->second.font_metrics.fAscent);
} else {
// inline-image need to get real width and height from lynx
if (child->IsInlineImageShadowNode()) {
child->UpdateLayoutStylesFromLynx();
}
child->SetBaselineOffset(
parent_metrics, 0,
0 - (child->Height() + child->MarginTop() + child->MarginBottom()));
}
}
}
}
#endif
std::shared_ptr<txt::Paragraph> TextRender::LayoutXCharacter(
double layout_width, const TextStyle& style) {
auto builder = std::make_unique<TextParagraphBuilder>(true, style);
builder->PushStyle(style);
// To get the x-height we default the layout "x"
std::u16string text = u"x";
builder->AddText(text);
auto paragraph = Build(std::move(builder));
paragraph->Layout(layout_width);
return paragraph;
}
float TextRender::GetMaxFontSize() {
auto res = measure_node_->text_style_->font_size.value();
for (auto* child : measure_node_->GetChildren()) {
if (child->IsInlineTextShadowNode()) {
static_cast<InlineTextShadowNode*>(child)->EnsureDefaultStyle();
res = std::max(res, static_cast<InlineTextShadowNode*>(child)
->text_style_->font_size.value());
}
}
return res;
}
void TextRender::HandleAutoSize(const MeasureConstraint& constraint,
ShadowLayoutContextMeasure* context) {
if (measure_node_->enable_auto_font_size_) {
if (HasInlineTruncationShadowNode(measure_node_)) {
return;
}
if (measure_node_->auto_font_size_preset_sizes_.empty() &&
(measure_node_->auto_font_size_step_granularity_ <= 0 ||
measure_node_->auto_font_size_min_size_ <= 0)) {
return;
}
if (!CheckTextFullyDisplayed(constraint, context)) {
TryShrinkFontSize(constraint, context);
} else {
TryExpandFontSize(constraint, context);
}
}
}
bool TextRender::CheckTextFullyDisplayed(
const MeasureConstraint& constraint,
ShadowLayoutContextMeasure* context_measure) {
bool exceed_maxlines = measure_node_->text_style_->max_lines.has_value() &&
cache_paragraph_->DidExceedMaxLines();
bool height_has_overflow =
constraint.height_mode != TextMeasureMode::kIndefinite &&
context_measure->measured_height_ > constraint.height;
bool width_has_overflow = false;
if (measure_node_->text_style_->white_space &&
measure_node_->text_style_->white_space == WhiteSpace::kNoWrap) {
width_has_overflow =
cache_paragraph_->GetMinIntrinsicWidth() > constraint.width;
}
return !(exceed_maxlines || height_has_overflow || width_has_overflow);
}
void TextRender::TryShrinkFontSize(
const MeasureConstraint& constraint,
ShadowLayoutContextMeasure* context_measure) {
// FIXME: if the number of auto_font_size_preset_sizes_ is too much, we
// should use dichotomy to find target
if (!measure_node_->auto_font_size_preset_sizes_.empty()) {
if (measure_node_->text_style_->font_size <=
measure_node_->auto_font_size_preset_sizes_.front()) {
return;
} else {
auto sizes = measure_node_->auto_font_size_preset_sizes_.size();
for (size_t i = 0; i < sizes; i++) {
if (measure_node_->text_style_->font_size <
measure_node_->auto_font_size_preset_sizes_[sizes - i - 1])
continue;
measure_node_->text_style_->font_size =
measure_node_->auto_font_size_preset_sizes_[sizes - i - 1];
update_flag_ = TextUpdateFlag::kUpdateFlagStyle;
BuildTextLayout(constraint, context_measure);
if (CheckTextFullyDisplayed(constraint, context_measure)) {
return;
}
}
}
return;
}
if (measure_node_->text_style_->font_size <=
measure_node_->auto_font_size_min_size_) {
measure_node_->text_style_->font_size =
measure_node_->auto_font_size_min_size_;
} else if (measure_node_->text_style_->font_size >=
measure_node_->auto_font_size_max_size_ &&
measure_node_->auto_font_size_max_size_ >
measure_node_->auto_font_size_min_size_) {
measure_node_->text_style_->font_size =
measure_node_->auto_font_size_max_size_;
}
while (measure_node_->text_style_->font_size >=
measure_node_->auto_font_size_min_size_) {
auto current_font_size = measure_node_->text_style_->font_size.value_or(
kDefaultFontSizeInDip * measure_node_->Logical2ClayPixelRatio());
auto target_font_size = std::max(
current_font_size - measure_node_->auto_font_size_step_granularity_,
measure_node_->auto_font_size_min_size_);
if (target_font_size == current_font_size) {
return;
}
measure_node_->text_style_->font_size = target_font_size;
FlexInlineFontSize(true, measure_node_->text_style_->font_size.value(),
measure_node_);
update_flag_ = TextUpdateFlag::kUpdateFlagStyle;
BuildTextLayout(constraint, context_measure);
if (CheckTextFullyDisplayed(constraint, context_measure)) {
return;
}
if (target_font_size == measure_node_->auto_font_size_min_size_) {
return;
}
}
}
void TextRender::TryExpandFontSize(
const MeasureConstraint& constraint,
ShadowLayoutContextMeasure* context_measure) {
// FIXME: if the number of auto_font_size_preset_sizes_ is too much, we
// should use dichotomy to find target
if (!measure_node_->auto_font_size_preset_sizes_.empty()) {
if (measure_node_->text_style_->font_size >=
measure_node_->auto_font_size_preset_sizes_.back()) {
return;
} else {
for (auto auto_font_size_preset_size :
measure_node_->auto_font_size_preset_sizes_) {
if (measure_node_->text_style_->font_size >=
auto_font_size_preset_size) {
continue;
}
auto pre_paragraph = std::move(cache_paragraph_);
auto original_font_size = measure_node_->text_style_->font_size;
measure_node_->text_style_->font_size = auto_font_size_preset_size;
update_flag_ = TextUpdateFlag::kUpdateFlagStyle;
BuildTextLayout(constraint, context_measure);
if (!CheckTextFullyDisplayed(constraint, context_measure)) {
measure_node_->text_style_->font_size = original_font_size;
cache_paragraph_ = std::move(pre_paragraph);
return;
}
}
}
return;
}
if (measure_node_->text_style_->font_size <=
measure_node_->auto_font_size_min_size_) {
measure_node_->text_style_->font_size =
measure_node_->auto_font_size_min_size_;
} else if (measure_node_->text_style_->font_size >=
measure_node_->auto_font_size_max_size_ &&
measure_node_->auto_font_size_max_size_ >
measure_node_->auto_font_size_min_size_) {
measure_node_->text_style_->font_size =
measure_node_->auto_font_size_max_size_;
}
while (measure_node_->text_style_->font_size <=
measure_node_->auto_font_size_max_size_) {
std::unique_ptr<txt::Paragraph> pre_paragraph = std::move(cache_paragraph_);
measure_node_->text_style_->font_size =
measure_node_->text_style_->font_size.value_or(
kDefaultFontSizeInDip * measure_node_->Logical2ClayPixelRatio()) +
measure_node_->auto_font_size_step_granularity_;
FlexInlineFontSize(false, measure_node_->text_style_->font_size.value(),
measure_node_);
if (measure_node_->text_style_->font_size <=
measure_node_->auto_font_size_max_size_) {
update_flag_ = TextUpdateFlag::kUpdateFlagStyle;
BuildTextLayout(constraint, context_measure);
if (!CheckTextFullyDisplayed(constraint, context_measure)) {
measure_node_->text_style_->font_size =
measure_node_->text_style_->font_size.value_or(
kDefaultFontSizeInDip *
measure_node_->Logical2ClayPixelRatio()) -
measure_node_->auto_font_size_step_granularity_;
cache_paragraph_ = std::move(pre_paragraph);
return;
}
} else {
cache_paragraph_ = std::move(pre_paragraph);
}
}
}
void TextRender::FlexInlineFontSize(bool shrink_or_expand, float font_size,
ShadowNode* shadow_node) {
for (auto* child : shadow_node->GetChildren()) {
if (child->IsInlineTextShadowNode()) {
auto inline_text_child = static_cast<InlineTextShadowNode*>(child);
if ((shrink_or_expand &&
inline_text_child->text_style_->font_size.value() > font_size) ||
(!shrink_or_expand &&
inline_text_child->text_style_->font_size.value() < font_size)) {
static_cast<InlineTextShadowNode*>(child)->text_style_->font_size =
font_size;
}
FlexInlineFontSize(shrink_or_expand, font_size, child);
} else if (child->IsInlineViewShadowNode()) {
FlexInlineFontSize(shrink_or_expand, font_size, child);
}
}
}
void TextRender::HandleInlineTruncation(const MeasureConstraint& constraint,
ShadowLayoutContextMeasure* context) {
for (auto child : measure_node_->GetChildren()) {
if (child->IsInlineTruncationShadowNode()) {
auto truncation_node = static_cast<InlineTruncationShadowNode*>(child);
if (cache_paragraph_ &&
(cache_paragraph_->DidExceedMaxLines() ||
(constraint.height_mode != MeasureMode::kIndefinite &&
cache_paragraph_->GetHeight() > constraint.height &&
cache_paragraph_->GetLineMetrics().size() > 1))) {
FloatSize truncation_size = truncation_node->CalculateTruncatedSize();
if (truncation_size.width() > constraint.width) {
truncation_node->SetNeedMount(false);
return;
}
truncation_node->SetNeedLayout(true);
truncation_node->SetNeedMount(true);
auto line_metrics = cache_paragraph_->GetLineMetrics();
if (line_metrics.empty()) {
truncation_node->SetNeedMount(false);
return;
}
const bool has_hard_break = std::any_of(
line_metrics.begin(), line_metrics.end(),
[](const auto& line_metric) { return line_metric.hard_break; });
const double truncation_layout_width =
has_hard_break ? prev_layout_width_
: cache_paragraph_->GetMaxIntrinsicWidth();
#ifndef CLAY_ENABLE_TTTEXT
auto end_dx = truncation_direction_ == TextDirection::kRtl
? truncation_size.width()
: truncation_layout_width - truncation_size.width();
#else
auto end_dx = truncation_layout_width - truncation_size.width();
#endif
auto last_line_height = line_metrics.back().height;
auto end_dy = constraint.height_mode == MeasureMode::kIndefinite
? cache_paragraph_->GetHeight()
: std::min<double>(cache_paragraph_->GetHeight(),
constraint.height.value_or(0)) -
last_line_height + kLayoutTolerance;
auto end_glyph_index =
cache_paragraph_->GetGlyphPositionAtCoordinate(end_dx, end_dy);
auto end_glyph_boxes =
end_glyph_index.position > 0
? cache_paragraph_->GetRectsForRange(
end_glyph_index.position - 1, end_glyph_index.position,
txt::Paragraph::RectHeightStyle::kTight,
txt::Paragraph::RectWidthStyle::kTight)
: std::vector<txt::Paragraph::TextBox>{};
size_t display_glyph_num = end_glyph_index.position;
if (!end_glyph_boxes.empty()) {
auto glyph_box = end_glyph_boxes.front();
bool glyph_overlaps_truncation =
glyph_box.rect.Right() >
prev_layout_width_ - truncation_size.width();
#ifndef CLAY_ENABLE_TTTEXT
if (truncation_direction_ == TextDirection::kRtl) {
glyph_overlaps_truncation =
glyph_box.rect.Left() < truncation_size.width();
}
#endif
if (glyph_overlaps_truncation) {
display_glyph_num =
end_glyph_index.position > 0 ? end_glyph_index.position - 1 : 0;
}
}
const size_t max_visible_glyph_num = display_glyph_num;
const size_t original_end_glyph_position = end_glyph_position_;
const std::optional<TextOverflow> overflow =
measure_node_->text_style_->overflow;
size_t target_visible_line_index = 0;
if (max_visible_glyph_num > 0) {
const size_t target_glyph_index = max_visible_glyph_num - 1;
for (size_t i = 0; i < line_metrics.size(); ++i) {
if (target_glyph_index < line_metrics[i].end_index ||
i == line_metrics.size() - 1) {
target_visible_line_index = i;
break;
}
}
}
auto rebuild_with_visible_glyph_num = [&](size_t visible_glyph_num) {
measure_node_->ResetEndIndex();
truncation_node->SetNeedLayout(true);
size_t remaining_glyph_num = visible_glyph_num;
ProcessTruncationContent(remaining_glyph_num, measure_node_);
inline_truncation_hidden_count_ = static_cast<int>(
original_end_glyph_position > visible_glyph_num
? original_end_glyph_position - visible_glyph_num
: 0);
measure_node_->text_style_->overflow = TextOverflow::kClip;
update_flag_ = TextUpdateFlag::kUpdateFlagStyle;
BuildTextLayout(constraint, context);
measure_node_->text_style_->overflow = overflow;
for (auto truncation_child : child->GetChildren()) {
if (truncation_child->IsInlineTextShadowNode()) {
static_cast<InlineTextShadowNode*>(truncation_child)
->LayoutRange(cache_paragraph_.get());
}
}
double visible_bottom = cache_paragraph_->GetHeight();
if (constraint.height_mode != MeasureMode::kIndefinite) {
visible_bottom =
std::min<double>(visible_bottom, constraint.height.value_or(0));
}
const auto& rebuilt_line_metrics = cache_paragraph_->GetLineMetrics();
if (rebuilt_line_metrics.empty()) {
return false;
}
const size_t visible_line_index = std::min(
target_visible_line_index, rebuilt_line_metrics.size() - 1);
const auto& visible_line = rebuilt_line_metrics[visible_line_index];
const double target_line_top =
visible_line.baseline - visible_line.ascent;
const double target_line_bottom =
visible_line.baseline + visible_line.descent;
std::optional<double> first_box_top;
return InlineTruncationTextBoxesFit(
cache_paragraph_.get(), truncation_node, prev_layout_width_,
visible_bottom, target_line_top, target_line_bottom,
first_box_top);
};
size_t visible_glyph_num = max_visible_glyph_num;
// Line breaking can change non-monotonically as text is removed, so
// back off from the original cut point and keep the closest fit.
bool found_fit = false;
while (true) {
found_fit = rebuild_with_visible_glyph_num(visible_glyph_num);
if (found_fit || visible_glyph_num == 0) {
break;
}
--visible_glyph_num;
}
truncation_node->UpdateTruncatedSize(truncation_size.width(),
truncation_size.height());
truncation_node->SetNeedLayout(false);
measure_node_->SetNeedSecondLayout(false);
return;
} else {
truncation_node->SetNeedMount(false);
}
return;
}
}
}
void TextRender::ProcessTruncationContent(size_t& display_glyph_num,
ShadowNode* node) {
for (auto* child : node->GetChildren()) {
if (child->IsRawTextShadowNode()) {
auto* raw_text_node = static_cast<RawTextShadowNode*>(child);
#if defined(CLAY_ENABLE_TTTEXT)
auto layout_text_length = raw_text_node->GetLayoutTextLength();
auto raw_end_index =
raw_text_node->GetRawEndIndexForLayoutTextLength(display_glyph_num);
#else
auto layout_text_length = raw_text_node->GetLayoutTextUtf16Length();
auto raw_end_index =
raw_text_node->GetRawEndIndexForLayoutTextUtf16Length(
display_glyph_num);
#endif
if (layout_text_length < display_glyph_num) {
display_glyph_num = display_glyph_num - layout_text_length;
} else {
raw_text_node->SetEndIndex(raw_end_index);
measure_node_->MarkNeedsUpdate(TextUpdateFlag::kUpdateFlagChildren);
display_glyph_num = 0;
}
} else if (child->IsInlineTextShadowNode()) {
ProcessTruncationContent(display_glyph_num, child);
} else if (child->IsInlineImageShadowNode() ||
child->IsInlineViewShadowNode()) {
if (display_glyph_num > 0) {
display_glyph_num--;
} else {
child->SetEndIndex(0);
}
}
}
}
} // namespace clay