123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731 |
- import {luckysheetfontformat} from '../utils/util';
- import menuButton from '../controllers/menuButton';
- import {checkstatusByCell} from './getdata';
- import {colLocationByIndex,colSpanLocationByIndex} from './location';
- import {checkWordByteLength, hasChinaword, isRealNull} from './validate';
- import {isInlineStringCell} from '../controllers/inlineString';
- import Store from '../store';
- /**
- * 计算范围行高
- *
- * @param d 原始数据
- * @param r1 起始行
- * @param r2 截至行
- * @param cfg 配置
- * @returns 计算后的配置
- */
- function rowlenByRange(d, r1, r2, cfg) {
- let cfg_clone = $.extend(true, {}, cfg);
- if(cfg_clone["rowlen"] == null){
- cfg_clone["rowlen"] = {};
- }
- if(cfg_clone["customHeight"] == null){
- cfg_clone["customHeight"] = {};
- }
- 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;
- }
- let currentRowLen = Store.defaultrowlen;
- //如果该行设置过高度,则不自定计算高度。设置的高度优先级大于自动计算高度
- if(cfg_clone["customHeight"][r]==1){
- continue;
- }
- delete cfg_clone["rowlen"][r];
- for(let c = 0; c < d[r].length; c++){
- let cell = d[r][c];
- if(cell == null){
- continue;
- }
- if(cell != null && (cell.v != null || isInlineStringCell(cell)) ){
- let cellWidth;
- if(cell.mc){
- if(c === cell.mc.c){
- let st_cellWidth = colLocationByIndex(c)[0];
- let ed_cellWidth = colLocationByIndex(cell.mc.c + cell.mc.cs - 1)[1];
- cellWidth = ed_cellWidth - st_cellWidth - 2;
- }else{
- continue;
- }
- } else {
- cellWidth = colLocationByIndex(c)[1] - colLocationByIndex(c)[0] - 2;
- }
- let textInfo = getCellTextInfo(cell, canvas,{
- r:r,
- c:c,
- cellWidth:cellWidth
- });
- let computeRowlen = 0;
- if(textInfo!=null){
- computeRowlen = textInfo.textHeightAll+2;
- }
- //比较计算高度和当前高度取最大高度
- if(computeRowlen > currentRowLen){
- currentRowLen = computeRowlen;
- }
- }
- }
- currentRowLen = currentRowLen/Store.zoomRatio;
- if(currentRowLen != Store.defaultrowlen){
- cfg_clone["rowlen"][r] = currentRowLen;
- }else{
- if(cfg["rowlen"]?.[r]){
- cfg_clone["rowlen"][r] = cfg["rowlen"][r]
- }
- }
- }
- return cfg_clone;
- }
- //根据内容计算行高
- function computeRowlenByContent(d, r) {
- let currentRowLen = 0;
- let canvas = $("#luckysheetTableContent").get(0).getContext("2d");
- canvas.textBaseline = 'top'; //textBaseline以top计算
- for(let c = 0; c < d[r].length; c++){
- let cell = d[r][c];
- if (cell == null) {
- continue;
- }
- if (cell.mc != null) {
- if (1 !== cell.mc.rs) {
- continue;
- }
- }
- if(Store.config["colhidden"] != null && Store.config["colhidden"][c] != null){
- continue;
- }
- if(cell != null && (cell.v != null || isInlineStringCell(cell)) ){
- let cellWidth = computeCellWidth(cell, c);
- let textInfo = getCellTextInfo(cell, canvas,{
- r:r,
- c:c,
- cellWidth:cellWidth
- });
- let computeRowlen = 0;
- if (textInfo != null) {
- computeRowlen = textInfo.textHeightAll + 2;
- }
- //比较计算高度和当前高度取最大高度
- if (computeRowlen > currentRowLen) {
- currentRowLen = computeRowlen;
- }
- }
- }
- return currentRowLen;
- }
- function computeCellWidth(cell, col_index) {
- let colLocationArr = colLocationByIndex(col_index);
- if (cell.mc && cell.mc.c !== cell.mc.cs) {
- colLocationArr = colSpanLocationByIndex(col_index, cell.mc.cs);
- }
- return colLocationArr[1] - colLocationArr[0] - 2;
- }
- function computeColWidthByContent(d, c, rh) {
- let currentColLen = 0;
- let rowlenArr = computeRowlenArr(rh, c)
- let canvas = $("#luckysheetTableContent").get(0).getContext("2d");
- canvas.textBaseline = 'top'; //textBaseline以top计算
- for (var i = 0; i < d.length; i++) {
- var cell = d[i][c]
- if (cell != null && (cell.v != null || isInlineStringCell(cell))) {
- let cellHeight = rowlenArr[c];
- let textInfo = getCellTextInfo(cell, canvas, {
- r: i,
- c: c,
- cellHeight: cellHeight
- });
- let computeCollen = 0;
- if (textInfo != null) {
- computeCollen = textInfo.textWidthAll + 2;
- }
- //比较计算高度和当前高度取最大高度
- if (computeCollen > currentColLen) {
- currentColLen = computeCollen;
- }
- }
- }
- return currentColLen;
- }
- //计算表格行高数组
- function computeRowlenArr(rowHeight, cfg) {
- let rowlenArr = [];
- let rh_height = 0;
- for (let i = 0; i < rowHeight; i++) {
- let rowlen = Store.defaultrowlen;
- if (cfg["rowlen"] != null && cfg["rowlen"][i] != null) {
- rowlen = cfg["rowlen"][i];
- }
- if (cfg["rowhidden"] != null && cfg["rowhidden"][i] != null) {
- rowlen = cfg["rowhidden"][i];
- rowlenArr.push(rh_height);
- continue;
- }
- else {
- rh_height += rowlen + 1;
- }
- rowlenArr.push(rh_height);//行的临时长度分布
- }
- return rowlenArr;
- }
- //获取换行单元格截断数组
- function getCellTextSplitArr(strValue, strArr, cellWidth, canvas){
- for(let strI = 1; strI <= strValue.length; strI++){
- let strV = strValue.substring(0, strI);
- let strtextMetrics = getMeasureText(strV, canvas).width;
- if(strtextMetrics > cellWidth){
- if(strI - 1 <= 0){
- return strArr;
- }
- else{
- strArr.push(strValue.substring(0, strI - 1));
- return getCellTextSplitArr(strValue.substring(strI - 1), strArr, cellWidth, canvas);
- }
- }
- else if(strI == strValue.length){
- strArr.push(strV);
- }
- }
- return strArr;
- }
- //获取有值单元格文本大小
- // let measureTextCache = {}, measureTextCacheTimeOut = null;
- function getMeasureText(value, ctx, fontset){
- let mtc = Store.measureTextCache[value + "_" + ctx.font];
- if(fontset!=null){
- mtc = Store.measureTextCache[value + "_" + fontset];
- }
- if(mtc != null){
- return mtc;
- }
- else{
- if(fontset!=null){
- let preFont = ctx.font;
- ctx.font = fontset;
- }
- let measureText = ctx.measureText(value), cache = {};
- // var regu = "^[ ]+$";
- // var re = new RegExp(regu);
- // if(measureText.actualBoundingBoxRight==null || re.test(value)){
- // cache.width = measureText.width;
- // }
- // else{
- // //measureText.actualBoundingBoxLeft +
- // cache.width = measureText.actualBoundingBoxRight;
- // }
- cache.width = measureText.width;
- if(fontset!=null){
- ctx.font = fontset;
- }
- cache.actualBoundingBoxDescent = measureText.actualBoundingBoxDescent;
- cache.actualBoundingBoxAscent = measureText.actualBoundingBoxAscent;
- if(cache.actualBoundingBoxDescent==null || cache.actualBoundingBoxAscent==null || isNaN(cache.actualBoundingBoxDescent) || isNaN(cache.actualBoundingBoxAscent)){
- let commonWord = "M"
- if(hasChinaword(value)){
- commonWord = "田";
- }
- let oneLineTextHeight = menuButton.getTextSize(commonWord, ctx.font)[1]*0.8;
- if(ctx.textBaseline=="top"){
- cache.actualBoundingBoxDescent = oneLineTextHeight;
- cache.actualBoundingBoxAscent = 0;
- }
- else if(ctx.textBaseline=="middle"){
- cache.actualBoundingBoxDescent = oneLineTextHeight/2;
- cache.actualBoundingBoxAscent = oneLineTextHeight/2;
- }
- else{
- cache.actualBoundingBoxDescent = 0;
- cache.actualBoundingBoxAscent = oneLineTextHeight;
- }
- //console.log(value, oneLineTextHeight, measureText.actualBoundingBoxDescent+measureText.actualBoundingBoxAscent,ctx.font);
- }
- if(ctx.textBaseline == 'alphabetic'){
- let descText = "gjpqy", matchText="abcdABCD";
- let descTextMeasure = Store.measureTextCache[descText + "_" + ctx.font];
- if(fontset!=null){
- descTextMeasure = Store.measureTextCache[descText + "_" + fontset];
- }
- let matchTextMeasure = Store.measureTextCache[matchText + "_" + ctx.font];
- if(fontset!=null){
- matchTextMeasure = Store.measureTextCache[matchText + "_" + fontset];
- }
- if(descTextMeasure == null){
- descTextMeasure = ctx.measureText(descText);
- }
- if(matchTextMeasure == null){
- matchTextMeasure = ctx.measureText(matchText);
- }
- if(cache.actualBoundingBoxDescent<=matchTextMeasure.actualBoundingBoxDescent){
- cache.actualBoundingBoxDescent = descTextMeasure.actualBoundingBoxDescent;
- if(cache.actualBoundingBoxDescent==null){
- cache.actualBoundingBoxDescent = 0;
- }
- }
- }
- cache.width *= Store.zoomRatio;
- cache.actualBoundingBoxDescent *= Store.zoomRatio;
- cache.actualBoundingBoxAscent *= Store.zoomRatio;
- Store.measureTextCache[value + "_" + Store.zoomRatio + "_" + ctx.font] = cache;
- // console.log(measureText, value);
- return cache;
- }
- }
- function isSupportBoundingBox(ctx){
- let measureText = ctx.measureText("田");
- if(measureText.actualBoundingBoxAscent==null){
- return false;
- }
- return true;
- }
- //获取单元格文本内容的渲染信息
- // let measureTextCache = {}, measureTextCacheTimeOut = null;
- // option {cellWidth,cellHeight,space_width,space_height}
- function getCellTextInfo(cell , ctx, option){
- let cellWidth = option.cellWidth;
- let cellHeight = option.cellHeight;
- let isMode = "", isModeSplit = "";
- // console.log("initialinfo", cell, option);
- if(cellWidth==null){
- isMode = "onlyWidth";
- isModeSplit = "_";
- }
- let textInfo = Store.measureTextCellInfoCache[option.r + "_" + option.c + isModeSplit + isMode];
- if(textInfo != null){
- return textInfo;
- }
- // let cell = Store.flowdata[r][c];
- let space_width = option.space_width, space_height = option.space_height; //宽高方向 间隙
- if(space_width==null){
- space_width = 2;
- }
- if(space_height==null){
- space_height = 2;
- }
- //水平对齐
- let horizonAlign = checkstatusByCell(cell, "ht");
- //垂直对齐
- let verticalAlign = checkstatusByCell(cell, "vt");
- let tb = checkstatusByCell(cell ,"tb");//wrap overflow
- let tr = checkstatusByCell(cell ,"tr");//rotate
- let rt = checkstatusByCell(cell ,"rt");//rotate angle
- let isRotateUp = 1, isRotateDown=0;
- if(rt==null){
- if(tr=="0"){
- rt = 0;
- }
- else if(tr=="1"){
- rt = 45;
- }
- else if(tr=="4"){
- rt = 90;
- }
- else if(tr=="2"){
- rt = 135;
- }
- else if(tr=="5"){
- rt = 180;
- }
- if(rt==null){
- rt = 0;
- }
- }
- if(rt>180 || rt<0){
- rt = 0;
- }
- rt = parseInt(rt);
- if(rt>90){
- rt = 90 -rt;
- isRotateUp = 0;
- isRotateDown = 1;
- }
- ctx.textAlign="start";
- let textContent = {};
- textContent.values = [];
- let fontset, cancelLine="0", underLine="0", fontSize=11, isInline=false, value, inlineStringArr=[];
- if(isInlineStringCell(cell)){
- 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, fs=shareCell.fs;
- v = v.replace(/\r\n/g, "_x000D_").replace(/ /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=="" && x!=splitArr.length-1){
- inlineStringArr.push({
- fontset:scfontset,
- fc:fc==null?"#000":fc,
- cl:cl==null?0:cl,
- un:un==null?0:un,
- wrap:true,
- fs:fs==null?11:fs,
- });
- 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,
- fs:fs==null?11:fs,
- });
- }
- if(x!=splitArr.length-1 ){
- inlineStringArr.push({
- fontset:scfontset,
- fc:fc==null?"#000":fc,
- cl:cl==null?0:cl,
- un:un==null?0:un,
- wrap:true,
- fs:fs==null?11:fs,
- });
- similarIndex++;
- }
- }
- }
- similarIndex++;
- }
- isInline = true;
- }
- else{
- fontset = luckysheetfontformat(cell);
- ctx.font = fontset;
- cancelLine = checkstatusByCell(cell ,"cl");//cancelLine
- underLine = checkstatusByCell(cell ,"un");//underLine
- fontSize = checkstatusByCell(cell ,"fs");
- if(cell instanceof Object){
- value = cell.m;
- if(value == null){
- value = cell.v;
- }
- }
- else{
- value = cell;
- }
- if(isRealNull(value)){
- return null;
- }
- }
- if(tr=="3"){//vertical text
- ctx.textBaseline = 'top';
- let textW_all = 0; //拆分后宽高度合计
- let textH_all = 0;
- let colIndex=0, textH_all_cache=0, textH_all_Column = {}, textH_all_ColumnHeight=[];
- if(isInline){
- let preShareCell = null;
- for(let i = 0; i < inlineStringArr.length; i++){
- let shareCell = inlineStringArr[i];
- let value = shareCell.v, showValue=shareCell.v;
- 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);
- textH_all_cache = 0;
- colIndex +=1;
- preShareCell = shareCell;
- continue;
- }
- }
- let measureText = getMeasureText(value, ctx, shareCell.fontset);
- 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;
- // textH_all = Math.max(textH_all,textH_all_cache);
- // console.log(">",i,colIndex);
- textH_all_ColumnHeight.push(textH_all_cache-textH);
- textH_all_cache = textH;
- colIndex +=1;
- }
- }
- if(i== inlineStringArr.length-1){
- textH_all_ColumnHeight.push(textH_all_cache);
- }
- if(textH_all_Column[colIndex]==null){
- textH_all_Column[colIndex]= [];
- }
- let item = {
- content:showValue,
- style:shareCell,
- width:textW,
- height:textH,
- left:0,
- top:0,
- colIndex:colIndex,
- asc:measureText.actualBoundingBoxAscent,
- desc:measureText.actualBoundingBoxDescent,
- inline:true,
- }
- 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);
- let textHeight = measureText.actualBoundingBoxDescent + measureText.actualBoundingBoxAscent;
- value = value.toString();
- let vArr = [];
- if(value.length > 1){
- 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;
- // textH_all = Math.max(textH_all,textH_all_cache);
- textH_all_ColumnHeight.push(textH_all_cache-textH);
- textH_all_cache = textH;
- 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,
- width:textW,
- height:textH,
- left:0,
- top:0,
- colIndex:colIndex,
- asc:measureText.actualBoundingBoxAscent,
- desc:measureText.actualBoundingBoxDescent
- });
- }
- }
- let textH_all_ColumWidth = [];
- for(let i = 0; i < textH_all_ColumnHeight.length; i++){
- let columnHeight = textH_all_ColumnHeight[i];
- let col = textH_all_Column[i], colMaxW=0;
- for(let c=0;c<col.length;c++){
- let word = col[c];
- colMaxW = Math.max(colMaxW, word.width);
- }
- textH_all_ColumWidth.push(colMaxW);
- textW_all += colMaxW;
- textH_all = Math.max(textH_all, columnHeight);
- }
- textContent.type = "verticalWrap";
- textContent.textWidthAll = textW_all;
- textContent.textHeightAll = textH_all;
- if(isMode=="onlyWidth"){
- // 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];
- let columnWidth = textH_all_ColumWidth[i];
- let col = textH_all_Column[i];
- 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;
- }
- else if(horizonAlign == "2"){
- left = cellWidth + cumColumnWidth - textW_all + space_width;
- }
- let top = (cellHeight - space_height) + cumWordHeight - columnHeight;
- if(verticalAlign == "0"){
- top = cellHeight / 2 + cumWordHeight - columnHeight/2;
- }
- else if(verticalAlign == "1"){
- top = space_height + cumWordHeight;
- }
- cumWordHeight += word.height;
- word.left = left;
- word.top = top;
- drawLineInfo(word, cancelLine, underLine,{
- width:columnWidth,
- height:word.height,
- left:left,
- top:top+word.height-space_height,
- asc:word.height,
- desc:0,
- fs:fontSize
- });
- textContent.values.push(word);
- }
- cumColumnWidth+=columnWidth;
- }
- }
- else{
- let supportBoundBox = isSupportBoundingBox(ctx);
- if(supportBoundBox){
- ctx.textBaseline = 'alphabetic';
- }
- else{
- ctx.textBaseline = 'bottom';
- }
- if(tb=="2" || isInline){//wrap
- let textW_all = 0; //拆分后宽高度合计
- let textH_all = 0;
- let textW_all_inner = 0;
- // let oneWordWidth = getMeasureText(vArr[0], ctx).width;
- let splitIndex=0, text_all_cache=0, text_all_split = {}, text_all_splitLen=[];
- textContent.rotate = rt;
- rt = Math.abs(rt);
- let anchor = 0, preHeight = 0, preWidth=0, preStr, preTextHeight, preTextWidth, preMeasureText, i=1, wrapStyle={}, spaceOrTwoByte=null, spaceOrTwoByteIndex=null;
- if(isInline){
- while(i <= inlineStringArr.length){
- let shareCells = inlineStringArr.slice(anchor, i);
- 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];
- let item = {
- content:sc.v,
- style:sc,
- width:sc.measureText.width,
- height:sc.measureText.actualBoundingBoxAscent+sc.measureText.actualBoundingBoxDescent,
- left:0,
- top:0,
- splitIndex:splitIndex,
- asc:sc.measureText.actualBoundingBoxAscent,
- desc:sc.measureText.actualBoundingBoxDescent,
- inline:true,
- fs:sc.fs
- }
- // if(rt!=0){//rotate
- // item.textHeight = sc.textHeight;
- // item.textWidth = sc.textWidth;
- // }
- text_all_split[splitIndex].push(item);
- }
- }
- if(shareCells.length==1 || i==inlineStringArr.length){
- let sc = shareCells[0];
- let measureText = getMeasureText("M", ctx, sc.fontset);
- if(text_all_split[splitIndex]==null){
- text_all_split[splitIndex] = [];
- }
- text_all_split[splitIndex].push({
- content:"",
- style:sc,
- width:measureText.width,
- height:measureText.actualBoundingBoxAscent+measureText.actualBoundingBoxDescent,
- left:0,
- top:0,
- splitIndex:splitIndex,
- asc:measureText.actualBoundingBoxAscent,
- desc:measureText.actualBoundingBoxDescent,
- inline:true,
- wrap:true,
- fs:sc.fs
- });
- }
- splitIndex +=1;
- i++;
- continue;
- }
- 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);
- }
- textWidth += sc.measureText.width;
- 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;
- let lastWord = shareCells[shareCells.length-1];
- if(lastWord.v==" " || checkWordByteLength(lastWord.v)==2){
- spaceOrTwoByteIndex = i;
- }
- if(rt!=0){//rotate
- // console.log("all",anchor, i , str);
- console.log(height,space_height, cellHeight, shareCells,(height+space_height)>cellHeight);
- if((height+space_height)>cellHeight && text_all_split[splitIndex]!=null && tb=="2" && i!= inlineStringArr.length){
- // 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({
- content:sc.v,
- style:sc,
- width:sc.measureText.width,
- height:sc.measureText.actualBoundingBoxAscent+sc.measureText.actualBoundingBoxDescent,
- left:0,
- top:0,
- splitIndex:splitIndex,
- asc:sc.measureText.actualBoundingBoxAscent,
- desc:sc.measureText.actualBoundingBoxDescent,
- inline:true,
- fs:sc.fs
- });
- }
- anchor = spaceOrTwoByteIndex;
- i = spaceOrTwoByteIndex + 1;
- 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({
- content:sc.v,
- style:sc,
- width:sc.measureText.width,
- height:sc.measureText.actualBoundingBoxAscent+sc.measureText.actualBoundingBoxDescent,
- left:0,
- top:0,
- splitIndex:splitIndex,
- asc:sc.measureText.actualBoundingBoxAscent,
- desc:sc.measureText.actualBoundingBoxDescent,
- inline:true,
- fs:sc.fs
- });
- }
- 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.measureText.width,
- height:sc.measureText.actualBoundingBoxAscent+sc.measureText.actualBoundingBoxDescent,
- left:0,
- top:0,
- splitIndex:splitIndex,
- asc:sc.measureText.actualBoundingBoxAscent,
- desc:sc.measureText.actualBoundingBoxDescent,
- inline:true,
- fs:sc.fs
- });
- }
- 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 && 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({
- content:sc.v,
- style:sc,
- width:sc.measureText.width,
- height:sc.measureText.actualBoundingBoxAscent+sc.measureText.actualBoundingBoxDescent,
- left:0,
- top:0,
- splitIndex:splitIndex,
- asc:sc.measureText.actualBoundingBoxAscent,
- desc:sc.measureText.actualBoundingBoxDescent,
- inline:true,
- fs:sc.fs
- });
- }
- anchor = spaceOrTwoByteIndex;
- i = spaceOrTwoByteIndex + 1;
- 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({
- content:sc.v,
- style:sc,
- width:sc.measureText.width,
- height:sc.measureText.actualBoundingBoxAscent+sc.measureText.actualBoundingBoxDescent,
- left:0,
- top:0,
- splitIndex:splitIndex,
- asc:sc.measureText.actualBoundingBoxAscent,
- desc:sc.measureText.actualBoundingBoxDescent,
- inline:true,
- fs:sc.fs
- });
- }
- 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.measureText.width,
- height:sc.measureText.actualBoundingBoxAscent+sc.measureText.actualBoundingBoxDescent,
- left:0,
- top:0,
- splitIndex:splitIndex,
- asc:sc.measureText.actualBoundingBoxAscent,
- desc:sc.measureText.actualBoundingBoxDescent,
- inline:true,
- fs:sc.fs
- });
- }
- break;
- }
- else{
- if(text_all_split[splitIndex]==null){
- text_all_split[splitIndex]= [];
- }
- i++;
- }
- }
- }
- }
- 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
- let lastWord = str.substr(str.length-1,1);
- if(lastWord==" " || checkWordByteLength(lastWord)==2){
- if(preMeasureText!=null){
- spaceOrTwoByte = {
- index:i,
- str:preStr + lastWord,
- 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){
- anchor = spaceOrTwoByte.index;
- 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,
- });
- // 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,
- 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({
- 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;
- 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;
- spaceOrTwoByte = null;
- }
- else{
- 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);
- 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;
- preTextHeight = textHeight;
- preTextWidth = textWidth;
- preMeasureText = measureText;
- }
- // console.log(text_all_split)
- }
- let split_all_size = [], oneLinemaxWordCount=0;
- // console.log("split",splitIndex, text_all_split);
- let splitLen = Object.keys(text_all_split).length;
- for(let i = 0; i < splitLen; i++){
- let splitLists = text_all_split[i];
- if(splitLists==null){
- continue;
- }
- let sWidth = 0, sHeight=0, maxDesc=0,maxAsc=0, lineHeight=0, maxWordCount=0;
- for(let s=0;s<splitLists.length;s++){
- let sp = splitLists[s];
- if(rt!=0){//rotate
- sWidth += sp.width;
- sHeight = Math.max(sHeight, sp.height-(supportBoundBox?sp.desc:0));
- }
- else{//plain
- sWidth += sp.width;
- sHeight = Math.max(sHeight, sp.height-(supportBoundBox?sp.desc:0));
- }
- maxDesc = Math.max(maxDesc,(supportBoundBox?sp.desc:0));
- maxAsc = Math.max(maxAsc, sp.asc);
- maxWordCount++;
- }
- lineHeight = sHeight/2;
- oneLinemaxWordCount = Math.max(oneLinemaxWordCount, maxWordCount);
- if(rt!=0){//rotate
- sHeight+=lineHeight;
- textW_all_inner = Math.max(textW_all_inner, sWidth);
- // textW_all = Math.max(textW_all, sWidth+ (textH_all)/Math.tan(rt*Math.PI/180));
- textH_all += sHeight;
- }
- else{//plain
- // console.log("textH_all",textW_all, textH_all);
- sHeight+=lineHeight;
- textW_all=Math.max(textW_all, sWidth);
- textH_all+=sHeight;
- }
- split_all_size.push({
- width:sWidth,
- height:sHeight,
- desc:maxDesc,
- asc:maxAsc,
- lineHeight:lineHeight,
- wordCount: maxWordCount
- });
- }
- // 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 lastLineSpaceHeight = lastLine.lineHeight;
- textH_all = textH_all - lastLineSpaceHeight + lastLine.desc;
- let rw = (textH_all)/Math.sin(rtPI) + textW_all_inner*Math.cos(rtPI);
- let rh = textW_all_inner*Math.sin(rtPI), fixOneLineLeft = 0;
- if(rt!=0){
- if(splitLen==1){
- textW_all = textW_all_inner + 2*(textH_all/Math.tan(rtPI));
- fixOneLineLeft = textH_all/Math.tan(rtPI);
- }
- else{
- textW_all = textW_all_inner + textH_all/Math.tan(rtPI);
- }
- textContent.textWidthAll = rw;
- textContent.textHeightAll = rh;
- }
- else{
- textContent.textWidthAll = textW_all;
- textContent.textHeightAll = textH_all;
- }
- if(isMode=="onlyWidth"){
- // console.log("plainWrap", textContent,cell, option);
- return textContent;
- }
- if(rt!=0 && isRotateUp=="1"){
- ctx.textAlign="end";
- for(let i = 0; i < splitLen; i++){
- let splitLists = text_all_split[i];
- if(splitLists==null){
- continue;
- }
- let size = split_all_size[i];
- cumColumnWidth = 0;
- for(let c=splitLists.length-1;c>=0;c--){
- let wordGroup = splitLists[c];
- let left, top;
- if(rt!=0){//rotate
- let x, y = cumWordHeight+size.asc;
- x = (cumWordHeight)/Math.tan(rtPI) - cumColumnWidth + textW_all_inner;
- 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;
- }
- else if(verticalAlign == "1"){//top
- left = x + cellWidth/2 - textW_all/2;
- top = y - (textH_all/2 - rh/2);
- }
- else if(verticalAlign == "2"){//bottom
- left = x + cellWidth/2 - (textW_all/2)+lastLineSpaceHeight*Math.cos(rtPI);
- top = y + cellHeight - rh/2 - textH_all/2 - lastLineSpaceHeight*Math.cos(rtPI);
- }
- }
- else if(horizonAlign == "1"){//left
- if(verticalAlign == "0"){//mid
- left = x - rh*Math.sin(rtPI)/2 + lastLineSpaceHeight*Math.cos(rtPI)/2;
- top = y + cellHeight/2 + rh*Math.cos(rtPI)/2 - lastLineSpaceHeight*Math.cos(rtPI)/2;
- }
- else if(verticalAlign == "1"){//top
- left = x - rh*Math.sin(rtPI);
- top = y + rh*Math.cos(rtPI);
- }
- else if(verticalAlign == "2"){//bottom
- left = x + lastLineSpaceHeight*Math.cos(rtPI);
- top = y + cellHeight - lastLineSpaceHeight*Math.cos(rtPI);
- }
- }
- else if(horizonAlign == "2"){//right
- if(verticalAlign == "0"){//mid
- left = x + cellWidth - rw/2 - (textW_all_inner/2+(textH_all/2)/Math.tan(rtPI))+ lastLineSpaceHeight*Math.cos(rtPI)/2;
- top = y + cellHeight/2 - textH_all/2 - lastLineSpaceHeight*Math.cos(rtPI)/2;
- }
- else if(verticalAlign == "1"){//top fixOneLineLeft
- left = x + cellWidth - textW_all + fixOneLineLeft;
- top = y - textH_all;
- }
- else if(verticalAlign == "2"){//bottom
- left = x + cellWidth - rw*Math.cos(rtPI) + lastLineSpaceHeight*Math.cos(rtPI);
- 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,
- left:left-wordGroup.width,
- top:top,
- asc:size.asc,
- desc:size.desc,
- fs:wordGroup.fs
- });
- textContent.values.push(wordGroup);
- cumColumnWidth += wordGroup.width;
- }
- cumWordHeight += size.height;
- }
- }
- else{
- for(let i = 0; i < splitLen; i++){
- let splitLists = text_all_split[i];
- if(splitLists==null){
- continue;
- }
- let size = split_all_size[i];
- cumColumnWidth = 0;
- for(let c=0;c<splitLists.length;c++){
- let wordGroup = splitLists[c];
- let left, top;
- if(rt!=0){//rotate
- let x, y = cumWordHeight+size.asc;
- x = (textH_all-cumWordHeight)/Math.tan(rtPI) + cumColumnWidth;
- 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;
- }
- else if(verticalAlign == "1"){//top
- left = x + cellWidth/2 - textW_all/2 - lastLineSpaceHeight*Math.cos(rtPI)/2;
- top = y - (textH_all/2 - rh/2)+lastLineSpaceHeight*Math.cos(rtPI)/2;
- }
- else if(verticalAlign == "2"){//bottom
- left = x + cellWidth/2 - (textW_all/2)-lastLineSpaceHeight*Math.cos(rtPI);
- top = y + cellHeight - rh/2 - textH_all/2 - lastLineSpaceHeight*Math.cos(rtPI);
- }
- }
- else if(horizonAlign == "1"){//left
- if(verticalAlign == "0"){//mid
- left = x - rh*Math.sin(rtPI)/2 - lastLineSpaceHeight*Math.cos(rtPI)/2;
- top = y - textH_all + cellHeight/2 - rh*Math.cos(rtPI)/2 - lastLineSpaceHeight*Math.cos(rtPI)/2;
- }
- else if(verticalAlign == "1"){//top
- left = x;
- top = y - textH_all;
- }
- else if(verticalAlign == "2"){//bottom
- left = x - rh*Math.sin(rtPI) - lastLineSpaceHeight*Math.cos(rtPI);
- top = y - textH_all + cellHeight - rh*Math.cos(rtPI) - lastLineSpaceHeight*Math.cos(rtPI);
- }
- }
- else if(horizonAlign == "2"){//right
- if(verticalAlign == "0"){//mid
- left = x + cellWidth - rw/2 - textW_all/2 - lastLineSpaceHeight*Math.cos(rtPI)/2;
- top = y + cellHeight/2 - textH_all/2 - lastLineSpaceHeight*Math.cos(rtPI)/2;
- }
- else if(verticalAlign == "1"){//top fixOneLineLeft
- left = x + cellWidth - rw*Math.cos(rtPI);
- top = y + rh*Math.cos(rtPI);
- }
- else if(verticalAlign == "2"){//bottom
- left = x + cellWidth - textW_all - lastLineSpaceHeight*Math.cos(rtPI) + fixOneLineLeft;
- top = y + cellHeight - lastLineSpaceHeight*Math.cos(rtPI);
- }
- }
- drawLineInfo(wordGroup, cancelLine, underLine,{
- width:wordGroup.width,
- height:wordGroup.height,
- left:left,
- top:top,
- asc:size.asc,
- desc:size.desc,
- fs:wordGroup.fs
- });
- }
- else{//plain
- left = space_width + cumColumnWidth;
- if(horizonAlign == "0"){
- //+ space_width*textH_all_ColumnHeight.length
- left = cellWidth / 2 + cumColumnWidth - size.width/2;
- }
- 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;
- }
- else if(verticalAlign == "1"){
- top = space_height + cumWordHeight+ size.asc;
- }
- drawLineInfo(wordGroup, cancelLine, underLine,{
- width:wordGroup.width,
- height:wordGroup.height,
- left:left,
- top:top,
- asc:size.asc,
- desc:size.desc,
- fs:wordGroup.fs
- });
- }
- wordGroup.left = left;
- wordGroup.top = top;
- textContent.values.push(wordGroup);
- cumColumnWidth += wordGroup.width;
- }
- cumWordHeight += size.height;
- }
- }
- textContent.type = "plainWrap";
- if(rt!=0){
- // let leftCenter = (textW_all + textH_all/Math.tan(rt*Math.PI/180))/2;
- // let topCenter = textH_all/2;
- // if(isRotateUp=="1"){
- // textContent.textLeftAll += leftCenter;
- // textContent.textTopAll += topCenter;
- // }
- // else {
- // textContent.textLeftAll += leftCenter;
- // textContent.textTopAll -= topCenter;
- // }
- if(horizonAlign == "0"){//center
- if(verticalAlign == "0"){//mid
- textContent.textLeftAll = cellWidth/2;
- textContent.textTopAll = cellHeight/2;
- }
- else if(verticalAlign == "1"){//top
- textContent.textLeftAll = cellWidth/2;
- textContent.textTopAll = rh/2;
- }
- else if(verticalAlign == "2"){//bottom
- textContent.textLeftAll = cellWidth/2;
- textContent.textTopAll = cellHeight - rh/2;
- }
- }
- else if(horizonAlign == "1"){//left
- if(verticalAlign == "0"){//mid
- textContent.textLeftAll = 0;
- textContent.textTopAll = cellHeight/2;
- }
- else if(verticalAlign == "1"){//top
- textContent.textLeftAll = 0;
- textContent.textTopAll = 0;
- }
- else if(verticalAlign == "2"){//bottom
- textContent.textLeftAll = 0;
- textContent.textTopAll = cellHeight;
- }
- }
- else if(horizonAlign == "2"){//right
- if(verticalAlign == "0"){//mid
- textContent.textLeftAll = cellWidth - rw/2;
- textContent.textTopAll = cellHeight/2;
- }
- else if(verticalAlign == "1"){//top
- textContent.textLeftAll = cellWidth;
- textContent.textTopAll = 0;
- }
- else if(verticalAlign == "2"){//bottom
- textContent.textLeftAll = cellWidth;
- textContent.textTopAll = cellHeight;
- }
- }
- }
- // else{
- // textContent.textWidthAll = textW_all;
- // textContent.textHeightAll = textH_all;
- // }
- }
- else{
- let measureText = getMeasureText(value, ctx);
- let textWidth = measureText.width;
- let textHeight = measureText.actualBoundingBoxDescent + measureText.actualBoundingBoxAscent;
- textContent.rotate = rt;
- rt = Math.abs(rt);
- let rtPI = rt*Math.PI/180;
- let textWidthAll = textWidth * Math.cos(rtPI) + textHeight * Math.sin(rtPI);//consider text box wdith and line height
- let textHeightAll = textWidth * Math.sin(rtPI) + textHeight * Math.cos(rtPI);//consider text box wdith and line height
- if(rt!=0){
- textContent.textHeightAll = textHeightAll;
- }
- else{
- 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);
- return textContent;
- }
- let width = textWidthAll, height = textHeightAll;
- let left = space_width + textHeight * Math.sin(rtPI)*isRotateUp; //默认为1,左对齐
- if(horizonAlign == "0"){ //居中对齐
- left = cellWidth / 2 - (width / 2) + textHeight * Math.sin(rtPI)*isRotateUp;
- }
- 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"){ //居中对齐
- top = cellHeight / 2 - (height / 2) + measureText.actualBoundingBoxAscent* Math.cos(rtPI) + textWidth * Math.sin(rtPI)*isRotateUp;
- }
- else if(verticalAlign == "1"){ //上对齐
- top = space_height + measureText.actualBoundingBoxAscent* Math.cos(rtPI) + textWidth * Math.sin(rtPI)*isRotateUp;
- }
- textContent.type = "plain";
- let wordGroup = {
- content:value,
- style:fontset,
- width:width,
- height:height,
- left:left,
- top:top,
- }
- drawLineInfo(wordGroup, cancelLine, underLine,{
- width:textWidth,
- height:textHeight,
- left:left,
- top:top,
- asc:measureText.actualBoundingBoxAscent,
- desc:measureText.actualBoundingBoxDescent,
- fs:fontSize,
- });
- textContent.values.push(wordGroup);
- textContent.textLeftAll = left;
- textContent.textTopAll = top;
- textContent.asc = measureText.actualBoundingBoxAscent;
- textContent.desc = measureText.actualBoundingBoxDescent;
- // console.log("plain",left,top);
- }
- }
- return textContent;
- }
- 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,fs = option.fs;
- if(wordGroup.wrap===true){
- return;
- }
- if(wordGroup.inline==true && wordGroup.style!=null){
- cancelLine = wordGroup.style.cl;
- underLine = wordGroup.style.un;
- }
- if(cancelLine!="0"){
- wordGroup.cancelLine = {};
- wordGroup.cancelLine.startX = left;
- wordGroup.cancelLine.startY = top-asc/2+1;
- wordGroup.cancelLine.endX = left + width;
- wordGroup.cancelLine.endY = top-asc/2+1;
- wordGroup.cancelLine.fs = fs;
- }
- if(underLine!="0"){
- wordGroup.underLine = [];
- if(underLine=="1" || underLine=="2"){
- let item = {};
- item.startX = left;
- item.startY = top + 3;
- item.endX = left + width;
- item.endY = top + 3;
- item.fs = fs;
- wordGroup.underLine.push(item);
- }
- if(underLine=="2"){
- let item = {};
- item.startX = left;
- item.startY = top+desc;
- item.endX = left + width;
- item.endY = top+desc;
- item.fs = fs;
- wordGroup.underLine.push(item);
- }
- if(underLine=="3" || underLine=="4"){
- let item = {};
- item.startX = left;
- item.startY = top+desc;
- item.endX = left + width;
- item.endY = top+desc;
- item.fs = fs;
- wordGroup.underLine.push(item);
- }
- if(underLine=="4"){
- let item = {};
- item.startX = left;
- item.startY = top+desc+2;
- item.endX = left + width;
- item.endY = top+desc+2;
- item.fs = fs;
- wordGroup.underLine.push(item);
- }
- }
- }
- export {
- computeColWidthByContent,
- rowlenByRange,
- computeRowlenByContent,
- computeRowlenArr,
- getCellTextSplitArr,
- getMeasureText,
- getCellTextInfo
- }
|