1 /*
2 * Copyright (c) 2022-2024 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/canvas/offscreen_canvas_pattern.h"
17
18 #include "base/utils/utils.h"
19 #include "core/common/ace_application_info.h"
20 #include "core/components_ng/pattern/canvas/offscreen_canvas_paint_method.h"
21
22 namespace OHOS::Ace::NG {
OffscreenCanvasPattern(int32_t width,int32_t height)23 OffscreenCanvasPattern::OffscreenCanvasPattern(int32_t width, int32_t height)
24 {
25 offscreenPaintMethod_ = MakeRefPtr<OffscreenCanvasPaintMethod>(std::max(width, 0), std::max(height, 0));
26 }
27
UpdateSize(int32_t width,int32_t height)28 void OffscreenCanvasPattern::UpdateSize(int32_t width, int32_t height)
29 {
30 CHECK_NULL_VOID(offscreenPaintMethod_);
31 return offscreenPaintMethod_->UpdateSize(std::max(width, 0), std::max(height, 0));
32 }
33
FillRect(const Rect & rect)34 void OffscreenCanvasPattern::FillRect(const Rect& rect)
35 {
36 offscreenPaintMethod_->FillRect(rect);
37 }
38
StrokeRect(const Rect & rect)39 void OffscreenCanvasPattern::StrokeRect(const Rect& rect)
40 {
41 offscreenPaintMethod_->StrokeRect(rect);
42 }
43
ClearRect(const Rect & rect)44 void OffscreenCanvasPattern::ClearRect(const Rect& rect)
45 {
46 offscreenPaintMethod_->ClearRect(rect);
47 }
48
Fill()49 void OffscreenCanvasPattern::Fill()
50 {
51 offscreenPaintMethod_->Fill();
52 }
53
Fill(const RefPtr<CanvasPath2D> & path)54 void OffscreenCanvasPattern::Fill(const RefPtr<CanvasPath2D>& path)
55 {
56 offscreenPaintMethod_->Fill(path);
57 }
58
Stroke()59 void OffscreenCanvasPattern::Stroke()
60 {
61 offscreenPaintMethod_->Stroke();
62 }
63
Stroke(const RefPtr<CanvasPath2D> & path)64 void OffscreenCanvasPattern::Stroke(const RefPtr<CanvasPath2D>& path)
65 {
66 offscreenPaintMethod_->Stroke(path);
67 }
68
Clip()69 void OffscreenCanvasPattern::Clip()
70 {
71 offscreenPaintMethod_->Clip();
72 }
73
Clip(const RefPtr<CanvasPath2D> & path)74 void OffscreenCanvasPattern::Clip(const RefPtr<CanvasPath2D>& path)
75 {
76 offscreenPaintMethod_->Clip(path);
77 }
78
BeginPath()79 void OffscreenCanvasPattern::BeginPath()
80 {
81 offscreenPaintMethod_->BeginPath();
82 }
83
ClosePath()84 void OffscreenCanvasPattern::ClosePath()
85 {
86 offscreenPaintMethod_->ClosePath();
87 }
88
MoveTo(double x,double y)89 void OffscreenCanvasPattern::MoveTo(double x, double y)
90 {
91 offscreenPaintMethod_->MoveTo(x, y);
92 }
93
LineTo(double x,double y)94 void OffscreenCanvasPattern::LineTo(double x, double y)
95 {
96 offscreenPaintMethod_->LineTo(x, y);
97 }
98
Arc(const ArcParam & param)99 void OffscreenCanvasPattern::Arc(const ArcParam& param)
100 {
101 offscreenPaintMethod_->Arc(param);
102 }
103
ArcTo(const ArcToParam & param)104 void OffscreenCanvasPattern::ArcTo(const ArcToParam& param)
105 {
106 offscreenPaintMethod_->ArcTo(param);
107 }
108
AddRect(const Rect & rect)109 void OffscreenCanvasPattern::AddRect(const Rect& rect)
110 {
111 offscreenPaintMethod_->AddRect(rect);
112 }
113
AddRoundRect(const Rect & rect,const std::vector<double> & radii)114 void OffscreenCanvasPattern::AddRoundRect(const Rect& rect, const std::vector<double>& radii)
115 {
116 offscreenPaintMethod_->AddRoundRect(rect, radii);
117 }
118
Ellipse(const EllipseParam & param)119 void OffscreenCanvasPattern::Ellipse(const EllipseParam& param)
120 {
121 offscreenPaintMethod_->Ellipse(param);
122 }
123
BezierCurveTo(const BezierCurveParam & param)124 void OffscreenCanvasPattern::BezierCurveTo(const BezierCurveParam& param)
125 {
126 offscreenPaintMethod_->BezierCurveTo(param);
127 }
128
QuadraticCurveTo(const QuadraticCurveParam & param)129 void OffscreenCanvasPattern::QuadraticCurveTo(const QuadraticCurveParam& param)
130 {
131 offscreenPaintMethod_->QuadraticCurveTo(param);
132 }
133
FillText(const std::string & text,double x,double y,std::optional<double> maxWidth)134 void OffscreenCanvasPattern::FillText(const std::string& text, double x, double y, std::optional<double> maxWidth)
135 {
136 UpdateTextDefaultDirection();
137 offscreenPaintMethod_->FillText(text, x, y, maxWidth);
138 }
139
StrokeText(const std::string & text,double x,double y,std::optional<double> maxWidth)140 void OffscreenCanvasPattern::StrokeText(const std::string& text, double x, double y, std::optional<double> maxWidth)
141 {
142 UpdateTextDefaultDirection();
143 offscreenPaintMethod_->StrokeText(text, x, y, maxWidth);
144 }
145
MeasureTextMetrics(const std::string & text,const PaintState & state)146 TextMetrics OffscreenCanvasPattern::MeasureTextMetrics(const std::string& text, const PaintState& state)
147 {
148 return offscreenPaintMethod_->MeasureTextMetrics(text, state);
149 }
150
DrawImage(const Ace::CanvasImage & image,double width,double height)151 void OffscreenCanvasPattern::DrawImage(const Ace::CanvasImage& image, double width, double height)
152 {
153 offscreenPaintMethod_->DrawImage(image, width, height);
154 }
155
DrawSvgImage(RefPtr<SvgDomBase> svgDom,const Ace::CanvasImage & image,const ImageFit & imageFit)156 void OffscreenCanvasPattern::DrawSvgImage(
157 RefPtr<SvgDomBase> svgDom, const Ace::CanvasImage& image, const ImageFit& imageFit)
158 {
159 offscreenPaintMethod_->DrawSvgImage(svgDom, image, imageFit);
160 }
161
DrawPixelMap(RefPtr<PixelMap> pixelMap,const Ace::CanvasImage & image)162 void OffscreenCanvasPattern::DrawPixelMap(RefPtr<PixelMap> pixelMap, const Ace::CanvasImage& image)
163 {
164 offscreenPaintMethod_->DrawPixelMap(pixelMap, image);
165 }
166
GetImageData(double left,double top,double width,double height)167 std::unique_ptr<Ace::ImageData> OffscreenCanvasPattern::GetImageData(
168 double left, double top, double width, double height)
169 {
170 return offscreenPaintMethod_->GetImageData(left, top, width, height);
171 }
172
GetImageData(const std::shared_ptr<Ace::ImageData> & imageData)173 void OffscreenCanvasPattern::GetImageData(const std::shared_ptr<Ace::ImageData>& imageData)
174 {
175 CHECK_NULL_VOID(offscreenPaintMethod_);
176 offscreenPaintMethod_->GetImageData(imageData);
177 }
178
PutImageData(const Ace::ImageData & imageData)179 void OffscreenCanvasPattern::PutImageData(const Ace::ImageData& imageData)
180 {
181 offscreenPaintMethod_->PutImageData(imageData);
182 }
183
SetAntiAlias(bool isEnabled)184 void OffscreenCanvasPattern::SetAntiAlias(bool isEnabled)
185 {
186 offscreenPaintMethod_->SetAntiAlias(isEnabled);
187 }
188
SetFillColor(const Color & color)189 void OffscreenCanvasPattern::SetFillColor(const Color& color)
190 {
191 offscreenPaintMethod_->SetFillColor(color);
192 }
193
SetFillRuleForPath(const CanvasFillRule rule)194 void OffscreenCanvasPattern::SetFillRuleForPath(const CanvasFillRule rule)
195 {
196 offscreenPaintMethod_->SetFillRuleForPath(rule);
197 }
198
SetFillRuleForPath2D(const CanvasFillRule rule)199 void OffscreenCanvasPattern::SetFillRuleForPath2D(const CanvasFillRule rule)
200 {
201 offscreenPaintMethod_->SetFillRuleForPath2D(rule);
202 }
203
GetWidth()204 int32_t OffscreenCanvasPattern::GetWidth()
205 {
206 return offscreenPaintMethod_->GetWidth();
207 }
208
GetHeight()209 int32_t OffscreenCanvasPattern::GetHeight()
210 {
211 return offscreenPaintMethod_->GetHeight();
212 }
213
GetLineDash() const214 LineDashParam OffscreenCanvasPattern::GetLineDash() const
215 {
216 return offscreenPaintMethod_->GetLineDash();
217 }
218
SetLineDash(const std::vector<double> & segments)219 void OffscreenCanvasPattern::SetLineDash(const std::vector<double>& segments)
220 {
221 offscreenPaintMethod_->SetLineDash(segments);
222 }
223
SetTextDirection(TextDirection direction)224 void OffscreenCanvasPattern::SetTextDirection(TextDirection direction)
225 {
226 currentSetTextDirection_ = direction;
227 if (direction == TextDirection::INHERIT) {
228 direction = AceApplicationInfo::GetInstance().IsRightToLeft() ? TextDirection::RTL : TextDirection::LTR;
229 }
230 offscreenPaintMethod_->SetTextDirection(direction);
231 }
232
SetFilterParam(const std::string & filterStr)233 void OffscreenCanvasPattern::SetFilterParam(const std::string& filterStr)
234 {
235 offscreenPaintMethod_->SetFilterParam(filterStr);
236 }
237
Save()238 void OffscreenCanvasPattern::Save()
239 {
240 offscreenPaintMethod_->Save();
241 }
242
Restore()243 void OffscreenCanvasPattern::Restore()
244 {
245 offscreenPaintMethod_->Restore();
246 }
247
Scale(double x,double y)248 void OffscreenCanvasPattern::Scale(double x, double y)
249 {
250 offscreenPaintMethod_->Scale(x, y);
251 }
252
Rotate(double angle)253 void OffscreenCanvasPattern::Rotate(double angle)
254 {
255 offscreenPaintMethod_->Rotate(angle);
256 }
257
SetTransform(const TransformParam & param)258 void OffscreenCanvasPattern::SetTransform(const TransformParam& param)
259 {
260 offscreenPaintMethod_->SetTransform(param);
261 }
262
ResetTransform()263 void OffscreenCanvasPattern::ResetTransform()
264 {
265 offscreenPaintMethod_->ResetTransform();
266 }
267
Transform(const TransformParam & param)268 void OffscreenCanvasPattern::Transform(const TransformParam& param)
269 {
270 offscreenPaintMethod_->Transform(param);
271 }
272
Translate(double x,double y)273 void OffscreenCanvasPattern::Translate(double x, double y)
274 {
275 offscreenPaintMethod_->Translate(x, y);
276 }
277
SetFillPattern(const std::weak_ptr<Ace::Pattern> & pattern)278 void OffscreenCanvasPattern::SetFillPattern(const std::weak_ptr<Ace::Pattern>& pattern)
279 {
280 offscreenPaintMethod_->SetFillPatternNG(pattern);
281 }
282
SetFillGradient(const Ace::Gradient & gradient)283 void OffscreenCanvasPattern::SetFillGradient(const Ace::Gradient& gradient)
284 {
285 offscreenPaintMethod_->SetFillGradient(gradient);
286 }
287
SetAlpha(double alpha)288 void OffscreenCanvasPattern::SetAlpha(double alpha)
289 {
290 offscreenPaintMethod_->SetAlpha(alpha);
291 }
292
SetCompositeType(CompositeOperation operation)293 void OffscreenCanvasPattern::SetCompositeType(CompositeOperation operation)
294 {
295 offscreenPaintMethod_->SetCompositeType(operation);
296 }
297
SetLineWidth(double width)298 void OffscreenCanvasPattern::SetLineWidth(double width)
299 {
300 offscreenPaintMethod_->SetLineWidth(width);
301 }
302
SetLineCap(LineCapStyle style)303 void OffscreenCanvasPattern::SetLineCap(LineCapStyle style)
304 {
305 offscreenPaintMethod_->SetLineCap(style);
306 }
307
SetLineJoin(LineJoinStyle style)308 void OffscreenCanvasPattern::SetLineJoin(LineJoinStyle style)
309 {
310 offscreenPaintMethod_->SetLineJoin(style);
311 }
312
SetMiterLimit(double limit)313 void OffscreenCanvasPattern::SetMiterLimit(double limit)
314 {
315 offscreenPaintMethod_->SetMiterLimit(limit);
316 }
317
SetTextAlign(TextAlign align)318 void OffscreenCanvasPattern::SetTextAlign(TextAlign align)
319 {
320 offscreenPaintMethod_->SetTextAlign(align);
321 }
322
SetTextBaseline(TextBaseline baseline)323 void OffscreenCanvasPattern::SetTextBaseline(TextBaseline baseline)
324 {
325 offscreenPaintMethod_->SetTextBaseline(baseline);
326 }
327
SetShadowBlur(double blur)328 void OffscreenCanvasPattern::SetShadowBlur(double blur)
329 {
330 offscreenPaintMethod_->SetShadowBlur(blur);
331 }
332
SetShadowOffsetX(double x)333 void OffscreenCanvasPattern::SetShadowOffsetX(double x)
334 {
335 offscreenPaintMethod_->SetShadowOffsetX(x);
336 }
337
SetShadowOffsetY(double y)338 void OffscreenCanvasPattern::SetShadowOffsetY(double y)
339 {
340 offscreenPaintMethod_->SetShadowOffsetY(y);
341 }
342
SetSmoothingEnabled(bool enabled)343 void OffscreenCanvasPattern::SetSmoothingEnabled(bool enabled)
344 {
345 offscreenPaintMethod_->SetSmoothingEnabled(enabled);
346 }
347
SetSmoothingQuality(const std::string & quality)348 void OffscreenCanvasPattern::SetSmoothingQuality(const std::string& quality)
349 {
350 offscreenPaintMethod_->SetSmoothingQuality(quality);
351 }
352
SetLineDashOffset(double offset)353 void OffscreenCanvasPattern::SetLineDashOffset(double offset)
354 {
355 offscreenPaintMethod_->SetLineDashOffset(offset);
356 }
357
SetShadowColor(const Color & color)358 void OffscreenCanvasPattern::SetShadowColor(const Color& color)
359 {
360 offscreenPaintMethod_->SetShadowColor(color);
361 }
362
SetStrokePattern(const std::weak_ptr<Ace::Pattern> & pattern)363 void OffscreenCanvasPattern::SetStrokePattern(const std::weak_ptr<Ace::Pattern>& pattern)
364 {
365 offscreenPaintMethod_->SetStrokePatternNG(pattern);
366 }
367
SetStrokeGradient(const Ace::Gradient & gradient)368 void OffscreenCanvasPattern::SetStrokeGradient(const Ace::Gradient& gradient)
369 {
370 offscreenPaintMethod_->SetStrokeGradient(gradient);
371 }
372
SetStrokeColor(const Color & color)373 void OffscreenCanvasPattern::SetStrokeColor(const Color& color)
374 {
375 offscreenPaintMethod_->SetStrokeColor(color);
376 }
377
SetFontWeight(FontWeight weight)378 void OffscreenCanvasPattern::SetFontWeight(FontWeight weight)
379 {
380 offscreenPaintMethod_->SetFontWeight(weight);
381 }
382
SetFontStyle(FontStyle style)383 void OffscreenCanvasPattern::SetFontStyle(FontStyle style)
384 {
385 offscreenPaintMethod_->SetFontStyle(style);
386 }
387
SetFontFamilies(const std::vector<std::string> & fontFamilies)388 void OffscreenCanvasPattern::SetFontFamilies(const std::vector<std::string>& fontFamilies)
389 {
390 offscreenPaintMethod_->SetFontFamilies(fontFamilies);
391 }
392
SetFontSize(const Dimension & size)393 void OffscreenCanvasPattern::SetFontSize(const Dimension& size)
394 {
395 offscreenPaintMethod_->SetFontSize(size);
396 }
397
SetLetterSpacing(const Dimension & letterSpacing)398 void OffscreenCanvasPattern::SetLetterSpacing(const Dimension& letterSpacing)
399 {
400 offscreenPaintMethod_->SetLetterSpacing(letterSpacing);
401 }
402
ToDataURL(const std::string & type,const double quality)403 std::string OffscreenCanvasPattern::ToDataURL(const std::string& type, const double quality)
404 {
405 return offscreenPaintMethod_->ToDataURL(type, quality);
406 }
407
GetTransform() const408 TransformParam OffscreenCanvasPattern::GetTransform() const
409 {
410 return offscreenPaintMethod_->GetTransform();
411 }
412
SetTransform(std::shared_ptr<Ace::Pattern> pattern,const TransformParam & transform)413 void OffscreenCanvasPattern::SetTransform(std::shared_ptr<Ace::Pattern> pattern, const TransformParam& transform)
414 {
415 offscreenPaintMethod_->SetTransform(pattern, transform);
416 }
417
SaveLayer()418 void OffscreenCanvasPattern::SaveLayer()
419 {
420 offscreenPaintMethod_->SaveLayer();
421 }
422
RestoreLayer()423 void OffscreenCanvasPattern::RestoreLayer()
424 {
425 offscreenPaintMethod_->RestoreLayer();
426 }
427
GetBitmapSize()428 size_t OffscreenCanvasPattern::GetBitmapSize()
429 {
430 CHECK_NULL_RETURN(offscreenPaintMethod_, 0);
431 return offscreenPaintMethod_->GetBitmapSize();
432 }
433
Reset()434 void OffscreenCanvasPattern::Reset()
435 {
436 offscreenPaintMethod_->Reset();
437 offscreenPaintMethod_->SetTextDirection(
438 AceApplicationInfo::GetInstance().IsRightToLeft() ? TextDirection::RTL : TextDirection::LTR);
439 }
440
UpdateTextDefaultDirection()441 void OffscreenCanvasPattern::UpdateTextDefaultDirection()
442 {
443 if (currentSetTextDirection_ != TextDirection::INHERIT) {
444 return;
445 }
446 offscreenPaintMethod_->SetTextDirection(
447 AceApplicationInfo::GetInstance().IsRightToLeft() ? TextDirection::RTL : TextDirection::LTR);
448 }
449
TransferToImageBitmap()450 RefPtr<PixelMap> OffscreenCanvasPattern::TransferToImageBitmap()
451 {
452 return offscreenPaintMethod_->TransferToImageBitmap();
453 }
454
SetDensity(double density)455 void OffscreenCanvasPattern::SetDensity(double density)
456 {
457 offscreenPaintMethod_->SetDensity(density);
458 }
459 } // namespace OHOS::Ace::NG
460