• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020-2021 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 "gfx_utils/diagram/common/paint.h"
17 namespace OHOS {
~Paint()18 Paint::~Paint()
19 {
20 #if defined(GRAPHIC_ENABLE_GRADIENT_FILL_FLAG) && GRAPHIC_ENABLE_GRADIENT_FILL_FLAG
21     stopAndColors_.Clear();
22 #endif
23 }
24 
InitDash(const Paint & paint)25 void Paint::InitDash(const Paint& paint)
26 {
27 #if defined(GRAPHIC_ENABLE_DASH_GENERATE_FLAG) && GRAPHIC_ENABLE_DASH_GENERATE_FLAG
28     if (isDashMode_ && ndashes_ > 0) {
29         dashArray_ = new float[ndashes_];
30         if (dashArray_) {
31             if (memset_s(dashArray_, ndashes_ * sizeof(float), 0, ndashes_ * sizeof(float)) != EOF) {
32             }
33             for (uint32_t i = 0; i < ndashes_; i++) {
34                 dashArray_[i] = paint.dashArray_[i];
35             }
36         } else {
37             ndashes_ = 0;
38             dashOffset_ = 0;
39             isDashMode_ = false;
40         }
41     } else {
42         dashArray_ = nullptr;
43     }
44 #endif
45 }
46 
47 /*
48  * Initialize data members.
49  * style_:       paint style.
50  * fillColor_:   Sets the fill color of the pen.
51  * strokeColor_: Sets the line color of the pen.
52  * opacity_:     Set transparency.
53  * strokeWidth_: Set lineweight.
54  * lineCap_:     Set pen cap.
55  * lineJoin_:    Sets the style at the path connection of the pen.
56  * miterLimit_:  Sets the spacing limit for sharp corners at path connections.
57  * dashOffset:   dash Point offset.
58  * isDrawDash:   Whether to draw dotted line.
59  * dashArray:    dash Point group.
60  * ndashes:      Number of dotted lines.
61  * globalAlpha:  Set element Global alpha.
62  * shadowBlurRadius:  Sets the shadow blur radius.
63  * shadowOffsetX:     Sets the abscissa offset of the shadow.
64  * shadowOffsetY:     Sets the shadow ordinate offset.
65  * shadowColor:       Set shadow color.
66  */
Init(const Paint & paint)67 void Paint::Init(const Paint& paint)
68 {
69     style_ = paint.style_;
70     fillColor_ = paint.fillColor_;
71     strokeColor_ = paint.strokeColor_;
72     strokeWidth_ = paint.strokeWidth_;
73     opacity_ = paint.opacity_;
74 #if defined(GRAPHIC_ENABLE_LINECAP_FLAG) && GRAPHIC_ENABLE_LINECAP_FLAG
75     lineCap_ = paint.lineCap_;
76 #endif
77 #if defined(GRAPHIC_ENABLE_LINEJOIN_FLAG) && GRAPHIC_ENABLE_LINEJOIN_FLAG
78     lineJoin_ = paint.lineJoin_;
79 #endif
80 #if defined(GRAPHIC_ENABLE_DASH_GENERATE_FLAG) && GRAPHIC_ENABLE_DASH_GENERATE_FLAG
81     isDashMode_ = paint.isDashMode_;
82     dashOffset_ = paint.dashOffset_;
83     dashArray_ = paint.dashArray_;
84     ndashes_ = paint.ndashes_;
85 #endif
86     changeFlag_ = paint.changeFlag_;
87     scaleRadioX_ = paint.scaleRadioX_;
88     scaleRadioY_ = paint.scaleRadioY_;
89     translationX_ = paint.translationX_;
90     translationY_ = paint.translationY_;
91     InitDash(paint);
92 #if defined(GRAPHIC_ENABLE_LINEJOIN_FLAG) && GRAPHIC_ENABLE_LINEJOIN_FLAG
93     miterLimit_ = paint.miterLimit_;
94 #endif
95 #if defined(GRAPHIC_ENABLE_GRADIENT_FILL_FLAG) && GRAPHIC_ENABLE_GRADIENT_FILL_FLAG
96     linearGradientPoint_ = paint.linearGradientPoint_;
97     radialGradientPoint_ = paint.radialGradientPoint_;
98     CopyStopAndColors(paint);
99     gradientflag_ = paint.gradientflag_;
100 #endif
101 #if defined(GRAPHIC_ENABLE_PATTERN_FILL_FLAG) && GRAPHIC_ENABLE_PATTERN_FILL_FLAG
102     patternRepeat_ = paint.patternRepeat_;
103 #endif
104 #if defined(GRAPHIC_ENABLE_SHADOW_EFFECT_FLAG) && GRAPHIC_ENABLE_SHADOW_EFFECT_FLAG
105     shadowBlurRadius_ = paint.shadowBlurRadius_;
106     shadowOffsetX_ = paint.shadowOffsetX_;
107     shadowOffsetY_ = paint.shadowOffsetY_;
108     shadowColor_ = paint.shadowColor_;
109     haveShadow_ = paint.haveShadow_;
110 #endif
111     globalAlpha_ = paint.globalAlpha_;
112     globalCompositeOperation_ = paint.globalCompositeOperation_;
113     rotateAngle_ = paint.rotateAngle_;
114     transfrom_ = paint.transfrom_;
115     haveComposite_ = paint.haveComposite_;
116 }
117 
SetStrokeStyle(ColorType color)118 void Paint::SetStrokeStyle(ColorType color)
119 {
120     SetStyle(Paint::STROKE_STYLE);
121     SetStrokeColor(color);
122 }
123 
SetFillStyle(ColorType color)124 void Paint::SetFillStyle(ColorType color)
125 {
126     SetStyle(Paint::FILL_STYLE);
127     SetFillColor(color);
128 }
129 
SetStrokeColor(ColorType color)130 void Paint::SetStrokeColor(ColorType color)
131 {
132     strokeColor_ = color;
133     changeFlag_ = true;
134 }
135 
SetFillColor(ColorType color)136 void Paint::SetFillColor(ColorType color)
137 {
138     fillColor_ = color;
139     changeFlag_ = true;
140 }
141 
142 #if defined(GRAPHIC_ENABLE_LINECAP_FLAG) && GRAPHIC_ENABLE_LINECAP_FLAG
SetLineCap(LineCap lineCap)143 void Paint::SetLineCap(LineCap lineCap)
144 {
145     lineCap_ = lineCap;
146     changeFlag_ = true;
147 }
148 #endif
149 
150 #if defined(GRAPHIC_ENABLE_LINEJOIN_FLAG) && GRAPHIC_ENABLE_LINEJOIN_FLAG
SetLineJoin(LineJoin lineJoin)151 void Paint::SetLineJoin(LineJoin lineJoin)
152 {
153     lineJoin_ = lineJoin;
154     changeFlag_ = true;
155 }
156 #endif
157 
158 #if defined(GRAPHIC_ENABLE_LINEJOIN_FLAG) && GRAPHIC_ENABLE_LINEJOIN_FLAG
SetMiterLimit(float miterLimit)159 void Paint::SetMiterLimit(float miterLimit)
160 {
161     miterLimit_ = miterLimit;
162     changeFlag_ = true;
163 }
164 #endif
165 
166 #if defined(GRAPHIC_ENABLE_DASH_GENERATE_FLAG) && GRAPHIC_ENABLE_DASH_GENERATE_FLAG
SetLineDash(float * lineDashs,const uint32_t ndash)167 void Paint::SetLineDash(float* lineDashs, const uint32_t ndash)
168 {
169     ClearLineDash();
170     if (lineDashs == nullptr || ndash == 0) {
171         return;
172     }
173     ndashes_ = ndash;
174     isDashMode_ = true;
175     dashArray_ = new float[ndashes_];
176     if (dashArray_) {
177         if (memset_s(dashArray_, ndashes_ * sizeof(float), 0, ndashes_ * sizeof(float)) != EOF) {
178         }
179         for (uint32_t iIndex = 0; iIndex < ndashes_; iIndex++) {
180             dashArray_[iIndex] = lineDashs[iIndex];
181         }
182     } else {
183         ndashes_ = 0;
184         dashOffset_ = 0;
185         isDashMode_ = false;
186     }
187     changeFlag_ = true;
188 }
189 
SetLineDashOffset(float offset)190 void Paint::SetLineDashOffset(float offset)
191 {
192     dashOffset_ = offset;
193     changeFlag_ = true;
194     isDashMode_ = true;
195 }
196 
ClearLineDash(void)197 void Paint::ClearLineDash(void)
198 {
199     dashOffset_ = 0;
200     ndashes_ = 0;
201     isDashMode_ = false;
202     if (dashArray_ != nullptr) {
203         delete[] dashArray_;
204         dashArray_ = nullptr;
205     }
206 }
207 #endif
208 
209 #if defined(GRAPHIC_ENABLE_GRADIENT_FILL_FLAG) && GRAPHIC_ENABLE_GRADIENT_FILL_FLAG
createLinearGradient(float startx,float starty,float endx,float endy)210 void Paint::createLinearGradient(float startx, float starty, float endx, float endy)
211 {
212     gradientflag_ = Linear;
213     linearGradientPoint_.x0 = startx;
214     linearGradientPoint_.y0 = starty;
215     linearGradientPoint_.x1 = endx;
216     linearGradientPoint_.y1 = endy;
217     changeFlag_ = true;
218 }
219 
addColorStop(float stop,ColorType color)220 void Paint::addColorStop(float stop, ColorType color)
221 {
222     StopAndColor stopAndColor;
223     stopAndColor.stop = stop;
224     stopAndColor.color = color;
225     stopAndColors_.PushBack(stopAndColor);
226 }
227 
createRadialGradient(float start_x,float start_y,float start_r,float end_x,float end_y,float end_r)228 void Paint::createRadialGradient(float start_x, float start_y, float start_r, float end_x, float end_y, float end_r)
229 {
230     gradientflag_ = Radial;
231     radialGradientPoint_.x0 = start_x;
232     radialGradientPoint_.y0 = start_y;
233     radialGradientPoint_.r0 = start_r;
234     radialGradientPoint_.x1 = end_x;
235     radialGradientPoint_.y1 = end_y;
236     radialGradientPoint_.r1 = end_r;
237     changeFlag_ = true;
238 }
239 #endif
240 
241 #if defined(GRAPHIC_ENABLE_PATTERN_FILL_FLAG) && GRAPHIC_ENABLE_PATTERN_FILL_FLAG
CreatePattern(const char * img,PatternRepeatMode patternRepeat)242 void Paint::CreatePattern(const char* img, PatternRepeatMode patternRepeat)
243 {
244     image_ = img;
245     patternRepeat_ = patternRepeat;
246     changeFlag_ = true;
247 }
248 #endif
249 
250 #if defined(GRAPHIC_ENABLE_SHADOW_EFFECT_FLAG) && GRAPHIC_ENABLE_SHADOW_EFFECT_FLAG
SetShadowBlur(uint16_t radius)251 void Paint::SetShadowBlur(uint16_t radius)
252 {
253     shadowBlurRadius_ = radius;
254     changeFlag_ = true;
255 }
256 
SetShadowOffsetX(float offset)257 void Paint::SetShadowOffsetX(float offset)
258 {
259     shadowOffsetX_ = offset;
260     changeFlag_ = true;
261 }
262 
SetShadowOffsetY(float offset)263 void Paint::SetShadowOffsetY(float offset)
264 {
265     shadowOffsetY_ = offset;
266     changeFlag_ = true;
267 }
268 
SetShadowColor(ColorType color)269 void Paint::SetShadowColor(ColorType color)
270 {
271     shadowColor_ = color;
272     changeFlag_ = true;
273     haveShadow_ = true;
274 }
275 #endif
276 
SetGlobalAlpha(float alphaPercentage)277 void Paint::SetGlobalAlpha(float alphaPercentage)
278 {
279     if (alphaPercentage > 1) {
280         globalAlpha_ = 1.0;
281         return;
282     }
283     if (alphaPercentage < 0) {
284         globalAlpha_ = 0.0;
285         return;
286     }
287     globalAlpha_ = alphaPercentage;
288     changeFlag_ = true;
289 }
290 
SetGlobalCompositeOperation(GlobalCompositeOperation globalCompositeOperation)291 void Paint::SetGlobalCompositeOperation(GlobalCompositeOperation globalCompositeOperation)
292 {
293     globalCompositeOperation_ = globalCompositeOperation;
294     changeFlag_ = true;
295     if (globalCompositeOperation != SOURCE_OVER) {
296         haveComposite_ = true;
297     }
298 }
299 
Scale(float scaleX,float scaleY)300 void Paint::Scale(float scaleX, float scaleY)
301 {
302     this->scaleRadioX_ *= scaleX;
303     this->scaleRadioY_ *= scaleX;
304     if (rotateAngle_ > 0.0f || rotateAngle_ < 0) {
305         transfrom_.Rotate(-rotateAngle_ * PI / BOXER);
306         transfrom_.Scale(scaleX, scaleY);
307         transfrom_.Rotate(rotateAngle_ * PI / BOXER);
308     } else {
309         transfrom_.Scale(scaleX, scaleY);
310     }
311     changeFlag_ = true;
312 }
313 
Rotate(float angle)314 void Paint::Rotate(float angle)
315 {
316     changeFlag_ = true;
317     transfrom_.Rotate(angle * PI / BOXER);
318     rotateAngle_ += angle;
319 }
320 
Rotate(float angle,int16_t x,int16_t y)321 void Paint::Rotate(float angle, int16_t x, int16_t y)
322 {
323     transfrom_.Translate(-x, -y);
324     transfrom_.Rotate(angle * PI / BOXER);
325     rotateAngle_ += angle;
326     transfrom_.Translate(x, y);
327     changeFlag_ = true;
328 }
329 
Translate(int16_t x,int16_t y)330 void Paint::Translate(int16_t x, int16_t y)
331 {
332     changeFlag_ = true;
333     transfrom_.Translate(x, y);
334     this->translationX_ += x;
335     this->translationY_ += y;
336 }
337 
SetTransform(float scaleX,float shearX,float shearY,float scaleY,int16_t transLateX,int16_t transLateY)338 void Paint::SetTransform(float scaleX, float shearX, float shearY,
339                          float scaleY, int16_t transLateX, int16_t transLateY)
340 {
341     transfrom_.Reset();
342     rotateAngle_ = 0;
343     Transform(scaleX, shearX, shearY, scaleY, transLateX, transLateY);
344     changeFlag_ = true;
345 }
346 
Transform(float scaleX,float shearX,float shearY,float scaleY,int16_t transLateX,int16_t transLateY)347 void Paint::Transform(float scaleX, float shearX, float shearY, float scaleY, int16_t transLateX, int16_t transLateY)
348 {
349     changeFlag_ = true;
350     this->translationX_ += transLateX;
351     this->translationY_ += transLateY;
352     transLateX += transfrom_.GetData()[2];
353     transLateY += transfrom_.GetData()[5];
354     transfrom_.Translate(-transfrom_.GetData()[2], -transfrom_.GetData()[5]);
355     Scale(scaleX, scaleY);
356     transfrom_.Translate(transLateX, transLateY);
357     transfrom_.SetData(1, transfrom_.GetData()[1] + shearX);
358     transfrom_.SetData(3, transfrom_.GetData()[3] + shearY);
359 }
360 
361 #if defined(GRAPHIC_ENABLE_GRADIENT_FILL_FLAG) && GRAPHIC_ENABLE_GRADIENT_FILL_FLAG
CopyStopAndColors(const Paint & paint)362 void Paint::CopyStopAndColors(const Paint& paint)
363 {
364     stopAndColors_.Clear();
365     if (!paint.stopAndColors_.IsEmpty()) {
366         ListNode<StopAndColor>* node = paint.stopAndColors_.Head();
367         const ListNode<StopAndColor>* head = paint.stopAndColors_.End();
368         do {
369             stopAndColors_.PushBack(node->data_);
370             node = paint.stopAndColors_.Next(node);
371         } while (node != head);
372     }
373 }
374 #endif
375 } // namespace OHOS