From 0b689adea0ffc5cdce61ed5098c6dea5162669d8 Mon Sep 17 00:00:00 2001 From: JoneLau <32630749+JoneLau@users.noreply.github.com> Date: Sat, 30 Mar 2019 18:19:00 +0800 Subject: [PATCH] fix tip (#4120) --- cocos/3d/ui/assembler/label/bmfont.ts | 1 - cocos/3d/ui/assembler/label/ttfUtils.ts | 55 +++++++------- cocos/3d/ui/assembler/utils.ts | 2 +- cocos/assets/CCSpriteFrame.ts | 3 +- .../utils/{text-utils.js => text-utils.ts} | 71 +++++++++++-------- 5 files changed, 73 insertions(+), 59 deletions(-) rename cocos/core/utils/{text-utils.js => text-utils.ts} (71%) diff --git a/cocos/3d/ui/assembler/label/bmfont.ts b/cocos/3d/ui/assembler/label/bmfont.ts index b64e412b3f..4d613e247a 100644 --- a/cocos/3d/ui/assembler/label/bmfont.ts +++ b/cocos/3d/ui/assembler/label/bmfont.ts @@ -29,7 +29,6 @@ import { Rect } from '../../../../core/value-types'; import { RenderData } from '../../../../renderer/ui/renderData'; import { UI } from '../../../../renderer/ui/ui'; import { LabelComponent } from '../../components/label-component'; -import { MeshBuffer } from '../../mesh-buffer'; import { IAssembler } from '../assembler'; import { fillMeshVertices3D } from '../utils'; import { bmfontUtils } from './bmfontUtils'; diff --git a/cocos/3d/ui/assembler/label/ttfUtils.ts b/cocos/3d/ui/assembler/label/ttfUtils.ts index a91bd4236a..952edef6fe 100644 --- a/cocos/3d/ui/assembler/label/ttfUtils.ts +++ b/cocos/3d/ui/assembler/label/ttfUtils.ts @@ -26,10 +26,9 @@ import { LabelOutline } from '../../../../2d/label/label-outline'; import { SpriteFrame } from '../../../../assets/CCSpriteFrame'; import { Component } from '../../../../components/component'; -import macro from '../../../../core/platform/CCMacro'; -import * as textUtils from '../../../../core/utils/text-utils'; +import { fragmentText, safeMeasureText } from '../../../../core/utils/text-utils'; import { Color, Size } from '../../../../core/value-types'; -import { LabelComponent } from '../../components/label-component'; +import { HorizontalTextAlignment, LabelComponent, VerticalTextAlignment } from '../../components/label-component'; const Overflow = LabelComponent.Overflow; const WHITE = Color.WHITE; @@ -228,20 +227,20 @@ export const ttfUtils = { let labelX; let firstLinelabelY; - if (_hAlign === macro.TextAlignment.RIGHT) { + if (_hAlign === HorizontalTextAlignment.RIGHT) { labelX = _canvasSize.width - _margin; } - else if (_hAlign === macro.TextAlignment.CENTER) { + else if (_hAlign === HorizontalTextAlignment.CENTER) { labelX = _canvasSize.width / 2; } else { labelX = 0 + _margin; } - if (_vAlign === macro.VerticalTextAlignment.TOP) { + if (_vAlign === VerticalTextAlignment.TOP) { firstLinelabelY = 0; } - else if (_vAlign === macro.VerticalTextAlignment.CENTER) { + else if (_vAlign === VerticalTextAlignment.CENTER) { firstLinelabelY = _canvasSize.height / 2 - lineHeight * (lineCount - 1) / 2; } else { @@ -303,10 +302,10 @@ export const ttfUtils = { labelX = 0 + _margin; - if (_vAlign === macro.VerticalTextAlignment.TOP) { + if (_vAlign === VerticalTextAlignment.TOP) { firstLinelabelY = _fontSize; } - else if (_vAlign === macro.VerticalTextAlignment.CENTER) { + else if (_vAlign === VerticalTextAlignment.CENTER) { firstLinelabelY = _canvasSize.height / 2 - lineHeight * (lineCount - 1) / 2 + _fontSize / 2; } else { @@ -317,6 +316,10 @@ export const ttfUtils = { }, _updateLabelDimensions () { + if (!_context){ + return; + } + const paragraphedStrings = _string.split('\n'); if (_overflow === Overflow.RESIZE_HEIGHT) { @@ -327,7 +330,7 @@ export const ttfUtils = { let canvasSizeX = 0; let canvasSizeY = 0; for (const para of paragraphedStrings) { - const paraLength = textUtils.safeMeasureText(_context, para); + const paraLength = safeMeasureText(_context, para); canvasSizeX = canvasSizeX > paraLength ? canvasSizeX : paraLength; } canvasSizeY = _splitedStrings.length * this._getLineHeight(); @@ -353,20 +356,20 @@ export const ttfUtils = { let hAlign; let vAlign; - if (_hAlign === macro.TextAlignment.RIGHT) { + if (_hAlign === HorizontalTextAlignment.RIGHT) { hAlign = 'right'; } - else if (_hAlign === macro.TextAlignment.CENTER) { + else if (_hAlign === HorizontalTextAlignment.CENTER) { hAlign = 'center'; } else { hAlign = 'left'; } - if (_vAlign === macro.VerticalTextAlignment.TOP) { + if (_vAlign === VerticalTextAlignment.TOP) { vAlign = 'top'; } - else if (_vAlign === macro.VerticalTextAlignment.CENTER) { + else if (_vAlign === VerticalTextAlignment.CENTER) { vAlign = 'middle'; } else { @@ -380,17 +383,17 @@ export const ttfUtils = { }, _calculateSplitedStrings () { + if (!_context){ + return; + } const paragraphedStrings = _string.split('\n'); if (_isWrapText) { _splitedStrings = []; const canvasWidthNoMargin = _canvasSize.width - 2 * _margin; for (const para of paragraphedStrings) { - const allWidth = textUtils.safeMeasureText(_context, para); - const textFragment = textUtils.fragmentText(para, - allWidth, - canvasWidthNoMargin, - this._measureText(_context!)); + const allWidth = safeMeasureText(_context, para); + const textFragment = fragmentText(para, allWidth, canvasWidthNoMargin, this._measureText(_context!)); _splitedStrings = _splitedStrings.concat(textFragment); } } @@ -429,7 +432,7 @@ export const ttfUtils = { const paragraphLength: number[] = []; for (const para of paragraphedStrings) { - const width: number = textUtils.safeMeasureText(ctx, para); + const width: number = safeMeasureText(ctx, para); paragraphLength.push(width); } @@ -437,8 +440,8 @@ export const ttfUtils = { }, _measureText (ctx: CanvasRenderingContext2D) { - return (string) => { - return textUtils.safeMeasureText(ctx, string); + return (string: string) => { + return safeMeasureText(ctx, string); }; }, @@ -470,7 +473,7 @@ export const ttfUtils = { totalHeight = canvasHeightNoMargin + 1; maxLength = canvasWidthNoMargin + 1; let actualFontSize = _fontSize + 1; - let textFragment = ''; + let textFragment: string[] = []; let tryDivideByTwo = true; let startShrinkFontSize = actualFontSize | 0; @@ -493,13 +496,13 @@ export const ttfUtils = { totalHeight = 0; for (i = 0; i < paragraphedStrings.length; ++i) { let j = 0; - const allWidth = textUtils.safeMeasureText(_context, paragraphedStrings[i]); - textFragment = textUtils.fragmentText(paragraphedStrings[i], + const allWidth = safeMeasureText(_context, paragraphedStrings[i]); + textFragment = fragmentText(paragraphedStrings[i], allWidth, canvasWidthNoMargin, this._measureText(_context)); while (j < textFragment.length) { - const measureWidth = textUtils.safeMeasureText(_context, textFragment[j]); + const measureWidth = safeMeasureText(_context, textFragment[j]); maxLength = measureWidth; totalHeight += this._getLineHeight(); ++j; diff --git a/cocos/3d/ui/assembler/utils.ts b/cocos/3d/ui/assembler/utils.ts index cec915b357..be536cfd45 100644 --- a/cocos/3d/ui/assembler/utils.ts +++ b/cocos/3d/ui/assembler/utils.ts @@ -2,9 +2,9 @@ import { Color, Mat4 } from '../../../core/value-types'; import { color4, vec3 } from '../../../core/vmath/index'; import { RenderData } from '../../../renderer/ui/renderData'; +import { UI } from '../../../renderer/ui/ui'; import { Node } from '../../../scene-graph/node'; import { MeshBuffer } from '../mesh-buffer'; -import { UI } from '../../../renderer/ui/ui'; const vec3_temp = vec3.create(); const _worldMatrix = new Mat4(); diff --git a/cocos/assets/CCSpriteFrame.ts b/cocos/assets/CCSpriteFrame.ts index 0ce7ca2153..d3caceece2 100644 --- a/cocos/assets/CCSpriteFrame.ts +++ b/cocos/assets/CCSpriteFrame.ts @@ -542,7 +542,8 @@ export class SpriteFrame extends EventTargetFactory(Texture2D) { // /** // * !#en - // * If you do not need to use the SpriteFrame temporarily, you can call this method so that its texture could be garbage collected. Then when you need to render the SpriteFrame, you should call `ensureLoadTexture` manually to reload texture. + // * If you do not need to use the SpriteFrame temporarily, you can call this method so that its texture could be garbage collected. + // * Then when you need to render the SpriteFrame, you should call `ensureLoadTexture` manually to reload texture. // * !#zh // * 当你暂时不再使用这个 SpriteFrame 时,可以调用这个方法来保证引用的贴图对象能被 GC。然后当你要渲染 SpriteFrame 时,你需要手动调用 `ensureLoadTexture` 来重新加载贴图。 // * diff --git a/cocos/core/utils/text-utils.js b/cocos/core/utils/text-utils.ts similarity index 71% rename from cocos/core/utils/text-utils.js rename to cocos/core/utils/text-utils.ts index 652b79fbc3..42c663d433 100644 --- a/cocos/core/utils/text-utils.js +++ b/cocos/core/utils/text-utils.ts @@ -31,46 +31,56 @@ const LAST_ENGLISH_REG = /[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôûаíìÍÌ const FIRST_ENGLISH_REG = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôûаíìÍÌïÁÀáàÉÈÒÓòóŐőÙÚŰúűñÑæÆœŒÃÂãÔõěščřžýáíéóúůťďňĚŠČŘŽÁÍÉÓÚŤżźśóńłęćąŻŹŚÓŃŁĘĆĄ-яА-ЯЁё]/; const WRAP_INSPECTION = true; -export function isUnicodeCJK (ch) { - var __CHINESE_REG = /^[\u4E00-\u9FFF\u3400-\u4DFF]+$/; - var __JAPANESE_REG = /[\u3000-\u303F]|[\u3040-\u309F]|[\u30A0-\u30FF]|[\uFF00-\uFFEF]|[\u4E00-\u9FAF]|[\u2605-\u2606]|[\u2190-\u2195]|\u203B/g; - var __KOREAN_REG = /^[\u1100-\u11FF]|[\u3130-\u318F]|[\uA960-\uA97F]|[\uAC00-\uD7AF]|[\uD7B0-\uD7FF]+$/; +export function isUnicodeCJK (ch: string) { + const __CHINESE_REG = /^[\u4E00-\u9FFF\u3400-\u4DFF]+$/; + const __JAPANESE_REG = /[\u3000-\u303F]|[\u3040-\u309F]|[\u30A0-\u30FF]|[\uFF00-\uFFEF]|[\u4E00-\u9FAF]|[\u2605-\u2606]|[\u2190-\u2195]|\u203B/g; + const __KOREAN_REG = /^[\u1100-\u11FF]|[\u3130-\u318F]|[\uA960-\uA97F]|[\uAC00-\uD7AF]|[\uD7B0-\uD7FF]+$/; return __CHINESE_REG.test(ch) || __JAPANESE_REG.test(ch) || __KOREAN_REG.test(ch); } -//Checking whether the character is a whitespace -export function isUnicodeSpace (ch) { - ch = ch.charCodeAt(0); - return ((ch >= 9 && ch <= 13) || ch === 32 || ch === 133 || ch === 160 || ch === 5760 || (ch >= 8192 && ch <= 8202) || ch === 8232 || ch === 8233 || ch === 8239 || ch === 8287 || ch === 12288); +// Checking whether the character is a whitespace +export function isUnicodeSpace (ch: string) { + const chCode = ch.charCodeAt(0); + return ((chCode >= 9 && chCode <= 13) || + chCode === 32 || + chCode === 133 || + chCode === 160 || + chCode === 5760 || + (chCode >= 8192 && chCode <= 8202) || + chCode === 8232 || + chCode === 8233 || + chCode === 8239 || + chCode === 8287 || + chCode === 12288); } -export function safeMeasureText (ctx, string) { - var metric = ctx.measureText(string); +export function safeMeasureText (ctx: CanvasRenderingContext2D, string: string) { + const metric = ctx.measureText(string); return metric && metric.width || 0; } -export function fragmentText (stringToken, allWidth, maxWidth, measureText) { - //check the first character - var wrappedWords = []; - //fast return if strArr is empty - if(stringToken.length === 0 || maxWidth < 0) { +export function fragmentText (stringToken: string, allWidth: number, maxWidth: number, measureText: (string: string) => number) { + // check the first character + const wrappedWords: string[] = []; + // fast return if strArr is empty + if (stringToken.length === 0 || maxWidth < 0) { wrappedWords.push(''); return wrappedWords; } - var text = stringToken; + let text = stringToken; while (allWidth > maxWidth && text.length > 1) { - var fuzzyLen = text.length * ( maxWidth / allWidth ) | 0; - var tmpText = text.substr(fuzzyLen); - var width = allWidth - measureText(tmpText); - var sLine = tmpText; - var pushNum = 0; + let fuzzyLen = text.length * ( maxWidth / allWidth ) | 0; + let tmpText = text.substr(fuzzyLen); + let width = allWidth - measureText(tmpText); + let sLine = tmpText; + let pushNum = 0; - var checkWhile = 0; - var checkCount = 10; + let checkWhile = 0; + const checkCount = 10; - //Exceeded the size + // Exceeded the size while (width > maxWidth && checkWhile++ < checkCount) { fuzzyLen *= maxWidth / width; fuzzyLen = fuzzyLen | 0; @@ -80,10 +90,10 @@ export function fragmentText (stringToken, allWidth, maxWidth, measureText) { checkWhile = 0; - //Find the truncation point + // Find the truncation point while (width <= maxWidth && checkWhile++ < checkCount) { if (tmpText) { - var exec = WORD_REG.exec(tmpText); + const exec = WORD_REG.exec(tmpText); pushNum = exec ? exec[0].length : 1; sLine = tmpText; } @@ -99,21 +109,22 @@ export function fragmentText (stringToken, allWidth, maxWidth, measureText) { sLine = sLine.substr(1); } - var sText = text.substr(0, fuzzyLen), result; + let sText = text.substr(0, fuzzyLen); + let result; - //symbol in the first + // symbol in the first if (WRAP_INSPECTION) { if (SYMBOL_REG.test(sLine || tmpText)) { result = LAST_WORD_REG.exec(sText); fuzzyLen -= result ? result[0].length : 0; - if (fuzzyLen === 0) fuzzyLen = 1; + if (fuzzyLen === 0) { fuzzyLen = 1; } sLine = text.substr(fuzzyLen); sText = text.substr(0, fuzzyLen); } } - //To judge whether a English words are truncated + // To judge whether a English words are truncated if (FIRST_ENGLISH_REG.test(sLine)) { result = LAST_ENGLISH_REG.exec(sText); if (result && sText !== result[0]) {