CCLabelLayout.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /****************************************************************************
  2. Copyright (c) 2020 Xiamen Yaji Software Co., Ltd.
  3. http://www.cocos.com
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #pragma once
  21. #include <string>
  22. #include "2d/CCTTFLabelAtlasCache.h"
  23. #include "math/Vec3.h"
  24. #include "math/Vec4.h"
  25. #include "base/ccConfig.h"
  26. #if CC_ENABLE_TTF_LABEL_RENDERER
  27. namespace cocos2d {
  28. namespace renderer {
  29. class CustomAssembler;
  30. class Texture2D;
  31. class MeshBuffer;
  32. class Effect;
  33. class EffectVariant;
  34. }
  35. namespace middleware {
  36. class MeshBuffer;
  37. }
  38. class TextRenderGroup;
  39. class LabelRenderer;
  40. class TextRowSpace {
  41. /**
  42. * Y
  43. * ^
  44. * |
  45. * |
  46. * +------> X
  47. */
  48. public:
  49. struct GlyphBlock {
  50. Rect area;
  51. Rect uv;
  52. int texId = 0;
  53. bool ignored = false;
  54. };
  55. TextRowSpace() = default;
  56. TextRowSpace(TextRowSpace &&other);
  57. void fillRect(int texId, Rect &rect, Rect &uv);
  58. void translate(float x, float y);
  59. Vec2 center() const;
  60. float getWidth() const { return _data.size() > 0 ? _right - _left : 0; }
  61. float getHeight() const { return _data.size() > 0 ? _top - _bottom : 0; }
  62. float getExtentedWidth(float left, float right) const;
  63. Vec2 getOffset() const { return Vec2(_x, _y); }
  64. GlyphBlock & operator[] (size_t i);
  65. bool isIgnored() const { return _ignored; }
  66. bool isIgnored(int i) const { return _data[i].ignored; }
  67. void clip(const Rect &rec);
  68. size_t size() const { return _data.size(); }
  69. bool validate() const { return _data.size() > 0; }
  70. Rect asRect() const;
  71. float getLeft() const { return _left; }
  72. float getRight() const { return _right; }
  73. float getTop() const { return _top; }
  74. float getBottom() const { return _bottom; }
  75. private:
  76. void reset();
  77. GlyphBlock & appendBlock();
  78. private:
  79. float _left = FLT_MAX;
  80. float _bottom = FLT_MAX;
  81. float _right = FLT_MIN;
  82. float _top = FLT_MIN;
  83. float _x = 0.0f;
  84. float _y = 0.0f;
  85. std::vector<GlyphBlock> _data;
  86. bool _ignored = false;
  87. };
  88. class LabelLayout {
  89. public:
  90. LabelLayout() = default;
  91. bool init(const std::string& font, const std::string& text, float fontSize, float retinaFontSize, LabelLayoutInfo *info);
  92. virtual ~LabelLayout();
  93. void setString(const std::string &txt, bool forceUpdate);
  94. bool isInited() const { return _inited; }
  95. void fillAssembler(renderer::CustomAssembler *assembeler, renderer::EffectVariant *effect);
  96. private:
  97. bool updateContent();
  98. private:
  99. std::string _string;
  100. std::u32string _u32string;
  101. std::string _font;
  102. float _fontSize = 0.0f;
  103. float _retinaFontSize = 0.0f;
  104. float _fontScale = 1.0f;
  105. float _scale = 1.0f;
  106. //weak reference
  107. LabelLayoutInfo *_layoutInfo = nullptr;
  108. std::shared_ptr<TTFLabelAtals> _fontAtlas;
  109. bool _enableKerning = true;
  110. bool _inited = false;
  111. std::vector<TextRowSpace> _textSpace;
  112. std::shared_ptr<TextRenderGroup> _groups;
  113. std::shared_ptr<TextRenderGroup> _shadowGroups;
  114. };
  115. }
  116. #endif