1 /* 2 * Copyright (c) 2021-2025 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 PEN_H 17 #define PEN_H 18 19 #include "draw/brush.h" 20 #include "draw/color.h" 21 #include "effect/blur_draw_looper.h" 22 #include "effect/filter.h" 23 #include "effect/path_effect.h" 24 #include "utils/rect.h" 25 26 namespace OHOS { 27 namespace Rosen { 28 namespace Drawing { 29 class DRAWING_API Pen { 30 public: 31 enum class JoinStyle { 32 MITER_JOIN, 33 ROUND_JOIN, 34 BEVEL_JOIN, 35 DEFAULT_JOIN = MITER_JOIN 36 }; 37 38 enum class CapStyle { 39 FLAT_CAP, 40 ROUND_CAP, 41 SQUARE_CAP, 42 DEFAULT_CAP = FLAT_CAP 43 }; 44 45 Pen() noexcept; 46 Pen(const Pen& p) noexcept = default; 47 Pen(const Color& c) noexcept; 48 Pen(int rgba) noexcept; ~Pen()49 ~Pen() {}; 50 51 /** 52 * @brief Get the Color of pen 53 * 54 * @return Color of pen 55 */ 56 Color GetColor() const; 57 58 /** 59 * @brief Set the Color of pen 60 * 61 * @param c color object to set 62 */ 63 void SetColor(const Color& c); 64 65 /** 66 * @brief Set the Color object 67 * 68 * @param c color uint32_t value to set 69 */ 70 void SetColor(uint32_t c); 71 72 /** 73 * @brief set ARGB of pen 74 * 75 * @param a alpha 76 * @param r red 77 * @param g green 78 * @param b blue 79 */ 80 void SetARGB(int a, int r, int g, int b); 81 82 /** 83 * @brief Get the Color 4f of pen 84 * 85 * @return Color4f 86 */ 87 const Color4f& GetColor4f(); 88 89 /** 90 * @brief Get the Color Space object 91 * 92 * @return a shared pointer ColorSpace 93 */ 94 const std::shared_ptr<ColorSpace> GetColorSpace() const; 95 96 /** 97 * @brief Get a Color Space pointer 98 * 99 * @return a pointer ColorSpace 100 */ 101 const ColorSpace* GetColorSpacePtr() const; 102 103 /** 104 * @brief Set the Color and ColorSpace of pen 105 * 106 * @param cf color4f object 107 * @param s a shared pointer to colorspace 108 */ 109 void SetColor(const Color4f& cf, std::shared_ptr<ColorSpace> s); 110 111 /** 112 * @brief Get the Alpha value of pen 113 * 114 * @return uint32_t of alpha 115 */ 116 uint32_t GetAlpha() const; 117 118 /** 119 * @brief Get the Alpha F object of pen 120 * 121 * @return value of alpha f 122 */ 123 scalar GetAlphaF() const; 124 125 /** 126 * @brief Get the Red F object of pen 127 * @return value of red f 128 */ 129 scalar GetRedF() const; 130 131 /** 132 * @brief Get the Green F object of pen 133 * @return value of green f 134 */ 135 scalar GetGreenF() const; 136 137 /** 138 * @brief Get the Blue F object of pen 139 * @return value of blue f 140 */ 141 scalar GetBlueF() const; 142 143 /** 144 * @brief Set the Alpha value of pen 145 * 146 * @param a the alpha value to set 147 */ 148 void SetAlpha(uint32_t a); 149 150 /** 151 * @brief Set the Alpha F value of pen 152 * 153 * @param a the alpha f value 154 */ 155 void SetAlphaF(scalar a); 156 157 /** 158 * @brief Get the Width value of pen 159 * 160 * @return width value of pen 161 */ 162 scalar GetWidth() const; 163 164 /** 165 * @brief Set the Width value of pen 166 * 167 * @param width the value of width 168 */ 169 void SetWidth(scalar width); 170 171 /** 172 * @brief Get the Miter Limit of pen 173 * 174 * @return the value of miter limit 175 */ 176 scalar GetMiterLimit() const; 177 178 /** 179 * @brief Set the Miter Limit of pen 180 * 181 * @param limit the value of miter limit to set 182 */ 183 void SetMiterLimit(scalar limit); 184 185 /** 186 * @brief Get the Cap Style of pen 187 * 188 * @return CapStyle of pen 189 */ 190 CapStyle GetCapStyle() const; 191 192 /** 193 * @brief Set the Cap Style of pen 194 * 195 * @param cs the cap style object 196 */ 197 void SetCapStyle(CapStyle cs); 198 199 /** 200 * @brief Get the Join Style of pen 201 * 202 * @return JoinStyle of pen 203 */ 204 JoinStyle GetJoinStyle() const; 205 206 /** 207 * @brief Sets the geometry drawn at the corners of strokes. 208 * 209 * @param js join style object 210 */ 211 void SetJoinStyle(JoinStyle js); 212 213 /** 214 * @brief Get the Blend Mode of pen 215 * 216 * @return the value of BlendMode 217 */ 218 BlendMode GetBlendMode() const; 219 220 /** 221 * @brief Set the Blend Mode of pen 222 * 223 * @param mode the value of pen's blend mode 224 */ 225 void SetBlendMode(BlendMode mode); 226 227 /** 228 * @brief Sets the current blender, increasing its refcnt, and if a blender is already 229 * present, decreasing that object's refcnt. 230 * @param blender Blender used to set 231 */ 232 void SetBlender(std::shared_ptr<Blender> blender); 233 234 /** 235 * @brief Returns the user-supplied blend function, if one has been set. 236 * @return the Blender assigned to this Brush, otherwise nullptr 237 */ 238 std::shared_ptr<Blender> GetBlender() const; 239 240 /** 241 * @brief Sets the blenderEnabled flag, which determines whether the blender is used. 242 * @param blenderEnabled whether the blender is used. 243 */ 244 void SetBlenderEnabled(bool blenderEnabled); 245 246 /** 247 * @brief Returns the blenderEnabled flag, which determines whether the blender is used. 248 * @return the blenderEnabled flag, whether the blender is used. 249 */ GetBlenderEnabled()250 bool GetBlenderEnabled() const { return blenderEnabled_; }; 251 252 /** 253 * @brief Returns the user-supplied blend function, if one has been set. 254 * @return the Blender assigned to this Brush, otherwise nullptr 255 */ 256 const Blender* GetBlenderPtr() const; 257 258 /** 259 * @brief Returns true if pixels on the active edges of Path may be drawn with partial transparency. 260 * @return antialiasing state 261 */ 262 bool IsAntiAlias() const; 263 264 /** 265 * @brief Requests, but does not require, that edge pixels draw opaque or with partial transparency. 266 * @param aa setting for antialiasing 267 */ 268 void SetAntiAlias(bool aa); 269 270 /** 271 * @brief Sets PathEffect to pen. Pass nullptr to leave the path geometry unaltered. 272 * @param e replace Path with a modification when drawn 273 */ 274 void SetPathEffect(std::shared_ptr<PathEffect> e); 275 276 /** 277 * @brief Returns PathEffect if set, or nullptr. 278 * @return A shared pointer to PathEffect if previously set, nullptr otherwise 279 */ 280 std::shared_ptr<PathEffect> GetPathEffect() const; 281 282 /** 283 * @brief Returns PathEffect if set, or nullptr. 284 * @return A pointer to PathEffect if previously set, nullptr otherwise 285 */ 286 const PathEffect* GetPathEffectPtr() const; 287 288 /** 289 * @brief Set the Filter object with three property. 290 * ColorFilter, MaskFilter, and ImageFilter to apply to subsequent draw 291 * @param filter the Filter object to set 292 */ 293 void SetFilter(const Filter& filter); 294 const Filter& GetFilter() const; 295 bool HasFilter() const; 296 297 /** 298 * @brief Sets optional colors used when filling a path, such as a gradient. Sets SkShader to shader 299 * @param shader how geometry is filled with color; if nullptr, color is used instead 300 */ 301 void SetShaderEffect(std::shared_ptr<ShaderEffect> e); 302 303 /** 304 * @brief Returns optional colors used when filling a path, such as a gradient. 305 * @return A shared pointer to ShaderEffect 306 */ 307 std::shared_ptr<ShaderEffect> GetShaderEffect() const; 308 309 /** 310 * @brief Returns optional colors used when filling a path, such as a gradient. 311 * @return A pointer to ShaderEffect 312 */ 313 const ShaderEffect* GetShaderEffectPtr() const; 314 315 /** 316 * @brief Sets all contents to their initial values. This is equivalent to replacing 317 * Pen with the result of Pen(). 318 */ 319 void Reset(); 320 321 /** 322 * @brief Sets BlurDrawLooper, it will generate two draw operations, which may affect performance. 323 */ 324 void SetLooper(std::shared_ptr<BlurDrawLooper> blurDrawLooper); 325 326 /** 327 * @brief Gets BlurDrawLooper. 328 */ 329 std::shared_ptr<BlurDrawLooper> GetLooper() const; 330 331 /** 332 * @brief Returns the filled equivalent of the stroked path. 333 * @param src Path read to create a filled version 334 * @param dst resulting Path; may be the same as src 335 * @param rect optional limit passed to PathEffect 336 * @param matrix tranform passed to PathEffect 337 */ 338 bool GetFillPath(const Path& src, Path& dst, const Rect* rect, const Matrix& matrix); 339 340 friend DRAWING_API bool operator==(const Pen& p1, const Pen& p2); 341 friend DRAWING_API bool operator!=(const Pen& p1, const Pen& p2); 342 343 void Dump(std::string& out) const; 344 345 private: 346 scalar width_; 347 scalar miterLimit_; 348 JoinStyle join_; 349 CapStyle cap_; 350 std::shared_ptr<PathEffect> pathEffect_; 351 352 Brush brush_; 353 354 bool blenderEnabled_ = true; 355 }; 356 } // namespace Drawing 357 } // namespace Rosen 358 } // namespace OHOS 359 #endif