1 /* 2 * Copyright (c) 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_PROPERTIES_PAINT_STATE_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_PROPERTIES_PAINT_STATE_H 18 19 #include "base/memory/ace_type.h" 20 #include "core/components/common/layout/constants.h" 21 #include "core/components/common/properties/color.h" 22 #include "core/components/common/properties/decoration.h" 23 #include "core/components/common/properties/text_style.h" 24 25 namespace OHOS::Ace { 26 27 struct BezierCurveParam { 28 double cp1x = 0.0; // first bezier point x 29 double cp1y = 0.0; // first bezier point y 30 double cp2x = 0.0; // second bezier point x 31 double cp2y = 0.0; // second bezier point y 32 double x = 0.0; // end point x 33 double y = 0.0; // end point y 34 }; 35 36 struct QuadraticCurveParam { 37 double cpx = 0.0; // bezier point x 38 double cpy = 0.0; // bezier point y 39 double x = 0.0; // end point x 40 double y = 0.0; // end point y 41 }; 42 43 struct ArcParam { 44 double x = 0.0; // point x of the circle 45 double y = 0.0; // point y of the circle 46 double radius = 0.0; // radius of the circle 47 double startAngle = 0.0; // start angle of the circle 48 double endAngle = 0.0; // end angle of the circle 49 bool anticlockwise = false; // is draw clock wise or not 50 }; 51 52 struct ArcToParam { 53 double x1 = 0.0; // start point x 54 double y1 = 0.0; // start point y 55 double x2 = 0.0; // end point x 56 double y2 = 0.0; // end point y 57 double radius = 0.0; // radius of the circle 58 }; 59 60 struct EllipseParam { 61 double x = 0.0; // point x of the ellipse 62 double y = 0.0; // point y of the ellipse 63 double radiusX = 0.0; // x axis radius of the ellipse 64 double radiusY = 0.0; // y axis radius of the ellipse 65 double rotation = 0.0; // rotation angle of the ellipse 66 double startAngle = 0.0; // start angle of the ellipse 67 double endAngle = 0.0; // end angle of the ellipse 68 bool anticlockwise = false; // is draw clock wise or not 69 }; 70 71 struct TransformParam { 72 double scaleX = 0.0; 73 double skewX = 0.0; 74 double skewY = 0.0; 75 double scaleY = 0.0; 76 double translateX = 0.0; 77 double translateY = 0.0; 78 }; 79 80 struct LineDashParam { 81 std::vector<double> lineDash; 82 double dashOffset = 0.0; 83 }; 84 85 struct CanvasImage { 86 int32_t flag = 0; 87 double sx = 0.0; 88 double sy = 0.0; 89 double sWidth = 0.0; 90 double sHeight = 0.0; 91 double dx = 0.0; 92 double dy = 0.0; 93 double dWidth = 0.0; 94 double dHeight = 0.0; 95 std::string src; 96 }; 97 98 struct ImageData { 99 int32_t x = 0; 100 int32_t y = 0; 101 int32_t dirtyX = 0; 102 int32_t dirtyY = 0; 103 int32_t dirtyWidth = 0; 104 int32_t dirtyHeight = 0; 105 std::vector<Color> data; 106 }; 107 108 struct TextMetrics { 109 double width; 110 double height; 111 double actualBoundingBoxLeft; 112 double actualBoundingBoxRight; 113 double actualBoundingBoxAscent; 114 double actualBoundingBoxDescent; 115 }; 116 117 enum class ContextType { 118 RENDER_2D, 119 RENDER_3D, 120 }; 121 122 // following the definition of FillType in skPath 123 enum class CanvasFillRule { 124 NONZERO = 0, 125 EVENODD, 126 }; 127 128 // following the definition in skPaint 129 enum class LineCapStyle { 130 BUTT = 0, 131 ROUND, 132 SQUARE, 133 }; 134 135 enum class LineJoinStyle { 136 MITER = 0, 137 ROUND, 138 BEVEL, 139 }; 140 141 enum class CompositeOperation { 142 SOURCE_OVER = 0, 143 SOURCE_ATOP, 144 SOURCE_IN, 145 SOURCE_OUT, 146 DESTINATION_OVER, 147 DESTINATION_ATOP, 148 DESTINATION_IN, 149 DESTINATION_OUT, 150 LIGHTER, 151 COPY, 152 XOR, 153 }; 154 155 class PaintState { 156 public: GetColor()157 const Color& GetColor() const 158 { 159 return color_; 160 } 161 SetColor(const Color & color)162 void SetColor(const Color& color) 163 { 164 color_ = color; 165 } 166 GetGradient()167 const Gradient& GetGradient() const 168 { 169 return gradient_; 170 } 171 SetGradient(const Gradient & gradient)172 void SetGradient(const Gradient& gradient) 173 { 174 gradient_ = gradient; 175 } 176 GetTextStyle()177 const TextStyle& GetTextStyle() const 178 { 179 return textStyle_; 180 } 181 SetTextStyle(const TextStyle & textStyle)182 void SetTextStyle(const TextStyle& textStyle) 183 { 184 textStyle_ = textStyle; 185 } 186 GetTextAlign()187 TextAlign GetTextAlign() const 188 { 189 return textAlign_; 190 } 191 SetTextAlign(TextAlign textAlign)192 void SetTextAlign(TextAlign textAlign) 193 { 194 textAlign_ = textAlign; 195 } 196 GetOffTextDirection()197 TextDirection GetOffTextDirection() const 198 { 199 return textDirection_; 200 } 201 SetOffTextDirection(TextDirection textDirection)202 void SetOffTextDirection(TextDirection textDirection) 203 { 204 textDirection_ = textDirection; 205 } 206 SetTextColor(const Color & color)207 void SetTextColor(const Color& color) 208 { 209 textStyle_.SetTextColor(color); 210 } 211 SetFontSize(const Dimension & size)212 void SetFontSize(const Dimension& size) 213 { 214 textStyle_.SetFontSize(size); 215 } 216 SetFontStyle(FontStyle style)217 void SetFontStyle(FontStyle style) 218 { 219 textStyle_.SetFontStyle(style); 220 } 221 SetFontWeight(FontWeight weight)222 void SetFontWeight(FontWeight weight) 223 { 224 textStyle_.SetFontWeight(weight); 225 } 226 SetFontFamilies(const std::vector<std::string> & fontFamilies)227 void SetFontFamilies(const std::vector<std::string>& fontFamilies) 228 { 229 textStyle_.SetFontFamilies(fontFamilies); 230 } 231 SetTextBaseline(TextBaseline baseline)232 void SetTextBaseline(TextBaseline baseline) 233 { 234 textStyle_.SetTextBaseline(baseline); 235 } 236 GetPattern()237 const Pattern& GetPattern() const 238 { 239 return pattern_; 240 } 241 SetPattern(const Pattern & pattern)242 void SetPattern(const Pattern& pattern) 243 { 244 pattern_ = pattern; 245 } 246 GetId()247 int32_t GetId() const 248 { 249 return id_; 250 } 251 SetId(int32_t id)252 void SetId(int32_t id) 253 { 254 id_ = id; 255 } 256 257 protected: 258 Color color_ = Color::BLACK; 259 Gradient gradient_; 260 TextStyle textStyle_; 261 TextAlign textAlign_ = TextAlign::LEFT; 262 TextDirection textDirection_ = TextDirection::LTR; 263 int32_t id_ = 0; 264 265 Pattern pattern_; 266 }; 267 268 class StrokePaintState : public PaintState { 269 public: GetLineCap()270 LineCapStyle GetLineCap() const 271 { 272 return lineCap_; 273 } 274 SetLineCap(LineCapStyle lineCap)275 void SetLineCap(LineCapStyle lineCap) 276 { 277 lineCap_ = lineCap; 278 } 279 GetLineJoin()280 LineJoinStyle GetLineJoin() const 281 { 282 return lineJoin_; 283 } 284 SetLineJoin(LineJoinStyle lineJoin)285 void SetLineJoin(LineJoinStyle lineJoin) 286 { 287 lineJoin_ = lineJoin; 288 } 289 GetLineWidth()290 double GetLineWidth() const 291 { 292 return lineWidth_; 293 } 294 SetLineWidth(double lineWidth)295 void SetLineWidth(double lineWidth) 296 { 297 lineWidth_ = lineWidth; 298 } 299 GetMiterLimit()300 double GetMiterLimit() const 301 { 302 return miterLimit_; 303 } 304 SetMiterLimit(double miterLimit)305 void SetMiterLimit(double miterLimit) 306 { 307 miterLimit_ = miterLimit; 308 } 309 GetLineDash()310 LineDashParam GetLineDash() const 311 { 312 return lineDash_; 313 } 314 SetLineDash(const LineDashParam & lineDash)315 void SetLineDash(const LineDashParam& lineDash) 316 { 317 lineDash_ = lineDash; 318 } 319 SetLineDashOffset(double offset)320 void SetLineDashOffset(double offset) 321 { 322 lineDash_.dashOffset = offset; 323 } 324 SetLineDash(const std::vector<double> & segments)325 void SetLineDash(const std::vector<double>& segments) 326 { 327 lineDash_.lineDash = segments; 328 } 329 330 private: 331 LineCapStyle lineCap_ = LineCapStyle::BUTT; 332 LineJoinStyle lineJoin_ = LineJoinStyle::MITER; 333 334 double lineWidth_ = 1.0; // default lineWidth 335 336 double miterLimit_ = 10.0; // default miterLimit 337 LineDashParam lineDash_; 338 }; 339 340 class GlobalPaintState { 341 public: GetAlpha()342 double GetAlpha() const 343 { 344 return alpha_; 345 } 346 SetAlpha(double alpha)347 void SetAlpha(double alpha) 348 { 349 alpha_ = alpha; 350 } 351 GetType()352 CompositeOperation GetType() const 353 { 354 return type_; 355 } 356 SetType(CompositeOperation type)357 void SetType(CompositeOperation type) 358 { 359 type_ = type; 360 } 361 HasGlobalAlpha()362 bool HasGlobalAlpha() const 363 { 364 return !NearEqual(alpha_, 1.0); 365 } 366 367 private: 368 double alpha_ = 1.0; 369 CompositeOperation type_ = CompositeOperation::SOURCE_OVER; 370 }; 371 372 struct PaintHolder { 373 PaintState fillState; 374 StrokePaintState strokeState; 375 GlobalPaintState globalState; 376 Shadow shadow; 377 }; 378 379 } // namespace OHOS::Ace 380 381 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_PROPERTIES_PAINT_STATE_H 382