From 367c73de72a628f7b52a74fbbe6ace3960c25c67 Mon Sep 17 00:00:00 2001 From: WangJiazhen Date: Fri, 29 Aug 2025 15:45:50 +0800 Subject: [PATCH] fillText,strokeText support globalCompositeOperation Signed-off-by: WangJiazhen --- .../canvas/custom_paint_paint_method.cpp | 52 ++++++++++++++----- .../canvas/custom_paint_paint_method.h | 3 +- 2 files changed, 42 insertions(+), 13 deletions(-) diff --git a/frameworks/core/components_ng/pattern/canvas/custom_paint_paint_method.cpp b/frameworks/core/components_ng/pattern/canvas/custom_paint_paint_method.cpp index 81821f01758..bcf4dd0dafb 100644 --- a/frameworks/core/components_ng/pattern/canvas/custom_paint_paint_method.cpp +++ b/frameworks/core/components_ng/pattern/canvas/custom_paint_paint_method.cpp @@ -1315,14 +1315,14 @@ void CustomPaintPaintMethod::PaintText( RSBrush compositeOperationpBrush; InitPaintBlend(compositeOperationpBrush); RSSaveLayerOps slo(nullptr, &compositeOperationpBrush); + if (shadowParagraph_ != nullptr && HasShadow()) { + PaintTextShadow(width, dx, dy, scale, &slo); + } if (Container::GreatOrEqualAPITargetVersion(PlatformVersion::VERSION_THIRTEEN)) { if (state_.globalState.GetType() != CompositeOperation::SOURCE_OVER) { rsCanvas_->SaveLayer(slo); } } - if (isStroke && shadowParagraph_ != nullptr && HasShadow()) { - PaintStrokeTextShadow(width, dx, dy, scale, &slo); - } if (scale.has_value()) { if (!NearZero(scale.value())) { dx /= scale.value(); @@ -1341,7 +1341,7 @@ void CustomPaintPaintMethod::PaintText( } } -void CustomPaintPaintMethod::PaintStrokeTextShadow( +void CustomPaintPaintMethod::PaintTextShadow( const float width, const double dx, const double dy, const std::optional scale, RSSaveLayerOps* slo) { CHECK_NULL_VOID(rsCanvas_); @@ -2183,7 +2183,7 @@ bool CustomPaintPaintMethod::UpdateFillParagraph(const std::string& text) std::unique_ptr builder = RSParagraphBuilder::Create(style, fontCollection); CHECK_NULL_RETURN(builder, false); RSTextStyle txtStyle; - if (HasShadow()) { + if ((Container::LessThanAPITargetVersion(PlatformVersion::VERSION_THIRTEEN)) && HasShadow()) { Rosen::TextShadow txtShadow; txtShadow.color = state_.shadow.GetColor().GetValue(); txtShadow.offset.SetX(state_.shadow.GetOffset().GetX()); @@ -2199,8 +2199,10 @@ bool CustomPaintPaintMethod::UpdateFillParagraph(const std::string& text) RSBrush brush; RSSamplingOptions options; GetFillPaint(brush, options); - InitPaintBlend(brush); txtStyle.foregroundBrush = brush; + if (HasShadow()) { + UpdateFillShadowParagraph(text, &brush, style); + } } else { UpdateFillTxtStyle(txtStyle); } @@ -2213,6 +2215,38 @@ bool CustomPaintPaintMethod::UpdateFillParagraph(const std::string& text) #endif } +void CustomPaintPaintMethod::UpdateFillShadowParagraph( + const std::string& text, const RSBrush* brush, const RSParagraphStyle& style) +{ +#ifndef ACE_UNITTEST + auto fontCollection = RosenFontCollection::GetInstance().GetFontCollection(); + CHECK_NULL_VOID(fontCollection); + RSTextStyle shadowStyle; + shadowStyle.locale = Localization::GetInstance()->GetFontLocale(); + UpdateFontFamilies(); + ConvertTxtStyle(state_.strokeState.GetTextStyle(), shadowStyle); + shadowStyle.fontSize = state_.strokeState.GetTextStyle().GetFontSize().Value(); + RSBrush shadowBrush; + shadowBrush.SetColor(state_.shadow.GetColor().GetValue()); + shadowBrush.SetAntiAlias(true); + + if (state_.globalState.HasGlobalAlpha()) { + shadowBrush.SetAlphaF( + state_.globalState.GetAlpha() * static_cast(state_.shadow.GetColor().GetValue()) / MAX_GRAYSCALE); + } + RSFilter filter; + filter.SetMaskFilter(RSMaskFilter::CreateBlurMaskFilter( + RSBlurType::NORMAL, RosenDecorationPainter::ConvertRadiusToSigma(state_.shadow.GetBlurRadius()))); + shadowBrush.SetFilter(filter); + shadowStyle.foregroundBrush = shadowBrush; + std::unique_ptr shadowBuilder = RSParagraphBuilder::Create(style, fontCollection); + CHECK_NULL_VOID(shadowBuilder); + shadowBuilder->PushStyle(shadowStyle); + shadowBuilder->AppendText(StringUtils::Str8ToStr16(text)); + shadowParagraph_ = shadowBuilder->CreateTypography(); +#endif +} + void CustomPaintPaintMethod::UpdateFillTxtStyle(RSTextStyle& txtStyle) { #ifndef ACE_UNITTEST @@ -2256,9 +2290,6 @@ bool CustomPaintPaintMethod::UpdateStrokeParagraph(const std::string& text) RSPen pen; RSSamplingOptions options; GetStrokePaint(pen, options); - if (Container::GreatOrEqualAPITargetVersion(PlatformVersion::VERSION_THIRTEEN)) { - InitPaintBlend(pen); - } ConvertTxtStyle(state_.strokeState.GetTextStyle(), txtStyle); txtStyle.fontSize = state_.strokeState.GetTextStyle().GetFontSize().Value(); txtStyle.foregroundPen = pen; @@ -2300,9 +2331,6 @@ void CustomPaintPaintMethod::UpdateStrokeShadowParagraph( filter.SetMaskFilter(RSMaskFilter::CreateBlurMaskFilter( RSBlurType::NORMAL, RosenDecorationPainter::ConvertRadiusToSigma(state_.shadow.GetBlurRadius()))); shadowPen.SetFilter(filter); - if (Container::GreatOrEqualAPITargetVersion(PlatformVersion::VERSION_THIRTEEN)) { - InitPaintBlend(shadowPen); - } shadowStyle.foregroundPen = shadowPen; std::unique_ptr shadowBuilder = RSParagraphBuilder::Create(style, fontCollection); CHECK_NULL_VOID(shadowBuilder); diff --git a/frameworks/core/components_ng/pattern/canvas/custom_paint_paint_method.h b/frameworks/core/components_ng/pattern/canvas/custom_paint_paint_method.h index 26bbf70a934..f66ff5f2efc 100644 --- a/frameworks/core/components_ng/pattern/canvas/custom_paint_paint_method.h +++ b/frameworks/core/components_ng/pattern/canvas/custom_paint_paint_method.h @@ -309,6 +309,7 @@ protected: void UpdateFillTxtStyle(RSTextStyle& txtStyle); bool UpdateStrokeParagraph(const std::string& text); void UpdateStrokeShadowParagraph(const std::string& text, const RSPen* pen, const RSParagraphStyle& style); + void UpdateFillShadowParagraph(const std::string& text, const RSBrush* brush, const RSParagraphStyle& style); void InitPaintBlend(RSBrush& brush); void InitPaintBlend(RSPen& pen); std::shared_ptr MakeConicGradient( @@ -364,7 +365,7 @@ protected: void PaintImageShadow(const RSPath& path, const Shadow& shadow, const RSBrush* brush = nullptr, const RSPen* pen = nullptr, RSSaveLayerOps* slo = nullptr); void PaintText(const float width, double x, double y, std::optional maxWidth, bool isStroke); - void PaintStrokeTextShadow( + void PaintTextShadow( const float width, const double dx, const double dy, const std::optional scale, RSSaveLayerOps* slo); double GetAlignOffset(TextAlign align, double width); double GetBaselineOffset(TextBaseline baseline, std::unique_ptr& paragraph); -- Gitee