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 int32_t instanceId = 0; 97 }; 98 99 struct ImageData { 100 RefPtr<Ace::PixelMap> pixelMap; 101 int32_t x = 0; 102 int32_t y = 0; 103 int32_t dirtyX = 0; 104 int32_t dirtyY = 0; 105 int32_t dirtyWidth = 0; 106 int32_t dirtyHeight = 0; 107 std::vector<Color> data; 108 }; 109 110 struct TextMetrics { 111 double width = 0; 112 double height = 0; 113 double actualBoundingBoxLeft = 0; 114 double actualBoundingBoxRight = 0; 115 double actualBoundingBoxAscent = 0; 116 double actualBoundingBoxDescent = 0; 117 double alphabeticBaseline = 0; 118 double emHeightAscent = 0; 119 double emHeightDescent = 0; 120 double fontBoundingBoxAscent = 0; 121 double fontBoundingBoxDescent = 0; 122 double hangingBaseline = 0; 123 double ideographicBaseline = 0; 124 }; 125 126 enum class ContextType { 127 RENDER_2D, 128 RENDER_3D, 129 }; 130 131 // following the definition of FillType in skPath 132 enum class CanvasFillRule { 133 NONZERO = 0, 134 EVENODD, 135 }; 136 137 // following the definition in skPaint 138 enum class LineCapStyle { 139 BUTT = 0, 140 ROUND, 141 SQUARE, 142 }; 143 144 enum class LineJoinStyle { 145 MITER = 0, 146 ROUND, 147 BEVEL, 148 }; 149 150 enum class CompositeOperation { 151 SOURCE_OVER = 0, 152 SOURCE_ATOP, 153 SOURCE_IN, 154 SOURCE_OUT, 155 DESTINATION_OVER, 156 DESTINATION_ATOP, 157 DESTINATION_IN, 158 DESTINATION_OUT, 159 LIGHTER, 160 COPY, 161 XOR, 162 }; 163 164 enum class PaintStyle { 165 NONE = 0, 166 Color, 167 Gradient, 168 ImagePattern 169 }; 170 171 class PaintState { 172 public: GetColor()173 const Color& GetColor() const 174 { 175 return color_; 176 } 177 SetColor(const Color & color)178 void SetColor(const Color& color) 179 { 180 paintStyle_ = PaintStyle::Color; 181 color_ = color; 182 } 183 GetGradient()184 const Gradient& GetGradient() const 185 { 186 return gradient_; 187 } 188 SetGradient(const Gradient & gradient)189 void SetGradient(const Gradient& gradient) 190 { 191 paintStyle_ = PaintStyle::Gradient; 192 gradient_ = gradient; 193 } 194 GetTextStyle()195 const TextStyle& GetTextStyle() const 196 { 197 return textStyle_; 198 } 199 SetTextStyle(const TextStyle & textStyle)200 void SetTextStyle(const TextStyle& textStyle) 201 { 202 textStyle_ = textStyle; 203 } 204 GetTextAlign()205 TextAlign GetTextAlign() const 206 { 207 return textAlign_; 208 } 209 SetTextAlign(TextAlign textAlign)210 void SetTextAlign(TextAlign textAlign) 211 { 212 textAlign_ = textAlign; 213 } 214 GetOffTextDirection()215 TextDirection GetOffTextDirection() const 216 { 217 return textDirection_; 218 } 219 SetOffTextDirection(TextDirection textDirection)220 void SetOffTextDirection(TextDirection textDirection) 221 { 222 textDirection_ = textDirection; 223 } 224 SetTextColor(const Color & color)225 void SetTextColor(const Color& color) 226 { 227 textStyle_.SetTextColor(color); 228 } 229 SetFontSize(const Dimension & size)230 void SetFontSize(const Dimension& size) 231 { 232 textStyle_.SetFontSize(size); 233 } 234 SetFontStyle(FontStyle style)235 void SetFontStyle(FontStyle style) 236 { 237 textStyle_.SetFontStyle(style); 238 } 239 SetFontWeight(FontWeight weight)240 void SetFontWeight(FontWeight weight) 241 { 242 textStyle_.SetFontWeight(weight); 243 } 244 SetFontFamilies(const std::vector<std::string> & fontFamilies)245 void SetFontFamilies(const std::vector<std::string>& fontFamilies) 246 { 247 textStyle_.SetFontFamilies(fontFamilies); 248 } 249 SetTextBaseline(TextBaseline baseline)250 void SetTextBaseline(TextBaseline baseline) 251 { 252 textStyle_.SetTextBaseline(baseline); 253 } 254 GetPattern()255 const Pattern& GetPattern() const 256 { 257 return pattern_; 258 } 259 SetPattern(const Pattern & pattern)260 void SetPattern(const Pattern& pattern) 261 { 262 paintStyle_ = PaintStyle::ImagePattern; 263 pattern_ = pattern; 264 } 265 GetPatternNG()266 std::weak_ptr<Ace::Pattern> GetPatternNG() const 267 { 268 return patternNG_; 269 } 270 GetPatternValue()271 Ace::Pattern GetPatternValue() const 272 { 273 Pattern pattern; 274 if (!patternNG_.expired()) { 275 auto value = patternNG_.lock(); 276 if (value) { 277 pattern = *value; 278 } 279 } 280 return pattern; 281 } 282 SetPatternNG(const std::weak_ptr<Ace::Pattern> & pattern)283 void SetPatternNG(const std::weak_ptr<Ace::Pattern>& pattern) 284 { 285 paintStyle_ = PaintStyle::ImagePattern; 286 patternNG_ = pattern; 287 } 288 GetId()289 int32_t GetId() const 290 { 291 return id_; 292 } 293 SetId(int32_t id)294 void SetId(int32_t id) 295 { 296 id_ = id; 297 } 298 GetPaintStyle()299 PaintStyle GetPaintStyle() const 300 { 301 return paintStyle_; 302 } 303 304 protected: 305 Color color_ = Color::BLACK; 306 Gradient gradient_; 307 TextStyle textStyle_; 308 TextAlign textAlign_ = TextAlign::LEFT; 309 TextDirection textDirection_ = TextDirection::LTR; 310 int32_t id_ = 0; 311 PaintStyle paintStyle_ = PaintStyle::Color; 312 Pattern pattern_; 313 std::weak_ptr<Ace::Pattern> patternNG_; 314 }; 315 316 class StrokePaintState : public PaintState { 317 public: GetLineCap()318 LineCapStyle GetLineCap() const 319 { 320 return lineCap_; 321 } 322 SetLineCap(LineCapStyle lineCap)323 void SetLineCap(LineCapStyle lineCap) 324 { 325 lineCap_ = lineCap; 326 } 327 GetLineJoin()328 LineJoinStyle GetLineJoin() const 329 { 330 return lineJoin_; 331 } 332 SetLineJoin(LineJoinStyle lineJoin)333 void SetLineJoin(LineJoinStyle lineJoin) 334 { 335 lineJoin_ = lineJoin; 336 } 337 GetLineWidth()338 double GetLineWidth() const 339 { 340 return lineWidth_; 341 } 342 SetLineWidth(double lineWidth)343 void SetLineWidth(double lineWidth) 344 { 345 lineWidth_ = lineWidth; 346 } 347 GetMiterLimit()348 double GetMiterLimit() const 349 { 350 return miterLimit_; 351 } 352 SetMiterLimit(double miterLimit)353 void SetMiterLimit(double miterLimit) 354 { 355 miterLimit_ = miterLimit; 356 } 357 GetLineDash()358 LineDashParam GetLineDash() const 359 { 360 return lineDash_; 361 } 362 SetLineDash(const LineDashParam & lineDash)363 void SetLineDash(const LineDashParam& lineDash) 364 { 365 lineDash_ = lineDash; 366 } 367 SetLineDashOffset(double offset)368 void SetLineDashOffset(double offset) 369 { 370 lineDash_.dashOffset = offset; 371 } 372 SetLineDash(const std::vector<double> & segments)373 void SetLineDash(const std::vector<double>& segments) 374 { 375 lineDash_.lineDash = segments; 376 } 377 378 private: 379 LineCapStyle lineCap_ = LineCapStyle::BUTT; 380 LineJoinStyle lineJoin_ = LineJoinStyle::MITER; 381 382 double lineWidth_ = 1.0; // default lineWidth 383 384 double miterLimit_ = 10.0; // default miterLimit 385 LineDashParam lineDash_; 386 }; 387 388 class GlobalPaintState { 389 public: GetAlpha()390 double GetAlpha() const 391 { 392 return alpha_; 393 } 394 SetAlpha(double alpha)395 void SetAlpha(double alpha) 396 { 397 alpha_ = alpha; 398 } 399 GetType()400 CompositeOperation GetType() const 401 { 402 return type_; 403 } 404 SetType(CompositeOperation type)405 void SetType(CompositeOperation type) 406 { 407 type_ = type; 408 } 409 HasGlobalAlpha()410 bool HasGlobalAlpha() const 411 { 412 return !NearEqual(alpha_, -1.0); 413 } 414 415 private: 416 double alpha_ = -1.0; 417 CompositeOperation type_ = CompositeOperation::SOURCE_OVER; 418 }; 419 420 struct PaintHolder { 421 PaintState fillState; 422 StrokePaintState strokeState; 423 GlobalPaintState globalState; 424 Shadow shadow; 425 }; 426 427 } // namespace OHOS::Ace 428 429 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_PROPERTIES_PAINT_STATE_H 430