2015-06-19 03:06:32 +00:00
|
|
|
(function(global) {
|
2015-06-16 20:54:03 +00:00
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
var fabric = global.fabric || (global.fabric = {}),
|
|
|
|
|
clone = fabric.util.object.clone;
|
2015-05-27 18:47:13 +00:00
|
|
|
|
2015-05-27 20:59:56 +00:00
|
|
|
/**
|
|
|
|
|
* Textbox class, based on IText, allows the user to resize the text rectangle
|
|
|
|
|
* and wraps lines automatically. Textboxes have their Y scaling locked, the
|
|
|
|
|
* user can only change width. Height is adjusted automatically based on the
|
|
|
|
|
* wrapping of lines.
|
|
|
|
|
* @class fabric.Textbox
|
|
|
|
|
* @extends fabric.IText
|
|
|
|
|
* @mixes fabric.Observable
|
|
|
|
|
* @return {fabric.Textbox} thisArg
|
|
|
|
|
* @see {@link fabric.Textbox#initialize} for constructor definition
|
|
|
|
|
*/
|
|
|
|
|
fabric.Textbox = fabric.util.createClass(fabric.IText, fabric.Observable, {
|
2016-05-08 19:22:51 +00:00
|
|
|
|
2015-05-27 18:47:13 +00:00
|
|
|
/**
|
2015-05-27 20:59:56 +00:00
|
|
|
* Type of an object
|
|
|
|
|
* @type String
|
|
|
|
|
* @default
|
|
|
|
|
*/
|
|
|
|
|
type: 'textbox',
|
2016-05-08 19:22:51 +00:00
|
|
|
|
2015-05-27 20:59:56 +00:00
|
|
|
/**
|
|
|
|
|
* Minimum width of textbox, in pixels.
|
|
|
|
|
* @type Number
|
|
|
|
|
* @default
|
|
|
|
|
*/
|
|
|
|
|
minWidth: 20,
|
2016-05-08 19:22:51 +00:00
|
|
|
|
2015-06-12 03:00:23 +00:00
|
|
|
/**
|
|
|
|
|
* Minimum calculated width of a textbox, in pixels.
|
|
|
|
|
* @type Number
|
|
|
|
|
* @default
|
|
|
|
|
*/
|
|
|
|
|
dynamicMinWidth: 0,
|
2016-05-08 19:22:51 +00:00
|
|
|
|
2015-05-27 20:59:56 +00:00
|
|
|
/**
|
|
|
|
|
* Cached array of text wrapping.
|
|
|
|
|
* @type Array
|
|
|
|
|
*/
|
|
|
|
|
__cachedLines: null,
|
2016-05-08 19:22:51 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Override standard Object class values
|
|
|
|
|
*/
|
|
|
|
|
lockScalingY: true,
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Override standard Object class values
|
|
|
|
|
*/
|
|
|
|
|
lockScalingFlip: true,
|
|
|
|
|
|
2015-05-27 20:59:56 +00:00
|
|
|
/**
|
|
|
|
|
* Constructor. Some scaling related property values are forced. Visibility
|
|
|
|
|
* of controls is also fixed; only the rotation and width controls are
|
|
|
|
|
* made available.
|
|
|
|
|
* @param {String} text Text string
|
|
|
|
|
* @param {Object} [options] Options object
|
2015-05-27 18:47:13 +00:00
|
|
|
* @return {fabric.Textbox} thisArg
|
|
|
|
|
*/
|
2015-06-19 03:06:32 +00:00
|
|
|
initialize: function(text, options) {
|
2015-05-27 20:59:56 +00:00
|
|
|
this.ctx = fabric.util.createCanvasElement().getContext('2d');
|
|
|
|
|
this.callSuper('initialize', text, options);
|
|
|
|
|
this.setControlsVisibility(fabric.Textbox.getTextboxControlVisibility());
|
2015-05-27 18:47:13 +00:00
|
|
|
|
2015-05-27 20:59:56 +00:00
|
|
|
// add width to this list of props that effect line wrapping.
|
|
|
|
|
this._dimensionAffectingProps.width = true;
|
|
|
|
|
},
|
2015-06-12 03:00:23 +00:00
|
|
|
|
2015-05-27 20:59:56 +00:00
|
|
|
/**
|
|
|
|
|
* Unlike superclass's version of this function, Textbox does not update
|
|
|
|
|
* its width.
|
|
|
|
|
* @param {CanvasRenderingContext2D} ctx Context to use for measurements
|
|
|
|
|
* @private
|
|
|
|
|
* @override
|
|
|
|
|
*/
|
2015-06-19 03:06:32 +00:00
|
|
|
_initDimensions: function(ctx) {
|
2015-05-27 20:59:56 +00:00
|
|
|
if (this.__skipDimension) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2015-05-27 18:47:13 +00:00
|
|
|
|
2015-05-27 20:59:56 +00:00
|
|
|
if (!ctx) {
|
|
|
|
|
ctx = fabric.util.createCanvasElement().getContext('2d');
|
|
|
|
|
this._setTextStyles(ctx);
|
|
|
|
|
}
|
2015-06-12 03:00:23 +00:00
|
|
|
|
|
|
|
|
// clear dynamicMinWidth as it will be different after we re-wrap line
|
|
|
|
|
this.dynamicMinWidth = 0;
|
|
|
|
|
|
|
|
|
|
// wrap lines
|
2015-05-27 20:59:56 +00:00
|
|
|
this._textLines = this._splitTextIntoLines();
|
2015-06-12 03:00:23 +00:00
|
|
|
// if after wrapping, the width is smaller than dynamicMinWidth, change the width and re-wrap
|
2015-06-16 20:19:41 +00:00
|
|
|
if (this.dynamicMinWidth > this.width) {
|
2015-06-12 03:00:23 +00:00
|
|
|
this._set('width', this.dynamicMinWidth);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// clear cache and re-calculate height
|
2015-05-27 20:59:56 +00:00
|
|
|
this._clearCache();
|
|
|
|
|
this.height = this._getTextHeight(ctx);
|
|
|
|
|
},
|
2015-06-11 00:12:54 +00:00
|
|
|
|
2015-07-17 09:40:54 +00:00
|
|
|
/**
|
2015-07-20 17:56:26 +00:00
|
|
|
* Generate an object that translates the style object so that it is
|
|
|
|
|
* broken up by visual lines (new lines and automatic wrapping).
|
|
|
|
|
* The original text styles object is broken up by actual lines (new lines only),
|
|
|
|
|
* which is only sufficient for Text / IText
|
2015-07-17 09:40:54 +00:00
|
|
|
* @private
|
|
|
|
|
*/
|
2015-06-19 03:06:32 +00:00
|
|
|
_generateStyleMap: function() {
|
2015-06-12 03:00:23 +00:00
|
|
|
var realLineCount = 0,
|
2015-06-11 17:25:43 +00:00
|
|
|
realLineCharCount = 0,
|
2015-06-12 03:00:23 +00:00
|
|
|
charCount = 0,
|
|
|
|
|
map = {};
|
2015-06-11 00:12:54 +00:00
|
|
|
|
2015-06-11 17:25:43 +00:00
|
|
|
for (var i = 0; i < this._textLines.length; i++) {
|
|
|
|
|
if (this.text[charCount] === '\n') {
|
2015-06-11 00:12:54 +00:00
|
|
|
realLineCharCount = 0;
|
|
|
|
|
charCount++;
|
|
|
|
|
realLineCount++;
|
|
|
|
|
}
|
2015-06-12 17:26:36 +00:00
|
|
|
else if (this.text[charCount] === ' ') {
|
2015-06-16 20:19:41 +00:00
|
|
|
// this case deals with space's that are removed from end of lines when wrapping
|
|
|
|
|
realLineCharCount++;
|
|
|
|
|
charCount++;
|
2015-06-12 17:26:36 +00:00
|
|
|
}
|
2015-06-11 00:12:54 +00:00
|
|
|
|
2015-06-16 20:54:03 +00:00
|
|
|
map[i] = { line: realLineCount, offset: realLineCharCount };
|
2015-06-11 00:12:54 +00:00
|
|
|
|
|
|
|
|
charCount += this._textLines[i].length;
|
|
|
|
|
realLineCharCount += this._textLines[i].length;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return map;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param {Number} lineIndex
|
|
|
|
|
* @param {Number} charIndex
|
|
|
|
|
* @param {Boolean} [returnCloneOrEmpty=false]
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
2015-06-19 03:06:32 +00:00
|
|
|
_getStyleDeclaration: function(lineIndex, charIndex, returnCloneOrEmpty) {
|
2015-06-12 03:00:23 +00:00
|
|
|
if (this._styleMap) {
|
2015-06-11 00:12:54 +00:00
|
|
|
var map = this._styleMap[lineIndex];
|
2015-08-20 13:55:44 +00:00
|
|
|
if (!map) {
|
|
|
|
|
return returnCloneOrEmpty ? { } : null;
|
|
|
|
|
}
|
2015-06-11 00:12:54 +00:00
|
|
|
lineIndex = map.line;
|
|
|
|
|
charIndex = map.offset + charIndex;
|
|
|
|
|
}
|
2015-07-28 17:20:14 +00:00
|
|
|
return this.callSuper('_getStyleDeclaration', lineIndex, charIndex, returnCloneOrEmpty);
|
2015-06-11 00:12:54 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param {Number} lineIndex
|
|
|
|
|
* @param {Number} charIndex
|
|
|
|
|
* @param {Object} style
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
2015-06-19 03:06:32 +00:00
|
|
|
_setStyleDeclaration: function(lineIndex, charIndex, style) {
|
2015-06-11 00:12:54 +00:00
|
|
|
var map = this._styleMap[lineIndex];
|
|
|
|
|
lineIndex = map.line;
|
|
|
|
|
charIndex = map.offset + charIndex;
|
|
|
|
|
|
|
|
|
|
this.styles[lineIndex][charIndex] = style;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param {Number} lineIndex
|
|
|
|
|
* @param {Number} charIndex
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
2015-06-19 03:06:32 +00:00
|
|
|
_deleteStyleDeclaration: function(lineIndex, charIndex) {
|
2015-06-11 00:12:54 +00:00
|
|
|
var map = this._styleMap[lineIndex];
|
|
|
|
|
lineIndex = map.line;
|
|
|
|
|
charIndex = map.offset + charIndex;
|
|
|
|
|
|
|
|
|
|
delete this.styles[lineIndex][charIndex];
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param {Number} lineIndex
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
2015-06-19 03:06:32 +00:00
|
|
|
_getLineStyle: function(lineIndex) {
|
2015-06-11 00:12:54 +00:00
|
|
|
var map = this._styleMap[lineIndex];
|
|
|
|
|
return this.styles[map.line];
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param {Number} lineIndex
|
|
|
|
|
* @param {Object} style
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
2015-06-19 03:06:32 +00:00
|
|
|
_setLineStyle: function(lineIndex, style) {
|
2015-06-11 00:12:54 +00:00
|
|
|
var map = this._styleMap[lineIndex];
|
|
|
|
|
this.styles[map.line] = style;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param {Number} lineIndex
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
2015-06-19 03:06:32 +00:00
|
|
|
_deleteLineStyle: function(lineIndex) {
|
2015-06-11 00:12:54 +00:00
|
|
|
var map = this._styleMap[lineIndex];
|
|
|
|
|
delete this.styles[map.line];
|
|
|
|
|
},
|
|
|
|
|
|
2015-05-27 20:59:56 +00:00
|
|
|
/**
|
|
|
|
|
* Wraps text using the 'width' property of Textbox. First this function
|
|
|
|
|
* splits text on newlines, so we preserve newlines entered by the user.
|
|
|
|
|
* Then it wraps each line using the width of the Textbox by calling
|
|
|
|
|
* _wrapLine().
|
|
|
|
|
* @param {CanvasRenderingContext2D} ctx Context to use for measurements
|
|
|
|
|
* @param {String} text The string of text that is split into lines
|
|
|
|
|
* @returns {Array} Array of lines
|
|
|
|
|
*/
|
2015-06-19 03:06:32 +00:00
|
|
|
_wrapText: function(ctx, text) {
|
2015-06-11 00:12:54 +00:00
|
|
|
var lines = text.split(this._reNewline), wrapped = [], i;
|
2015-05-27 18:47:13 +00:00
|
|
|
|
2015-05-27 20:59:56 +00:00
|
|
|
for (i = 0; i < lines.length; i++) {
|
2015-06-11 00:12:54 +00:00
|
|
|
wrapped = wrapped.concat(this._wrapLine(ctx, lines[i], i));
|
2015-05-27 20:59:56 +00:00
|
|
|
}
|
2015-05-27 18:47:13 +00:00
|
|
|
|
2015-05-27 20:59:56 +00:00
|
|
|
return wrapped;
|
|
|
|
|
},
|
2015-06-03 23:39:10 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Helper function to measure a string of text, given its lineIndex and charIndex offset
|
|
|
|
|
*
|
|
|
|
|
* @param {CanvasRenderingContext2D} ctx
|
|
|
|
|
* @param {String} text
|
|
|
|
|
* @param {number} lineIndex
|
|
|
|
|
* @param {number} charOffset
|
|
|
|
|
* @returns {number}
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
2015-06-19 03:06:32 +00:00
|
|
|
_measureText: function(ctx, text, lineIndex, charOffset) {
|
2015-08-18 08:45:14 +00:00
|
|
|
var width = 0;
|
2015-06-03 23:39:10 +00:00
|
|
|
charOffset = charOffset || 0;
|
2015-08-18 08:45:14 +00:00
|
|
|
for (var i = 0, len = text.length; i < len; i++) {
|
|
|
|
|
width += this._getWidthOfChar(ctx, text[i], lineIndex, i + charOffset);
|
2015-06-03 23:39:10 +00:00
|
|
|
}
|
|
|
|
|
return width;
|
|
|
|
|
},
|
|
|
|
|
|
2015-05-27 20:59:56 +00:00
|
|
|
/**
|
|
|
|
|
* Wraps a line of text using the width of the Textbox and a context.
|
|
|
|
|
* @param {CanvasRenderingContext2D} ctx Context to use for measurements
|
|
|
|
|
* @param {String} text The string of text to split into lines
|
2015-06-11 00:12:54 +00:00
|
|
|
* @param {Number} lineIndex
|
2015-05-27 20:59:56 +00:00
|
|
|
* @returns {Array} Array of line(s) into which the given text is wrapped
|
|
|
|
|
* to.
|
|
|
|
|
*/
|
2015-06-19 03:06:32 +00:00
|
|
|
_wrapLine: function(ctx, text, lineIndex) {
|
2015-08-11 01:12:38 +00:00
|
|
|
var lineWidth = 0,
|
|
|
|
|
lines = [],
|
2015-06-16 20:19:41 +00:00
|
|
|
line = '',
|
|
|
|
|
words = text.split(' '),
|
2015-08-11 01:12:38 +00:00
|
|
|
word = '',
|
2015-06-16 20:19:41 +00:00
|
|
|
offset = 0,
|
2015-08-11 01:12:38 +00:00
|
|
|
infix = ' ',
|
2015-06-16 20:19:41 +00:00
|
|
|
wordWidth = 0,
|
2015-08-11 01:12:38 +00:00
|
|
|
infixWidth = 0,
|
2016-03-26 10:41:49 +00:00
|
|
|
largestWordWidth = 0,
|
2016-08-14 21:19:42 +00:00
|
|
|
lineJustStarted = true,
|
|
|
|
|
additionalSpace = this._getWidthOfCharSpacing();
|
2015-06-16 20:19:41 +00:00
|
|
|
|
2015-08-11 01:12:38 +00:00
|
|
|
for (var i = 0; i < words.length; i++) {
|
2015-08-21 09:42:14 +00:00
|
|
|
word = words[i];
|
2015-08-11 01:12:38 +00:00
|
|
|
wordWidth = this._measureText(ctx, word, lineIndex, offset);
|
2016-03-26 10:41:49 +00:00
|
|
|
|
2015-08-11 01:12:38 +00:00
|
|
|
offset += word.length;
|
2015-06-16 20:19:41 +00:00
|
|
|
|
2016-08-14 21:19:42 +00:00
|
|
|
lineWidth += infixWidth + wordWidth - additionalSpace;
|
2015-08-11 01:12:38 +00:00
|
|
|
|
2016-03-26 10:41:49 +00:00
|
|
|
if (lineWidth >= this.width && !lineJustStarted) {
|
2015-06-16 20:19:41 +00:00
|
|
|
lines.push(line);
|
|
|
|
|
line = '';
|
2015-08-11 01:12:38 +00:00
|
|
|
lineWidth = wordWidth;
|
2016-03-26 10:41:49 +00:00
|
|
|
lineJustStarted = true;
|
2015-06-12 03:00:23 +00:00
|
|
|
}
|
2016-08-14 21:19:42 +00:00
|
|
|
else {
|
|
|
|
|
lineWidth += additionalSpace;
|
|
|
|
|
}
|
2015-06-12 03:00:23 +00:00
|
|
|
|
2016-03-26 10:41:49 +00:00
|
|
|
if (!lineJustStarted) {
|
2015-08-11 01:12:38 +00:00
|
|
|
line += infix;
|
2015-05-27 20:59:56 +00:00
|
|
|
}
|
2015-08-11 01:12:38 +00:00
|
|
|
line += word;
|
|
|
|
|
|
2016-09-05 16:26:15 +00:00
|
|
|
infixWidth = this._measureText(ctx, infix, lineIndex, offset);
|
2015-08-11 01:12:38 +00:00
|
|
|
offset++;
|
2016-03-26 10:41:49 +00:00
|
|
|
lineJustStarted = false;
|
2015-06-16 20:19:41 +00:00
|
|
|
// keep track of largest word
|
|
|
|
|
if (wordWidth > largestWordWidth) {
|
|
|
|
|
largestWordWidth = wordWidth;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-11 01:12:38 +00:00
|
|
|
i && lines.push(line);
|
|
|
|
|
|
2015-06-16 20:19:41 +00:00
|
|
|
if (largestWordWidth > this.dynamicMinWidth) {
|
2016-08-14 21:19:42 +00:00
|
|
|
this.dynamicMinWidth = largestWordWidth - additionalSpace;
|
2015-05-27 20:59:56 +00:00
|
|
|
}
|
2015-05-27 18:47:13 +00:00
|
|
|
|
2015-05-27 20:59:56 +00:00
|
|
|
return lines;
|
|
|
|
|
},
|
|
|
|
|
/**
|
|
|
|
|
* Gets lines of text to render in the Textbox. This function calculates
|
|
|
|
|
* text wrapping on the fly everytime it is called.
|
|
|
|
|
* @returns {Array} Array of lines in the Textbox.
|
|
|
|
|
* @override
|
|
|
|
|
*/
|
2015-06-19 03:06:32 +00:00
|
|
|
_splitTextIntoLines: function() {
|
2015-08-20 15:04:05 +00:00
|
|
|
var originalAlign = this.textAlign;
|
2015-05-27 20:59:56 +00:00
|
|
|
this.ctx.save();
|
|
|
|
|
this._setTextStyles(this.ctx);
|
2015-08-20 15:04:05 +00:00
|
|
|
this.textAlign = 'left';
|
2015-06-03 23:39:10 +00:00
|
|
|
var lines = this._wrapText(this.ctx, this.text);
|
2015-08-20 15:04:05 +00:00
|
|
|
this.textAlign = originalAlign;
|
2015-05-27 20:59:56 +00:00
|
|
|
this.ctx.restore();
|
2015-07-17 09:40:54 +00:00
|
|
|
this._textLines = lines;
|
|
|
|
|
this._styleMap = this._generateStyleMap();
|
2015-05-27 20:59:56 +00:00
|
|
|
return lines;
|
|
|
|
|
},
|
2015-05-27 18:47:13 +00:00
|
|
|
|
2015-05-27 20:59:56 +00:00
|
|
|
/**
|
|
|
|
|
* When part of a group, we don't want the Textbox's scale to increase if
|
|
|
|
|
* the group's increases. That's why we reduce the scale of the Textbox by
|
|
|
|
|
* the amount that the group's increases. This is to maintain the effective
|
|
|
|
|
* scale of the Textbox at 1, so that font-size values make sense. Otherwise
|
|
|
|
|
* the same font-size value would result in different actual size depending
|
|
|
|
|
* on the value of the scale.
|
|
|
|
|
* @param {String} key
|
2016-08-20 10:05:19 +00:00
|
|
|
* @param {*} value
|
2015-05-27 20:59:56 +00:00
|
|
|
*/
|
2015-06-19 03:06:32 +00:00
|
|
|
setOnGroup: function(key, value) {
|
2015-05-27 20:59:56 +00:00
|
|
|
if (key === 'scaleX') {
|
2015-05-31 22:44:44 +00:00
|
|
|
this.set('scaleX', Math.abs(1 / value));
|
2015-05-27 20:59:56 +00:00
|
|
|
this.set('width', (this.get('width') * value) /
|
|
|
|
|
(typeof this.__oldScaleX === 'undefined' ? 1 : this.__oldScaleX));
|
|
|
|
|
this.__oldScaleX = value;
|
|
|
|
|
}
|
|
|
|
|
},
|
2015-05-27 18:47:13 +00:00
|
|
|
|
2015-05-27 20:59:56 +00:00
|
|
|
/**
|
|
|
|
|
* Returns 2d representation (lineIndex and charIndex) of cursor (or selection start).
|
|
|
|
|
* Overrides the superclass function to take into account text wrapping.
|
2015-05-28 03:24:06 +00:00
|
|
|
*
|
|
|
|
|
* @param {Number} [selectionStart] Optional index. When not given, current selectionStart is used.
|
2015-05-27 20:59:56 +00:00
|
|
|
*/
|
2015-06-19 03:06:32 +00:00
|
|
|
get2DCursorLocation: function(selectionStart) {
|
2015-05-27 20:59:56 +00:00
|
|
|
if (typeof selectionStart === 'undefined') {
|
2015-06-16 20:19:41 +00:00
|
|
|
selectionStart = this.selectionStart;
|
2015-05-27 20:59:56 +00:00
|
|
|
}
|
2015-06-16 20:19:41 +00:00
|
|
|
|
|
|
|
|
var numLines = this._textLines.length,
|
|
|
|
|
removed = 0;
|
|
|
|
|
|
2015-05-28 03:24:06 +00:00
|
|
|
for (var i = 0; i < numLines; i++) {
|
2015-06-16 20:19:41 +00:00
|
|
|
var line = this._textLines[i],
|
|
|
|
|
lineLen = line.length;
|
|
|
|
|
|
|
|
|
|
if (selectionStart <= removed + lineLen) {
|
|
|
|
|
return {
|
|
|
|
|
lineIndex: i,
|
|
|
|
|
charIndex: selectionStart - removed
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
removed += lineLen;
|
|
|
|
|
|
|
|
|
|
if (this.text[removed] === '\n' || this.text[removed] === ' ') {
|
|
|
|
|
removed++;
|
|
|
|
|
}
|
2015-05-27 20:59:56 +00:00
|
|
|
}
|
2015-06-16 20:19:41 +00:00
|
|
|
|
2015-05-27 20:59:56 +00:00
|
|
|
return {
|
2015-06-16 20:19:41 +00:00
|
|
|
lineIndex: numLines - 1,
|
|
|
|
|
charIndex: this._textLines[numLines - 1].length
|
2015-05-27 20:59:56 +00:00
|
|
|
};
|
|
|
|
|
},
|
2015-05-28 03:24:06 +00:00
|
|
|
|
2015-05-27 18:47:13 +00:00
|
|
|
/**
|
2015-05-27 20:59:56 +00:00
|
|
|
* Overrides superclass function and uses text wrapping data to get cursor
|
|
|
|
|
* boundary offsets instead of the array of chars.
|
|
|
|
|
* @param {Array} chars Unused
|
|
|
|
|
* @param {String} typeOfBoundaries Can be 'cursor' or 'selection'
|
|
|
|
|
* @returns {Object} Object with 'top', 'left', and 'lineLeft' properties set.
|
2015-05-27 18:47:13 +00:00
|
|
|
*/
|
2015-06-19 03:06:32 +00:00
|
|
|
_getCursorBoundariesOffsets: function(chars, typeOfBoundaries) {
|
2015-06-12 03:00:23 +00:00
|
|
|
var topOffset = 0,
|
|
|
|
|
leftOffset = 0,
|
|
|
|
|
cursorLocation = this.get2DCursorLocation(),
|
|
|
|
|
lineChars = this._textLines[cursorLocation.lineIndex].split(''),
|
2015-07-28 16:01:01 +00:00
|
|
|
lineLeftOffset = this._getLineLeftOffset(this._getLineWidth(this.ctx, cursorLocation.lineIndex));
|
2015-05-27 20:59:56 +00:00
|
|
|
|
|
|
|
|
for (var i = 0; i < cursorLocation.charIndex; i++) {
|
|
|
|
|
leftOffset += this._getWidthOfChar(this.ctx, lineChars[i], cursorLocation.lineIndex, i);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < cursorLocation.lineIndex; i++) {
|
|
|
|
|
topOffset += this._getHeightOfLine(this.ctx, i);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (typeOfBoundaries === 'cursor') {
|
2015-05-27 21:18:52 +00:00
|
|
|
topOffset += (1 - this._fontSizeFraction) * this._getHeightOfLine(this.ctx, cursorLocation.lineIndex)
|
|
|
|
|
/ this.lineHeight - this.getCurrentCharFontSize(cursorLocation.lineIndex, cursorLocation.charIndex)
|
|
|
|
|
* (1 - this._fontSizeFraction);
|
2015-05-27 20:59:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
top: topOffset,
|
|
|
|
|
left: leftOffset,
|
|
|
|
|
lineLeft: lineLeftOffset
|
|
|
|
|
};
|
|
|
|
|
},
|
2015-06-12 03:00:23 +00:00
|
|
|
|
2015-06-19 03:06:32 +00:00
|
|
|
getMinWidth: function() {
|
2015-06-12 03:00:23 +00:00
|
|
|
return Math.max(this.minWidth, this.dynamicMinWidth);
|
|
|
|
|
},
|
|
|
|
|
|
2015-05-27 18:47:13 +00:00
|
|
|
/**
|
2015-05-27 20:59:56 +00:00
|
|
|
* Returns object representation of an instance
|
|
|
|
|
* @method toObject
|
|
|
|
|
* @param {Array} [propertiesToInclude] Any properties that you might want to additionally include in the output
|
|
|
|
|
* @return {Object} object representation of an instance
|
2015-05-27 18:47:13 +00:00
|
|
|
*/
|
2015-06-19 03:06:32 +00:00
|
|
|
toObject: function(propertiesToInclude) {
|
2015-05-27 20:59:56 +00:00
|
|
|
return fabric.util.object.extend(this.callSuper('toObject', propertiesToInclude), {
|
|
|
|
|
minWidth: this.minWidth
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
/**
|
|
|
|
|
* Returns fabric.Textbox instance from an object representation
|
|
|
|
|
* @static
|
|
|
|
|
* @memberOf fabric.Textbox
|
|
|
|
|
* @param {Object} object Object to create an instance from
|
|
|
|
|
* @return {fabric.Textbox} instance of fabric.Textbox
|
|
|
|
|
*/
|
2015-06-19 03:06:32 +00:00
|
|
|
fabric.Textbox.fromObject = function(object) {
|
2015-05-27 20:59:56 +00:00
|
|
|
return new fabric.Textbox(object.text, clone(object));
|
|
|
|
|
};
|
|
|
|
|
/**
|
|
|
|
|
* Returns the default controls visibility required for Textboxes.
|
|
|
|
|
* @returns {Object}
|
|
|
|
|
*/
|
2015-06-19 03:06:32 +00:00
|
|
|
fabric.Textbox.getTextboxControlVisibility = function() {
|
2015-05-27 20:59:56 +00:00
|
|
|
return {
|
|
|
|
|
tl: false,
|
|
|
|
|
tr: false,
|
|
|
|
|
br: false,
|
|
|
|
|
bl: false,
|
|
|
|
|
ml: true,
|
|
|
|
|
mt: false,
|
|
|
|
|
mr: true,
|
|
|
|
|
mb: false,
|
|
|
|
|
mtr: true
|
2015-05-27 18:47:13 +00:00
|
|
|
};
|
2015-05-27 20:59:56 +00:00
|
|
|
};
|
2016-05-25 13:00:38 +00:00
|
|
|
|
2015-06-16 20:54:03 +00:00
|
|
|
})(typeof exports !== 'undefined' ? exports : this);
|