* For example: 'some text {a|some text}other text{b|some text}xxx{c|}xxx' * Also consider 'bbbb{a|xxx\nzzz}xxxx\naaaa'. * * @public * @param {string} text * @param {Object} style * @return {Object} block * { * width, * height, * lines: [{ * lineHeight, *
(text, style)
| 8061 | * If styleName is undefined, it is plain text. |
| 8062 | */ |
| 8063 | function parseRichText(text, style) { |
| 8064 | var contentBlock = {lines: [], width: 0, height: 0}; |
| 8065 | |
| 8066 | text != null && (text += ''); |
| 8067 | if (!text) { |
| 8068 | return contentBlock; |
| 8069 | } |
| 8070 | |
| 8071 | var lastIndex = STYLE_REG.lastIndex = 0; |
| 8072 | var result; |
| 8073 | while ((result = STYLE_REG.exec(text)) != null) { |
| 8074 | var matchedIndex = result.index; |
| 8075 | if (matchedIndex > lastIndex) { |
| 8076 | pushTokens(contentBlock, text.substring(lastIndex, matchedIndex)); |
| 8077 | } |
| 8078 | pushTokens(contentBlock, result[2], result[1]); |
| 8079 | lastIndex = STYLE_REG.lastIndex; |
| 8080 | } |
| 8081 | |
| 8082 | if (lastIndex < text.length) { |
| 8083 | pushTokens(contentBlock, text.substring(lastIndex, text.length)); |
| 8084 | } |
| 8085 | |
| 8086 | var lines = contentBlock.lines; |
| 8087 | var contentHeight = 0; |
| 8088 | var contentWidth = 0; |
| 8089 | // For `textWidth: 100%` |
| 8090 | var pendingList = []; |
| 8091 | |
| 8092 | var stlPadding = style.textPadding; |
| 8093 | |
| 8094 | var truncate = style.truncate; |
| 8095 | var truncateWidth = truncate && truncate.outerWidth; |
| 8096 | var truncateHeight = truncate && truncate.outerHeight; |
| 8097 | if (stlPadding) { |
| 8098 | truncateWidth != null && (truncateWidth -= stlPadding[1] + stlPadding[3]); |
| 8099 | truncateHeight != null && (truncateHeight -= stlPadding[0] + stlPadding[2]); |
| 8100 | } |
| 8101 | |
| 8102 | // Calculate layout info of tokens. |
| 8103 | for (var i = 0; i < lines.length; i++) { |
| 8104 | var line = lines[i]; |
| 8105 | var lineHeight = 0; |
| 8106 | var lineWidth = 0; |
| 8107 | |
| 8108 | for (var j = 0; j < line.tokens.length; j++) { |
| 8109 | var token = line.tokens[j]; |
| 8110 | var tokenStyle = token.styleName && style.rich[token.styleName] || {}; |
| 8111 | // textPadding should not inherit from style. |
| 8112 | var textPadding = token.textPadding = tokenStyle.textPadding; |
| 8113 | |
| 8114 | // textFont has been asigned to font by `normalizeStyle`. |
| 8115 | var font = token.font = tokenStyle.font || style.font; |
| 8116 | |
| 8117 | // textHeight can be used when textVerticalAlign is specified in token. |
| 8118 | var tokenHeight = token.textHeight = retrieve2( |
| 8119 | // textHeight should not be inherited, consider it can be specified |
| 8120 | // as box height of the block. |
no test coverage detected