parseText.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. import * as imageHelper from '../helper/image';
  2. import { extend, retrieve2, retrieve3, reduce } from '../../core/util';
  3. import { getLineHeight, getWidth, parsePercent } from '../../contain/text';
  4. var STYLE_REG = /\{([a-zA-Z0-9_]+)\|([^}]*)\}/g;
  5. export function truncateText(text, containerWidth, font, ellipsis, options) {
  6. if (!containerWidth) {
  7. return '';
  8. }
  9. var textLines = (text + '').split('\n');
  10. options = prepareTruncateOptions(containerWidth, font, ellipsis, options);
  11. for (var i = 0, len = textLines.length; i < len; i++) {
  12. textLines[i] = truncateSingleLine(textLines[i], options);
  13. }
  14. return textLines.join('\n');
  15. }
  16. function prepareTruncateOptions(containerWidth, font, ellipsis, options) {
  17. options = options || {};
  18. var preparedOpts = extend({}, options);
  19. preparedOpts.font = font;
  20. ellipsis = retrieve2(ellipsis, '...');
  21. preparedOpts.maxIterations = retrieve2(options.maxIterations, 2);
  22. var minChar = preparedOpts.minChar = retrieve2(options.minChar, 0);
  23. preparedOpts.cnCharWidth = getWidth('国', font);
  24. var ascCharWidth = preparedOpts.ascCharWidth = getWidth('a', font);
  25. preparedOpts.placeholder = retrieve2(options.placeholder, '');
  26. var contentWidth = containerWidth = Math.max(0, containerWidth - 1);
  27. for (var i = 0; i < minChar && contentWidth >= ascCharWidth; i++) {
  28. contentWidth -= ascCharWidth;
  29. }
  30. var ellipsisWidth = getWidth(ellipsis, font);
  31. if (ellipsisWidth > contentWidth) {
  32. ellipsis = '';
  33. ellipsisWidth = 0;
  34. }
  35. contentWidth = containerWidth - ellipsisWidth;
  36. preparedOpts.ellipsis = ellipsis;
  37. preparedOpts.ellipsisWidth = ellipsisWidth;
  38. preparedOpts.contentWidth = contentWidth;
  39. preparedOpts.containerWidth = containerWidth;
  40. return preparedOpts;
  41. }
  42. function truncateSingleLine(textLine, options) {
  43. var containerWidth = options.containerWidth;
  44. var font = options.font;
  45. var contentWidth = options.contentWidth;
  46. if (!containerWidth) {
  47. return '';
  48. }
  49. var lineWidth = getWidth(textLine, font);
  50. if (lineWidth <= containerWidth) {
  51. return textLine;
  52. }
  53. for (var j = 0;; j++) {
  54. if (lineWidth <= contentWidth || j >= options.maxIterations) {
  55. textLine += options.ellipsis;
  56. break;
  57. }
  58. var subLength = j === 0
  59. ? estimateLength(textLine, contentWidth, options.ascCharWidth, options.cnCharWidth)
  60. : lineWidth > 0
  61. ? Math.floor(textLine.length * contentWidth / lineWidth)
  62. : 0;
  63. textLine = textLine.substr(0, subLength);
  64. lineWidth = getWidth(textLine, font);
  65. }
  66. if (textLine === '') {
  67. textLine = options.placeholder;
  68. }
  69. return textLine;
  70. }
  71. function estimateLength(text, contentWidth, ascCharWidth, cnCharWidth) {
  72. var width = 0;
  73. var i = 0;
  74. for (var len = text.length; i < len && width < contentWidth; i++) {
  75. var charCode = text.charCodeAt(i);
  76. width += (0 <= charCode && charCode <= 127) ? ascCharWidth : cnCharWidth;
  77. }
  78. return i;
  79. }
  80. export function parsePlainText(text, style) {
  81. text != null && (text += '');
  82. var overflow = style.overflow;
  83. var padding = style.padding;
  84. var font = style.font;
  85. var truncate = overflow === 'truncate';
  86. var calculatedLineHeight = getLineHeight(font);
  87. var lineHeight = retrieve2(style.lineHeight, calculatedLineHeight);
  88. var truncateLineOverflow = style.lineOverflow === 'truncate';
  89. var width = style.width;
  90. var lines;
  91. if (width != null && overflow === 'break' || overflow === 'breakAll') {
  92. lines = text ? wrapText(text, style.font, width, overflow === 'breakAll', 0).lines : [];
  93. }
  94. else {
  95. lines = text ? text.split('\n') : [];
  96. }
  97. var contentHeight = lines.length * lineHeight;
  98. var height = retrieve2(style.height, contentHeight);
  99. if (contentHeight > height && truncateLineOverflow) {
  100. var lineCount = Math.floor(height / lineHeight);
  101. lines = lines.slice(0, lineCount);
  102. }
  103. var outerHeight = height;
  104. var outerWidth = width;
  105. if (padding) {
  106. outerHeight += padding[0] + padding[2];
  107. if (outerWidth != null) {
  108. outerWidth += padding[1] + padding[3];
  109. }
  110. }
  111. if (text && truncate && outerWidth != null) {
  112. var options = prepareTruncateOptions(width, font, style.ellipsis, {
  113. minChar: style.truncateMinChar,
  114. placeholder: style.placeholder
  115. });
  116. for (var i = 0; i < lines.length; i++) {
  117. lines[i] = truncateSingleLine(lines[i], options);
  118. }
  119. }
  120. if (width == null) {
  121. var maxWidth = 0;
  122. for (var i = 0; i < lines.length; i++) {
  123. maxWidth = Math.max(getWidth(lines[i], font), maxWidth);
  124. }
  125. width = maxWidth;
  126. }
  127. return {
  128. lines: lines,
  129. height: height,
  130. outerHeight: outerHeight,
  131. lineHeight: lineHeight,
  132. calculatedLineHeight: calculatedLineHeight,
  133. contentHeight: contentHeight,
  134. width: width
  135. };
  136. }
  137. var RichTextToken = (function () {
  138. function RichTextToken() {
  139. }
  140. return RichTextToken;
  141. }());
  142. var RichTextLine = (function () {
  143. function RichTextLine(tokens) {
  144. this.tokens = [];
  145. if (tokens) {
  146. this.tokens = tokens;
  147. }
  148. }
  149. return RichTextLine;
  150. }());
  151. var RichTextContentBlock = (function () {
  152. function RichTextContentBlock() {
  153. this.width = 0;
  154. this.height = 0;
  155. this.contentWidth = 0;
  156. this.contentHeight = 0;
  157. this.outerWidth = 0;
  158. this.outerHeight = 0;
  159. this.lines = [];
  160. }
  161. return RichTextContentBlock;
  162. }());
  163. export { RichTextContentBlock };
  164. export function parseRichText(text, style) {
  165. var contentBlock = new RichTextContentBlock();
  166. text != null && (text += '');
  167. if (!text) {
  168. return contentBlock;
  169. }
  170. var topWidth = style.width;
  171. var topHeight = style.height;
  172. var overflow = style.overflow;
  173. var wrapInfo = (overflow === 'break' || overflow === 'breakAll') && topWidth != null
  174. ? { width: topWidth, accumWidth: 0, breakAll: overflow === 'breakAll' }
  175. : null;
  176. var lastIndex = STYLE_REG.lastIndex = 0;
  177. var result;
  178. while ((result = STYLE_REG.exec(text)) != null) {
  179. var matchedIndex = result.index;
  180. if (matchedIndex > lastIndex) {
  181. pushTokens(contentBlock, text.substring(lastIndex, matchedIndex), style, wrapInfo);
  182. }
  183. pushTokens(contentBlock, result[2], style, wrapInfo, result[1]);
  184. lastIndex = STYLE_REG.lastIndex;
  185. }
  186. if (lastIndex < text.length) {
  187. pushTokens(contentBlock, text.substring(lastIndex, text.length), style, wrapInfo);
  188. }
  189. var pendingList = [];
  190. var calculatedHeight = 0;
  191. var calculatedWidth = 0;
  192. var stlPadding = style.padding;
  193. var truncate = overflow === 'truncate';
  194. var truncateLine = style.lineOverflow === 'truncate';
  195. function finishLine(line, lineWidth, lineHeight) {
  196. line.width = lineWidth;
  197. line.lineHeight = lineHeight;
  198. calculatedHeight += lineHeight;
  199. calculatedWidth = Math.max(calculatedWidth, lineWidth);
  200. }
  201. outer: for (var i = 0; i < contentBlock.lines.length; i++) {
  202. var line = contentBlock.lines[i];
  203. var lineHeight = 0;
  204. var lineWidth = 0;
  205. for (var j = 0; j < line.tokens.length; j++) {
  206. var token = line.tokens[j];
  207. var tokenStyle = token.styleName && style.rich[token.styleName] || {};
  208. var textPadding = token.textPadding = tokenStyle.padding;
  209. var paddingH = textPadding ? textPadding[1] + textPadding[3] : 0;
  210. var font = token.font = tokenStyle.font || style.font;
  211. token.contentHeight = getLineHeight(font);
  212. var tokenHeight = retrieve2(tokenStyle.height, token.contentHeight);
  213. token.innerHeight = tokenHeight;
  214. textPadding && (tokenHeight += textPadding[0] + textPadding[2]);
  215. token.height = tokenHeight;
  216. token.lineHeight = retrieve3(tokenStyle.lineHeight, style.lineHeight, tokenHeight);
  217. token.align = tokenStyle && tokenStyle.align || style.align;
  218. token.verticalAlign = tokenStyle && tokenStyle.verticalAlign || 'middle';
  219. if (truncateLine && topHeight != null && calculatedHeight + token.lineHeight > topHeight) {
  220. if (j > 0) {
  221. line.tokens = line.tokens.slice(0, j);
  222. finishLine(line, lineWidth, lineHeight);
  223. contentBlock.lines = contentBlock.lines.slice(0, i + 1);
  224. }
  225. else {
  226. contentBlock.lines = contentBlock.lines.slice(0, i);
  227. }
  228. break outer;
  229. }
  230. var styleTokenWidth = tokenStyle.width;
  231. var tokenWidthNotSpecified = styleTokenWidth == null || styleTokenWidth === 'auto';
  232. if (typeof styleTokenWidth === 'string' && styleTokenWidth.charAt(styleTokenWidth.length - 1) === '%') {
  233. token.percentWidth = styleTokenWidth;
  234. pendingList.push(token);
  235. token.contentWidth = getWidth(token.text, font);
  236. }
  237. else {
  238. if (tokenWidthNotSpecified) {
  239. var textBackgroundColor = tokenStyle.backgroundColor;
  240. var bgImg = textBackgroundColor && textBackgroundColor.image;
  241. if (bgImg) {
  242. bgImg = imageHelper.findExistImage(bgImg);
  243. if (imageHelper.isImageReady(bgImg)) {
  244. token.width = Math.max(token.width, bgImg.width * tokenHeight / bgImg.height);
  245. }
  246. }
  247. }
  248. var remainTruncWidth = truncate && topWidth != null
  249. ? topWidth - lineWidth : null;
  250. if (remainTruncWidth != null && remainTruncWidth < token.width) {
  251. if (!tokenWidthNotSpecified || remainTruncWidth < paddingH) {
  252. token.text = '';
  253. token.width = token.contentWidth = 0;
  254. }
  255. else {
  256. token.text = truncateText(token.text, remainTruncWidth - paddingH, font, style.ellipsis, { minChar: style.truncateMinChar });
  257. token.width = token.contentWidth = getWidth(token.text, font);
  258. }
  259. }
  260. else {
  261. token.contentWidth = getWidth(token.text, font);
  262. }
  263. }
  264. token.width += paddingH;
  265. lineWidth += token.width;
  266. tokenStyle && (lineHeight = Math.max(lineHeight, token.lineHeight));
  267. }
  268. finishLine(line, lineWidth, lineHeight);
  269. }
  270. contentBlock.outerWidth = contentBlock.width = retrieve2(topWidth, calculatedWidth);
  271. contentBlock.outerHeight = contentBlock.height = retrieve2(topHeight, calculatedHeight);
  272. contentBlock.contentHeight = calculatedHeight;
  273. contentBlock.contentWidth = calculatedWidth;
  274. if (stlPadding) {
  275. contentBlock.outerWidth += stlPadding[1] + stlPadding[3];
  276. contentBlock.outerHeight += stlPadding[0] + stlPadding[2];
  277. }
  278. for (var i = 0; i < pendingList.length; i++) {
  279. var token = pendingList[i];
  280. var percentWidth = token.percentWidth;
  281. token.width = parseInt(percentWidth, 10) / 100 * contentBlock.width;
  282. }
  283. return contentBlock;
  284. }
  285. function pushTokens(block, str, style, wrapInfo, styleName) {
  286. var isEmptyStr = str === '';
  287. var tokenStyle = styleName && style.rich[styleName] || {};
  288. var lines = block.lines;
  289. var font = tokenStyle.font || style.font;
  290. var newLine = false;
  291. var strLines;
  292. var linesWidths;
  293. if (wrapInfo) {
  294. var tokenPadding = tokenStyle.padding;
  295. var tokenPaddingH = tokenPadding ? tokenPadding[1] + tokenPadding[3] : 0;
  296. if (tokenStyle.width != null && tokenStyle.width !== 'auto') {
  297. var outerWidth_1 = parsePercent(tokenStyle.width, wrapInfo.width) + tokenPaddingH;
  298. if (lines.length > 0) {
  299. if (outerWidth_1 + wrapInfo.accumWidth > wrapInfo.width) {
  300. strLines = str.split('\n');
  301. newLine = true;
  302. }
  303. }
  304. wrapInfo.accumWidth = outerWidth_1;
  305. }
  306. else {
  307. var res = wrapText(str, font, wrapInfo.width, wrapInfo.breakAll, wrapInfo.accumWidth);
  308. wrapInfo.accumWidth = res.accumWidth + tokenPaddingH;
  309. linesWidths = res.linesWidths;
  310. strLines = res.lines;
  311. }
  312. }
  313. else {
  314. strLines = str.split('\n');
  315. }
  316. for (var i = 0; i < strLines.length; i++) {
  317. var text = strLines[i];
  318. var token = new RichTextToken();
  319. token.styleName = styleName;
  320. token.text = text;
  321. token.isLineHolder = !text && !isEmptyStr;
  322. if (typeof tokenStyle.width === 'number') {
  323. token.width = tokenStyle.width;
  324. }
  325. else {
  326. token.width = linesWidths
  327. ? linesWidths[i]
  328. : getWidth(text, font);
  329. }
  330. if (!i && !newLine) {
  331. var tokens = (lines[lines.length - 1] || (lines[0] = new RichTextLine())).tokens;
  332. var tokensLen = tokens.length;
  333. (tokensLen === 1 && tokens[0].isLineHolder)
  334. ? (tokens[0] = token)
  335. : ((text || !tokensLen || isEmptyStr) && tokens.push(token));
  336. }
  337. else {
  338. lines.push(new RichTextLine([token]));
  339. }
  340. }
  341. }
  342. function isLatin(ch) {
  343. var code = ch.charCodeAt(0);
  344. return code >= 0x21 && code <= 0xFF;
  345. }
  346. var breakCharMap = reduce(',&?/;] '.split(''), function (obj, ch) {
  347. obj[ch] = true;
  348. return obj;
  349. }, {});
  350. function isWordBreakChar(ch) {
  351. if (isLatin(ch)) {
  352. if (breakCharMap[ch]) {
  353. return true;
  354. }
  355. return false;
  356. }
  357. return true;
  358. }
  359. function wrapText(text, font, lineWidth, isBreakAll, lastAccumWidth) {
  360. var lines = [];
  361. var linesWidths = [];
  362. var line = '';
  363. var currentWord = '';
  364. var currentWordWidth = 0;
  365. var accumWidth = 0;
  366. for (var i = 0; i < text.length; i++) {
  367. var ch = text.charAt(i);
  368. if (ch === '\n') {
  369. if (currentWord) {
  370. line += currentWord;
  371. accumWidth += currentWordWidth;
  372. }
  373. lines.push(line);
  374. linesWidths.push(accumWidth);
  375. line = '';
  376. currentWord = '';
  377. currentWordWidth = 0;
  378. accumWidth = 0;
  379. continue;
  380. }
  381. var chWidth = getWidth(ch, font);
  382. var inWord = isBreakAll ? false : !isWordBreakChar(ch);
  383. if (!lines.length
  384. ? lastAccumWidth + accumWidth + chWidth > lineWidth
  385. : accumWidth + chWidth > lineWidth) {
  386. if (!accumWidth) {
  387. if (inWord) {
  388. lines.push(currentWord);
  389. linesWidths.push(currentWordWidth);
  390. currentWord = ch;
  391. currentWordWidth = chWidth;
  392. }
  393. else {
  394. lines.push(ch);
  395. linesWidths.push(chWidth);
  396. }
  397. }
  398. else if (line || currentWord) {
  399. if (inWord) {
  400. if (!line) {
  401. line = currentWord;
  402. currentWord = '';
  403. currentWordWidth = 0;
  404. accumWidth = currentWordWidth;
  405. }
  406. lines.push(line);
  407. linesWidths.push(accumWidth - currentWordWidth);
  408. currentWord += ch;
  409. currentWordWidth += chWidth;
  410. line = '';
  411. accumWidth = currentWordWidth;
  412. }
  413. else {
  414. if (currentWord) {
  415. line += currentWord;
  416. accumWidth += currentWordWidth;
  417. currentWord = '';
  418. currentWordWidth = 0;
  419. }
  420. lines.push(line);
  421. linesWidths.push(accumWidth);
  422. line = ch;
  423. accumWidth = chWidth;
  424. }
  425. }
  426. continue;
  427. }
  428. accumWidth += chWidth;
  429. if (inWord) {
  430. currentWord += ch;
  431. currentWordWidth += chWidth;
  432. }
  433. else {
  434. if (currentWord) {
  435. line += currentWord;
  436. currentWord = '';
  437. currentWordWidth = 0;
  438. }
  439. line += ch;
  440. }
  441. }
  442. if (!lines.length && !line) {
  443. line = text;
  444. currentWord = '';
  445. currentWordWidth = 0;
  446. }
  447. if (currentWord) {
  448. line += currentWord;
  449. }
  450. if (line) {
  451. lines.push(line);
  452. linesWidths.push(accumWidth);
  453. }
  454. if (lines.length === 1) {
  455. accumWidth += lastAccumWidth;
  456. }
  457. return {
  458. accumWidth: accumWidth,
  459. lines: lines,
  460. linesWidths: linesWidths
  461. };
  462. }