Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add img support for Richtext #63

Merged
merged 1 commit into from
Jul 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const Node = require('./Node');
const Label = require('./Label');
const get_font_path_by_uuid = require('./Utils').get_font_path_by_uuid;
const Utils = require('./Utils');

class RichText extends Node {
constructor(data) {
Expand All @@ -19,7 +19,6 @@ class RichText extends Node {
// <outline xxx=yyy ...> -> <outline xxx='yyy' ...>
var text = component._N$string;
let regex = /(<outline color|width)=(\w*) (color|width)=(\w*)/;
var match = text.match(regex);
text = text.replace(regex, "$1='$2' $3='$4'");

// <br/> -> \n
Expand All @@ -29,6 +28,25 @@ class RichText extends Node {
if (text[0] !== '<')
text = '<font>' + text + '</font>';

// add sprite frames if there is img component
if (component._N$imageAtlas) {
// find sprite frame name
regex = /<img src=\'(\w+)\'/;
let resource = text.match(regex)[1];

// add sprite frames
let json_uuid = component._N$imageAtlas.__uuid__;
let json_content = Utils.get_sprite_frame_json_by_uuid(json_uuid);
let resource_uuid = json_content._spriteFrames[resource].__uuid__;
Utils.get_sprite_frame_name_by_uuid(resource_uuid);

// <img src='xx'/> -> <img scr='xxx' width='yyy' height='zzz'/>
let resource_json_content = Utils.get_sprite_frame_json_by_uuid(resource_uuid);
let resource_size = {width: resource_json_content.content.originalSize[0],
height: resource_json_content.content.originalSize[1]};
text = text.replace(/(<img\s+src=\'\w+\')/, "$1 width='" + resource_size.width + "' height='" + resource_size.height + "'");
}

this._properties.text = text;

this._properties.horizontalAlignment = Label.H_ALIGNMENTS[component._N$horizontalAlign];
Expand All @@ -37,7 +55,7 @@ class RichText extends Node {
this._properties.lineHeight = component._N$lineHeight;
let f = component._N$font;
if (f)
this._properties.fontFilename = get_font_path_by_uuid(f.__uuid__);
this._properties.fontFilename = Utils.get_font_path_by_uuid(f.__uuid__);
}
}
RichText.H_ALIGNMENTS = ['Left', 'Center', 'Right'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

const std::string RichtextStringVisitor::COLOR_FLAG = "color=";
const std::string RichtextStringVisitor::SIZE_FLAG = "size=";
const std::string RichtextStringVisitor::IMG_FLAG = "img";

const std::map<std::string, std::string> RichtextStringVisitor::COLOR_MAP = {
{"white", "#ffffff"},
Expand Down Expand Up @@ -64,10 +65,15 @@ void RichtextStringVisitor::startElement(void *ctx, const char *name, const char
_outputXML.append("=");

_outputXML.append("\"");
std::string attributeValue = convertAttributeValue(attributeName, *atts++);
std::string attributeValue = convertAttributeValue(name, attributeName, *atts++);
_outputXML.append(attributeValue);
_outputXML.append("\"");
}
// the resource type is a sprite frame name
// creator only supports sprite frame for img tag
if (name == RichtextStringVisitor::IMG_FLAG)
_outputXML.append(" type='1' ");

_outputXML.append(">");

_addFontEndFlags.push(false);
Expand Down Expand Up @@ -126,10 +132,13 @@ std::string RichtextStringVisitor::convertAttributeName(const std::string& tagNa
return attributeName;
}

std::string RichtextStringVisitor::convertAttributeValue(const std::string& attributeName, const std::string& attributeValue) const
std::string RichtextStringVisitor::convertAttributeValue(const std::string& tagName, const std::string& attributeName, const std::string& attributeValue) const
{
if (attributeName == "color")
return convertColorString2Hex(attributeValue);
else
return attributeValue;

if (tagName == RichtextStringVisitor::IMG_FLAG && attributeName == "src")
return attributeValue + ".png";

return attributeValue;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ class RichtextStringVisitor : public cocos2d::SAXDelegator

std::string convertColorString2Hex(const std::string& colorString) const;
std::string convertAttributeName(const std::string& tagName, const std::string& attributeName) const;
std::string convertAttributeValue(const std::string& attributeName, const std::string& attributeValue) const;
std::string convertAttributeValue(const std::string& tagName, const std::string& attributeName, const std::string& attributeValue) const;

const static std::string COLOR_FLAG;
const static std::string SIZE_FLAG;
const static std::string IMG_FLAG;
const static std::map<std::string, std::string> COLOR_MAP;

std::string _outputXML;
Expand Down