Advertisement
KoctrX

Untitled

Apr 10th, 2023
599
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. fabric.Textbox.prototype._wrapText = function (lines, desiredWidth) {
  2.   var wrapped = [], i;
  3.   this.wrapped_positions = [];
  4.   this.isWrapping = true;
  5.  
  6.   this.wrapped_positions = [];
  7.  
  8.   let temp_lines = [];
  9.   for (i = 0; i < lines.length; i++) {
  10.     let wraped_lines = this._wrapLine(lines[i], i, desiredWidth);
  11.     temp_lines.push(wraped_lines);
  12.     wrapped = wrapped.concat(wraped_lines);
  13.   }
  14.   let new_positions = [];
  15.  
  16.   if(this.positions){
  17.     let temp_positions = _.cloneDeep(this.positions);
  18.     for(let i=0; i<temp_lines.length; i++){
  19.       let line = temp_lines[i];
  20.       if(line.length>1 && this.positions[i]){
  21.         let latest_left = 0;
  22.  
  23.         for(let x=0; x<line.length; x++){
  24.           let strip_len = line[x].length;        
  25.                    
  26.           if(x===0){
  27.             new_positions.push(temp_positions[i].splice(0, strip_len+1));
  28.           }else{
  29.             let temp1 = temp_positions[i].splice(0, strip_len+1);
  30.             temp1 = temp1.map(function(el){
  31.                             return {left: el.left-latest_left}; //посчитать офсет
  32.                         });
  33.             console.log("temp1", temp1);
  34.             new_positions.push(temp1)
  35.           }
  36.  
  37.           //let last_char_width = new_positions.at(-1).at(-1).left-new_positions.at(-1).at(-2).left;
  38.           // console.log('new_positions', _.cloneDeep(new_positions));
  39.           if(new_positions && new_positions.length && new_positions.at(-1) && new_positions.at(-1).at(-1)) {
  40.             latest_left = new_positions.at(-1).at(-1).left || 0;
  41.           } else {
  42.             latest_left = 0;
  43.           }
  44.         }
  45.          //new_positions.push(temp_positions[i]);
  46.       }else{
  47.         new_positions.push(temp_positions[i]);
  48.       }
  49.     }
  50.   }
  51.   if(new_positions.length){
  52.     this.wrapped_positions = new_positions;
  53.   }
  54.      
  55.   //console.log("new_positions", new_positions);
  56.   this.isWrapping = false;
  57.   return wrapped;
  58. }
  59. fabric.Textbox.prototype._getGraphemeBox =  function (grapheme, lineIndex, charIndex, prevGrapheme, skipLeft, skipPositions = false) {
  60.   var style = this.getCompleteStyleDeclaration(lineIndex, charIndex),
  61.       prevStyle = prevGrapheme ? this.getCompleteStyleDeclaration(lineIndex, charIndex - 1) : {},
  62.       info = this._measureChar(grapheme, style, prevGrapheme, prevStyle),
  63.       kernedWidth = info.kernedWidth,
  64.       width = info.width, charSpacing;
  65.  
  66.   if (this.charSpacing !== 0) {
  67.     charSpacing = this._getWidthOfCharSpacing();
  68.     width += charSpacing;
  69.     kernedWidth += charSpacing;
  70.   }
  71.  
  72.   var box = {
  73.     width: width,
  74.     left: 0,
  75.     height: style.fontSize,
  76.     kernedWidth: kernedWidth,
  77.     deltaY: style.deltaY,
  78.   };
  79.   if (charIndex > 0 && !skipLeft) {
  80.     var previousBox = this.__charBounds[lineIndex][charIndex - 1];
  81.     box.left = previousBox.left + previousBox.width + info.kernedWidth - info.width;
  82.   }
  83.  
  84.   //TODO замена ширины символа. необходимо для кастомного чарспейса.
  85.   if (!skipPositions) {
  86.     let positions = this.positions;
  87.     if(this.wrapped_positions){
  88.       positions = this.wrapped_positions;
  89.     }
  90.     //console.log("use positions?", positions);
  91.  
  92.     if(typeof positions[lineIndex] != 'undefined'){
  93.       if (typeof positions[lineIndex][charIndex + 1] != 'undefined') {
  94.         var next_left = positions[lineIndex][charIndex + 1].left;
  95.         var current_left = positions[lineIndex][charIndex].left;
  96.         box.width = next_left - current_left;
  97.         box.kernedWidth = box.width
  98.  
  99.         if (box.width < 0) {
  100.           box.width = 5;
  101.         }
  102.       }
  103.     }
  104.   }
  105.  
  106.   return box;
  107. }
  108.  
  109. fabric.Textbox.prototype._renderChars =  function (method, ctx, line, left, top, lineIndex) {
  110.   // set proper line offset
  111.   var lineHeight = this.getHeightOfLine(lineIndex),
  112.       isJustify = this.textAlign.indexOf('justify') !== -1,
  113.       actualStyle,
  114.       nextStyle,
  115.       charsToRender = '',
  116.       charBox,
  117.       boxWidth = 0,
  118.       timeToRender,
  119.       path = this.path,
  120.       blockLeft = left,
  121.       shortCut = !isJustify && this.charSpacing === 0 && this.isEmptyStyles(lineIndex) && !path,
  122.       isLtr = this.direction === 'ltr', sign = this.direction === 'ltr' ? 1 : -1,
  123.       drawingLeft, currentDirection = ctx.canvas.getAttribute('dir');
  124.   ctx.save();
  125.   if (currentDirection !== this.direction) {
  126.     ctx.canvas.setAttribute('dir', isLtr ? 'ltr' : 'rtl');
  127.     ctx.direction = isLtr ? 'ltr' : 'rtl';
  128.     ctx.textAlign = isLtr ? 'left' : 'right';
  129.   }
  130.   top -= lineHeight * this._fontSizeFraction / this.lineHeight;
  131.   if (shortCut) {
  132.     // render all the line in one pass without checking
  133.     // drawingLeft = isLtr ? left : left - this.getLineWidth(lineIndex);
  134.     this._renderChar(method, ctx, lineIndex, 0, line.join(''), left, top, lineHeight);
  135.     ctx.restore();
  136.     return;
  137.   }
  138.   for (var i = 0, len = line.length - 1; i <= len; i++) {
  139.     timeToRender = i === len || this.charSpacing || path;
  140.     charsToRender += line[i];
  141.     charBox = this.__charBounds[lineIndex][i];
  142.     if (boxWidth === 0) {
  143.       left += sign * (charBox.kernedWidth - charBox.width);
  144.       boxWidth += charBox.width;
  145.     }
  146.     else {
  147.       boxWidth += charBox.kernedWidth;
  148.     }
  149.     if (isJustify && !timeToRender) {
  150.       if (this._reSpaceAndTab.test(line[i])) {
  151.         timeToRender = true;
  152.       }
  153.     }
  154.  
  155.     let positions = this.positions;
  156.     if(this.wrapped_positions){
  157.       positions = this.wrapped_positions;
  158.     }
  159.     //console.log("use another positions", positions);
  160.  
  161.     if (typeof positions[lineIndex] != 'undefined' && typeof positions[lineIndex][i] != 'undefined') {
  162.       left = blockLeft + positions[lineIndex][i].left;
  163.     }
  164.  
  165.     if (!timeToRender) {
  166.       // if we have charSpacing, we render char by char
  167.       actualStyle = actualStyle || this.getCompleteStyleDeclaration(lineIndex, i);
  168.       nextStyle = this.getCompleteStyleDeclaration(lineIndex, i + 1);
  169.       timeToRender = this._hasStyleChanged(actualStyle, nextStyle);
  170.     }
  171.     if (timeToRender) {
  172.       if (path) {
  173.         ctx.save();
  174.         ctx.translate(charBox.renderLeft, charBox.renderTop);
  175.         ctx.rotate(charBox.angle);
  176.         this._renderChar(method, ctx, lineIndex, i, charsToRender, -boxWidth / 2, 0, lineHeight);
  177.         ctx.restore();
  178.       }
  179.       else {
  180.         drawingLeft = left;
  181.         this._renderChar(method, ctx, lineIndex, i, charsToRender, drawingLeft, top, lineHeight);
  182.       }
  183.       charsToRender = '';
  184.       actualStyle = nextStyle;
  185.       left += sign * boxWidth;
  186.       boxWidth = 0;
  187.     }
  188.   }
  189.   ctx.restore();
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement