getRowlen.js 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731
  1. import {luckysheetfontformat} from '../utils/util';
  2. import menuButton from '../controllers/menuButton';
  3. import {checkstatusByCell} from './getdata';
  4. import {colLocationByIndex,colSpanLocationByIndex} from './location';
  5. import {checkWordByteLength, hasChinaword, isRealNull} from './validate';
  6. import {isInlineStringCell} from '../controllers/inlineString';
  7. import Store from '../store';
  8. /**
  9. * 计算范围行高
  10. *
  11. * @param d 原始数据
  12. * @param r1 起始行
  13. * @param r2 截至行
  14. * @param cfg 配置
  15. * @returns 计算后的配置
  16. */
  17. function rowlenByRange(d, r1, r2, cfg) {
  18. let cfg_clone = $.extend(true, {}, cfg);
  19. if(cfg_clone["rowlen"] == null){
  20. cfg_clone["rowlen"] = {};
  21. }
  22. if(cfg_clone["customHeight"] == null){
  23. cfg_clone["customHeight"] = {};
  24. }
  25. let canvas = $("#luckysheetTableContent").get(0).getContext("2d");
  26. canvas.textBaseline = 'top'; //textBaseline以top计算
  27. for(let r = r1; r <= r2; r++){
  28. if (cfg_clone["rowhidden"] != null && cfg_clone["rowhidden"][r] != null) {
  29. continue;
  30. }
  31. let currentRowLen = Store.defaultrowlen;
  32. //如果该行设置过高度,则不自定计算高度。设置的高度优先级大于自动计算高度
  33. if(cfg_clone["customHeight"][r]==1){
  34. continue;
  35. }
  36. delete cfg_clone["rowlen"][r];
  37. for(let c = 0; c < d[r].length; c++){
  38. let cell = d[r][c];
  39. if(cell == null){
  40. continue;
  41. }
  42. if(cell != null && (cell.v != null || isInlineStringCell(cell)) ){
  43. let cellWidth;
  44. if(cell.mc){
  45. if(c === cell.mc.c){
  46. let st_cellWidth = colLocationByIndex(c)[0];
  47. let ed_cellWidth = colLocationByIndex(cell.mc.c + cell.mc.cs - 1)[1];
  48. cellWidth = ed_cellWidth - st_cellWidth - 2;
  49. }else{
  50. continue;
  51. }
  52. } else {
  53. cellWidth = colLocationByIndex(c)[1] - colLocationByIndex(c)[0] - 2;
  54. }
  55. let textInfo = getCellTextInfo(cell, canvas,{
  56. r:r,
  57. c:c,
  58. cellWidth:cellWidth
  59. });
  60. let computeRowlen = 0;
  61. if(textInfo!=null){
  62. computeRowlen = textInfo.textHeightAll+2;
  63. }
  64. //比较计算高度和当前高度取最大高度
  65. if(computeRowlen > currentRowLen){
  66. currentRowLen = computeRowlen;
  67. }
  68. }
  69. }
  70. currentRowLen = currentRowLen/Store.zoomRatio;
  71. if(currentRowLen != Store.defaultrowlen){
  72. cfg_clone["rowlen"][r] = currentRowLen;
  73. }else{
  74. if(cfg["rowlen"]?.[r]){
  75. cfg_clone["rowlen"][r] = cfg["rowlen"][r]
  76. }
  77. }
  78. }
  79. return cfg_clone;
  80. }
  81. //根据内容计算行高
  82. function computeRowlenByContent(d, r) {
  83. let currentRowLen = 0;
  84. let canvas = $("#luckysheetTableContent").get(0).getContext("2d");
  85. canvas.textBaseline = 'top'; //textBaseline以top计算
  86. for(let c = 0; c < d[r].length; c++){
  87. let cell = d[r][c];
  88. if (cell == null) {
  89. continue;
  90. }
  91. if (cell.mc != null) {
  92. if (1 !== cell.mc.rs) {
  93. continue;
  94. }
  95. }
  96. if(Store.config["colhidden"] != null && Store.config["colhidden"][c] != null){
  97. continue;
  98. }
  99. if(cell != null && (cell.v != null || isInlineStringCell(cell)) ){
  100. let cellWidth = computeCellWidth(cell, c);
  101. let textInfo = getCellTextInfo(cell, canvas,{
  102. r:r,
  103. c:c,
  104. cellWidth:cellWidth
  105. });
  106. let computeRowlen = 0;
  107. if (textInfo != null) {
  108. computeRowlen = textInfo.textHeightAll + 2;
  109. }
  110. //比较计算高度和当前高度取最大高度
  111. if (computeRowlen > currentRowLen) {
  112. currentRowLen = computeRowlen;
  113. }
  114. }
  115. }
  116. return currentRowLen;
  117. }
  118. function computeCellWidth(cell, col_index) {
  119. let colLocationArr = colLocationByIndex(col_index);
  120. if (cell.mc && cell.mc.c !== cell.mc.cs) {
  121. colLocationArr = colSpanLocationByIndex(col_index, cell.mc.cs);
  122. }
  123. return colLocationArr[1] - colLocationArr[0] - 2;
  124. }
  125. function computeColWidthByContent(d, c, rh) {
  126. let currentColLen = 0;
  127. let rowlenArr = computeRowlenArr(rh, c)
  128. let canvas = $("#luckysheetTableContent").get(0).getContext("2d");
  129. canvas.textBaseline = 'top'; //textBaseline以top计算
  130. for (var i = 0; i < d.length; i++) {
  131. var cell = d[i][c]
  132. if (cell != null && (cell.v != null || isInlineStringCell(cell))) {
  133. let cellHeight = rowlenArr[c];
  134. let textInfo = getCellTextInfo(cell, canvas, {
  135. r: i,
  136. c: c,
  137. cellHeight: cellHeight
  138. });
  139. let computeCollen = 0;
  140. if (textInfo != null) {
  141. computeCollen = textInfo.textWidthAll + 2;
  142. }
  143. //比较计算高度和当前高度取最大高度
  144. if (computeCollen > currentColLen) {
  145. currentColLen = computeCollen;
  146. }
  147. }
  148. }
  149. return currentColLen;
  150. }
  151. //计算表格行高数组
  152. function computeRowlenArr(rowHeight, cfg) {
  153. let rowlenArr = [];
  154. let rh_height = 0;
  155. for (let i = 0; i < rowHeight; i++) {
  156. let rowlen = Store.defaultrowlen;
  157. if (cfg["rowlen"] != null && cfg["rowlen"][i] != null) {
  158. rowlen = cfg["rowlen"][i];
  159. }
  160. if (cfg["rowhidden"] != null && cfg["rowhidden"][i] != null) {
  161. rowlen = cfg["rowhidden"][i];
  162. rowlenArr.push(rh_height);
  163. continue;
  164. }
  165. else {
  166. rh_height += rowlen + 1;
  167. }
  168. rowlenArr.push(rh_height);//行的临时长度分布
  169. }
  170. return rowlenArr;
  171. }
  172. //获取换行单元格截断数组
  173. function getCellTextSplitArr(strValue, strArr, cellWidth, canvas){
  174. for(let strI = 1; strI <= strValue.length; strI++){
  175. let strV = strValue.substring(0, strI);
  176. let strtextMetrics = getMeasureText(strV, canvas).width;
  177. if(strtextMetrics > cellWidth){
  178. if(strI - 1 <= 0){
  179. return strArr;
  180. }
  181. else{
  182. strArr.push(strValue.substring(0, strI - 1));
  183. return getCellTextSplitArr(strValue.substring(strI - 1), strArr, cellWidth, canvas);
  184. }
  185. }
  186. else if(strI == strValue.length){
  187. strArr.push(strV);
  188. }
  189. }
  190. return strArr;
  191. }
  192. //获取有值单元格文本大小
  193. // let measureTextCache = {}, measureTextCacheTimeOut = null;
  194. function getMeasureText(value, ctx, fontset){
  195. let mtc = Store.measureTextCache[value + "_" + ctx.font];
  196. if(fontset!=null){
  197. mtc = Store.measureTextCache[value + "_" + fontset];
  198. }
  199. if(mtc != null){
  200. return mtc;
  201. }
  202. else{
  203. if(fontset!=null){
  204. let preFont = ctx.font;
  205. ctx.font = fontset;
  206. }
  207. let measureText = ctx.measureText(value), cache = {};
  208. // var regu = "^[ ]+$";
  209. // var re = new RegExp(regu);
  210. // if(measureText.actualBoundingBoxRight==null || re.test(value)){
  211. // cache.width = measureText.width;
  212. // }
  213. // else{
  214. // //measureText.actualBoundingBoxLeft +
  215. // cache.width = measureText.actualBoundingBoxRight;
  216. // }
  217. cache.width = measureText.width;
  218. if(fontset!=null){
  219. ctx.font = fontset;
  220. }
  221. cache.actualBoundingBoxDescent = measureText.actualBoundingBoxDescent;
  222. cache.actualBoundingBoxAscent = measureText.actualBoundingBoxAscent;
  223. if(cache.actualBoundingBoxDescent==null || cache.actualBoundingBoxAscent==null || isNaN(cache.actualBoundingBoxDescent) || isNaN(cache.actualBoundingBoxAscent)){
  224. let commonWord = "M"
  225. if(hasChinaword(value)){
  226. commonWord = "田";
  227. }
  228. let oneLineTextHeight = menuButton.getTextSize(commonWord, ctx.font)[1]*0.8;
  229. if(ctx.textBaseline=="top"){
  230. cache.actualBoundingBoxDescent = oneLineTextHeight;
  231. cache.actualBoundingBoxAscent = 0;
  232. }
  233. else if(ctx.textBaseline=="middle"){
  234. cache.actualBoundingBoxDescent = oneLineTextHeight/2;
  235. cache.actualBoundingBoxAscent = oneLineTextHeight/2;
  236. }
  237. else{
  238. cache.actualBoundingBoxDescent = 0;
  239. cache.actualBoundingBoxAscent = oneLineTextHeight;
  240. }
  241. //console.log(value, oneLineTextHeight, measureText.actualBoundingBoxDescent+measureText.actualBoundingBoxAscent,ctx.font);
  242. }
  243. if(ctx.textBaseline == 'alphabetic'){
  244. let descText = "gjpqy", matchText="abcdABCD";
  245. let descTextMeasure = Store.measureTextCache[descText + "_" + ctx.font];
  246. if(fontset!=null){
  247. descTextMeasure = Store.measureTextCache[descText + "_" + fontset];
  248. }
  249. let matchTextMeasure = Store.measureTextCache[matchText + "_" + ctx.font];
  250. if(fontset!=null){
  251. matchTextMeasure = Store.measureTextCache[matchText + "_" + fontset];
  252. }
  253. if(descTextMeasure == null){
  254. descTextMeasure = ctx.measureText(descText);
  255. }
  256. if(matchTextMeasure == null){
  257. matchTextMeasure = ctx.measureText(matchText);
  258. }
  259. if(cache.actualBoundingBoxDescent<=matchTextMeasure.actualBoundingBoxDescent){
  260. cache.actualBoundingBoxDescent = descTextMeasure.actualBoundingBoxDescent;
  261. if(cache.actualBoundingBoxDescent==null){
  262. cache.actualBoundingBoxDescent = 0;
  263. }
  264. }
  265. }
  266. cache.width *= Store.zoomRatio;
  267. cache.actualBoundingBoxDescent *= Store.zoomRatio;
  268. cache.actualBoundingBoxAscent *= Store.zoomRatio;
  269. Store.measureTextCache[value + "_" + Store.zoomRatio + "_" + ctx.font] = cache;
  270. // console.log(measureText, value);
  271. return cache;
  272. }
  273. }
  274. function isSupportBoundingBox(ctx){
  275. let measureText = ctx.measureText("田");
  276. if(measureText.actualBoundingBoxAscent==null){
  277. return false;
  278. }
  279. return true;
  280. }
  281. //获取单元格文本内容的渲染信息
  282. // let measureTextCache = {}, measureTextCacheTimeOut = null;
  283. // option {cellWidth,cellHeight,space_width,space_height}
  284. function getCellTextInfo(cell , ctx, option){
  285. let cellWidth = option.cellWidth;
  286. let cellHeight = option.cellHeight;
  287. let isMode = "", isModeSplit = "";
  288. // console.log("initialinfo", cell, option);
  289. if(cellWidth==null){
  290. isMode = "onlyWidth";
  291. isModeSplit = "_";
  292. }
  293. let textInfo = Store.measureTextCellInfoCache[option.r + "_" + option.c + isModeSplit + isMode];
  294. if(textInfo != null){
  295. return textInfo;
  296. }
  297. // let cell = Store.flowdata[r][c];
  298. let space_width = option.space_width, space_height = option.space_height; //宽高方向 间隙
  299. if(space_width==null){
  300. space_width = 2;
  301. }
  302. if(space_height==null){
  303. space_height = 2;
  304. }
  305. //水平对齐
  306. let horizonAlign = checkstatusByCell(cell, "ht");
  307. //垂直对齐
  308. let verticalAlign = checkstatusByCell(cell, "vt");
  309. let tb = checkstatusByCell(cell ,"tb");//wrap overflow
  310. let tr = checkstatusByCell(cell ,"tr");//rotate
  311. let rt = checkstatusByCell(cell ,"rt");//rotate angle
  312. let isRotateUp = 1, isRotateDown=0;
  313. if(rt==null){
  314. if(tr=="0"){
  315. rt = 0;
  316. }
  317. else if(tr=="1"){
  318. rt = 45;
  319. }
  320. else if(tr=="4"){
  321. rt = 90;
  322. }
  323. else if(tr=="2"){
  324. rt = 135;
  325. }
  326. else if(tr=="5"){
  327. rt = 180;
  328. }
  329. if(rt==null){
  330. rt = 0;
  331. }
  332. }
  333. if(rt>180 || rt<0){
  334. rt = 0;
  335. }
  336. rt = parseInt(rt);
  337. if(rt>90){
  338. rt = 90 -rt;
  339. isRotateUp = 0;
  340. isRotateDown = 1;
  341. }
  342. ctx.textAlign="start";
  343. let textContent = {};
  344. textContent.values = [];
  345. let fontset, cancelLine="0", underLine="0", fontSize=11, isInline=false, value, inlineStringArr=[];
  346. if(isInlineStringCell(cell)){
  347. let sharedStrings = cell.ct.s, similarIndex = 0;
  348. for(let i=0;i<sharedStrings.length;i++){
  349. let shareCell = sharedStrings[i];
  350. let scfontset = luckysheetfontformat(shareCell);
  351. let fc = shareCell.fc, cl=shareCell.cl,un = shareCell.un, v = shareCell.v, fs=shareCell.fs;
  352. v = v.replace(/\r\n/g, "_x000D_").replace(/&#13;&#10;/g, "_x000D_").replace(/\r/g, "_x000D_").replace(/\n/g, "_x000D_");
  353. let splitArr = v.split("_x000D_");
  354. for(let x=0;x<splitArr.length;x++){
  355. let newValue = splitArr[x];
  356. if(newValue=="" && x!=splitArr.length-1){
  357. inlineStringArr.push({
  358. fontset:scfontset,
  359. fc:fc==null?"#000":fc,
  360. cl:cl==null?0:cl,
  361. un:un==null?0:un,
  362. wrap:true,
  363. fs:fs==null?11:fs,
  364. });
  365. similarIndex++;
  366. }
  367. else{
  368. let newValueArray = newValue.split("");
  369. for(let n=0;n<newValueArray.length;n++){
  370. let nv = newValueArray[n];
  371. inlineStringArr.push({
  372. fontset:scfontset,
  373. fc:fc==null?"#000":fc,
  374. cl:cl==null?0:cl,
  375. un:un==null?0:un,
  376. v: nv,
  377. si:similarIndex,
  378. fs:fs==null?11:fs,
  379. });
  380. }
  381. if(x!=splitArr.length-1 ){
  382. inlineStringArr.push({
  383. fontset:scfontset,
  384. fc:fc==null?"#000":fc,
  385. cl:cl==null?0:cl,
  386. un:un==null?0:un,
  387. wrap:true,
  388. fs:fs==null?11:fs,
  389. });
  390. similarIndex++;
  391. }
  392. }
  393. }
  394. similarIndex++;
  395. }
  396. isInline = true;
  397. }
  398. else{
  399. fontset = luckysheetfontformat(cell);
  400. ctx.font = fontset;
  401. cancelLine = checkstatusByCell(cell ,"cl");//cancelLine
  402. underLine = checkstatusByCell(cell ,"un");//underLine
  403. fontSize = checkstatusByCell(cell ,"fs");
  404. if(cell instanceof Object){
  405. value = cell.m;
  406. if(value == null){
  407. value = cell.v;
  408. }
  409. }
  410. else{
  411. value = cell;
  412. }
  413. if(isRealNull(value)){
  414. return null;
  415. }
  416. }
  417. if(tr=="3"){//vertical text
  418. ctx.textBaseline = 'top';
  419. let textW_all = 0; //拆分后宽高度合计
  420. let textH_all = 0;
  421. let colIndex=0, textH_all_cache=0, textH_all_Column = {}, textH_all_ColumnHeight=[];
  422. if(isInline){
  423. let preShareCell = null;
  424. for(let i = 0; i < inlineStringArr.length; i++){
  425. let shareCell = inlineStringArr[i];
  426. let value = shareCell.v, showValue=shareCell.v;
  427. if(shareCell.wrap===true){
  428. value = "M";
  429. showValue = "";
  430. if( preShareCell!=null && preShareCell.wrap!==true && (i<inlineStringArr.length-1)){
  431. // console.log("wrap",i,colIndex,preShareCell.wrap);
  432. textH_all_ColumnHeight.push(textH_all_cache);
  433. textH_all_cache = 0;
  434. colIndex +=1;
  435. preShareCell = shareCell;
  436. continue;
  437. }
  438. }
  439. let measureText = getMeasureText(value, ctx, shareCell.fontset);
  440. let textW = measureText.width + space_width;
  441. let textH = measureText.actualBoundingBoxAscent + measureText.actualBoundingBoxDescent + space_height;
  442. // textW_all += textW;
  443. textH_all_cache += textH;
  444. if(tb=="2" && !shareCell.wrap){
  445. if(textH_all_cache>cellHeight && textH_all_Column[colIndex]!=null){
  446. // textW_all += textW;
  447. // textH_all = Math.max(textH_all,textH_all_cache);
  448. // console.log(">",i,colIndex);
  449. textH_all_ColumnHeight.push(textH_all_cache-textH);
  450. textH_all_cache = textH;
  451. colIndex +=1;
  452. }
  453. }
  454. if(i== inlineStringArr.length-1){
  455. textH_all_ColumnHeight.push(textH_all_cache);
  456. }
  457. if(textH_all_Column[colIndex]==null){
  458. textH_all_Column[colIndex]= [];
  459. }
  460. let item = {
  461. content:showValue,
  462. style:shareCell,
  463. width:textW,
  464. height:textH,
  465. left:0,
  466. top:0,
  467. colIndex:colIndex,
  468. asc:measureText.actualBoundingBoxAscent,
  469. desc:measureText.actualBoundingBoxDescent,
  470. inline:true,
  471. }
  472. if(shareCell.wrap===true){
  473. item.wrap=true;
  474. }
  475. textH_all_Column[colIndex].push(item);
  476. console.log("normal",i,colIndex,shareCell, preShareCell, textH_all_Column);
  477. preShareCell = shareCell;
  478. }
  479. }
  480. else{
  481. let measureText = getMeasureText(value, ctx);
  482. let textHeight = measureText.actualBoundingBoxDescent + measureText.actualBoundingBoxAscent;
  483. value = value.toString();
  484. let vArr = [];
  485. if(value.length > 1){
  486. vArr = value.split("");
  487. }
  488. else{
  489. vArr.push(value);
  490. }
  491. let oneWordWidth = getMeasureText(vArr[0], ctx).width;
  492. for(let i = 0; i < vArr.length; i++){
  493. let textW = oneWordWidth + space_width;
  494. let textH = textHeight + space_height;
  495. // textW_all += textW;
  496. textH_all_cache += textH;
  497. if(tb=="2"){
  498. if(textH_all_cache>cellHeight && textH_all_Column[colIndex]!=null){
  499. // textW_all += textW;
  500. // textH_all = Math.max(textH_all,textH_all_cache);
  501. textH_all_ColumnHeight.push(textH_all_cache-textH);
  502. textH_all_cache = textH;
  503. colIndex +=1;
  504. }
  505. }
  506. if(i== vArr.length-1){
  507. textH_all_ColumnHeight.push(textH_all_cache);
  508. }
  509. if(textH_all_Column[colIndex]==null){
  510. textH_all_Column[colIndex]= [];
  511. }
  512. textH_all_Column[colIndex].push({
  513. content:vArr[i],
  514. style:fontset,
  515. width:textW,
  516. height:textH,
  517. left:0,
  518. top:0,
  519. colIndex:colIndex,
  520. asc:measureText.actualBoundingBoxAscent,
  521. desc:measureText.actualBoundingBoxDescent
  522. });
  523. }
  524. }
  525. let textH_all_ColumWidth = [];
  526. for(let i = 0; i < textH_all_ColumnHeight.length; i++){
  527. let columnHeight = textH_all_ColumnHeight[i];
  528. let col = textH_all_Column[i], colMaxW=0;
  529. for(let c=0;c<col.length;c++){
  530. let word = col[c];
  531. colMaxW = Math.max(colMaxW, word.width);
  532. }
  533. textH_all_ColumWidth.push(colMaxW);
  534. textW_all += colMaxW;
  535. textH_all = Math.max(textH_all, columnHeight);
  536. }
  537. textContent.type = "verticalWrap";
  538. textContent.textWidthAll = textW_all;
  539. textContent.textHeightAll = textH_all;
  540. if(isMode=="onlyWidth"){
  541. // console.log("verticalWrap", textContent,cell, option);
  542. return textContent;
  543. }
  544. let cumColumnWidth = 0;
  545. for(let i = 0; i < textH_all_ColumnHeight.length; i++){
  546. let columnHeight = textH_all_ColumnHeight[i];
  547. let columnWidth = textH_all_ColumWidth[i];
  548. let col = textH_all_Column[i];
  549. let cumWordHeight = 0;
  550. for(let c=0;c<col.length;c++){
  551. let word = col[c];
  552. let left = space_width + cumColumnWidth;
  553. if(horizonAlign == "0"){
  554. left = cellWidth / 2 + cumColumnWidth - textW_all/2 + space_width*textH_all_ColumnHeight.length;
  555. }
  556. else if(horizonAlign == "2"){
  557. left = cellWidth + cumColumnWidth - textW_all + space_width;
  558. }
  559. let top = (cellHeight - space_height) + cumWordHeight - columnHeight;
  560. if(verticalAlign == "0"){
  561. top = cellHeight / 2 + cumWordHeight - columnHeight/2;
  562. }
  563. else if(verticalAlign == "1"){
  564. top = space_height + cumWordHeight;
  565. }
  566. cumWordHeight += word.height;
  567. word.left = left;
  568. word.top = top;
  569. drawLineInfo(word, cancelLine, underLine,{
  570. width:columnWidth,
  571. height:word.height,
  572. left:left,
  573. top:top+word.height-space_height,
  574. asc:word.height,
  575. desc:0,
  576. fs:fontSize
  577. });
  578. textContent.values.push(word);
  579. }
  580. cumColumnWidth+=columnWidth;
  581. }
  582. }
  583. else{
  584. let supportBoundBox = isSupportBoundingBox(ctx);
  585. if(supportBoundBox){
  586. ctx.textBaseline = 'alphabetic';
  587. }
  588. else{
  589. ctx.textBaseline = 'bottom';
  590. }
  591. if(tb=="2" || isInline){//wrap
  592. let textW_all = 0; //拆分后宽高度合计
  593. let textH_all = 0;
  594. let textW_all_inner = 0;
  595. // let oneWordWidth = getMeasureText(vArr[0], ctx).width;
  596. let splitIndex=0, text_all_cache=0, text_all_split = {}, text_all_splitLen=[];
  597. textContent.rotate = rt;
  598. rt = Math.abs(rt);
  599. let anchor = 0, preHeight = 0, preWidth=0, preStr, preTextHeight, preTextWidth, preMeasureText, i=1, wrapStyle={}, spaceOrTwoByte=null, spaceOrTwoByteIndex=null;
  600. if(isInline){
  601. while(i <= inlineStringArr.length){
  602. let shareCells = inlineStringArr.slice(anchor, i);
  603. if(shareCells[shareCells.length-1].wrap===true){
  604. anchor = i;
  605. if(shareCells.length>1){
  606. for(let s=0;s<shareCells.length-1;s++){
  607. let sc = shareCells[s];
  608. let item = {
  609. content:sc.v,
  610. style:sc,
  611. width:sc.measureText.width,
  612. height:sc.measureText.actualBoundingBoxAscent+sc.measureText.actualBoundingBoxDescent,
  613. left:0,
  614. top:0,
  615. splitIndex:splitIndex,
  616. asc:sc.measureText.actualBoundingBoxAscent,
  617. desc:sc.measureText.actualBoundingBoxDescent,
  618. inline:true,
  619. fs:sc.fs
  620. }
  621. // if(rt!=0){//rotate
  622. // item.textHeight = sc.textHeight;
  623. // item.textWidth = sc.textWidth;
  624. // }
  625. text_all_split[splitIndex].push(item);
  626. }
  627. }
  628. if(shareCells.length==1 || i==inlineStringArr.length){
  629. let sc = shareCells[0];
  630. let measureText = getMeasureText("M", ctx, sc.fontset);
  631. if(text_all_split[splitIndex]==null){
  632. text_all_split[splitIndex] = [];
  633. }
  634. text_all_split[splitIndex].push({
  635. content:"",
  636. style:sc,
  637. width:measureText.width,
  638. height:measureText.actualBoundingBoxAscent+measureText.actualBoundingBoxDescent,
  639. left:0,
  640. top:0,
  641. splitIndex:splitIndex,
  642. asc:measureText.actualBoundingBoxAscent,
  643. desc:measureText.actualBoundingBoxDescent,
  644. inline:true,
  645. wrap:true,
  646. fs:sc.fs
  647. });
  648. }
  649. splitIndex +=1;
  650. i++;
  651. continue;
  652. }
  653. let textWidth=0, textHeight=0;
  654. for(let s=0;s<shareCells.length;s++){
  655. let sc = shareCells[s];
  656. if(sc.measureText==null){
  657. sc.measureText = getMeasureText(sc.v, ctx, sc.fontset);
  658. }
  659. textWidth += sc.measureText.width;
  660. textHeight = Math.max(sc.measureText.actualBoundingBoxAscent+sc.measureText.actualBoundingBoxDescent);
  661. // console.log(sc.v,sc.measureText.width,sc.measureText.actualBoundingBoxAscent,sc.measureText.actualBoundingBoxDescent);
  662. }
  663. let width = textWidth * Math.cos(rt*Math.PI/180) + textHeight * Math.sin(rt*Math.PI/180);//consider text box wdith and line height
  664. let height = textWidth * Math.sin(rt*Math.PI/180) + textHeight * Math.cos(rt*Math.PI/180);//consider text box wdith and line height
  665. // textW_all += textW;
  666. let lastWord = shareCells[shareCells.length-1];
  667. if(lastWord.v==" " || checkWordByteLength(lastWord.v)==2){
  668. spaceOrTwoByteIndex = i;
  669. }
  670. if(rt!=0){//rotate
  671. // console.log("all",anchor, i , str);
  672. console.log(height,space_height, cellHeight, shareCells,(height+space_height)>cellHeight);
  673. if((height+space_height)>cellHeight && text_all_split[splitIndex]!=null && tb=="2" && i!= inlineStringArr.length){
  674. // console.log("cut",anchor, i , str);
  675. if(spaceOrTwoByteIndex!=null && spaceOrTwoByteIndex<i){
  676. for(let s=0;s<spaceOrTwoByteIndex-anchor;s++){
  677. let sc = shareCells[s];
  678. text_all_split[splitIndex].push({
  679. content:sc.v,
  680. style:sc,
  681. width:sc.measureText.width,
  682. height:sc.measureText.actualBoundingBoxAscent+sc.measureText.actualBoundingBoxDescent,
  683. left:0,
  684. top:0,
  685. splitIndex:splitIndex,
  686. asc:sc.measureText.actualBoundingBoxAscent,
  687. desc:sc.measureText.actualBoundingBoxDescent,
  688. inline:true,
  689. fs:sc.fs
  690. });
  691. }
  692. anchor = spaceOrTwoByteIndex;
  693. i = spaceOrTwoByteIndex + 1;
  694. splitIndex +=1;
  695. spaceOrTwoByteIndex = null;
  696. }
  697. else{
  698. anchor = i-1;
  699. for(let s=0;s<shareCells.length-1;s++){
  700. let sc = shareCells[s];
  701. text_all_split[splitIndex].push({
  702. content:sc.v,
  703. style:sc,
  704. width:sc.measureText.width,
  705. height:sc.measureText.actualBoundingBoxAscent+sc.measureText.actualBoundingBoxDescent,
  706. left:0,
  707. top:0,
  708. splitIndex:splitIndex,
  709. asc:sc.measureText.actualBoundingBoxAscent,
  710. desc:sc.measureText.actualBoundingBoxDescent,
  711. inline:true,
  712. fs:sc.fs
  713. });
  714. }
  715. splitIndex +=1;
  716. }
  717. }
  718. else if(i== inlineStringArr.length){
  719. // console.log("last",anchor, i , str);
  720. if(text_all_split[splitIndex]==null){
  721. text_all_split[splitIndex]= [];
  722. }
  723. for(let s=0;s<shareCells.length;s++){
  724. let sc = shareCells[s];
  725. text_all_split[splitIndex].push({
  726. content:sc.v,
  727. style:sc,
  728. width:sc.measureText.width,
  729. height:sc.measureText.actualBoundingBoxAscent+sc.measureText.actualBoundingBoxDescent,
  730. left:0,
  731. top:0,
  732. splitIndex:splitIndex,
  733. asc:sc.measureText.actualBoundingBoxAscent,
  734. desc:sc.measureText.actualBoundingBoxDescent,
  735. inline:true,
  736. fs:sc.fs
  737. });
  738. }
  739. break;
  740. }
  741. else{
  742. if(text_all_split[splitIndex]==null){
  743. text_all_split[splitIndex]= [];
  744. }
  745. i++;
  746. }
  747. }
  748. else{//plain
  749. if((width+space_width)>cellWidth && text_all_split[splitIndex]!=null && tb=="2" && i!= inlineStringArr.length){
  750. if(spaceOrTwoByteIndex!=null && spaceOrTwoByteIndex<i){
  751. for(let s=0;s<spaceOrTwoByteIndex-anchor;s++){
  752. let sc = shareCells[s];
  753. text_all_split[splitIndex].push({
  754. content:sc.v,
  755. style:sc,
  756. width:sc.measureText.width,
  757. height:sc.measureText.actualBoundingBoxAscent+sc.measureText.actualBoundingBoxDescent,
  758. left:0,
  759. top:0,
  760. splitIndex:splitIndex,
  761. asc:sc.measureText.actualBoundingBoxAscent,
  762. desc:sc.measureText.actualBoundingBoxDescent,
  763. inline:true,
  764. fs:sc.fs
  765. });
  766. }
  767. anchor = spaceOrTwoByteIndex;
  768. i = spaceOrTwoByteIndex + 1;
  769. splitIndex +=1;
  770. spaceOrTwoByteIndex = null;
  771. }
  772. else{
  773. anchor = i-1;
  774. for(let s=0;s<shareCells.length-1;s++){
  775. let sc = shareCells[s];
  776. text_all_split[splitIndex].push({
  777. content:sc.v,
  778. style:sc,
  779. width:sc.measureText.width,
  780. height:sc.measureText.actualBoundingBoxAscent+sc.measureText.actualBoundingBoxDescent,
  781. left:0,
  782. top:0,
  783. splitIndex:splitIndex,
  784. asc:sc.measureText.actualBoundingBoxAscent,
  785. desc:sc.measureText.actualBoundingBoxDescent,
  786. inline:true,
  787. fs:sc.fs
  788. });
  789. }
  790. splitIndex +=1;
  791. }
  792. }
  793. else if(i== inlineStringArr.length){
  794. if(text_all_split[splitIndex]==null){
  795. text_all_split[splitIndex]= [];
  796. }
  797. for(let s=0;s<shareCells.length;s++){
  798. let sc = shareCells[s];
  799. text_all_split[splitIndex].push({
  800. content:sc.v,
  801. style:sc,
  802. width:sc.measureText.width,
  803. height:sc.measureText.actualBoundingBoxAscent+sc.measureText.actualBoundingBoxDescent,
  804. left:0,
  805. top:0,
  806. splitIndex:splitIndex,
  807. asc:sc.measureText.actualBoundingBoxAscent,
  808. desc:sc.measureText.actualBoundingBoxDescent,
  809. inline:true,
  810. fs:sc.fs
  811. });
  812. }
  813. break;
  814. }
  815. else{
  816. if(text_all_split[splitIndex]==null){
  817. text_all_split[splitIndex]= [];
  818. }
  819. i++;
  820. }
  821. }
  822. }
  823. }
  824. else{
  825. value = value.toString();
  826. while(i <= value.length){
  827. let str = value.substring(anchor, i);
  828. let measureText = getMeasureText(str, ctx);
  829. let textWidth = measureText.width;
  830. let textHeight = measureText.actualBoundingBoxAscent+measureText.actualBoundingBoxDescent;
  831. let width = textWidth * Math.cos(rt*Math.PI/180) + textHeight * Math.sin(rt*Math.PI/180);//consider text box wdith and line height
  832. let height = textWidth * Math.sin(rt*Math.PI/180) + textHeight * Math.cos(rt*Math.PI/180);//consider text box wdith and line height
  833. let lastWord = str.substr(str.length-1,1);
  834. if(lastWord==" " || checkWordByteLength(lastWord)==2){
  835. if(preMeasureText!=null){
  836. spaceOrTwoByte = {
  837. index:i,
  838. str:preStr + lastWord,
  839. width:preTextWidth,
  840. height:preTextHeight,
  841. asc:preMeasureText.actualBoundingBoxAscent,
  842. desc:preMeasureText.actualBoundingBoxDescent,
  843. };
  844. }
  845. }
  846. // textW_all += textW;
  847. // console.log(str,anchor,i);
  848. if(rt!=0){//rotate
  849. // console.log("all",anchor, i , str);
  850. if((height+space_height)>cellHeight && text_all_split[splitIndex]!=null && i!= value.length){
  851. // console.log("cut",anchor, i , str);
  852. if(spaceOrTwoByte!=null && spaceOrTwoByte.index<i){
  853. anchor = spaceOrTwoByte.index;
  854. i = spaceOrTwoByte.index + 1;
  855. text_all_split[splitIndex].push({
  856. content:spaceOrTwoByte.str,
  857. style:fontset,
  858. width:spaceOrTwoByte.width,
  859. height:spaceOrTwoByte.height,
  860. left:0,
  861. top:0,
  862. splitIndex:splitIndex,
  863. asc:spaceOrTwoByte.asc,
  864. desc:spaceOrTwoByte.desc,
  865. fs:fontSize,
  866. });
  867. // console.log(1,anchor,i,splitIndex , spaceOrTwoByte.str);
  868. splitIndex +=1;
  869. spaceOrTwoByte = null;
  870. }
  871. else{
  872. anchor = i-1;
  873. text_all_split[splitIndex].push({
  874. content:preStr,
  875. style:fontset,
  876. left:0,
  877. top:0,
  878. splitIndex:splitIndex,
  879. height:preTextHeight,
  880. width:preTextWidth,
  881. asc:measureText.actualBoundingBoxAscent,
  882. desc:measureText.actualBoundingBoxDescent,
  883. fs:fontSize,
  884. });
  885. // console.log(2,anchor,i, splitIndex, preStr);
  886. splitIndex +=1;
  887. }
  888. }
  889. else if(i== value.length){
  890. // console.log("last",anchor, i , str);
  891. if(text_all_split[splitIndex]==null){
  892. text_all_split[splitIndex]= [];
  893. }
  894. text_all_split[splitIndex].push({
  895. content:str,
  896. style:fontset,
  897. left:0,
  898. top:0,
  899. splitIndex:splitIndex,
  900. height:textHeight,
  901. width:textWidth,
  902. asc:measureText.actualBoundingBoxAscent,
  903. desc:measureText.actualBoundingBoxDescent,
  904. fs:fontSize,
  905. });
  906. break;
  907. }
  908. else{
  909. if(text_all_split[splitIndex]==null){
  910. text_all_split[splitIndex]= [];
  911. }
  912. i++;
  913. }
  914. }
  915. else{//plain
  916. if((width+space_width)>cellWidth && text_all_split[splitIndex]!=null && i!= value.length){
  917. // console.log(spaceOrTwoByte, i, anchor);
  918. if(spaceOrTwoByte!=null && spaceOrTwoByte.index<i){
  919. anchor = spaceOrTwoByte.index;
  920. i = spaceOrTwoByte.index + 1;
  921. text_all_split[splitIndex].push({
  922. content:spaceOrTwoByte.str,
  923. style:fontset,
  924. width:spaceOrTwoByte.width,
  925. height:spaceOrTwoByte.height,
  926. left:0,
  927. top:0,
  928. splitIndex:splitIndex,
  929. asc:spaceOrTwoByte.asc,
  930. desc:spaceOrTwoByte.desc,
  931. fs:fontSize,
  932. });
  933. splitIndex +=1;
  934. spaceOrTwoByte = null;
  935. }
  936. else{
  937. spaceOrTwoByte = null;
  938. anchor = i-1;
  939. text_all_split[splitIndex].push({
  940. content:preStr,
  941. style:fontset,
  942. width:preTextWidth,
  943. height:preTextHeight,
  944. left:0,
  945. top:0,
  946. splitIndex:splitIndex,
  947. asc:measureText.actualBoundingBoxAscent,
  948. desc:measureText.actualBoundingBoxDescent,
  949. fs:fontSize,
  950. });
  951. // console.log(2);
  952. splitIndex +=1;
  953. }
  954. }
  955. else if(i== value.length){
  956. if(text_all_split[splitIndex]==null){
  957. text_all_split[splitIndex]= [];
  958. }
  959. text_all_split[splitIndex].push({
  960. content:str,
  961. style:fontset,
  962. width:textWidth,
  963. height:textHeight,
  964. left:0,
  965. top:0,
  966. splitIndex:splitIndex,
  967. asc:measureText.actualBoundingBoxAscent,
  968. desc:measureText.actualBoundingBoxDescent,
  969. fs:fontSize,
  970. });
  971. break;
  972. }
  973. else{
  974. if(text_all_split[splitIndex]==null){
  975. text_all_split[splitIndex]= [];
  976. }
  977. i++;
  978. }
  979. }
  980. preStr = str;
  981. preTextHeight = textHeight;
  982. preTextWidth = textWidth;
  983. preMeasureText = measureText;
  984. }
  985. // console.log(text_all_split)
  986. }
  987. let split_all_size = [], oneLinemaxWordCount=0;
  988. // console.log("split",splitIndex, text_all_split);
  989. let splitLen = Object.keys(text_all_split).length;
  990. for(let i = 0; i < splitLen; i++){
  991. let splitLists = text_all_split[i];
  992. if(splitLists==null){
  993. continue;
  994. }
  995. let sWidth = 0, sHeight=0, maxDesc=0,maxAsc=0, lineHeight=0, maxWordCount=0;
  996. for(let s=0;s<splitLists.length;s++){
  997. let sp = splitLists[s];
  998. if(rt!=0){//rotate
  999. sWidth += sp.width;
  1000. sHeight = Math.max(sHeight, sp.height-(supportBoundBox?sp.desc:0));
  1001. }
  1002. else{//plain
  1003. sWidth += sp.width;
  1004. sHeight = Math.max(sHeight, sp.height-(supportBoundBox?sp.desc:0));
  1005. }
  1006. maxDesc = Math.max(maxDesc,(supportBoundBox?sp.desc:0));
  1007. maxAsc = Math.max(maxAsc, sp.asc);
  1008. maxWordCount++;
  1009. }
  1010. lineHeight = sHeight/2;
  1011. oneLinemaxWordCount = Math.max(oneLinemaxWordCount, maxWordCount);
  1012. if(rt!=0){//rotate
  1013. sHeight+=lineHeight;
  1014. textW_all_inner = Math.max(textW_all_inner, sWidth);
  1015. // textW_all = Math.max(textW_all, sWidth+ (textH_all)/Math.tan(rt*Math.PI/180));
  1016. textH_all += sHeight;
  1017. }
  1018. else{//plain
  1019. // console.log("textH_all",textW_all, textH_all);
  1020. sHeight+=lineHeight;
  1021. textW_all=Math.max(textW_all, sWidth);
  1022. textH_all+=sHeight;
  1023. }
  1024. split_all_size.push({
  1025. width:sWidth,
  1026. height:sHeight,
  1027. desc:maxDesc,
  1028. asc:maxAsc,
  1029. lineHeight:lineHeight,
  1030. wordCount: maxWordCount
  1031. });
  1032. }
  1033. // console.log(textH_all,textW_all,textW_all_inner);
  1034. // let cumColumnWidth = 0;
  1035. let cumWordHeight = 0,cumColumnWidth = 0;
  1036. let rtPI = rt*Math.PI/180;
  1037. let lastLine = split_all_size[splitLen-1];
  1038. let lastLineSpaceHeight = lastLine.lineHeight;
  1039. textH_all = textH_all - lastLineSpaceHeight + lastLine.desc;
  1040. let rw = (textH_all)/Math.sin(rtPI) + textW_all_inner*Math.cos(rtPI);
  1041. let rh = textW_all_inner*Math.sin(rtPI), fixOneLineLeft = 0;
  1042. if(rt!=0){
  1043. if(splitLen==1){
  1044. textW_all = textW_all_inner + 2*(textH_all/Math.tan(rtPI));
  1045. fixOneLineLeft = textH_all/Math.tan(rtPI);
  1046. }
  1047. else{
  1048. textW_all = textW_all_inner + textH_all/Math.tan(rtPI);
  1049. }
  1050. textContent.textWidthAll = rw;
  1051. textContent.textHeightAll = rh;
  1052. }
  1053. else{
  1054. textContent.textWidthAll = textW_all;
  1055. textContent.textHeightAll = textH_all;
  1056. }
  1057. if(isMode=="onlyWidth"){
  1058. // console.log("plainWrap", textContent,cell, option);
  1059. return textContent;
  1060. }
  1061. if(rt!=0 && isRotateUp=="1"){
  1062. ctx.textAlign="end";
  1063. for(let i = 0; i < splitLen; i++){
  1064. let splitLists = text_all_split[i];
  1065. if(splitLists==null){
  1066. continue;
  1067. }
  1068. let size = split_all_size[i];
  1069. cumColumnWidth = 0;
  1070. for(let c=splitLists.length-1;c>=0;c--){
  1071. let wordGroup = splitLists[c];
  1072. let left, top;
  1073. if(rt!=0){//rotate
  1074. let x, y = cumWordHeight+size.asc;
  1075. x = (cumWordHeight)/Math.tan(rtPI) - cumColumnWidth + textW_all_inner;
  1076. if(horizonAlign == "0"){//center
  1077. let sh = textH_all/Math.sin(rtPI);
  1078. if(verticalAlign == "0"){//mid
  1079. left = x + cellWidth/2 - (textW_all/2) + lastLineSpaceHeight*Math.cos(rtPI)/2;
  1080. top = y + cellHeight/2 - textH_all/2 - lastLineSpaceHeight*Math.cos(rtPI)/2;
  1081. }
  1082. else if(verticalAlign == "1"){//top
  1083. left = x + cellWidth/2 - textW_all/2;
  1084. top = y - (textH_all/2 - rh/2);
  1085. }
  1086. else if(verticalAlign == "2"){//bottom
  1087. left = x + cellWidth/2 - (textW_all/2)+lastLineSpaceHeight*Math.cos(rtPI);
  1088. top = y + cellHeight - rh/2 - textH_all/2 - lastLineSpaceHeight*Math.cos(rtPI);
  1089. }
  1090. }
  1091. else if(horizonAlign == "1"){//left
  1092. if(verticalAlign == "0"){//mid
  1093. left = x - rh*Math.sin(rtPI)/2 + lastLineSpaceHeight*Math.cos(rtPI)/2;
  1094. top = y + cellHeight/2 + rh*Math.cos(rtPI)/2 - lastLineSpaceHeight*Math.cos(rtPI)/2;
  1095. }
  1096. else if(verticalAlign == "1"){//top
  1097. left = x - rh*Math.sin(rtPI);
  1098. top = y + rh*Math.cos(rtPI);
  1099. }
  1100. else if(verticalAlign == "2"){//bottom
  1101. left = x + lastLineSpaceHeight*Math.cos(rtPI);
  1102. top = y + cellHeight - lastLineSpaceHeight*Math.cos(rtPI);
  1103. }
  1104. }
  1105. else if(horizonAlign == "2"){//right
  1106. if(verticalAlign == "0"){//mid
  1107. left = x + cellWidth - rw/2 - (textW_all_inner/2+(textH_all/2)/Math.tan(rtPI))+ lastLineSpaceHeight*Math.cos(rtPI)/2;
  1108. top = y + cellHeight/2 - textH_all/2 - lastLineSpaceHeight*Math.cos(rtPI)/2;
  1109. }
  1110. else if(verticalAlign == "1"){//top fixOneLineLeft
  1111. left = x + cellWidth - textW_all + fixOneLineLeft;
  1112. top = y - textH_all;
  1113. }
  1114. else if(verticalAlign == "2"){//bottom
  1115. left = x + cellWidth - rw*Math.cos(rtPI) + lastLineSpaceHeight*Math.cos(rtPI);
  1116. top = y + cellHeight - rw*Math.sin(rtPI) - lastLineSpaceHeight*Math.cos(rtPI);
  1117. }
  1118. }
  1119. }
  1120. wordGroup.left = left;
  1121. wordGroup.top = top;
  1122. // console.log(left, top, cumWordHeight, size.height);
  1123. drawLineInfo(wordGroup, cancelLine, underLine,{
  1124. width:wordGroup.width,
  1125. height:wordGroup.height,
  1126. left:left-wordGroup.width,
  1127. top:top,
  1128. asc:size.asc,
  1129. desc:size.desc,
  1130. fs:wordGroup.fs
  1131. });
  1132. textContent.values.push(wordGroup);
  1133. cumColumnWidth += wordGroup.width;
  1134. }
  1135. cumWordHeight += size.height;
  1136. }
  1137. }
  1138. else{
  1139. for(let i = 0; i < splitLen; i++){
  1140. let splitLists = text_all_split[i];
  1141. if(splitLists==null){
  1142. continue;
  1143. }
  1144. let size = split_all_size[i];
  1145. cumColumnWidth = 0;
  1146. for(let c=0;c<splitLists.length;c++){
  1147. let wordGroup = splitLists[c];
  1148. let left, top;
  1149. if(rt!=0){//rotate
  1150. let x, y = cumWordHeight+size.asc;
  1151. x = (textH_all-cumWordHeight)/Math.tan(rtPI) + cumColumnWidth;
  1152. if(horizonAlign == "0"){//center
  1153. let sh = textH_all/Math.sin(rtPI);
  1154. if(verticalAlign == "0"){//mid
  1155. left = x + cellWidth/2 - (textW_all/2) - lastLineSpaceHeight*Math.cos(rtPI)/2;
  1156. top = y + cellHeight/2 - textH_all/2 + lastLineSpaceHeight*Math.cos(rtPI)/2;
  1157. }
  1158. else if(verticalAlign == "1"){//top
  1159. left = x + cellWidth/2 - textW_all/2 - lastLineSpaceHeight*Math.cos(rtPI)/2;
  1160. top = y - (textH_all/2 - rh/2)+lastLineSpaceHeight*Math.cos(rtPI)/2;
  1161. }
  1162. else if(verticalAlign == "2"){//bottom
  1163. left = x + cellWidth/2 - (textW_all/2)-lastLineSpaceHeight*Math.cos(rtPI);
  1164. top = y + cellHeight - rh/2 - textH_all/2 - lastLineSpaceHeight*Math.cos(rtPI);
  1165. }
  1166. }
  1167. else if(horizonAlign == "1"){//left
  1168. if(verticalAlign == "0"){//mid
  1169. left = x - rh*Math.sin(rtPI)/2 - lastLineSpaceHeight*Math.cos(rtPI)/2;
  1170. top = y - textH_all + cellHeight/2 - rh*Math.cos(rtPI)/2 - lastLineSpaceHeight*Math.cos(rtPI)/2;
  1171. }
  1172. else if(verticalAlign == "1"){//top
  1173. left = x;
  1174. top = y - textH_all;
  1175. }
  1176. else if(verticalAlign == "2"){//bottom
  1177. left = x - rh*Math.sin(rtPI) - lastLineSpaceHeight*Math.cos(rtPI);
  1178. top = y - textH_all + cellHeight - rh*Math.cos(rtPI) - lastLineSpaceHeight*Math.cos(rtPI);
  1179. }
  1180. }
  1181. else if(horizonAlign == "2"){//right
  1182. if(verticalAlign == "0"){//mid
  1183. left = x + cellWidth - rw/2 - textW_all/2 - lastLineSpaceHeight*Math.cos(rtPI)/2;
  1184. top = y + cellHeight/2 - textH_all/2 - lastLineSpaceHeight*Math.cos(rtPI)/2;
  1185. }
  1186. else if(verticalAlign == "1"){//top fixOneLineLeft
  1187. left = x + cellWidth - rw*Math.cos(rtPI);
  1188. top = y + rh*Math.cos(rtPI);
  1189. }
  1190. else if(verticalAlign == "2"){//bottom
  1191. left = x + cellWidth - textW_all - lastLineSpaceHeight*Math.cos(rtPI) + fixOneLineLeft;
  1192. top = y + cellHeight - lastLineSpaceHeight*Math.cos(rtPI);
  1193. }
  1194. }
  1195. drawLineInfo(wordGroup, cancelLine, underLine,{
  1196. width:wordGroup.width,
  1197. height:wordGroup.height,
  1198. left:left,
  1199. top:top,
  1200. asc:size.asc,
  1201. desc:size.desc,
  1202. fs:wordGroup.fs
  1203. });
  1204. }
  1205. else{//plain
  1206. left = space_width + cumColumnWidth;
  1207. if(horizonAlign == "0"){
  1208. //+ space_width*textH_all_ColumnHeight.length
  1209. left = cellWidth / 2 + cumColumnWidth - size.width/2;
  1210. }
  1211. else if(horizonAlign == "2"){
  1212. left = cellWidth + cumColumnWidth - size.width;
  1213. }
  1214. top = (cellHeight - space_height) + cumWordHeight +size.asc-textH_all;
  1215. if(verticalAlign == "0"){
  1216. top = cellHeight / 2 + cumWordHeight - textH_all/2 + size.asc;
  1217. }
  1218. else if(verticalAlign == "1"){
  1219. top = space_height + cumWordHeight+ size.asc;
  1220. }
  1221. drawLineInfo(wordGroup, cancelLine, underLine,{
  1222. width:wordGroup.width,
  1223. height:wordGroup.height,
  1224. left:left,
  1225. top:top,
  1226. asc:size.asc,
  1227. desc:size.desc,
  1228. fs:wordGroup.fs
  1229. });
  1230. }
  1231. wordGroup.left = left;
  1232. wordGroup.top = top;
  1233. textContent.values.push(wordGroup);
  1234. cumColumnWidth += wordGroup.width;
  1235. }
  1236. cumWordHeight += size.height;
  1237. }
  1238. }
  1239. textContent.type = "plainWrap";
  1240. if(rt!=0){
  1241. // let leftCenter = (textW_all + textH_all/Math.tan(rt*Math.PI/180))/2;
  1242. // let topCenter = textH_all/2;
  1243. // if(isRotateUp=="1"){
  1244. // textContent.textLeftAll += leftCenter;
  1245. // textContent.textTopAll += topCenter;
  1246. // }
  1247. // else {
  1248. // textContent.textLeftAll += leftCenter;
  1249. // textContent.textTopAll -= topCenter;
  1250. // }
  1251. if(horizonAlign == "0"){//center
  1252. if(verticalAlign == "0"){//mid
  1253. textContent.textLeftAll = cellWidth/2;
  1254. textContent.textTopAll = cellHeight/2;
  1255. }
  1256. else if(verticalAlign == "1"){//top
  1257. textContent.textLeftAll = cellWidth/2;
  1258. textContent.textTopAll = rh/2;
  1259. }
  1260. else if(verticalAlign == "2"){//bottom
  1261. textContent.textLeftAll = cellWidth/2;
  1262. textContent.textTopAll = cellHeight - rh/2;
  1263. }
  1264. }
  1265. else if(horizonAlign == "1"){//left
  1266. if(verticalAlign == "0"){//mid
  1267. textContent.textLeftAll = 0;
  1268. textContent.textTopAll = cellHeight/2;
  1269. }
  1270. else if(verticalAlign == "1"){//top
  1271. textContent.textLeftAll = 0;
  1272. textContent.textTopAll = 0;
  1273. }
  1274. else if(verticalAlign == "2"){//bottom
  1275. textContent.textLeftAll = 0;
  1276. textContent.textTopAll = cellHeight;
  1277. }
  1278. }
  1279. else if(horizonAlign == "2"){//right
  1280. if(verticalAlign == "0"){//mid
  1281. textContent.textLeftAll = cellWidth - rw/2;
  1282. textContent.textTopAll = cellHeight/2;
  1283. }
  1284. else if(verticalAlign == "1"){//top
  1285. textContent.textLeftAll = cellWidth;
  1286. textContent.textTopAll = 0;
  1287. }
  1288. else if(verticalAlign == "2"){//bottom
  1289. textContent.textLeftAll = cellWidth;
  1290. textContent.textTopAll = cellHeight;
  1291. }
  1292. }
  1293. }
  1294. // else{
  1295. // textContent.textWidthAll = textW_all;
  1296. // textContent.textHeightAll = textH_all;
  1297. // }
  1298. }
  1299. else{
  1300. let measureText = getMeasureText(value, ctx);
  1301. let textWidth = measureText.width;
  1302. let textHeight = measureText.actualBoundingBoxDescent + measureText.actualBoundingBoxAscent;
  1303. textContent.rotate = rt;
  1304. rt = Math.abs(rt);
  1305. let rtPI = rt*Math.PI/180;
  1306. let textWidthAll = textWidth * Math.cos(rtPI) + textHeight * Math.sin(rtPI);//consider text box wdith and line height
  1307. let textHeightAll = textWidth * Math.sin(rtPI) + textHeight * Math.cos(rtPI);//consider text box wdith and line height
  1308. if(rt!=0){
  1309. textContent.textHeightAll = textHeightAll;
  1310. }
  1311. else{
  1312. textContent.textHeightAll = textHeightAll+textHeight/2-measureText.actualBoundingBoxDescent-space_height;
  1313. }
  1314. textContent.textWidthAll = textWidthAll;
  1315. // console.log(textContent.textWidthAll , textContent.textHeightAll);
  1316. if(isMode=="onlyWidth"){
  1317. // console.log("plain", textContent,cell, option);
  1318. return textContent;
  1319. }
  1320. let width = textWidthAll, height = textHeightAll;
  1321. let left = space_width + textHeight * Math.sin(rtPI)*isRotateUp; //默认为1,左对齐
  1322. if(horizonAlign == "0"){ //居中对齐
  1323. left = cellWidth / 2 - (width / 2) + textHeight * Math.sin(rtPI)*isRotateUp;
  1324. }
  1325. else if(horizonAlign == "2"){ //右对齐
  1326. left = (cellWidth - space_width) - width + textHeight * Math.sin(rtPI)*isRotateUp;
  1327. }
  1328. let top = (cellHeight - space_height) - height + measureText.actualBoundingBoxAscent * Math.cos(rtPI) + textWidth * Math.sin(rtPI)*isRotateUp; //默认为2,下对齐
  1329. if(verticalAlign == "0"){ //居中对齐
  1330. top = cellHeight / 2 - (height / 2) + measureText.actualBoundingBoxAscent* Math.cos(rtPI) + textWidth * Math.sin(rtPI)*isRotateUp;
  1331. }
  1332. else if(verticalAlign == "1"){ //上对齐
  1333. top = space_height + measureText.actualBoundingBoxAscent* Math.cos(rtPI) + textWidth * Math.sin(rtPI)*isRotateUp;
  1334. }
  1335. textContent.type = "plain";
  1336. let wordGroup = {
  1337. content:value,
  1338. style:fontset,
  1339. width:width,
  1340. height:height,
  1341. left:left,
  1342. top:top,
  1343. }
  1344. drawLineInfo(wordGroup, cancelLine, underLine,{
  1345. width:textWidth,
  1346. height:textHeight,
  1347. left:left,
  1348. top:top,
  1349. asc:measureText.actualBoundingBoxAscent,
  1350. desc:measureText.actualBoundingBoxDescent,
  1351. fs:fontSize,
  1352. });
  1353. textContent.values.push(wordGroup);
  1354. textContent.textLeftAll = left;
  1355. textContent.textTopAll = top;
  1356. textContent.asc = measureText.actualBoundingBoxAscent;
  1357. textContent.desc = measureText.actualBoundingBoxDescent;
  1358. // console.log("plain",left,top);
  1359. }
  1360. }
  1361. return textContent;
  1362. }
  1363. function drawLineInfo(wordGroup, cancelLine,underLine,option){
  1364. let left = option.left, top = option.top, width=option.width, height = option.height, asc = option.asc,desc = option.desc,fs = option.fs;
  1365. if(wordGroup.wrap===true){
  1366. return;
  1367. }
  1368. if(wordGroup.inline==true && wordGroup.style!=null){
  1369. cancelLine = wordGroup.style.cl;
  1370. underLine = wordGroup.style.un;
  1371. }
  1372. if(cancelLine!="0"){
  1373. wordGroup.cancelLine = {};
  1374. wordGroup.cancelLine.startX = left;
  1375. wordGroup.cancelLine.startY = top-asc/2+1;
  1376. wordGroup.cancelLine.endX = left + width;
  1377. wordGroup.cancelLine.endY = top-asc/2+1;
  1378. wordGroup.cancelLine.fs = fs;
  1379. }
  1380. if(underLine!="0"){
  1381. wordGroup.underLine = [];
  1382. if(underLine=="1" || underLine=="2"){
  1383. let item = {};
  1384. item.startX = left;
  1385. item.startY = top + 3;
  1386. item.endX = left + width;
  1387. item.endY = top + 3;
  1388. item.fs = fs;
  1389. wordGroup.underLine.push(item);
  1390. }
  1391. if(underLine=="2"){
  1392. let item = {};
  1393. item.startX = left;
  1394. item.startY = top+desc;
  1395. item.endX = left + width;
  1396. item.endY = top+desc;
  1397. item.fs = fs;
  1398. wordGroup.underLine.push(item);
  1399. }
  1400. if(underLine=="3" || underLine=="4"){
  1401. let item = {};
  1402. item.startX = left;
  1403. item.startY = top+desc;
  1404. item.endX = left + width;
  1405. item.endY = top+desc;
  1406. item.fs = fs;
  1407. wordGroup.underLine.push(item);
  1408. }
  1409. if(underLine=="4"){
  1410. let item = {};
  1411. item.startX = left;
  1412. item.startY = top+desc+2;
  1413. item.endX = left + width;
  1414. item.endY = top+desc+2;
  1415. item.fs = fs;
  1416. wordGroup.underLine.push(item);
  1417. }
  1418. }
  1419. }
  1420. export {
  1421. computeColWidthByContent,
  1422. rowlenByRange,
  1423. computeRowlenByContent,
  1424. computeRowlenArr,
  1425. getCellTextSplitArr,
  1426. getMeasureText,
  1427. getCellTextInfo
  1428. }