Browse Source

refactor(inline develop): inline string

master
liuyang 5 years ago
parent
commit
0bbd7ec718
  1. 42
      src/global/draw.js
  2. 420
      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 { isRealNull, isRealNum } from './validate';
import { getCellTextSplitArr,getMeasureText,getCellTextInfo } from './getRowlen';
import { getcellvalue } from './getdata';
import { getcellvalue,getRealCellValue } from './getdata';
import { getBorderInfoCompute } from './border';
import { getSheetIndex } from '../methods/get';
import { getObjType, chatatABC, luckysheetfontformat } from '../utils/util';
@ -674,10 +674,7 @@ function luckysheetDrawMain(scrollWidth, scrollHeight, drawWidth, drawHeight, of
// continue;
}
else{
value = getcellvalue(r, c, null, "m");
if(value == null){
value = getcellvalue(r, c);
}
value = getRealCellValue(r,c);
}
if(value == null || value.toString().length == 0){
@ -718,11 +715,13 @@ function luckysheetDrawMain(scrollWidth, scrollHeight, drawWidth, drawHeight, of
let value = null;
let margeMaindata = cell["mc"];
value = getcellvalue(margeMaindata.r, margeMaindata.c, null, "m");
// value = getcellvalue(margeMaindata.r, margeMaindata.c, null, "m");
if(value == null){
value = getcellvalue(margeMaindata.r, margeMaindata.c);
}
// if(value == null){
// value = getcellvalue(margeMaindata.r, margeMaindata.c);
// }
value = getRealCellValue(margeMaindata.r,margeMaindata.c);
r = margeMaindata.r;
c = margeMaindata.c;
@ -2052,19 +2051,26 @@ function cellTextRender(textInfo, ctx, option){
// ctx.fillStyle = "rgb(0,0,0)";
for(let i=0;i<values.length;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);
if(word.cancelLine!=null){
let c = word.cancelLine;
ctx.beginPath();
ctx.moveTo(
(pos_x +c.startX)/Store.zoomRatio ,
(pos_y+c.startY)/Store.zoomRatio
Math.floor((pos_x +c.startX)/Store.zoomRatio)+0.5 ,
Math.floor((pos_y+c.startY)/Store.zoomRatio)+0.5 ,
);
ctx.lineTo(
(pos_x +c.endX)/Store.zoomRatio,
(pos_y+c.endY)/Store.zoomRatio
Math.floor((pos_x +c.endX)/Store.zoomRatio)+0.5 ,
Math.floor((pos_y+c.endY)/Store.zoomRatio)+0.5 ,
);
ctx.lineWidth = 1;
ctx.strokeStyle = ctx.fillStyle;
@ -2078,12 +2084,12 @@ function cellTextRender(textInfo, ctx, option){
let item = underLines[a];
ctx.beginPath();
ctx.moveTo(
(pos_x +item.startX)/Store.zoomRatio ,
(pos_y+item.startY)/Store.zoomRatio
Math.floor((pos_x +item.startX)/Store.zoomRatio)+0.5 ,
Math.floor((pos_y+item.startY)/Store.zoomRatio)
);
ctx.lineTo(
(pos_x +item.endX)/Store.zoomRatio,
(pos_y+ item.endY)/Store.zoomRatio
Math.floor((pos_x +item.endX)/Store.zoomRatio)+0.5,
Math.floor((pos_y+ item.endY)/Store.zoomRatio)+0.5
);
ctx.lineWidth = 1;
ctx.strokeStyle = ctx.fillStyle;

420
src/global/getRowlen.js

@ -196,7 +196,7 @@ function getMeasureText(value, ctx, fontset){
}
if(fontset!=null){
ctx.font = preFont;
ctx.font = fontset;
}
cache.actualBoundingBoxDescent = measureText.actualBoundingBoxDescent;
@ -317,12 +317,52 @@ function getCellTextInfo(cell , ctx, option){
let textContent = {};
textContent.values = [];
let fontset, cancelLine="0", underLine="0", isInline=false, value;
if(cell.ct!=null && cell.ct.t=="inlineStr" && cell.ct.sharedStrings!=null && cell.ct.sharedStrings.length>0){
let strArr = [];
let sharedStrings = cell.ct.s;
for(let i=0;i<cell.ct.sharedStrings.length;i++){
let s = cell.ct.sharedStrings[i];
let fontset, cancelLine="0", underLine="0", isInline=false, value, inlineStringArr=[];
if(cell.ct!=null && cell.ct.t=="inlineStr" && cell.ct.s!=null && cell.ct.s.length>0){
let sharedStrings = cell.ct.s, similarIndex = 0;
for(let i=0;i<sharedStrings.length;i++){
let shareCell = 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;
}
@ -496,7 +536,6 @@ function getCellTextInfo(cell , ctx, option){
}
if(tb=="2" || isInline){//wrap
value = value.toString();
let textW_all = 0; //拆分后宽高度合计
let textH_all = 0;
@ -508,119 +547,286 @@ function getCellTextInfo(cell , ctx, option){
textContent.rotate = rt;
rt = Math.abs(rt);
let anchor = 0, preHeight = 0, preWidth=0, preStr, preTextHeight, preTextWidth, i=0;
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 anchor = 0, preHeight = 0, preWidth=0, preStr, preTextHeight, preTextWidth, i=1;
if(isInline){
while(i <= inlineStringArr.length){
let shareCells = inlineStringArr.slice(anchor, i);
if(shareCells[shareCells.length-1].wrap===true){
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;
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];
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,
}
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
});
if(rt!=0){//rotate
item.textHeight = sc.textHeight;
item.textWidth = sc.textWidth;
}
text_all_split[splitIndex].push(item);
}
splitIndex +=1;
continue;
}
else if(i== value.length){
// console.log("last",anchor, i , str);
if(text_all_split[splitIndex]==null){
text_all_split[splitIndex]= [];
let textWidth=0, textHeight=0;
for(let s=0;s<shareCells.length;s++){
let sc = shareCells[s];
if(sc.measureText==null){
sc.measureText = getMeasureText(sc.v, ctx, sc.fontset);
}
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;
textWidth += sc.measureText.width;
textHeight += sc.measureText.actualBoundingBoxAscent+sc.measureText.actualBoundingBoxDescent;
}
else{
if(text_all_split[splitIndex]==null){
text_all_split[splitIndex]= [];
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;
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;
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
});
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]= [];
}
splitIndex +=1;
}
else if(i== value.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;
}
text_all_split[splitIndex].push({
content:str,
style:fontset,
width:width,
height:height,
left:0,
top:0,
splitIndex:splitIndex,
asc:measureText.actualBoundingBoxAscent,
desc:measureText.actualBoundingBoxDescent
});
else{
if(text_all_split[splitIndex]==null){
text_all_split[splitIndex]= [];
}
i++;
}
}
break;
}
}
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;
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;
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(text_all_split[splitIndex]==null){
text_all_split[splitIndex]= [];
else{//plain
if((width+space_width)>cellWidth && text_all_split[splitIndex]!=null){
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;
}
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:width,
height:height,
left:0,
top:0,
splitIndex:splitIndex,
asc:measureText.actualBoundingBoxAscent,
desc:measureText.actualBoundingBoxDescent
});
break;
}
else{
if(text_all_split[splitIndex]==null){
text_all_split[splitIndex]= [];
}
i++;
}
i++;
}
preWidth = width;
preHeight = height;
preStr = str;
preTextHeight = textHeight;
preTextWidth = textWidth;
}
preWidth = width;
preHeight = height;
preStr = str;
preTextHeight = textHeight;
preTextWidth = textWidth;
}
let split_all_size = [], oneLinemaxWordCount=0;
@ -1117,6 +1323,12 @@ function getCellTextInfo(cell , ctx, 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;
if(wordGroup.inline==true && wordGroup.style!=null){
cancelLine = wordGroup.style.cl;
underLine = wordGroup.style.un;
}
if(cancelLine!="0"){
wordGroup.cancelLine = {};
wordGroup.cancelLine.startX = left;

15
src/global/getdata.js

@ -261,4 +261,19 @@ export function getOrigincell(r, c, i) {
return data[r][c];
}
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":{
// "fa":"General",
// "t":"inlineStr",
// "sharedStrings":[
// "s":[
// {
// "ff":"等线", //font family
// "fc":"#fff000",//font color

Loading…
Cancel
Save