2014-03-11 17:13:54 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013 cocos2d-x.org
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-12-28 19:10:50 +08:00
|
|
|
Copyright (c) 2021 Bytedance Inc.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-09 01:23:11 +08:00
|
|
|
https://axis-project.github.io
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-03-20 21:53:44 +08:00
|
|
|
#include "ui/UIRichText.h"
|
2016-01-20 12:41:31 +08:00
|
|
|
|
2016-03-07 09:50:47 +08:00
|
|
|
#include <sstream>
|
2016-01-20 12:41:31 +08:00
|
|
|
#include <vector>
|
2016-04-20 04:53:27 +08:00
|
|
|
#include <locale>
|
2017-07-05 09:25:12 +08:00
|
|
|
#include <algorithm>
|
2016-01-20 12:41:31 +08:00
|
|
|
|
2014-06-04 14:26:21 +08:00
|
|
|
#include "platform/CCFileUtils.h"
|
2016-01-20 12:41:31 +08:00
|
|
|
#include "platform/CCApplication.h"
|
|
|
|
#include "base/CCEventListenerTouch.h"
|
|
|
|
#include "base/CCEventDispatcher.h"
|
|
|
|
#include "base/CCDirector.h"
|
2014-06-04 14:26:21 +08:00
|
|
|
#include "2d/CCLabel.h"
|
|
|
|
#include "2d/CCSprite.h"
|
2014-07-17 17:58:40 +08:00
|
|
|
#include "base/ccUTF8.h"
|
2014-07-28 17:24:37 +08:00
|
|
|
#include "ui/UIHelper.h"
|
2014-03-11 17:13:54 +08:00
|
|
|
|
2016-08-25 07:37:03 +08:00
|
|
|
#include "platform/CCSAXParser.h"
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
USING_NS_AX;
|
|
|
|
using namespace axis::ui;
|
2014-03-11 17:13:54 +08:00
|
|
|
|
2016-03-07 09:50:47 +08:00
|
|
|
class ListenerComponent : public Component
|
|
|
|
{
|
|
|
|
public:
|
2021-12-25 10:04:45 +08:00
|
|
|
static const std::string COMPONENT_NAME; /*!< component name */
|
2016-03-07 09:50:47 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
static ListenerComponent* create(Node* parent,
|
2021-12-26 23:26:34 +08:00
|
|
|
std::string_view url,
|
2021-12-25 10:04:45 +08:00
|
|
|
const RichText::OpenUrlHandler handleOpenUrl = nullptr)
|
2016-01-20 12:41:31 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
auto component = new ListenerComponent(parent, url, std::move(handleOpenUrl));
|
2016-03-07 09:50:47 +08:00
|
|
|
component->autorelease();
|
|
|
|
return component;
|
|
|
|
}
|
2016-01-20 12:41:31 +08:00
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
explicit ListenerComponent(Node* parent, std::string_view url, const RichText::OpenUrlHandler handleOpenUrl)
|
2021-12-25 10:04:45 +08:00
|
|
|
: _parent(parent), _url(url), _handleOpenUrl(std::move(handleOpenUrl))
|
2016-03-07 09:50:47 +08:00
|
|
|
{
|
|
|
|
setName(ListenerComponent::COMPONENT_NAME);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
_touchListener = axis::EventListenerTouchAllAtOnce::create();
|
2022-07-15 19:17:01 +08:00
|
|
|
_touchListener->onTouchesEnded = AX_CALLBACK_2(ListenerComponent::onTouchesEnded, this);
|
2016-01-20 12:41:31 +08:00
|
|
|
|
2016-03-07 09:50:47 +08:00
|
|
|
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(_touchListener, _parent);
|
|
|
|
_touchListener->retain();
|
|
|
|
}
|
2016-01-20 12:41:31 +08:00
|
|
|
|
2016-03-07 09:50:47 +08:00
|
|
|
virtual ~ListenerComponent()
|
|
|
|
{
|
|
|
|
Director::getInstance()->getEventDispatcher()->removeEventListener(_touchListener);
|
|
|
|
_touchListener->release();
|
|
|
|
}
|
2016-01-20 12:41:31 +08:00
|
|
|
|
2016-11-16 09:48:37 +08:00
|
|
|
void onTouchesEnded(const std::vector<Touch*>& touches, Event* /*event*/)
|
2016-03-07 09:50:47 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
for (const auto& touch : touches)
|
2016-01-20 12:41:31 +08:00
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
// FIXME: Node::getBoundBox() doesn't return it in local coordinates... so create one manually.
|
|
|
|
Rect localRect = Rect(Vec2::ZERO, _parent->getContentSize());
|
2021-12-25 10:04:45 +08:00
|
|
|
if (localRect.containsPoint(_parent->convertTouchToNodeSpace(touch)))
|
|
|
|
{
|
|
|
|
if (_handleOpenUrl)
|
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
_handleOpenUrl(_url);
|
|
|
|
}
|
2016-01-20 12:41:31 +08:00
|
|
|
}
|
|
|
|
}
|
2016-03-07 09:50:47 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
void setOpenUrlHandler(const RichText::OpenUrlHandler& handleOpenUrl) { _handleOpenUrl = handleOpenUrl; }
|
2016-01-20 12:41:31 +08:00
|
|
|
|
2016-03-07 09:50:47 +08:00
|
|
|
private:
|
2021-12-25 10:04:45 +08:00
|
|
|
Node* _parent; // weak ref.
|
2016-03-07 09:50:47 +08:00
|
|
|
std::string _url;
|
|
|
|
RichText::OpenUrlHandler _handleOpenUrl;
|
2021-12-25 10:04:45 +08:00
|
|
|
EventListenerTouchAllAtOnce* _touchListener; // strong ref.
|
2016-03-07 09:50:47 +08:00
|
|
|
};
|
|
|
|
const std::string ListenerComponent::COMPONENT_NAME("cocos2d_ui_UIRichText_ListenerComponent");
|
2014-07-28 17:24:37 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool RichElement::init(int tag, const Color3B& color, uint8_t opacity)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_tag = tag;
|
|
|
|
_color = color;
|
2014-03-11 17:13:54 +08:00
|
|
|
_opacity = opacity;
|
|
|
|
return true;
|
|
|
|
}
|
2016-03-07 09:50:47 +08:00
|
|
|
|
|
|
|
bool RichElement::equalType(Type type)
|
|
|
|
{
|
|
|
|
return (_type == type);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RichElement::setColor(const Color3B& color)
|
|
|
|
{
|
|
|
|
_color = color;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
RichElementText* RichElementText::create(int tag,
|
|
|
|
const Color3B& color,
|
|
|
|
uint8_t opacity,
|
2021-12-26 23:26:34 +08:00
|
|
|
std::string_view text,
|
|
|
|
std::string_view fontName,
|
2021-12-25 10:04:45 +08:00
|
|
|
float fontSize,
|
|
|
|
uint32_t flags,
|
2021-12-26 23:26:34 +08:00
|
|
|
std::string_view url,
|
2021-12-25 10:04:45 +08:00
|
|
|
const Color3B& outlineColor,
|
|
|
|
int outlineSize,
|
|
|
|
const Color3B& shadowColor,
|
|
|
|
const Vec2& shadowOffset,
|
|
|
|
int shadowBlurRadius,
|
2016-03-07 09:50:47 +08:00
|
|
|
const Color3B& glowColor)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
RichElementText* element = new RichElementText();
|
2021-12-25 10:04:45 +08:00
|
|
|
if (element->init(tag, color, opacity, text, fontName, fontSize, flags, url, outlineColor, outlineSize, shadowColor,
|
|
|
|
shadowOffset, shadowBlurRadius, glowColor))
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
element->autorelease();
|
|
|
|
return element;
|
|
|
|
}
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE(element);
|
2014-07-10 00:45:27 +08:00
|
|
|
return nullptr;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
bool RichElementText::init(int tag,
|
|
|
|
const Color3B& color,
|
|
|
|
uint8_t opacity,
|
2021-12-26 23:26:34 +08:00
|
|
|
std::string_view text,
|
|
|
|
std::string_view fontName,
|
2021-12-25 10:04:45 +08:00
|
|
|
float fontSize,
|
|
|
|
uint32_t flags,
|
2021-12-26 23:26:34 +08:00
|
|
|
std::string_view url,
|
2021-12-25 10:04:45 +08:00
|
|
|
const Color3B& outlineColor,
|
|
|
|
int outlineSize,
|
|
|
|
const Color3B& shadowColor,
|
|
|
|
const Vec2& shadowOffset,
|
|
|
|
int shadowBlurRadius,
|
2016-03-07 09:50:47 +08:00
|
|
|
const Color3B& glowColor)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
if (RichElement::init(tag, color, opacity))
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_text = text;
|
|
|
|
_fontName = fontName;
|
|
|
|
_fontSize = fontSize;
|
|
|
|
_flags = flags;
|
|
|
|
_url = url;
|
|
|
|
_outlineColor = outlineColor;
|
|
|
|
_outlineSize = outlineSize;
|
|
|
|
_shadowColor = shadowColor;
|
|
|
|
_shadowOffset = shadowOffset;
|
2016-03-07 09:50:47 +08:00
|
|
|
_shadowBlurRadius = shadowBlurRadius;
|
2021-12-25 10:04:45 +08:00
|
|
|
_glowColor = glowColor;
|
2014-03-11 17:13:54 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
RichElementImage* RichElementImage::create(int tag,
|
|
|
|
const Color3B& color,
|
|
|
|
uint8_t opacity,
|
2021-12-26 23:26:34 +08:00
|
|
|
std::string_view filePath,
|
|
|
|
std::string_view url,
|
2021-12-25 10:04:45 +08:00
|
|
|
Widget::TextureResType texType)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
RichElementImage* element = new RichElementImage();
|
|
|
|
if (element->init(tag, color, opacity, filePath, url, texType))
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
element->autorelease();
|
|
|
|
return element;
|
|
|
|
}
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE(element);
|
2014-07-10 00:45:27 +08:00
|
|
|
return nullptr;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
bool RichElementImage::init(int tag,
|
|
|
|
const Color3B& color,
|
|
|
|
uint8_t opacity,
|
2021-12-26 23:26:34 +08:00
|
|
|
std::string_view filePath,
|
|
|
|
std::string_view url,
|
2021-12-25 10:04:45 +08:00
|
|
|
Widget::TextureResType texType)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
if (RichElement::init(tag, color, opacity))
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_filePath = filePath;
|
|
|
|
_width = -1;
|
|
|
|
_height = -1;
|
|
|
|
_url = url;
|
2017-07-24 10:35:52 +08:00
|
|
|
_textureType = texType;
|
2014-03-11 17:13:54 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2016-03-07 09:50:47 +08:00
|
|
|
|
2016-01-20 12:41:31 +08:00
|
|
|
void RichElementImage::setWidth(int width)
|
|
|
|
{
|
|
|
|
_width = width;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RichElementImage::setHeight(int height)
|
|
|
|
{
|
|
|
|
_height = height;
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
void RichElementImage::setUrl(std::string_view url)
|
2016-03-07 09:50:47 +08:00
|
|
|
{
|
|
|
|
_url = url;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
RichElementCustomNode* RichElementCustomNode::create(int tag,
|
|
|
|
const Color3B& color,
|
|
|
|
uint8_t opacity,
|
2022-07-11 17:50:21 +08:00
|
|
|
axis::Node* customNode)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
RichElementCustomNode* element = new RichElementCustomNode();
|
|
|
|
if (element->init(tag, color, opacity, customNode))
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
element->autorelease();
|
|
|
|
return element;
|
|
|
|
}
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE(element);
|
2014-07-10 00:45:27 +08:00
|
|
|
return nullptr;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
bool RichElementCustomNode::init(int tag, const Color3B& color, uint8_t opacity, axis::Node* customNode)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
if (RichElement::init(tag, color, opacity))
|
|
|
|
{
|
|
|
|
_customNode = customNode;
|
|
|
|
_customNode->retain();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-06-05 17:58:33 +08:00
|
|
|
RichElementNewLine* RichElementNewLine::create(int tag, const Color3B& color, uint8_t opacity)
|
2015-09-28 13:33:17 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
RichElementNewLine* element = new RichElementNewLine();
|
|
|
|
if (element->init(tag, color, opacity))
|
2015-09-28 13:33:17 +08:00
|
|
|
{
|
|
|
|
element->autorelease();
|
|
|
|
return element;
|
|
|
|
}
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE(element);
|
2015-09-28 13:33:17 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
2016-01-20 12:41:31 +08:00
|
|
|
|
2016-03-07 09:50:47 +08:00
|
|
|
/** @brief parse a XML. */
|
2016-08-25 07:37:03 +08:00
|
|
|
class MyXMLVisitor : public SAXDelegator
|
2016-01-20 12:41:31 +08:00
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
public:
|
|
|
|
/** @brief underline or strikethrough */
|
2021-12-25 10:04:45 +08:00
|
|
|
enum class StyleLine
|
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
NONE,
|
2021-12-25 10:04:45 +08:00
|
|
|
UNDERLINE, /*!< underline */
|
|
|
|
STRIKETHROUGH /*!< a typographical presentation of words with a horizontal line through their center */
|
2016-03-07 09:50:47 +08:00
|
|
|
};
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2016-03-07 09:50:47 +08:00
|
|
|
/** @brief outline, shadow or glow */
|
2021-12-25 10:04:45 +08:00
|
|
|
enum class StyleEffect
|
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
NONE,
|
2021-12-25 10:04:45 +08:00
|
|
|
OUTLINE, /*!< outline effect enabled */
|
|
|
|
SHADOW, /*!< shadow effect enabled */
|
|
|
|
GLOW /*!< glow effect enabled @discussion Limiting use to only when the Label created with true type font. */
|
2016-03-07 09:50:47 +08:00
|
|
|
};
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2016-03-07 09:50:47 +08:00
|
|
|
/** @brief the attributes of text tag */
|
2016-01-20 12:41:31 +08:00
|
|
|
struct Attributes
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
std::string face; /*!< font name */
|
|
|
|
std::string url; /*!< url is a attribute of a anchor tag */
|
|
|
|
float fontSize; /*!< font size */
|
|
|
|
Color3B color; /*!< font color */
|
|
|
|
bool hasColor; /*!< or color is specified? */
|
|
|
|
bool bold; /*!< bold text */
|
|
|
|
bool italics; /*!< italic text */
|
|
|
|
StyleLine line; /*!< underline or strikethrough */
|
|
|
|
StyleEffect effect; /*!< outline, shadow or glow */
|
|
|
|
Color3B outlineColor; /*!< the color of the outline */
|
|
|
|
int outlineSize; /*!< the outline effect size value */
|
|
|
|
Color3B shadowColor; /*!< the shadow effect color value */
|
|
|
|
Vec2 shadowOffset; /*!< shadow effect offset value */
|
|
|
|
int shadowBlurRadius; /*!< the shadow effect blur radius */
|
|
|
|
Color3B glowColor; /*!< the glow effect color value */
|
|
|
|
|
2016-01-20 12:41:31 +08:00
|
|
|
void setColor(const Color3B& acolor)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
color = acolor;
|
2016-01-20 12:41:31 +08:00
|
|
|
hasColor = true;
|
|
|
|
}
|
|
|
|
Attributes()
|
2021-12-25 10:04:45 +08:00
|
|
|
: fontSize(-1)
|
|
|
|
, hasColor(false)
|
|
|
|
, bold(false)
|
|
|
|
, italics(false)
|
|
|
|
, line(StyleLine::NONE)
|
|
|
|
, effect(StyleEffect::NONE)
|
|
|
|
{}
|
2016-01-20 12:41:31 +08:00
|
|
|
};
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2016-03-07 09:50:47 +08:00
|
|
|
private:
|
2016-01-20 12:41:31 +08:00
|
|
|
std::vector<Attributes> _fontElements;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2016-01-20 12:41:31 +08:00
|
|
|
RichText* _richText;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
struct TagBehavior
|
|
|
|
{
|
|
|
|
bool isFontElement;
|
|
|
|
RichText::VisitEnterHandler handleVisitEnter;
|
2016-03-07 09:50:47 +08:00
|
|
|
};
|
2021-12-26 23:26:34 +08:00
|
|
|
typedef hlookup::string_map<TagBehavior> TagTables;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
static TagTables _tagTables;
|
|
|
|
|
2016-03-07 09:50:47 +08:00
|
|
|
public:
|
|
|
|
explicit MyXMLVisitor(RichText* richText);
|
|
|
|
virtual ~MyXMLVisitor();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2016-03-07 09:50:47 +08:00
|
|
|
Color3B getColor() const;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2016-03-07 09:50:47 +08:00
|
|
|
float getFontSize() const;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2016-03-07 09:50:47 +08:00
|
|
|
std::string getFace() const;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2016-03-07 09:50:47 +08:00
|
|
|
std::string getURL() const;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2016-03-07 09:50:47 +08:00
|
|
|
bool getBold() const;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2016-03-07 09:50:47 +08:00
|
|
|
bool getItalics() const;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2016-03-07 09:50:47 +08:00
|
|
|
bool getUnderline() const;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2016-03-07 09:50:47 +08:00
|
|
|
bool getStrikethrough() const;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2016-03-07 09:50:47 +08:00
|
|
|
std::tuple<bool, Color3B, int> getOutline() const;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2021-10-23 23:27:14 +08:00
|
|
|
std::tuple<bool, Color3B, Vec2, int> getShadow() const;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2016-03-07 09:50:47 +08:00
|
|
|
std::tuple<bool, Color3B> getGlow() const;
|
2016-08-25 07:37:03 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void startElement(void* ctx, const char* name, const char** atts) override;
|
2016-08-25 07:37:03 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void endElement(void* ctx, const char* name) override;
|
|
|
|
|
|
|
|
void textHandler(void* ctx, const char* s, size_t len) override;
|
2016-08-25 07:37:03 +08:00
|
|
|
|
2016-03-07 09:50:47 +08:00
|
|
|
void pushBackFontElement(const Attributes& attribs);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2016-03-07 09:50:47 +08:00
|
|
|
void popBackFontElement();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2016-03-07 09:50:47 +08:00
|
|
|
void pushBackElement(RichElement* element);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
static void setTagDescription(std::string_view tag,
|
2021-12-25 10:04:45 +08:00
|
|
|
bool isFontElement,
|
|
|
|
RichText::VisitEnterHandler&& handleVisitEnter);
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
static void removeTagDescription(std::string_view tag);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2016-03-07 09:50:47 +08:00
|
|
|
private:
|
2021-12-25 10:04:45 +08:00
|
|
|
ValueMap tagAttrMapWithXMLElement(const char** attrs);
|
2016-03-07 09:50:47 +08:00
|
|
|
};
|
2016-01-20 12:41:31 +08:00
|
|
|
|
2016-03-07 09:50:47 +08:00
|
|
|
MyXMLVisitor::TagTables MyXMLVisitor::_tagTables;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
MyXMLVisitor::MyXMLVisitor(RichText* richText) : _fontElements(20), _richText(richText)
|
2016-03-07 09:50:47 +08:00
|
|
|
{
|
|
|
|
MyXMLVisitor::setTagDescription("font", true, [](const ValueMap& tagAttrValueMap) {
|
|
|
|
// supported attributes:
|
|
|
|
// size, color, align, face
|
|
|
|
ValueMap attrValueMap;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
if (tagAttrValueMap.find("size") != tagAttrValueMap.end())
|
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
attrValueMap[RichText::KEY_FONT_SIZE] = tagAttrValueMap.at("size").asString();
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (tagAttrValueMap.find("color") != tagAttrValueMap.end())
|
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
attrValueMap[RichText::KEY_FONT_COLOR_STRING] = tagAttrValueMap.at("color").asString();
|
2016-01-20 12:41:31 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (tagAttrValueMap.find("face") != tagAttrValueMap.end())
|
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
attrValueMap[RichText::KEY_FONT_FACE] = tagAttrValueMap.at("face").asString();
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2016-03-07 09:50:47 +08:00
|
|
|
return make_pair(attrValueMap, nullptr);
|
|
|
|
});
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2016-11-16 09:48:37 +08:00
|
|
|
MyXMLVisitor::setTagDescription("b", true, [](const ValueMap& /*tagAttrValueMap*/) {
|
2016-03-07 09:50:47 +08:00
|
|
|
// no supported attributes
|
|
|
|
ValueMap attrValueMap;
|
|
|
|
attrValueMap[RichText::KEY_TEXT_BOLD] = true;
|
|
|
|
return make_pair(attrValueMap, nullptr);
|
|
|
|
});
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2016-11-16 09:48:37 +08:00
|
|
|
MyXMLVisitor::setTagDescription("i", true, [](const ValueMap& /*tagAttrValueMap*/) {
|
2016-03-07 09:50:47 +08:00
|
|
|
// no supported attributes
|
|
|
|
ValueMap attrValueMap;
|
|
|
|
attrValueMap[RichText::KEY_TEXT_ITALIC] = true;
|
|
|
|
return make_pair(attrValueMap, nullptr);
|
|
|
|
});
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2016-11-16 09:48:37 +08:00
|
|
|
MyXMLVisitor::setTagDescription("del", true, [](const ValueMap& /*tagAttrValueMap*/) {
|
2016-03-07 09:50:47 +08:00
|
|
|
// no supported attributes
|
|
|
|
ValueMap attrValueMap;
|
|
|
|
attrValueMap[RichText::KEY_TEXT_LINE] = RichText::VALUE_TEXT_LINE_DEL;
|
|
|
|
return make_pair(attrValueMap, nullptr);
|
|
|
|
});
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2016-11-16 09:48:37 +08:00
|
|
|
MyXMLVisitor::setTagDescription("u", true, [](const ValueMap& /*tagAttrValueMap*/) {
|
2016-03-07 09:50:47 +08:00
|
|
|
// no supported attributes
|
|
|
|
ValueMap attrValueMap;
|
|
|
|
attrValueMap[RichText::KEY_TEXT_LINE] = RichText::VALUE_TEXT_LINE_UNDER;
|
|
|
|
return make_pair(attrValueMap, nullptr);
|
|
|
|
});
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2016-11-16 09:48:37 +08:00
|
|
|
MyXMLVisitor::setTagDescription("small", true, [](const ValueMap& /*tagAttrValueMap*/) {
|
2016-03-07 09:50:47 +08:00
|
|
|
ValueMap attrValueMap;
|
|
|
|
attrValueMap[RichText::KEY_FONT_SMALL] = true;
|
|
|
|
return make_pair(attrValueMap, nullptr);
|
|
|
|
});
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2016-11-16 09:48:37 +08:00
|
|
|
MyXMLVisitor::setTagDescription("big", true, [](const ValueMap& /*tagAttrValueMap*/) {
|
2016-03-07 09:50:47 +08:00
|
|
|
ValueMap attrValueMap;
|
|
|
|
attrValueMap[RichText::KEY_FONT_BIG] = true;
|
|
|
|
return make_pair(attrValueMap, nullptr);
|
|
|
|
});
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2016-03-07 09:50:47 +08:00
|
|
|
MyXMLVisitor::setTagDescription("img", false, [](const ValueMap& tagAttrValueMap) {
|
|
|
|
// supported attributes:
|
|
|
|
// src, height, width
|
|
|
|
std::string src;
|
2021-12-25 10:04:45 +08:00
|
|
|
int height = -1;
|
|
|
|
int width = -1;
|
2017-07-24 10:35:52 +08:00
|
|
|
Widget::TextureResType resType = Widget::TextureResType::LOCAL;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
if (tagAttrValueMap.find("src") != tagAttrValueMap.end())
|
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
src = tagAttrValueMap.at("src").asString();
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (tagAttrValueMap.find("height") != tagAttrValueMap.end())
|
|
|
|
{
|
2020-08-18 11:33:18 +08:00
|
|
|
height = tagAttrValueMap.at("height").asInt();
|
2016-03-07 09:50:47 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (tagAttrValueMap.find("width") != tagAttrValueMap.end())
|
|
|
|
{
|
2020-08-18 11:33:18 +08:00
|
|
|
width = tagAttrValueMap.at("width").asInt();
|
2016-03-07 09:50:47 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (tagAttrValueMap.find("type") != tagAttrValueMap.end())
|
|
|
|
{
|
2017-07-24 10:35:52 +08:00
|
|
|
// texture type
|
|
|
|
// 0: normal file path
|
|
|
|
// 1: sprite frame name
|
2020-08-18 11:33:18 +08:00
|
|
|
int type = tagAttrValueMap.at("type").asInt();
|
2021-12-25 10:04:45 +08:00
|
|
|
resType = type == 0 ? Widget::TextureResType::LOCAL : Widget::TextureResType::PLIST;
|
2017-07-24 10:35:52 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2016-03-07 09:50:47 +08:00
|
|
|
RichElementImage* elementImg = nullptr;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (src.length())
|
|
|
|
{
|
2017-07-24 10:35:52 +08:00
|
|
|
elementImg = RichElementImage::create(0, Color3B::WHITE, 255, src, "", resType);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (0 <= height)
|
|
|
|
elementImg->setHeight(height);
|
|
|
|
if (0 <= width)
|
|
|
|
elementImg->setWidth(width);
|
2016-03-07 09:50:47 +08:00
|
|
|
}
|
|
|
|
return make_pair(ValueMap(), elementImg);
|
|
|
|
});
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2016-03-07 09:50:47 +08:00
|
|
|
MyXMLVisitor::setTagDescription("a", true, [](const ValueMap& tagAttrValueMap) {
|
|
|
|
// supported attributes:
|
|
|
|
ValueMap attrValueMap;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
if (tagAttrValueMap.find("href") != tagAttrValueMap.end())
|
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
attrValueMap[RichText::KEY_URL] = tagAttrValueMap.at("href").asString();
|
|
|
|
}
|
|
|
|
return make_pair(attrValueMap, nullptr);
|
|
|
|
});
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
MyXMLVisitor::setTagDescription("br", false, [](const ValueMap& /*tagAttrValueMap*/) {
|
2016-03-07 09:50:47 +08:00
|
|
|
RichElementNewLine* richElement = RichElementNewLine::create(0, Color3B::WHITE, 255);
|
|
|
|
return make_pair(ValueMap(), richElement);
|
|
|
|
});
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2016-03-07 09:50:47 +08:00
|
|
|
MyXMLVisitor::setTagDescription("outline", true, [](const ValueMap& tagAttrValueMap) {
|
|
|
|
// supported attributes:
|
2016-06-22 10:22:05 +08:00
|
|
|
// color, size
|
2016-03-07 09:50:47 +08:00
|
|
|
ValueMap attrValueMap;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2016-03-07 09:50:47 +08:00
|
|
|
attrValueMap[RichText::KEY_TEXT_STYLE] = RichText::VALUE_TEXT_STYLE_OUTLINE;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (tagAttrValueMap.find("color") != tagAttrValueMap.end())
|
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
attrValueMap[RichText::KEY_TEXT_OUTLINE_COLOR] = tagAttrValueMap.at("color").asString();
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (tagAttrValueMap.find("size") != tagAttrValueMap.end())
|
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
attrValueMap[RichText::KEY_TEXT_OUTLINE_SIZE] = tagAttrValueMap.at("size").asString();
|
|
|
|
}
|
|
|
|
return make_pair(attrValueMap, nullptr);
|
|
|
|
});
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2016-03-07 09:50:47 +08:00
|
|
|
MyXMLVisitor::setTagDescription("shadow", true, [](const ValueMap& tagAttrValueMap) {
|
|
|
|
// supported attributes:
|
|
|
|
// color, offsetWidth, offsetHeight, blurRadius
|
|
|
|
ValueMap attrValueMap;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2016-03-07 09:50:47 +08:00
|
|
|
attrValueMap[RichText::KEY_TEXT_STYLE] = RichText::VALUE_TEXT_STYLE_SHADOW;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (tagAttrValueMap.find("color") != tagAttrValueMap.end())
|
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
attrValueMap[RichText::KEY_TEXT_SHADOW_COLOR] = tagAttrValueMap.at("color").asString();
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (tagAttrValueMap.find("offsetWidth") != tagAttrValueMap.end())
|
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
attrValueMap[RichText::KEY_TEXT_SHADOW_OFFSET_WIDTH] = tagAttrValueMap.at("offsetWidth").asString();
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (tagAttrValueMap.find("offsetHeight") != tagAttrValueMap.end())
|
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
attrValueMap[RichText::KEY_TEXT_SHADOW_OFFSET_HEIGHT] = tagAttrValueMap.at("offsetHeight").asString();
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (tagAttrValueMap.find("blurRadius") != tagAttrValueMap.end())
|
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
attrValueMap[RichText::KEY_TEXT_SHADOW_BLUR_RADIUS] = tagAttrValueMap.at("blurRadius").asString();
|
|
|
|
}
|
|
|
|
return make_pair(attrValueMap, nullptr);
|
|
|
|
});
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2016-03-07 09:50:47 +08:00
|
|
|
MyXMLVisitor::setTagDescription("glow", true, [](const ValueMap& tagAttrValueMap) {
|
|
|
|
// supported attributes:
|
|
|
|
// color
|
|
|
|
ValueMap attrValueMap;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2016-03-07 09:50:47 +08:00
|
|
|
attrValueMap[RichText::KEY_TEXT_STYLE] = RichText::VALUE_TEXT_STYLE_GLOW;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (tagAttrValueMap.find("color") != tagAttrValueMap.end())
|
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
attrValueMap[RichText::KEY_TEXT_GLOW_COLOR] = tagAttrValueMap.at("color").asString();
|
|
|
|
}
|
|
|
|
return make_pair(attrValueMap, nullptr);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
MyXMLVisitor::~MyXMLVisitor() {}
|
2016-03-07 09:50:47 +08:00
|
|
|
|
|
|
|
Color3B MyXMLVisitor::getColor() const
|
|
|
|
{
|
2016-10-27 15:10:24 +08:00
|
|
|
for (auto i = _fontElements.rbegin(), iRend = _fontElements.rend(); i != iRend; ++i)
|
2016-03-07 09:50:47 +08:00
|
|
|
{
|
|
|
|
if (i->hasColor)
|
|
|
|
return i->color;
|
2016-01-20 12:41:31 +08:00
|
|
|
}
|
2016-03-07 09:50:47 +08:00
|
|
|
return Color3B::WHITE;
|
|
|
|
}
|
2016-01-20 12:41:31 +08:00
|
|
|
|
2016-03-07 09:50:47 +08:00
|
|
|
float MyXMLVisitor::getFontSize() const
|
|
|
|
{
|
2016-10-27 15:10:24 +08:00
|
|
|
for (auto i = _fontElements.rbegin(), iRend = _fontElements.rend(); i != iRend; ++i)
|
2016-01-20 12:41:31 +08:00
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
if (i->fontSize != -1)
|
|
|
|
return i->fontSize;
|
2016-01-20 12:41:31 +08:00
|
|
|
}
|
2016-03-07 09:50:47 +08:00
|
|
|
return 12;
|
|
|
|
}
|
2016-01-20 12:41:31 +08:00
|
|
|
|
2016-03-07 09:50:47 +08:00
|
|
|
std::string MyXMLVisitor::getFace() const
|
|
|
|
{
|
2016-10-27 15:10:24 +08:00
|
|
|
for (auto i = _fontElements.rbegin(), iRend = _fontElements.rend(); i != iRend; ++i)
|
2016-01-20 12:41:31 +08:00
|
|
|
{
|
2020-08-06 17:37:34 +08:00
|
|
|
if (!i->face.empty())
|
2016-03-07 09:50:47 +08:00
|
|
|
return i->face;
|
2016-01-20 12:41:31 +08:00
|
|
|
}
|
2016-03-07 09:50:47 +08:00
|
|
|
return "fonts/Marker Felt.ttf";
|
|
|
|
}
|
2016-01-20 12:41:31 +08:00
|
|
|
|
2016-03-07 09:50:47 +08:00
|
|
|
std::string MyXMLVisitor::getURL() const
|
|
|
|
{
|
2016-10-27 15:10:24 +08:00
|
|
|
for (auto i = _fontElements.rbegin(), iRend = _fontElements.rend(); i != iRend; ++i)
|
2016-01-20 12:41:31 +08:00
|
|
|
{
|
2020-08-06 17:37:34 +08:00
|
|
|
if (!i->url.empty())
|
2016-03-07 09:50:47 +08:00
|
|
|
return i->url;
|
2016-01-20 12:41:31 +08:00
|
|
|
}
|
2016-03-07 09:50:47 +08:00
|
|
|
return "";
|
|
|
|
}
|
2016-01-20 12:41:31 +08:00
|
|
|
|
2016-03-07 09:50:47 +08:00
|
|
|
bool MyXMLVisitor::getBold() const
|
|
|
|
{
|
2016-10-27 15:10:24 +08:00
|
|
|
for (auto i = _fontElements.rbegin(), iRend = _fontElements.rend(); i != iRend; ++i)
|
2016-01-20 12:41:31 +08:00
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
if (i->bold)
|
|
|
|
return true;
|
2016-01-20 12:41:31 +08:00
|
|
|
}
|
2016-03-07 09:50:47 +08:00
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 12:41:31 +08:00
|
|
|
|
2016-03-07 09:50:47 +08:00
|
|
|
bool MyXMLVisitor::getItalics() const
|
|
|
|
{
|
2016-10-27 15:10:24 +08:00
|
|
|
for (auto i = _fontElements.rbegin(), iRend = _fontElements.rend(); i != iRend; ++i)
|
2016-01-20 12:41:31 +08:00
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
if (i->italics)
|
|
|
|
return true;
|
2016-01-20 12:41:31 +08:00
|
|
|
}
|
2016-03-07 09:50:47 +08:00
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 12:41:31 +08:00
|
|
|
|
2016-03-07 09:50:47 +08:00
|
|
|
bool MyXMLVisitor::getUnderline() const
|
|
|
|
{
|
2016-10-27 15:10:24 +08:00
|
|
|
for (auto i = _fontElements.rbegin(), iRend = _fontElements.rend(); i != iRend; ++i)
|
2016-01-20 12:41:31 +08:00
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
if (i->line == StyleLine::UNDERLINE)
|
|
|
|
return true;
|
2016-01-20 12:41:31 +08:00
|
|
|
}
|
2016-03-07 09:50:47 +08:00
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 12:41:31 +08:00
|
|
|
|
2016-03-07 09:50:47 +08:00
|
|
|
bool MyXMLVisitor::getStrikethrough() const
|
|
|
|
{
|
2016-10-27 15:10:24 +08:00
|
|
|
for (auto i = _fontElements.rbegin(), iRend = _fontElements.rend(); i != iRend; ++i)
|
2016-01-20 12:41:31 +08:00
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
if (i->line == StyleLine::STRIKETHROUGH)
|
|
|
|
return true;
|
2016-01-20 12:41:31 +08:00
|
|
|
}
|
2016-03-07 09:50:47 +08:00
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 12:41:31 +08:00
|
|
|
|
2016-03-07 09:50:47 +08:00
|
|
|
std::tuple<bool, Color3B, int> MyXMLVisitor::getOutline() const
|
|
|
|
{
|
2016-10-27 15:10:24 +08:00
|
|
|
for (auto i = _fontElements.rbegin(), iRend = _fontElements.rend(); i != iRend; ++i)
|
2016-03-07 09:50:47 +08:00
|
|
|
{
|
|
|
|
if (i->effect == StyleEffect::OUTLINE)
|
|
|
|
return std::make_tuple(true, i->outlineColor, i->outlineSize);
|
|
|
|
}
|
|
|
|
return std::make_tuple(false, Color3B::WHITE, -1);
|
|
|
|
}
|
2016-01-20 12:41:31 +08:00
|
|
|
|
2021-10-23 23:27:14 +08:00
|
|
|
std::tuple<bool, Color3B, Vec2, int> MyXMLVisitor::getShadow() const
|
2016-03-07 09:50:47 +08:00
|
|
|
{
|
2016-10-27 15:10:24 +08:00
|
|
|
for (auto i = _fontElements.rbegin(), iRend = _fontElements.rend(); i != iRend; ++i)
|
2016-01-20 12:41:31 +08:00
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
if (i->effect == StyleEffect::SHADOW)
|
|
|
|
return std::make_tuple(true, i->shadowColor, i->shadowOffset, i->shadowBlurRadius);
|
|
|
|
}
|
2021-10-23 23:27:14 +08:00
|
|
|
return std::make_tuple(false, Color3B::BLACK, Vec2(2.0, -2.0), 0);
|
2016-03-07 09:50:47 +08:00
|
|
|
}
|
2016-01-20 12:41:31 +08:00
|
|
|
|
2016-03-07 09:50:47 +08:00
|
|
|
std::tuple<bool, Color3B> MyXMLVisitor::getGlow() const
|
|
|
|
{
|
2016-10-27 15:10:24 +08:00
|
|
|
for (auto i = _fontElements.rbegin(), iRend = _fontElements.rend(); i != iRend; ++i)
|
2016-03-07 09:50:47 +08:00
|
|
|
{
|
|
|
|
if (i->effect == StyleEffect::GLOW)
|
|
|
|
return std::make_tuple(true, i->glowColor);
|
|
|
|
}
|
|
|
|
return std::make_tuple(false, Color3B::WHITE);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void MyXMLVisitor::startElement(void* /*ctx*/, const char* elementName, const char** atts)
|
2016-03-07 09:50:47 +08:00
|
|
|
{
|
|
|
|
auto it = _tagTables.find(elementName);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (it != _tagTables.end())
|
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
auto tagBehavior = it->second;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (tagBehavior.handleVisitEnter != nullptr)
|
|
|
|
{
|
2016-08-25 07:37:03 +08:00
|
|
|
ValueMap&& tagAttrValueMap = tagAttrMapWithXMLElement(atts);
|
2021-12-25 10:04:45 +08:00
|
|
|
auto result = tagBehavior.handleVisitEnter(tagAttrValueMap);
|
|
|
|
ValueMap& attrValueMap = result.first;
|
|
|
|
RichElement* richElement = result.second;
|
|
|
|
if (!attrValueMap.empty())
|
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
Attributes attributes;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
if (attrValueMap.find(RichText::KEY_FONT_SIZE) != attrValueMap.end())
|
|
|
|
{
|
2020-08-18 11:33:18 +08:00
|
|
|
attributes.fontSize = attrValueMap.at(RichText::KEY_FONT_SIZE).asFloat();
|
2016-03-07 09:50:47 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (attrValueMap.find(RichText::KEY_FONT_SMALL) != attrValueMap.end())
|
|
|
|
{
|
2016-07-04 22:12:45 +08:00
|
|
|
attributes.fontSize = getFontSize() * 0.8f;
|
2016-01-20 12:41:31 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (attrValueMap.find(RichText::KEY_FONT_BIG) != attrValueMap.end())
|
|
|
|
{
|
2016-07-04 22:12:45 +08:00
|
|
|
attributes.fontSize = getFontSize() * 1.25f;
|
2016-03-07 09:50:47 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (attrValueMap.find(RichText::KEY_FONT_COLOR_STRING) != attrValueMap.end())
|
|
|
|
{
|
|
|
|
attributes.setColor(
|
|
|
|
_richText->color3BWithString(attrValueMap.at(RichText::KEY_FONT_COLOR_STRING).asString()));
|
2016-03-07 09:50:47 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (attrValueMap.find(RichText::KEY_FONT_FACE) != attrValueMap.end())
|
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
attributes.face = attrValueMap.at(RichText::KEY_FONT_FACE).asString();
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (attrValueMap.find(RichText::KEY_TEXT_BOLD) != attrValueMap.end())
|
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
attributes.bold = true;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (attrValueMap.find(RichText::KEY_TEXT_ITALIC) != attrValueMap.end())
|
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
attributes.italics = true;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (attrValueMap.find(RichText::KEY_TEXT_LINE) != attrValueMap.end())
|
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
auto keyTextLine = attrValueMap.at(RichText::KEY_TEXT_LINE).asString();
|
2021-12-25 10:04:45 +08:00
|
|
|
if (keyTextLine == RichText::VALUE_TEXT_LINE_DEL)
|
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
attributes.line = StyleLine::STRIKETHROUGH;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (keyTextLine == RichText::VALUE_TEXT_LINE_UNDER)
|
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
attributes.line = StyleLine::UNDERLINE;
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (attrValueMap.find(RichText::KEY_URL) != attrValueMap.end())
|
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
attributes.url = attrValueMap.at(RichText::KEY_URL).asString();
|
|
|
|
attributes.setColor(_richText->getAnchorFontColor3B());
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_richText->isAnchorTextBoldEnabled())
|
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
attributes.bold = true;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_richText->isAnchorTextItalicEnabled())
|
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
attributes.italics = true;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_richText->isAnchorTextUnderlineEnabled())
|
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
attributes.line = StyleLine::UNDERLINE;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_richText->isAnchorTextDelEnabled())
|
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
attributes.line = StyleLine::STRIKETHROUGH;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_richText->isAnchorTextOutlineEnabled())
|
|
|
|
{
|
|
|
|
attributes.effect = StyleEffect::OUTLINE;
|
2016-03-07 09:50:47 +08:00
|
|
|
attributes.outlineColor = _richText->getAnchorTextOutlineColor3B();
|
2021-12-25 10:04:45 +08:00
|
|
|
attributes.outlineSize = _richText->getAnchorTextOutlineSize();
|
2016-03-07 09:50:47 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_richText->isAnchorTextShadowEnabled())
|
|
|
|
{
|
|
|
|
attributes.effect = StyleEffect::SHADOW;
|
|
|
|
attributes.shadowColor = _richText->getAnchorTextShadowColor3B();
|
|
|
|
attributes.shadowOffset = _richText->getAnchorTextShadowOffset();
|
2016-03-07 09:50:47 +08:00
|
|
|
attributes.shadowBlurRadius = _richText->getAnchorTextShadowBlurRadius();
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_richText->isAnchorTextGlowEnabled())
|
|
|
|
{
|
|
|
|
attributes.effect = StyleEffect::GLOW;
|
2016-03-07 09:50:47 +08:00
|
|
|
attributes.glowColor = _richText->getAnchorTextGlowColor3B();
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (attrValueMap.find(RichText::KEY_TEXT_STYLE) != attrValueMap.end())
|
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
auto keyTextStyle = attrValueMap.at(RichText::KEY_TEXT_STYLE).asString();
|
2021-12-25 10:04:45 +08:00
|
|
|
if (keyTextStyle == RichText::VALUE_TEXT_STYLE_OUTLINE)
|
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
attributes.effect = StyleEffect::OUTLINE;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (attrValueMap.find(RichText::KEY_TEXT_OUTLINE_COLOR) != attrValueMap.end())
|
|
|
|
{
|
|
|
|
attributes.outlineColor = _richText->color3BWithString(
|
|
|
|
attrValueMap.at(RichText::KEY_TEXT_OUTLINE_COLOR).asString());
|
2016-03-07 09:50:47 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (attrValueMap.find(RichText::KEY_TEXT_OUTLINE_SIZE) != attrValueMap.end())
|
|
|
|
{
|
2020-08-18 11:33:18 +08:00
|
|
|
attributes.outlineSize = attrValueMap.at(RichText::KEY_TEXT_OUTLINE_SIZE).asInt();
|
2016-03-07 09:50:47 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (keyTextStyle == RichText::VALUE_TEXT_STYLE_SHADOW)
|
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
attributes.effect = StyleEffect::SHADOW;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (attrValueMap.find(RichText::KEY_TEXT_SHADOW_COLOR) != attrValueMap.end())
|
|
|
|
{
|
|
|
|
attributes.shadowColor = _richText->color3BWithString(
|
|
|
|
attrValueMap.at(RichText::KEY_TEXT_SHADOW_COLOR).asString());
|
2016-03-07 09:50:47 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if ((attrValueMap.find(RichText::KEY_TEXT_SHADOW_OFFSET_WIDTH) != attrValueMap.end()) &&
|
|
|
|
(attrValueMap.find(RichText::KEY_TEXT_SHADOW_OFFSET_HEIGHT) != attrValueMap.end()))
|
|
|
|
{
|
|
|
|
attributes.shadowOffset =
|
|
|
|
Vec2(attrValueMap.at(RichText::KEY_TEXT_SHADOW_OFFSET_WIDTH).asFloat(),
|
|
|
|
attrValueMap.at(RichText::KEY_TEXT_SHADOW_OFFSET_HEIGHT).asFloat());
|
2016-03-07 09:50:47 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (attrValueMap.find(RichText::KEY_TEXT_SHADOW_BLUR_RADIUS) != attrValueMap.end())
|
|
|
|
{
|
|
|
|
attributes.shadowBlurRadius =
|
|
|
|
attrValueMap.at(RichText::KEY_TEXT_SHADOW_BLUR_RADIUS).asInt();
|
2016-03-07 09:50:47 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (keyTextStyle == RichText::VALUE_TEXT_STYLE_GLOW)
|
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
attributes.effect = StyleEffect::GLOW;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (attrValueMap.find(RichText::KEY_TEXT_GLOW_COLOR) != attrValueMap.end())
|
|
|
|
{
|
|
|
|
attributes.glowColor =
|
|
|
|
_richText->color3BWithString(attrValueMap.at(RichText::KEY_TEXT_GLOW_COLOR).asString());
|
2016-03-07 09:50:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2016-03-07 09:50:47 +08:00
|
|
|
pushBackFontElement(attributes);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (richElement)
|
|
|
|
{
|
|
|
|
if (richElement->equalType(RichElement::Type::IMAGE))
|
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
richElement->setColor(getColor());
|
|
|
|
auto* richElementImage = static_cast<RichElementImage*>(richElement);
|
|
|
|
richElementImage->setUrl(getURL());
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (richElement->equalType(RichElement::Type::NEWLINE))
|
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
richElement->setColor(getColor());
|
|
|
|
}
|
|
|
|
pushBackElement(richElement);
|
2016-01-20 12:41:31 +08:00
|
|
|
}
|
|
|
|
}
|
2016-03-07 09:50:47 +08:00
|
|
|
}
|
|
|
|
}
|
2016-01-20 12:41:31 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void MyXMLVisitor::endElement(void* /*ctx*/, const char* elementName)
|
2016-03-07 09:50:47 +08:00
|
|
|
{
|
|
|
|
auto it = _tagTables.find(elementName);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (it != _tagTables.end())
|
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
auto tagBehavior = it->second;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (tagBehavior.isFontElement)
|
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
popBackFontElement();
|
2016-01-20 12:41:31 +08:00
|
|
|
}
|
2016-03-07 09:50:47 +08:00
|
|
|
}
|
|
|
|
}
|
2016-01-20 12:41:31 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void MyXMLVisitor::textHandler(void* /*ctx*/, const char* str, size_t len)
|
2016-03-07 09:50:47 +08:00
|
|
|
{
|
2016-08-25 07:37:03 +08:00
|
|
|
std::string text(str, len);
|
2021-12-25 10:04:45 +08:00
|
|
|
auto color = getColor();
|
|
|
|
auto face = getFace();
|
|
|
|
auto fontSize = getFontSize();
|
|
|
|
auto italics = getItalics();
|
|
|
|
auto underline = getUnderline();
|
2016-03-07 09:50:47 +08:00
|
|
|
auto strikethrough = getStrikethrough();
|
2021-12-25 10:04:45 +08:00
|
|
|
auto bold = getBold();
|
|
|
|
auto url = getURL();
|
|
|
|
auto outline = getOutline();
|
|
|
|
auto shadow = getShadow();
|
|
|
|
auto glow = getGlow();
|
|
|
|
|
2016-03-07 09:50:47 +08:00
|
|
|
uint32_t flags = 0;
|
|
|
|
if (italics)
|
|
|
|
flags |= RichElementText::ITALICS_FLAG;
|
|
|
|
if (bold)
|
|
|
|
flags |= RichElementText::BOLD_FLAG;
|
|
|
|
if (underline)
|
|
|
|
flags |= RichElementText::UNDERLINE_FLAG;
|
|
|
|
if (strikethrough)
|
|
|
|
flags |= RichElementText::STRIKETHROUGH_FLAG;
|
2020-08-06 17:37:34 +08:00
|
|
|
if (!url.empty())
|
2016-03-07 09:50:47 +08:00
|
|
|
flags |= RichElementText::URL_FLAG;
|
|
|
|
if (std::get<0>(outline))
|
|
|
|
flags |= RichElementText::OUTLINE_FLAG;
|
|
|
|
if (std::get<0>(shadow))
|
|
|
|
flags |= RichElementText::SHADOW_FLAG;
|
|
|
|
if (std::get<0>(glow))
|
|
|
|
flags |= RichElementText::GLOW_FLAG;
|
2016-08-25 07:37:03 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
auto element = RichElementText::create(0, color, 255, text, face, fontSize, flags, url, std::get<1>(outline),
|
|
|
|
std::get<2>(outline), std::get<1>(shadow), std::get<2>(shadow),
|
|
|
|
std::get<3>(shadow), std::get<1>(glow));
|
2016-03-07 09:50:47 +08:00
|
|
|
_richText->pushBackElement(element);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyXMLVisitor::pushBackFontElement(const MyXMLVisitor::Attributes& attribs)
|
|
|
|
{
|
|
|
|
_fontElements.push_back(attribs);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyXMLVisitor::popBackFontElement()
|
|
|
|
{
|
|
|
|
_fontElements.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyXMLVisitor::pushBackElement(RichElement* element)
|
|
|
|
{
|
|
|
|
_richText->pushBackElement(element);
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
void MyXMLVisitor::setTagDescription(std::string_view tag,
|
2021-12-25 10:04:45 +08:00
|
|
|
bool isFontElement,
|
|
|
|
RichText::VisitEnterHandler&& handleVisitEnter)
|
2016-03-07 09:50:47 +08:00
|
|
|
{
|
2021-12-26 23:26:34 +08:00
|
|
|
hlookup::set_item(
|
|
|
|
MyXMLVisitor::_tagTables, tag,
|
|
|
|
TagBehavior{
|
|
|
|
isFontElement,
|
2021-12-28 11:00:34 +08:00
|
|
|
std::move(
|
|
|
|
handleVisitEnter)}); // MyXMLVisitor::_tagTables[tag] = {isFontElement, std::move(handleVisitEnter)};
|
2016-03-07 09:50:47 +08:00
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
void MyXMLVisitor::removeTagDescription(std::string_view tag)
|
2016-03-07 09:50:47 +08:00
|
|
|
{
|
|
|
|
MyXMLVisitor::_tagTables.erase(tag);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
ValueMap MyXMLVisitor::tagAttrMapWithXMLElement(const char** attrs)
|
2016-03-07 09:50:47 +08:00
|
|
|
{
|
|
|
|
ValueMap tagAttrValueMap;
|
2021-12-25 10:04:45 +08:00
|
|
|
for (const char** attr = attrs; *attr != nullptr; attr = (attrs += 2))
|
|
|
|
{
|
|
|
|
if (attr[0] && attr[1])
|
|
|
|
{
|
2016-08-25 07:37:03 +08:00
|
|
|
tagAttrValueMap[attr[0]] = attr[1];
|
2016-01-20 12:41:31 +08:00
|
|
|
}
|
|
|
|
}
|
2016-03-07 09:50:47 +08:00
|
|
|
return tagAttrValueMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string RichText::KEY_VERTICAL_SPACE("KEY_VERTICAL_SPACE");
|
|
|
|
const std::string RichText::KEY_WRAP_MODE("KEY_WRAP_MODE");
|
2017-07-05 09:25:12 +08:00
|
|
|
const std::string RichText::KEY_HORIZONTAL_ALIGNMENT("KEY_HORIZONTAL_ALIGNMENT");
|
2016-03-07 09:50:47 +08:00
|
|
|
const std::string RichText::KEY_FONT_COLOR_STRING("KEY_FONT_COLOR_STRING");
|
|
|
|
const std::string RichText::KEY_FONT_SIZE("KEY_FONT_SIZE");
|
|
|
|
const std::string RichText::KEY_FONT_SMALL("KEY_FONT_SMALL");
|
|
|
|
const std::string RichText::KEY_FONT_BIG("KEY_FONT_BIG");
|
|
|
|
const std::string RichText::KEY_FONT_FACE("KEY_FONT_FACE");
|
|
|
|
const std::string RichText::KEY_TEXT_BOLD("KEY_TEXT_BOLD");
|
|
|
|
const std::string RichText::KEY_TEXT_ITALIC("KEY_TEXT_ITALIC");
|
|
|
|
const std::string RichText::KEY_TEXT_LINE("KEY_TEXT_LINE");
|
|
|
|
const std::string RichText::VALUE_TEXT_LINE_NONE("VALUE_TEXT_LINE_NONE");
|
|
|
|
const std::string RichText::VALUE_TEXT_LINE_DEL("VALUE_TEXT_LINE_DEL");
|
|
|
|
const std::string RichText::VALUE_TEXT_LINE_UNDER("VALUE_TEXT_LINE_UNDER");
|
|
|
|
const std::string RichText::KEY_TEXT_STYLE("KEY_TEXT_STYLE");
|
|
|
|
const std::string RichText::VALUE_TEXT_STYLE_NONE("VALUE_TEXT_STYLE_NONE");
|
|
|
|
const std::string RichText::VALUE_TEXT_STYLE_OUTLINE("VALUE_TEXT_STYLE_OUTLINE");
|
|
|
|
const std::string RichText::VALUE_TEXT_STYLE_SHADOW("VALUE_TEXT_STYLE_SHADOW");
|
|
|
|
const std::string RichText::VALUE_TEXT_STYLE_GLOW("VALUE_TEXT_STYLE_GLOW");
|
|
|
|
const std::string RichText::KEY_TEXT_OUTLINE_COLOR("KEY_TEXT_OUTLINE_COLOR");
|
|
|
|
const std::string RichText::KEY_TEXT_OUTLINE_SIZE("KEY_TEXT_OUTLINE_SIZE");
|
|
|
|
const std::string RichText::KEY_TEXT_SHADOW_COLOR("KEY_TEXT_SHADOW_COLOR");
|
|
|
|
const std::string RichText::KEY_TEXT_SHADOW_OFFSET_WIDTH("KEY_TEXT_SHADOW_OFFSET_WIDTH");
|
|
|
|
const std::string RichText::KEY_TEXT_SHADOW_OFFSET_HEIGHT("KEY_TEXT_SHADOW_OFFSET_HEIGHT");
|
|
|
|
const std::string RichText::KEY_TEXT_SHADOW_BLUR_RADIUS("KEY_TEXT_SHADOW_BLUR_RADIUS");
|
|
|
|
const std::string RichText::KEY_TEXT_GLOW_COLOR("KEY_TEXT_GLOW_COLOR");
|
|
|
|
const std::string RichText::KEY_URL("KEY_URL");
|
|
|
|
const std::string RichText::KEY_ANCHOR_FONT_COLOR_STRING("KEY_ANCHOR_FONT_COLOR_STRING");
|
|
|
|
const std::string RichText::KEY_ANCHOR_TEXT_BOLD("KEY_ANCHOR_TEXT_BOLD");
|
|
|
|
const std::string RichText::KEY_ANCHOR_TEXT_ITALIC("KEY_ANCHOR_TEXT_ITALIC");
|
|
|
|
const std::string RichText::KEY_ANCHOR_TEXT_LINE("KEY_ANCHOR_TEXT_LINE");
|
|
|
|
const std::string RichText::KEY_ANCHOR_TEXT_STYLE("KEY_ANCHOR_TEXT_STYLE");
|
|
|
|
const std::string RichText::KEY_ANCHOR_TEXT_OUTLINE_COLOR("KEY_ANCHOR_TEXT_OUTLINE_COLOR");
|
|
|
|
const std::string RichText::KEY_ANCHOR_TEXT_OUTLINE_SIZE("KEY_ANCHOR_TEXT_OUTLINE_SIZE");
|
|
|
|
const std::string RichText::KEY_ANCHOR_TEXT_SHADOW_COLOR("KEY_ANCHOR_TEXT_SHADOW_COLOR");
|
|
|
|
const std::string RichText::KEY_ANCHOR_TEXT_SHADOW_OFFSET_WIDTH("KEY_ANCHOR_TEXT_SHADOW_OFFSET_WIDTH");
|
|
|
|
const std::string RichText::KEY_ANCHOR_TEXT_SHADOW_OFFSET_HEIGHT("KEY_ANCHOR_TEXT_SHADOW_OFFSET_HEIGHT");
|
|
|
|
const std::string RichText::KEY_ANCHOR_TEXT_SHADOW_BLUR_RADIUS("KEY_ANCHOR_TEXT_SHADOW_BLUR_RADIUS");
|
|
|
|
const std::string RichText::KEY_ANCHOR_TEXT_GLOW_COLOR("KEY_ANCHOR_TEXT_GLOW_COLOR");
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
RichText::RichText() : _formatTextDirty(true), _leftSpaceWidth(0.0f)
|
2016-03-07 09:50:47 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_defaults[KEY_VERTICAL_SPACE] = 0.0f;
|
|
|
|
_defaults[KEY_WRAP_MODE] = static_cast<int>(WrapMode::WRAP_PER_WORD);
|
|
|
|
_defaults[KEY_HORIZONTAL_ALIGNMENT] = static_cast<int>(HorizontalAlignment::LEFT);
|
|
|
|
_defaults[KEY_FONT_COLOR_STRING] = "#ffffff";
|
|
|
|
_defaults[KEY_FONT_SIZE] = 12.0f;
|
|
|
|
_defaults[KEY_FONT_FACE] = "Verdana";
|
2016-03-07 09:50:47 +08:00
|
|
|
_defaults[KEY_ANCHOR_FONT_COLOR_STRING] = "#0000FF";
|
2021-12-25 10:04:45 +08:00
|
|
|
_defaults[KEY_ANCHOR_TEXT_BOLD] = false;
|
|
|
|
_defaults[KEY_ANCHOR_TEXT_ITALIC] = false;
|
|
|
|
_defaults[KEY_ANCHOR_TEXT_LINE] = VALUE_TEXT_LINE_NONE;
|
|
|
|
_defaults[KEY_ANCHOR_TEXT_STYLE] = VALUE_TEXT_STYLE_NONE;
|
2016-03-07 09:50:47 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2016-03-07 09:50:47 +08:00
|
|
|
RichText::~RichText()
|
|
|
|
{
|
|
|
|
_richElements.clear();
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2016-03-07 09:50:47 +08:00
|
|
|
RichText* RichText::create()
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
RichText* widget = new RichText();
|
|
|
|
if (widget->init())
|
2016-01-20 12:41:31 +08:00
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
widget->autorelease();
|
|
|
|
return widget;
|
2016-01-20 12:41:31 +08:00
|
|
|
}
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE(widget);
|
2016-03-07 09:50:47 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
2016-01-20 12:41:31 +08:00
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
RichText* RichText::createWithXML(std::string_view xml, const ValueMap& defaults, const OpenUrlHandler& handleOpenUrl)
|
2016-03-07 09:50:47 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
RichText* widget = new RichText();
|
|
|
|
if (widget->initWithXML(xml, defaults, handleOpenUrl))
|
2016-03-07 09:50:47 +08:00
|
|
|
{
|
|
|
|
widget->autorelease();
|
|
|
|
return widget;
|
|
|
|
}
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE(widget);
|
2016-03-07 09:50:47 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RichText::init()
|
|
|
|
{
|
|
|
|
if (Widget::init())
|
2016-01-20 12:41:31 +08:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2016-03-07 09:50:47 +08:00
|
|
|
return false;
|
|
|
|
}
|
2016-01-20 12:41:31 +08:00
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
bool RichText::initWithXML(std::string_view origxml, const ValueMap& defaults, const OpenUrlHandler& handleOpenUrl)
|
2016-01-20 12:41:31 +08:00
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
static std::function<std::string(RichText*)> startTagFont = [](RichText* richText) {
|
|
|
|
std::string fontFace = richText->getFontFace();
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << richText->getFontSize();
|
2021-12-25 10:04:45 +08:00
|
|
|
std::string fontSize = ss.str();
|
2016-03-07 09:50:47 +08:00
|
|
|
std::string fontColor = richText->getFontColor();
|
|
|
|
return "<font face=\"" + fontFace + "\" size=\"" + fontSize + "\" color=\"" + fontColor + "\">";
|
|
|
|
};
|
2016-01-20 12:41:31 +08:00
|
|
|
if (Widget::init())
|
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
setDefaults(defaults);
|
|
|
|
setOpenUrlHandler(handleOpenUrl);
|
2016-01-20 12:41:31 +08:00
|
|
|
|
|
|
|
// solves to issues:
|
|
|
|
// - creates defaults values
|
|
|
|
// - makes sure that the xml well formed and starts with an element
|
2016-03-07 09:50:47 +08:00
|
|
|
std::string xml = startTagFont(this);
|
|
|
|
xml += origxml;
|
|
|
|
xml += "</font>";
|
2016-01-20 12:41:31 +08:00
|
|
|
|
2016-08-25 07:37:03 +08:00
|
|
|
MyXMLVisitor visitor(this);
|
|
|
|
SAXParser parser;
|
|
|
|
parser.setDelegator(&visitor);
|
|
|
|
return parser.parseIntrusive(&xml.front(), xml.length());
|
2016-01-20 12:41:31 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void RichText::initRenderer() {}
|
|
|
|
|
|
|
|
void RichText::insertElement(RichElement* element, int index)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
_richElements.insert(index, element);
|
|
|
|
_formatTextDirty = true;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
void RichText::pushBackElement(RichElement* element)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
_richElements.pushBack(element);
|
|
|
|
_formatTextDirty = true;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
void RichText::removeElement(int index)
|
|
|
|
{
|
|
|
|
_richElements.erase(index);
|
|
|
|
_formatTextDirty = true;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
void RichText::removeElement(RichElement* element)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
_richElements.eraseObject(element);
|
|
|
|
_formatTextDirty = true;
|
|
|
|
}
|
2016-01-22 13:44:45 +08:00
|
|
|
|
|
|
|
RichText::WrapMode RichText::getWrapMode() const
|
|
|
|
{
|
2020-08-18 11:33:18 +08:00
|
|
|
return static_cast<RichText::WrapMode>(_defaults.at(KEY_WRAP_MODE).asInt());
|
2016-01-22 13:44:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void RichText::setWrapMode(RichText::WrapMode wrapMode)
|
|
|
|
{
|
2020-08-18 11:33:18 +08:00
|
|
|
if (static_cast<RichText::WrapMode>(_defaults.at(KEY_WRAP_MODE).asInt()) != wrapMode)
|
2016-01-22 13:44:45 +08:00
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
_defaults[KEY_WRAP_MODE] = static_cast<int>(wrapMode);
|
2021-12-25 10:04:45 +08:00
|
|
|
_formatTextDirty = true;
|
2016-01-22 13:44:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-05 09:25:12 +08:00
|
|
|
RichText::HorizontalAlignment RichText::getHorizontalAlignment() const
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
return static_cast<RichText::HorizontalAlignment>(_defaults.at(KEY_HORIZONTAL_ALIGNMENT).asInt());
|
2017-07-05 09:25:12 +08:00
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
void RichText::setHorizontalAlignment(axis::ui::RichText::HorizontalAlignment a)
|
2017-07-05 09:25:12 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (static_cast<RichText::HorizontalAlignment>(_defaults.at(KEY_HORIZONTAL_ALIGNMENT).asInt()) != a)
|
|
|
|
{
|
|
|
|
_defaults[KEY_HORIZONTAL_ALIGNMENT] = static_cast<int>(a);
|
|
|
|
_formatTextDirty = true;
|
|
|
|
}
|
2017-07-05 09:25:12 +08:00
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
void RichText::setFontColor(std::string_view color)
|
2016-03-07 09:50:47 +08:00
|
|
|
{
|
|
|
|
_defaults[KEY_FONT_COLOR_STRING] = color;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string RichText::getFontColor()
|
|
|
|
{
|
|
|
|
return _defaults.at(KEY_FONT_COLOR_STRING).asString();
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
axis::Color3B RichText::getFontColor3B()
|
2016-03-07 09:50:47 +08:00
|
|
|
{
|
|
|
|
return color3BWithString(getFontColor());
|
|
|
|
}
|
|
|
|
|
|
|
|
void RichText::setFontSize(float size)
|
|
|
|
{
|
|
|
|
_defaults[KEY_FONT_SIZE] = size;
|
|
|
|
}
|
|
|
|
|
|
|
|
float RichText::getFontSize()
|
|
|
|
{
|
2020-08-18 11:33:18 +08:00
|
|
|
return _defaults.at(KEY_FONT_SIZE).asFloat();
|
2016-03-07 09:50:47 +08:00
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
void RichText::setFontFace(std::string_view face)
|
2016-03-07 09:50:47 +08:00
|
|
|
{
|
|
|
|
_defaults[KEY_FONT_FACE] = face;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string RichText::getFontFace()
|
|
|
|
{
|
|
|
|
return _defaults.at(KEY_FONT_FACE).asString();
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
void RichText::setAnchorFontColor(std::string_view color)
|
2016-03-07 09:50:47 +08:00
|
|
|
{
|
|
|
|
_defaults[KEY_ANCHOR_FONT_COLOR_STRING] = color;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string RichText::getAnchorFontColor()
|
|
|
|
{
|
|
|
|
return _defaults.at(KEY_ANCHOR_FONT_COLOR_STRING).asString();
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
axis::Color3B RichText::getAnchorFontColor3B()
|
2016-03-07 09:50:47 +08:00
|
|
|
{
|
|
|
|
return color3BWithString(getAnchorFontColor());
|
|
|
|
}
|
|
|
|
|
|
|
|
void RichText::setAnchorTextBold(bool enable)
|
|
|
|
{
|
|
|
|
_defaults[KEY_ANCHOR_TEXT_BOLD] = enable;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RichText::isAnchorTextBoldEnabled()
|
|
|
|
{
|
2020-08-18 11:33:18 +08:00
|
|
|
return _defaults[KEY_ANCHOR_TEXT_BOLD].asBool();
|
2016-03-07 09:50:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void RichText::setAnchorTextItalic(bool enable)
|
|
|
|
{
|
|
|
|
_defaults[KEY_ANCHOR_TEXT_ITALIC] = enable;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RichText::isAnchorTextItalicEnabled()
|
|
|
|
{
|
2020-08-18 11:33:18 +08:00
|
|
|
return _defaults[KEY_ANCHOR_TEXT_ITALIC].asBool();
|
2016-03-07 09:50:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void RichText::setAnchorTextDel(bool enable)
|
|
|
|
{
|
|
|
|
if (enable)
|
|
|
|
_defaults[KEY_ANCHOR_TEXT_LINE] = VALUE_TEXT_LINE_DEL;
|
|
|
|
else if (_defaults[KEY_ANCHOR_TEXT_LINE].asString() == VALUE_TEXT_LINE_DEL)
|
|
|
|
_defaults[KEY_ANCHOR_TEXT_LINE] = VALUE_TEXT_LINE_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RichText::isAnchorTextDelEnabled()
|
|
|
|
{
|
|
|
|
return (_defaults[KEY_ANCHOR_TEXT_LINE].asString() == VALUE_TEXT_LINE_DEL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RichText::setAnchorTextUnderline(bool enable)
|
|
|
|
{
|
|
|
|
if (enable)
|
|
|
|
_defaults[KEY_ANCHOR_TEXT_LINE] = VALUE_TEXT_LINE_UNDER;
|
|
|
|
else if (_defaults[KEY_ANCHOR_TEXT_LINE].asString() == VALUE_TEXT_LINE_UNDER)
|
|
|
|
_defaults[KEY_ANCHOR_TEXT_LINE] = VALUE_TEXT_LINE_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RichText::isAnchorTextUnderlineEnabled()
|
|
|
|
{
|
|
|
|
return (_defaults[KEY_ANCHOR_TEXT_LINE].asString() == VALUE_TEXT_LINE_UNDER);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RichText::setAnchorTextOutline(bool enable, const Color3B& outlineColor, int outlineSize)
|
|
|
|
{
|
|
|
|
if (enable)
|
|
|
|
_defaults[KEY_ANCHOR_TEXT_STYLE] = VALUE_TEXT_STYLE_OUTLINE;
|
|
|
|
else if (_defaults[KEY_ANCHOR_TEXT_STYLE].asString() == VALUE_TEXT_STYLE_OUTLINE)
|
|
|
|
_defaults[KEY_ANCHOR_TEXT_STYLE] = VALUE_TEXT_STYLE_NONE;
|
|
|
|
_defaults[KEY_ANCHOR_TEXT_OUTLINE_COLOR] = stringWithColor3B(outlineColor);
|
2021-12-25 10:04:45 +08:00
|
|
|
_defaults[KEY_ANCHOR_TEXT_OUTLINE_SIZE] = outlineSize;
|
2016-03-07 09:50:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RichText::isAnchorTextOutlineEnabled()
|
|
|
|
{
|
|
|
|
return (_defaults[KEY_ANCHOR_TEXT_STYLE].asString() == VALUE_TEXT_STYLE_OUTLINE);
|
|
|
|
}
|
|
|
|
|
|
|
|
Color3B RichText::getAnchorTextOutlineColor3B()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_defaults.find(KEY_ANCHOR_TEXT_OUTLINE_COLOR) != _defaults.end())
|
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
return color3BWithString(_defaults.at(KEY_ANCHOR_TEXT_OUTLINE_COLOR).asString());
|
|
|
|
}
|
|
|
|
return Color3B();
|
|
|
|
}
|
|
|
|
|
|
|
|
int RichText::getAnchorTextOutlineSize()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_defaults.find(KEY_ANCHOR_TEXT_OUTLINE_SIZE) != _defaults.end())
|
|
|
|
{
|
2020-08-18 11:33:18 +08:00
|
|
|
return _defaults.at(KEY_ANCHOR_TEXT_OUTLINE_SIZE).asInt();
|
2016-03-07 09:50:47 +08:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-10-23 23:27:14 +08:00
|
|
|
void RichText::setAnchorTextShadow(bool enable, const Color3B& shadowColor, const Vec2& offset, int blurRadius)
|
2016-03-07 09:50:47 +08:00
|
|
|
{
|
|
|
|
if (enable)
|
|
|
|
_defaults[KEY_ANCHOR_TEXT_STYLE] = VALUE_TEXT_STYLE_SHADOW;
|
|
|
|
else if (_defaults[KEY_ANCHOR_TEXT_STYLE].asString() == VALUE_TEXT_STYLE_SHADOW)
|
|
|
|
_defaults[KEY_ANCHOR_TEXT_STYLE] = VALUE_TEXT_STYLE_NONE;
|
2021-12-25 10:04:45 +08:00
|
|
|
_defaults[KEY_ANCHOR_TEXT_SHADOW_COLOR] = stringWithColor3B(shadowColor);
|
|
|
|
_defaults[KEY_ANCHOR_TEXT_SHADOW_OFFSET_WIDTH] = offset.width;
|
2016-03-07 09:50:47 +08:00
|
|
|
_defaults[KEY_ANCHOR_TEXT_SHADOW_OFFSET_HEIGHT] = offset.height;
|
2021-12-25 10:04:45 +08:00
|
|
|
_defaults[KEY_ANCHOR_TEXT_SHADOW_BLUR_RADIUS] = blurRadius;
|
2016-03-07 09:50:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RichText::isAnchorTextShadowEnabled()
|
|
|
|
{
|
|
|
|
return (_defaults[KEY_ANCHOR_TEXT_STYLE].asString() == VALUE_TEXT_STYLE_SHADOW);
|
|
|
|
}
|
|
|
|
|
|
|
|
Color3B RichText::getAnchorTextShadowColor3B()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_defaults.find(KEY_ANCHOR_TEXT_SHADOW_COLOR) != _defaults.end())
|
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
return color3BWithString(_defaults.at(KEY_ANCHOR_TEXT_SHADOW_COLOR).asString());
|
|
|
|
}
|
|
|
|
return Color3B();
|
|
|
|
}
|
|
|
|
|
2021-10-23 23:27:14 +08:00
|
|
|
Vec2 RichText::getAnchorTextShadowOffset()
|
2016-03-07 09:50:47 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
float width = 2.0f;
|
2016-03-07 09:50:47 +08:00
|
|
|
float height = -2.0f;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_defaults.find(KEY_ANCHOR_TEXT_SHADOW_OFFSET_WIDTH) != _defaults.end())
|
|
|
|
{
|
2020-08-18 11:33:18 +08:00
|
|
|
width = _defaults.at(KEY_ANCHOR_TEXT_SHADOW_OFFSET_WIDTH).asFloat();
|
2016-03-07 09:50:47 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_defaults.find(KEY_ANCHOR_TEXT_SHADOW_OFFSET_HEIGHT) != _defaults.end())
|
|
|
|
{
|
2020-08-18 11:33:18 +08:00
|
|
|
height = _defaults.at(KEY_ANCHOR_TEXT_SHADOW_OFFSET_HEIGHT).asFloat();
|
2016-03-07 09:50:47 +08:00
|
|
|
}
|
2021-10-23 23:27:14 +08:00
|
|
|
return Vec2(width, height);
|
2016-03-07 09:50:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int RichText::getAnchorTextShadowBlurRadius()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_defaults.find(KEY_ANCHOR_TEXT_SHADOW_BLUR_RADIUS) != _defaults.end())
|
|
|
|
{
|
2020-08-18 11:33:18 +08:00
|
|
|
return _defaults.at(KEY_ANCHOR_TEXT_SHADOW_BLUR_RADIUS).asInt();
|
2016-03-07 09:50:47 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RichText::setAnchorTextGlow(bool enable, const Color3B& glowColor)
|
|
|
|
{
|
|
|
|
if (enable)
|
|
|
|
_defaults[KEY_ANCHOR_TEXT_STYLE] = VALUE_TEXT_STYLE_GLOW;
|
|
|
|
else if (_defaults[KEY_ANCHOR_TEXT_STYLE].asString() == VALUE_TEXT_STYLE_GLOW)
|
|
|
|
_defaults[KEY_ANCHOR_TEXT_STYLE] = VALUE_TEXT_STYLE_NONE;
|
|
|
|
_defaults[KEY_ANCHOR_TEXT_GLOW_COLOR] = stringWithColor3B(glowColor);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RichText::isAnchorTextGlowEnabled()
|
|
|
|
{
|
|
|
|
return (_defaults[KEY_ANCHOR_TEXT_STYLE].asString() == VALUE_TEXT_STYLE_GLOW);
|
|
|
|
}
|
|
|
|
|
|
|
|
Color3B RichText::getAnchorTextGlowColor3B()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_defaults.find(KEY_ANCHOR_TEXT_GLOW_COLOR) != _defaults.end())
|
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
return color3BWithString(_defaults.at(KEY_ANCHOR_TEXT_GLOW_COLOR).asString());
|
|
|
|
}
|
|
|
|
return Color3B();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RichText::setDefaults(const ValueMap& defaults)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (defaults.find(KEY_VERTICAL_SPACE) != defaults.end())
|
|
|
|
{
|
2020-08-18 11:33:18 +08:00
|
|
|
_defaults[KEY_VERTICAL_SPACE] = defaults.at(KEY_VERTICAL_SPACE).asFloat();
|
2016-03-07 09:50:47 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (defaults.find(KEY_WRAP_MODE) != defaults.end())
|
|
|
|
{
|
2020-08-18 11:33:18 +08:00
|
|
|
_defaults[KEY_WRAP_MODE] = defaults.at(KEY_WRAP_MODE).asInt();
|
2016-03-07 09:50:47 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (defaults.find(KEY_HORIZONTAL_ALIGNMENT) != defaults.end())
|
|
|
|
{
|
|
|
|
_defaults[KEY_HORIZONTAL_ALIGNMENT] = defaults.at(KEY_HORIZONTAL_ALIGNMENT).asInt();
|
|
|
|
}
|
|
|
|
if (defaults.find(KEY_FONT_COLOR_STRING) != defaults.end())
|
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
_defaults[KEY_FONT_COLOR_STRING] = defaults.at(KEY_FONT_COLOR_STRING).asString();
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (defaults.find(KEY_FONT_SIZE) != defaults.end())
|
|
|
|
{
|
2020-08-18 11:33:18 +08:00
|
|
|
_defaults[KEY_FONT_SIZE] = defaults.at(KEY_FONT_SIZE).asFloat();
|
2016-03-07 09:50:47 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (defaults.find(KEY_FONT_FACE) != defaults.end())
|
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
_defaults[KEY_FONT_FACE] = defaults.at(KEY_FONT_FACE).asString();
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (defaults.find(KEY_ANCHOR_FONT_COLOR_STRING) != defaults.end())
|
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
_defaults[KEY_ANCHOR_FONT_COLOR_STRING] = defaults.at(KEY_ANCHOR_FONT_COLOR_STRING).asString();
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (defaults.find(KEY_ANCHOR_TEXT_BOLD) != defaults.end())
|
|
|
|
{
|
2020-08-18 11:33:18 +08:00
|
|
|
_defaults[KEY_ANCHOR_TEXT_BOLD] = defaults.at(KEY_ANCHOR_TEXT_BOLD).asBool();
|
2016-03-07 09:50:47 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (defaults.find(KEY_ANCHOR_TEXT_ITALIC) != defaults.end())
|
|
|
|
{
|
2020-08-18 11:33:18 +08:00
|
|
|
_defaults[KEY_ANCHOR_TEXT_ITALIC] = defaults.at(KEY_ANCHOR_TEXT_ITALIC).asBool();
|
2016-03-07 09:50:47 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (defaults.find(KEY_ANCHOR_TEXT_LINE) != defaults.end())
|
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
_defaults[KEY_ANCHOR_TEXT_LINE] = defaults.at(KEY_ANCHOR_TEXT_LINE).asString();
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (defaults.find(KEY_ANCHOR_TEXT_STYLE) != defaults.end())
|
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
_defaults[KEY_ANCHOR_TEXT_STYLE] = defaults.at(KEY_ANCHOR_TEXT_STYLE).asString();
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (defaults.find(KEY_ANCHOR_TEXT_OUTLINE_COLOR) != defaults.end())
|
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
_defaults[KEY_ANCHOR_TEXT_OUTLINE_COLOR] = defaults.at(KEY_ANCHOR_TEXT_OUTLINE_COLOR).asString();
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (defaults.find(KEY_ANCHOR_TEXT_OUTLINE_SIZE) != defaults.end())
|
|
|
|
{
|
2020-08-18 11:33:18 +08:00
|
|
|
_defaults[KEY_ANCHOR_TEXT_OUTLINE_SIZE] = defaults.at(KEY_ANCHOR_TEXT_OUTLINE_SIZE).asInt();
|
2016-03-07 09:50:47 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (defaults.find(KEY_ANCHOR_TEXT_SHADOW_COLOR) != defaults.end())
|
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
_defaults[KEY_ANCHOR_TEXT_SHADOW_COLOR] = defaults.at(KEY_ANCHOR_TEXT_SHADOW_COLOR).asString();
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (defaults.find(KEY_ANCHOR_TEXT_SHADOW_OFFSET_WIDTH) != defaults.end())
|
|
|
|
{
|
2020-08-18 11:33:18 +08:00
|
|
|
_defaults[KEY_ANCHOR_TEXT_SHADOW_OFFSET_WIDTH] = defaults.at(KEY_ANCHOR_TEXT_SHADOW_OFFSET_WIDTH).asFloat();
|
2016-03-07 09:50:47 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (defaults.find(KEY_ANCHOR_TEXT_SHADOW_OFFSET_HEIGHT) != defaults.end())
|
|
|
|
{
|
2020-08-18 11:33:18 +08:00
|
|
|
_defaults[KEY_ANCHOR_TEXT_SHADOW_OFFSET_HEIGHT] = defaults.at(KEY_ANCHOR_TEXT_SHADOW_OFFSET_HEIGHT).asFloat();
|
2016-03-07 09:50:47 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (defaults.find(KEY_ANCHOR_TEXT_SHADOW_BLUR_RADIUS) != defaults.end())
|
|
|
|
{
|
2020-08-18 11:33:18 +08:00
|
|
|
_defaults[KEY_ANCHOR_TEXT_SHADOW_BLUR_RADIUS] = defaults.at(KEY_ANCHOR_TEXT_SHADOW_BLUR_RADIUS).asInt();
|
2016-03-07 09:50:47 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (defaults.find(KEY_ANCHOR_TEXT_GLOW_COLOR) != defaults.end())
|
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
_defaults[KEY_ANCHOR_TEXT_GLOW_COLOR] = defaults.at(KEY_ANCHOR_TEXT_GLOW_COLOR).asString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ValueMap RichText::getDefaults() const
|
|
|
|
{
|
|
|
|
ValueMap defaults;
|
|
|
|
return defaults;
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
axis::Color3B RichText::color3BWithString(std::string_view color)
|
2016-03-07 09:50:47 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (color.length() == 4)
|
|
|
|
{
|
2021-12-28 19:10:50 +08:00
|
|
|
unsigned int r, g, b;
|
2021-12-26 23:26:34 +08:00
|
|
|
sscanf(color.data(), "%*c%1x%1x%1x", &r, &g, &b);
|
2016-03-07 09:50:47 +08:00
|
|
|
r += r * 16;
|
|
|
|
g += g * 16;
|
|
|
|
b += b * 16;
|
|
|
|
return Color3B(r, g, b);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (color.length() == 7)
|
|
|
|
{
|
2021-12-28 19:10:50 +08:00
|
|
|
unsigned int r, g, b;
|
2021-12-26 23:26:34 +08:00
|
|
|
sscanf(color.data(), "%*c%2x%2x%2x", &r, &g, &b);
|
2016-03-07 09:50:47 +08:00
|
|
|
return Color3B(r, g, b);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (color.length() == 9)
|
|
|
|
{
|
2021-12-28 19:10:50 +08:00
|
|
|
unsigned int r, g, b, a;
|
2021-12-26 23:26:34 +08:00
|
|
|
sscanf(color.data(), "%*c%2x%2x%2x%2x", &r, &g, &b, &a);
|
2016-03-07 09:50:47 +08:00
|
|
|
return Color3B(r, g, b);
|
|
|
|
}
|
|
|
|
return Color3B::WHITE;
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
std::string RichText::stringWithColor3B(const axis::Color3B& color3b)
|
2016-03-07 09:50:47 +08:00
|
|
|
{
|
|
|
|
int r = color3b.r;
|
|
|
|
int g = color3b.g;
|
|
|
|
int b = color3b.b;
|
|
|
|
char buf[8];
|
|
|
|
snprintf(buf, sizeof(buf), "#%02x%02x%02x", r, g, b);
|
|
|
|
return std::string(buf, 7);
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
std::string RichText::stringWithColor4B(const axis::Color4B& color4b)
|
2016-03-07 09:50:47 +08:00
|
|
|
{
|
|
|
|
int r = color4b.r;
|
|
|
|
int g = color4b.g;
|
|
|
|
int b = color4b.b;
|
|
|
|
int a = color4b.a;
|
|
|
|
char buf[10];
|
|
|
|
snprintf(buf, sizeof(buf), "#%02x%02x%02x%02x", r, g, b, a);
|
|
|
|
return std::string(buf, 9);
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
void RichText::setTagDescription(std::string_view tag, bool isFontElement, VisitEnterHandler handleVisitEnter)
|
2016-03-07 09:50:47 +08:00
|
|
|
{
|
2020-08-06 17:37:34 +08:00
|
|
|
MyXMLVisitor::setTagDescription(tag, isFontElement, std::move(handleVisitEnter));
|
2016-03-07 09:50:47 +08:00
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
void RichText::removeTagDescription(std::string_view tag)
|
2016-03-07 09:50:47 +08:00
|
|
|
{
|
|
|
|
MyXMLVisitor::removeTagDescription(tag);
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
void RichText::openUrl(std::string_view url)
|
2016-03-07 09:50:47 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_handleOpenUrl)
|
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
_handleOpenUrl(url);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else
|
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
Application::getInstance()->openURL(url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RichText::setOpenUrlHandler(const OpenUrlHandler& handleOpenUrl)
|
|
|
|
{
|
|
|
|
_handleOpenUrl = handleOpenUrl;
|
|
|
|
}
|
|
|
|
|
2022-05-25 09:42:12 +08:00
|
|
|
void RichText::formatText(bool force)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2022-05-25 09:42:12 +08:00
|
|
|
_formatTextDirty |= force;
|
2014-03-11 17:13:54 +08:00
|
|
|
if (_formatTextDirty)
|
|
|
|
{
|
2015-07-28 11:38:21 +08:00
|
|
|
this->removeAllProtectedChildren();
|
2014-03-11 17:13:54 +08:00
|
|
|
_elementRenders.clear();
|
2017-11-08 13:59:57 +08:00
|
|
|
_lineHeights.clear();
|
2014-03-11 17:13:54 +08:00
|
|
|
if (_ignoreSize)
|
|
|
|
{
|
|
|
|
addNewLine();
|
2021-12-25 10:04:45 +08:00
|
|
|
for (ssize_t i = 0, size = _richElements.size(); i < size; ++i)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
RichElement* element = _richElements.at(i);
|
2014-07-10 00:45:27 +08:00
|
|
|
Node* elementRenderer = nullptr;
|
2014-03-11 17:13:54 +08:00
|
|
|
switch (element->_type)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
case RichElement::Type::TEXT:
|
|
|
|
{
|
|
|
|
RichElementText* elmtText = static_cast<RichElementText*>(element);
|
|
|
|
Label* label;
|
|
|
|
if (FileUtils::getInstance()->isFileExist(elmtText->_fontName))
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
label = Label::createWithTTF(elmtText->_text, elmtText->_fontName, elmtText->_fontSize);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
label = Label::createWithSystemFont(elmtText->_text, elmtText->_fontName, elmtText->_fontSize);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (elmtText->_flags & RichElementText::ITALICS_FLAG)
|
|
|
|
label->enableItalics();
|
|
|
|
if (elmtText->_flags & RichElementText::BOLD_FLAG)
|
|
|
|
label->enableBold();
|
|
|
|
if (elmtText->_flags & RichElementText::UNDERLINE_FLAG)
|
|
|
|
label->enableUnderline();
|
|
|
|
if (elmtText->_flags & RichElementText::STRIKETHROUGH_FLAG)
|
|
|
|
label->enableStrikethrough();
|
|
|
|
if (elmtText->_flags & RichElementText::URL_FLAG)
|
|
|
|
label->addComponent(ListenerComponent::create(
|
|
|
|
label, elmtText->_url, std::bind(&RichText::openUrl, this, std::placeholders::_1)));
|
|
|
|
if (elmtText->_flags & RichElementText::OUTLINE_FLAG)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
label->enableOutline(Color4B(elmtText->_outlineColor), elmtText->_outlineSize);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (elmtText->_flags & RichElementText::SHADOW_FLAG)
|
2015-09-28 13:33:17 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
label->enableShadow(Color4B(elmtText->_shadowColor), elmtText->_shadowOffset,
|
|
|
|
elmtText->_shadowBlurRadius);
|
2015-09-28 13:33:17 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (elmtText->_flags & RichElementText::GLOW_FLAG)
|
|
|
|
{
|
|
|
|
label->enableGlow(Color4B(elmtText->_glowColor));
|
|
|
|
}
|
|
|
|
label->setTextColor(Color4B(elmtText->_color));
|
|
|
|
elementRenderer = label;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case RichElement::Type::IMAGE:
|
|
|
|
{
|
|
|
|
RichElementImage* elmtImage = static_cast<RichElementImage*>(element);
|
|
|
|
if (elmtImage->_textureType == Widget::TextureResType::LOCAL)
|
|
|
|
elementRenderer = Sprite::create(elmtImage->_filePath);
|
|
|
|
else
|
|
|
|
elementRenderer = Sprite::createWithSpriteFrameName(elmtImage->_filePath);
|
|
|
|
|
|
|
|
if (elementRenderer && (elmtImage->_height != -1 || elmtImage->_width != -1))
|
|
|
|
{
|
|
|
|
auto currentSize = elementRenderer->getContentSize();
|
|
|
|
if (elmtImage->_width != -1)
|
|
|
|
elementRenderer->setScaleX(elmtImage->_width / currentSize.width);
|
|
|
|
if (elmtImage->_height != -1)
|
|
|
|
elementRenderer->setScaleY(elmtImage->_height / currentSize.height);
|
|
|
|
elementRenderer->setContentSize(Vec2(currentSize.width * elementRenderer->getScaleX(),
|
|
|
|
currentSize.height * elementRenderer->getScaleY()));
|
|
|
|
elementRenderer->addComponent(
|
|
|
|
ListenerComponent::create(elementRenderer, elmtImage->_url,
|
|
|
|
std::bind(&RichText::openUrl, this, std::placeholders::_1)));
|
|
|
|
elementRenderer->setColor(element->_color);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case RichElement::Type::CUSTOM:
|
|
|
|
{
|
|
|
|
RichElementCustomNode* elmtCustom = static_cast<RichElementCustomNode*>(element);
|
|
|
|
elementRenderer = elmtCustom->_customNode;
|
|
|
|
elementRenderer->setColor(element->_color);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case RichElement::Type::NEWLINE:
|
|
|
|
{
|
|
|
|
addNewLine();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2016-01-20 12:41:31 +08:00
|
|
|
|
|
|
|
if (elementRenderer)
|
|
|
|
{
|
|
|
|
elementRenderer->setOpacity(element->_opacity);
|
|
|
|
pushToContainer(elementRenderer);
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
addNewLine();
|
2021-12-25 10:04:45 +08:00
|
|
|
for (ssize_t i = 0, size = _richElements.size(); i < size; ++i)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
RichElement* element = static_cast<RichElement*>(_richElements.at(i));
|
|
|
|
switch (element->_type)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
case RichElement::Type::TEXT:
|
|
|
|
{
|
|
|
|
RichElementText* elmtText = static_cast<RichElementText*>(element);
|
|
|
|
handleTextRenderer(elmtText->_text, elmtText->_fontName, elmtText->_fontSize, elmtText->_color,
|
|
|
|
elmtText->_opacity, elmtText->_flags, elmtText->_url, elmtText->_outlineColor,
|
|
|
|
elmtText->_outlineSize, elmtText->_shadowColor, elmtText->_shadowOffset,
|
|
|
|
elmtText->_shadowBlurRadius, elmtText->_glowColor);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case RichElement::Type::IMAGE:
|
|
|
|
{
|
|
|
|
RichElementImage* elmtImage = static_cast<RichElementImage*>(element);
|
|
|
|
handleImageRenderer(elmtImage->_filePath, elmtImage->_textureType, elmtImage->_color,
|
|
|
|
elmtImage->_opacity, elmtImage->_width, elmtImage->_height, elmtImage->_url);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case RichElement::Type::CUSTOM:
|
|
|
|
{
|
|
|
|
RichElementCustomNode* elmtCustom = static_cast<RichElementCustomNode*>(element);
|
|
|
|
handleCustomRenderer(elmtCustom->_customNode);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case RichElement::Type::NEWLINE:
|
|
|
|
{
|
|
|
|
addNewLine();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-06 10:28:11 +08:00
|
|
|
formatRenderers();
|
2014-03-11 17:13:54 +08:00
|
|
|
_formatTextDirty = false;
|
|
|
|
}
|
|
|
|
}
|
2016-01-20 12:41:31 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
inline bool isUTF8CharWrappable(const StringUtils::StringUTF8::CharUTF8& ch)
|
|
|
|
{
|
|
|
|
return (!ch.isASCII() || !std::isalnum(ch._char[0], std::locale()));
|
|
|
|
}
|
2016-01-22 13:44:45 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
int getPrevWordPos(const StringUtils::StringUTF8& text, int idx)
|
|
|
|
{
|
|
|
|
if (idx <= 0)
|
|
|
|
return -1;
|
2017-11-08 13:59:57 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// start from idx-1
|
|
|
|
const StringUtils::StringUTF8::CharUTF8Store& str = text.getString();
|
|
|
|
auto it = std::find_if(str.rbegin() + (str.size() - idx + 1), str.rend(), isUTF8CharWrappable);
|
|
|
|
if (it == str.rend())
|
|
|
|
return -1;
|
|
|
|
return static_cast<int>(it.base() - str.begin());
|
|
|
|
}
|
2016-01-22 13:44:45 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
int getNextWordPos(const StringUtils::StringUTF8& text, int idx)
|
|
|
|
{
|
|
|
|
const StringUtils::StringUTF8::CharUTF8Store& str = text.getString();
|
|
|
|
if (idx + 1 >= static_cast<int>(str.size()))
|
|
|
|
return static_cast<int>(str.size());
|
2016-01-22 13:44:45 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
auto it = std::find_if(str.begin() + idx + 1, str.end(), isUTF8CharWrappable);
|
|
|
|
return static_cast<int>(it - str.begin());
|
|
|
|
}
|
2017-11-08 13:59:57 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool isWrappable(const StringUtils::StringUTF8& text)
|
|
|
|
{
|
|
|
|
const StringUtils::StringUTF8::CharUTF8Store& str = text.getString();
|
|
|
|
return std::any_of(str.begin(), str.end(), isUTF8CharWrappable);
|
|
|
|
}
|
2016-01-22 13:44:45 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
int findSplitPositionForWord(Label* label,
|
|
|
|
const StringUtils::StringUTF8& text,
|
|
|
|
int estimatedIdx,
|
|
|
|
float originalLeftSpaceWidth,
|
|
|
|
float newLineWidth)
|
|
|
|
{
|
|
|
|
bool startingNewLine = (newLineWidth == originalLeftSpaceWidth);
|
|
|
|
if (!isWrappable(text))
|
|
|
|
return (startingNewLine ? static_cast<int>(text.length()) : 0);
|
2017-11-08 13:59:57 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// The adjustment of the new line position
|
|
|
|
int idx = getNextWordPos(text, estimatedIdx);
|
|
|
|
std::string leftStr = text.getAsCharSequence(0, idx);
|
|
|
|
label->setString(leftStr);
|
|
|
|
float textRendererWidth = label->getContentSize().width;
|
|
|
|
if (originalLeftSpaceWidth < textRendererWidth) // Have protruding
|
|
|
|
{
|
|
|
|
while (1)
|
2016-01-22 13:44:45 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// try to erase a word
|
|
|
|
int newidx = getPrevWordPos(text, idx);
|
|
|
|
if (newidx >= 0)
|
2017-11-08 13:59:57 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
leftStr = text.getAsCharSequence(0, newidx);
|
|
|
|
label->setString(leftStr);
|
|
|
|
textRendererWidth = label->getContentSize().width;
|
|
|
|
if (textRendererWidth <= originalLeftSpaceWidth) // is fitted
|
|
|
|
return newidx;
|
|
|
|
idx = newidx;
|
|
|
|
continue;
|
2017-11-08 13:59:57 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
// newidx < 0 means no prev word
|
|
|
|
return (startingNewLine ? idx : 0);
|
2016-01-22 13:44:45 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
else if (textRendererWidth < originalLeftSpaceWidth) // A wide margin
|
|
|
|
{
|
|
|
|
while (1)
|
2016-01-22 13:44:45 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// try to append a word
|
|
|
|
int newidx = getNextWordPos(text, idx);
|
|
|
|
leftStr = text.getAsCharSequence(0, newidx);
|
|
|
|
label->setString(leftStr);
|
|
|
|
textRendererWidth = label->getContentSize().width;
|
|
|
|
if (textRendererWidth < originalLeftSpaceWidth)
|
2017-11-08 13:59:57 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// the whole string is tested
|
|
|
|
if (newidx == static_cast<int>(text.length()))
|
|
|
|
return newidx;
|
|
|
|
idx = newidx;
|
|
|
|
continue;
|
2017-11-08 13:59:57 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
// protruded ? undo add, or quite fit
|
|
|
|
return (textRendererWidth > originalLeftSpaceWidth ? idx : newidx);
|
2016-01-22 13:44:45 +08:00
|
|
|
}
|
2017-11-08 13:59:57 +08:00
|
|
|
}
|
2016-01-22 13:44:45 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
int findSplitPositionForChar(Label* label,
|
|
|
|
const StringUtils::StringUTF8& text,
|
|
|
|
int estimatedIdx,
|
|
|
|
float originalLeftSpaceWidth,
|
|
|
|
float newLineWidth)
|
|
|
|
{
|
|
|
|
bool startingNewLine = (newLineWidth == originalLeftSpaceWidth);
|
2016-01-22 13:44:45 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
int stringLength = static_cast<int>(text.length());
|
|
|
|
int leftLength = estimatedIdx;
|
2016-01-22 13:44:45 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// The adjustment of the new line position
|
|
|
|
std::string leftStr = text.getAsCharSequence(0, leftLength);
|
|
|
|
label->setString(leftStr);
|
|
|
|
float textRendererWidth = label->getContentSize().width;
|
|
|
|
if (originalLeftSpaceWidth < textRendererWidth) // Have protruding
|
|
|
|
{
|
|
|
|
while (leftLength-- > 0)
|
2017-11-08 13:59:57 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// try to erase a char
|
|
|
|
auto& ch = text.getString().at(leftLength);
|
|
|
|
leftStr.erase(leftStr.end() - ch._char.length(), leftStr.end());
|
|
|
|
label->setString(leftStr);
|
|
|
|
textRendererWidth = label->getContentSize().width;
|
|
|
|
if (textRendererWidth <= originalLeftSpaceWidth) // is fitted
|
|
|
|
break;
|
2016-01-22 13:44:45 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
else if (textRendererWidth < originalLeftSpaceWidth) // A wide margin
|
|
|
|
{
|
|
|
|
while (leftLength < stringLength)
|
2017-11-08 13:59:57 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// try to append a char
|
|
|
|
auto& ch = text.getString().at(leftLength);
|
|
|
|
++leftLength;
|
|
|
|
leftStr.append(ch._char);
|
|
|
|
label->setString(leftStr);
|
|
|
|
textRendererWidth = label->getContentSize().width;
|
|
|
|
if (originalLeftSpaceWidth < textRendererWidth) // protruded, undo add
|
2017-11-08 13:59:57 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
--leftLength;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (originalLeftSpaceWidth == textRendererWidth) // quite fit
|
|
|
|
{
|
|
|
|
break;
|
2016-01-22 13:44:45 +08:00
|
|
|
}
|
|
|
|
}
|
2017-11-08 13:59:57 +08:00
|
|
|
}
|
2016-01-22 13:44:45 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (leftLength <= 0)
|
|
|
|
return (startingNewLine) ? 1 : 0;
|
|
|
|
return leftLength;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
void RichText::handleTextRenderer(std::string_view text,
|
|
|
|
std::string_view fontName,
|
2021-12-25 10:04:45 +08:00
|
|
|
float fontSize,
|
|
|
|
const Color3B& color,
|
|
|
|
uint8_t opacity,
|
|
|
|
uint32_t flags,
|
2021-12-26 23:26:34 +08:00
|
|
|
std::string_view url,
|
2021-12-25 10:04:45 +08:00
|
|
|
const Color3B& outlineColor,
|
|
|
|
int outlineSize,
|
|
|
|
const Color3B& shadowColor,
|
|
|
|
const Vec2& shadowOffset,
|
|
|
|
int shadowBlurRadius,
|
2016-03-07 09:50:47 +08:00
|
|
|
const Color3B& glowColor)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
bool fileExist = FileUtils::getInstance()->isFileExist(fontName);
|
2020-08-18 11:33:18 +08:00
|
|
|
RichText::WrapMode wrapMode = static_cast<RichText::WrapMode>(_defaults.at(KEY_WRAP_MODE).asInt());
|
2017-11-08 13:59:57 +08:00
|
|
|
|
|
|
|
// split text by \n
|
2021-12-26 23:26:34 +08:00
|
|
|
std::stringstream ss;
|
|
|
|
ss << text;
|
2017-11-08 13:59:57 +08:00
|
|
|
std::string currentText;
|
|
|
|
size_t realLines = 0;
|
|
|
|
while (std::getline(ss, currentText, '\n'))
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2017-11-08 13:59:57 +08:00
|
|
|
if (realLines > 0)
|
|
|
|
{
|
|
|
|
addNewLine();
|
|
|
|
_lineHeights.back() = fontSize;
|
|
|
|
}
|
|
|
|
++realLines;
|
|
|
|
|
|
|
|
size_t splitParts = 0;
|
|
|
|
StringUtils::StringUTF8 utf8Text(currentText);
|
|
|
|
while (!currentText.empty())
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2017-11-08 13:59:57 +08:00
|
|
|
if (splitParts > 0)
|
2014-04-09 23:31:05 +08:00
|
|
|
{
|
2017-11-08 13:59:57 +08:00
|
|
|
addNewLine();
|
|
|
|
_lineHeights.back() = fontSize;
|
2014-07-17 17:58:40 +08:00
|
|
|
}
|
2017-11-08 13:59:57 +08:00
|
|
|
++splitParts;
|
|
|
|
|
|
|
|
Label* textRenderer = fileExist ? Label::createWithTTF(currentText, fontName, fontSize)
|
2021-12-25 10:04:45 +08:00
|
|
|
: Label::createWithSystemFont(currentText, fontName, fontSize);
|
2017-11-08 13:59:57 +08:00
|
|
|
|
|
|
|
if (flags & RichElementText::ITALICS_FLAG)
|
|
|
|
textRenderer->enableItalics();
|
|
|
|
if (flags & RichElementText::BOLD_FLAG)
|
|
|
|
textRenderer->enableBold();
|
|
|
|
if (flags & RichElementText::UNDERLINE_FLAG)
|
|
|
|
textRenderer->enableUnderline();
|
|
|
|
if (flags & RichElementText::STRIKETHROUGH_FLAG)
|
|
|
|
textRenderer->enableStrikethrough();
|
|
|
|
if (flags & RichElementText::URL_FLAG)
|
2021-12-25 10:04:45 +08:00
|
|
|
textRenderer->addComponent(ListenerComponent::create(
|
|
|
|
textRenderer, url, std::bind(&RichText::openUrl, this, std::placeholders::_1)));
|
2017-11-08 13:59:57 +08:00
|
|
|
if (flags & RichElementText::OUTLINE_FLAG)
|
|
|
|
textRenderer->enableOutline(Color4B(outlineColor), outlineSize);
|
|
|
|
if (flags & RichElementText::SHADOW_FLAG)
|
|
|
|
textRenderer->enableShadow(Color4B(shadowColor), shadowOffset, shadowBlurRadius);
|
|
|
|
if (flags & RichElementText::GLOW_FLAG)
|
|
|
|
textRenderer->enableGlow(Color4B(glowColor));
|
|
|
|
|
2018-08-17 11:52:16 +08:00
|
|
|
textRenderer->setTextColor(Color4B(color));
|
2017-11-08 13:59:57 +08:00
|
|
|
textRenderer->setOpacity(opacity);
|
|
|
|
|
2017-11-10 15:05:21 +08:00
|
|
|
// textRendererWidth will get 0.0f, when we've got glError: 0x0501 in Label::getContentSize
|
|
|
|
// It happens when currentText is very very long so that can't generate a texture
|
2017-11-08 13:59:57 +08:00
|
|
|
float textRendererWidth = textRenderer->getContentSize().width;
|
|
|
|
|
|
|
|
// no splitting
|
2017-11-10 15:05:21 +08:00
|
|
|
if (textRendererWidth > 0.0f && _leftSpaceWidth >= textRendererWidth)
|
2014-04-09 23:31:05 +08:00
|
|
|
{
|
2017-11-10 15:05:21 +08:00
|
|
|
_leftSpaceWidth -= textRendererWidth;
|
2017-11-08 13:59:57 +08:00
|
|
|
pushToContainer(textRenderer);
|
|
|
|
break;
|
2014-04-09 23:31:05 +08:00
|
|
|
}
|
2017-11-08 13:59:57 +08:00
|
|
|
|
2017-11-10 15:05:21 +08:00
|
|
|
// rough estimate
|
|
|
|
// when textRendererWidth == 0.0f, use fontSize as the rough estimate of width for each char,
|
|
|
|
// (_leftSpaceWidth / fontSize) means how many chars can be aligned in leftSpaceWidth.
|
|
|
|
int estimatedIdx = 0;
|
|
|
|
if (textRendererWidth > 0.0f)
|
|
|
|
estimatedIdx = static_cast<int>(_leftSpaceWidth / textRendererWidth * utf8Text.length());
|
|
|
|
else
|
|
|
|
estimatedIdx = static_cast<int>(_leftSpaceWidth / fontSize);
|
|
|
|
|
2017-11-08 13:59:57 +08:00
|
|
|
int leftLength = 0;
|
|
|
|
if (wrapMode == WRAP_PER_WORD)
|
2021-12-25 10:04:45 +08:00
|
|
|
leftLength =
|
|
|
|
findSplitPositionForWord(textRenderer, utf8Text, estimatedIdx, _leftSpaceWidth, _customSize.width);
|
2017-11-08 13:59:57 +08:00
|
|
|
else
|
2021-12-25 10:04:45 +08:00
|
|
|
leftLength =
|
|
|
|
findSplitPositionForChar(textRenderer, utf8Text, estimatedIdx, _leftSpaceWidth, _customSize.width);
|
2017-11-08 13:59:57 +08:00
|
|
|
|
|
|
|
// split string
|
|
|
|
if (leftLength > 0)
|
2014-04-09 23:31:05 +08:00
|
|
|
{
|
2017-11-08 13:59:57 +08:00
|
|
|
textRenderer->setString(utf8Text.getAsCharSequence(0, leftLength));
|
|
|
|
pushToContainer(textRenderer);
|
2014-04-09 23:31:05 +08:00
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
|
2017-11-08 13:59:57 +08:00
|
|
|
StringUtils::StringUTF8::CharUTF8Store& str = utf8Text.getString();
|
|
|
|
|
|
|
|
// erase the chars which are processed
|
2018-06-08 17:06:29 +08:00
|
|
|
str.erase(str.begin(), str.begin() + leftLength);
|
2017-11-08 13:59:57 +08:00
|
|
|
currentText = utf8Text.getAsCharSequence();
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
}
|
2017-11-08 13:59:57 +08:00
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
void RichText::handleImageRenderer(std::string_view filePath,
|
2021-12-25 10:04:45 +08:00
|
|
|
Widget::TextureResType textureType,
|
|
|
|
const Color3B& /*color*/,
|
|
|
|
uint8_t /*opacity*/,
|
|
|
|
int width,
|
|
|
|
int height,
|
2021-12-26 23:26:34 +08:00
|
|
|
std::string_view url)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2020-08-06 17:37:34 +08:00
|
|
|
Sprite* imageRenderer;
|
|
|
|
if (textureType == Widget::TextureResType::LOCAL)
|
|
|
|
imageRenderer = Sprite::create(filePath);
|
|
|
|
else
|
|
|
|
imageRenderer = Sprite::createWithSpriteFrameName(filePath);
|
|
|
|
|
2016-01-20 12:41:31 +08:00
|
|
|
if (imageRenderer)
|
|
|
|
{
|
|
|
|
auto currentSize = imageRenderer->getContentSize();
|
|
|
|
if (width != -1)
|
|
|
|
imageRenderer->setScaleX(width / currentSize.width);
|
|
|
|
if (height != -1)
|
|
|
|
imageRenderer->setScaleY(height / currentSize.height);
|
2021-12-25 10:04:45 +08:00
|
|
|
imageRenderer->setContentSize(
|
|
|
|
Vec2(currentSize.width * imageRenderer->getScaleX(), currentSize.height * imageRenderer->getScaleY()));
|
2017-07-05 09:25:12 +08:00
|
|
|
imageRenderer->setScale(1.f, 1.f);
|
2016-01-21 09:59:13 +08:00
|
|
|
handleCustomRenderer(imageRenderer);
|
2021-12-25 10:04:45 +08:00
|
|
|
imageRenderer->addComponent(
|
|
|
|
ListenerComponent::create(imageRenderer, url, std::bind(&RichText::openUrl, this, std::placeholders::_1)));
|
2016-01-21 09:59:13 +08:00
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2016-01-20 12:41:31 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
void RichText::handleCustomRenderer(axis::Node* renderer)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2021-10-23 23:27:14 +08:00
|
|
|
Vec2 imgSize = renderer->getContentSize();
|
2014-03-11 17:13:54 +08:00
|
|
|
_leftSpaceWidth -= imgSize.width;
|
|
|
|
if (_leftSpaceWidth < 0.0f)
|
|
|
|
{
|
|
|
|
addNewLine();
|
|
|
|
pushToContainer(renderer);
|
|
|
|
_leftSpaceWidth -= imgSize.width;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pushToContainer(renderer);
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
void RichText::addNewLine()
|
|
|
|
{
|
|
|
|
_leftSpaceWidth = _customSize.width;
|
2017-07-04 14:31:29 +08:00
|
|
|
_elementRenders.emplace_back();
|
2017-11-08 13:59:57 +08:00
|
|
|
_lineHeights.emplace_back();
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2017-11-06 10:28:11 +08:00
|
|
|
void RichText::formatRenderers()
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2020-08-18 11:33:18 +08:00
|
|
|
float verticalSpace = _defaults[KEY_VERTICAL_SPACE].asFloat();
|
2021-12-25 10:04:45 +08:00
|
|
|
float fontSize = _defaults[KEY_FONT_SIZE].asFloat();
|
2017-11-08 13:59:57 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
if (_ignoreSize)
|
|
|
|
{
|
|
|
|
float newContentSizeWidth = 0.0f;
|
2021-12-25 10:04:45 +08:00
|
|
|
float nextPosY = 0.0f;
|
|
|
|
std::vector<std::pair<Vector<Node*>*, float>> rowWidthPairs;
|
2017-07-05 09:25:12 +08:00
|
|
|
rowWidthPairs.reserve(_elementRenders.size());
|
2021-12-25 10:04:45 +08:00
|
|
|
for (auto& element : _elementRenders)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2016-01-20 12:41:31 +08:00
|
|
|
float nextPosX = 0.0f;
|
2021-12-25 10:04:45 +08:00
|
|
|
float maxY = 0.0f;
|
2017-07-04 14:31:29 +08:00
|
|
|
for (auto& iter : element)
|
2016-01-20 12:41:31 +08:00
|
|
|
{
|
2017-02-06 15:14:18 +08:00
|
|
|
iter->setAnchorPoint(Vec2::ZERO);
|
|
|
|
iter->setPosition(nextPosX, nextPosY);
|
|
|
|
this->addProtectedChild(iter, 1);
|
2021-10-23 23:27:14 +08:00
|
|
|
Vec2 iSize = iter->getContentSize();
|
2016-01-20 12:41:31 +08:00
|
|
|
newContentSizeWidth += iSize.width;
|
|
|
|
nextPosX += iSize.width;
|
2017-11-08 13:59:57 +08:00
|
|
|
maxY = std::max(maxY, iSize.height);
|
2016-01-20 12:41:31 +08:00
|
|
|
}
|
|
|
|
nextPosY -= maxY;
|
2017-07-05 09:25:12 +08:00
|
|
|
rowWidthPairs.emplace_back(&element, nextPosX);
|
2016-01-20 12:41:31 +08:00
|
|
|
}
|
2021-10-23 23:27:14 +08:00
|
|
|
this->setContentSize(Vec2(newContentSizeWidth, -nextPosY));
|
2021-12-25 10:04:45 +08:00
|
|
|
for (auto& row : rowWidthPairs)
|
2017-07-05 09:25:12 +08:00
|
|
|
doHorizontalAlignment(*row.first, row.second);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-11-08 13:59:57 +08:00
|
|
|
// calculate real height
|
2014-03-11 17:13:54 +08:00
|
|
|
float newContentSizeHeight = 0.0f;
|
2017-07-05 09:25:12 +08:00
|
|
|
std::vector<float> maxHeights(_elementRenders.size());
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
for (size_t i = 0, size = _elementRenders.size(); i < size; i++)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2017-07-04 14:31:29 +08:00
|
|
|
Vector<Node*>& row = _elementRenders[i];
|
2021-12-25 10:04:45 +08:00
|
|
|
float maxHeight = 0.0f;
|
2017-07-04 14:31:29 +08:00
|
|
|
for (auto& iter : row)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2017-11-08 13:59:57 +08:00
|
|
|
maxHeight = std::max(iter->getContentSize().height, maxHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
// gap for empty line, if _lineHeights[i] == 0, use current RichText's fontSize
|
|
|
|
if (row.empty())
|
|
|
|
{
|
|
|
|
maxHeight = (_lineHeights[i] != 0.0f ? _lineHeights[i] : fontSize);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
maxHeights[i] = maxHeight;
|
2017-11-08 13:59:57 +08:00
|
|
|
|
|
|
|
// vertical space except for first line
|
|
|
|
newContentSizeHeight += (i != 0 ? maxHeight + verticalSpace : maxHeight);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2017-11-08 13:59:57 +08:00
|
|
|
_customSize.height = newContentSizeHeight;
|
|
|
|
|
|
|
|
// align renders
|
2014-03-11 17:13:54 +08:00
|
|
|
float nextPosY = _customSize.height;
|
2021-12-25 10:04:45 +08:00
|
|
|
for (size_t i = 0, size = _elementRenders.size(); i < size; i++)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2017-07-04 14:31:29 +08:00
|
|
|
Vector<Node*>& row = _elementRenders[i];
|
2021-12-25 10:04:45 +08:00
|
|
|
float nextPosX = 0.0f;
|
2017-11-08 13:59:57 +08:00
|
|
|
nextPosY -= (i != 0 ? maxHeights[i] + verticalSpace : maxHeights[i]);
|
2017-07-04 14:31:29 +08:00
|
|
|
for (auto& iter : row)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2017-02-06 15:14:18 +08:00
|
|
|
iter->setAnchorPoint(Vec2::ZERO);
|
|
|
|
iter->setPosition(nextPosX, nextPosY);
|
|
|
|
this->addProtectedChild(iter, 1);
|
|
|
|
nextPosX += iter->getContentSize().width;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2017-07-05 09:25:12 +08:00
|
|
|
doHorizontalAlignment(row, nextPosX);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
_elementRenders.clear();
|
2017-11-08 13:59:57 +08:00
|
|
|
_lineHeights.clear();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
if (_ignoreSize)
|
|
|
|
{
|
2021-10-23 23:27:14 +08:00
|
|
|
Vec2 s = getVirtualRendererSize();
|
2014-06-24 15:08:58 +08:00
|
|
|
this->setContentSize(s);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-06-24 15:08:58 +08:00
|
|
|
this->setContentSize(_customSize);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2014-06-20 10:40:16 +08:00
|
|
|
updateContentSizeWithTextureSize(_contentSize);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2017-07-05 09:25:12 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
float getPaddingAmount(const RichText::HorizontalAlignment alignment, const float leftOver)
|
|
|
|
{
|
|
|
|
switch (alignment)
|
|
|
|
{
|
|
|
|
case RichText::HorizontalAlignment::CENTER:
|
|
|
|
return leftOver / 2.f;
|
|
|
|
case RichText::HorizontalAlignment::RIGHT:
|
|
|
|
return leftOver;
|
|
|
|
default:
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(false, "invalid horizontal alignment!");
|
2021-12-25 10:04:45 +08:00
|
|
|
return 0.f;
|
2017-07-05 09:25:12 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
} // namespace
|
2017-07-05 09:25:12 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
void RichText::doHorizontalAlignment(const Vector<axis::Node*>& row, float rowWidth)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2020-08-18 11:33:18 +08:00
|
|
|
const auto alignment = static_cast<HorizontalAlignment>(_defaults.at(KEY_HORIZONTAL_ALIGNMENT).asInt());
|
2021-12-25 10:04:45 +08:00
|
|
|
if (alignment != HorizontalAlignment::LEFT)
|
|
|
|
{
|
|
|
|
const auto diff = stripTrailingWhitespace(row);
|
|
|
|
const auto leftOver = getContentSize().width - (rowWidth + diff);
|
2017-07-05 09:25:12 +08:00
|
|
|
const float leftPadding = getPaddingAmount(alignment, leftOver);
|
|
|
|
const Vec2 offset(leftPadding, 0.f);
|
2021-12-25 10:04:45 +08:00
|
|
|
for (auto& node : row)
|
|
|
|
{
|
2017-07-05 09:25:12 +08:00
|
|
|
node->setPosition(node->getPosition() + offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
bool isWhitespace(char c)
|
|
|
|
{
|
|
|
|
return std::isspace(c, std::locale());
|
2017-07-05 09:25:12 +08:00
|
|
|
}
|
2021-12-26 23:26:34 +08:00
|
|
|
void rtrim(std::string& s)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
|
|
|
s.erase(std::find_if_not(s.rbegin(), s.rend(), isWhitespace).base(), s.end());
|
|
|
|
}
|
|
|
|
} // namespace
|
2017-07-05 09:25:12 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
float RichText::stripTrailingWhitespace(const Vector<axis::Node*>& row)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
|
|
|
if (!row.empty())
|
|
|
|
{
|
|
|
|
if (auto label = dynamic_cast<Label*>(row.back()))
|
|
|
|
{
|
2021-12-28 11:00:34 +08:00
|
|
|
const auto width = label->getContentSize().width;
|
2021-12-26 23:26:34 +08:00
|
|
|
std::string trimmedString{label->getString()};
|
|
|
|
rtrim(trimmedString);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (label->getString() != trimmedString)
|
|
|
|
{
|
2017-07-05 09:25:12 +08:00
|
|
|
label->setString(trimmedString);
|
|
|
|
return label->getContentSize().width - width;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0.0f;
|
|
|
|
}
|
|
|
|
|
2014-07-24 11:51:47 +08:00
|
|
|
void RichText::adaptRenderers()
|
|
|
|
{
|
|
|
|
this->formatText();
|
|
|
|
}
|
2017-07-05 09:25:12 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
void RichText::pushToContainer(axis::Node* renderer)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2020-08-06 17:37:34 +08:00
|
|
|
if (_elementRenders.empty())
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
_elementRenders[_elementRenders.size() - 1].pushBack(renderer);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
void RichText::setVerticalSpace(float space)
|
|
|
|
{
|
2016-03-07 09:50:47 +08:00
|
|
|
_defaults[KEY_VERTICAL_SPACE] = space;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2015-07-23 14:59:24 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
void RichText::ignoreContentAdaptWithSize(bool ignore)
|
|
|
|
{
|
|
|
|
if (_ignoreSize != ignore)
|
|
|
|
{
|
|
|
|
_formatTextDirty = true;
|
|
|
|
Widget::ignoreContentAdaptWithSize(ignore);
|
|
|
|
}
|
|
|
|
}
|
2017-11-10 15:05:21 +08:00
|
|
|
|
2014-03-24 15:25:44 +08:00
|
|
|
std::string RichText::getDescription() const
|
|
|
|
{
|
|
|
|
return "RichText";
|
|
|
|
}
|