vector_icon_types.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright 2015 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef UI_GFX_VECTOR_ICON_TYPES_H_
  5. #define UI_GFX_VECTOR_ICON_TYPES_H_
  6. #include "third_party/skia/include/core/SkScalar.h"
  7. #include "ui/gfx/animation/tween.h"
  8. namespace gfx {
  9. // This macro allows defining the list of commands in this file, then pulling
  10. // them each in to the template files via using-declarations. Files which want
  11. // to do this should do the following:
  12. // #define DECLARE_VECTOR_COMMAND(x) using gfx::x;
  13. // DECLARE_VECTOR_COMMANDS
  14. // The alternative would be to have the template files pull in the whole gfx
  15. // namespace via using-directives, which is banned by the style guide.
  16. #define DECLARE_VECTOR_COMMANDS \
  17. /* A new <path> element. For the first path, this is assumed. */ \
  18. DECLARE_VECTOR_COMMAND(NEW_PATH) \
  19. /* Sets the alpha for the current path. */ \
  20. DECLARE_VECTOR_COMMAND(PATH_COLOR_ALPHA) \
  21. /* Sets the color for the current path. */ \
  22. DECLARE_VECTOR_COMMAND(PATH_COLOR_ARGB) \
  23. /* Sets the path to clear mode (Skia's kClear_Mode). */ \
  24. DECLARE_VECTOR_COMMAND(PATH_MODE_CLEAR) \
  25. /* By default, the path will be filled. This changes the paint action to */ \
  26. /* stroke at the given width. */ \
  27. DECLARE_VECTOR_COMMAND(STROKE) \
  28. /* By default, a stroke has a round cap. This sets it to square. */ \
  29. DECLARE_VECTOR_COMMAND(CAP_SQUARE) \
  30. /* These correspond to pathing commands. */ \
  31. DECLARE_VECTOR_COMMAND(MOVE_TO) \
  32. DECLARE_VECTOR_COMMAND(R_MOVE_TO) \
  33. DECLARE_VECTOR_COMMAND(ARC_TO) \
  34. DECLARE_VECTOR_COMMAND(R_ARC_TO) \
  35. DECLARE_VECTOR_COMMAND(LINE_TO) \
  36. DECLARE_VECTOR_COMMAND(R_LINE_TO) \
  37. DECLARE_VECTOR_COMMAND(H_LINE_TO) \
  38. DECLARE_VECTOR_COMMAND(R_H_LINE_TO) \
  39. DECLARE_VECTOR_COMMAND(V_LINE_TO) \
  40. DECLARE_VECTOR_COMMAND(R_V_LINE_TO) \
  41. DECLARE_VECTOR_COMMAND(CUBIC_TO) \
  42. DECLARE_VECTOR_COMMAND(R_CUBIC_TO) \
  43. DECLARE_VECTOR_COMMAND(CUBIC_TO_SHORTHAND) \
  44. DECLARE_VECTOR_COMMAND(QUADRATIC_TO) \
  45. DECLARE_VECTOR_COMMAND(R_QUADRATIC_TO) \
  46. DECLARE_VECTOR_COMMAND(QUADRATIC_TO_SHORTHAND) \
  47. DECLARE_VECTOR_COMMAND(R_QUADRATIC_TO_SHORTHAND) \
  48. DECLARE_VECTOR_COMMAND(CIRCLE) \
  49. DECLARE_VECTOR_COMMAND(OVAL) \
  50. DECLARE_VECTOR_COMMAND(ROUND_RECT) \
  51. DECLARE_VECTOR_COMMAND(CLOSE) \
  52. /* Sets the dimensions of the canvas in dip. */ \
  53. DECLARE_VECTOR_COMMAND(CANVAS_DIMENSIONS) \
  54. /* Sets a bounding rect for the path. This allows fine adjustment because */ \
  55. /* it can tweak edge anti-aliasing. Args are x, y, w, h. */ \
  56. DECLARE_VECTOR_COMMAND(CLIP) \
  57. /* Disables anti-aliasing for this path. */ \
  58. DECLARE_VECTOR_COMMAND(DISABLE_AA) \
  59. /* Flips the x-axis in RTL locales. Default is false, this command sets */ \
  60. /* it to true. */ \
  61. DECLARE_VECTOR_COMMAND(FLIPS_IN_RTL)
  62. #define DECLARE_VECTOR_COMMAND(x) x,
  63. // A command to Skia.
  64. enum CommandType { DECLARE_VECTOR_COMMANDS };
  65. #undef DECLARE_VECTOR_COMMAND
  66. // A POD that describes either a path command or an argument for it.
  67. struct PathElement {
  68. constexpr PathElement(CommandType value) : command(value) {}
  69. constexpr PathElement(SkScalar value) : arg(value) {}
  70. union {
  71. CommandType command;
  72. SkScalar arg;
  73. };
  74. };
  75. // Describes the drawing commands for a single vector icon at a particular pixel
  76. // size or range of sizes.
  77. struct VectorIconRep {
  78. VectorIconRep() = default;
  79. constexpr VectorIconRep(const PathElement* path, size_t path_size)
  80. : path(path), path_size(path_size) {}
  81. VectorIconRep(const VectorIconRep&) = delete;
  82. VectorIconRep& operator=(const VectorIconRep&) = delete;
  83. const PathElement* path = nullptr;
  84. // The length of |path|.
  85. size_t path_size = 0u;
  86. };
  87. // A vector icon that stores one or more representations to be used for various
  88. // scale factors and pixel dimensions.
  89. struct VectorIcon {
  90. VectorIcon() = default;
  91. constexpr VectorIcon(const VectorIconRep* reps,
  92. size_t reps_size,
  93. const char* name)
  94. : reps(reps), reps_size(reps_size), name(name) {}
  95. VectorIcon(const VectorIcon&) = delete;
  96. VectorIcon& operator=(const VectorIcon&) = delete;
  97. bool is_empty() const { return !reps; }
  98. const VectorIconRep* const reps = nullptr;
  99. size_t reps_size = 0u;
  100. // A human-readable name, useful for debugging, derived from the name of the
  101. // icon file. This can also be used as an identifier, but vector icon targets
  102. // should be careful to ensure this is unique.
  103. const char* name = nullptr;
  104. bool operator<(const VectorIcon& other) const;
  105. };
  106. } // namespace gfx
  107. #endif // UI_GFX_VECTOR_ICON_TYPES_H_