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 "texgine_canvas.h"
17
18 namespace OHOS {
19 namespace Rosen {
20 namespace TextEngine {
DrawLine(double x0,double y0,double x1,double y1,const TexginePaint & paint)21 void TexgineCanvas::DrawLine(double x0, double y0, double x1, double y1, const TexginePaint &paint)
22 {
23 if (canvas_ == nullptr) {
24 return;
25 }
26 #ifndef USE_ROSEN_DRAWING
27 canvas_->drawLine(x0, y0, x1, y1, paint.GetPaint());
28 #else
29 RSPoint pointL(x0, y0);
30 RSPoint pointR(x1, y1);
31 if (paint.GetStyle() == TexginePaint::Style::FILL) {
32 canvas_->AttachBrush(paint.GetBrush());
33 canvas_->DrawLine(pointL, pointR);
34 canvas_->DetachBrush();
35 } else if (paint.GetStyle() == TexginePaint::Style::STROKE) {
36 canvas_->AttachPen(paint.GetPen());
37 canvas_->DrawLine(pointL, pointR);
38 canvas_->DetachPen();
39 } else {
40 canvas_->AttachBrush(paint.GetBrush());
41 canvas_->AttachPen(paint.GetPen());
42 canvas_->DrawLine(pointL, pointR);
43 canvas_->DetachBrush();
44 canvas_->DetachPen();
45 }
46 #endif
47 }
48
DrawRect(const TexgineRect & rect,const TexginePaint & paint) const49 void TexgineCanvas::DrawRect(const TexgineRect &rect, const TexginePaint &paint) const
50 {
51 if (canvas_ == nullptr || rect.GetRect() == nullptr) {
52 return;
53 }
54 #ifndef USE_ROSEN_DRAWING
55 canvas_->drawRect(*rect.GetRect(), paint.GetPaint());
56 #else
57 if (paint.GetStyle() == TexginePaint::Style::FILL) {
58 canvas_->AttachBrush(paint.GetBrush());
59 canvas_->DrawRect(*rect.GetRect());
60 canvas_->DetachBrush();
61 } else if (paint.GetStyle() == TexginePaint::Style::STROKE) {
62 canvas_->AttachPen(paint.GetPen());
63 canvas_->DrawRect(*rect.GetRect());
64 canvas_->DetachPen();
65 } else {
66 canvas_->AttachBrush(paint.GetBrush());
67 canvas_->AttachPen(paint.GetPen());
68 canvas_->DrawRect(*rect.GetRect());
69 canvas_->DetachBrush();
70 canvas_->DetachPen();
71 }
72 #endif
73 }
74
DrawRRect(const TexgineRect & rect,const TexginePaint & paint) const75 void TexgineCanvas::DrawRRect(const TexgineRect &rect, const TexginePaint &paint) const
76 {
77 if (canvas_ == nullptr || rect.GetRRect() == nullptr) {
78 return;
79 }
80 #ifndef USE_ROSEN_DRAWING
81 canvas_->drawRRect(*rect.GetRRect(), paint.GetPaint());
82 #else
83 if (paint.GetStyle() == TexginePaint::Style::FILL) {
84 canvas_->AttachBrush(paint.GetBrush());
85 canvas_->DrawRoundRect(*rect.GetRRect());
86 canvas_->DetachBrush();
87 } else if (paint.GetStyle() == TexginePaint::Style::STROKE) {
88 canvas_->AttachPen(paint.GetPen());
89 canvas_->DrawRoundRect(*rect.GetRRect());
90 canvas_->DetachPen();
91 } else {
92 canvas_->AttachBrush(paint.GetBrush());
93 canvas_->AttachPen(paint.GetPen());
94 canvas_->DrawRoundRect(*rect.GetRRect());
95 canvas_->DetachBrush();
96 canvas_->DetachPen();
97 }
98 #endif
99 }
100
DrawTextBlob(const std::shared_ptr<TexgineTextBlob> & blob,float x,float y,const TexginePaint & paint)101 void TexgineCanvas::DrawTextBlob(
102 const std::shared_ptr<TexgineTextBlob> &blob, float x, float y, const TexginePaint &paint)
103 {
104 if (canvas_ == nullptr || blob == nullptr) {
105 return;
106 }
107 #ifndef USE_ROSEN_DRAWING
108 canvas_->drawTextBlob(blob->GetTextBlob(), x, y, paint.GetPaint());
109 #else
110 if (paint.GetStyle() == TexginePaint::Style::FILL) {
111 canvas_->AttachBrush(paint.GetBrush());
112 canvas_->DrawTextBlob(blob->GetTextBlob().get(), x, y);
113 canvas_->DetachBrush();
114 } else if (paint.GetStyle() == TexginePaint::Style::STROKE) {
115 canvas_->AttachPen(paint.GetPen());
116 canvas_->DrawTextBlob(blob->GetTextBlob().get(), x, y);
117 canvas_->DetachPen();
118 } else {
119 canvas_->AttachBrush(paint.GetBrush());
120 canvas_->AttachPen(paint.GetPen());
121 canvas_->DrawTextBlob(blob->GetTextBlob().get(), x, y);
122 canvas_->DetachBrush();
123 canvas_->DetachPen();
124 }
125 #endif
126 }
127
128 #ifndef USE_ROSEN_DRAWING
DrawSymbol(const HMSymbolData & symbol,SkPoint locate,const TexginePaint & paint)129 void TexgineCanvas::DrawSymbol(const HMSymbolData &symbol, SkPoint locate, const TexginePaint &paint)
130 #else
131 void TexgineCanvas::DrawSymbol(const RSHMSymbolData &symbol, RSPoint locate, const TexginePaint &paint)
132 #endif
133 {
134 if (canvas_ == nullptr) {
135 return;
136 }
137 #ifndef USE_ROSEN_DRAWING
138 canvas_->drawSymbol(symbol, locate, paint.GetPaint());
139 #else
140 if (paint.GetStyle() == TexginePaint::Style::FILL) {
141 canvas_->AttachBrush(paint.GetBrush());
142 canvas_->DrawSymbol(symbol, locate);
143 canvas_->DetachBrush();
144 } else if (paint.GetStyle() == TexginePaint::Style::STROKE) {
145 canvas_->AttachPen(paint.GetPen());
146 canvas_->DrawSymbol(symbol, locate);
147 canvas_->DetachPen();
148 } else {
149 canvas_->AttachBrush(paint.GetBrush());
150 canvas_->AttachPen(paint.GetPen());
151 canvas_->DrawSymbol(symbol, locate);
152 canvas_->DetachBrush();
153 canvas_->DetachPen();
154 }
155 #endif
156 }
157
158 #ifndef USE_ROSEN_DRAWING
DrawPath(const SkPath & path,const TexginePaint & paint)159 void TexgineCanvas::DrawPath(const SkPath &path, const TexginePaint &paint)
160 #else
161 void TexgineCanvas::DrawPath(const RSPath &path, const TexginePaint &paint)
162 #endif
163 {
164 if (canvas_ == nullptr) {
165 return;
166 }
167 #ifndef USE_ROSEN_DRAWING
168 canvas_->drawPath(path, paint.GetPaint());
169 #else
170 if (paint.GetStyle() == TexginePaint::Style::FILL) {
171 canvas_->AttachBrush(paint.GetBrush());
172 canvas_->DrawPath(path);
173 canvas_->DetachBrush();
174 } else if (paint.GetStyle() == TexginePaint::Style::STROKE) {
175 canvas_->AttachPen(paint.GetPen());
176 canvas_->DrawPath(path);
177 canvas_->DetachPen();
178 } else {
179 canvas_->AttachBrush(paint.GetBrush());
180 canvas_->AttachPen(paint.GetPen());
181 canvas_->DrawPath(path);
182 canvas_->DetachBrush();
183 canvas_->DetachPen();
184 }
185 #endif
186 }
187
Clear(uint32_t color) const188 void TexgineCanvas::Clear(uint32_t color) const
189 {
190 if (canvas_ == nullptr) {
191 return;
192 }
193 #ifndef USE_ROSEN_DRAWING
194 canvas_->clear(color);
195 #else
196 canvas_->Clear(color);
197 #endif
198 }
199
Save() const200 int TexgineCanvas::Save() const
201 {
202 if (canvas_ == nullptr) {
203 // 1 is failed
204 return 1;
205 }
206 #ifndef USE_ROSEN_DRAWING
207 return canvas_->save();
208 #else
209 return canvas_->Save();
210 #endif
211 }
212
Translate(float dx,float dy) const213 void TexgineCanvas::Translate(float dx, float dy) const
214 {
215 if (canvas_ == nullptr) {
216 return;
217 }
218 #ifndef USE_ROSEN_DRAWING
219 canvas_->translate(dx, dy);
220 #else
221 canvas_->Translate(dx, dy);
222 #endif
223 }
224
225 #ifndef USE_ROSEN_DRAWING
GetCanvas() const226 SkCanvas *TexgineCanvas::GetCanvas() const
227 #else
228 RSCanvas *TexgineCanvas::GetCanvas() const
229 #endif
230 {
231 return canvas_;
232 }
233
Restore() const234 void TexgineCanvas::Restore() const
235 {
236 if (canvas_ == nullptr) {
237 return;
238 }
239 #ifndef USE_ROSEN_DRAWING
240 canvas_->restore();
241 #else
242 canvas_->Restore();
243 #endif
244 }
245
246 #ifndef USE_ROSEN_DRAWING
SetCanvas(SkCanvas * canvas)247 void TexgineCanvas::SetCanvas(SkCanvas *canvas)
248 #else
249 void TexgineCanvas::SetCanvas(RSCanvas *canvas)
250 #endif
251 {
252 canvas_ = canvas;
253 }
254 } // namespace TextEngine
255 } // namespace Rosen
256 } // namespace OHOS
257