Browse Source

fix(bug): 文本自动换行bug

master
break-wave 5 years ago
parent
commit
bc926e5c49
  1. 231
      src/global/getRowlen.js

231
src/global/getRowlen.js

@ -19,7 +19,7 @@ function rowlenByRange(d, r1, r2, cfg) {
let canvas = $("#luckysheetTableContent").get(0).getContext("2d");
canvas.textBaseline = 'top'; //textBaseline以top计算
for(let r = r1; r <= r2; r++){
if (cfg_clone["rowhidden"] != null && cfg_clone["rowhidden"][r] != null) {
continue;
@ -50,7 +50,7 @@ function rowlenByRange(d, r1, r2, cfg) {
});
let computeRowlen = 0;
if(textInfo!=null){
computeRowlen = textInfo.textHeightAll+2;
}
@ -63,7 +63,7 @@ function rowlenByRange(d, r1, r2, cfg) {
}
currentRowLen = currentRowLen/Store.zoomRatio;
if(currentRowLen != Store.defaultrowlen){
cfg_clone["rowlen"][r] = currentRowLen;
}
@ -79,7 +79,7 @@ function computeRowlenArr(rowHeight, cfg) {
for (let i = 0; i < rowHeight; i++) {
let rowlen = Store.defaultrowlen;
if (cfg["rowlen"] != null && cfg["rowlen"][i] != null) {
rowlen = cfg["rowlen"][i];
}
@ -92,7 +92,7 @@ function computeRowlenArr(rowHeight, cfg) {
else {
rh_height += rowlen + 1;
}
rowlenArr.push(rh_height);//行的临时长度分布
}
@ -147,7 +147,7 @@ function getMeasureText(value, ctx, fontset){
// cache.width = measureText.width;
// }
// else{
// //measureText.actualBoundingBoxLeft +
// //measureText.actualBoundingBoxLeft +
// cache.width = measureText.actualBoundingBoxRight;
// }
@ -317,7 +317,7 @@ function getCellTextInfo(cell , ctx, option){
let splitArr = v.split("_x000D_"), preNewValue=null;
for(let x=0;x<splitArr.length;x++){
let newValue = splitArr[x];
if(newValue=="" && x!=splitArr.length-1){
inlineStringArr.push({
fontset:scfontset,
@ -343,7 +343,7 @@ function getCellTextInfo(cell , ctx, option){
si:similarIndex,
fs:fs==null?11:fs,
});
}
if(x!=splitArr.length-1 && preNewValue!="" ){
@ -356,15 +356,15 @@ function getCellTextInfo(cell , ctx, option){
fs:fs==null?11:fs,
});
similarIndex++;
}
}
}
preNewValue = newValue;
}
similarIndex++;
}
isInline = true;
}
@ -375,7 +375,7 @@ function getCellTextInfo(cell , ctx, option){
cancelLine = checkstatusByCell(cell ,"cl");//cancelLine
underLine = checkstatusByCell(cell ,"un");//underLine
fontSize = checkstatusByCell(cell ,"fs");
if(cell instanceof Object){
value = cell.m;
if(value == null){
@ -385,16 +385,16 @@ function getCellTextInfo(cell , ctx, option){
else{
value = cell;
}
if(isRealNull(value)){
return null;
}
}
// let measureText = getMeasureText(value, ctx);
// let measureText = getMeasureText(value, ctx);
// //luckysheetTableContent.measureText(value);
// let textWidth = measureText.width;
// let textHeight = measureText.actualBoundingBoxDescent + measureText.actualBoundingBoxAscent;
@ -404,7 +404,7 @@ function getCellTextInfo(cell , ctx, option){
let textW_all = 0; //拆分后宽高度合计
let textH_all = 0;
let textH_all = 0;
let colIndex=0, textH_all_cache=0, textH_all_Column = {}, textH_all_ColumnHeight=[];
if(isInline){
let preShareCell = null;
@ -414,8 +414,8 @@ function getCellTextInfo(cell , ctx, option){
if(shareCell.wrap===true){
value = "M";
showValue = "";
if( preShareCell!=null && preShareCell.wrap!==true && (i<inlineStringArr.length-1)){
// console.log("wrap",i,colIndex,preShareCell.wrap);
textH_all_ColumnHeight.push(textH_all_cache);
@ -431,11 +431,11 @@ function getCellTextInfo(cell , ctx, option){
let textW = measureText.width + space_width;
let textH = measureText.actualBoundingBoxAscent + measureText.actualBoundingBoxDescent + space_height;
// textW_all += textW;
textH_all_cache += textH;
if(tb=="2" && !shareCell.wrap){
if(textH_all_cache>cellHeight && textH_all_Column[colIndex]!=null){
// textW_all += textW;
@ -446,11 +446,11 @@ function getCellTextInfo(cell , ctx, option){
colIndex +=1;
}
}
if(i== inlineStringArr.length-1){
textH_all_ColumnHeight.push(textH_all_cache);
}
if(textH_all_Column[colIndex]==null){
textH_all_Column[colIndex]= [];
}
@ -471,36 +471,36 @@ function getCellTextInfo(cell , ctx, option){
if(shareCell.wrap===true){
item.wrap=true;
}
textH_all_Column[colIndex].push(item);
console.log("normal",i,colIndex,shareCell, preShareCell, textH_all_Column);
preShareCell = shareCell;
}
}
else{
let measureText = getMeasureText(value, ctx);
else{
let measureText = getMeasureText(value, ctx);
let textHeight = measureText.actualBoundingBoxDescent + measureText.actualBoundingBoxAscent;
value = value.toString();
let vArr = [];
if(value.length > 1){
vArr = value.split("");
vArr = value.split("");
}
else{
vArr.push(value);
}
let oneWordWidth = getMeasureText(vArr[0], ctx).width;
for(let i = 0; i < vArr.length; i++){
let textW = oneWordWidth + space_width;
let textH = textHeight + space_height;
// textW_all += textW;
textH_all_cache += textH;
if(tb=="2"){
if(textH_all_cache>cellHeight && textH_all_Column[colIndex]!=null){
// textW_all += textW;
@ -510,15 +510,15 @@ function getCellTextInfo(cell , ctx, option){
colIndex +=1;
}
}
if(i== vArr.length-1){
textH_all_ColumnHeight.push(textH_all_cache);
}
if(textH_all_Column[colIndex]==null){
textH_all_Column[colIndex]= [];
}
textH_all_Column[colIndex].push({
content:vArr[i],
style:fontset,
@ -530,7 +530,7 @@ function getCellTextInfo(cell , ctx, option){
asc:measureText.actualBoundingBoxAscent,
desc:measureText.actualBoundingBoxDescent
});
}
}
@ -555,7 +555,7 @@ function getCellTextInfo(cell , ctx, option){
// console.log("verticalWrap", textContent,cell, option);
return textContent;
}
let cumColumnWidth = 0;
for(let i = 0; i < textH_all_ColumnHeight.length; i++){
let columnHeight = textH_all_ColumnHeight[i];
@ -565,7 +565,7 @@ function getCellTextInfo(cell , ctx, option){
let cumWordHeight = 0;
for(let c=0;c<col.length;c++){
let word = col[c];
let left = space_width + cumColumnWidth;
if(horizonAlign == "0"){
left = cellWidth / 2 + cumColumnWidth - textW_all/2 + space_width*textH_all_ColumnHeight.length;
@ -588,8 +588,8 @@ function getCellTextInfo(cell , ctx, option){
word.top = top;
drawLineInfo(word, cancelLine, underLine,{
width:columnWidth,
height:word.height,
width:columnWidth,
height:word.height,
left:left,
top:top+word.height-space_height,
asc:word.height,
@ -616,7 +616,7 @@ function getCellTextInfo(cell , ctx, option){
if(tb=="2" || isInline){//wrap
let textW_all = 0; //拆分后宽高度合计
let textH_all = 0;
let textH_all = 0;
let textW_all_inner = 0;
// let oneWordWidth = getMeasureText(vArr[0], ctx).width;
@ -632,7 +632,7 @@ function getCellTextInfo(cell , ctx, option){
if(shareCells[shareCells.length-1].wrap===true){
anchor = i;
if(shareCells.length>1){
for(let s=0;s<shareCells.length-1;s++){
let sc = shareCells[s];
@ -649,13 +649,13 @@ function getCellTextInfo(cell , ctx, option){
inline:true,
fs:sc.fs
}
// if(rt!=0){//rotate
// item.textHeight = sc.textHeight;
// item.textWidth = sc.textWidth;
// }
text_all_split[splitIndex].push(item);
text_all_split[splitIndex].push(item);
}
}
@ -700,11 +700,11 @@ function getCellTextInfo(cell , ctx, option){
textHeight = Math.max(sc.measureText.actualBoundingBoxAscent+sc.measureText.actualBoundingBoxDescent);
// console.log(sc.v,sc.measureText.width,sc.measureText.actualBoundingBoxAscent,sc.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
// textW_all += textW;
@ -713,7 +713,7 @@ function getCellTextInfo(cell , ctx, option){
spaceOrTwoByteIndex = i;
}
if(rt!=0){//rotate
// console.log("all",anchor, i , str);
console.log(height,space_height, cellHeight, shareCells,(height+space_height)>cellHeight);
@ -721,7 +721,7 @@ function getCellTextInfo(cell , ctx, option){
// console.log("cut",anchor, i , str);
if(spaceOrTwoByteIndex!=null && spaceOrTwoByteIndex<i){
for(let s=0;s<spaceOrTwoByteIndex-anchor;s++){
let sc = shareCells[s];
text_all_split[splitIndex].push({
@ -745,12 +745,12 @@ function getCellTextInfo(cell , ctx, option){
splitIndex +=1;
spaceOrTwoByteIndex = null;
}
else{
anchor = i-1;
for(let s=0;s<shareCells.length-1;s++){
let sc = shareCells[s];
text_all_split[splitIndex].push({
@ -767,7 +767,7 @@ function getCellTextInfo(cell , ctx, option){
fs:sc.fs
});
}
splitIndex +=1;
}
@ -804,10 +804,10 @@ function getCellTextInfo(cell , ctx, option){
}
else{//plain
if((width+space_width)>cellWidth && text_all_split[splitIndex]!=null && tb=="2" && i!= inlineStringArr.length){
if(spaceOrTwoByteIndex!=null && spaceOrTwoByteIndex<i){
for(let s=0;s<spaceOrTwoByteIndex-anchor;s++){
let sc = shareCells[s];
text_all_split[splitIndex].push({
@ -831,11 +831,11 @@ function getCellTextInfo(cell , ctx, option){
splitIndex +=1;
spaceOrTwoByteIndex = null;
}
else{
anchor = i-1;
for(let s=0;s<shareCells.length-1;s++){
let sc = shareCells[s];
text_all_split[splitIndex].push({
@ -852,7 +852,7 @@ function getCellTextInfo(cell , ctx, option){
fs:sc.fs
});
}
splitIndex +=1;
}
@ -878,7 +878,7 @@ function getCellTextInfo(cell , ctx, option){
fs:sc.fs
});
}
break;
}
else{
@ -898,11 +898,13 @@ function getCellTextInfo(cell , ctx, option){
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);
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 = {
@ -924,11 +926,11 @@ function getCellTextInfo(cell , ctx, option){
// console.log("cut",anchor, i , str);
if(spaceOrTwoByte!=null && spaceOrTwoByte.index<i){
anchor = spaceOrTwoByte.index;
i = spaceOrTwoByte.index + 1;
text_all_split[splitIndex].push({
content:spaceOrTwoByte.str,
style:fontset,
@ -945,15 +947,15 @@ function getCellTextInfo(cell , ctx, option){
// console.log(1,anchor,i,splitIndex , spaceOrTwoByte.str);
splitIndex +=1;
spaceOrTwoByte = null;
}
else{
anchor = i-1;
text_all_split[splitIndex].push({
content:preStr,
style:fontset,
@ -968,10 +970,10 @@ function getCellTextInfo(cell , ctx, option){
});
// console.log(2,anchor,i, splitIndex, preStr);
splitIndex +=1;
}
}
else if(i== value.length){
@ -1004,11 +1006,11 @@ function getCellTextInfo(cell , ctx, option){
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;
i = spaceOrTwoByte.index + 1;
text_all_split[splitIndex].push({
content:spaceOrTwoByte.str,
style:fontset,
@ -1026,14 +1028,14 @@ function getCellTextInfo(cell , ctx, option){
spaceOrTwoByte = null;
}
else{
spaceOrTwoByte = null;
anchor = i-1;
text_all_split[splitIndex].push({
content:preStr,
style:fontset,
@ -1049,11 +1051,30 @@ function getCellTextInfo(cell , ctx, option){
// console.log(2);
splitIndex +=1;
}
}
else if(isSpace && text_all_split[splitIndex]!=null && i!= value.length) {
spaceOrTwoByte = null;
anchor = 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]= [];
@ -1070,7 +1091,7 @@ function getCellTextInfo(cell , ctx, option){
desc:measureText.actualBoundingBoxDescent,
fs:fontSize,
});
break;
}
else{
@ -1080,12 +1101,12 @@ function getCellTextInfo(cell , ctx, option){
i++;
}
}
preStr = str;
preTextHeight = textHeight;
preTextWidth = textWidth;
preMeasureText = measureText;
}
// console.log(text_all_split)
@ -1143,8 +1164,8 @@ function getCellTextInfo(cell , ctx, option){
// console.log(textH_all,textW_all,textW_all_inner);
// let cumColumnWidth = 0;
let cumWordHeight = 0,cumColumnWidth = 0;
let rtPI = rt*Math.PI/180;
let lastLine = split_all_size[splitLen-1];
let rtPI = rt*Math.PI/180;
let lastLine = split_all_size[splitLen-1];
let lastLineSpaceHeight = lastLine.lineHeight;
textH_all = textH_all - lastLineSpaceHeight + lastLine.desc;
let rw = (textH_all)/Math.sin(rtPI) + textW_all_inner*Math.cos(rtPI);
@ -1181,7 +1202,7 @@ function getCellTextInfo(cell , ctx, option){
let size = split_all_size[i];
cumColumnWidth = 0;
for(let c=splitLists.length-1;c>=0;c--){
let wordGroup = splitLists[c];
let left, top;
@ -1192,7 +1213,7 @@ function getCellTextInfo(cell , ctx, option){
if(horizonAlign == "0"){//center
let sh = textH_all/Math.sin(rtPI);
if(verticalAlign == "0"){//mid
left = x + cellWidth/2 - (textW_all/2) + lastLineSpaceHeight*Math.cos(rtPI)/2;
top = y + cellHeight/2 - textH_all/2 - lastLineSpaceHeight*Math.cos(rtPI)/2;
}
@ -1233,17 +1254,17 @@ function getCellTextInfo(cell , ctx, option){
top = y + cellHeight - rw*Math.sin(rtPI) - lastLineSpaceHeight*Math.cos(rtPI);
}
}
}
wordGroup.left = left;
wordGroup.top = top;
// console.log(left, top, cumWordHeight, size.height);
drawLineInfo(wordGroup, cancelLine, underLine,{
width:wordGroup.width,
height:wordGroup.height,
width:wordGroup.width,
height:wordGroup.height,
left:left-wordGroup.width,
top:top,
asc:size.asc,
@ -1271,7 +1292,7 @@ function getCellTextInfo(cell , ctx, option){
let size = split_all_size[i];
cumColumnWidth = 0;
for(let c=0;c<splitLists.length;c++){
let wordGroup = splitLists[c];
let left, top;
@ -1283,7 +1304,7 @@ function getCellTextInfo(cell , ctx, option){
if(horizonAlign == "0"){//center
let sh = textH_all/Math.sin(rtPI);
if(verticalAlign == "0"){//mid
left = x + cellWidth/2 - (textW_all/2) - lastLineSpaceHeight*Math.cos(rtPI)/2;
top = y + cellHeight/2 - textH_all/2 + lastLineSpaceHeight*Math.cos(rtPI)/2;
}
@ -1326,8 +1347,8 @@ function getCellTextInfo(cell , ctx, option){
}
drawLineInfo(wordGroup, cancelLine, underLine,{
width:wordGroup.width,
height:wordGroup.height,
width:wordGroup.width,
height:wordGroup.height,
left:left,
top:top,
asc:size.asc,
@ -1345,7 +1366,7 @@ function getCellTextInfo(cell , ctx, option){
else if(horizonAlign == "2"){
left = cellWidth + cumColumnWidth - size.width;
}
top = (cellHeight - space_height) + cumWordHeight +size.asc-textH_all;
if(verticalAlign == "0"){
top = cellHeight / 2 + cumWordHeight - textH_all/2 + size.asc;
@ -1353,12 +1374,12 @@ function getCellTextInfo(cell , ctx, option){
else if(verticalAlign == "1"){
top = space_height + cumWordHeight+ size.asc;
}
drawLineInfo(wordGroup, cancelLine, underLine,{
width:wordGroup.width,
height:wordGroup.height,
width:wordGroup.width,
height:wordGroup.height,
left:left,
top:top,
asc:size.asc,
@ -1366,7 +1387,7 @@ function getCellTextInfo(cell , ctx, option){
fs:wordGroup.fs
});
}
wordGroup.left = left;
wordGroup.top = top;
@ -1406,7 +1427,7 @@ function getCellTextInfo(cell , ctx, option){
else if(verticalAlign == "1"){//top
textContent.textLeftAll = cellWidth/2;
textContent.textTopAll = rh/2;
}
else if(verticalAlign == "2"){//bottom
textContent.textLeftAll = cellWidth/2;
@ -1450,7 +1471,7 @@ function getCellTextInfo(cell , ctx, option){
}
else{
let measureText = getMeasureText(value, ctx);
let measureText = getMeasureText(value, ctx);
let textWidth = measureText.width;
let textHeight = measureText.actualBoundingBoxDescent + measureText.actualBoundingBoxAscent;
@ -1470,7 +1491,7 @@ function getCellTextInfo(cell , ctx, option){
textContent.textHeightAll = textHeightAll+textHeight/2-measureText.actualBoundingBoxDescent-space_height;
}
textContent.textWidthAll = textWidthAll;
// console.log(textContent.textWidthAll , textContent.textHeightAll);
if(isMode=="onlyWidth"){
// console.log("plain", textContent,cell, option);
@ -1486,9 +1507,9 @@ function getCellTextInfo(cell , ctx, option){
else if(horizonAlign == "2"){ //右对齐
left = (cellWidth - space_width) - width + textHeight * Math.sin(rtPI)*isRotateUp;
}
let top = (cellHeight - space_height) - height + measureText.actualBoundingBoxAscent * Math.cos(rtPI) + textWidth * Math.sin(rtPI)*isRotateUp; //默认为2,下对齐
if(verticalAlign == "0"){ //居中对齐
if(verticalAlign == "0"){ //居中对齐
top = cellHeight / 2 - (height / 2) + measureText.actualBoundingBoxAscent* Math.cos(rtPI) + textWidth * Math.sin(rtPI)*isRotateUp;
}
else if(verticalAlign == "1"){ //上对齐
@ -1507,8 +1528,8 @@ function getCellTextInfo(cell , ctx, option){
}
drawLineInfo(wordGroup, cancelLine, underLine,{
width:textWidth,
height:textHeight,
width:textWidth,
height:textHeight,
left:left,
top:top,
asc:measureText.actualBoundingBoxAscent,

Loading…
Cancel
Save