text_custom_emoji.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // This file is part of Desktop App Toolkit,
  2. // a set of libraries for developing nice desktop applications.
  3. //
  4. // For license and copyright information please follow this link:
  5. // https://github.com/desktop-app/legal/blob/master/LEGAL
  6. //
  7. #pragma once
  8. #include <QtGui/QColor>
  9. #include <QtGui/QImage>
  10. #include <QtCore/QSize>
  11. #include <QtCore/QPoint>
  12. #include <crl/crl_time.h>
  13. #include <any>
  14. class QPainter;
  15. namespace Ui::Text {
  16. struct MarkedContext;
  17. [[nodiscard]] int AdjustCustomEmojiSize(int emojiSize);
  18. struct CustomEmojiPaintContext {
  19. required<QColor> textColor;
  20. QSize size; // Required only when scaled = true, for path scaling.
  21. crl::time now = 0;
  22. float64 scale = 0.;
  23. QPoint position;
  24. bool paused = false;
  25. bool scaled = false;
  26. mutable struct {
  27. bool colorized = false;
  28. bool forceFirstFrame = false;
  29. bool forceLastFrame = false;
  30. bool overrideFirstWithLastFrame = false;
  31. } internal;
  32. };
  33. class CustomEmoji {
  34. public:
  35. virtual ~CustomEmoji() = default;
  36. [[nodiscard]] virtual int width() = 0;
  37. [[nodiscard]] virtual QString entityData() = 0;
  38. using Context = CustomEmojiPaintContext;
  39. virtual void paint(QPainter &p, const Context &context) = 0;
  40. virtual void unload() = 0;
  41. [[nodiscard]] virtual bool ready() = 0;
  42. [[nodiscard]] virtual bool readyInDefaultState() = 0;
  43. };
  44. class ShiftedEmoji final : public CustomEmoji {
  45. public:
  46. ShiftedEmoji(std::unique_ptr<CustomEmoji> wrapped, QPoint shift);
  47. int width() override;
  48. QString entityData() override;
  49. void paint(QPainter &p, const Context &context) override;
  50. void unload() override;
  51. bool ready() override;
  52. bool readyInDefaultState() override;
  53. private:
  54. const std::unique_ptr<Ui::Text::CustomEmoji> _wrapped;
  55. const QPoint _shift;
  56. };
  57. class FirstFrameEmoji final : public CustomEmoji {
  58. public:
  59. explicit FirstFrameEmoji(std::unique_ptr<CustomEmoji> wrapped);
  60. int width() override;
  61. QString entityData() override;
  62. void paint(QPainter &p, const Context &context) override;
  63. void unload() override;
  64. bool ready() override;
  65. bool readyInDefaultState() override;
  66. private:
  67. const std::unique_ptr<Ui::Text::CustomEmoji> _wrapped;
  68. };
  69. class LimitedLoopsEmoji final : public CustomEmoji {
  70. public:
  71. LimitedLoopsEmoji(
  72. std::unique_ptr<CustomEmoji> wrapped,
  73. int limit,
  74. bool stopOnLast = false);
  75. int width() override;
  76. QString entityData() override;
  77. void paint(QPainter &p, const Context &context) override;
  78. void unload() override;
  79. bool ready() override;
  80. bool readyInDefaultState() override;
  81. private:
  82. const std::unique_ptr<Ui::Text::CustomEmoji> _wrapped;
  83. const int _limit = 0;
  84. int _played = 0;
  85. bool _inLoop = false;
  86. bool _stopOnLast = false;
  87. };
  88. [[nodiscard]] std::unique_ptr<CustomEmoji> MakeCustomEmoji(
  89. QStringView data,
  90. const MarkedContext &context);
  91. } // namespace Ui::Text