1
0

UIRichText.cpp 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835
  1. /****************************************************************************
  2. Copyright (c) 2013 cocos2d-x.org
  3. http://www.cocos2d-x.org
  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. #include "ui/UIRichText.h"
  21. #include <sstream>
  22. #include <vector>
  23. #include <locale>
  24. #include "platform/CCFileUtils.h"
  25. #include "platform/CCApplication.h"
  26. #include "base/CCEventListenerTouch.h"
  27. #include "base/CCEventDispatcher.h"
  28. #include "base/CCDirector.h"
  29. #include "2d/CCLabel.h"
  30. #include "2d/CCSprite.h"
  31. #include "base/ccUTF8.h"
  32. #include "ui/UIHelper.h"
  33. #include "platform/CCSAXParser.h"
  34. USING_NS_CC;
  35. using namespace cocos2d::ui;
  36. class ListenerComponent : public Component
  37. {
  38. public:
  39. static const std::string COMPONENT_NAME; /*!< component name */
  40. static ListenerComponent* create(Node* parent, const std::string& url, const RichText::OpenUrlHandler handleOpenUrl = nullptr)
  41. {
  42. auto component = new (std::nothrow) ListenerComponent(parent, url, handleOpenUrl);
  43. component->autorelease();
  44. return component;
  45. }
  46. explicit ListenerComponent(Node* parent, const std::string& url, const RichText::OpenUrlHandler handleOpenUrl)
  47. : _parent(parent)
  48. , _url(url)
  49. , _handleOpenUrl(handleOpenUrl)
  50. {
  51. setName(ListenerComponent::COMPONENT_NAME);
  52. _touchListener = cocos2d::EventListenerTouchAllAtOnce::create();
  53. _touchListener->onTouchesEnded = CC_CALLBACK_2(ListenerComponent::onTouchesEnded, this);
  54. Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(_touchListener, _parent);
  55. _touchListener->retain();
  56. }
  57. virtual ~ListenerComponent()
  58. {
  59. Director::getInstance()->getEventDispatcher()->removeEventListener(_touchListener);
  60. _touchListener->release();
  61. }
  62. void onTouchesEnded(const std::vector<Touch*>& touches, Event* /*event*/)
  63. {
  64. for (const auto& touch: touches)
  65. {
  66. // FIXME: Node::getBoundBox() doesn't return it in local coordinates... so create one manually.
  67. Rect localRect = Rect(Vec2::ZERO, _parent->getContentSize());
  68. if (localRect.containsPoint(_parent->convertTouchToNodeSpace(touch))) {
  69. if (_handleOpenUrl) {
  70. _handleOpenUrl(_url);
  71. }
  72. }
  73. }
  74. }
  75. void setOpenUrlHandler(const RichText::OpenUrlHandler& handleOpenUrl)
  76. {
  77. _handleOpenUrl = handleOpenUrl;
  78. }
  79. private:
  80. Node* _parent; // weak ref.
  81. std::string _url;
  82. RichText::OpenUrlHandler _handleOpenUrl;
  83. EventDispatcher* _eventDispatcher; // weak ref.
  84. EventListenerTouchAllAtOnce* _touchListener; // strong ref.
  85. };
  86. const std::string ListenerComponent::COMPONENT_NAME("cocos2d_ui_UIRichText_ListenerComponent");
  87. bool RichElement::init(int tag, const Color3B &color, GLubyte opacity)
  88. {
  89. _tag = tag;
  90. _color = color;
  91. _opacity = opacity;
  92. return true;
  93. }
  94. bool RichElement::equalType(Type type)
  95. {
  96. return (_type == type);
  97. }
  98. void RichElement::setColor(const Color3B& color)
  99. {
  100. _color = color;
  101. }
  102. RichElementText* RichElementText::create(int tag, const Color3B &color, GLubyte opacity, const std::string& text,
  103. const std::string& fontName, float fontSize, uint32_t flags, const std::string& url,
  104. const Color3B& outlineColor, int outlineSize ,
  105. const Color3B& shadowColor, const cocos2d::Size& shadowOffset, int shadowBlurRadius,
  106. const Color3B& glowColor)
  107. {
  108. RichElementText* element = new (std::nothrow) RichElementText();
  109. if (element && element->init(tag, color, opacity, text, fontName, fontSize, flags, url,
  110. outlineColor, outlineSize, shadowColor, shadowOffset, shadowBlurRadius, glowColor))
  111. {
  112. element->autorelease();
  113. return element;
  114. }
  115. CC_SAFE_DELETE(element);
  116. return nullptr;
  117. }
  118. bool RichElementText::init(int tag, const Color3B &color, GLubyte opacity, const std::string& text,
  119. const std::string& fontName, float fontSize, uint32_t flags, const std::string& url,
  120. const Color3B& outlineColor, int outlineSize ,
  121. const Color3B& shadowColor, const cocos2d::Size& shadowOffset, int shadowBlurRadius,
  122. const Color3B& glowColor)
  123. {
  124. if (RichElement::init(tag, color, opacity))
  125. {
  126. _text = text;
  127. _fontName = fontName;
  128. _fontSize = fontSize;
  129. _flags = flags;
  130. _url = url;
  131. _outlineColor = outlineColor;
  132. _outlineSize = outlineSize;
  133. _shadowColor = shadowColor;
  134. _shadowOffset = shadowOffset;
  135. _shadowBlurRadius = shadowBlurRadius;
  136. _glowColor = glowColor;
  137. return true;
  138. }
  139. return false;
  140. }
  141. RichElementImage* RichElementImage::create(int tag, const Color3B &color, GLubyte opacity, const std::string& filePath, const std::string& url)
  142. {
  143. RichElementImage* element = new (std::nothrow) RichElementImage();
  144. if (element && element->init(tag, color, opacity, filePath, url))
  145. {
  146. element->autorelease();
  147. return element;
  148. }
  149. CC_SAFE_DELETE(element);
  150. return nullptr;
  151. }
  152. bool RichElementImage::init(int tag, const Color3B &color, GLubyte opacity, const std::string& filePath, const std::string& url)
  153. {
  154. if (RichElement::init(tag, color, opacity))
  155. {
  156. _filePath = filePath;
  157. _width = -1;
  158. _height = -1;
  159. _url = url;
  160. return true;
  161. }
  162. return false;
  163. }
  164. void RichElementImage::setWidth(int width)
  165. {
  166. _width = width;
  167. }
  168. void RichElementImage::setHeight(int height)
  169. {
  170. _height = height;
  171. }
  172. void RichElementImage::setUrl(const std::string& url)
  173. {
  174. _url = url;
  175. }
  176. RichElementCustomNode* RichElementCustomNode::create(int tag, const Color3B &color, GLubyte opacity, cocos2d::Node *customNode)
  177. {
  178. RichElementCustomNode* element = new (std::nothrow) RichElementCustomNode();
  179. if (element && element->init(tag, color, opacity, customNode))
  180. {
  181. element->autorelease();
  182. return element;
  183. }
  184. CC_SAFE_DELETE(element);
  185. return nullptr;
  186. }
  187. bool RichElementCustomNode::init(int tag, const Color3B &color, GLubyte opacity, cocos2d::Node *customNode)
  188. {
  189. if (RichElement::init(tag, color, opacity))
  190. {
  191. _customNode = customNode;
  192. _customNode->retain();
  193. return true;
  194. }
  195. return false;
  196. }
  197. RichElementNewLine* RichElementNewLine::create(int tag, const Color3B& color, GLubyte opacity)
  198. {
  199. RichElementNewLine* element = new (std::nothrow) RichElementNewLine();
  200. if (element && element->init(tag, color, opacity))
  201. {
  202. element->autorelease();
  203. return element;
  204. }
  205. CC_SAFE_DELETE(element);
  206. return nullptr;
  207. }
  208. /** @brief parse a XML. */
  209. class MyXMLVisitor : public SAXDelegator
  210. {
  211. public:
  212. /** @brief underline or strikethrough */
  213. enum class StyleLine {
  214. NONE,
  215. UNDERLINE, /*!< underline */
  216. STRIKETHROUGH /*!< a typographical presentation of words with a horizontal line through their center */
  217. };
  218. /** @brief outline, shadow or glow */
  219. enum class StyleEffect {
  220. NONE,
  221. OUTLINE, /*!< outline effect enabled */
  222. SHADOW, /*!< shadow effect enabled */
  223. GLOW /*!< glow effect enabled @discussion Limiting use to only when the Label created with true type font. */
  224. };
  225. /** @brief the attributes of text tag */
  226. struct Attributes
  227. {
  228. std::string face; /*!< font name */
  229. std::string url; /*!< url is a attribute of a anchor tag */
  230. float fontSize; /*!< font size */
  231. Color3B color; /*!< font color */
  232. bool hasColor; /*!< or color is specified? */
  233. bool bold; /*!< bold text */
  234. bool italics; /*!< italic text */
  235. StyleLine line; /*!< underline or strikethrough */
  236. StyleEffect effect; /*!< outline, shadow or glow */
  237. Color3B outlineColor; /*!< the color of the outline */
  238. int outlineSize; /*!< the outline effect size value */
  239. Color3B shadowColor; /*!< the shadow effect color value */
  240. cocos2d::Size shadowOffset; /*!< shadow effect offset value */
  241. int shadowBlurRadius; /*!< the shadow effect blur radius */
  242. Color3B glowColor; /*!< the glow effect color value */
  243. void setColor(const Color3B& acolor)
  244. {
  245. color = acolor;
  246. hasColor = true;
  247. }
  248. Attributes()
  249. : fontSize(-1)
  250. , hasColor(false)
  251. , bold(false)
  252. , italics(false)
  253. , line(StyleLine::NONE)
  254. , effect(StyleEffect::NONE)
  255. {
  256. }
  257. };
  258. private:
  259. std::vector<Attributes> _fontElements;
  260. RichText* _richText;
  261. struct TagBehavior {
  262. bool isFontElement;
  263. RichText::VisitEnterHandler handleVisitEnter;
  264. };
  265. typedef std::unordered_map<std::string, TagBehavior> TagTables;
  266. static TagTables _tagTables;
  267. public:
  268. explicit MyXMLVisitor(RichText* richText);
  269. virtual ~MyXMLVisitor();
  270. Color3B getColor() const;
  271. float getFontSize() const;
  272. std::string getFace() const;
  273. std::string getURL() const;
  274. bool getBold() const;
  275. bool getItalics() const;
  276. bool getUnderline() const;
  277. bool getStrikethrough() const;
  278. std::tuple<bool, Color3B, int> getOutline() const;
  279. std::tuple<bool, Color3B, cocos2d::Size, int> getShadow() const;
  280. std::tuple<bool, Color3B> getGlow() const;
  281. void startElement(void *ctx, const char *name, const char **atts) override;
  282. void endElement(void *ctx, const char *name) override;
  283. void textHandler(void *ctx, const char *s, size_t len) override;
  284. void pushBackFontElement(const Attributes& attribs);
  285. void popBackFontElement();
  286. void pushBackElement(RichElement* element);
  287. static void setTagDescription(const std::string& tag, bool isFontElement, RichText::VisitEnterHandler handleVisitEnter);
  288. static void removeTagDescription(const std::string& tag);
  289. private:
  290. ValueMap tagAttrMapWithXMLElement(const char ** attrs);
  291. };
  292. MyXMLVisitor::TagTables MyXMLVisitor::_tagTables;
  293. MyXMLVisitor::MyXMLVisitor(RichText* richText)
  294. : _fontElements(20)
  295. , _richText(richText)
  296. {
  297. MyXMLVisitor::setTagDescription("font", true, [](const ValueMap& tagAttrValueMap) {
  298. // supported attributes:
  299. // size, color, align, face
  300. ValueMap attrValueMap;
  301. if (tagAttrValueMap.find("size") != tagAttrValueMap.end()) {
  302. attrValueMap[RichText::KEY_FONT_SIZE] = tagAttrValueMap.at("size").asString();
  303. }
  304. if (tagAttrValueMap.find("color") != tagAttrValueMap.end()) {
  305. attrValueMap[RichText::KEY_FONT_COLOR_STRING] = tagAttrValueMap.at("color").asString();
  306. }
  307. if (tagAttrValueMap.find("face") != tagAttrValueMap.end()) {
  308. attrValueMap[RichText::KEY_FONT_FACE] = tagAttrValueMap.at("face").asString();
  309. }
  310. return make_pair(attrValueMap, nullptr);
  311. });
  312. MyXMLVisitor::setTagDescription("b", true, [](const ValueMap& /*tagAttrValueMap*/) {
  313. // no supported attributes
  314. ValueMap attrValueMap;
  315. attrValueMap[RichText::KEY_TEXT_BOLD] = true;
  316. return make_pair(attrValueMap, nullptr);
  317. });
  318. MyXMLVisitor::setTagDescription("i", true, [](const ValueMap& /*tagAttrValueMap*/) {
  319. // no supported attributes
  320. ValueMap attrValueMap;
  321. attrValueMap[RichText::KEY_TEXT_ITALIC] = true;
  322. return make_pair(attrValueMap, nullptr);
  323. });
  324. MyXMLVisitor::setTagDescription("del", true, [](const ValueMap& /*tagAttrValueMap*/) {
  325. // no supported attributes
  326. ValueMap attrValueMap;
  327. attrValueMap[RichText::KEY_TEXT_LINE] = RichText::VALUE_TEXT_LINE_DEL;
  328. return make_pair(attrValueMap, nullptr);
  329. });
  330. MyXMLVisitor::setTagDescription("u", true, [](const ValueMap& /*tagAttrValueMap*/) {
  331. // no supported attributes
  332. ValueMap attrValueMap;
  333. attrValueMap[RichText::KEY_TEXT_LINE] = RichText::VALUE_TEXT_LINE_UNDER;
  334. return make_pair(attrValueMap, nullptr);
  335. });
  336. MyXMLVisitor::setTagDescription("small", true, [](const ValueMap& /*tagAttrValueMap*/) {
  337. ValueMap attrValueMap;
  338. attrValueMap[RichText::KEY_FONT_SMALL] = true;
  339. return make_pair(attrValueMap, nullptr);
  340. });
  341. MyXMLVisitor::setTagDescription("big", true, [](const ValueMap& /*tagAttrValueMap*/) {
  342. ValueMap attrValueMap;
  343. attrValueMap[RichText::KEY_FONT_BIG] = true;
  344. return make_pair(attrValueMap, nullptr);
  345. });
  346. MyXMLVisitor::setTagDescription("img", false, [](const ValueMap& tagAttrValueMap) {
  347. // supported attributes:
  348. // src, height, width
  349. std::string src;
  350. int height = -1;
  351. int width = -1;
  352. if (tagAttrValueMap.find("src") != tagAttrValueMap.end()) {
  353. src = tagAttrValueMap.at("src").asString();
  354. }
  355. if (tagAttrValueMap.find("height") != tagAttrValueMap.end()) {
  356. height = tagAttrValueMap.at("height").asInt();
  357. }
  358. if (tagAttrValueMap.find("width") != tagAttrValueMap.end()) {
  359. width = tagAttrValueMap.at("width").asInt();
  360. }
  361. RichElementImage* elementImg = nullptr;
  362. if (src.length()) {
  363. elementImg = RichElementImage::create(0, Color3B::WHITE, 255, src);
  364. if (0 <= height) elementImg->setHeight(height);
  365. if (0 <= width) elementImg->setWidth(width);
  366. }
  367. return make_pair(ValueMap(), elementImg);
  368. });
  369. MyXMLVisitor::setTagDescription("a", true, [](const ValueMap& tagAttrValueMap) {
  370. // supported attributes:
  371. ValueMap attrValueMap;
  372. if (tagAttrValueMap.find("href") != tagAttrValueMap.end()) {
  373. attrValueMap[RichText::KEY_URL] = tagAttrValueMap.at("href").asString();
  374. }
  375. return make_pair(attrValueMap, nullptr);
  376. });
  377. MyXMLVisitor::setTagDescription("br", false, [](const ValueMap& /*tagAttrValueMap*/) {
  378. RichElementNewLine* richElement = RichElementNewLine::create(0, Color3B::WHITE, 255);
  379. return make_pair(ValueMap(), richElement);
  380. });
  381. MyXMLVisitor::setTagDescription("outline", true, [](const ValueMap& tagAttrValueMap) {
  382. // supported attributes:
  383. // color, size
  384. ValueMap attrValueMap;
  385. attrValueMap[RichText::KEY_TEXT_STYLE] = RichText::VALUE_TEXT_STYLE_OUTLINE;
  386. if (tagAttrValueMap.find("color") != tagAttrValueMap.end()) {
  387. attrValueMap[RichText::KEY_TEXT_OUTLINE_COLOR] = tagAttrValueMap.at("color").asString();
  388. }
  389. if (tagAttrValueMap.find("size") != tagAttrValueMap.end()) {
  390. attrValueMap[RichText::KEY_TEXT_OUTLINE_SIZE] = tagAttrValueMap.at("size").asString();
  391. }
  392. return make_pair(attrValueMap, nullptr);
  393. });
  394. MyXMLVisitor::setTagDescription("shadow", true, [](const ValueMap& tagAttrValueMap) {
  395. // supported attributes:
  396. // color, offsetWidth, offsetHeight, blurRadius
  397. ValueMap attrValueMap;
  398. attrValueMap[RichText::KEY_TEXT_STYLE] = RichText::VALUE_TEXT_STYLE_SHADOW;
  399. if (tagAttrValueMap.find("color") != tagAttrValueMap.end()) {
  400. attrValueMap[RichText::KEY_TEXT_SHADOW_COLOR] = tagAttrValueMap.at("color").asString();
  401. }
  402. if (tagAttrValueMap.find("offsetWidth") != tagAttrValueMap.end()) {
  403. attrValueMap[RichText::KEY_TEXT_SHADOW_OFFSET_WIDTH] = tagAttrValueMap.at("offsetWidth").asString();
  404. }
  405. if (tagAttrValueMap.find("offsetHeight") != tagAttrValueMap.end()) {
  406. attrValueMap[RichText::KEY_TEXT_SHADOW_OFFSET_HEIGHT] = tagAttrValueMap.at("offsetHeight").asString();
  407. }
  408. if (tagAttrValueMap.find("blurRadius") != tagAttrValueMap.end()) {
  409. attrValueMap[RichText::KEY_TEXT_SHADOW_BLUR_RADIUS] = tagAttrValueMap.at("blurRadius").asString();
  410. }
  411. return make_pair(attrValueMap, nullptr);
  412. });
  413. MyXMLVisitor::setTagDescription("glow", true, [](const ValueMap& tagAttrValueMap) {
  414. // supported attributes:
  415. // color
  416. ValueMap attrValueMap;
  417. attrValueMap[RichText::KEY_TEXT_STYLE] = RichText::VALUE_TEXT_STYLE_GLOW;
  418. if (tagAttrValueMap.find("color") != tagAttrValueMap.end()) {
  419. attrValueMap[RichText::KEY_TEXT_GLOW_COLOR] = tagAttrValueMap.at("color").asString();
  420. }
  421. return make_pair(attrValueMap, nullptr);
  422. });
  423. }
  424. MyXMLVisitor::~MyXMLVisitor()
  425. {
  426. }
  427. Color3B MyXMLVisitor::getColor() const
  428. {
  429. for (auto i = _fontElements.rbegin(), iRend = _fontElements.rend(); i != iRend; ++i)
  430. {
  431. if (i->hasColor)
  432. return i->color;
  433. }
  434. return Color3B::WHITE;
  435. }
  436. float MyXMLVisitor::getFontSize() const
  437. {
  438. for (auto i = _fontElements.rbegin(), iRend = _fontElements.rend(); i != iRend; ++i)
  439. {
  440. if (i->fontSize != -1)
  441. return i->fontSize;
  442. }
  443. return 12;
  444. }
  445. std::string MyXMLVisitor::getFace() const
  446. {
  447. for (auto i = _fontElements.rbegin(), iRend = _fontElements.rend(); i != iRend; ++i)
  448. {
  449. if (i->face.size() != 0)
  450. return i->face;
  451. }
  452. return "fonts/Marker Felt.ttf";
  453. }
  454. std::string MyXMLVisitor::getURL() const
  455. {
  456. for (auto i = _fontElements.rbegin(), iRend = _fontElements.rend(); i != iRend; ++i)
  457. {
  458. if (i->url.size() != 0)
  459. return i->url;
  460. }
  461. return "";
  462. }
  463. bool MyXMLVisitor::getBold() const
  464. {
  465. for (auto i = _fontElements.rbegin(), iRend = _fontElements.rend(); i != iRend; ++i)
  466. {
  467. if (i->bold)
  468. return true;
  469. }
  470. return false;
  471. }
  472. bool MyXMLVisitor::getItalics() const
  473. {
  474. for (auto i = _fontElements.rbegin(), iRend = _fontElements.rend(); i != iRend; ++i)
  475. {
  476. if (i->italics)
  477. return true;
  478. }
  479. return false;
  480. }
  481. bool MyXMLVisitor::getUnderline() const
  482. {
  483. for (auto i = _fontElements.rbegin(), iRend = _fontElements.rend(); i != iRend; ++i)
  484. {
  485. if (i->line == StyleLine::UNDERLINE)
  486. return true;
  487. }
  488. return false;
  489. }
  490. bool MyXMLVisitor::getStrikethrough() const
  491. {
  492. for (auto i = _fontElements.rbegin(), iRend = _fontElements.rend(); i != iRend; ++i)
  493. {
  494. if (i->line == StyleLine::STRIKETHROUGH)
  495. return true;
  496. }
  497. return false;
  498. }
  499. std::tuple<bool, Color3B, int> MyXMLVisitor::getOutline() const
  500. {
  501. for (auto i = _fontElements.rbegin(), iRend = _fontElements.rend(); i != iRend; ++i)
  502. {
  503. if (i->effect == StyleEffect::OUTLINE)
  504. return std::make_tuple(true, i->outlineColor, i->outlineSize);
  505. }
  506. return std::make_tuple(false, Color3B::WHITE, -1);
  507. }
  508. std::tuple<bool, Color3B, cocos2d::Size, int> MyXMLVisitor::getShadow() const
  509. {
  510. for (auto i = _fontElements.rbegin(), iRend = _fontElements.rend(); i != iRend; ++i)
  511. {
  512. if (i->effect == StyleEffect::SHADOW)
  513. return std::make_tuple(true, i->shadowColor, i->shadowOffset, i->shadowBlurRadius);
  514. }
  515. return std::make_tuple(false, Color3B::BLACK, Size(2.0, -2.0), 0);
  516. }
  517. std::tuple<bool, Color3B> MyXMLVisitor::getGlow() const
  518. {
  519. for (auto i = _fontElements.rbegin(), iRend = _fontElements.rend(); i != iRend; ++i)
  520. {
  521. if (i->effect == StyleEffect::GLOW)
  522. return std::make_tuple(true, i->glowColor);
  523. }
  524. return std::make_tuple(false, Color3B::WHITE);
  525. }
  526. void MyXMLVisitor::startElement(void* /*ctx*/, const char *elementName, const char **atts)
  527. {
  528. auto it = _tagTables.find(elementName);
  529. if (it != _tagTables.end()) {
  530. auto tagBehavior = it->second;
  531. if (tagBehavior.handleVisitEnter != nullptr) {
  532. ValueMap&& tagAttrValueMap = tagAttrMapWithXMLElement(atts);
  533. auto result = tagBehavior.handleVisitEnter(tagAttrValueMap);
  534. ValueMap& attrValueMap = result.first;
  535. RichElement* richElement = result.second;
  536. if (!attrValueMap.empty()) {
  537. Attributes attributes;
  538. if (attrValueMap.find(RichText::KEY_FONT_SIZE) != attrValueMap.end()) {
  539. attributes.fontSize = attrValueMap.at(RichText::KEY_FONT_SIZE).asFloat();
  540. }
  541. if (attrValueMap.find(RichText::KEY_FONT_SMALL) != attrValueMap.end()) {
  542. attributes.fontSize = getFontSize() * 0.8f;
  543. }
  544. if (attrValueMap.find(RichText::KEY_FONT_BIG) != attrValueMap.end()) {
  545. attributes.fontSize = getFontSize() * 1.25f;
  546. }
  547. if (attrValueMap.find(RichText::KEY_FONT_COLOR_STRING) != attrValueMap.end()) {
  548. attributes.setColor(_richText->color3BWithString(attrValueMap.at(RichText::KEY_FONT_COLOR_STRING).asString()));
  549. }
  550. if (attrValueMap.find(RichText::KEY_FONT_FACE) != attrValueMap.end()) {
  551. attributes.face = attrValueMap.at(RichText::KEY_FONT_FACE).asString();
  552. }
  553. if (attrValueMap.find(RichText::KEY_TEXT_BOLD) != attrValueMap.end()) {
  554. attributes.bold = true;
  555. }
  556. if (attrValueMap.find(RichText::KEY_TEXT_ITALIC) != attrValueMap.end()) {
  557. attributes.italics = true;
  558. }
  559. if (attrValueMap.find(RichText::KEY_TEXT_LINE) != attrValueMap.end()) {
  560. auto keyTextLine = attrValueMap.at(RichText::KEY_TEXT_LINE).asString();
  561. if (keyTextLine == RichText::VALUE_TEXT_LINE_DEL) {
  562. attributes.line = StyleLine::STRIKETHROUGH;
  563. }
  564. else if (keyTextLine == RichText::VALUE_TEXT_LINE_UNDER) {
  565. attributes.line = StyleLine::UNDERLINE;
  566. }
  567. }
  568. if (attrValueMap.find(RichText::KEY_URL) != attrValueMap.end()) {
  569. attributes.url = attrValueMap.at(RichText::KEY_URL).asString();
  570. attributes.setColor(_richText->getAnchorFontColor3B());
  571. if (_richText->isAnchorTextBoldEnabled()) {
  572. attributes.bold = true;
  573. }
  574. if (_richText->isAnchorTextItalicEnabled()) {
  575. attributes.italics = true;
  576. }
  577. if (_richText->isAnchorTextUnderlineEnabled()) {
  578. attributes.line = StyleLine::UNDERLINE;
  579. }
  580. if (_richText->isAnchorTextDelEnabled()) {
  581. attributes.line = StyleLine::STRIKETHROUGH;
  582. }
  583. if (_richText->isAnchorTextOutlineEnabled()) {
  584. attributes.effect = StyleEffect::OUTLINE;
  585. attributes.outlineColor = _richText->getAnchorTextOutlineColor3B();
  586. attributes.outlineSize = _richText->getAnchorTextOutlineSize();
  587. }
  588. if (_richText->isAnchorTextShadowEnabled()) {
  589. attributes.effect = StyleEffect::SHADOW;
  590. attributes.shadowColor = _richText->getAnchorTextShadowColor3B();
  591. attributes.shadowOffset = _richText->getAnchorTextShadowOffset();
  592. attributes.shadowBlurRadius = _richText->getAnchorTextShadowBlurRadius();
  593. }
  594. if (_richText->isAnchorTextGlowEnabled()) {
  595. attributes.effect = StyleEffect::GLOW;
  596. attributes.glowColor = _richText->getAnchorTextGlowColor3B();
  597. }
  598. }
  599. if (attrValueMap.find(RichText::KEY_TEXT_STYLE) != attrValueMap.end()) {
  600. auto keyTextStyle = attrValueMap.at(RichText::KEY_TEXT_STYLE).asString();
  601. if (keyTextStyle == RichText::VALUE_TEXT_STYLE_OUTLINE) {
  602. attributes.effect = StyleEffect::OUTLINE;
  603. if (attrValueMap.find(RichText::KEY_TEXT_OUTLINE_COLOR) != attrValueMap.end()) {
  604. attributes.outlineColor = _richText->color3BWithString(attrValueMap.at(RichText::KEY_TEXT_OUTLINE_COLOR).asString());
  605. }
  606. if (attrValueMap.find(RichText::KEY_TEXT_OUTLINE_SIZE) != attrValueMap.end()) {
  607. attributes.outlineSize = attrValueMap.at(RichText::KEY_TEXT_OUTLINE_SIZE).asInt();
  608. }
  609. }
  610. else if (keyTextStyle == RichText::VALUE_TEXT_STYLE_SHADOW) {
  611. attributes.effect = StyleEffect::SHADOW;
  612. if (attrValueMap.find(RichText::KEY_TEXT_SHADOW_COLOR) != attrValueMap.end()) {
  613. attributes.shadowColor = _richText->color3BWithString(attrValueMap.at(RichText::KEY_TEXT_SHADOW_COLOR).asString());
  614. }
  615. if ((attrValueMap.find(RichText::KEY_TEXT_SHADOW_OFFSET_WIDTH) != attrValueMap.end())
  616. && (attrValueMap.find(RichText::KEY_TEXT_SHADOW_OFFSET_HEIGHT) != attrValueMap.end())) {
  617. attributes.shadowOffset = Size(attrValueMap.at(RichText::KEY_TEXT_SHADOW_OFFSET_WIDTH).asFloat(),
  618. attrValueMap.at(RichText::KEY_TEXT_SHADOW_OFFSET_HEIGHT).asFloat());
  619. }
  620. if (attrValueMap.find(RichText::KEY_TEXT_SHADOW_BLUR_RADIUS) != attrValueMap.end()) {
  621. attributes.shadowBlurRadius = attrValueMap.at(RichText::KEY_TEXT_SHADOW_BLUR_RADIUS).asInt();
  622. }
  623. }
  624. else if (keyTextStyle == RichText::VALUE_TEXT_STYLE_GLOW) {
  625. attributes.effect = StyleEffect::GLOW;
  626. if (attrValueMap.find(RichText::KEY_TEXT_GLOW_COLOR) != attrValueMap.end()) {
  627. attributes.glowColor = _richText->color3BWithString(attrValueMap.at(RichText::KEY_TEXT_GLOW_COLOR).asString());
  628. }
  629. }
  630. }
  631. pushBackFontElement(attributes);
  632. }
  633. if (richElement) {
  634. if (richElement->equalType(RichElement::Type::IMAGE)) {
  635. richElement->setColor(getColor());
  636. auto* richElementImage = static_cast<RichElementImage*>(richElement);
  637. richElementImage->setUrl(getURL());
  638. }
  639. else if (richElement->equalType(RichElement::Type::NEWLINE)) {
  640. richElement->setColor(getColor());
  641. }
  642. pushBackElement(richElement);
  643. }
  644. }
  645. }
  646. }
  647. void MyXMLVisitor::endElement(void* /*ctx*/, const char *elementName)
  648. {
  649. auto it = _tagTables.find(elementName);
  650. if (it != _tagTables.end()) {
  651. auto tagBehavior = it->second;
  652. if (tagBehavior.isFontElement) {
  653. popBackFontElement();
  654. }
  655. }
  656. }
  657. void MyXMLVisitor::textHandler(void* /*ctx*/, const char *str, size_t len)
  658. {
  659. std::string text(str, len);
  660. auto color = getColor();
  661. auto face = getFace();
  662. auto fontSize = getFontSize();
  663. auto italics = getItalics();
  664. auto underline = getUnderline();
  665. auto strikethrough = getStrikethrough();
  666. auto bold = getBold();
  667. auto url = getURL();
  668. auto outline = getOutline();
  669. auto shadow = getShadow();
  670. auto glow = getGlow();
  671. uint32_t flags = 0;
  672. if (italics)
  673. flags |= RichElementText::ITALICS_FLAG;
  674. if (bold)
  675. flags |= RichElementText::BOLD_FLAG;
  676. if (underline)
  677. flags |= RichElementText::UNDERLINE_FLAG;
  678. if (strikethrough)
  679. flags |= RichElementText::STRIKETHROUGH_FLAG;
  680. if (url.size() > 0)
  681. flags |= RichElementText::URL_FLAG;
  682. if (std::get<0>(outline))
  683. flags |= RichElementText::OUTLINE_FLAG;
  684. if (std::get<0>(shadow))
  685. flags |= RichElementText::SHADOW_FLAG;
  686. if (std::get<0>(glow))
  687. flags |= RichElementText::GLOW_FLAG;
  688. auto element = RichElementText::create(0, color, 255, text, face, fontSize, flags, url,
  689. std::get<1>(outline), std::get<2>(outline),
  690. std::get<1>(shadow), std::get<2>(shadow), std::get<3>(shadow),
  691. std::get<1>(glow));
  692. _richText->pushBackElement(element);
  693. }
  694. void MyXMLVisitor::pushBackFontElement(const MyXMLVisitor::Attributes& attribs)
  695. {
  696. _fontElements.push_back(attribs);
  697. }
  698. void MyXMLVisitor::popBackFontElement()
  699. {
  700. _fontElements.pop_back();
  701. }
  702. void MyXMLVisitor::pushBackElement(RichElement* element)
  703. {
  704. _richText->pushBackElement(element);
  705. }
  706. void MyXMLVisitor::setTagDescription(const std::string& tag, bool isFontElement, RichText::VisitEnterHandler handleVisitEnter)
  707. {
  708. MyXMLVisitor::_tagTables[tag] = {isFontElement, handleVisitEnter};
  709. }
  710. void MyXMLVisitor::removeTagDescription(const std::string& tag)
  711. {
  712. MyXMLVisitor::_tagTables.erase(tag);
  713. }
  714. ValueMap MyXMLVisitor::tagAttrMapWithXMLElement(const char ** attrs)
  715. {
  716. ValueMap tagAttrValueMap;
  717. for (const char** attr = attrs; *attr != nullptr; attr = (attrs += 2)) {
  718. if (attr[0] && attr[1]) {
  719. tagAttrValueMap[attr[0]] = attr[1];
  720. }
  721. }
  722. return tagAttrValueMap;
  723. }
  724. const std::string RichText::KEY_VERTICAL_SPACE("KEY_VERTICAL_SPACE");
  725. const std::string RichText::KEY_WRAP_MODE("KEY_WRAP_MODE");
  726. const std::string RichText::KEY_FONT_COLOR_STRING("KEY_FONT_COLOR_STRING");
  727. const std::string RichText::KEY_FONT_SIZE("KEY_FONT_SIZE");
  728. const std::string RichText::KEY_FONT_SMALL("KEY_FONT_SMALL");
  729. const std::string RichText::KEY_FONT_BIG("KEY_FONT_BIG");
  730. const std::string RichText::KEY_FONT_FACE("KEY_FONT_FACE");
  731. const std::string RichText::KEY_TEXT_BOLD("KEY_TEXT_BOLD");
  732. const std::string RichText::KEY_TEXT_ITALIC("KEY_TEXT_ITALIC");
  733. const std::string RichText::KEY_TEXT_LINE("KEY_TEXT_LINE");
  734. const std::string RichText::VALUE_TEXT_LINE_NONE("VALUE_TEXT_LINE_NONE");
  735. const std::string RichText::VALUE_TEXT_LINE_DEL("VALUE_TEXT_LINE_DEL");
  736. const std::string RichText::VALUE_TEXT_LINE_UNDER("VALUE_TEXT_LINE_UNDER");
  737. const std::string RichText::KEY_TEXT_STYLE("KEY_TEXT_STYLE");
  738. const std::string RichText::VALUE_TEXT_STYLE_NONE("VALUE_TEXT_STYLE_NONE");
  739. const std::string RichText::VALUE_TEXT_STYLE_OUTLINE("VALUE_TEXT_STYLE_OUTLINE");
  740. const std::string RichText::VALUE_TEXT_STYLE_SHADOW("VALUE_TEXT_STYLE_SHADOW");
  741. const std::string RichText::VALUE_TEXT_STYLE_GLOW("VALUE_TEXT_STYLE_GLOW");
  742. const std::string RichText::KEY_TEXT_OUTLINE_COLOR("KEY_TEXT_OUTLINE_COLOR");
  743. const std::string RichText::KEY_TEXT_OUTLINE_SIZE("KEY_TEXT_OUTLINE_SIZE");
  744. const std::string RichText::KEY_TEXT_SHADOW_COLOR("KEY_TEXT_SHADOW_COLOR");
  745. const std::string RichText::KEY_TEXT_SHADOW_OFFSET_WIDTH("KEY_TEXT_SHADOW_OFFSET_WIDTH");
  746. const std::string RichText::KEY_TEXT_SHADOW_OFFSET_HEIGHT("KEY_TEXT_SHADOW_OFFSET_HEIGHT");
  747. const std::string RichText::KEY_TEXT_SHADOW_BLUR_RADIUS("KEY_TEXT_SHADOW_BLUR_RADIUS");
  748. const std::string RichText::KEY_TEXT_GLOW_COLOR("KEY_TEXT_GLOW_COLOR");
  749. const std::string RichText::KEY_URL("KEY_URL");
  750. const std::string RichText::KEY_ANCHOR_FONT_COLOR_STRING("KEY_ANCHOR_FONT_COLOR_STRING");
  751. const std::string RichText::KEY_ANCHOR_TEXT_BOLD("KEY_ANCHOR_TEXT_BOLD");
  752. const std::string RichText::KEY_ANCHOR_TEXT_ITALIC("KEY_ANCHOR_TEXT_ITALIC");
  753. const std::string RichText::KEY_ANCHOR_TEXT_LINE("KEY_ANCHOR_TEXT_LINE");
  754. const std::string RichText::KEY_ANCHOR_TEXT_STYLE("KEY_ANCHOR_TEXT_STYLE");
  755. const std::string RichText::KEY_ANCHOR_TEXT_OUTLINE_COLOR("KEY_ANCHOR_TEXT_OUTLINE_COLOR");
  756. const std::string RichText::KEY_ANCHOR_TEXT_OUTLINE_SIZE("KEY_ANCHOR_TEXT_OUTLINE_SIZE");
  757. const std::string RichText::KEY_ANCHOR_TEXT_SHADOW_COLOR("KEY_ANCHOR_TEXT_SHADOW_COLOR");
  758. const std::string RichText::KEY_ANCHOR_TEXT_SHADOW_OFFSET_WIDTH("KEY_ANCHOR_TEXT_SHADOW_OFFSET_WIDTH");
  759. const std::string RichText::KEY_ANCHOR_TEXT_SHADOW_OFFSET_HEIGHT("KEY_ANCHOR_TEXT_SHADOW_OFFSET_HEIGHT");
  760. const std::string RichText::KEY_ANCHOR_TEXT_SHADOW_BLUR_RADIUS("KEY_ANCHOR_TEXT_SHADOW_BLUR_RADIUS");
  761. const std::string RichText::KEY_ANCHOR_TEXT_GLOW_COLOR("KEY_ANCHOR_TEXT_GLOW_COLOR");
  762. RichText::RichText()
  763. : _formatTextDirty(true)
  764. , _leftSpaceWidth(0.0f)
  765. {
  766. _defaults[KEY_VERTICAL_SPACE] = 0.0f;
  767. _defaults[KEY_WRAP_MODE] = static_cast<int>(WrapMode::WRAP_PER_WORD);
  768. _defaults[KEY_FONT_COLOR_STRING] = "#ffffff";
  769. _defaults[KEY_FONT_SIZE] = 12.0f;
  770. _defaults[KEY_FONT_FACE] = "Verdana";
  771. _defaults[KEY_ANCHOR_FONT_COLOR_STRING] = "#0000FF";
  772. _defaults[KEY_ANCHOR_TEXT_BOLD] = false;
  773. _defaults[KEY_ANCHOR_TEXT_ITALIC] = false;
  774. _defaults[KEY_ANCHOR_TEXT_LINE] = VALUE_TEXT_LINE_NONE;
  775. _defaults[KEY_ANCHOR_TEXT_STYLE] = VALUE_TEXT_STYLE_NONE;
  776. }
  777. RichText::~RichText()
  778. {
  779. _richElements.clear();
  780. }
  781. RichText* RichText::create()
  782. {
  783. RichText* widget = new (std::nothrow) RichText();
  784. if (widget && widget->init())
  785. {
  786. widget->autorelease();
  787. return widget;
  788. }
  789. CC_SAFE_DELETE(widget);
  790. return nullptr;
  791. }
  792. RichText* RichText::createWithXML(const std::string& xml, const ValueMap& defaults, const OpenUrlHandler& handleOpenUrl)
  793. {
  794. RichText* widget = new (std::nothrow) RichText();
  795. if (widget && widget->initWithXML(xml, defaults, handleOpenUrl))
  796. {
  797. widget->autorelease();
  798. return widget;
  799. }
  800. CC_SAFE_DELETE(widget);
  801. return nullptr;
  802. }
  803. bool RichText::init()
  804. {
  805. if (Widget::init())
  806. {
  807. return true;
  808. }
  809. return false;
  810. }
  811. bool RichText::initWithXML(const std::string& origxml, const ValueMap& defaults, const OpenUrlHandler& handleOpenUrl)
  812. {
  813. static std::function<std::string(RichText*)> startTagFont = [](RichText* richText) {
  814. std::string fontFace = richText->getFontFace();
  815. std::stringstream ss;
  816. ss << richText->getFontSize();
  817. std::string fontSize = ss.str();
  818. std::string fontColor = richText->getFontColor();
  819. return "<font face=\"" + fontFace + "\" size=\"" + fontSize + "\" color=\"" + fontColor + "\">";
  820. };
  821. if (Widget::init())
  822. {
  823. setDefaults(defaults);
  824. setOpenUrlHandler(handleOpenUrl);
  825. // solves to issues:
  826. // - creates defaults values
  827. // - makes sure that the xml well formed and starts with an element
  828. std::string xml = startTagFont(this);
  829. xml += origxml;
  830. xml += "</font>";
  831. MyXMLVisitor visitor(this);
  832. SAXParser parser;
  833. parser.setDelegator(&visitor);
  834. return parser.parseIntrusive(&xml.front(), xml.length());
  835. }
  836. return false;
  837. }
  838. void RichText::initRenderer()
  839. {
  840. }
  841. void RichText::insertElement(RichElement *element, int index)
  842. {
  843. _richElements.insert(index, element);
  844. _formatTextDirty = true;
  845. }
  846. void RichText::pushBackElement(RichElement *element)
  847. {
  848. _richElements.pushBack(element);
  849. _formatTextDirty = true;
  850. }
  851. void RichText::removeElement(int index)
  852. {
  853. _richElements.erase(index);
  854. _formatTextDirty = true;
  855. }
  856. void RichText::removeElement(RichElement *element)
  857. {
  858. _richElements.eraseObject(element);
  859. _formatTextDirty = true;
  860. }
  861. RichText::WrapMode RichText::getWrapMode() const
  862. {
  863. return static_cast<RichText::WrapMode>(_defaults.at(KEY_WRAP_MODE).asInt());
  864. }
  865. void RichText::setWrapMode(RichText::WrapMode wrapMode)
  866. {
  867. if (static_cast<RichText::WrapMode>(_defaults.at(KEY_WRAP_MODE).asInt()) != wrapMode)
  868. {
  869. _defaults[KEY_WRAP_MODE] = static_cast<int>(wrapMode);
  870. _formatTextDirty = true;
  871. }
  872. }
  873. void RichText::setFontColor(const std::string& color)
  874. {
  875. _defaults[KEY_FONT_COLOR_STRING] = color;
  876. }
  877. std::string RichText::getFontColor()
  878. {
  879. return _defaults.at(KEY_FONT_COLOR_STRING).asString();
  880. }
  881. cocos2d::Color3B RichText::getFontColor3B()
  882. {
  883. return color3BWithString(getFontColor());
  884. }
  885. void RichText::setFontSize(float size)
  886. {
  887. _defaults[KEY_FONT_SIZE] = size;
  888. }
  889. float RichText::getFontSize()
  890. {
  891. return _defaults.at(KEY_FONT_SIZE).asFloat();
  892. }
  893. void RichText::setFontFace(const std::string& face)
  894. {
  895. _defaults[KEY_FONT_FACE] = face;
  896. }
  897. std::string RichText::getFontFace()
  898. {
  899. return _defaults.at(KEY_FONT_FACE).asString();
  900. }
  901. void RichText::setAnchorFontColor(const std::string& color)
  902. {
  903. _defaults[KEY_ANCHOR_FONT_COLOR_STRING] = color;
  904. }
  905. std::string RichText::getAnchorFontColor()
  906. {
  907. return _defaults.at(KEY_ANCHOR_FONT_COLOR_STRING).asString();
  908. }
  909. cocos2d::Color3B RichText::getAnchorFontColor3B()
  910. {
  911. return color3BWithString(getAnchorFontColor());
  912. }
  913. void RichText::setAnchorTextBold(bool enable)
  914. {
  915. _defaults[KEY_ANCHOR_TEXT_BOLD] = enable;
  916. }
  917. bool RichText::isAnchorTextBoldEnabled()
  918. {
  919. return _defaults[KEY_ANCHOR_TEXT_BOLD].asBool();
  920. }
  921. void RichText::setAnchorTextItalic(bool enable)
  922. {
  923. _defaults[KEY_ANCHOR_TEXT_ITALIC] = enable;
  924. }
  925. bool RichText::isAnchorTextItalicEnabled()
  926. {
  927. return _defaults[KEY_ANCHOR_TEXT_ITALIC].asBool();
  928. }
  929. void RichText::setAnchorTextDel(bool enable)
  930. {
  931. if (enable)
  932. _defaults[KEY_ANCHOR_TEXT_LINE] = VALUE_TEXT_LINE_DEL;
  933. else if (_defaults[KEY_ANCHOR_TEXT_LINE].asString() == VALUE_TEXT_LINE_DEL)
  934. _defaults[KEY_ANCHOR_TEXT_LINE] = VALUE_TEXT_LINE_NONE;
  935. }
  936. bool RichText::isAnchorTextDelEnabled()
  937. {
  938. return (_defaults[KEY_ANCHOR_TEXT_LINE].asString() == VALUE_TEXT_LINE_DEL);
  939. }
  940. void RichText::setAnchorTextUnderline(bool enable)
  941. {
  942. if (enable)
  943. _defaults[KEY_ANCHOR_TEXT_LINE] = VALUE_TEXT_LINE_UNDER;
  944. else if (_defaults[KEY_ANCHOR_TEXT_LINE].asString() == VALUE_TEXT_LINE_UNDER)
  945. _defaults[KEY_ANCHOR_TEXT_LINE] = VALUE_TEXT_LINE_NONE;
  946. }
  947. bool RichText::isAnchorTextUnderlineEnabled()
  948. {
  949. return (_defaults[KEY_ANCHOR_TEXT_LINE].asString() == VALUE_TEXT_LINE_UNDER);
  950. }
  951. void RichText::setAnchorTextOutline(bool enable, const Color3B& outlineColor, int outlineSize)
  952. {
  953. if (enable)
  954. _defaults[KEY_ANCHOR_TEXT_STYLE] = VALUE_TEXT_STYLE_OUTLINE;
  955. else if (_defaults[KEY_ANCHOR_TEXT_STYLE].asString() == VALUE_TEXT_STYLE_OUTLINE)
  956. _defaults[KEY_ANCHOR_TEXT_STYLE] = VALUE_TEXT_STYLE_NONE;
  957. _defaults[KEY_ANCHOR_TEXT_OUTLINE_COLOR] = stringWithColor3B(outlineColor);
  958. _defaults[KEY_ANCHOR_TEXT_OUTLINE_SIZE] = outlineSize;
  959. }
  960. bool RichText::isAnchorTextOutlineEnabled()
  961. {
  962. return (_defaults[KEY_ANCHOR_TEXT_STYLE].asString() == VALUE_TEXT_STYLE_OUTLINE);
  963. }
  964. Color3B RichText::getAnchorTextOutlineColor3B()
  965. {
  966. if (_defaults.find(KEY_ANCHOR_TEXT_OUTLINE_COLOR) != _defaults.end()) {
  967. return color3BWithString(_defaults.at(KEY_ANCHOR_TEXT_OUTLINE_COLOR).asString());
  968. }
  969. return Color3B();
  970. }
  971. int RichText::getAnchorTextOutlineSize()
  972. {
  973. if (_defaults.find(KEY_ANCHOR_TEXT_OUTLINE_SIZE) != _defaults.end()) {
  974. return _defaults.at(KEY_ANCHOR_TEXT_OUTLINE_SIZE).asInt();
  975. }
  976. return -1;
  977. }
  978. void RichText::setAnchorTextShadow(bool enable, const Color3B& shadowColor, const Size& offset, int blurRadius)
  979. {
  980. if (enable)
  981. _defaults[KEY_ANCHOR_TEXT_STYLE] = VALUE_TEXT_STYLE_SHADOW;
  982. else if (_defaults[KEY_ANCHOR_TEXT_STYLE].asString() == VALUE_TEXT_STYLE_SHADOW)
  983. _defaults[KEY_ANCHOR_TEXT_STYLE] = VALUE_TEXT_STYLE_NONE;
  984. _defaults[KEY_ANCHOR_TEXT_SHADOW_COLOR] = stringWithColor3B(shadowColor);
  985. _defaults[KEY_ANCHOR_TEXT_SHADOW_OFFSET_WIDTH] = offset.width;
  986. _defaults[KEY_ANCHOR_TEXT_SHADOW_OFFSET_HEIGHT] = offset.height;
  987. _defaults[KEY_ANCHOR_TEXT_SHADOW_BLUR_RADIUS] = blurRadius;
  988. }
  989. bool RichText::isAnchorTextShadowEnabled()
  990. {
  991. return (_defaults[KEY_ANCHOR_TEXT_STYLE].asString() == VALUE_TEXT_STYLE_SHADOW);
  992. }
  993. Color3B RichText::getAnchorTextShadowColor3B()
  994. {
  995. if (_defaults.find(KEY_ANCHOR_TEXT_SHADOW_COLOR) != _defaults.end()) {
  996. return color3BWithString(_defaults.at(KEY_ANCHOR_TEXT_SHADOW_COLOR).asString());
  997. }
  998. return Color3B();
  999. }
  1000. Size RichText::getAnchorTextShadowOffset()
  1001. {
  1002. float width = 2.0f;
  1003. float height = -2.0f;
  1004. if (_defaults.find(KEY_ANCHOR_TEXT_SHADOW_OFFSET_WIDTH) != _defaults.end()) {
  1005. width = _defaults.at(KEY_ANCHOR_TEXT_SHADOW_OFFSET_WIDTH).asFloat();
  1006. }
  1007. if (_defaults.find(KEY_ANCHOR_TEXT_SHADOW_OFFSET_HEIGHT) != _defaults.end()) {
  1008. height = _defaults.at(KEY_ANCHOR_TEXT_SHADOW_OFFSET_HEIGHT).asFloat();
  1009. }
  1010. return Size(width, height);
  1011. }
  1012. int RichText::getAnchorTextShadowBlurRadius()
  1013. {
  1014. if (_defaults.find(KEY_ANCHOR_TEXT_SHADOW_BLUR_RADIUS) != _defaults.end()) {
  1015. return _defaults.at(KEY_ANCHOR_TEXT_SHADOW_BLUR_RADIUS).asInt();
  1016. }
  1017. return 0;
  1018. }
  1019. void RichText::setAnchorTextGlow(bool enable, const Color3B& glowColor)
  1020. {
  1021. if (enable)
  1022. _defaults[KEY_ANCHOR_TEXT_STYLE] = VALUE_TEXT_STYLE_GLOW;
  1023. else if (_defaults[KEY_ANCHOR_TEXT_STYLE].asString() == VALUE_TEXT_STYLE_GLOW)
  1024. _defaults[KEY_ANCHOR_TEXT_STYLE] = VALUE_TEXT_STYLE_NONE;
  1025. _defaults[KEY_ANCHOR_TEXT_GLOW_COLOR] = stringWithColor3B(glowColor);
  1026. }
  1027. bool RichText::isAnchorTextGlowEnabled()
  1028. {
  1029. return (_defaults[KEY_ANCHOR_TEXT_STYLE].asString() == VALUE_TEXT_STYLE_GLOW);
  1030. }
  1031. Color3B RichText::getAnchorTextGlowColor3B()
  1032. {
  1033. if (_defaults.find(KEY_ANCHOR_TEXT_GLOW_COLOR) != _defaults.end()) {
  1034. return color3BWithString(_defaults.at(KEY_ANCHOR_TEXT_GLOW_COLOR).asString());
  1035. }
  1036. return Color3B();
  1037. }
  1038. void RichText::setDefaults(const ValueMap& defaults)
  1039. {
  1040. if (defaults.find(KEY_VERTICAL_SPACE) != defaults.end()) {
  1041. _defaults[KEY_VERTICAL_SPACE] = defaults.at(KEY_VERTICAL_SPACE).asFloat();
  1042. }
  1043. if (defaults.find(KEY_WRAP_MODE) != defaults.end()) {
  1044. _defaults[KEY_WRAP_MODE] = defaults.at(KEY_WRAP_MODE).asInt();
  1045. }
  1046. if (defaults.find(KEY_FONT_COLOR_STRING) != defaults.end()) {
  1047. _defaults[KEY_FONT_COLOR_STRING] = defaults.at(KEY_FONT_COLOR_STRING).asString();
  1048. }
  1049. if (defaults.find(KEY_FONT_SIZE) != defaults.end()) {
  1050. _defaults[KEY_FONT_SIZE] = defaults.at(KEY_FONT_SIZE).asFloat();
  1051. }
  1052. if (defaults.find(KEY_FONT_FACE) != defaults.end()) {
  1053. _defaults[KEY_FONT_FACE] = defaults.at(KEY_FONT_FACE).asString();
  1054. }
  1055. if (defaults.find(KEY_ANCHOR_FONT_COLOR_STRING) != defaults.end()) {
  1056. _defaults[KEY_ANCHOR_FONT_COLOR_STRING] = defaults.at(KEY_ANCHOR_FONT_COLOR_STRING).asString();
  1057. }
  1058. if (defaults.find(KEY_ANCHOR_TEXT_BOLD) != defaults.end()) {
  1059. _defaults[KEY_ANCHOR_TEXT_BOLD] = defaults.at(KEY_ANCHOR_TEXT_BOLD).asBool();
  1060. }
  1061. if (defaults.find(KEY_ANCHOR_TEXT_ITALIC) != defaults.end()) {
  1062. _defaults[KEY_ANCHOR_TEXT_ITALIC] = defaults.at(KEY_ANCHOR_TEXT_ITALIC).asBool();
  1063. }
  1064. if (defaults.find(KEY_ANCHOR_TEXT_LINE) != defaults.end()) {
  1065. _defaults[KEY_ANCHOR_TEXT_LINE] = defaults.at(KEY_ANCHOR_TEXT_LINE).asString();
  1066. }
  1067. if (defaults.find(KEY_ANCHOR_TEXT_STYLE) != defaults.end()) {
  1068. _defaults[KEY_ANCHOR_TEXT_STYLE] = defaults.at(KEY_ANCHOR_TEXT_STYLE).asString();
  1069. }
  1070. if (defaults.find(KEY_ANCHOR_TEXT_OUTLINE_COLOR) != defaults.end()) {
  1071. _defaults[KEY_ANCHOR_TEXT_OUTLINE_COLOR] = defaults.at(KEY_ANCHOR_TEXT_OUTLINE_COLOR).asString();
  1072. }
  1073. if (defaults.find(KEY_ANCHOR_TEXT_OUTLINE_SIZE) != defaults.end()) {
  1074. _defaults[KEY_ANCHOR_TEXT_OUTLINE_SIZE] = defaults.at(KEY_ANCHOR_TEXT_OUTLINE_SIZE).asInt();
  1075. }
  1076. if (defaults.find(KEY_ANCHOR_TEXT_SHADOW_COLOR) != defaults.end()) {
  1077. _defaults[KEY_ANCHOR_TEXT_SHADOW_COLOR] = defaults.at(KEY_ANCHOR_TEXT_SHADOW_COLOR).asString();
  1078. }
  1079. if (defaults.find(KEY_ANCHOR_TEXT_SHADOW_OFFSET_WIDTH) != defaults.end()) {
  1080. _defaults[KEY_ANCHOR_TEXT_SHADOW_OFFSET_WIDTH] = defaults.at(KEY_ANCHOR_TEXT_SHADOW_OFFSET_WIDTH).asFloat();
  1081. }
  1082. if (defaults.find(KEY_ANCHOR_TEXT_SHADOW_OFFSET_HEIGHT) != defaults.end()) {
  1083. _defaults[KEY_ANCHOR_TEXT_SHADOW_OFFSET_HEIGHT] = defaults.at(KEY_ANCHOR_TEXT_SHADOW_OFFSET_HEIGHT).asFloat();
  1084. }
  1085. if (defaults.find(KEY_ANCHOR_TEXT_SHADOW_BLUR_RADIUS) != defaults.end()) {
  1086. _defaults[KEY_ANCHOR_TEXT_SHADOW_BLUR_RADIUS] = defaults.at(KEY_ANCHOR_TEXT_SHADOW_BLUR_RADIUS).asInt();
  1087. }
  1088. if (defaults.find(KEY_ANCHOR_TEXT_GLOW_COLOR) != defaults.end()) {
  1089. _defaults[KEY_ANCHOR_TEXT_GLOW_COLOR] = defaults.at(KEY_ANCHOR_TEXT_GLOW_COLOR).asString();
  1090. }
  1091. }
  1092. ValueMap RichText::getDefaults() const
  1093. {
  1094. ValueMap defaults;
  1095. return defaults;
  1096. }
  1097. cocos2d::Color3B RichText::color3BWithString(const std::string& color)
  1098. {
  1099. if (color.length() == 4) {
  1100. int r, g, b;
  1101. sscanf(color.c_str(), "%*c%1x%1x%1x", &r, &g, &b);
  1102. r += r * 16;
  1103. g += g * 16;
  1104. b += b * 16;
  1105. return Color3B(r, g, b);
  1106. }
  1107. else if (color.length() == 7) {
  1108. int r, g, b;
  1109. sscanf(color.c_str(), "%*c%2x%2x%2x", &r, &g, &b);
  1110. return Color3B(r, g, b);
  1111. }
  1112. else if (color.length() == 9) {
  1113. int r, g, b, a;
  1114. sscanf(color.c_str(), "%*c%2x%2x%2x%2x", &r, &g, &b, &a);
  1115. return Color3B(r, g, b);
  1116. }
  1117. return Color3B::WHITE;
  1118. }
  1119. std::string RichText::stringWithColor3B(const cocos2d::Color3B& color3b)
  1120. {
  1121. int r = color3b.r;
  1122. int g = color3b.g;
  1123. int b = color3b.b;
  1124. char buf[8];
  1125. snprintf(buf, sizeof(buf), "#%02x%02x%02x", r, g, b);
  1126. return std::string(buf, 7);
  1127. }
  1128. std::string RichText::stringWithColor4B(const cocos2d::Color4B& color4b)
  1129. {
  1130. int r = color4b.r;
  1131. int g = color4b.g;
  1132. int b = color4b.b;
  1133. int a = color4b.a;
  1134. char buf[10];
  1135. snprintf(buf, sizeof(buf), "#%02x%02x%02x%02x", r, g, b, a);
  1136. return std::string(buf, 9);
  1137. }
  1138. void RichText::setTagDescription(const std::string& tag, bool isFontElement, VisitEnterHandler handleVisitEnter)
  1139. {
  1140. MyXMLVisitor::setTagDescription(tag, isFontElement, handleVisitEnter);
  1141. }
  1142. void RichText::removeTagDescription(const std::string& tag)
  1143. {
  1144. MyXMLVisitor::removeTagDescription(tag);
  1145. }
  1146. void RichText::openUrl(const std::string& url)
  1147. {
  1148. if (_handleOpenUrl) {
  1149. _handleOpenUrl(url);
  1150. }
  1151. else {
  1152. Application::getInstance()->openURL(url);
  1153. }
  1154. }
  1155. void RichText::setOpenUrlHandler(const OpenUrlHandler& handleOpenUrl)
  1156. {
  1157. _handleOpenUrl = handleOpenUrl;
  1158. }
  1159. void RichText::formatText()
  1160. {
  1161. if (_formatTextDirty)
  1162. {
  1163. this->removeAllProtectedChildren();
  1164. _elementRenders.clear();
  1165. if (_ignoreSize)
  1166. {
  1167. addNewLine();
  1168. for (ssize_t i=0, size = _richElements.size(); i<size; ++i)
  1169. {
  1170. RichElement* element = _richElements.at(i);
  1171. Node* elementRenderer = nullptr;
  1172. switch (element->_type)
  1173. {
  1174. case RichElement::Type::TEXT:
  1175. {
  1176. RichElementText* elmtText = static_cast<RichElementText*>(element);
  1177. Label* label;
  1178. if (FileUtils::getInstance()->isFileExist(elmtText->_fontName))
  1179. {
  1180. label = Label::createWithTTF(elmtText->_text, elmtText->_fontName, elmtText->_fontSize);
  1181. }
  1182. else
  1183. {
  1184. label = Label::createWithSystemFont(elmtText->_text, elmtText->_fontName, elmtText->_fontSize);
  1185. }
  1186. if (elmtText->_flags & RichElementText::ITALICS_FLAG)
  1187. label->enableItalics();
  1188. if (elmtText->_flags & RichElementText::BOLD_FLAG)
  1189. label->enableBold();
  1190. if (elmtText->_flags & RichElementText::UNDERLINE_FLAG)
  1191. label->enableUnderline();
  1192. if (elmtText->_flags & RichElementText::STRIKETHROUGH_FLAG)
  1193. label->enableStrikethrough();
  1194. if (elmtText->_flags & RichElementText::URL_FLAG)
  1195. label->addComponent(ListenerComponent::create(label, elmtText->_url,
  1196. std::bind(&RichText::openUrl, this, std::placeholders::_1)));
  1197. if (elmtText->_flags & RichElementText::OUTLINE_FLAG) {
  1198. label->enableOutline(Color4B(elmtText->_outlineColor), elmtText->_outlineSize);
  1199. }
  1200. if (elmtText->_flags & RichElementText::SHADOW_FLAG) {
  1201. label->enableShadow(Color4B(elmtText->_shadowColor),
  1202. elmtText->_shadowOffset,
  1203. elmtText->_shadowBlurRadius);
  1204. }
  1205. if (elmtText->_flags & RichElementText::GLOW_FLAG) {
  1206. label->enableGlow(Color4B(elmtText->_glowColor));
  1207. }
  1208. elementRenderer = label;
  1209. break;
  1210. }
  1211. case RichElement::Type::IMAGE:
  1212. {
  1213. RichElementImage* elmtImage = static_cast<RichElementImage*>(element);
  1214. elementRenderer = Sprite::create(elmtImage->_filePath);
  1215. if (elementRenderer && (elmtImage->_height != -1 || elmtImage->_width != -1))
  1216. {
  1217. auto currentSize = elementRenderer->getContentSize();
  1218. if (elmtImage->_width != -1)
  1219. elementRenderer->setScaleX(elmtImage->_width / currentSize.width);
  1220. if (elmtImage->_height != -1)
  1221. elementRenderer->setScaleY(elmtImage->_height / currentSize.height);
  1222. elementRenderer->setContentSize(Size(currentSize.width * elementRenderer->getScaleX(),
  1223. currentSize.height * elementRenderer->getScaleY()));
  1224. elementRenderer->addComponent(ListenerComponent::create(elementRenderer,
  1225. elmtImage->_url,
  1226. std::bind(&RichText::openUrl, this, std::placeholders::_1)));
  1227. }
  1228. break;
  1229. }
  1230. case RichElement::Type::CUSTOM:
  1231. {
  1232. RichElementCustomNode* elmtCustom = static_cast<RichElementCustomNode*>(element);
  1233. elementRenderer = elmtCustom->_customNode;
  1234. break;
  1235. }
  1236. case RichElement::Type::NEWLINE:
  1237. {
  1238. addNewLine();
  1239. break;
  1240. }
  1241. default:
  1242. break;
  1243. }
  1244. if (elementRenderer)
  1245. {
  1246. elementRenderer->setColor(element->_color);
  1247. elementRenderer->setOpacity(element->_opacity);
  1248. pushToContainer(elementRenderer);
  1249. }
  1250. }
  1251. }
  1252. else
  1253. {
  1254. addNewLine();
  1255. for (ssize_t i=0, size = _richElements.size(); i<size; ++i)
  1256. {
  1257. RichElement* element = static_cast<RichElement*>(_richElements.at(i));
  1258. switch (element->_type)
  1259. {
  1260. case RichElement::Type::TEXT:
  1261. {
  1262. RichElementText* elmtText = static_cast<RichElementText*>(element);
  1263. handleTextRenderer(elmtText->_text, elmtText->_fontName, elmtText->_fontSize, elmtText->_color,
  1264. elmtText->_opacity, elmtText->_flags, elmtText->_url,
  1265. elmtText->_outlineColor, elmtText->_outlineSize,
  1266. elmtText->_shadowColor, elmtText->_shadowOffset, elmtText->_shadowBlurRadius,
  1267. elmtText->_glowColor);
  1268. break;
  1269. }
  1270. case RichElement::Type::IMAGE:
  1271. {
  1272. RichElementImage* elmtImage = static_cast<RichElementImage*>(element);
  1273. handleImageRenderer(elmtImage->_filePath, elmtImage->_color, elmtImage->_opacity, elmtImage->_width, elmtImage->_height, elmtImage->_url);
  1274. break;
  1275. }
  1276. case RichElement::Type::CUSTOM:
  1277. {
  1278. RichElementCustomNode* elmtCustom = static_cast<RichElementCustomNode*>(element);
  1279. handleCustomRenderer(elmtCustom->_customNode);
  1280. break;
  1281. }
  1282. case RichElement::Type::NEWLINE:
  1283. {
  1284. addNewLine();
  1285. break;
  1286. }
  1287. default:
  1288. break;
  1289. }
  1290. }
  1291. }
  1292. formarRenderers();
  1293. _formatTextDirty = false;
  1294. }
  1295. }
  1296. static int getPrevWord(const std::string& text, int idx)
  1297. {
  1298. // start from idx-1
  1299. for (int i=idx-1; i>=0; --i)
  1300. {
  1301. if (!std::isalnum(text[i], std::locale()))
  1302. return i;
  1303. }
  1304. return -1;
  1305. }
  1306. static bool isWrappable(const std::string& text)
  1307. {
  1308. for (size_t i = 0, size = text.length(); i < size; ++i)
  1309. {
  1310. if (!std::isalnum(text[i], std::locale()))
  1311. return true;
  1312. }
  1313. return false;
  1314. }
  1315. int RichText::findSplitPositionForWord(cocos2d::Label* label, const std::string& text)
  1316. {
  1317. auto originalLeftSpaceWidth = _leftSpaceWidth + label->getContentSize().width;
  1318. bool startingNewLine = (_customSize.width == originalLeftSpaceWidth);
  1319. if (!isWrappable(text))
  1320. {
  1321. if (startingNewLine)
  1322. return (int) text.length();
  1323. return 0;
  1324. }
  1325. for(int idx = (int)text.size()-1; idx >=0; )
  1326. {
  1327. int newidx = getPrevWord(text, idx);
  1328. if (newidx >=0)
  1329. {
  1330. idx = newidx;
  1331. auto leftStr = Helper::getSubStringOfUTF8String(text, 0, idx);
  1332. label->setString(leftStr);
  1333. if (label->getContentSize().width <= originalLeftSpaceWidth)
  1334. return idx;
  1335. }
  1336. else
  1337. {
  1338. if (startingNewLine)
  1339. return idx;
  1340. return 0;
  1341. }
  1342. }
  1343. // no spaces... return the original label + size
  1344. label->setString(text);
  1345. return (int)text.size();
  1346. }
  1347. int RichText::findSplitPositionForChar(cocos2d::Label* label, const std::string& text)
  1348. {
  1349. float textRendererWidth = label->getContentSize().width;
  1350. float overstepPercent = (-_leftSpaceWidth) / textRendererWidth;
  1351. std::string curText = text;
  1352. size_t stringLength = StringUtils::getCharacterCountInUTF8String(text);
  1353. // rough estimate
  1354. int leftLength = stringLength * (1.0f - overstepPercent);
  1355. // The adjustment of the new line position
  1356. auto originalLeftSpaceWidth = _leftSpaceWidth + textRendererWidth;
  1357. auto leftStr = Helper::getSubStringOfUTF8String(curText, 0, leftLength);
  1358. label->setString(leftStr);
  1359. auto leftWidth = label->getContentSize().width;
  1360. if (originalLeftSpaceWidth < leftWidth) {
  1361. // Have protruding
  1362. for (;;) {
  1363. leftLength--;
  1364. leftStr = Helper::getSubStringOfUTF8String(curText, 0, leftLength);
  1365. label->setString(leftStr);
  1366. leftWidth = label->getContentSize().width;
  1367. if (leftWidth <= originalLeftSpaceWidth) {
  1368. break;
  1369. }
  1370. else if (leftLength <= 0) {
  1371. break;
  1372. }
  1373. }
  1374. }
  1375. else if (leftWidth < originalLeftSpaceWidth) {
  1376. // A wide margin
  1377. for (;;) {
  1378. leftLength++;
  1379. leftStr = Helper::getSubStringOfUTF8String(curText, 0, leftLength);
  1380. label->setString(leftStr);
  1381. leftWidth = label->getContentSize().width;
  1382. if (originalLeftSpaceWidth < leftWidth) {
  1383. leftLength--;
  1384. break;
  1385. }
  1386. else if (static_cast<int>(stringLength) <= leftLength) {
  1387. break;
  1388. }
  1389. }
  1390. }
  1391. if (leftLength < 0)
  1392. leftLength = (int)text.size()-1;
  1393. return leftLength;
  1394. }
  1395. void RichText::handleTextRenderer(const std::string& text, const std::string& fontName, float fontSize, const Color3B &color,
  1396. GLubyte opacity, uint32_t flags, const std::string& url,
  1397. const Color3B& outlineColor, int outlineSize ,
  1398. const Color3B& shadowColor, const cocos2d::Size& shadowOffset, int shadowBlurRadius,
  1399. const Color3B& glowColor)
  1400. {
  1401. auto fileExist = FileUtils::getInstance()->isFileExist(fontName);
  1402. Label* textRenderer = nullptr;
  1403. if (fileExist)
  1404. {
  1405. textRenderer = Label::createWithTTF(text, fontName, fontSize);
  1406. }
  1407. else
  1408. {
  1409. textRenderer = Label::createWithSystemFont(text, fontName, fontSize);
  1410. }
  1411. if (flags & RichElementText::ITALICS_FLAG)
  1412. textRenderer->enableItalics();
  1413. if (flags & RichElementText::BOLD_FLAG)
  1414. textRenderer->enableBold();
  1415. if (flags & RichElementText::UNDERLINE_FLAG)
  1416. textRenderer->enableUnderline();
  1417. if (flags & RichElementText::STRIKETHROUGH_FLAG)
  1418. textRenderer->enableStrikethrough();
  1419. if (flags & RichElementText::URL_FLAG)
  1420. textRenderer->addComponent(ListenerComponent::create(textRenderer,
  1421. url,
  1422. std::bind(&RichText::openUrl, this, std::placeholders::_1)));
  1423. if (flags & RichElementText::OUTLINE_FLAG) {
  1424. textRenderer->enableOutline(Color4B(outlineColor), outlineSize);
  1425. }
  1426. if (flags & RichElementText::SHADOW_FLAG) {
  1427. textRenderer->enableShadow(Color4B(shadowColor), shadowOffset, shadowBlurRadius);
  1428. }
  1429. if (flags & RichElementText::GLOW_FLAG) {
  1430. textRenderer->enableGlow(Color4B(glowColor));
  1431. }
  1432. float textRendererWidth = textRenderer->getContentSize().width;
  1433. _leftSpaceWidth -= textRendererWidth;
  1434. if (_leftSpaceWidth < 0.0f)
  1435. {
  1436. int leftLength = 0;
  1437. if (static_cast<RichText::WrapMode>(_defaults.at(KEY_WRAP_MODE).asInt()) == WRAP_PER_WORD)
  1438. leftLength = findSplitPositionForWord(textRenderer, text);
  1439. else
  1440. leftLength = findSplitPositionForChar(textRenderer, text);
  1441. //The minimum cut length is 1, otherwise will cause the infinite loop.
  1442. // if (0 == leftLength) leftLength = 1;
  1443. std::string leftWords = Helper::getSubStringOfUTF8String(text, 0, leftLength);
  1444. int rightStart = leftLength;
  1445. if (std::isspace(text[rightStart], std::locale()))
  1446. rightStart++;
  1447. std::string cutWords = Helper::getSubStringOfUTF8String(text, rightStart, text.length() - leftLength);
  1448. if (leftLength > 0)
  1449. {
  1450. Label* leftRenderer = nullptr;
  1451. if (fileExist)
  1452. {
  1453. leftRenderer = Label::createWithTTF(Helper::getSubStringOfUTF8String(leftWords, 0, leftLength), fontName, fontSize);
  1454. }
  1455. else
  1456. {
  1457. leftRenderer = Label::createWithSystemFont(Helper::getSubStringOfUTF8String(leftWords, 0, leftLength), fontName, fontSize);
  1458. }
  1459. if (leftRenderer)
  1460. {
  1461. leftRenderer->setColor(color);
  1462. leftRenderer->setOpacity(opacity);
  1463. pushToContainer(leftRenderer);
  1464. if (flags & RichElementText::ITALICS_FLAG)
  1465. leftRenderer->enableItalics();
  1466. if (flags & RichElementText::BOLD_FLAG)
  1467. leftRenderer->enableBold();
  1468. if (flags & RichElementText::UNDERLINE_FLAG)
  1469. leftRenderer->enableUnderline();
  1470. if (flags & RichElementText::STRIKETHROUGH_FLAG)
  1471. leftRenderer->enableStrikethrough();
  1472. if (flags & RichElementText::URL_FLAG)
  1473. leftRenderer->addComponent(ListenerComponent::create(leftRenderer,
  1474. url,
  1475. std::bind(&RichText::openUrl, this, std::placeholders::_1)));
  1476. if (flags & RichElementText::OUTLINE_FLAG) {
  1477. leftRenderer->enableOutline(Color4B(outlineColor), outlineSize);
  1478. }
  1479. if (flags & RichElementText::SHADOW_FLAG) {
  1480. leftRenderer->enableShadow(Color4B(shadowColor), shadowOffset, shadowBlurRadius);
  1481. }
  1482. if (flags & RichElementText::GLOW_FLAG) {
  1483. leftRenderer->enableGlow(Color4B(glowColor));
  1484. }
  1485. }
  1486. }
  1487. addNewLine();
  1488. handleTextRenderer(cutWords, fontName, fontSize, color, opacity, flags, url,
  1489. outlineColor, outlineSize,
  1490. shadowColor, shadowOffset, shadowBlurRadius,
  1491. glowColor);
  1492. }
  1493. else
  1494. {
  1495. textRenderer->setColor(color);
  1496. textRenderer->setOpacity(opacity);
  1497. pushToContainer(textRenderer);
  1498. }
  1499. }
  1500. void RichText::handleImageRenderer(const std::string& filePath, const Color3B &/*color*/, GLubyte /*opacity*/, int width, int height, const std::string& url)
  1501. {
  1502. Sprite* imageRenderer = Sprite::create(filePath);
  1503. if (imageRenderer)
  1504. {
  1505. auto currentSize = imageRenderer->getContentSize();
  1506. if (width != -1)
  1507. imageRenderer->setScaleX(width / currentSize.width);
  1508. if (height != -1)
  1509. imageRenderer->setScaleY(height / currentSize.height);
  1510. imageRenderer->setContentSize(Size(currentSize.width * imageRenderer->getScaleX(),
  1511. currentSize.height * imageRenderer->getScaleY()));
  1512. handleCustomRenderer(imageRenderer);
  1513. imageRenderer->addComponent(ListenerComponent::create(imageRenderer,
  1514. url,
  1515. std::bind(&RichText::openUrl, this, std::placeholders::_1)));
  1516. }
  1517. }
  1518. void RichText::handleCustomRenderer(cocos2d::Node *renderer)
  1519. {
  1520. Size imgSize = renderer->getContentSize();
  1521. _leftSpaceWidth -= imgSize.width;
  1522. if (_leftSpaceWidth < 0.0f)
  1523. {
  1524. addNewLine();
  1525. pushToContainer(renderer);
  1526. _leftSpaceWidth -= imgSize.width;
  1527. }
  1528. else
  1529. {
  1530. pushToContainer(renderer);
  1531. }
  1532. }
  1533. void RichText::addNewLine()
  1534. {
  1535. _leftSpaceWidth = _customSize.width;
  1536. _elementRenders.push_back(new Vector<Node*>());
  1537. }
  1538. void RichText::formarRenderers()
  1539. {
  1540. if (_ignoreSize)
  1541. {
  1542. float newContentSizeWidth = 0.0f;
  1543. float nextPosY = 0.0f;
  1544. for (auto& element: _elementRenders)
  1545. {
  1546. Vector<Node*>* row = element;
  1547. float nextPosX = 0.0f;
  1548. float maxY = 0.0f;
  1549. for (auto& iter : *row)
  1550. {
  1551. iter->setAnchorPoint(Vec2::ZERO);
  1552. iter->setPosition(nextPosX, nextPosY);
  1553. this->addProtectedChild(iter, 1);
  1554. Size iSize = iter->getContentSize();
  1555. newContentSizeWidth += iSize.width;
  1556. nextPosX += iSize.width;
  1557. maxY = MAX(maxY, iSize.height);
  1558. }
  1559. nextPosY -= maxY;
  1560. }
  1561. this->setContentSize(Size(newContentSizeWidth, -nextPosY));
  1562. }
  1563. else
  1564. {
  1565. float newContentSizeHeight = 0.0f;
  1566. float *maxHeights = new (std::nothrow) float[_elementRenders.size()];
  1567. for (size_t i=0, size = _elementRenders.size(); i<size; i++)
  1568. {
  1569. Vector<Node*>* row = (_elementRenders[i]);
  1570. float maxHeight = 0.0f;
  1571. for (auto& iter : *row)
  1572. {
  1573. maxHeight = MAX(iter->getContentSize().height, maxHeight);
  1574. }
  1575. maxHeights[i] = maxHeight;
  1576. newContentSizeHeight += maxHeights[i];
  1577. }
  1578. float nextPosY = _customSize.height;
  1579. for (size_t i=0, size = _elementRenders.size(); i<size; i++)
  1580. {
  1581. Vector<Node*>* row = (_elementRenders[i]);
  1582. float nextPosX = 0.0f;
  1583. nextPosY -= (maxHeights[i] + _defaults.at(KEY_VERTICAL_SPACE).asFloat());
  1584. for (auto& iter : *row)
  1585. {
  1586. iter->setAnchorPoint(Vec2::ZERO);
  1587. iter->setPosition(nextPosX, nextPosY);
  1588. this->addProtectedChild(iter, 1);
  1589. nextPosX += iter->getContentSize().width;
  1590. }
  1591. }
  1592. delete [] maxHeights;
  1593. }
  1594. for (auto& iter : _elementRenders)
  1595. {
  1596. iter->clear();
  1597. delete iter;
  1598. }
  1599. _elementRenders.clear();
  1600. if (_ignoreSize)
  1601. {
  1602. Size s = getVirtualRendererSize();
  1603. this->setContentSize(s);
  1604. }
  1605. else
  1606. {
  1607. this->setContentSize(_customSize);
  1608. }
  1609. updateContentSizeWithTextureSize(_contentSize);
  1610. }
  1611. void RichText::adaptRenderers()
  1612. {
  1613. this->formatText();
  1614. }
  1615. void RichText::pushToContainer(cocos2d::Node *renderer)
  1616. {
  1617. if (_elementRenders.size() <= 0)
  1618. {
  1619. return;
  1620. }
  1621. _elementRenders[_elementRenders.size()-1]->pushBack(renderer);
  1622. }
  1623. void RichText::setVerticalSpace(float space)
  1624. {
  1625. _defaults[KEY_VERTICAL_SPACE] = space;
  1626. }
  1627. void RichText::ignoreContentAdaptWithSize(bool ignore)
  1628. {
  1629. if (_ignoreSize != ignore)
  1630. {
  1631. _formatTextDirty = true;
  1632. Widget::ignoreContentAdaptWithSize(ignore);
  1633. }
  1634. }
  1635. std::string RichText::getDescription() const
  1636. {
  1637. return "RichText";
  1638. }