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     RSPoint pointL(x0, y0);
27     RSPoint pointR(x1, y1);
28     if (paint.GetStyle() == TexginePaint::Style::FILL) {
29         canvas_->AttachBrush(paint.GetBrush());
30         canvas_->DrawLine(pointL, pointR);
31         canvas_->DetachBrush();
32     } else if (paint.GetStyle() == TexginePaint::Style::STROKE) {
33         canvas_->AttachPen(paint.GetPen());
34         canvas_->DrawLine(pointL, pointR);
35         canvas_->DetachPen();
36     } else {
37         canvas_->AttachBrush(paint.GetBrush());
38         canvas_->AttachPen(paint.GetPen());
39         canvas_->DrawLine(pointL, pointR);
40         canvas_->DetachBrush();
41         canvas_->DetachPen();
42     }
43 }
44 
DrawRect(const TexgineRect & rect,const TexginePaint & paint) const45 void TexgineCanvas::DrawRect(const TexgineRect &rect, const TexginePaint &paint) const
46 {
47     if (canvas_ == nullptr || rect.GetRect() == nullptr) {
48         return;
49     }
50     if (paint.GetStyle() == TexginePaint::Style::FILL) {
51         canvas_->AttachBrush(paint.GetBrush());
52         canvas_->DrawRect(*rect.GetRect());
53         canvas_->DetachBrush();
54     } else if (paint.GetStyle() == TexginePaint::Style::STROKE) {
55         canvas_->AttachPen(paint.GetPen());
56         canvas_->DrawRect(*rect.GetRect());
57         canvas_->DetachPen();
58     } else {
59         canvas_->AttachBrush(paint.GetBrush());
60         canvas_->AttachPen(paint.GetPen());
61         canvas_->DrawRect(*rect.GetRect());
62         canvas_->DetachBrush();
63         canvas_->DetachPen();
64     }
65 }
66 
DrawRRect(const TexgineRect & rect,const TexginePaint & paint) const67 void TexgineCanvas::DrawRRect(const TexgineRect &rect, const TexginePaint &paint) const
68 {
69     if (canvas_ == nullptr || rect.GetRRect() == nullptr) {
70         return;
71     }
72     if (paint.GetStyle() == TexginePaint::Style::FILL) {
73         canvas_->AttachBrush(paint.GetBrush());
74         canvas_->DrawRoundRect(*rect.GetRRect());
75         canvas_->DetachBrush();
76     } else if (paint.GetStyle() == TexginePaint::Style::STROKE) {
77         canvas_->AttachPen(paint.GetPen());
78         canvas_->DrawRoundRect(*rect.GetRRect());
79         canvas_->DetachPen();
80     } else {
81         canvas_->AttachBrush(paint.GetBrush());
82         canvas_->AttachPen(paint.GetPen());
83         canvas_->DrawRoundRect(*rect.GetRRect());
84         canvas_->DetachBrush();
85         canvas_->DetachPen();
86     }
87 }
88 
DrawTextBlob(const std::shared_ptr<TexgineTextBlob> & blob,float x,float y,const TexginePaint & paint)89 void TexgineCanvas::DrawTextBlob(
90     const std::shared_ptr<TexgineTextBlob> &blob, float x, float y, const TexginePaint &paint)
91 {
92     if (canvas_ == nullptr || blob == nullptr) {
93         return;
94     }
95     if (paint.GetStyle() == TexginePaint::Style::FILL) {
96         canvas_->AttachBrush(paint.GetBrush());
97         canvas_->DrawTextBlob(blob->GetTextBlob().get(), x, y);
98         canvas_->DetachBrush();
99     } else if (paint.GetStyle() == TexginePaint::Style::STROKE) {
100         canvas_->AttachPen(paint.GetPen());
101         canvas_->DrawTextBlob(blob->GetTextBlob().get(), x, y);
102         canvas_->DetachPen();
103     } else {
104         canvas_->AttachBrush(paint.GetBrush());
105         canvas_->AttachPen(paint.GetPen());
106         canvas_->DrawTextBlob(blob->GetTextBlob().get(), x, y);
107         canvas_->DetachBrush();
108         canvas_->DetachPen();
109     }
110 }
111 
DrawSymbol(const RSHMSymbolData & symbol,RSPoint locate,const TexginePaint & paint)112 void TexgineCanvas::DrawSymbol(const RSHMSymbolData &symbol, RSPoint locate, const TexginePaint &paint)
113 {
114     if (canvas_ == nullptr) {
115         return;
116     }
117     if (paint.GetStyle() == TexginePaint::Style::FILL) {
118         canvas_->AttachBrush(paint.GetBrush());
119         canvas_->DrawSymbol(symbol, locate);
120         canvas_->DetachBrush();
121     } else if (paint.GetStyle() == TexginePaint::Style::STROKE) {
122         canvas_->AttachPen(paint.GetPen());
123         canvas_->DrawSymbol(symbol, locate);
124         canvas_->DetachPen();
125     } else {
126         canvas_->AttachBrush(paint.GetBrush());
127         canvas_->AttachPen(paint.GetPen());
128         canvas_->DrawSymbol(symbol, locate);
129         canvas_->DetachBrush();
130         canvas_->DetachPen();
131     }
132 }
133 
DrawPath(const RSPath & path,const TexginePaint & paint)134 void TexgineCanvas::DrawPath(const RSPath &path, const TexginePaint &paint)
135 {
136     if (canvas_ == nullptr) {
137         return;
138     }
139     if (paint.GetStyle() == TexginePaint::Style::FILL) {
140         canvas_->AttachBrush(paint.GetBrush());
141         canvas_->DrawPath(path);
142         canvas_->DetachBrush();
143     } else if (paint.GetStyle() == TexginePaint::Style::STROKE) {
144         canvas_->AttachPen(paint.GetPen());
145         canvas_->DrawPath(path);
146         canvas_->DetachPen();
147     } else {
148         canvas_->AttachBrush(paint.GetBrush());
149         canvas_->AttachPen(paint.GetPen());
150         canvas_->DrawPath(path);
151         canvas_->DetachBrush();
152         canvas_->DetachPen();
153     }
154 }
155 
Clear(uint32_t color) const156 void TexgineCanvas::Clear(uint32_t color) const
157 {
158     if (canvas_ == nullptr) {
159         return;
160     }
161     canvas_->Clear(color);
162 }
163 
Save() const164 int TexgineCanvas::Save() const
165 {
166     if (canvas_ == nullptr) {
167         // 1 is failed
168         return 1;
169     }
170     return canvas_->Save();
171 }
172 
Translate(float dx,float dy) const173 void TexgineCanvas::Translate(float dx, float dy) const
174 {
175     if (canvas_ == nullptr) {
176         return;
177     }
178     canvas_->Translate(dx, dy);
179 }
180 
GetCanvas() const181 RSCanvas *TexgineCanvas::GetCanvas() const
182 {
183     return canvas_;
184 }
185 
Restore() const186 void TexgineCanvas::Restore() const
187 {
188     if (canvas_ == nullptr) {
189         return;
190     }
191     canvas_->Restore();
192 }
193 
SetCanvas(RSCanvas * canvas)194 void TexgineCanvas::SetCanvas(RSCanvas *canvas)
195 {
196     canvas_ = canvas;
197 }
198 } // namespace TextEngine
199 } // namespace Rosen
200 } // namespace OHOS
201