Browse Source

refactor(inline develop): inline string

master
liuyang 5 years ago
parent
commit
0bbd7ec718
  1. 42
      src/global/draw.js
  2. 410
      src/global/getRowlen.js
  3. 15
      src/global/getdata.js
  4. 2
      src/index.html

42
src/global/draw.js

@ -9,7 +9,7 @@ import { dynamicArrayCompute } from './dynamicArray';
import browser from './browser'; import browser from './browser';
import { isRealNull, isRealNum } from './validate'; import { isRealNull, isRealNum } from './validate';
import { getCellTextSplitArr,getMeasureText,getCellTextInfo } from './getRowlen'; import { getCellTextSplitArr,getMeasureText,getCellTextInfo } from './getRowlen';
import { getcellvalue } from './getdata'; import { getcellvalue,getRealCellValue } from './getdata';
import { getBorderInfoCompute } from './border'; import { getBorderInfoCompute } from './border';
import { getSheetIndex } from '../methods/get'; import { getSheetIndex } from '../methods/get';
import { getObjType, chatatABC, luckysheetfontformat } from '../utils/util'; import { getObjType, chatatABC, luckysheetfontformat } from '../utils/util';
@ -674,10 +674,7 @@ function luckysheetDrawMain(scrollWidth, scrollHeight, drawWidth, drawHeight, of
// continue; // continue;
} }
else{ else{
value = getcellvalue(r, c, null, "m"); value = getRealCellValue(r,c);
if(value == null){
value = getcellvalue(r, c);
}
} }
if(value == null || value.toString().length == 0){ if(value == null || value.toString().length == 0){
@ -718,11 +715,13 @@ function luckysheetDrawMain(scrollWidth, scrollHeight, drawWidth, drawHeight, of
let value = null; let value = null;
let margeMaindata = cell["mc"]; let margeMaindata = cell["mc"];
value = getcellvalue(margeMaindata.r, margeMaindata.c, null, "m"); // value = getcellvalue(margeMaindata.r, margeMaindata.c, null, "m");
if(value == null){ // if(value == null){
value = getcellvalue(margeMaindata.r, margeMaindata.c); // value = getcellvalue(margeMaindata.r, margeMaindata.c);
} // }
value = getRealCellValue(margeMaindata.r,margeMaindata.c);
r = margeMaindata.r; r = margeMaindata.r;
c = margeMaindata.c; c = margeMaindata.c;
@ -2052,19 +2051,26 @@ function cellTextRender(textInfo, ctx, option){
// ctx.fillStyle = "rgb(0,0,0)"; // ctx.fillStyle = "rgb(0,0,0)";
for(let i=0;i<values.length;i++){ for(let i=0;i<values.length;i++){
let word = values[i]; let word = values[i];
ctx.font = word.style; if(word.inline===true && word.style!=null){
ctx.font = word.style.fontset;
ctx.fillStyle = word.style.fc;
}
else{
ctx.font = word.style;
}
ctx.fillText(word.content, (pos_x + word.left)/Store.zoomRatio, (pos_y+word.top)/Store.zoomRatio); ctx.fillText(word.content, (pos_x + word.left)/Store.zoomRatio, (pos_y+word.top)/Store.zoomRatio);
if(word.cancelLine!=null){ if(word.cancelLine!=null){
let c = word.cancelLine; let c = word.cancelLine;
ctx.beginPath(); ctx.beginPath();
ctx.moveTo( ctx.moveTo(
(pos_x +c.startX)/Store.zoomRatio , Math.floor((pos_x +c.startX)/Store.zoomRatio)+0.5 ,
(pos_y+c.startY)/Store.zoomRatio Math.floor((pos_y+c.startY)/Store.zoomRatio)+0.5 ,
); );
ctx.lineTo( ctx.lineTo(
(pos_x +c.endX)/Store.zoomRatio, Math.floor((pos_x +c.endX)/Store.zoomRatio)+0.5 ,
(pos_y+c.endY)/Store.zoomRatio Math.floor((pos_y+c.endY)/Store.zoomRatio)+0.5 ,
); );
ctx.lineWidth = 1; ctx.lineWidth = 1;
ctx.strokeStyle = ctx.fillStyle; ctx.strokeStyle = ctx.fillStyle;
@ -2078,12 +2084,12 @@ function cellTextRender(textInfo, ctx, option){
let item = underLines[a]; let item = underLines[a];
ctx.beginPath(); ctx.beginPath();
ctx.moveTo( ctx.moveTo(
(pos_x +item.startX)/Store.zoomRatio , Math.floor((pos_x +item.startX)/Store.zoomRatio)+0.5 ,
(pos_y+item.startY)/Store.zoomRatio Math.floor((pos_y+item.startY)/Store.zoomRatio)
); );
ctx.lineTo( ctx.lineTo(
(pos_x +item.endX)/Store.zoomRatio, Math.floor((pos_x +item.endX)/Store.zoomRatio)+0.5,
(pos_y+ item.endY)/Store.zoomRatio Math.floor((pos_y+ item.endY)/Store.zoomRatio)+0.5
); );
ctx.lineWidth = 1; ctx.lineWidth = 1;
ctx.strokeStyle = ctx.fillStyle; ctx.strokeStyle = ctx.fillStyle;

410
src/global/getRowlen.js

@ -196,7 +196,7 @@ function getMeasureText(value, ctx, fontset){
} }
if(fontset!=null){ if(fontset!=null){
ctx.font = preFont; ctx.font = fontset;
} }
cache.actualBoundingBoxDescent = measureText.actualBoundingBoxDescent; cache.actualBoundingBoxDescent = measureText.actualBoundingBoxDescent;
@ -317,12 +317,52 @@ function getCellTextInfo(cell , ctx, option){
let textContent = {}; let textContent = {};
textContent.values = []; textContent.values = [];
let fontset, cancelLine="0", underLine="0", isInline=false, value; let fontset, cancelLine="0", underLine="0", isInline=false, value, inlineStringArr=[];
if(cell.ct!=null && cell.ct.t=="inlineStr" && cell.ct.sharedStrings!=null && cell.ct.sharedStrings.length>0){ if(cell.ct!=null && cell.ct.t=="inlineStr" && cell.ct.s!=null && cell.ct.s.length>0){
let strArr = []; let sharedStrings = cell.ct.s, similarIndex = 0;
let sharedStrings = cell.ct.s; for(let i=0;i<sharedStrings.length;i++){
for(let i=0;i<cell.ct.sharedStrings.length;i++){ let shareCell = sharedStrings[i];
let s = cell.ct.sharedStrings[i]; let scfontset = luckysheetfontformat(shareCell);
let fc = shareCell.fc, cl=shareCell.cl,un = shareCell.un, v = shareCell.v;
v = v.replace(/\r\n/g, "_x000D_").replace(/&#13;&#10;/g, "_x000D_").replace(/\r/g, "_x000D_").replace(/\n/g, "_x000D_");
let splitArr = v.split("_x000D_");
for(let x=0;x<splitArr.length;x++){
let newValue = splitArr[x];
if(newValue==""){
inlineStringArr.push({
wrap:true
});
similarIndex++;
}
else{
let newValueArray = newValue.split("");
for(let n=0;n<newValueArray.length;n++){
let nv = newValueArray[n];
inlineStringArr.push({
fontset:scfontset,
fc:fc==null?"#000":fc,
cl:cl==null?0:cl,
un:un==null?0:un,
v: nv,
si:similarIndex
});
}
// if(x!=splitArr.length-1){
// inlineStringArr.push({
// wrap:true
// });
// similarIndex++;
// }
}
}
similarIndex++;
} }
isInline = true; isInline = true;
} }
@ -496,7 +536,6 @@ function getCellTextInfo(cell , ctx, option){
} }
if(tb=="2" || isInline){//wrap if(tb=="2" || isInline){//wrap
value = value.toString();
let textW_all = 0; //拆分后宽高度合计 let textW_all = 0; //拆分后宽高度合计
let textH_all = 0; let textH_all = 0;
@ -508,119 +547,286 @@ function getCellTextInfo(cell , ctx, option){
textContent.rotate = rt; textContent.rotate = rt;
rt = Math.abs(rt); rt = Math.abs(rt);
let anchor = 0, preHeight = 0, preWidth=0, preStr, preTextHeight, preTextWidth, i=0; let anchor = 0, preHeight = 0, preWidth=0, preStr, preTextHeight, preTextWidth, i=1;
while(i <= value.length){ if(isInline){
let str = value.substring(anchor, i); while(i <= inlineStringArr.length){
let measureText = getMeasureText(str, ctx); let shareCells = inlineStringArr.slice(anchor, i);
let textWidth = measureText.width; if(shareCells[shareCells.length-1].wrap===true){
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 anchor = i-1;
// textW_all += textW; for(let s=0;s<shareCells.length-1;s++){
let sc = shareCells[s];
let item = {
content:sc.v,
style:sc,
width:sc.width,
height:sc.height,
left:0,
top:0,
splitIndex:splitIndex,
asc:sc.measureText.actualBoundingBoxAscent,
desc:sc.measureText.actualBoundingBoxDescent,
inline:true,
}
if(rt!=0){//rotate if(rt!=0){//rotate
// console.log("all",anchor, i , str); item.textHeight = sc.textHeight;
if((height+space_height)>cellHeight && text_all_split[splitIndex]!=null){ item.textWidth = sc.textWidth;
// console.log("cut",anchor, i , str); }
anchor = i-1;
text_all_split[splitIndex].push({ text_all_split[splitIndex].push(item);
content:preStr, }
style:fontset,
width:preWidth,
height:preHeight,
left:0,
top:0,
splitIndex:splitIndex,
textHeight:preTextHeight,
textWidth:preTextWidth,
asc:measureText.actualBoundingBoxAscent,
desc:measureText.actualBoundingBoxDescent
});
splitIndex +=1; splitIndex +=1;
continue;
} }
else if(i== value.length){
// console.log("last",anchor, i , str); let textWidth=0, textHeight=0;
if(text_all_split[splitIndex]==null){ for(let s=0;s<shareCells.length;s++){
text_all_split[splitIndex]= []; let sc = shareCells[s];
if(sc.measureText==null){
sc.measureText = getMeasureText(sc.v, ctx, sc.fontset);
} }
text_all_split[splitIndex].push({ textWidth += sc.measureText.width;
content:str, textHeight += sc.measureText.actualBoundingBoxAscent+sc.measureText.actualBoundingBoxDescent;
style:fontset,
width:width,
height:height,
left:0,
top:0,
splitIndex:splitIndex,
textHeight:textHeight,
textWidth:textWidth,
asc:measureText.actualBoundingBoxAscent,
desc:measureText.actualBoundingBoxDescent
});
break;
} }
else{
if(text_all_split[splitIndex]==null){ let width = textWidth * Math.cos(rt*Math.PI/180) + textHeight * Math.sin(rt*Math.PI/180);//consider text box wdith and line height
text_all_split[splitIndex]= [];
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;
if(rt!=0){//rotate
// console.log("all",anchor, i , str);
if((height+space_height)>cellHeight && text_all_split[splitIndex]!=null){
// console.log("cut",anchor, i , str);
anchor = i-1;
for(let s=0;s<shareCells.length-1;s++){
let sc = shareCells[s];
text_all_split[splitIndex].push({
content:sc.v,
style:sc,
width:sc.width,
height:sc.height,
left:0,
top:0,
splitIndex:splitIndex,
textHeight:sc.textHeight,
textWidth:sc.textWidth,
asc:sc.measureText.actualBoundingBoxAscent,
desc:sc.measureText.actualBoundingBoxDescent,
inline:true,
});
}
splitIndex +=1;
}
else if(i== inlineStringArr.length){
// console.log("last",anchor, i , str);
if(text_all_split[splitIndex]==null){
text_all_split[splitIndex]= [];
}
for(let s=0;s<shareCells.length;s++){
let sc = shareCells[s];
text_all_split[splitIndex].push({
content:sc.v,
style:sc,
width:sc.width,
height:sc.height,
left:0,
top:0,
splitIndex:splitIndex,
textHeight:sc.textHeight,
textWidth:sc.textWidth,
asc:sc.measureText.actualBoundingBoxAscent,
desc:sc.measureText.actualBoundingBoxDescent,
inline:true,
});
}
break;
}
else{
if(text_all_split[splitIndex]==null){
text_all_split[splitIndex]= [];
}
i++;
} }
i++;
} }
else{//plain
if((width+space_width)>cellWidth && text_all_split[splitIndex]!=null){
anchor = i-1;
for(let s=0;s<shareCells.length-1;s++){
let sc = shareCells[s];
text_all_split[splitIndex].push({
content:sc.v,
style:sc,
width:sc.width,
height:sc.height,
left:0,
top:0,
splitIndex:splitIndex,
asc:sc.measureText.actualBoundingBoxAscent,
desc:sc.measureText.actualBoundingBoxDescent,
inline:true,
});
}
splitIndex +=1;
}
else if(i== inlineStringArr.length){
if(text_all_split[splitIndex]==null){
text_all_split[splitIndex]= [];
}
for(let s=0;s<shareCells.length;s++){
let sc = shareCells[s];
text_all_split[splitIndex].push({
content:sc.v,
style:sc,
width:sc.width,
height:sc.height,
left:0,
top:0,
splitIndex:splitIndex,
asc:sc.measureText.actualBoundingBoxAscent,
desc:sc.measureText.actualBoundingBoxDescent,
inline:true,
});
}
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){ else{
value = value.toString();
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;
anchor = i-1; let width = textWidth * Math.cos(rt*Math.PI/180) + textHeight * Math.sin(rt*Math.PI/180);//consider text box wdith and line height
text_all_split[splitIndex].push({ let height = textWidth * Math.sin(rt*Math.PI/180) + textHeight * Math.cos(rt*Math.PI/180);//consider text box wdith and line height
content:preStr,
style:fontset,
width:preWidth,
height:preHeight,
left:0,
top:0,
splitIndex:splitIndex,
asc:measureText.actualBoundingBoxAscent,
desc:measureText.actualBoundingBoxDescent
});
splitIndex +=1; // textW_all += textW;
if(rt!=0){//rotate
// console.log("all",anchor, i , str);
if((height+space_height)>cellHeight && text_all_split[splitIndex]!=null){
// console.log("cut",anchor, i , str);
anchor = i-1;
text_all_split[splitIndex].push({
content:preStr,
style:fontset,
width:preWidth,
height:preHeight,
left:0,
top:0,
splitIndex:splitIndex,
textHeight:preTextHeight,
textWidth:preTextWidth,
asc:measureText.actualBoundingBoxAscent,
desc:measureText.actualBoundingBoxDescent
});
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,
width:width,
height:height,
left:0,
top:0,
splitIndex:splitIndex,
textHeight:textHeight,
textWidth:textWidth,
asc:measureText.actualBoundingBoxAscent,
desc:measureText.actualBoundingBoxDescent
});
break;
}
else{
if(text_all_split[splitIndex]==null){
text_all_split[splitIndex]= [];
}
i++;
}
} }
else if(i== value.length){ else{//plain
if(text_all_split[splitIndex]==null){ if((width+space_width)>cellWidth && text_all_split[splitIndex]!=null){
text_all_split[splitIndex]= [];
anchor = i-1;
text_all_split[splitIndex].push({
content:preStr,
style:fontset,
width:preWidth,
height:preHeight,
left:0,
top:0,
splitIndex:splitIndex,
asc:measureText.actualBoundingBoxAscent,
desc:measureText.actualBoundingBoxDescent
});
splitIndex +=1;
} }
text_all_split[splitIndex].push({ else if(i== value.length){
content:str, if(text_all_split[splitIndex]==null){
style:fontset, text_all_split[splitIndex]= [];
width:width, }
height:height, text_all_split[splitIndex].push({
left:0, content:str,
top:0, style:fontset,
splitIndex:splitIndex, width:width,
asc:measureText.actualBoundingBoxAscent, height:height,
desc:measureText.actualBoundingBoxDescent left:0,
}); top:0,
splitIndex:splitIndex,
asc:measureText.actualBoundingBoxAscent,
desc:measureText.actualBoundingBoxDescent
});
break; break;
} }
else{ else{
if(text_all_split[splitIndex]==null){ if(text_all_split[splitIndex]==null){
text_all_split[splitIndex]= []; text_all_split[splitIndex]= [];
}
i++;
} }
i++;
} }
}
preWidth = width; preWidth = width;
preHeight = height; preHeight = height;
preStr = str; preStr = str;
preTextHeight = textHeight; preTextHeight = textHeight;
preTextWidth = textWidth; preTextWidth = textWidth;
}
} }
let split_all_size = [], oneLinemaxWordCount=0; let split_all_size = [], oneLinemaxWordCount=0;
@ -1117,6 +1323,12 @@ function getCellTextInfo(cell , ctx, option){
function drawLineInfo(wordGroup, cancelLine,underLine,option){ function drawLineInfo(wordGroup, cancelLine,underLine,option){
let left = option.left, top = option.top, width=option.width, height = option.height, asc = option.asc,desc = option.desc; let left = option.left, top = option.top, width=option.width, height = option.height, asc = option.asc,desc = option.desc;
if(wordGroup.inline==true && wordGroup.style!=null){
cancelLine = wordGroup.style.cl;
underLine = wordGroup.style.un;
}
if(cancelLine!="0"){ if(cancelLine!="0"){
wordGroup.cancelLine = {}; wordGroup.cancelLine = {};
wordGroup.cancelLine.startX = left; wordGroup.cancelLine.startX = left;

15
src/global/getdata.js

@ -262,3 +262,18 @@ export function getOrigincell(r, c, i) {
} }
export function getRealCellValue(r, c){
let value = getcellvalue(r, c, null, "m");
if(value == null){
value = getcellvalue(r, c);
if(value==null){
let ct = getcellvalue(r, c, null, "ct");
if(ct!=null && ct.t=="inlineStr" && ct.s!=null && ct.s.length>0){
value = ct.s;
}
}
}
return value;
}

2
src/index.html

@ -37,7 +37,7 @@
// "ct":{ // "ct":{
// "fa":"General", // "fa":"General",
// "t":"inlineStr", // "t":"inlineStr",
// "sharedStrings":[ // "s":[
// { // {
// "ff":"等线", //font family // "ff":"等线", //font family
// "fc":"#fff000",//font color // "fc":"#fff000",//font color

Loading…
Cancel
Save