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