Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- fabric.Textbox.prototype._wrapText = function (lines, desiredWidth) {
- var wrapped = [], i;
- this.wrapped_positions = [];
- this.isWrapping = true;
- this.wrapped_positions = [];
- let temp_lines = [];
- for (i = 0; i < lines.length; i++) {
- let wraped_lines = this._wrapLine(lines[i], i, desiredWidth);
- temp_lines.push(wraped_lines);
- wrapped = wrapped.concat(wraped_lines);
- }
- let new_positions = [];
- if(this.positions){
- let temp_positions = _.cloneDeep(this.positions);
- for(let i=0; i<temp_lines.length; i++){
- let line = temp_lines[i];
- if(line.length>1 && this.positions[i]){
- let latest_left = 0;
- for(let x=0; x<line.length; x++){
- let strip_len = line[x].length;
- if(x===0){
- new_positions.push(temp_positions[i].splice(0, strip_len+1));
- }else{
- let temp1 = temp_positions[i].splice(0, strip_len+1);
- temp1 = temp1.map(function(el){
- return {left: el.left-latest_left}; //посчитать офсет
- });
- console.log("temp1", temp1);
- new_positions.push(temp1)
- }
- //let last_char_width = new_positions.at(-1).at(-1).left-new_positions.at(-1).at(-2).left;
- // console.log('new_positions', _.cloneDeep(new_positions));
- if(new_positions && new_positions.length && new_positions.at(-1) && new_positions.at(-1).at(-1)) {
- latest_left = new_positions.at(-1).at(-1).left || 0;
- } else {
- latest_left = 0;
- }
- }
- //new_positions.push(temp_positions[i]);
- }else{
- new_positions.push(temp_positions[i]);
- }
- }
- }
- if(new_positions.length){
- this.wrapped_positions = new_positions;
- }
- //console.log("new_positions", new_positions);
- this.isWrapping = false;
- return wrapped;
- }
- fabric.Textbox.prototype._getGraphemeBox = function (grapheme, lineIndex, charIndex, prevGrapheme, skipLeft, skipPositions = false) {
- var style = this.getCompleteStyleDeclaration(lineIndex, charIndex),
- prevStyle = prevGrapheme ? this.getCompleteStyleDeclaration(lineIndex, charIndex - 1) : {},
- info = this._measureChar(grapheme, style, prevGrapheme, prevStyle),
- kernedWidth = info.kernedWidth,
- width = info.width, charSpacing;
- if (this.charSpacing !== 0) {
- charSpacing = this._getWidthOfCharSpacing();
- width += charSpacing;
- kernedWidth += charSpacing;
- }
- var box = {
- width: width,
- left: 0,
- height: style.fontSize,
- kernedWidth: kernedWidth,
- deltaY: style.deltaY,
- };
- if (charIndex > 0 && !skipLeft) {
- var previousBox = this.__charBounds[lineIndex][charIndex - 1];
- box.left = previousBox.left + previousBox.width + info.kernedWidth - info.width;
- }
- //TODO замена ширины символа. необходимо для кастомного чарспейса.
- if (!skipPositions) {
- let positions = this.positions;
- if(this.wrapped_positions){
- positions = this.wrapped_positions;
- }
- //console.log("use positions?", positions);
- if(typeof positions[lineIndex] != 'undefined'){
- if (typeof positions[lineIndex][charIndex + 1] != 'undefined') {
- var next_left = positions[lineIndex][charIndex + 1].left;
- var current_left = positions[lineIndex][charIndex].left;
- box.width = next_left - current_left;
- box.kernedWidth = box.width
- if (box.width < 0) {
- box.width = 5;
- }
- }
- }
- }
- return box;
- }
- fabric.Textbox.prototype._renderChars = function (method, ctx, line, left, top, lineIndex) {
- // set proper line offset
- var lineHeight = this.getHeightOfLine(lineIndex),
- isJustify = this.textAlign.indexOf('justify') !== -1,
- actualStyle,
- nextStyle,
- charsToRender = '',
- charBox,
- boxWidth = 0,
- timeToRender,
- path = this.path,
- blockLeft = left,
- shortCut = !isJustify && this.charSpacing === 0 && this.isEmptyStyles(lineIndex) && !path,
- isLtr = this.direction === 'ltr', sign = this.direction === 'ltr' ? 1 : -1,
- drawingLeft, currentDirection = ctx.canvas.getAttribute('dir');
- ctx.save();
- if (currentDirection !== this.direction) {
- ctx.canvas.setAttribute('dir', isLtr ? 'ltr' : 'rtl');
- ctx.direction = isLtr ? 'ltr' : 'rtl';
- ctx.textAlign = isLtr ? 'left' : 'right';
- }
- top -= lineHeight * this._fontSizeFraction / this.lineHeight;
- if (shortCut) {
- // render all the line in one pass without checking
- // drawingLeft = isLtr ? left : left - this.getLineWidth(lineIndex);
- this._renderChar(method, ctx, lineIndex, 0, line.join(''), left, top, lineHeight);
- ctx.restore();
- return;
- }
- for (var i = 0, len = line.length - 1; i <= len; i++) {
- timeToRender = i === len || this.charSpacing || path;
- charsToRender += line[i];
- charBox = this.__charBounds[lineIndex][i];
- if (boxWidth === 0) {
- left += sign * (charBox.kernedWidth - charBox.width);
- boxWidth += charBox.width;
- }
- else {
- boxWidth += charBox.kernedWidth;
- }
- if (isJustify && !timeToRender) {
- if (this._reSpaceAndTab.test(line[i])) {
- timeToRender = true;
- }
- }
- let positions = this.positions;
- if(this.wrapped_positions){
- positions = this.wrapped_positions;
- }
- //console.log("use another positions", positions);
- if (typeof positions[lineIndex] != 'undefined' && typeof positions[lineIndex][i] != 'undefined') {
- left = blockLeft + positions[lineIndex][i].left;
- }
- if (!timeToRender) {
- // if we have charSpacing, we render char by char
- actualStyle = actualStyle || this.getCompleteStyleDeclaration(lineIndex, i);
- nextStyle = this.getCompleteStyleDeclaration(lineIndex, i + 1);
- timeToRender = this._hasStyleChanged(actualStyle, nextStyle);
- }
- if (timeToRender) {
- if (path) {
- ctx.save();
- ctx.translate(charBox.renderLeft, charBox.renderTop);
- ctx.rotate(charBox.angle);
- this._renderChar(method, ctx, lineIndex, i, charsToRender, -boxWidth / 2, 0, lineHeight);
- ctx.restore();
- }
- else {
- drawingLeft = left;
- this._renderChar(method, ctx, lineIndex, i, charsToRender, drawingLeft, top, lineHeight);
- }
- charsToRender = '';
- actualStyle = nextStyle;
- left += sign * boxWidth;
- boxWidth = 0;
- }
- }
- ctx.restore();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement