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