This commit is contained in:
CPPAlien 2019-07-15 17:47:17 +08:00
parent 3d14c9c446
commit 19b85f12c7

View File

@ -143,16 +143,16 @@ export default class Painter {
} }
_preProcess(view, notClip) { _preProcess(view, notClip) {
let width=0; let width = 0;
let height; let height;
let extra; let extra;
switch (view.type) { switch (view.type) {
case 'text': { case 'text': {
const textArray = view.text.split('\n'); const textArray = view.text.split('\n');
//处理多个连续的'\n' // 处理多个连续的'\n'
for(let i = 0; i < textArray.length ; ++i){ for (let i = 0; i < textArray.length; ++i) {
if(textArray[i]===''){ if (textArray[i] === '') {
textArray[i]=' '; textArray[i] = ' ';
} }
} }
const fontWeight = view.css.fontWeight === 'bold' ? 'bold' : 'normal'; const fontWeight = view.css.fontWeight === 'bold' ? 'bold' : 'normal';
@ -160,9 +160,9 @@ export default class Painter {
this.ctx.font = `normal ${fontWeight} ${view.css.fontSize.toPx()}px ${view.css.fontFamily ? view.css.fontFamily : 'sans-serif'}`; this.ctx.font = `normal ${fontWeight} ${view.css.fontSize.toPx()}px ${view.css.fontFamily ? view.css.fontFamily : 'sans-serif'}`;
// this.ctx.setFontSize(view.css.fontSize.toPx()); // this.ctx.setFontSize(view.css.fontSize.toPx());
// 计算行数 // 计算行数
let lines=0; let lines = 0;
let linesArray=[]; const linesArray = [];
for(let i=0; i<textArray.length; ++i){ for (let i = 0; i < textArray.length; ++i) {
const textLength = this.ctx.measureText(textArray[i]).width; const textLength = this.ctx.measureText(textArray[i]).width;
const partWidth = view.css.width ? view.css.width.toPx() : textLength; const partWidth = view.css.width ? view.css.width.toPx() : textLength;
const calLines = Math.ceil(textLength / partWidth); const calLines = Math.ceil(textLength / partWidth);
@ -363,24 +363,24 @@ export default class Painter {
lines, lines,
lineHeight, lineHeight,
textArray, textArray,
linesArray linesArray,
} = extra; } = extra;
// 如果设置了id则保留 text 的长度 // 如果设置了id则保留 text 的长度
if (view.id) { if (view.id) {
let textWidth = 0; let textWidth = 0;
for(let i = 0; i < textArray.length; ++i){ for (let i = 0; i < textArray.length; ++i) {
textWidth = this.ctx.measureText(textArray[i]).width > textWidth ? this.ctx.measureText(textArray[i]).width : textWidth; textWidth = this.ctx.measureText(textArray[i]).width > textWidth ? this.ctx.measureText(textArray[i]).width : textWidth;
} }
this.globalTextWidth[view.id] = width ? (textWidth < width ? textWidth : width) : textWidth; this.globalTextWidth[view.id] = width ? (textWidth < width ? textWidth : width) : textWidth;
} }
let lineIndex=0; let lineIndex = 0;
for(let j = 0; j < textArray.length; ++j){ for (let j = 0; j < textArray.length; ++j) {
const preLineLength = Math.round(textArray[j].length / linesArray[j]); const preLineLength = Math.round(textArray[j].length / linesArray[j]);
let start = 0; let start = 0;
let alreadyCount = 0; let alreadyCount = 0;
for (let i = 0; i < linesArray[j]; ++i) { for (let i = 0; i < linesArray[j]; ++i) {
//绘制行数大于最大行数,则直接跳出循环 // 绘制行数大于最大行数,则直接跳出循环
if(lineIndex >= lines){ if (lineIndex >= lines) {
break; break;
} }
alreadyCount = preLineLength; alreadyCount = preLineLength;
@ -402,7 +402,7 @@ export default class Painter {
} }
start += text.length; start += text.length;
// 如果是最后一行了,发现还有未绘制完的内容,则加... // 如果是最后一行了,发现还有未绘制完的内容,则加...
if (lineIndex === lines - 1 && (j < textArray.length -1 || start < textArray[j].length)) { if (lineIndex === lines - 1 && (j < textArray.length - 1 || start < textArray[j].length)) {
while (this.ctx.measureText(`${text}...`).width > width) { while (this.ctx.measureText(`${text}...`).width > width) {
if (text.length <= 1) { if (text.length <= 1) {
// 如果只有一个字符时,直接跳出循环 // 如果只有一个字符时,直接跳出循环