@@ -42,10 +42,68 @@ SkFontStyle MakeSkFontStyle(txt::FontWeight font_weight,
4242 : SkFontStyle::Slant::kItalic_Slant );
4343}
4444
45- skt::ParagraphStyle TxtToSkia (const ParagraphStyle& txt) {
45+ } // anonymous namespace
46+
47+ ParagraphBuilderSkia::ParagraphBuilderSkia (
48+ const ParagraphStyle& style,
49+ std::shared_ptr<FontCollection> font_collection)
50+ : base_style_(style.GetTextStyle()) {
51+ builder_ = skt::ParagraphBuilder::make (
52+ TxtToSkia (style), font_collection->CreateSktFontCollection ());
53+ }
54+
55+ ParagraphBuilderSkia::~ParagraphBuilderSkia () = default ;
56+
57+ void ParagraphBuilderSkia::PushStyle (const TextStyle& style) {
58+ builder_->pushStyle (TxtToSkia (style));
59+ txt_style_stack_.push (style);
60+ }
61+
62+ void ParagraphBuilderSkia::Pop () {
63+ builder_->pop ();
64+ txt_style_stack_.pop ();
65+ }
66+
67+ const TextStyle& ParagraphBuilderSkia::PeekStyle () {
68+ return txt_style_stack_.empty () ? base_style_ : txt_style_stack_.top ();
69+ }
70+
71+ void ParagraphBuilderSkia::AddText (const std::u16string& text) {
72+ builder_->addText (text);
73+ }
74+
75+ void ParagraphBuilderSkia::AddPlaceholder (PlaceholderRun& span) {
76+ skt::PlaceholderStyle placeholder_style;
77+ placeholder_style.fHeight = span.height ;
78+ placeholder_style.fWidth = span.width ;
79+ placeholder_style.fBaseline = static_cast <skt::TextBaseline>(span.baseline );
80+ placeholder_style.fBaselineOffset = span.baseline_offset ;
81+ placeholder_style.fAlignment =
82+ static_cast <skt::PlaceholderAlignment>(span.alignment );
83+
84+ builder_->addPlaceholder (placeholder_style);
85+ }
86+
87+ std::unique_ptr<Paragraph> ParagraphBuilderSkia::Build () {
88+ return std::make_unique<ParagraphSkia>(builder_->Build (),
89+ std::move (dl_paints_));
90+ }
91+
92+ skt::ParagraphPainter::PaintID ParagraphBuilderSkia::CreatePaintID (
93+ const flutter::DlPaint& dl_paint) {
94+ dl_paints_.push_back (dl_paint);
95+ return dl_paints_.size () - 1 ;
96+ }
97+
98+ skt::ParagraphStyle ParagraphBuilderSkia::TxtToSkia (const ParagraphStyle& txt) {
4699 skt::ParagraphStyle skia;
47100 skt::TextStyle text_style;
48101
102+ // Convert the default color of an SkParagraph text style into a DlPaint.
103+ flutter::DlPaint dl_paint;
104+ dl_paint.setColor (text_style.getColor ());
105+ text_style.setForegroundPaintID (CreatePaintID (dl_paint));
106+
49107 text_style.setFontStyle (MakeSkFontStyle (txt.font_weight , txt.font_style ));
50108 text_style.setFontSize (SkDoubleToScalar (txt.font_size ));
51109 text_style.setHeight (SkDoubleToScalar (txt.height ));
@@ -84,7 +142,7 @@ skt::ParagraphStyle TxtToSkia(const ParagraphStyle& txt) {
84142 return skia;
85143}
86144
87- skt::TextStyle TxtToSkia (const TextStyle& txt) {
145+ skt::TextStyle ParagraphBuilderSkia:: TxtToSkia (const TextStyle& txt) {
88146 skt::TextStyle skia;
89147
90148 skia.setColor (txt.color );
@@ -112,10 +170,14 @@ skt::TextStyle TxtToSkia(const TextStyle& txt) {
112170
113171 skia.setLocale (SkString (txt.locale .c_str ()));
114172 if (txt.has_background ) {
115- skia.setBackgroundColor ( txt.background );
173+ skia.setBackgroundPaintID ( CreatePaintID ( txt.background_dl ) );
116174 }
117175 if (txt.has_foreground ) {
118- skia.setForegroundColor (txt.foreground );
176+ skia.setForegroundPaintID (CreatePaintID (txt.foreground_dl ));
177+ } else {
178+ flutter::DlPaint dl_paint;
179+ dl_paint.setColor (txt.color );
180+ skia.setForegroundPaintID (CreatePaintID (dl_paint));
119181 }
120182
121183 skia.resetFontFeatures ();
@@ -153,50 +215,4 @@ skt::TextStyle TxtToSkia(const TextStyle& txt) {
153215 return skia;
154216}
155217
156- } // anonymous namespace
157-
158- ParagraphBuilderSkia::ParagraphBuilderSkia (
159- const ParagraphStyle& style,
160- std::shared_ptr<FontCollection> font_collection)
161- : builder_(skt::ParagraphBuilder::make(
162- TxtToSkia (style),
163- font_collection->CreateSktFontCollection())),
164- base_style_(style.GetTextStyle()) {}
165-
166- ParagraphBuilderSkia::~ParagraphBuilderSkia () = default ;
167-
168- void ParagraphBuilderSkia::PushStyle (const TextStyle& style) {
169- builder_->pushStyle (TxtToSkia (style));
170- txt_style_stack_.push (style);
171- }
172-
173- void ParagraphBuilderSkia::Pop () {
174- builder_->pop ();
175- txt_style_stack_.pop ();
176- }
177-
178- const TextStyle& ParagraphBuilderSkia::PeekStyle () {
179- return txt_style_stack_.empty () ? base_style_ : txt_style_stack_.top ();
180- }
181-
182- void ParagraphBuilderSkia::AddText (const std::u16string& text) {
183- builder_->addText (text);
184- }
185-
186- void ParagraphBuilderSkia::AddPlaceholder (PlaceholderRun& span) {
187- skt::PlaceholderStyle placeholder_style;
188- placeholder_style.fHeight = span.height ;
189- placeholder_style.fWidth = span.width ;
190- placeholder_style.fBaseline = static_cast <skt::TextBaseline>(span.baseline );
191- placeholder_style.fBaselineOffset = span.baseline_offset ;
192- placeholder_style.fAlignment =
193- static_cast <skt::PlaceholderAlignment>(span.alignment );
194-
195- builder_->addPlaceholder (placeholder_style);
196- }
197-
198- std::unique_ptr<Paragraph> ParagraphBuilderSkia::Build () {
199- return std::make_unique<ParagraphSkia>(builder_->Build ());
200- }
201-
202218} // namespace txt
0 commit comments