1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "core/components_ng/pattern/text_field/text_field_content_modifier.h"
17
18 #include "base/utils/utils.h"
19 #include "core/components_ng/pattern/text_field/auto_fill_controller.h"
20 #include "core/components_ng/pattern/text_field/text_field_pattern.h"
21 #include "core/pipeline_ng/pipeline_context.h"
22
23 namespace OHOS::Ace::NG {
24 namespace {
25 const FontWeight FONT_WEIGHT_CONVERT_MAP[] = {
26 FontWeight::W100,
27 FontWeight::W200,
28 FontWeight::W300,
29 FontWeight::W400,
30 FontWeight::W500,
31 FontWeight::W600,
32 FontWeight::W700,
33 FontWeight::W800,
34 FontWeight::W900,
35 FontWeight::W700,
36 FontWeight::W400,
37 FontWeight::W900,
38 FontWeight::W100,
39 FontWeight::W500,
40 FontWeight::W400,
41 };
42 constexpr float ROUND_VALUE = 0.5f;
43 constexpr Dimension DEFAULT_FADEOUT_VP = 16.0_vp;
44 constexpr double MAX_TEXTFADEOUT_PERCENT = 0.5;
45 constexpr double MIN_TEXTFADEOUT_DELTA = 1.0;
46
ConvertFontWeight(FontWeight fontWeight)47 inline FontWeight ConvertFontWeight(FontWeight fontWeight)
48 {
49 return FONT_WEIGHT_CONVERT_MAP[static_cast<int>(fontWeight)];
50 }
51 } // namespace
52
TextFieldContentModifier(const WeakPtr<OHOS::Ace::NG::Pattern> & pattern)53 TextFieldContentModifier::TextFieldContentModifier(const WeakPtr<OHOS::Ace::NG::Pattern>& pattern) : pattern_(pattern)
54 {
55 SetDefaultAnimatablePropertyValue();
56 SetDefaultPropertyValue();
57 }
58
onDraw(DrawingContext & context)59 void TextFieldContentModifier::onDraw(DrawingContext& context)
60 {
61 auto textFieldPattern = DynamicCast<TextFieldPattern>(pattern_.Upgrade());
62 CHECK_NULL_VOID(textFieldPattern);
63 auto paragraph = textFieldPattern->GetParagraph();
64 CHECK_NULL_VOID(paragraph);
65 auto autofillController = textFieldPattern->GetOrCreateAutoFillController();
66 CHECK_NULL_VOID(autofillController);
67 auto autoFillAnimationStatus = autofillController->GetAutoFillAnimationStatus();
68 if (autoFillAnimationStatus != AutoFillAnimationStatus::INIT) {
69 DoAutoFillDraw(context);
70 return;
71 }
72 if (textFieldPattern->IsInlineMode() || TextOverflow::ELLIPSIS == paragraph->GetParagraphStyle().textOverflow ||
73 !textFadeoutEnabled_) {
74 DoNormalDraw(context);
75 } else {
76 DoTextFadeoutDraw(context);
77 }
78 }
79
GetFrameRectClip(RSRect & clipRect,std::vector<RSPoint> & clipRadius)80 void TextFieldContentModifier::GetFrameRectClip(RSRect& clipRect, std::vector<RSPoint>& clipRadius)
81 {
82 auto textFieldPattern = DynamicCast<TextFieldPattern>(pattern_.Upgrade());
83 CHECK_NULL_VOID(textFieldPattern);
84 auto host = textFieldPattern->GetHost();
85 CHECK_NULL_VOID(host);
86 auto renderContext = host->GetRenderContext();
87 CHECK_NULL_VOID(renderContext);
88 auto textFrameRect = textFieldPattern->GetFrameRect();
89 clipRect = RSRect(0.0f, 0.0f, textFrameRect.Width(), textFrameRect.Height());
90 auto radius = renderContext->GetBorderRadius().value_or(BorderRadiusProperty());
91 auto radiusTopLeft = RSPoint(static_cast<float>(radius.radiusTopLeft.value_or(0.0_vp).ConvertToPx()),
92 static_cast<float>(radius.radiusTopLeft.value_or(0.0_vp).ConvertToPx()));
93 clipRadius.emplace_back(radiusTopLeft);
94 auto radiusTopRight = RSPoint(static_cast<float>(radius.radiusTopRight.value_or(0.0_vp).ConvertToPx()),
95 static_cast<float>(radius.radiusTopRight.value_or(0.0_vp).ConvertToPx()));
96 clipRadius.emplace_back(radiusTopRight);
97 auto radiusBottomRight = RSPoint(static_cast<float>(radius.radiusBottomRight.value_or(0.0_vp).ConvertToPx()),
98 static_cast<float>(radius.radiusBottomRight.value_or(0.0_vp).ConvertToPx()));
99 clipRadius.emplace_back(radiusBottomRight);
100 auto radiusBottomLeft = RSPoint(static_cast<float>(radius.radiusBottomLeft.value_or(0.0_vp).ConvertToPx()),
101 static_cast<float>(radius.radiusBottomLeft.value_or(0.0_vp).ConvertToPx()));
102 clipRadius.emplace_back(radiusBottomLeft);
103 }
104
SetDefaultAnimatablePropertyValue()105 void TextFieldContentModifier::SetDefaultAnimatablePropertyValue()
106 {
107 RefPtr<TextTheme> theme;
108 RefPtr<TextFieldLayoutProperty> textFieldLayoutProperty;
109 RefPtr<PipelineContext> pipelineContext;
110 auto textPartten = pattern_.Upgrade();
111 CHECK_NULL_VOID(textPartten);
112 auto frameNode = textPartten->GetHost();
113 CHECK_NULL_VOID(frameNode);
114 pipelineContext = frameNode->GetContext();
115 CHECK_NULL_VOID(pipelineContext);
116 theme = pipelineContext->GetTheme<TextTheme>();
117 textFieldLayoutProperty = frameNode->GetLayoutProperty<TextFieldLayoutProperty>();
118 auto textFieldPattern = DynamicCast<TextFieldPattern>(pattern_.Upgrade());
119 CHECK_NULL_VOID(textFieldPattern);
120 TextStyle textStyle;
121 if (!textFieldPattern->GetTextUtf16Value().empty()) {
122 textStyle = CreateTextStyleUsingTheme(
123 textFieldLayoutProperty->GetFontStyle(), textFieldLayoutProperty->GetTextLineStyle(), theme);
124 } else {
125 textStyle = CreateTextStyleUsingTheme(textFieldLayoutProperty->GetPlaceholderFontStyle(),
126 textFieldLayoutProperty->GetPlaceholderTextLineStyle(), theme);
127 }
128 SetDefaultFontSize(textStyle);
129 SetDefaultAdaptMinFontSize(textStyle);
130 SetDefaultAdaptMaxFontSize(textStyle);
131 SetDefaultFontWeight(textStyle);
132 SetDefaultTextColor(textStyle);
133 SetDefaultFontStyle(textStyle);
134 SetDefaultTextOverflow(textStyle);
135 SetDefaultTextDecoration(textStyle);
136 SetDefaultAutoFillTranslationOffset();
137 SetDefaultAutoFillTextScrollOffset();
138 SetDefaultAutoFillCharIndex();
139 }
140
SetDefaultPropertyValue()141 void TextFieldContentModifier::SetDefaultPropertyValue()
142 {
143 RefPtr<TextFieldTheme> theme;
144 RefPtr<PipelineContext> pipelineContext;
145 auto textPattern = pattern_.Upgrade();
146 CHECK_NULL_VOID(textPattern);
147 auto frameNode = textPattern->GetHost();
148 CHECK_NULL_VOID(frameNode);
149 pipelineContext = frameNode->GetContext();
150 CHECK_NULL_VOID(pipelineContext);
151 theme = pipelineContext->GetTheme<TextFieldTheme>();
152 if (theme) {
153 autoFillEmphasizeCharTextColor_ = theme->GetAutoFillIconEmphasizeColor();
154 autoFillDefaultCharInitTextColor_ = theme->GetTextColor();
155 }
156 auto textFieldPattern = DynamicCast<TextFieldPattern>(pattern_.Upgrade());
157
158 textObscured_ = AceType::MakeRefPtr<PropertyBool>(textFieldPattern->GetTextObscured());
159 dragStatus_ = AceType::MakeRefPtr<PropertyBool>(false);
160 contentOffset_ = AceType::MakeRefPtr<PropertyOffsetF>(
161 OffsetF(textFieldPattern->GetTextRect().GetX(), textFieldPattern->GetTextRect().GetY()));
162 contentSize_ = AceType::MakeRefPtr<PropertySizeF>(SizeF());
163 textValue_ = AceType::MakeRefPtr<PropertyU16String>(u"");
164 errorTextValue_ = AceType::MakeRefPtr<PropertyU16String>(u"");
165 placeholderValue_ = AceType::MakeRefPtr<PropertyU16String>(u"");
166 textRectY_ = AceType::MakeRefPtr<PropertyFloat>(textFieldPattern->GetTextRect().GetY());
167 textRectX_ = AceType::MakeRefPtr<PropertyFloat>(textFieldPattern->GetTextRect().GetX());
168 textAlign_ = AceType::MakeRefPtr<PropertyInt>(static_cast<int32_t>(TextAlign::START));
169 showErrorState_ = AceType::MakeRefPtr<PropertyBool>(false);
170 fontFamilyString_ = AceType::MakeRefPtr<PropertyString>("");
171 fontReady_ = AceType::MakeRefPtr<PropertyBool>(false);
172 contentChange_ = AceType::MakeRefPtr<PropertyBool>(false);
173 AttachProperty(contentOffset_);
174 AttachProperty(contentSize_);
175 AttachProperty(textValue_);
176 AttachProperty(errorTextValue_);
177 AttachProperty(placeholderValue_);
178 AttachProperty(textRectY_);
179 AttachProperty(textObscured_);
180 AttachProperty(dragStatus_);
181 AttachProperty(textRectX_);
182 AttachProperty(textAlign_);
183 AttachProperty(showErrorState_);
184 AttachProperty(showUnderline_);
185 AttachProperty(fontFamilyString_);
186 AttachProperty(fontReady_);
187 AttachProperty(contentChange_);
188 }
189
SetDefaultFontSize(const TextStyle & textStyle)190 void TextFieldContentModifier::SetDefaultFontSize(const TextStyle& textStyle)
191 {
192 float fontSizeValue;
193 auto pipelineContext = PipelineContext::GetCurrentContextSafelyWithCheck();
194 if (pipelineContext) {
195 fontSizeValue = textStyle.GetFontSize().ConvertToPxDistribute(
196 textStyle.GetMinFontScale(), textStyle.GetMaxFontScale(), textStyle.IsAllowScale());
197 } else {
198 fontSizeValue = textStyle.GetFontSize().ConvertToPx();
199 }
200
201 fontSizeFloat_ = AceType::MakeRefPtr<AnimatablePropertyFloat>(fontSizeValue);
202 AttachProperty(fontSizeFloat_);
203 }
204
SetDefaultAdaptMinFontSize(const TextStyle & textStyle)205 void TextFieldContentModifier::SetDefaultAdaptMinFontSize(const TextStyle& textStyle)
206 {
207 float minFontSizeValue = textStyle.GetFontSize().Value();
208 auto pipelineContext = PipelineContext::GetCurrentContextSafelyWithCheck();
209 if (pipelineContext) {
210 minFontSizeValue = textStyle.GetAdaptMinFontSize().ConvertToPxDistribute(
211 textStyle.GetMinFontScale(), textStyle.GetMaxFontScale(), textStyle.IsAllowScale());
212 }
213 adaptMinFontSizeFloat_ = AceType::MakeRefPtr<AnimatablePropertyFloat>(minFontSizeValue);
214 AttachProperty(adaptMinFontSizeFloat_);
215 }
216
SetDefaultAdaptMaxFontSize(const TextStyle & textStyle)217 void TextFieldContentModifier::SetDefaultAdaptMaxFontSize(const TextStyle& textStyle)
218 {
219 float maxFontSizeValue = textStyle.GetFontSize().Value();
220 auto pipelineContext = PipelineContext::GetCurrentContextSafelyWithCheck();
221 if (pipelineContext) {
222 maxFontSizeValue = textStyle.GetAdaptMaxFontSize().ConvertToPxDistribute(
223 textStyle.GetMinFontScale(), textStyle.GetMaxFontScale(), textStyle.IsAllowScale());
224 }
225 adaptMaxFontSizeFloat_ = AceType::MakeRefPtr<AnimatablePropertyFloat>(maxFontSizeValue);
226 AttachProperty(adaptMaxFontSizeFloat_);
227 }
228
SetDefaultFontWeight(const TextStyle & textStyle)229 void TextFieldContentModifier::SetDefaultFontWeight(const TextStyle& textStyle)
230 {
231 fontWeightFloat_ =
232 AceType::MakeRefPtr<AnimatablePropertyFloat>(static_cast<float>(ConvertFontWeight(textStyle.GetFontWeight())));
233 AttachProperty(fontWeightFloat_);
234 }
235
SetDefaultTextColor(const TextStyle & textStyle)236 void TextFieldContentModifier::SetDefaultTextColor(const TextStyle& textStyle)
237 {
238 animatableTextColor_ = AceType::MakeRefPtr<AnimatablePropertyColor>(LinearColor(textStyle.GetTextColor()));
239 AttachProperty(animatableTextColor_);
240 }
241
SetDefaultFontStyle(const TextStyle & textStyle)242 void TextFieldContentModifier::SetDefaultFontStyle(const TextStyle& textStyle)
243 {
244 fontStyle_ = AceType::MakeRefPtr<PropertyInt>(static_cast<int32_t>(textStyle.GetFontStyle()));
245 AttachProperty(fontStyle_);
246 }
247
SetDefaultTextOverflow(const TextStyle & textStyle)248 void TextFieldContentModifier::SetDefaultTextOverflow(const TextStyle& textStyle)
249 {
250 textOverflow_ = AceType::MakeRefPtr<PropertyInt>(static_cast<int32_t>(textStyle.GetTextOverflow()));
251 AttachProperty(textOverflow_);
252 }
253
SetDefaultTextDecoration(const TextStyle & textStyle)254 void TextFieldContentModifier::SetDefaultTextDecoration(const TextStyle& textStyle)
255 {
256 textDecoration_ = textStyle.GetTextDecorationFirst();
257 textDecorationStyle_ = textStyle.GetTextDecorationStyle();
258 textDecorationColor_ = textStyle.GetTextDecorationColor();
259 textDecorationColorAlpha_ = MakeRefPtr<AnimatablePropertyFloat>(
260 textDecoration_ == TextDecoration::NONE ? 0.0f : textDecorationColor_->GetAlpha());
261 AttachProperty(textDecorationColorAlpha_);
262 }
263
ModifyTextStyle(TextStyle & textStyle)264 void TextFieldContentModifier::ModifyTextStyle(TextStyle& textStyle)
265 {
266 if (fontSize_.has_value() && fontSizeFloat_) {
267 textStyle.SetFontSize(Dimension(fontSizeFloat_->Get(), DimensionUnit::PX));
268 }
269 if (textStyle.GetAdaptTextSize()) {
270 if (adaptMinFontSize_.has_value() && adaptMinFontSizeFloat_) {
271 textStyle.SetAdaptMinFontSize(Dimension(adaptMinFontSizeFloat_->Get(), DimensionUnit::PX));
272 }
273 if (adaptMaxFontSize_.has_value() && adaptMaxFontSizeFloat_) {
274 textStyle.SetAdaptMaxFontSize(Dimension(adaptMaxFontSizeFloat_->Get(), DimensionUnit::PX));
275 }
276 }
277 if (fontWeight_.has_value() && fontWeightFloat_) {
278 textStyle.SetFontWeight(static_cast<FontWeight>(std::floor(fontWeightFloat_->Get() + 0.5f)));
279 }
280 if (textColor_.has_value() && animatableTextColor_) {
281 textStyle.SetTextColor(Color(animatableTextColor_->Get().GetValue()));
282 }
283 ModifyDecorationInTextStyle(textStyle);
284 }
285
SetFontFamilies(const std::vector<std::string> & value)286 void TextFieldContentModifier::SetFontFamilies(const std::vector<std::string>& value)
287 {
288 CHECK_NULL_VOID(fontFamilyString_);
289 fontFamilyString_->Set(V2::ConvertFontFamily(value));
290 }
291
SetFontSize(const Dimension & value,const TextStyle & textStyle)292 void TextFieldContentModifier::SetFontSize(const Dimension& value, const TextStyle& textStyle)
293 {
294 auto valPx = value.ConvertToPxDistribute(textStyle.GetMinFontScale(),
295 textStyle.GetMaxFontScale(), textStyle.IsAllowScale());
296 fontSize_ = Dimension(valPx);
297 CHECK_NULL_VOID(fontSizeFloat_);
298 fontSizeFloat_->Set(valPx);
299 }
300
SetAdaptMinFontSize(const Dimension & value,const TextStyle & textStyle)301 void TextFieldContentModifier::SetAdaptMinFontSize(const Dimension& value, const TextStyle& textStyle)
302 {
303 auto valPx = value.ConvertToPxDistribute(textStyle.GetMinFontScale(),
304 textStyle.GetMaxFontScale(), textStyle.IsAllowScale());
305 adaptMinFontSize_ = Dimension(valPx);
306 CHECK_NULL_VOID(adaptMinFontSizeFloat_);
307 adaptMinFontSizeFloat_->Set(valPx);
308 }
309
SetAdaptMaxFontSize(const Dimension & value,const TextStyle & textStyle)310 void TextFieldContentModifier::SetAdaptMaxFontSize(const Dimension& value, const TextStyle& textStyle)
311 {
312 auto valPx = value.ConvertToPxDistribute(textStyle.GetMinFontScale(),
313 textStyle.GetMaxFontScale(), textStyle.IsAllowScale());
314 adaptMaxFontSize_ = Dimension(valPx);
315 CHECK_NULL_VOID(adaptMaxFontSizeFloat_);
316 adaptMaxFontSizeFloat_->Set(valPx);
317 }
318
SetFontWeight(const FontWeight & value)319 void TextFieldContentModifier::SetFontWeight(const FontWeight& value)
320 {
321 fontWeight_ = ConvertFontWeight(value);
322 CHECK_NULL_VOID(fontWeightFloat_);
323 fontWeightFloat_->Set(static_cast<int>(ConvertFontWeight(value)));
324 }
325
SetTextColor(const Color & value)326 void TextFieldContentModifier::SetTextColor(const Color& value)
327 {
328 textColor_ = value;
329 CHECK_NULL_VOID(animatableTextColor_);
330 animatableTextColor_->Set(LinearColor(value));
331 }
332
SetTextOverflow(const TextOverflow value)333 void TextFieldContentModifier::SetTextOverflow(const TextOverflow value)
334 {
335 if (textOverflow_->Get() != static_cast<int32_t>(value)) {
336 textOverflow_->Set(static_cast<int32_t>(value));
337 }
338 }
339
SetFontStyle(const OHOS::Ace::FontStyle & value)340 void TextFieldContentModifier::SetFontStyle(const OHOS::Ace::FontStyle& value)
341 {
342 if (fontStyle_->Get() != static_cast<int32_t>(value)) {
343 fontStyle_->Set(static_cast<int32_t>(value));
344 }
345 }
346
SetContentOffset(OffsetF & value)347 void TextFieldContentModifier::SetContentOffset(OffsetF& value)
348 {
349 if (contentOffset_) {
350 contentOffset_->Set(value);
351 }
352 }
353
GetContentOffsetY()354 float TextFieldContentModifier::GetContentOffsetY()
355 {
356 return contentOffset_->Get().GetY();
357 }
358
SetContentSize(SizeF & value)359 void TextFieldContentModifier::SetContentSize(SizeF& value)
360 {
361 if (contentSize_) {
362 contentSize_->Set(value);
363 }
364 }
365
SetTextValue(std::u16string & value)366 void TextFieldContentModifier::SetTextValue(std::u16string& value)
367 {
368 if (textValue_->Get() != value) {
369 textValue_->Set(value);
370 }
371 }
372
SetErrorTextValue(const std::u16string & value)373 void TextFieldContentModifier::SetErrorTextValue(const std::u16string& value)
374 {
375 if (errorTextValue_->Get() != value) {
376 errorTextValue_->Set(value);
377 }
378 }
379
SetPlaceholderValue(std::u16string && value)380 void TextFieldContentModifier::SetPlaceholderValue(std::u16string&& value)
381 {
382 if (placeholderValue_->Get() != value) {
383 placeholderValue_->Set(value);
384 }
385 }
386
SetTextRectY(const float value)387 void TextFieldContentModifier::SetTextRectY(const float value)
388 {
389 if (textRectY_->Get() != value) {
390 textRectY_->Set(value);
391 }
392 }
393
GetTextRectY()394 float TextFieldContentModifier::GetTextRectY()
395 {
396 return textRectY_->Get();
397 }
398
SetTextObscured(bool value)399 void TextFieldContentModifier::SetTextObscured(bool value)
400 {
401 if (textObscured_) {
402 textObscured_->Set(value);
403 }
404 }
405
ChangeDragStatus()406 void TextFieldContentModifier::ChangeDragStatus()
407 {
408 dragStatus_->Set(!dragStatus_->Get());
409 }
410
SetTextRectX(const float value)411 void TextFieldContentModifier::SetTextRectX(const float value)
412 {
413 if (textRectX_->Get() != value) {
414 textRectX_->Set(value);
415 }
416 }
417
GetTextRectX()418 float TextFieldContentModifier::GetTextRectX()
419 {
420 return textRectX_->Get();
421 }
422
SetTextAlign(const TextAlign value)423 void TextFieldContentModifier::SetTextAlign(const TextAlign value)
424 {
425 if (textAlign_->Get() != static_cast<int32_t>(value)) {
426 textAlign_->Set(static_cast<int32_t>(value));
427 }
428 }
429
SetShowErrorState(bool value)430 void TextFieldContentModifier::SetShowErrorState(bool value)
431 {
432 if (showErrorState_) {
433 showErrorState_->Set(value);
434 }
435 }
436
SetShowUnderlineState(bool value)437 void TextFieldContentModifier::SetShowUnderlineState(bool value)
438 {
439 if (showUnderline_) {
440 showUnderline_->Set(value);
441 }
442 }
443
SetFontReady(bool value)444 void TextFieldContentModifier::SetFontReady(bool value)
445 {
446 if (fontReady_) {
447 fontReady_->Set(value);
448 }
449 }
450
ContentChange()451 void TextFieldContentModifier::ContentChange()
452 {
453 CHECK_NULL_VOID(contentChange_);
454 contentChange_->Set(!contentChange_->Get());
455 }
456
NeedMeasureUpdate(PropertyChangeFlag & flag)457 bool TextFieldContentModifier::NeedMeasureUpdate(PropertyChangeFlag& flag)
458 {
459 flag = 0;
460 if (fontSize_.has_value() && fontSizeFloat_ && !NearEqual(fontSize_.value().Value(), fontSizeFloat_->Get())) {
461 flag |= PROPERTY_UPDATE_MEASURE;
462 }
463 if (adaptMinFontSize_.has_value() && adaptMinFontSizeFloat_ &&
464 !NearEqual(adaptMinFontSize_.value().Value(), adaptMinFontSizeFloat_->Get())) {
465 flag |= PROPERTY_UPDATE_MEASURE;
466 }
467 if (adaptMaxFontSize_.has_value() && adaptMaxFontSizeFloat_ &&
468 !NearEqual(adaptMaxFontSize_.value().Value(), adaptMaxFontSizeFloat_->Get())) {
469 flag |= PROPERTY_UPDATE_MEASURE;
470 }
471 if (fontWeight_.has_value() && fontWeightFloat_ &&
472 !NearEqual(static_cast<float>(fontWeight_.value()), fontWeightFloat_->Get())) {
473 flag |= PROPERTY_UPDATE_MEASURE;
474 }
475 if (textColor_.has_value() && animatableTextColor_ &&
476 textColor_->GetValue() != animatableTextColor_->Get().GetValue()) {
477 flag |= PROPERTY_UPDATE_MEASURE_SELF;
478 }
479 UpdateTextDecorationMeasureFlag(flag);
480 flag &= (PROPERTY_UPDATE_MEASURE | PROPERTY_UPDATE_MEASURE_SELF | PROPERTY_UPDATE_MEASURE_SELF_AND_PARENT);
481 return flag;
482 }
483
SetTextDecoration(const TextDecoration & value,const Color & color,const TextDecorationStyle & style)484 void TextFieldContentModifier::SetTextDecoration(const TextDecoration& value, const Color& color,
485 const TextDecorationStyle& style)
486 {
487 auto oldTextDecoration = textDecoration_.value_or(TextDecoration::NONE);
488 auto oldTextDecorationColor = textDecorationColor_.value_or(Color::BLACK);
489 auto oldTextDecorationStyle = textDecorationStyle_.value_or(TextDecorationStyle::SOLID);
490 if ((oldTextDecoration == value) && (oldTextDecorationColor == color) && (oldTextDecorationStyle == style)) {
491 return;
492 }
493
494 textDecorationAnimatable_ = (oldTextDecoration == TextDecoration::NONE && value == TextDecoration::UNDERLINE) ||
495 (oldTextDecoration == TextDecoration::UNDERLINE && value == TextDecoration::NONE);
496
497 textDecoration_ = value;
498 textDecorationColor_ = color;
499 textDecorationStyle_ = style;
500 CHECK_NULL_VOID(textDecorationColorAlpha_);
501
502 oldColorAlpha_ = textDecorationColorAlpha_->Get();
503 if (textDecoration_ == TextDecoration::NONE) {
504 textDecorationColorAlpha_->Set(0.0f);
505 } else {
506 textDecorationColorAlpha_->Set(static_cast<float>(textDecorationColor_.value().GetAlpha()));
507 }
508 }
509
ModifyDecorationInTextStyle(TextStyle & textStyle)510 void TextFieldContentModifier::ModifyDecorationInTextStyle(TextStyle& textStyle)
511 {
512 if (textDecoration_.has_value() && textDecorationColor_.has_value() && textDecorationColorAlpha_) {
513 if (textDecorationAnimatable_) {
514 uint8_t alpha = static_cast<uint8_t>(std::floor(textDecorationColorAlpha_->Get() + ROUND_VALUE));
515 if (alpha == 0) {
516 textStyle.SetTextDecoration(TextDecoration::NONE);
517 textStyle.SetTextDecorationColor(textDecorationColor_.value());
518 } else {
519 textStyle.SetTextDecoration(TextDecoration::UNDERLINE);
520 textStyle.SetTextDecorationColor(Color(textDecorationColor_.value()).ChangeAlpha(alpha));
521 }
522 } else {
523 textStyle.SetTextDecoration(textDecoration_.value());
524 textStyle.SetTextDecorationColor(textDecorationColor_.value());
525 }
526 }
527 if (textDecorationStyle_.has_value()) {
528 textStyle.SetTextDecorationStyle(textDecorationStyle_.value());
529 }
530 }
531
UpdateTextDecorationMeasureFlag(PropertyChangeFlag & flag)532 void TextFieldContentModifier::UpdateTextDecorationMeasureFlag(PropertyChangeFlag& flag)
533 {
534 if (textDecoration_.has_value() && textDecorationColor_.has_value() && textDecorationColorAlpha_) {
535 uint8_t alpha = static_cast<uint8_t>(std::floor(textDecorationColorAlpha_->Get() + ROUND_VALUE));
536 if (textDecoration_.value() == TextDecoration::UNDERLINE && alpha != textDecorationColor_.value().GetAlpha()) {
537 flag |= PROPERTY_UPDATE_MEASURE;
538 } else if (textDecoration_.value() == TextDecoration::NONE && alpha != 0.0) {
539 flag |= PROPERTY_UPDATE_MEASURE;
540 }
541 }
542 }
543
SetTextFadeoutEnabled(bool enabled)544 void TextFieldContentModifier::SetTextFadeoutEnabled(bool enabled)
545 {
546 textFadeoutEnabled_ = enabled;
547 }
548
DoNormalDraw(DrawingContext & context)549 void TextFieldContentModifier::DoNormalDraw(DrawingContext& context)
550 {
551 auto& canvas = context.canvas;
552 auto textFieldPattern = DynamicCast<TextFieldPattern>(pattern_.Upgrade());
553 CHECK_NULL_VOID(textFieldPattern);
554 auto paragraph = textFieldPattern->GetParagraph();
555 CHECK_NULL_VOID(paragraph);
556 CHECK_NULL_VOID(contentOffset_);
557 auto contentOffset = contentOffset_->Get();
558 auto contentRect = textFieldPattern->GetContentRect();
559 auto clipRectHeight = 0.0f;
560 auto frameNode = textFieldPattern->GetHost();
561 CHECK_NULL_VOID(frameNode);
562 auto layoutProperty = frameNode->GetLayoutProperty<TextFieldLayoutProperty>();
563 CHECK_NULL_VOID(layoutProperty);
564 clipRectHeight = contentRect.GetY() + contentRect.Height();
565 canvas.Save();
566 RSRect clipInnerRect = RSRect(contentRect.GetX(), contentRect.GetY(),
567 contentRect.Width() + contentRect.GetX() + textFieldPattern->GetInlinePadding(), clipRectHeight);
568 canvas.ClipRect(clipInnerRect, RSClipOp::INTERSECT);
569 if (paragraph) {
570 auto textField = textFieldPattern->IsTextArea() ? "TextArea" : "TextInput";
571 ACE_LAYOUT_SCOPED_TRACE("[%s][id:%d] [Rect:%s]", textField, frameNode->GetId(), contentRect.ToString().c_str());
572 if (Container::GreatOrEqualAPIVersion(PlatformVersion::VERSION_ELEVEN)) {
573 canvas.Save();
574 RSRect clipRect;
575 std::vector<RSPoint> clipRadius;
576 GetFrameRectClip(clipRect, clipRadius);
577 canvas.ClipRoundRect(clipRect, clipRadius, true);
578 paragraph->Paint(canvas, textFieldPattern->GetTextRect().GetX(),
579 textFieldPattern->IsTextArea() ? textFieldPattern->GetTextRect().GetY() : contentOffset.GetY());
580 canvas.Restore();
581 } else {
582 paragraph->Paint(canvas, textFieldPattern->GetTextRect().GetX(),
583 textFieldPattern->IsTextArea() ? textFieldPattern->GetTextRect().GetY() : contentOffset.GetY());
584 }
585 }
586 canvas.Restore();
587 }
588
DoTextFadeoutDraw(DrawingContext & context)589 void TextFieldContentModifier::DoTextFadeoutDraw(DrawingContext& context)
590 {
591 auto& canvas = context.canvas;
592 auto textFieldPattern = DynamicCast<TextFieldPattern>(pattern_.Upgrade());
593 CHECK_NULL_VOID(textFieldPattern);
594 auto paragraph = textFieldPattern->GetParagraph();
595 CHECK_NULL_VOID(paragraph);
596 auto contentRect = textFieldPattern->GetContentRect();
597 auto clipRectHeight = contentRect.GetY() + contentRect.Height();
598 RSRect clipInnerRect = RSRect(contentRect.GetX(), contentRect.GetY(),
599 contentRect.Width() + contentRect.GetX() + textFieldPattern->GetInlinePadding(), clipRectHeight);
600 RSSaveLayerOps slo(&clipInnerRect, nullptr);
601 canvas.SaveLayer(slo);
602
603 DrawTextFadeout(context);
604
605 canvas.Restore();
606 }
607
DrawTextFadeout(DrawingContext & context)608 void TextFieldContentModifier::DrawTextFadeout(DrawingContext& context)
609 {
610 auto& canvas = context.canvas;
611 auto textFieldPattern = DynamicCast<TextFieldPattern>(pattern_.Upgrade());
612 CHECK_NULL_VOID(textFieldPattern);
613 auto frameNode = textFieldPattern->GetHost();
614 CHECK_NULL_VOID(frameNode);
615 auto paragraph = textFieldPattern->GetParagraph();
616 CHECK_NULL_VOID(paragraph);
617 auto contentOffset = contentOffset_->Get();
618 auto contentRect = frameNode->GetGeometryNode()->GetContentRect();
619 auto contentRectX = contentRect.GetX();
620 auto textRect = textFieldPattern->GetTextRect();
621 auto textRectX = textRect.GetX();
622 auto leftFadeOn = false;
623 auto rigthFadeOn = false;
624 auto textFadeoutWidth = DEFAULT_FADEOUT_VP.ConvertToPx();
625 auto gradientPercent = std::min(MAX_TEXTFADEOUT_PERCENT,
626 textFadeoutWidth / std::max(static_cast<double>(contentRect.Width()), textFadeoutWidth));
627 auto textFadeRect = RectF(contentRect.GetX(), contentOffset.GetY(), contentRect.Width(),
628 std::max(textRect.Height(), contentRect.Height()));
629 AdjustTextFadeRect(textFadeRect);
630
631 RSRect clipRect;
632 std::vector<RSPoint> clipRadius;
633 GetFrameRectClip(clipRect, clipRadius);
634 canvas.ClipRoundRect(clipRect, clipRadius, true);
635
636 canvas.Save();
637 RSRect clipTextInnerRect = RSRect(textFadeRect.GetX(), textFadeRect.GetY(),
638 textFadeRect.Width() + textFadeRect.GetX(), textFadeRect.GetY() + textFadeRect.Height());
639 canvas.ClipRect(clipTextInnerRect, RSClipOp::INTERSECT);
640 paragraph->Paint(canvas, textRectX, contentOffset.GetY());
641 canvas.Restore();
642
643 auto textIndent = std::max(textFieldPattern->GetTextParagraphIndent(), 0.0f);
644 auto textWidth = paragraph->GetTextWidth();
645 if (GreatNotEqual(textWidth + textIndent, contentRect.Width())) {
646 leftFadeOn = LessNotEqual(textRectX + MIN_TEXTFADEOUT_DELTA, contentRectX);
647 rigthFadeOn = GreatNotEqual((textRectX + textWidth + textIndent - MIN_TEXTFADEOUT_DELTA), contentRect.Right());
648 }
649 UpdateTextFadeout(canvas, textFadeRect, gradientPercent, leftFadeOn, rigthFadeOn);
650 }
651
AdjustTextFadeRect(RectF & textFadeRect)652 void TextFieldContentModifier::AdjustTextFadeRect(RectF& textFadeRect)
653 {
654 const float TEXT_FADE_ADJUST_PX = 1;
655
656 textFadeRect -= OffsetF(TEXT_FADE_ADJUST_PX, TEXT_FADE_ADJUST_PX);
657 textFadeRect += SizeF((TEXT_FADE_ADJUST_PX + TEXT_FADE_ADJUST_PX), (TEXT_FADE_ADJUST_PX + TEXT_FADE_ADJUST_PX));
658 }
659
UpdateTextFadeout(RSCanvas & canvas,const RectF & textRect,float gradientPercent,bool leftFade,bool rightFade)660 void TextFieldContentModifier::UpdateTextFadeout(
661 RSCanvas& canvas, const RectF& textRect, float gradientPercent, bool leftFade, bool rightFade)
662 {
663 RSBrush brush;
664 std::vector<RSPoint> points = { RSPoint(textRect.Left(), textRect.Top()),
665 RSPoint(textRect.Right(), textRect.Top()) };
666 std::vector<RSColorQuad> colors = { Color::TRANSPARENT.GetValue(), Color::WHITE.GetValue(), Color::WHITE.GetValue(),
667 Color::TRANSPARENT.GetValue() };
668 float leftEndPercent = leftFade ? gradientPercent : 0;
669 float rightStartPercent = 1.0f;
670 if (rightFade && gradientPercent > 0 && gradientPercent < 1.0f) {
671 rightStartPercent = 1.0f - gradientPercent;
672 }
673 std::vector<RSScalar> pos = { 0.0f, leftEndPercent, rightStartPercent, 1.0f };
674 brush.SetShaderEffect(
675 RSShaderEffect::CreateLinearGradient(points.at(0), points.at(1), colors, pos, RSTileMode::CLAMP));
676 brush.SetBlendMode(RSBlendMode::DST_IN);
677 RSRect textFadeoutRect = RSRect(textRect.Left(), textRect.Top(), textRect.Right(), textRect.Bottom());
678 canvas.AttachBrush(brush);
679 canvas.DrawRect(textFadeoutRect);
680 canvas.DetachBrush();
681 }
682
SetAutoFillTranslationOffset(const float value)683 void TextFieldContentModifier::SetAutoFillTranslationOffset(const float value)
684 {
685 CHECK_NULL_VOID(autoFillTranslationOffset_);
686 autoFillTranslationOffset_->Set(value);
687 }
688
SetDefaultAutoFillTranslationOffset()689 void TextFieldContentModifier::SetDefaultAutoFillTranslationOffset()
690 {
691 autoFillTranslationOffset_ = AceType::MakeRefPtr<AnimatablePropertyFloat>(0.0f);
692 AttachProperty(autoFillTranslationOffset_);
693 }
694
SetAutoFillTextScrollOffset(const float value)695 void TextFieldContentModifier::SetAutoFillTextScrollOffset(const float value)
696 {
697 CHECK_NULL_VOID(autoFillTextScrollOffset_);
698 autoFillTextScrollOffset_->Set(value);
699 }
700
SetDefaultAutoFillTextScrollOffset()701 void TextFieldContentModifier::SetDefaultAutoFillTextScrollOffset()
702 {
703 autoFillTextScrollOffset_ = AceType::MakeRefPtr<AnimatablePropertyFloat>(0.0f);
704 AttachProperty(autoFillTextScrollOffset_);
705 }
706
SetAutoFillEmphasizeCharIndex(const float value)707 void TextFieldContentModifier::SetAutoFillEmphasizeCharIndex(const float value)
708 {
709 CHECK_NULL_VOID(autoFillEmphasizeCharIndex_);
710 autoFillEmphasizeCharIndex_->Set(value);
711 }
712
SetAutoFillDefaultCharIndex(const float value)713 void TextFieldContentModifier::SetAutoFillDefaultCharIndex(const float value)
714 {
715 CHECK_NULL_VOID(autoFillDefaultCharIndex_);
716 autoFillDefaultCharIndex_->Set(value);
717 }
718
SetDefaultAutoFillCharIndex()719 void TextFieldContentModifier::SetDefaultAutoFillCharIndex()
720 {
721 autoFillEmphasizeCharIndex_ = AceType::MakeRefPtr<AnimatablePropertyFloat>(0.0f);
722 AttachProperty(autoFillEmphasizeCharIndex_);
723 autoFillDefaultCharIndex_ = AceType::MakeRefPtr<AnimatablePropertyFloat>(0.0f);
724 AttachProperty(autoFillDefaultCharIndex_);
725 }
726
SetAutoFillOriginTextColor(const Color & value)727 void TextFieldContentModifier::SetAutoFillOriginTextColor(const Color& value)
728 {
729 autoFillOriginTextColor_ = value;
730 }
731
DoAutoFillDraw(DrawingContext & context)732 void TextFieldContentModifier::DoAutoFillDraw(DrawingContext& context)
733 {
734 auto& canvas = context.canvas;
735 auto textFieldPattern = DynamicCast<TextFieldPattern>(pattern_.Upgrade());
736 CHECK_NULL_VOID(textFieldPattern);
737 auto autoFillController = textFieldPattern->GetOrCreateAutoFillController();
738 CHECK_NULL_VOID(autoFillController);
739 auto paragraph = autoFillController->GetAutoFillParagraph();
740 CHECK_NULL_VOID(paragraph);
741 CHECK_NULL_VOID(contentOffset_);
742 auto contentOffset = contentOffset_->Get();
743 auto contentRect = textFieldPattern->GetContentRect();
744 auto clipRectY1 = 0.0f;
745 auto frameNode = textFieldPattern->GetHost();
746 CHECK_NULL_VOID(frameNode);
747 auto layoutProperty = frameNode->GetLayoutProperty<TextFieldLayoutProperty>();
748 CHECK_NULL_VOID(layoutProperty);
749 clipRectY1 = contentRect.GetY() + contentRect.Height();
750
751 auto defaultCharIndex = std::ceil(autoFillDefaultCharIndex_->Get());
752 auto length = paragraph->GetParagraphText().length();
753 auto originTextColor = autoFillOriginTextColor_.value_or(autoFillDefaultCharInitTextColor_);
754 paragraph->UpdateColor(0, defaultCharIndex, originTextColor);
755 paragraph->UpdateColor(defaultCharIndex, length, autoFillEmphasizeCharTextColor_);
756
757 auto emphasizeTranslationOffset = autoFillTranslationOffset_->Get();
758 auto isRTL = layoutProperty->GetNonAutoLayoutDirection() == TextDirection::RTL;
759 canvas.Save();
760 auto textShowWidth = std::abs(emphasizeTranslationOffset);
761 auto clipRectX0 = contentRect.GetX();
762 auto clipRectX1 = contentRect.GetX() + textShowWidth;
763 if (isRTL) {
764 clipRectX0 = contentRect.GetX() + contentRect.Width() + emphasizeTranslationOffset;
765 clipRectX1 = contentRect.GetX() + contentRect.Width();
766 }
767 RSRect clipInnerRect = RSRect(clipRectX0, contentRect.GetY(), clipRectX1, clipRectY1);
768 canvas.ClipRect(clipInnerRect, RSClipOp::INTERSECT);
769 auto textRectX = autoFillController->GetAnimationTextRect().GetX();
770 if (paragraph) {
771 auto autoFillTextRectOffsetX = autoFillTextScrollOffset_->Get();
772 RSRect clipRect;
773 std::vector<RSPoint> clipRadius;
774 GetFrameRectClip(clipRect, clipRadius);
775 auto textFrameRect = textFieldPattern->GetFrameRect();
776 clipRect = RSRect(0.0f, 0.0f, clipRectX1, textFrameRect.Height());
777 if (isRTL) {
778 clipRect = RSRect(clipRectX0, 0.0f, clipRectX1, textFrameRect.Height());
779 }
780 canvas.ClipRoundRect(clipRect, clipRadius, true);
781 paragraph->Paint(canvas, textRectX - autoFillTextRectOffsetX, contentOffset.GetY());
782 }
783 canvas.Restore();
784 }
785 } // namespace OHOS::Ace::NG
786