KoctrX

Untitled

Jan 12th, 2023
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 28.16 KB | None | 0 0
  1. function guid(){
  2. function s4() { return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1); }
  3. return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
  4. }
  5.  
  6. var clip_stack = [];
  7. var render_in_slider = false;
  8. let shading_id = 0;
  9.  
  10. var basename = name => {
  11. console.log('name:', name);
  12. return /([^\/]*)\.[^.]*$/gm.exec(name)[1];
  13. };
  14. function getSystemFont(nc=""){
  15. var epta_fonts = {
  16. // "Arial": "Arial",
  17. // "Arial-Bold": "Arial-Bold",
  18. // "ArialNarrow": "ArialNarrow",
  19. // "ArialNarrow-Bold": "ArialNarrow-Bold",
  20. // "ArialMT": "Arial",
  21. // "ArialMT-Bold": "Arial-Bold",
  22. // "Arial-BoldMT": "Arial-Bold",
  23. "Helvetica": "Helvetica",
  24. "Helvetica-Bold": "Helvetica-Bold",
  25. "Helvetica-Oblique": "Helvetica-Oblique",
  26. "Helvetica-BoldOblique": "Helvetica-BoldOblique",
  27. "Times-Roman": "Times-Roman",
  28. "Times-Bold": "Times-Roman",
  29. "Times-Roman-Bold": "Times-Roman-Bold",
  30. "Times-Italic": "Times-Italic",
  31. "Symbol": "Symbol",
  32. "ZapfDingbats": "ZapfDingbats",
  33. "Courier": "Courier",
  34. "Courier-Bold": "Courier",
  35. "Courier-Oblique": "Courier",
  36. "Courier-BoldOblique": "Courier",
  37.  
  38.  
  39. };
  40.  
  41. if(typeof epta_fonts[nc]!='undefined'){
  42. return epta_fonts[nc];
  43. }
  44. return false;
  45. }
  46.  
  47.  
  48.  
  49.  
  50. var pf2 = function pf(value) {
  51. if (Number.isInteger(value)) {
  52. return value.toString();
  53. }
  54.  
  55. var s = value.toFixed(10);
  56. var i = s.length - 1;
  57.  
  58. if (s[i] !== '0') {
  59. return s;
  60. }
  61.  
  62. do {
  63. i--;
  64. } while (s[i] === '0');
  65.  
  66. return s.substring(0, s[i] === '.' ? i : i + 1);
  67. };
  68.  
  69. var pm2 = function pm(m) {
  70. if (m[4] === 0 && m[5] === 0) {
  71. if (m[1] === 0 && m[2] === 0) {
  72. if (m[0] === 1 && m[3] === 1) {
  73. return '';
  74. }
  75.  
  76. return "scale(".concat(pf(m[0]), " ").concat(pf(m[3]), ")");
  77. }
  78.  
  79. if (m[0] === m[3] && m[1] === -m[2]) {
  80. var a = Math.acos(m[0]) * 180 / Math.PI;
  81. return "rotate(".concat(pf(a), ")");
  82. }
  83. } else {
  84. if (m[0] === 1 && m[1] === 0 && m[2] === 0 && m[3] === 1) {
  85. return "translate(".concat(pf(m[4]), " ").concat(pf(m[5]), ")");
  86. }
  87. }
  88.  
  89. return "matrix(".concat(pf(m[0]), " ").concat(pf(m[1]), " ").concat(pf(m[2]), " ").concat(pf(m[3]), " ").concat(pf(m[4]), " ") + "".concat(pf(m[5]), ")");
  90. };
  91.  
  92. function getPrevCharRight(positions, line, pos){
  93. if(typeof positions[line]=='undefined' || typeof positions[line][pos-1]=='undefined' || typeof positions[line][pos]=='undefined' ){
  94. return 0;
  95. }
  96. return positions[line][pos-1].left+(positions[line][pos].left-positions[line][pos-1].left);
  97. }
  98.  
  99. function getCharWidth(positions, line, pos){
  100. if(typeof positions[line]=='undefined' || typeof positions[line][pos]=='undefined' || typeof positions[line][pos-1]=='undefined'){
  101. return 0;
  102. }
  103. return positions[line][pos].left-positions[line][pos-1].left;
  104. }
  105.  
  106. function escapeRegExp(string) {
  107. return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
  108. }
  109.  
  110.  
  111. function getCharUnicode(code, font){
  112.  
  113. if(typeof viewer.embedFontsNames_reverse[font]=='undefined'){
  114. return code;
  115. }
  116.  
  117.  
  118. var fn = viewer.embedFontsNames_reverse[font];
  119.  
  120. var new_codes = [];
  121. for(let i=0; i!=code.length; i++){
  122. char_code = code[i];
  123. var x = String.fromCharCode(char_code);
  124. if(typeof viewer.chars_table_inverse[fn][x]!='undefined'){
  125. new_codes[i] = viewer.chars_table_inverse[fn][x].charCodeAt(x);
  126. }else{
  127. new_codes[i] = char_code;
  128. }
  129. }
  130.  
  131. return new_codes;
  132. }
  133.  
  134. function charInFont(char, font){
  135. if(typeof viewer.chars_table_inverse[font]!= 'undefined' && typeof viewer.chars_table_inverse[font][char]!='undefined'){
  136. return true;
  137. }
  138. return false;
  139. }
  140.  
  141. function info(msg){
  142. $.toast({
  143. heading: 'Information',
  144. text: msg,
  145. icon: 'info',
  146. loader: true, // Change it to false to disable loader
  147. loaderBg: '#9EC600' // To change the background
  148. });
  149. };
  150.  
  151.  
  152. const toBase64 = file => new Promise((resolve, reject) => {
  153. const reader = new FileReader();
  154. reader.readAsDataURL(file);
  155. reader.onload = () => resolve(reader.result);
  156. reader.onerror = error => reject(error);
  157. });
  158.  
  159. const convertEptaCode = char => {
  160.  
  161.  
  162. var p = document.createElement("P");
  163. var c = document.createTextNode(char)
  164. p.appendChild(c);
  165.  
  166. return p.innerHTML;
  167. }
  168.  
  169.  
  170. var saveBlob = (function () {
  171. var a = document.createElement("a");
  172. document.body.appendChild(a);
  173. a.style = "display: none";
  174. return function (blob, fileName) {
  175. console.log('blob, fileName:', blob, fileName);
  176. var url = window.URL.createObjectURL(blob);
  177. a.href = url;
  178. a.download = fileName;
  179. a.click();
  180. window.URL.revokeObjectURL(url);
  181. };
  182. }());
  183.  
  184.  
  185. function rgb2hex(rgb){
  186. if(rgb.includes("#")){
  187. return rgb;
  188. }
  189.  
  190. rgb = rgb.match(/^rgba?[\s+]?\([\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?/i);
  191. return (rgb && rgb.length === 4) ? "#" +
  192. ("0" + parseInt(rgb[1],10).toString(16)).slice(-2) +
  193. ("0" + parseInt(rgb[2],10).toString(16)).slice(-2) +
  194. ("0" + parseInt(rgb[3],10).toString(16)).slice(-2) : '';
  195. }
  196.  
  197.  
  198.  
  199.  
  200. async function awaitImage(url) {
  201. var cont = await fetch(url);
  202. var buff = await cont.blob();
  203. return readFileAsync(buff);
  204. }
  205.  
  206.  
  207. function readFileAsync(file) {
  208. return new Promise((resolve, reject) => {
  209. let reader = new FileReader();
  210. reader.onload = () => {
  211. resolve(reader.result);
  212. };
  213. reader.onerror = reject;
  214. reader.readAsDataURL(file);
  215. })
  216. };
  217.  
  218. async function base64ToBufferAsync(base64) {
  219. var dataUrl = base64;
  220. var cont = await fetch(dataUrl);
  221. var buff = await cont.arrayBuffer();
  222. return new Uint8Array(buff);
  223. }
  224.  
  225. function getBase64(file) {
  226. return new Promise((resolve, reject) => {
  227. const reader = new FileReader();
  228. reader.readAsDataURL(file);
  229. reader.onload = () => resolve(reader.result);
  230. reader.onerror = error => reject(error);
  231. });
  232. }
  233.  
  234. function getFieldType(f, v){
  235. switch(f){
  236. case 'Tx':
  237. return 'input';
  238. break;
  239. case 'Ch':
  240. return 'select';
  241. break;
  242. case 'Btn':
  243. if(v.checkBox){
  244. return "checkbox";
  245. }
  246. if(v.radioButton){
  247. return "radio";
  248. }
  249. if(v.pushButton){
  250. return "button";
  251. }
  252. console.warn("Unknown form element 1", f);
  253. return "unk";
  254. break;
  255.  
  256. default:
  257. console.log("Unknown form element ", f, "return tx?");
  258. return "Tx";
  259. return f;
  260. break;
  261. }
  262.  
  263. }
  264.  
  265. function searchElementByHtmlId(id){
  266. var f = false;
  267. $.each(viewer.pages, function(i,page){
  268. page.fcanvas.getObjects().forEach((v, i)=>{
  269. if(typeof v.uniq_id!='undefined'){
  270. if(v.uniq_id==id){
  271. f = v;
  272. return f;
  273. }
  274. }
  275. });
  276. });
  277. return f;
  278. }
  279.  
  280.  
  281. function positionHtml(obj, canvas){
  282. var absCoords = canvas.getAbsoluteCoords(obj);
  283. html = $(`[uniq_id=${obj.uniq_id}]`)[0];
  284. console.log(html);
  285.  
  286. html.style.left = (absCoords.left - 1 / 2) + 'px';
  287. html.style.top = (absCoords.top - 1 / 2) + 'px';
  288. }
  289.  
  290.  
  291. function isFloat(n) {
  292. return n === +n && n !== (n|0);
  293. }
  294.  
  295. function isInteger(n) {
  296. return n === +n && n === (n|0);
  297. }
  298.  
  299.  
  300. var updateCanvasState = function(fcanvas) {
  301. // if ((fcanvas.undoStatus == false && fcanvas.redoStatus == false)) {
  302. // var jsonData = fcanvas.toJSON();
  303. // var canvasAsJson = JSON.stringify(jsonData);
  304. // if (fcanvas.currentStateIndex < fcanvas.canvasState.length - 1) {
  305. // var indexToBeInserted = fcanvas.currentStateIndex + 1;
  306. // fcanvas.canvasState[indexToBeInserted] = canvasAsJson;
  307. // var numberOfElementsToRetain = indexToBeInserted + 1;
  308. // fcanvas.canvasState = fcanvas.canvasState.splice(0, numberOfElementsToRetain);
  309. // } else {
  310. // fcanvas.canvasState.push(canvasAsJson);
  311. // }
  312. // fcanvas.currentStateIndex = fcanvas.canvasState.length - 1;
  313. // if((fcanvas.currentStateIndex == fcanvas.canvasState.length - 1) && fcanvas.currentStateIndex != -1) {
  314. // fcanvas.redoButton.disabled = "disabled";
  315. // }
  316. // }
  317. }
  318.  
  319.  
  320. var canvasUndo = function(fcanvas) {
  321. // if (fcanvas.undoFinishedStatus) {
  322. // if (fcanvas.currentStateIndex == -1) {
  323. // fcanvas.undoStatus = false;
  324. // }else if(fcanvas.currentStateIndex===0){
  325.  
  326.  
  327. // } else {
  328. // if (fcanvas.canvasState.length >= 1) {
  329. // fcanvas.undoFinishedStatus = 0;
  330. // if (fcanvas.currentStateIndex != 0) {
  331. // fcanvas.undoStatus = true;
  332. // fcanvas.loadFromJSON(fcanvas.canvasState[fcanvas.currentStateIndex - 1], function() {
  333. // var jsonData = JSON.parse(fcanvas.canvasState[fcanvas.currentStateIndex - 1]);
  334. // fcanvas.renderAll();
  335. // fcanvas.undoStatus = false;
  336. // fcanvas.currentStateIndex -= 1;
  337. // fcanvas.undoButton.removeAttribute("disabled");
  338. // if (fcanvas.currentStateIndex !== fcanvas.canvasState.length - 1) {
  339. // fcanvas.redoButton.removeAttribute('disabled');
  340. // }
  341. // fcanvas.undoFinishedStatus = 1;
  342. // });
  343. // } else if (fcanvas.currentStateIndex == 0) {
  344. // fcanvas.clear();
  345. // fcanvas.undoFinishedStatus = 1;
  346. // fcanvas.undoButton.disabled = "disabled";
  347. // fcanvas.redoButton.removeAttribute('disabled');
  348. // fcanvas.currentStateIndex -= 1;
  349. // }
  350. // }
  351. // }
  352. // }
  353. }
  354.  
  355. var canvasRedo = function(fcanvas) {
  356. // if (fcanvas.redoFinishedStatus) {
  357. // if ((fcanvas.currentStateIndex == fcanvas.canvasState.length - 1) && fcanvas.currentStateIndex != -1) {
  358. // fcanvas.redoButton.disabled = "disabled";
  359. // } else {
  360. // if (fcanvas.canvasState.length > fcanvas.currentStateIndex && fcanvas.canvasState.length != 0) {
  361. // fcanvas.redoFinishedStatus = 0;
  362. // fcanvas.redoStatus = true;
  363. // fcanvas.loadFromJSON(fcanvas.canvasState[fcanvas.currentStateIndex + 1], function() {
  364. // var jsonData = JSON.parse(fcanvas.canvasState[fcanvas.currentStateIndex + 1]);
  365. // fcanvas.renderAll();
  366. // fcanvas.redoStatus = false;
  367. // fcanvas.currentStateIndex += 1;
  368. // if (fcanvas.currentStateIndex != -1) {
  369. // fcanvas.undoButton.removeAttribute('disabled');
  370. // }
  371. // fcanvas.redoFinishedStatus = 1;
  372. // if ((fcanvas.currentStateIndex == fcanvas.canvasState.length - 1) && fcanvas.currentStateIndex != -1) {
  373. // fcanvas.redoButton.disabled = "disabled";
  374. // }
  375. // });
  376. // }
  377. // }
  378. // }
  379. }
  380.  
  381.  
  382.  
  383. function setInputSelection(el, startOffset, endOffset) {
  384. // el.focus();
  385. if (typeof el.selectionStart == "number" && typeof el.selectionEnd == "number") {
  386. el.selectionStart = startOffset;
  387. el.selectionEnd = endOffset;
  388. } else {
  389. var range = el.createTextRange();
  390. var startCharMove = offsetToRangeCharacterMove(el, startOffset);
  391. range.collapse(true);
  392. if (startOffset == endOffset) {
  393. range.move("character", startCharMove);
  394. } else {
  395. range.moveEnd("character", offsetToRangeCharacterMove(el, endOffset));
  396. range.moveStart("character", startCharMove);
  397. }
  398. range.select();
  399. }
  400. }
  401.  
  402.  
  403. function getInputSelection(el) {
  404. var start = 0,
  405. end = 0,
  406. normalizedValue, range,
  407. textInputRange, len, endRange;
  408. if (typeof el.selectionStart == "number" && typeof el.selectionEnd == "number") {
  409. start = el.selectionStart;
  410. end = el.selectionEnd;
  411. } else {
  412. range = document.selection.createRange();
  413. if (range && range.parentElement() == el) {
  414. len = el.value.length;
  415. normalizedValue = el.value.replace(/\r\n/g, "\n");
  416. // Create a working TextRange that lives only in the input
  417. textInputRange = el.createTextRange();
  418. textInputRange.moveToBookmark(range.getBookmark());
  419. // Check if the start and end of the selection are at the very end
  420. // of the input, since moveStart/moveEnd doesn't return what we want
  421. // in those cases
  422. endRange = el.createTextRange();
  423. endRange.collapse(false);
  424. if (textInputRange.compareEndPoints("StartToEnd", endRange) > -1) {
  425. start = end = len;
  426. } else {
  427. start = -textInputRange.moveStart("character", -len);
  428. start += normalizedValue.slice(0, start).split("\n").length - 1;
  429. if (textInputRange.compareEndPoints("EndToEnd", endRange) > -1) {
  430. end = len;
  431. } else {
  432. end = -textInputRange.moveEnd("character", -len);
  433. end += normalizedValue.slice(0, end).split("\n").length - 1;
  434. }
  435. }
  436. }
  437. }
  438. return {
  439. start: start,
  440. end: end
  441. };
  442. }
  443.  
  444. function transformTypedChar(charStr){ //useless
  445. return charStr;
  446. return charStr == "a" ? "z" : charStr;
  447. }
  448.  
  449. function changeTextArea(evt){
  450. if (evt.which) {
  451. var charStr = String.fromCharCode(evt.which);
  452. var transformedChar = transformTypedChar(charStr);
  453. if (transformedChar != charStr) {
  454. var sel = getInputSelection(this),
  455. val = this.value;
  456. this.value = val.slice(0, sel.start) + transformedChar + val.slice(sel.end);
  457. this.value.replace(/a/g, "z");
  458. console.log(this.value);
  459. // Move the caret
  460. setInputSelection(this, sel.start + 1, sel.start + 1);
  461. }
  462. }
  463. }
  464.  
  465. function getFontLoadedName(loaded){
  466. var ret = false;
  467. $.each(viewer.embedFontsNames, function(i,v){
  468. if(v==loaded){
  469. ret = i;
  470. }
  471. });
  472. return ret;
  473. }
  474.  
  475. //used in pdfjs
  476. function uniqFontName(name){
  477. var x_font_name = name.split("+");
  478. if (typeof x_font_name[1] != 'undefined') {
  479. var niceName = x_font_name[1];
  480. } else {
  481. var niceName = x_font_name[0];
  482. }
  483. var ret = niceName.replace(",","_")+"xx"+viewer.font_iterator;
  484. viewer.font_iterator+=1;
  485. return ret;
  486. }
  487.  
  488.  
  489. $.fn.getElementOffset = function (parent, in_mm) {
  490. //this
  491. if (typeof parent === 'undefined' || !parent) {
  492. parent = $(this).closest(".page");
  493. }
  494.  
  495. var bs = 0;
  496. if($(this).hasClass("text_content_element")){
  497. var el = $(this);
  498. var bs1 = parseFloat($(this).height())-parseFloat($(this).css("font-size"));
  499. bs = parseFloat(el.css("line-height"))-parseFloat(el.css("font-size"));
  500. }
  501.  
  502.  
  503. var childPos = this.offset();
  504. var parentPos = this.closest(parent).offset();
  505. var childOffset = {
  506. top: childPos.top - parentPos.top,
  507. topbs: childPos.top - parentPos.top-bs,
  508. left: childPos.left - parentPos.left,
  509. width: this[0].getBoundingClientRect().width,
  510. height: this[0].getBoundingClientRect().height,
  511. }
  512. if (in_mm) {
  513. $.each(childOffset, function (i, v) {
  514. childOffset[i] = px2mm(v);
  515. })
  516. }
  517. return childOffset;
  518. };
  519.  
  520.  
  521. var last_pressed_key = "";
  522.  
  523. function proccessShadingPoints(point0, point1, transform){
  524. var new_point0 = [point0[0], point0[1]];
  525. var new_point1 = [point1[0], point1[1]];
  526. var revert_color = false;
  527.  
  528.  
  529.  
  530. if(transform[0]<=0 && transform[3]<=0){
  531. new_point0 = point1.reverse();
  532. new_point1 = point0.reverse();
  533. }else{
  534. if(transform[0]<=0){
  535. new_point0[0] = point0[1];
  536. new_point0[1] = point0[0];
  537. revert_color = true;
  538. }
  539. if(transform[3]<=0){
  540. new_point1[0] = point1[1];
  541. new_point1[1] = point1[0];
  542. }
  543. }
  544. return [new_point0, new_point1, revert_color];
  545. }
  546.  
  547. function proccessShading(args, that, trans, trans2){
  548. //this
  549. switch (args[0]) {
  550. case 'RadialAxial':
  551. var shadingId = "shading_"+(shading_id++);
  552. var colorStops = args[2];
  553. var gradient = {};
  554.  
  555. var new_points = proccessShadingPoints(args[3], args[4], trans);
  556. var point0 = new_points[0];
  557. var point1 = new_points[1];
  558. switch (args[1]) {
  559. case 'axial':
  560. //поворот с помощью трансформы
  561. if(trans[3]<0){
  562. gradient= {
  563. type: "linear",
  564. gradientUnits: "userSpaceOnUse",
  565. x1: point0[1],
  566. y1: point0[0],
  567. x2: point1[1],
  568. y2: point1[0],
  569. revert_color: new_points[2],
  570. colorStops: {}
  571. };
  572. }else{
  573. gradient= {
  574. type: "linear",
  575. gradientUnits: "userSpaceOnUse",
  576. x1: point0[0],
  577. y1: point0[1],
  578. x2: point1[0],
  579. y2: point1[1],
  580. revert_color: new_points[2],
  581. colorStops: {}
  582. };
  583. }
  584. break;
  585. case 'radial':
  586. var focalPoint = args[3];
  587. var circlePoint = args[4];
  588. var focalRadius = args[5];
  589. var circleRadius = args[6];
  590.  
  591. gradient= {
  592. type: "radial",
  593. id: shadingId,
  594. gradientUnits: "userSpaceOnUse",
  595. x1: point0[0],
  596. y1: point0[1],
  597. x2: point1[0],
  598. y2: point1[1],
  599. r1: 0.5,
  600. r2: 0.6,
  601. colorStops: {}
  602. };
  603. break;
  604. default:
  605. throw new Error("Unknown RadialAxial type: ".concat(args[1]));
  606. }
  607.  
  608. var _iteratorNormalCompletion5 = true;
  609. var _didIteratorError5 = false;
  610. var _iteratorError5 = undefined;
  611.  
  612. try {
  613.  
  614. for (var _iterator5 = colorStops[Symbol.iterator](), _step5; !(_iteratorNormalCompletion5 = (_step5 = _iterator5.next()).done); _iteratorNormalCompletion5 = true) {
  615. var colorStop = _step5.value;
  616. gradient.colorStops[colorStop[0]] = colorStop[1];
  617. }
  618. console.log("grad is ", gradient);
  619. return gradient
  620. } catch (err) {
  621. console.error("gradient error", err);
  622. }
  623.  
  624. return false;
  625.  
  626. case 'Mesh':
  627. console.warn('Unimplemented pattern Mesh');
  628. return null;
  629.  
  630. case 'Dummy':
  631. return 'hotpink';
  632.  
  633. default:
  634. return;
  635. // throw new Error("Unknown IR type: ".concat(args[0]));
  636. }
  637.  
  638.  
  639. }
  640.  
  641.  
  642. function hex2rgb(hex, opacity = 1) {
  643. var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
  644. var result = result ? {
  645. r: parseInt(result[1], 16),
  646. g: parseInt(result[2], 16),
  647. b: parseInt(result[3], 16)
  648. } : null;
  649.  
  650. if(result) return "rgba("+Object.values(result).join(",")+","+opacity+")";
  651. }
  652.  
  653.  
  654.  
  655.  
  656. function proccessTextChunks(glyphs, current, ctx, _util, that){
  657.  
  658. //this
  659. var font = current.font;
  660. var pageNumber = ctx.pageNumber;
  661. var temp_fb = viewer.pages[pageNumber];
  662. var is_type3 = false;
  663.  
  664.  
  665.  
  666. if(typeof temp_fb=='undefined'){
  667. temp_fb = viewer.pages[viewer.now_render];
  668. pageNumber = viewer.now_render;
  669. console.warn("canvas is not defined", temp_fb.fabricText);
  670. }
  671.  
  672. if (typeof temp_fb.fabricText[temp_fb.blockIterator][temp_fb.lineIterator] === 'undefined') {
  673. viewer.pages[pageNumber].fabricText[temp_fb.blockIterator][temp_fb.lineIterator] = [];
  674.  
  675. }
  676.  
  677. if (font.isType3Font) {
  678. is_type3 = true
  679. return that.showType3Text(glyphs);
  680. }
  681.  
  682. var fontSize = current.fontSize;
  683. var fontSizeScale = current.fontSizeScale;
  684. var charSpacing = current.charSpacing;
  685. var wordSpacing = current.wordSpacing;
  686. var fontDirection = current.fontDirection;
  687. var textHScale = current.textHScale * fontDirection;
  688. var glyphsLength = glyphs.length;
  689. var vertical = font.vertical;
  690. var spacingDir = vertical ? 1 : -1;
  691. var defaultVMetrics = font.defaultVMetrics;
  692. var widthAdvanceScale = fontSize * current.fontMatrix[0];
  693. var simpleFillText = current.textRenderingMode === _util.TextRenderingMode.FILL && !font.disableFontFace && !current.patternFill;
  694.  
  695.  
  696. if (fontSize === 0) {
  697. return undefined;
  698. }
  699.  
  700.  
  701. ctx.save();
  702. var patternTransform;
  703.  
  704. //TODO
  705. if (current.patternFill) {
  706. patternFill();
  707. }
  708.  
  709. var ct = (ctx.mozCurrentTransform);
  710. var font_scale = ct[0]; //* tm[0];
  711.  
  712. if(!font_scale){
  713. if(ct[1]!=0){
  714. font_scale = ct[1];
  715. }else{
  716. font_scale = 1;
  717. }
  718. }
  719. ctx.transform.apply(ctx, current.textMatrix);
  720. ctx.translate(current.x, current.y + current.textRise);
  721.  
  722. if (fontDirection > 0) {
  723. ctx.scale(textHScale, -1);
  724. } else {
  725. ctx.scale(textHScale, 1);
  726. }
  727.  
  728.  
  729. var lineWidth = current.lineWidth;
  730. var scale = current.textMatrixScale;
  731.  
  732. if (scale === 0 || lineWidth === 0) {
  733. var fillStrokeMode = current.textRenderingMode & _util.TextRenderingMode.FILL_STROKE_MASK;
  734. if (fillStrokeMode === _util.TextRenderingMode.STROKE || fillStrokeMode === _util.TextRenderingMode.FILL_STROKE) {
  735. that._cachedGetSinglePixelWidth = null;
  736. lineWidth = that.getSinglePixelWidth() * MIN_WIDTH_FACTOR;
  737. }
  738. } else {
  739. lineWidth /= scale;
  740. }
  741. if (fontSizeScale !== 1.0) {
  742. ctx.scale(fontSizeScale, fontSizeScale);
  743. lineWidth /= fontSizeScale;
  744. }
  745. ctx.lineWidth = lineWidth;
  746.  
  747. var x = 0,
  748. i;
  749.  
  750. //======================================
  751. //======================================
  752. //======================================
  753. //======================================
  754.  
  755. var blockWidth = 0;
  756. var blockLeft = false;
  757. var blockTop = false;
  758. var char_offset = false;
  759. var blockWidth = 0;
  760. var prependPosition = 0;
  761. var breakFlag = false;
  762.  
  763.  
  764. // if(temp_fb.blockIterator){
  765. // console.log(font);
  766. // console.log(current);
  767. // alert();
  768. // }
  769. var full_str = "";
  770.  
  771.  
  772. for (i = 0; i < glyphsLength; ++i) {
  773. var glyph = glyphs[i];
  774. if ((0, _util.isNum)(glyph)) {
  775. x += spacingDir * glyph * fontSize / 1000;
  776. if(breakFlag){
  777. blockLeft += (spacingDir * glyph * fontSize / 1000)*current.textMatrixScale;
  778. }
  779. continue;
  780. }
  781. var restoreNeeded = false;
  782. var spacing = (glyph.isSpace ? wordSpacing : 0) + charSpacing;
  783. var character = glyph.fontChar;
  784. var accent = glyph.accent;
  785. var scaledX, scaledY, scaledAccentX, scaledAccentY;
  786. var width = glyph.width;
  787.  
  788. if (vertical) {
  789. var vmetric, vx, vy;
  790. vmetric = glyph.vmetric || defaultVMetrics;
  791. vx = glyph.vmetric ? vmetric[1] : width * 0.5;
  792. vx = -vx * widthAdvanceScale;
  793. vy = vmetric[2] * widthAdvanceScale;
  794. width = vmetric ? -vmetric[0] : width;
  795. scaledX = vx / fontSizeScale;
  796. scaledY = (x + vy) / fontSizeScale;
  797. } else {
  798. scaledX = x / fontSizeScale;
  799. scaledY = 0;
  800. }
  801.  
  802. if (font.remeasure && width > 0) {
  803. var measuredWidth = ctx.measureText(character).width * 1000 / fontSize * fontSizeScale;
  804.  
  805. if (width < measuredWidth && that.isFontSubpixelAAEnabled) {
  806. var characterScaleX = width / measuredWidth;
  807. restoreNeeded = true;
  808. ctx.save();
  809. ctx.scale(characterScaleX, 1);
  810. scaledX /= characterScaleX;
  811. } else if (width !== measuredWidth) {
  812. scaledX += (width - measuredWidth) / 2000 * fontSize / fontSizeScale;
  813. }
  814. // alert("remeasure");
  815. }
  816.  
  817. if (window.debug == 1) {
  818. if (glyph.isInFont || font.missingFile) {
  819.  
  820. if (simpleFillText && !accent) {
  821. ctx.fillText(character, scaledX, scaledY);
  822. } else {
  823. //TODO draw here
  824. //alert("time to draw not simpled text 2");
  825. that.paintChar(character, scaledX, scaledY, patternTransform);
  826.  
  827. if (accent) {
  828. scaledAccentX = scaledX + accent.offset.x / fontSizeScale;
  829. scaledAccentY = scaledY - accent.offset.y / fontSizeScale;
  830. //TODO draw here
  831. that.paintChar(accent.fontChar, scaledAccentX, scaledAccentY, patternTransform);
  832. }
  833. }
  834.  
  835. alert("debug text "+viewer.pages[pageNumber].currentZIndex);
  836. }
  837. }
  838.  
  839. var charWidth = width * widthAdvanceScale + spacing * fontDirection;
  840. x += (charWidth);
  841. var spaceWidth = font.defaultWidth * widthAdvanceScale + spacing * fontDirection;
  842.  
  843. if (restoreNeeded) {
  844. ctx.restore();
  845. }
  846. var cw = (charWidth * current.textMatrixScale) * font_scale;
  847. var original_char_width = cw;
  848. viewer.last_char_width = cw;
  849. blockWidth += cw
  850. var current_transform = (ctx.getTransform());
  851. var fs = (viewer.pages[pageNumber].currentFontSize * current.textMatrixScale) * font_scale;
  852.  
  853. if(blockLeft===false){
  854. var transform = ctx.mozCurrentTransform;
  855. var current_canvas_position = viewer.getCanvasPosition(transform, 0,0);
  856. blockLeft = current_canvas_position[0];
  857. blockTop = current_canvas_position[1]-fs;
  858. if(prependPosition){
  859. blockLeft+=prependPosition;
  860. }
  861. }
  862.  
  863. temp_iterator++;
  864.  
  865.  
  866. if(true){
  867. var char_offset = 0;
  868. if (!scaledX) {
  869. char_offset = blockLeft;
  870. } else {
  871. if (i == 0) {
  872. char_offset = blockLeft;
  873. } else {
  874. char_offset = (blockLeft + (((x * current.textMatrixScale) - (charWidth * current.textMatrixScale)) * font_scale))
  875. }
  876. }
  877. }
  878. var char_scaled_x = ( (((x * current.textMatrixScale) - (charWidth * current.textMatrixScale)) * font_scale));
  879.  
  880. if(breakFlag){
  881. breakFlag = false;
  882. }
  883.  
  884. var rad = Math.atan2(current_transform.b, current_transform.a)
  885. //sp = spaceWidth;
  886. var sp = 278 * widthAdvanceScale + spacing * fontDirection
  887.  
  888. buildCharsTable(font.missingFile ? font.fallbackName : font.loadedName, character, glyph.unicode, original_char_width);
  889.  
  890.  
  891.  
  892. if (blockLeft > 0 && blockTop > 0){
  893. var use_fallback = false;
  894. var fontFamily = font.missingFile ? false : font.loadedName;
  895. if (!fontFamily) {
  896. //console.log(font);
  897. }
  898.  
  899. var x_font_name = font.name.split("+");
  900. if (typeof x_font_name[1] != 'undefined'){
  901. var niceName = x_font_name[1];
  902. }else{
  903. var niceName = x_font_name[0];
  904. }
  905.  
  906. var new_nc = getSystemFont(niceName);
  907. if(new_nc){
  908. fontFamily = new_nc;
  909. character = glyph.unicode
  910. }
  911. var loadedFont = font.name.replace(",", "-").replace("+", "-");
  912. var cy = viewer.pages[pageNumber].line_offset_top - scaledY * fontSizeScale;
  913.  
  914. //TODO бьем строку на несколько блоков, если длинный пробел
  915. if(false && spaceWidth>0 && glyph.isSpace && spaceWidth*2<cw){
  916. //console.log("==break==",temp_fb.blockIterator);
  917. breakFlag = true;
  918. full_str = "";
  919. //char_offset
  920. x = 0;
  921. //alert(blockLeft + " || " +char_offset);
  922. console.log("prevvvvv");
  923. console.log(blockLeft, char_offset, cw,blockWidth,x);
  924. console.log("eeeeeprevv");
  925. blockLeft = char_offset+cw;
  926. char_offset = 0;
  927. blockWidth = 0;
  928. createNewBlock(pageNumber);
  929. var temp_fb = viewer.pages[pageNumber];
  930. current.fillColor = "rgb(255,0,0)";
  931.  
  932. if (vertical) {
  933. current.y -= x * textHScale;
  934. } else {
  935. current.x += x * textHScale;
  936. }
  937.  
  938.  
  939. continue;
  940. }
  941.  
  942. full_str += glyph.unicode;
  943. viewer.pages[pageNumber].fabricText[temp_fb.blockIterator][temp_fb.lineIterator].push({
  944. type3: false,
  945. rad: rad,
  946. charSpacing: charSpacing * fs,
  947. fontSizeScale: fontSizeScale,
  948. blockLeft: blockLeft,
  949. blockTop: (blockTop), //+font.ascent-font.descent+0.5),
  950. blockWidth: blockWidth,
  951. fontSize: fs,
  952. fallbackFont: font.fallbackName,
  953. loadedFont: loadedFont,
  954. font: is_type3?"Arial":fontFamily,
  955. character: is_type3?glyph.unicode:character,
  956. unicode: glyph.unicode,
  957. isSpace: glyph.isSpace,
  958. spacing: spacing,
  959. fontSpaceWidth: spaceWidth,
  960. inFont: glyph.isInFont,
  961. lineBreaker: false,
  962. color: current.fillColor,
  963. current,
  964. charWidth: cw,
  965. spaceWidth: sp,
  966. inBlockLeft: 0,
  967. charLeft: char_offset,
  968. x: x,
  969. y: cy,
  970. });
  971.  
  972. }
  973. prev_char_offset = char_offset;
  974. }
  975.  
  976. //endfor
  977.  
  978. if (vertical) {
  979. current.y -= x * textHScale;
  980. } else {
  981. current.x += x * textHScale;
  982. }
  983. window.prev_block_right = blockLeft + cw;
  984. ctx.restore();
  985. }
  986.  
  987.  
  988.  
  989. function removeFakeSpaces(texts) {
  990. var new_texts = {};
  991. var block_iterator = 0;
  992. var fakeSpaceFactor = 3;
  993.  
  994.  
  995.  
  996. $.each(texts, function(ti, text_block){
  997.  
  998. if(ti!=36){
  999. //return;
  1000. }
  1001. if(typeof new_texts[block_iterator]=='undefined'){
  1002. new_texts[block_iterator] = {zIndex: text_block.zIndex};
  1003. }
  1004. $.each(text_block, function(i, line){
  1005. if(i=='zIndex'){
  1006. return;
  1007. }
  1008. var prepend = 0;
  1009. if(typeof new_texts[block_iterator][i]=='undefined'){
  1010. //new_texts[block_iterator][i] = [];
  1011. }
  1012. try{
  1013.  
  1014. line.forEach(function(char, ci){
  1015. var char_offset = false;
  1016. if(typeof line[ci+1]!='undefined'){
  1017. next_left = line[ci+1].charLeft;
  1018. current_right = line[ci].charLeft+line[ci].charWidth;
  1019. char_offset = parseInt(next_left)-parseInt(current_right);
  1020. }
  1021.  
  1022. if(
  1023. char.isSpace && ((char.fontSpaceWidth>1 && char.fontSpaceWidth*fakeSpaceFactor<char.charWidth))
  1024. || (char_offset && char.charWidth && char.charWidth*fakeSpaceFactor < char_offset)
  1025. ){
  1026. block_iterator++;
  1027. new_texts[block_iterator] = {};
  1028. if(typeof line[ci+1]=='undefined'){
  1029. prepend = 0;
  1030. }else{
  1031. prepend = line[ci+1].charLeft-char.blockLeft;
  1032. }
  1033.  
  1034. }else{
  1035. if(typeof new_texts[block_iterator][i]=='undefined'){
  1036. new_texts[block_iterator][i] = [];
  1037. }
  1038. char.blockLeft += prepend; //recalculate
  1039. char.blockWidth -= prepend;
  1040. new_texts[block_iterator][i].push(char);
  1041. }
  1042. });
  1043. }catch(e){
  1044. console.log(e)
  1045. alert("error 14");
  1046.  
  1047. }
  1048. });
  1049. block_iterator++;
  1050. });
  1051. $.each(new_texts, function(i,v){
  1052. if(!Object.entries(v).length){
  1053. delete new_texts[i];
  1054. }
  1055. });
  1056. return new_texts;
  1057. }
  1058.  
  1059.  
  1060.  
  1061. function isInView(el) {
  1062. const box = el.getBoundingClientRect();
  1063. return box.top < window.innerHeight && box.bottom >= 0;
  1064. }
  1065.  
  1066. function getRangeArray(from=0, to=0){
  1067. return [...[...Array(to-from).keys()].map(i => i+from)]
  1068. }
  1069.  
Add Comment
Please, Sign In to add comment