svg_texture.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /**************************************************************************/
  2. /* svg_texture.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #pragma once
  31. #include "core/templates/lru.h"
  32. #include "scene/resources/texture.h"
  33. class BitMap;
  34. class SVGTexture : public Texture2D {
  35. GDCLASS(SVGTexture, Texture2D);
  36. RES_BASE_EXTENSION("svgtex");
  37. String source;
  38. float base_scale = 1.0;
  39. float saturation = 1.0;
  40. Dictionary color_map;
  41. Size2 size_override;
  42. struct ScalingLevel {
  43. HashSet<SVGTexture *> textures;
  44. int32_t refcount = 1;
  45. };
  46. static Mutex mutex;
  47. static HashMap<double, ScalingLevel> scaling_levels;
  48. mutable RID base_texture;
  49. mutable HashMap<double, RID> texture_cache;
  50. mutable Ref<BitMap> alpha_cache;
  51. mutable HashMap<Color, Color> cmap;
  52. mutable Size2 base_size;
  53. mutable Size2 size;
  54. void _remove_scale(double p_scale);
  55. RID _ensure_scale(double p_scale) const;
  56. RID _load_at_scale(double p_scale, bool p_set_size) const;
  57. void _update_texture();
  58. void _clear();
  59. protected:
  60. static void _bind_methods();
  61. public:
  62. static Ref<SVGTexture> create_from_string(const String &p_source, float p_scale = 1.0, float p_saturation = 1.0, const Dictionary &p_color_map = Dictionary());
  63. void set_source(const String &p_source);
  64. String get_source() const;
  65. void set_base_scale(float p_scale);
  66. float get_base_scale() const;
  67. void set_color_map(const Dictionary &p_color_map);
  68. Dictionary get_color_map() const;
  69. void set_saturation(float p_saturation);
  70. float get_saturation() const;
  71. Ref<Image> get_image() const override;
  72. int get_width() const override;
  73. int get_height() const override;
  74. virtual RID get_rid() const override;
  75. bool has_alpha() const override;
  76. virtual RID get_scaled_rid() const override;
  77. virtual void draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false) const override;
  78. virtual void draw_rect(RID p_canvas_item, const Rect2 &p_rect, bool p_tile = false, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false) const override;
  79. virtual void draw_rect_region(RID p_canvas_item, const Rect2 &p_rect, const Rect2 &p_src_rect, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, bool p_clip_uv = true) const override;
  80. void set_size_override(const Size2i &p_size);
  81. bool is_pixel_opaque(int p_x, int p_y) const override;
  82. static void reference_scaling_level(double p_scale);
  83. static void unreference_scaling_level(double p_scale);
  84. ~SVGTexture();
  85. };