Browse Source

fix: 换行

master
jerry 5 years ago
parent
commit
94022a48b6
  1. 316
      src/global/getRowlen.js

316
src/global/getRowlen.js

@ -893,154 +893,187 @@ function getCellTextInfo(cell , ctx, option){
} }
else{ else{
value = value.toString(); value = value.toString();
while(i <= value.length){ let splitValue = value.replace(/\r\n/g, "_x000D_").replace(/&#13;&#10;/g, "_x000D_").replace(/\r/g, "_x000D_").replace(/\n/g, "_x000D_");
let str = value.substring(anchor, i); let splitArr = splitValue.split("_x000D_");
let measureText = getMeasureText(str, ctx); for(let x=0, len=splitArr.length;x<len;x++){
let textWidth = measureText.width; value = splitArr[x];
let textHeight = measureText.actualBoundingBoxAscent+measureText.actualBoundingBoxDescent; anchor = 0;
preStr = undefined;
let width = textWidth * Math.cos(rt*Math.PI/180) + textHeight * Math.sin(rt*Math.PI/180);//consider text box wdith and line height preTextHeight = undefined;
preTextWidth = undefined;
preMeasureText = undefined;
i=1;
spaceOrTwoByte=null;
while(i <= value.length){
let str = value.substring(anchor, i);
let measureText = getMeasureText(str, ctx);
let textWidth = measureText.width;
let textHeight = measureText.actualBoundingBoxAscent+measureText.actualBoundingBoxDescent;
let width = textWidth * Math.cos(rt*Math.PI/180) + textHeight * Math.sin(rt*Math.PI/180);//consider text box wdith and line height
let height = textWidth * Math.sin(rt*Math.PI/180) + textHeight * Math.cos(rt*Math.PI/180);//consider text box wdith and line height
let lastWord = str.substr(str.length-1,1);
if(lastWord==" " || checkWordByteLength(lastWord)==2){
if(preMeasureText!=null){
spaceOrTwoByte = {
index:i,
str:preStr,
width:preTextWidth,
height:preTextHeight,
asc:preMeasureText.actualBoundingBoxAscent,
desc:preMeasureText.actualBoundingBoxDescent,
};
}
let height = textWidth * Math.sin(rt*Math.PI/180) + textHeight * Math.cos(rt*Math.PI/180);//consider text box wdith and line height
let lastWord = str.substr(str.length-1,1);
let isSpace = lastWord.replace(/\r\n/g, "_x000D_").replace(/&#13;&#10;/g, "_x000D_").replace(/\r/g, "_x000D_").replace(/\n/g, "_x000D_");
isSpace = isSpace === '_x000D_'
if(lastWord==" " || checkWordByteLength(lastWord)==2){
if(preMeasureText!=null){
spaceOrTwoByte = {
index:i,
str:preStr,
width:preTextWidth,
height:preTextHeight,
asc:preMeasureText.actualBoundingBoxAscent,
desc:preMeasureText.actualBoundingBoxDescent,
};
} }
// textW_all += textW;
// console.log(str,anchor,i);
if(rt!=0){//rotate
// console.log("all",anchor, i , str);
if((height+space_height)>cellHeight && text_all_split[splitIndex]!=null && i!= value.length){
// console.log("cut",anchor, i , str);
} if(spaceOrTwoByte!=null && spaceOrTwoByte.index<i){
// textW_all += textW;
// console.log(str,anchor,i);
if(rt!=0){//rotate
// console.log("all",anchor, i , str);
if((height+space_height)>cellHeight && text_all_split[splitIndex]!=null && i!= value.length){
// console.log("cut",anchor, i , str);
if(spaceOrTwoByte!=null && spaceOrTwoByte.index<i){ anchor = spaceOrTwoByte.index;
anchor = spaceOrTwoByte.index; i = spaceOrTwoByte.index + 1;
i = spaceOrTwoByte.index + 1; text_all_split[splitIndex].push({
content:spaceOrTwoByte.str,
style:fontset,
width:spaceOrTwoByte.width,
height:spaceOrTwoByte.height,
left:0,
top:0,
splitIndex:splitIndex,
asc:spaceOrTwoByte.asc,
desc:spaceOrTwoByte.desc,
fs:fontSize,
});
text_all_split[splitIndex].push({ // console.log(1,anchor,i,splitIndex , spaceOrTwoByte.str);
content:spaceOrTwoByte.str,
style:fontset,
width:spaceOrTwoByte.width,
height:spaceOrTwoByte.height,
left:0,
top:0,
splitIndex:splitIndex,
asc:spaceOrTwoByte.asc,
desc:spaceOrTwoByte.desc,
fs:fontSize,
});
// console.log(1,anchor,i,splitIndex , spaceOrTwoByte.str); splitIndex +=1;
splitIndex +=1; spaceOrTwoByte = null;
spaceOrTwoByte = null;
}
else{
anchor = i-1;
} text_all_split[splitIndex].push({
else{ content:preStr,
anchor = i-1; style:fontset,
left:0,
top:0,
splitIndex:splitIndex,
height:preTextHeight,
width:preTextWidth,
asc:measureText.actualBoundingBoxAscent,
desc:measureText.actualBoundingBoxDescent,
fs:fontSize,
});
// console.log(2,anchor,i, splitIndex, preStr);
splitIndex +=1;
}
}
else if(i== value.length){
// console.log("last",anchor, i , str);
if(text_all_split[splitIndex]==null){
text_all_split[splitIndex]= [];
}
text_all_split[splitIndex].push({ text_all_split[splitIndex].push({
content:preStr, content:str,
style:fontset, style:fontset,
left:0, left:0,
top:0, top:0,
splitIndex:splitIndex, splitIndex:splitIndex,
height:preTextHeight, height:textHeight,
width:preTextWidth, width:textWidth,
asc:measureText.actualBoundingBoxAscent, asc:measureText.actualBoundingBoxAscent,
desc:measureText.actualBoundingBoxDescent, desc:measureText.actualBoundingBoxDescent,
fs:fontSize, fs:fontSize,
}); });
break;
}
else{
if(text_all_split[splitIndex]==null){
text_all_split[splitIndex]= [];
}
i++;
}
}
else{//plain
if((width+space_width)>cellWidth && text_all_split[splitIndex]!=null && i!= value.length){
// console.log(spaceOrTwoByte, i, anchor);
if(spaceOrTwoByte!=null && spaceOrTwoByte.index<i){
// console.log(2,anchor,i, splitIndex, preStr); anchor = spaceOrTwoByte.index;
splitIndex +=1; i = spaceOrTwoByte.index + 1;
text_all_split[splitIndex].push({
content:spaceOrTwoByte.str,
style:fontset,
width:spaceOrTwoByte.width,
height:spaceOrTwoByte.height,
left:0,
top:0,
splitIndex:splitIndex,
asc:spaceOrTwoByte.asc,
desc:spaceOrTwoByte.desc,
fs:fontSize,
});
} splitIndex +=1;
}
else if(i== value.length){
// console.log("last",anchor, i , str);
if(text_all_split[splitIndex]==null){
text_all_split[splitIndex]= [];
}
text_all_split[splitIndex].push({
content:str,
style:fontset,
left:0,
top:0,
splitIndex:splitIndex,
height:textHeight,
width:textWidth,
asc:measureText.actualBoundingBoxAscent,
desc:measureText.actualBoundingBoxDescent,
fs:fontSize,
});
break;
}
else{
if(text_all_split[splitIndex]==null){
text_all_split[splitIndex]= [];
}
i++;
}
}
else{//plain
if((width+space_width)>cellWidth && text_all_split[splitIndex]!=null && i!= value.length){
// console.log(spaceOrTwoByte, i, anchor);
if(spaceOrTwoByte!=null && spaceOrTwoByte.index<i){
anchor = spaceOrTwoByte.index; spaceOrTwoByte = null;
i = spaceOrTwoByte.index + 1;
text_all_split[splitIndex].push({
content:spaceOrTwoByte.str,
style:fontset,
width:spaceOrTwoByte.width,
height:spaceOrTwoByte.height,
left:0,
top:0,
splitIndex:splitIndex,
asc:spaceOrTwoByte.asc,
desc:spaceOrTwoByte.desc,
fs:fontSize,
});
splitIndex +=1; }
else{
spaceOrTwoByte = null; spaceOrTwoByte = null;
anchor = i-1;
text_all_split[splitIndex].push({
content:preStr,
style:fontset,
width:preTextWidth,
height:preTextHeight,
left:0,
top:0,
splitIndex:splitIndex,
asc:measureText.actualBoundingBoxAscent,
desc:measureText.actualBoundingBoxDescent,
fs:fontSize,
});
// console.log(2);
}
else{
spaceOrTwoByte = null;
anchor = i-1;
splitIndex +=1;
}
}
else if(i== value.length){
if(text_all_split[splitIndex]==null){
text_all_split[splitIndex]= [];
}
text_all_split[splitIndex].push({ text_all_split[splitIndex].push({
content:preStr, content:str,
style:fontset, style:fontset,
width:preTextWidth, width:textWidth,
height:preTextHeight, height:textHeight,
left:0, left:0,
top:0, top:0,
splitIndex:splitIndex, splitIndex:splitIndex,
@ -1049,64 +1082,23 @@ function getCellTextInfo(cell , ctx, option){
fs:fontSize, fs:fontSize,
}); });
// console.log(2); break;
splitIndex +=1;
} }
} else{
else if(isSpace && text_all_split[splitIndex]!=null && i!= value.length) { if(text_all_split[splitIndex]==null){
text_all_split[splitIndex]= [];
spaceOrTwoByte = null; }
anchor = i; i++;
text_all_split[splitIndex].push({
content:str,
style:fontset,
width:preTextWidth,
height:preTextHeight,
left:0,
top:0,
splitIndex:splitIndex,
asc:measureText.actualBoundingBoxAscent,
desc:measureText.actualBoundingBoxDescent,
fs:fontSize,
});
splitIndex +=1;
}
else if(i== value.length){
if(text_all_split[splitIndex]==null){
text_all_split[splitIndex]= [];
}
text_all_split[splitIndex].push({
content:str,
style:fontset,
width:textWidth,
height:textHeight,
left:0,
top:0,
splitIndex:splitIndex,
asc:measureText.actualBoundingBoxAscent,
desc:measureText.actualBoundingBoxDescent,
fs:fontSize,
});
break;
}
else{
if(text_all_split[splitIndex]==null){
text_all_split[splitIndex]= [];
} }
i++;
} }
}
preStr = str; preStr = str;
preTextHeight = textHeight; preTextHeight = textHeight;
preTextWidth = textWidth; preTextWidth = textWidth;
preMeasureText = measureText; preMeasureText = measureText;
}
splitIndex +=1;
} }
// console.log(text_all_split) // console.log(text_all_split)

Loading…
Cancel
Save