• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_CUSTOM_PAINT_CUSTOM_PAINT_PAINT_METHOD_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_CUSTOM_PAINT_CUSTOM_PAINT_PAINT_METHOD_H
18 
19 #include "base/geometry/ng/offset_t.h"
20 #include "base/memory/ace_type.h"
21 #include "base/utils/macros.h"
22 #include "core/common/font_manager.h"
23 #include "core/components/common/properties/paint_state.h"
24 #include "core/components_ng/image_provider/svg_dom_base.h"
25 #include "core/components_ng/pattern/canvas/canvas_modifier.h"
26 #include "core/components_ng/render/drawing.h"
27 #include "core/components_ng/render/node_paint_method.h"
28 #ifndef ACE_UNITTEST
29 #include "core/image/image_loader.h"
30 #include "core/image/image_object.h"
31 #include "core/image/image_source_info.h"
32 #endif
33 #include "core/pipeline_ng/pipeline_context.h"
34 
35 namespace OHOS::Ace::NG {
36 
37 const int32_t DEFAULT_SAVE_COUNT = 1;
38 
39 enum class FilterType {
40     NONE,
41     GRAYSCALE,
42     SEPIA,
43     INVERT,
44     OPACITY,
45     BRIGHTNESS,
46     CONTRAST,
47     BLUR,
48     DROP_SHADOW,
49     SATURATE,
50     HUE_ROTATE
51 };
52 
53 struct FilterProperty {
54     FilterType filterType_;
55     std::string filterParam_;
56 };
57 
58 class CustomPaintPaintMethod : public NodePaintMethod {
59     DECLARE_ACE_TYPE(CustomPaintPaintMethod, NodePaintMethod)
60 public:
61     CustomPaintPaintMethod();
62     ~CustomPaintPaintMethod() override = default;
63 
GetContentModifier(PaintWrapper * paintWrapper)64     RefPtr<Modifier> GetContentModifier(PaintWrapper* paintWrapper) override
65     {
66         return contentModifier_;
67     }
68 
69     void SetFillRuleForPath(const CanvasFillRule& rule);
70     void SetFillRuleForPath2D(const CanvasFillRule& rule);
71 
72     void FillRect(const Rect& rect);
73     void StrokeRect(const Rect& rect);
74     void ClearRect(const Rect& rect);
75     void Fill();
76     void Fill(const RefPtr<CanvasPath2D>& path);
77     void Stroke();
78     void Stroke(const RefPtr<CanvasPath2D>& path);
79     void Clip();
80     void Clip(const RefPtr<CanvasPath2D>& path);
81     void BeginPath();
82     void ClosePath();
83     void MoveTo(double x, double y);
84     void LineTo(double x, double y);
85     void Arc(const ArcParam& param);
86     void ArcTo(const ArcToParam& param);
87     void AddRect(const Rect& rect);
88     void Ellipse(const EllipseParam& param);
89     void BezierCurveTo(const BezierCurveParam& param);
90     void QuadraticCurveTo(const QuadraticCurveParam& param);
91     void PutImageData(const Ace::ImageData& imageData);
92 
93     void Save();
94     void Restore();
95     void Scale(double x, double y);
96     void Rotate(double angle);
97     void SetTransform(const TransformParam& param);
98     virtual TransformParam GetTransform() const;
99     void ResetTransform();
100     void Transform(const TransformParam& param);
101     void Translate(double x, double y);
102     void SaveLayer();
103     void RestoreLayer();
104     void SetFilterParam(const std::string& filterStr);
105 
SetAntiAlias(bool isEnabled)106     void SetAntiAlias(bool isEnabled)
107     {
108         antiAlias_ = isEnabled;
109     }
110 
SetFillColor(const Color & color)111     void SetFillColor(const Color& color)
112     {
113         state_.fillState.SetColor(color);
114         state_.fillState.SetTextColor(color);
115     }
116 
SetFillPattern(const Ace::Pattern & pattern)117     void SetFillPattern(const Ace::Pattern& pattern)
118     {
119         state_.fillState.SetPattern(pattern);
120     }
121 
SetFillPatternNG(const std::weak_ptr<Ace::Pattern> & pattern)122     void SetFillPatternNG(const std::weak_ptr<Ace::Pattern>& pattern)
123     {
124         state_.fillState.SetPatternNG(pattern);
125     }
126 
SetFillGradient(const Ace::Gradient & gradient)127     void SetFillGradient(const Ace::Gradient& gradient)
128     {
129         state_.fillState.SetGradient(gradient);
130     }
131 
SetAlpha(double alpha)132     void SetAlpha(double alpha)
133     {
134         state_.globalState.SetAlpha(alpha);
135     }
136 
SetCompositeType(CompositeOperation operation)137     void SetCompositeType(CompositeOperation operation)
138     {
139         state_.globalState.SetType(operation);
140     }
141 
142     // direction is also available in strokeText
SetTextDirection(TextDirection direction)143     void SetTextDirection(TextDirection direction)
144     {
145         state_.fillState.SetOffTextDirection(direction);
146     }
147 
SetStrokeColor(const Color & color)148     void SetStrokeColor(const Color& color)
149     {
150         state_.strokeState.SetColor(color);
151     }
152 
SetStrokePatternNG(const std::weak_ptr<Ace::Pattern> & pattern)153     void SetStrokePatternNG(const std::weak_ptr<Ace::Pattern>& pattern)
154     {
155         state_.strokeState.SetPatternNG(pattern);
156     }
157 
SetStrokePattern(const Ace::Pattern & pattern)158     void SetStrokePattern(const Ace::Pattern& pattern)
159     {
160         state_.strokeState.SetPattern(pattern);
161     }
162 
SetStrokeGradient(const Ace::Gradient & gradient)163     void SetStrokeGradient(const Ace::Gradient& gradient)
164     {
165         state_.strokeState.SetGradient(gradient);
166     }
167 
SetLineCap(LineCapStyle style)168     void SetLineCap(LineCapStyle style)
169     {
170         state_.strokeState.SetLineCap(style);
171     }
172 
SetLineDashOffset(double offset)173     void SetLineDashOffset(double offset)
174     {
175         state_.strokeState.SetLineDashOffset(offset);
176     }
177 
SetLineJoin(LineJoinStyle style)178     void SetLineJoin(LineJoinStyle style)
179     {
180         state_.strokeState.SetLineJoin(style);
181     }
182 
SetLineWidth(double width)183     void SetLineWidth(double width)
184     {
185         state_.strokeState.SetLineWidth(width);
186     }
187 
SetMiterLimit(double limit)188     void SetMiterLimit(double limit)
189     {
190         state_.strokeState.SetMiterLimit(limit);
191     }
192 
GetLineDash()193     virtual LineDashParam GetLineDash() const
194     {
195         return lineDash_;
196     }
197 
SetLineDashParam(const std::vector<double> & segments)198     void SetLineDashParam(const std::vector<double>& segments)
199     {
200         lineDash_.lineDash = segments;
201     }
202 
SetLineDash(const std::vector<double> & segments)203     void SetLineDash(const std::vector<double>& segments)
204     {
205         state_.strokeState.SetLineDash(segments);
206     }
207 
SetTextAlign(TextAlign align)208     void SetTextAlign(TextAlign align)
209     {
210         state_.fillState.SetTextAlign(align);
211         state_.strokeState.SetTextAlign(align);
212     }
213 
SetTextBaseline(TextBaseline baseline)214     void SetTextBaseline(TextBaseline baseline)
215     {
216         state_.fillState.SetTextBaseline(baseline);
217         state_.strokeState.SetTextBaseline(baseline);
218     }
219 
SetShadowColor(const Color & color)220     void SetShadowColor(const Color& color)
221     {
222         state_.shadow.SetColor(color);
223     }
224 
SetShadowBlur(double blur)225     void SetShadowBlur(double blur)
226     {
227         state_.shadow.SetBlurRadius(blur);
228     }
229 
SetShadowOffsetX(double x)230     void SetShadowOffsetX(double x)
231     {
232         state_.shadow.SetOffsetX(x);
233     }
234 
SetShadowOffsetY(double y)235     void SetShadowOffsetY(double y)
236     {
237         state_.shadow.SetOffsetY(y);
238     }
239 
SetSmoothingEnabled(bool enabled)240     void SetSmoothingEnabled(bool enabled)
241     {
242         smoothingEnabled_ = enabled;
243     }
244 
SetSmoothingQuality(const std::string & quality)245     void SetSmoothingQuality(const std::string& quality)
246     {
247         smoothingQuality_ = quality;
248     }
249 
SetFontSize(const Dimension & size)250     void SetFontSize(const Dimension& size)
251     {
252         state_.fillState.SetFontSize(size);
253         state_.strokeState.SetFontSize(size);
254     }
255 
SetLetterSpacing(const Dimension & letterSpacing)256     void SetLetterSpacing(const Dimension& letterSpacing)
257     {
258         state_.fillState.SetLetterSpacing(letterSpacing);
259         state_.strokeState.SetLetterSpacing(letterSpacing);
260     }
261 
SetFontStyle(OHOS::Ace::FontStyle style)262     void SetFontStyle(OHOS::Ace::FontStyle style)
263     {
264         state_.fillState.SetFontStyle(style);
265         state_.strokeState.SetFontStyle(style);
266     }
267 
SetFontWeight(FontWeight weight)268     void SetFontWeight(FontWeight weight)
269     {
270         state_.fillState.SetFontWeight(weight);
271         state_.strokeState.SetFontWeight(weight);
272     }
273 
SetFontFamilies(const std::vector<std::string> & fontFamilies)274     void SetFontFamilies(const std::vector<std::string>& fontFamilies)
275     {
276         state_.fillState.SetFontFamilies(fontFamilies);
277         state_.strokeState.SetFontFamilies(fontFamilies);
278     }
279 
280     void SaveProperties();
281     void RestoreProperties();
282     void ResetTransformMatrix();
283     void ResetLineDash();
284     void RotateMatrix(double angle);
285     void ScaleMatrix(double x, double y);
286     void SetTransformMatrix(const TransformParam& param);
287     void TransformMatrix(const TransformParam& param);
288     void TranslateMatrix(double tx, double ty);
289     void DrawSvgImage(RefPtr<SvgDomBase> svgDom, const Ace::CanvasImage& canvasImage, const ImageFit& imageFit);
290     void DrawImage(const Ace::CanvasImage& canvasImage, double width, double height);
291     void FillText(const std::string& text, double x, double y, std::optional<double> maxWidth);
292     void StrokeText(const std::string& text, double x, double y, std::optional<double> maxWidth);
293     TextMetrics MeasureTextMetrics(const std::string& text, const PaintState& state);
294     void SetTransform(std::shared_ptr<Ace::Pattern> pattern, const TransformParam& transform);
SetDensity(double density)295     void SetDensity(double density)
296     {
297         density_ = density;
298     }
299 
300 protected:
301     std::optional<double> CalcTextScale(double maxIntrinsicWidth, std::optional<double> maxWidth);
302     bool HasShadow() const;
303     void UpdateFontFamilies();
304     void UpdateLineDash(RSPen& pen);
305     void UpdatePaintShader(RSPen* pen, RSBrush* brush, const Ace::Gradient& gradient);
306     void UpdatePaintShader(const Ace::Pattern& pattern, RSPen* pen, RSBrush* brush);
307     bool UpdateFillParagraph(const std::string& text);
308     void UpdateFillTxtStyle(RSTextStyle& txtStyle);
309     bool UpdateStrokeParagraph(const std::string& text);
310     void UpdateStrokeShadowParagraph(const std::string& text, const RSPen* pen, const RSParagraphStyle& style);
311     void InitPaintBlend(RSBrush& brush);
312     void InitPaintBlend(RSPen& pen);
313     std::shared_ptr<RSShaderEffect> MakeConicGradient(const Ace::Gradient& gradient);
314 
315     void Path2DFill();
316     void Path2DStroke();
317     void Path2DClip();
318     void ParsePath2D(const RefPtr<CanvasPath2D>& path);
319     void Path2DAddPath(const PathArgs& args);
320     void Path2DClosePath();
321     void Path2DMoveTo(const PathArgs& args);
322     void Path2DLineTo(const PathArgs& args);
323     void Path2DArc(const PathArgs& args);
324     void Path2DArcTo(const PathArgs& args);
325     void Path2DRect(const PathArgs& args);
326     void Path2DEllipse(const PathArgs& args);
327     void Path2DBezierCurveTo(const PathArgs& args);
328     void Path2DQuadraticCurveTo(const PathArgs& args);
329     void Path2DSetTransform(const PathArgs& args);
330     RSMatrix GetMatrixFromPattern(const Ace::Pattern& pattern);
331 
332     void SetGrayFilter(const std::string& percent);
333     void SetSepiaFilter(const std::string& percent);
334     void SetSaturateFilter(const std::string& percent);
335     void SetHueRotateFilter(const std::string& percent);
336     void SetInvertFilter(const std::string& percent);
337     void SetOpacityFilter(const std::string& percent);
338     void SetBrightnessFilter(const std::string& percent);
339     void SetContrastFilter(const std::string& percent);
340     void SetBlurFilter(const std::string& percent);
341 
342     bool GetFilterType(const std::string& filterStr, std::vector<FilterProperty>& filters);
343     bool IsPercentStr(std::string& percentStr);
344     double PxStrToDouble(const std::string& str);
345     double BlurStrToDouble(const std::string& str);
346     bool CheckNumberAndPercentage(const std::string& param, bool isClamped, float& result);
347     void InitImagePaint(RSPen* pen, RSBrush* brush, RSSamplingOptions& options);
348     void GetStrokePaint(RSPen& pen, RSSamplingOptions& options);
349     void GetFillPaint(RSBrush& brush, RSSamplingOptions& options);
350 
351     void SetPaintImage(RSPen* pen, RSBrush* brush);
352     void ClearPaintImage(RSPen* pen, RSBrush* brush);
353     float PercentStrToFloat(const std::string& percentStr);
354     bool CheckFilterProperty(FilterType filterType, const std::string& filterParam);
355     bool ParseFilter(std::string& filter, std::vector<FilterProperty>& filters);
356     FilterType FilterStrToFilterType(const std::string& filterStr);
357 
358     std::shared_ptr<RSImage> GetImage(const std::string& src);
359     void PaintShadow(const RSPath& path, const Shadow& shadow, const RSBrush* brush = nullptr,
360         const RSPen* pen = nullptr, RSSaveLayerOps* slo = nullptr);
361     void PaintImageShadow(const RSPath& path, const Shadow& shadow, const RSBrush* brush = nullptr,
362         const RSPen* pen = nullptr, RSSaveLayerOps* slo = nullptr);
363     void PaintText(const float width, double x, double y, std::optional<double> maxWidth, bool isStroke);
364     void PaintStrokeTextShadow(
365         const float width, const double dx, const double dy, const std::optional<double> scale, RSSaveLayerOps* slo);
366     double GetAlignOffset(TextAlign align, double width);
367     double GetBaselineOffset(TextBaseline baseline, std::unique_ptr<RSParagraph>& paragraph);
368     RSTextAlign GetEffectiveAlign(RSTextAlign align, RSTextDirection direction) const;
369 #ifndef ACE_UNITTEST
370     double GetFontBaseline(const Rosen::Drawing::FontMetrics& fontMetrics, TextBaseline baseline) const;
371     double GetFontAlign(TextAlign align, std::unique_ptr<RSParagraph>& paragraph) const;
372     virtual void ConvertTxtStyle(const TextStyle& textStyle, Rosen::TextStyle& txtStyle) = 0;
373 #endif
374     void ResetStates();
375     void DrawImageInternal(const Ace::CanvasImage& canvasImage, const std::shared_ptr<RSImage>& image);
376 
377     // PaintHolder includes fillState, strokeState, globalState and shadow for save
378     PaintHolder state_;
379     std::vector<PaintHolder> saveStates_;
380     LineDashParam lineDash_;
381     RSMatrix matrix_;
382     std::vector<RSMatrix> matrixStates_;
383     std::vector<LineDashParam> lineDashStates_;
384 
385     bool smoothingEnabled_ = true;
386     std::string smoothingQuality_ = "low";
387     bool antiAlias_ = false;
388     std::unique_ptr<RSParagraph> paragraph_;
389     std::unique_ptr<RSParagraph> shadowParagraph_;
390 
391     WeakPtr<PipelineBase> context_;
392 
393     bool isPathChanged_ = true;
394     bool isPath2dChanged_ = true;
395     RSPath rsPath_;
396     RSPath rsPath2d_;
397     RSBrush imageBrush_;
398     RSSamplingOptions sampleOptions_;
399     std::shared_ptr<RSCanvas> rsCanvas_;
400 
401     Ace::CanvasImage canvasImage_;
402     std::unique_ptr<Shadow> imageShadow_;
403     RSColorMatrix colorMatrix_;
404     double density_ = 1.0;
405 
406 #ifndef ACE_UNITTEST
407     sk_sp<SkSVGDOM> skiaDom_ = nullptr;
408     ImageSourceInfo currentSource_;
409     ImageSourceInfo loadingSource_;
410 #endif
411 
412     RefPtr<CanvasModifier> contentModifier_;
413 
414     SizeF lastLayoutSize_;
415     RefPtr<ImageCache> imageCache_;
416     enum DrawImageType {
417         THREE_PARAMS,
418         FIVE_PARAMS,
419         NINE_PARAMS,
420     };
421     static const LinearMapNode<void (*)(std::shared_ptr<RSImage>&, std::shared_ptr<RSShaderEffect>&, RSMatrix&)>
422         staticPattern[];
423     const float defaultOpacity = 1.0f;
424     std::shared_ptr<RSColorFilter> colorFilter_ = RSColorFilter::CreateMatrixColorFilter(colorMatrix_);
425     std::shared_ptr<RSImageFilter> blurFilter_ = RSImageFilter::CreateBlurImageFilter(0, 0, RSTileMode::DECAL, nullptr);
426     std::vector<std::shared_ptr<RSColorFilter>> saveColorFilter_;
427     std::vector<std::shared_ptr<RSImageFilter>> saveBlurFilter_;
428     int32_t apiVersion_ = 0;
429 };
430 } // namespace OHOS::Ace::NG
431 
432 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_CUSTOM_PAINT_CUSTOM_PAINT_PAINT_METHOD_H
433