CCLabel.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. /****************************************************************************
  2. Copyright (c) 2013 Zynga Inc.
  3. Copyright (c) 2013-2016 Chukong Technologies Inc.
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #ifndef _COCOS2D_CCLABEL_H_
  23. #define _COCOS2D_CCLABEL_H_
  24. #include "2d/CCNode.h"
  25. #include "renderer/CCCustomCommand.h"
  26. #include "renderer/CCQuadCommand.h"
  27. #include "2d/CCFontAtlas.h"
  28. #include "base/ccTypes.h"
  29. NS_CC_BEGIN
  30. /**
  31. * @addtogroup _2d
  32. * @{
  33. */
  34. /**
  35. * @struct TTFConfig
  36. * @see `GlyphCollection`
  37. */
  38. typedef struct _ttfConfig
  39. {
  40. std::string fontFilePath;
  41. float fontSize;
  42. GlyphCollection glyphs;
  43. const char *customGlyphs;
  44. bool distanceFieldEnabled;
  45. int outlineSize;
  46. bool italics;
  47. bool bold;
  48. bool underline;
  49. bool strikethrough;
  50. _ttfConfig(const std::string& filePath = "",float size = 12, const GlyphCollection& glyphCollection = GlyphCollection::DYNAMIC,
  51. const char *customGlyphCollection = nullptr, bool useDistanceField = false, int outline = 0,
  52. bool useItalics = false, bool useBold = false, bool useUnderline = false, bool useStrikethrough = false)
  53. : fontFilePath(filePath)
  54. , fontSize(size)
  55. , glyphs(glyphCollection)
  56. , customGlyphs(customGlyphCollection)
  57. , distanceFieldEnabled(useDistanceField)
  58. , outlineSize(outline)
  59. , italics(useItalics)
  60. , bold(useBold)
  61. , underline(useUnderline)
  62. , strikethrough(useStrikethrough)
  63. {
  64. if(outline > 0)
  65. {
  66. distanceFieldEnabled = false;
  67. }
  68. }
  69. } TTFConfig;
  70. class Sprite;
  71. class SpriteBatchNode;
  72. class DrawNode;
  73. class EventListenerCustom;
  74. /**
  75. * @brief Label is a subclass of Node that knows how to render text labels.
  76. *
  77. * Label can be created with:
  78. * - A true type font file.
  79. * - A bitmap font file.
  80. * - A char map file.
  81. * - The built in system font.
  82. *
  83. * Bitmap Font supported editors:
  84. * - http://glyphdesigner.71squared.com/ (Commercial, Mac OS X)
  85. * - http://www.n4te.com/hiero/hiero.jnlp (Free, Java)
  86. * - http://slick.cokeandcode.com/demos/hiero.jnlp (Free, Java)
  87. * - http://www.angelcode.com/products/bmfont/ (Free, Windows only)
  88. * @js NA
  89. */
  90. class CC_DLL Label : public Node, public LabelProtocol, public BlendProtocol
  91. {
  92. public:
  93. enum class Overflow
  94. {
  95. //In NONE mode, the dimensions is (0,0) and the content size will change dynamically to fit the label.
  96. NONE,
  97. /**
  98. *In CLAMP mode, when label content goes out of the bounding box, it will be clipped.
  99. */
  100. CLAMP,
  101. /**
  102. * In SHRINK mode, the font size will change dynamically to adapt the content size.
  103. */
  104. SHRINK,
  105. /**
  106. *In RESIZE_HEIGHT mode, you can only change the width of label and the height is changed automatically.
  107. */
  108. RESIZE_HEIGHT
  109. };
  110. /// @name Creators
  111. /// @{
  112. /**
  113. * Allocates and initializes a Label, with default settings.
  114. *
  115. * @return An automatically released Label object.
  116. */
  117. static Label* create();
  118. /**
  119. * Allocates and initializes a Label, base on platform-dependent API.
  120. *
  121. * @param text The initial text.
  122. * @param font A font file or a font family name.
  123. * @param fontSize The font size. This value must be > 0.
  124. * @param dimensions
  125. * @param hAlignment The text horizontal alignment.
  126. * @param vAlignment The text vertical alignment.
  127. *
  128. * @warning It will generate texture by the platform-dependent code.
  129. *
  130. * @return An automatically released Label object.
  131. */
  132. static Label* createWithSystemFont(const std::string& text, const std::string& font, float fontSize,
  133. const Size& dimensions = Size::ZERO, TextHAlignment hAlignment = TextHAlignment::LEFT,
  134. TextVAlignment vAlignment = TextVAlignment::TOP);
  135. /**
  136. * Allocates and initializes a Label, base on FreeType2.
  137. *
  138. * @param text The initial text.
  139. * @param fontFilePath A font file.
  140. * @param fontSize The font size. This value must be > 0.
  141. * @param dimensions
  142. * @param hAlignment The text horizontal alignment.
  143. * @param vAlignment The text vertical alignment.
  144. *
  145. * @return An automatically released Label object.
  146. */
  147. static Label * createWithTTF(const std::string& text, const std::string& fontFilePath, float fontSize,
  148. const Size& dimensions = Size::ZERO, TextHAlignment hAlignment = TextHAlignment::LEFT,
  149. TextVAlignment vAlignment = TextVAlignment::TOP);
  150. /**
  151. * Allocates and initializes a Label, base on FreeType2.
  152. *
  153. * @param ttfConfig A pointer to a TTFConfig object.
  154. * @param text The initial text.
  155. * @param hAlignment The text horizontal alignment.
  156. * @param maxLineWidth The max line width.
  157. *
  158. * @return An automatically released Label object.
  159. * @see TTFConfig setTTFConfig setMaxLineWidth
  160. */
  161. static Label* createWithTTF(const TTFConfig& ttfConfig, const std::string& text,
  162. TextHAlignment hAlignment = TextHAlignment::LEFT, int maxLineWidth = 0);
  163. /**
  164. * Allocates and initializes a Label, with a bitmap font file.
  165. *
  166. * @param bmfontPath A bitmap font file, it's a FNT format.
  167. * @param text The initial text.
  168. * @param hAlignment Text horizontal alignment.
  169. * @param maxLineWidth The max line width.
  170. * @param imageOffset
  171. *
  172. * @return An automatically released Label object.
  173. * @see setBMFontFilePath setMaxLineWidth
  174. */
  175. static Label* createWithBMFont(const std::string& bmfontPath, const std::string& text,
  176. const TextHAlignment& hAlignment = TextHAlignment::LEFT, int maxLineWidth = 0,
  177. const Vec2& imageOffset = Vec2::ZERO);
  178. /**
  179. * Allocates and initializes a Label, with char map configuration.
  180. *
  181. * @param charMapFile A char map file, it's a PNG format.
  182. * @param itemWidth The width in points of each element.
  183. * @param itemHeight The height in points of each element.
  184. * @param startCharMap The starting char of the char map.
  185. *
  186. * @return An automatically released Label object.
  187. */
  188. static Label * createWithCharMap(const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap);
  189. /**
  190. * Allocates and initializes a Label, with char map configuration.
  191. *
  192. * @param texture A pointer to an existing Texture2D object.
  193. * @param itemWidth The width in points of each element.
  194. * @param itemHeight The height in points of each element.
  195. * @param startCharMap The starting char of the char map.
  196. *
  197. * @return An automatically released Label object.
  198. */
  199. static Label * createWithCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap);
  200. /**
  201. * Allocates and initializes a Label, with char map configuration.
  202. *
  203. * @param plistFile A configuration file of char map.
  204. *
  205. * @return An automatically released Label object.
  206. */
  207. static Label * createWithCharMap(const std::string& plistFile);
  208. // end of creators group
  209. /// @}
  210. /// @{
  211. /// @name Font methods
  212. /**
  213. * Sets a new TTF configuration to Label.
  214. * @see `TTFConfig`
  215. */
  216. virtual bool setTTFConfig(const TTFConfig& ttfConfig);
  217. /**
  218. * Returns the TTF configuration object used by the Label.
  219. * @see `TTFConfig`
  220. */
  221. virtual const TTFConfig& getTTFConfig() const { return _fontConfig;}
  222. /** Sets a new bitmap font to Label */
  223. virtual bool setBMFontFilePath(const std::string& bmfontFilePath, const Vec2& imageOffset = Vec2::ZERO, float fontSize = 0);
  224. /** Returns the bitmap font used by the Label.*/
  225. const std::string& getBMFontFilePath() const { return _bmFontPath;}
  226. /**
  227. * Sets a new char map configuration to Label.
  228. *
  229. * @see `createWithCharMap(const std::string&,int,int,int)`
  230. */
  231. virtual bool setCharMap(const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap);
  232. /**
  233. * Sets a new char map configuration to Label.
  234. *
  235. * @see `createWithCharMap(Texture2D*,int,int,int)`
  236. */
  237. virtual bool setCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap);
  238. /**
  239. * Sets a new char map configuration to Label.
  240. *
  241. * @see `createWithCharMap(const std::string&)`
  242. */
  243. virtual bool setCharMap(const std::string& plistFile);
  244. /**
  245. * Sets a new system font to Label.
  246. *
  247. * @param font A font file or a font family name.
  248. * @warning
  249. */
  250. virtual void setSystemFontName(const std::string& font);
  251. /** Returns the system font used by the Label.*/
  252. virtual const std::string& getSystemFontName() const { return _systemFont;}
  253. /* Sets the system font size of Label.*/
  254. virtual void setSystemFontSize(float fontSize);
  255. /** Returns the bitmap font path used by the Label.*/
  256. virtual float getSystemFontSize() const { return _systemFontSize;}
  257. /**
  258. * @warning This method is not recommended for game developers.
  259. */
  260. virtual void requestSystemFontRefresh() { _systemFontDirty = true;}
  261. // end of font methods
  262. /// @}
  263. /** Sets the text that this Label is to display.*/
  264. virtual void setString(const std::string& text) override;
  265. /** Return the text the Label is currently displaying.*/
  266. virtual const std::string& getString() const override { return _utf8Text; }
  267. /**
  268. * Return the number of lines of text.
  269. */
  270. int getStringNumLines();
  271. /**
  272. * Return length of string.
  273. */
  274. int getStringLength();
  275. /**
  276. * Sets the text color of Label.
  277. *
  278. * The text color is different from the color of Node.
  279. *
  280. * @warning Limiting use to only when the Label created with true type font or system font.
  281. */
  282. virtual void setTextColor(const Color4B &color);
  283. /** Returns the text color of the Label.*/
  284. const Color4B& getTextColor() const { return _textColor;}
  285. /**
  286. * Enable shadow effect to Label.
  287. *
  288. * @todo Support blur for shadow effect.
  289. */
  290. virtual void enableShadow(const Color4B& shadowColor = Color4B::BLACK,const Size &offset = Size(2,-2), int blurRadius = 0);
  291. /**
  292. * Enable outline effect to Label.
  293. * @warning Limiting use to only when the Label created with true type font or system font.
  294. */
  295. virtual void enableOutline(const Color4B& outlineColor,int outlineSize = -1);
  296. /**
  297. * Enable glow effect to Label.
  298. * @warning Limiting use to only when the Label created with true type font.
  299. */
  300. virtual void enableGlow(const Color4B& glowColor);
  301. /**
  302. * Enable italics rendering
  303. */
  304. void enableItalics();
  305. /**
  306. * Enable bold rendering
  307. */
  308. void enableBold();
  309. /**
  310. * Enable underline
  311. */
  312. void enableUnderline();
  313. /**
  314. * Enables strikethrough.
  315. * Underline and Strikethrough cannot be enabled at the same time.
  316. * Strikethrough is like an underline but at the middle of the glyph
  317. */
  318. void enableStrikethrough();
  319. /**
  320. * Disable all effect applied to Label.
  321. * @warning Please use disableEffect(LabelEffect::ALL) instead of this API.
  322. */
  323. virtual void disableEffect();
  324. /**
  325. * Disable effect to Label.
  326. *
  327. * @see `LabelEffect`
  328. */
  329. virtual void disableEffect(LabelEffect effect);
  330. /**
  331. * Return whether the shadow effect is enabled.
  332. */
  333. bool isShadowEnabled() const { return _shadowEnabled; }
  334. /**
  335. * Return shadow effect offset value.
  336. */
  337. Size getShadowOffset() const { return _shadowOffset; }
  338. /**
  339. * Return the shadow effect blur radius.
  340. */
  341. float getShadowBlurRadius() const { return _shadowBlurRadius; }
  342. /**
  343. * Return the shadow effect color value.
  344. */
  345. Color4F getShadowColor() const { return _shadowColor4F; }
  346. /**
  347. * Return the outline effect size value.
  348. */
  349. float getOutlineSize() const { return _outlineSize; }
  350. /**
  351. * Return current effect type.
  352. */
  353. LabelEffect getLabelEffectType() const { return _currLabelEffect; }
  354. /**
  355. * Return current effect color value.
  356. */
  357. Color4F getEffectColor() const { return _effectColorF; }
  358. /** Sets the Label's text horizontal alignment.*/
  359. void setAlignment(TextHAlignment hAlignment) { setAlignment(hAlignment,_vAlignment);}
  360. /** Returns the Label's text horizontal alignment.*/
  361. TextHAlignment getTextAlignment() const { return _hAlignment;}
  362. /** Sets the Label's text alignment.*/
  363. void setAlignment(TextHAlignment hAlignment,TextVAlignment vAlignment);
  364. /** Sets the Label's text horizontal alignment.*/
  365. void setHorizontalAlignment(TextHAlignment hAlignment) { setAlignment(hAlignment,_vAlignment); }
  366. /** Returns the Label's text horizontal alignment.*/
  367. TextHAlignment getHorizontalAlignment() const { return _hAlignment; }
  368. /** Sets the Label's text vertical alignment.*/
  369. void setVerticalAlignment(TextVAlignment vAlignment) { setAlignment(_hAlignment,vAlignment); }
  370. /** Returns the Label's text vertical alignment.*/
  371. TextVAlignment getVerticalAlignment() const { return _vAlignment; }
  372. /**
  373. * Specify what happens when a line is too long for Label.
  374. *
  375. * @param breakWithoutSpace Lines are automatically broken between words if this value is false.
  376. */
  377. void setLineBreakWithoutSpace(bool breakWithoutSpace);
  378. /**
  379. * Makes the Label at most this line untransformed width.
  380. * The Label's max line width be used for force line breaks if the value not equal zero.
  381. */
  382. void setMaxLineWidth(float maxLineWidth);
  383. float getMaxLineWidth() { return _maxLineWidth; }
  384. /**
  385. * Change font size of label type BMFONT
  386. * Note: This function only scale the BMFONT letter to mimic the font size change effect.
  387. *
  388. * @param fontSize The desired font size in float.
  389. */
  390. void setBMFontSize(float fontSize);
  391. /**
  392. * Return the user define BMFont size.
  393. *
  394. * @return The BMFont size in float value.
  395. */
  396. float getBMFontSize()const;
  397. /**
  398. * Toggle wrap option of the label.
  399. * Note: System font doesn't support manually toggle wrap.
  400. *
  401. * @param enable Set true to enable wrap and false to disable wrap.
  402. */
  403. void enableWrap(bool enable);
  404. /**
  405. * Query the wrap is enabled or not.
  406. * Note: System font will always return true.
  407. */
  408. bool isWrapEnabled()const;
  409. /**
  410. * Change the label's Overflow type, currently only TTF and BMFont support all the valid Overflow type.
  411. * Char Map font supports all the Overflow type except for SHRINK, because we can't measure it's font size.
  412. * System font only support Overflow::Normal and Overflow::RESIZE_HEIGHT.
  413. *
  414. * @param overflow see `Overflow`
  415. */
  416. void setOverflow(Overflow overflow);
  417. /**
  418. * Query the label's Overflow type.
  419. *
  420. * @return see `Overflow`
  421. */
  422. Overflow getOverflow()const;
  423. /**
  424. * Makes the Label exactly this untransformed width.
  425. *
  426. * The Label's width be used for text align if the value not equal zero.
  427. */
  428. void setWidth(float width) { setDimensions(width,_labelHeight);}
  429. float getWidth() const { return _labelWidth; }
  430. /**
  431. * Makes the Label exactly this untransformed height.
  432. *
  433. * The Label's height be used for text align if the value not equal zero.
  434. * The text will display incomplete if the size of Label is not large enough to display all text.
  435. */
  436. void setHeight(float height){ setDimensions(_labelWidth, height); }
  437. float getHeight() const { return _labelHeight; }
  438. /** Sets the untransformed size of the Label in a more efficient way. */
  439. void setDimensions(float width, float height);
  440. const Size& getDimensions() const{ return _labelDimensions;}
  441. /** Update content immediately.*/
  442. virtual void updateContent();
  443. /**
  444. * Provides a way to treat each character like a Sprite.
  445. * @warning No support system font.
  446. */
  447. virtual Sprite * getLetter(int lettetIndex);
  448. /** Clips upper and lower margin to reduce height of Label.*/
  449. void setClipMarginEnabled(bool clipEnabled) { _clipEnabled = clipEnabled; }
  450. bool isClipMarginEnabled() const { return _clipEnabled; }
  451. /** Sets the line height of the Label.
  452. * @warning Not support system font.
  453. * @since v3.2.0
  454. */
  455. void setLineHeight(float height);
  456. /**
  457. * Returns the line height of this Label.
  458. * @warning Not support system font.
  459. * @since v3.2.0
  460. */
  461. float getLineHeight() const;
  462. void setLineSpacing(float height);
  463. float getLineSpacing() const;
  464. /**
  465. * Sets the additional kerning of the Label.
  466. *
  467. * @warning Not support system font.
  468. * @since v3.2.0
  469. */
  470. void setAdditionalKerning(float space);
  471. /**
  472. * Returns the additional kerning of the Label.
  473. *
  474. * @warning Not support system font.
  475. * @since v3.2.0
  476. */
  477. float getAdditionalKerning() const;
  478. FontAtlas* getFontAtlas() { return _fontAtlas; }
  479. virtual const BlendFunc& getBlendFunc() const override { return _blendFunc; }
  480. virtual void setBlendFunc(const BlendFunc &blendFunc) override;
  481. virtual bool isOpacityModifyRGB() const override { return _isOpacityModifyRGB; }
  482. virtual void setOpacityModifyRGB(bool isOpacityModifyRGB) override;
  483. virtual void updateDisplayedColor(const Color3B& parentColor) override;
  484. virtual void updateDisplayedOpacity(GLubyte parentOpacity) override;
  485. virtual std::string getDescription() const override;
  486. virtual const Size& getContentSize() const override;
  487. virtual Rect getBoundingBox() const override;
  488. virtual void visit(Renderer *renderer, const Mat4 &parentTransform, uint32_t parentFlags) override;
  489. virtual void draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) override;
  490. virtual void setCameraMask(unsigned short mask, bool applyChildren = true) override;
  491. virtual void removeAllChildrenWithCleanup(bool cleanup) override;
  492. virtual void removeChild(Node* child, bool cleanup = true) override;
  493. virtual void setGlobalZOrder(float globalZOrder) override;
  494. CC_DEPRECATED_ATTRIBUTE static Label* create(const std::string& text, const std::string& font, float fontSize,
  495. const Size& dimensions = Size::ZERO, TextHAlignment hAlignment = TextHAlignment::LEFT,
  496. TextVAlignment vAlignment = TextVAlignment::TOP);
  497. CC_DEPRECATED_ATTRIBUTE virtual void setFontDefinition(const FontDefinition& textDefinition);
  498. CC_DEPRECATED_ATTRIBUTE FontDefinition getFontDefinition() const { return _getFontDefinition(); }
  499. CC_DEPRECATED_ATTRIBUTE int getCommonLineHeight() const { return (int)getLineHeight();}
  500. CC_CONSTRUCTOR_ACCESS:
  501. /**
  502. * Constructor of Label.
  503. * @js NA
  504. */
  505. Label(TextHAlignment hAlignment = TextHAlignment::LEFT,
  506. TextVAlignment vAlignment = TextVAlignment::TOP);
  507. /**
  508. * Destructor of Label.
  509. * @js NA
  510. * @lua NA
  511. */
  512. virtual ~Label();
  513. bool initWithTTF(const std::string& text, const std::string& fontFilePath, float fontSize,
  514. const Size& dimensions = Size::ZERO, TextHAlignment hAlignment = TextHAlignment::LEFT,
  515. TextVAlignment vAlignment = TextVAlignment::TOP);
  516. bool initWithTTF(const TTFConfig& ttfConfig, const std::string& text,
  517. TextHAlignment hAlignment = TextHAlignment::LEFT, int maxLineWidth = 0);
  518. protected:
  519. struct LetterInfo
  520. {
  521. char32_t utf32Char;
  522. bool valid;
  523. float positionX;
  524. float positionY;
  525. int atlasIndex;
  526. int lineIndex;
  527. };
  528. enum class LabelType {
  529. TTF,
  530. BMFONT,
  531. CHARMAP,
  532. STRING_TEXTURE
  533. };
  534. virtual void setFontAtlas(FontAtlas* atlas, bool distanceFieldEnabled = false, bool useA8Shader = false);
  535. bool getFontLetterDef(char32_t character, FontLetterDefinition& letterDef) const;
  536. void computeStringNumLines();
  537. void onDraw(const Mat4& transform, bool transformUpdated);
  538. void onDrawShadow(GLProgram* glProgram, const Color4F& shadowColor);
  539. void drawSelf(bool visibleByCamera, Renderer* renderer, uint32_t flags);
  540. bool multilineTextWrapByChar();
  541. bool multilineTextWrapByWord();
  542. bool multilineTextWrap(const std::function<int(const std::u32string&, int, int)>& lambda);
  543. void shrinkLabelToContentSize(const std::function<bool(void)>& lambda);
  544. bool isHorizontalClamp();
  545. bool isVerticalClamp();
  546. float getRenderingFontSize()const;
  547. void rescaleWithOriginalFontSize();
  548. void updateLabelLetters();
  549. virtual bool alignText();
  550. void computeAlignmentOffset();
  551. bool computeHorizontalKernings(const std::u32string& stringToRender);
  552. void recordLetterInfo(const cocos2d::Vec2& point, char32_t utf32Char, int letterIndex, int lineIndex);
  553. void recordPlaceholderInfo(int letterIndex, char32_t utf16Char);
  554. bool updateQuads();
  555. void createSpriteForSystemFont(const FontDefinition& fontDef);
  556. void createShadowSpriteForSystemFont(const FontDefinition& fontDef);
  557. virtual void updateShaderProgram();
  558. void updateBMFontScale();
  559. void scaleFontSizeDown(float fontSize);
  560. bool setTTFConfigInternal(const TTFConfig& ttfConfig);
  561. void setBMFontSizeInternal(float fontSize);
  562. bool isHorizontalClamped(float letterPositionX, int lineIndex);
  563. void restoreFontSize();
  564. void updateLetterSpriteScale(Sprite* sprite);
  565. int getFirstCharLen(const std::u32string& utf32Text, int startIndex, int textLen) const;
  566. int getFirstWordLen(const std::u32string& utf32Text, int startIndex, int textLen) const;
  567. void reset();
  568. FontDefinition _getFontDefinition() const;
  569. virtual void updateColor() override;
  570. LabelType _currentLabelType;
  571. bool _contentDirty;
  572. std::u32string _utf32Text;
  573. std::string _utf8Text;
  574. int _numberOfLines;
  575. std::string _bmFontPath;
  576. TTFConfig _fontConfig;
  577. float _outlineSize;
  578. bool _systemFontDirty;
  579. std::string _systemFont;
  580. float _systemFontSize;
  581. Sprite* _textSprite;
  582. Sprite* _shadowNode;
  583. FontAtlas* _fontAtlas;
  584. Vector<SpriteBatchNode*> _batchNodes;
  585. std::vector<LetterInfo> _lettersInfo;
  586. //! used for optimization
  587. Sprite *_reusedLetter;
  588. Rect _reusedRect;
  589. int _lengthOfString;
  590. //layout relevant properties.
  591. float _lineHeight;
  592. float _lineSpacing;
  593. float _additionalKerning;
  594. int* _horizontalKernings;
  595. bool _lineBreakWithoutSpaces;
  596. float _maxLineWidth;
  597. Size _labelDimensions;
  598. float _labelWidth;
  599. float _labelHeight;
  600. TextHAlignment _hAlignment;
  601. TextVAlignment _vAlignment;
  602. float _textDesiredHeight;
  603. std::vector<float> _linesWidth;
  604. std::vector<float> _linesOffsetX;
  605. float _letterOffsetY;
  606. float _tailoredTopY;
  607. float _tailoredBottomY;
  608. LabelEffect _currLabelEffect;
  609. Color4F _effectColorF;
  610. Color4B _textColor;
  611. Color4F _textColorF;
  612. QuadCommand _quadCommand;
  613. CustomCommand _customCommand;
  614. Mat4 _shadowTransform;
  615. GLint _uniformEffectColor;
  616. GLint _uniformEffectType; // 0: None, 1: Outline, 2: Shadow; Only used when outline is enabled.
  617. GLint _uniformTextColor;
  618. bool _useDistanceField;
  619. bool _useA8Shader;
  620. bool _shadowDirty;
  621. bool _shadowEnabled;
  622. Size _shadowOffset;
  623. Color4F _shadowColor4F;
  624. Color3B _shadowColor3B;
  625. GLubyte _shadowOpacity;
  626. float _shadowBlurRadius;
  627. bool _clipEnabled;
  628. bool _blendFuncDirty;
  629. BlendFunc _blendFunc;
  630. /// whether or not the label was inside bounds the previous frame
  631. bool _insideBounds;
  632. bool _isOpacityModifyRGB;
  633. std::unordered_map<int, Sprite*> _letters;
  634. EventListenerCustom* _purgeTextureListener;
  635. EventListenerCustom* _resetTextureListener;
  636. #if CC_LABEL_DEBUG_DRAW
  637. DrawNode* _debugDrawNode;
  638. #endif
  639. bool _enableWrap;
  640. float _bmFontSize;
  641. float _bmfontScale;
  642. Overflow _overflow;
  643. float _originalFontSize;
  644. bool _boldEnabled;
  645. DrawNode* _underlineNode;
  646. bool _strikethroughEnabled;
  647. private:
  648. CC_DISALLOW_COPY_AND_ASSIGN(Label);
  649. };
  650. // end group
  651. /// @}
  652. NS_CC_END
  653. #endif /*__COCOS2D_CCLABEL_H */