fabric.js/src/shapes/text.class.js

1041 lines
30 KiB
JavaScript
Raw Normal View History

(function(global) {
"use strict";
var fabric = global.fabric || (global.fabric = { }),
extend = fabric.util.object.extend,
clone = fabric.util.object.clone,
toFixed = fabric.util.toFixed,
supportsLineDash = fabric.StaticCanvas.supports('setLineDash');
2010-06-09 22:34:55 +00:00
if (fabric.Text) {
fabric.warn('fabric.Text is already defined');
2010-06-09 22:34:55 +00:00
return;
}
var stateProperties = fabric.Object.prototype.stateProperties.concat();
stateProperties.push(
'fontFamily',
'fontWeight',
'fontSize',
'text',
'textDecoration',
'textAlign',
'fontStyle',
'lineHeight',
'textBackgroundColor',
2013-09-13 16:59:19 +00:00
'useNative',
'path'
);
/**
2012-12-15 16:05:23 +00:00
* Text class
2013-04-25 18:21:32 +00:00
* @class fabric.Text
* @extends fabric.Object
2013-04-25 18:21:32 +00:00
* @return {fabric.Text} thisArg
* @tutorial {@link http://fabricjs.com/fabric-intro-part-2/#text}
2013-10-05 18:21:28 +00:00
* @see {@link fabric.Text#initialize} for constructor definition
*/
2013-04-25 17:52:05 +00:00
fabric.Text = fabric.util.createClass(fabric.Object, /** @lends fabric.Text.prototype */ {
/**
* Properties which when set cause object to change dimensions
* @type Object
* @private
*/
_dimensionAffectingProps: {
fontSize: true,
fontWeight: true,
fontFamily: true,
textDecoration: true,
fontStyle: true,
lineHeight: true,
stroke: true,
strokeWidth: true,
text: true
},
2013-09-13 16:59:19 +00:00
/**
* Retrieves object's fontSize
* @method getFontSize
* @memberOf fabric.Text.prototype
* @return {String} Font size (in pixels)
*/
/**
* Sets object's fontSize
* @method setFontSize
* @memberOf fabric.Text.prototype
* @param {Number} fontSize Font size (in pixels)
* @return {fabric.Text}
* @chainable
*/
/**
* Retrieves object's fontWeight
* @method getFontWeight
* @memberOf fabric.Text.prototype
* @return {(String|Number)} Font weight
*/
/**
* Sets object's fontWeight
* @method setFontWeight
* @memberOf fabric.Text.prototype
* @param {(Number|String)} fontWeight Font weight
* @return {fabric.Text}
* @chainable
*/
/**
* Retrieves object's fontFamily
* @method getFontFamily
* @memberOf fabric.Text.prototype
* @return {String} Font family
*/
/**
* Sets object's fontFamily
* @method setFontFamily
* @memberOf fabric.Text.prototype
* @param {String} fontFamily Font family
* @return {fabric.Text}
* @chainable
*/
/**
* Retrieves object's text
* @method getText
* @memberOf fabric.Text.prototype
* @return {String} text
*/
/**
* Sets object's text
* @method setText
* @memberOf fabric.Text.prototype
* @param {String} text Text
* @return {fabric.Text}
* @chainable
*/
/**
* Retrieves object's textDecoration
* @method getTextDecoration
* @memberOf fabric.Text.prototype
* @return {String} Text decoration
*/
/**
* Sets object's textDecoration
* @method setTextDecoration
* @memberOf fabric.Text.prototype
* @param {String} textDecoration Text decoration
* @return {fabric.Text}
* @chainable
*/
/**
* Retrieves object's fontStyle
* @method getFontStyle
* @memberOf fabric.Text.prototype
* @return {String} Font style
*/
/**
* Sets object's fontStyle
* @method setFontStyle
* @memberOf fabric.Text.prototype
* @param {String} fontStyle Font style
* @return {fabric.Text}
* @chainable
*/
/**
* Retrieves object's lineHeight
* @method getLineHeight
* @memberOf fabric.Text.prototype
* @return {Number} Line height
*/
/**
* Sets object's lineHeight
* @method setLineHeight
* @memberOf fabric.Text.prototype
* @param {Number} lineHeight Line height
* @return {fabric.Text}
* @chainable
*/
/**
* Retrieves object's textAlign
* @method getTextAlign
* @memberOf fabric.Text.prototype
* @return {String} Text alignment
*/
/**
* Sets object's textAlign
* @method setTextAlign
* @memberOf fabric.Text.prototype
* @param {String} textAlign Text alignment
* @return {fabric.Text}
* @chainable
*/
/**
* Retrieves object's textBackgroundColor
* @method getTextBackgroundColor
* @memberOf fabric.Text.prototype
* @return {String} Text background color
*/
/**
* Sets object's textBackgroundColor
* @method setTextBackgroundColor
* @memberOf fabric.Text.prototype
* @param {String} textBackgroundColor Text background color
* @return {fabric.Text}
* @chainable
*/
/**
* Type of an object
* @type String
* @default
*/
type: 'text',
/**
2013-02-06 21:36:39 +00:00
* Font size (in pixels)
* @type Number
* @default
*/
fontSize: 40,
/**
2012-12-15 16:05:23 +00:00
* Font weight (e.g. bold, normal, 400, 600, 800)
2013-09-13 16:59:19 +00:00
* @type {(Number|String)}
* @default
*/
fontWeight: 'normal',
/**
2012-12-15 16:05:23 +00:00
* Font family
* @type String
* @default
*/
fontFamily: 'Times New Roman',
/**
* Text decoration Possible values: "", "underline", "overline" or "line-through".
* @type String
* @default
*/
textDecoration: '',
/**
2012-12-15 16:05:23 +00:00
* Text alignment. Possible values: "left", "center", or "right".
* @type String
* @default
*/
textAlign: 'left',
/**
* Font style . Possible values: "", "normal", "italic" or "oblique".
* @type String
* @default
*/
fontStyle: '',
/**
2012-12-15 16:05:23 +00:00
* Line height
* @type Number
* @default
*/
lineHeight: 1.3,
/**
2012-12-15 16:05:23 +00:00
* Background color of text lines
* @type String
* @default
*/
textBackgroundColor: '',
/**
2012-12-15 16:05:23 +00:00
* URL of a font file, when using Cufon
* @type String | null
* @default
*/
path: null,
2012-07-14 20:35:45 +00:00
/**
* Indicates whether canvas native text methods should be used to render text (otherwise, Cufon is used)
2012-07-14 20:35:45 +00:00
* @type Boolean
* @default
2012-07-14 20:35:45 +00:00
*/
useNative: true,
2013-08-31 10:52:30 +00:00
/**
* List of properties to consider when checking if state of an object is changed ({@link fabric.Object#hasStateChanged})
* as well as for history (undo/redo) purposes
* @type Array
*/
stateProperties: stateProperties,
2013-08-31 10:52:30 +00:00
/**
* When defined, an object is rendered via stroke and this property specifies its color.
* <b>Backwards incompatibility note:</b> This property was named "strokeStyle" until v1.1.6
* @type String
* @default
*/
stroke: null,
/**
* Shadow object representing shadow of this shape.
* <b>Backwards incompatibility note:</b> This property was named "textShadow" (String) until v1.2.11
* @type fabric.Shadow
* @default
*/
shadow: null,
/**
* Constructor
* @param {String} text Text string
* @param {Object} [options] Options object
* @return {fabric.Text} thisArg
*/
2010-06-09 22:34:55 +00:00
initialize: function(text, options) {
options = options || { };
2010-06-09 22:34:55 +00:00
this.text = text;
this.__skipDimension = true;
this.setOptions(options);
this.__skipDimension = false;
this._initDimensions();
2010-06-09 22:34:55 +00:00
this.setCoords();
},
/**
* Renders text object on offscreen canvas, so that it would get dimensions
* @private
*/
_initDimensions: function() {
if (this.__skipDimension) return;
var canvasEl = fabric.util.createCanvasElement();
this._render(canvasEl.getContext('2d'));
},
/**
* Returns string representation of an instance
* @return {String} String representation of text object
*/
2010-06-09 22:34:55 +00:00
toString: function() {
return '#<fabric.Text (' + this.complexity() +
'): { "text": "' + this.text + '", "fontFamily": "' + this.fontFamily + '" }>';
2010-06-09 22:34:55 +00:00
},
/**
* @private
* @param {CanvasRenderingContext2D} ctx Context to render on
*/
_render: function(ctx) {
var isInPathGroup = this.group && this.group.type !== 'group';
if (isInPathGroup && !this.transformMatrix) {
ctx.translate(-this.group.width/2 + this.left, -this.group.height / 2 + this.top);
}
else if (isInPathGroup && this.transformMatrix) {
ctx.translate(-this.group.width/2, -this.group.height/2);
}
2012-07-14 20:35:45 +00:00
if (typeof Cufon === 'undefined' || this.useNative === true) {
this._renderViaNative(ctx);
2012-07-14 20:35:45 +00:00
}
else {
this._renderViaCufon(ctx);
2012-07-14 20:35:45 +00:00
}
},
/**
* @private
* @param {CanvasRenderingContext2D} ctx Context to render on
*/
_renderViaNative: function(ctx) {
this.transform(ctx, fabric.isLikelyNode);
this._setTextStyles(ctx);
var textLines = this.text.split(/\r?\n/);
this.width = this._getTextWidth(ctx, textLines);
this.height = this._getTextHeight(ctx, textLines);
this.clipTo && fabric.util.clipContext(this, ctx);
this._renderTextBackground(ctx, textLines);
if (this.textAlign !== 'left' && this.textAlign !== 'justify') {
ctx.save();
ctx.translate(this.textAlign === 'center' ? (this.width / 2) : this.width, 0);
}
ctx.save();
this._setShadow(ctx);
this._renderTextFill(ctx, textLines);
this._renderTextStroke(ctx, textLines);
this._removeShadow(ctx);
ctx.restore();
if (this.textAlign !== 'left' && this.textAlign !== 'justify') {
ctx.restore();
}
this._renderTextDecoration(ctx, textLines);
this.clipTo && ctx.restore();
this._setBoundaries(ctx, textLines);
this._totalLineHeight = 0;
},
2012-07-14 20:35:45 +00:00
/**
* @private
* @param {CanvasRenderingContext2D} ctx Context to render on
* @param {Array} textLines Array of all text lines
*/
_setBoundaries: function(ctx, textLines) {
this._boundaries = [ ];
for (var i = 0, len = textLines.length; i < len; i++) {
var lineWidth = this._getLineWidth(ctx, textLines[i]);
var lineLeftOffset = this._getLineLeftOffset(lineWidth);
this._boundaries.push({
height: this.fontSize * this.lineHeight,
width: lineWidth,
left: lineLeftOffset
});
}
},
/**
* @private
* @param {CanvasRenderingContext2D} ctx Context to render on
*/
_setTextStyles: function(ctx) {
this._setFillStyles(ctx);
this._setStrokeStyles(ctx);
ctx.textBaseline = 'alphabetic';
if (!this.skipTextAlign) {
ctx.textAlign = this.textAlign;
}
ctx.font = this._getFontDeclaration();
},
/**
* @private
* @param {CanvasRenderingContext2D} ctx Context to render on
* @param {Array} textLines Array of all text lines
* @return {Number} Height of fabric.Text object
*/
_getTextHeight: function(ctx, textLines) {
return this.fontSize * textLines.length * this.lineHeight;
},
/**
* @private
* @param {CanvasRenderingContext2D} ctx Context to render on
* @param {Array} textLines Array of all text lines
* @return {Number} Maximum width of fabric.Text object
*/
_getTextWidth: function(ctx, textLines) {
2013-10-25 07:03:10 +00:00
var maxWidth = ctx.measureText(textLines[0] || '|').width;
2012-07-14 20:35:45 +00:00
for (var i = 1, len = textLines.length; i < len; i++) {
var currentLineWidth = ctx.measureText(textLines[i]).width;
if (currentLineWidth > maxWidth) {
maxWidth = currentLineWidth;
}
2012-07-14 20:35:45 +00:00
}
return maxWidth;
},
/**
* @private
* @param {String} method Method name ("fillText" or "strokeText")
* @param {CanvasRenderingContext2D} ctx Context to render on
* @param {String} line Chars to render
* @param {Number} left Left position of text
* @param {Number} top Top position of text
*/
_drawChars: function(method, ctx, chars, left, top) {
ctx[method](chars, left, top);
},
/**
* @private
* @param {String} method Method name ("fillText" or "strokeText")
* @param {CanvasRenderingContext2D} ctx Context to render on
* @param {String} line Text to render
* @param {Number} left Left position of text
* @param {Number} top Top position of text
* @param {Number} lineIndex Index of a line in a text
*/
_drawTextLine: function(method, ctx, line, left, top, lineIndex) {
2013-09-18 15:28:36 +00:00
// lift the line by quarter of fontSize
top -= this.fontSize / 4;
// short-circuit
if (this.textAlign !== 'justify') {
this._drawChars(method, ctx, line, left, top, lineIndex);
return;
}
var lineWidth = ctx.measureText(line).width;
var totalWidth = this.width;
if (totalWidth > lineWidth) {
// stretch the line
var words = line.split(/\s+/);
var wordsWidth = ctx.measureText(line.replace(/\s+/g, '')).width;
var widthDiff = totalWidth - wordsWidth;
var numSpaces = words.length - 1;
var spaceWidth = widthDiff / numSpaces;
var leftOffset = 0;
for (var i = 0, len = words.length; i < len; i++) {
this._drawChars(method, ctx, words[i], left + leftOffset, top, lineIndex);
leftOffset += ctx.measureText(words[i]).width + spaceWidth;
}
}
else {
this._drawChars(method, ctx, line, left, top, lineIndex);
}
},
/**
* @private
* @return {Number} Left offset
*/
_getLeftOffset: function() {
if (fabric.isLikelyNode) {
return 0;
}
return -this.width / 2;
},
/**
* @private
* @return {Number} Top offset
*/
_getTopOffset: function() {
return -this.height / 2;
},
2012-12-15 16:05:23 +00:00
/**
* @private
* @param {CanvasRenderingContext2D} ctx Context to render on
* @param {Array} textLines Array of all text lines
2012-12-15 16:05:23 +00:00
*/
_renderTextFill: function(ctx, textLines) {
if (!this.fill && !this.skipFillStrokeCheck) return;
this._boundaries = [ ];
var lineHeights = 0;
for (var i = 0, len = textLines.length; i < len; i++) {
var heightOfLine = this._getHeightOfLine(ctx, i, textLines);
lineHeights += heightOfLine;
this._drawTextLine(
'fillText',
ctx,
textLines[i],
this._getLeftOffset(),
this._getTopOffset() + lineHeights,
i
);
}
},
/**
* @private
* @param {CanvasRenderingContext2D} ctx Context to render on
* @param {Array} textLines Array of all text lines
*/
_renderTextStroke: function(ctx, textLines) {
if (!this.stroke && !this.skipFillStrokeCheck) return;
var lineHeights = 0;
ctx.save();
if (this.strokeDashArray) {
// Spec requires the concatenation of two copies the dash list when the number of elements is odd
if (1 & this.strokeDashArray.length) {
this.strokeDashArray.push.apply(this.strokeDashArray, this.strokeDashArray);
}
supportsLineDash && ctx.setLineDash(this.strokeDashArray);
}
ctx.beginPath();
for (var i = 0, len = textLines.length; i < len; i++) {
var heightOfLine = this._getHeightOfLine(ctx, i, textLines);
lineHeights += heightOfLine;
this._drawTextLine(
'strokeText',
ctx,
textLines[i],
this._getLeftOffset(),
this._getTopOffset() + lineHeights,
i
);
}
ctx.closePath();
ctx.restore();
},
_getHeightOfLine: function() {
return this.fontSize * this.lineHeight;
},
/**
* @private
* @param {CanvasRenderingContext2D} ctx Context to render on
* @param {Array} textLines Array of all text lines
*/
_renderTextBackground: function(ctx, textLines) {
this._renderTextBoxBackground(ctx);
this._renderTextLinesBackground(ctx, textLines);
},
/**
* @private
* @param {CanvasRenderingContext2D} ctx Context to render on
*/
_renderTextBoxBackground: function(ctx) {
if (!this.backgroundColor) return;
ctx.save();
ctx.fillStyle = this.backgroundColor;
ctx.fillRect(
this._getLeftOffset(),
this._getTopOffset(),
this.width,
this.height
);
ctx.restore();
},
/**
* @private
* @param {CanvasRenderingContext2D} ctx Context to render on
* @param {Array} textLines Array of all text lines
*/
_renderTextLinesBackground: function(ctx, textLines) {
if (!this.textBackgroundColor) return;
ctx.save();
ctx.fillStyle = this.textBackgroundColor;
for (var i = 0, len = textLines.length; i < len; i++) {
if (textLines[i] !== '') {
var lineWidth = this._getLineWidth(ctx, textLines[i]);
var lineLeftOffset = this._getLineLeftOffset(lineWidth);
ctx.fillRect(
this._getLeftOffset() + lineLeftOffset,
this._getTopOffset() + (i * this.fontSize * this.lineHeight),
lineWidth,
this.fontSize * this.lineHeight
);
}
}
ctx.restore();
},
/**
* @private
* @param {Number} lineWidth Width of text line
* @return {Number} Line left offset
*/
_getLineLeftOffset: function(lineWidth) {
if (this.textAlign === 'center') {
return (this.width - lineWidth) / 2;
}
if (this.textAlign === 'right') {
return this.width - lineWidth;
}
return 0;
},
/**
* @private
* @param {CanvasRenderingContext2D} ctx Context to render on
* @param {String} line Text line
* @return {Number} Line width
*/
_getLineWidth: function(ctx, line) {
return this.textAlign === 'justify'
? this.width
: ctx.measureText(line).width;
},
/**
* @private
* @param {CanvasRenderingContext2D} ctx Context to render on
* @param {Array} textLines Array of all text lines
*/
_renderTextDecoration: function(ctx, textLines) {
if (!this.textDecoration) return;
// var halfOfVerticalBox = this.originY === 'top' ? 0 : this._getTextHeight(ctx, textLines) / 2;
var halfOfVerticalBox = this._getTextHeight(ctx, textLines) / 2;
var _this = this;
2012-11-23 12:38:13 +00:00
/** @ignore */
function renderLinesAtOffset(offset) {
for (var i = 0, len = textLines.length; i < len; i++) {
var lineWidth = _this._getLineWidth(ctx, textLines[i]);
var lineLeftOffset = _this._getLineLeftOffset(lineWidth);
ctx.fillRect(
_this._getLeftOffset() + lineLeftOffset,
~~((offset + (i * _this._getHeightOfLine(ctx, i, textLines))) - halfOfVerticalBox),
lineWidth,
1);
}
}
if (this.textDecoration.indexOf('underline') > -1) {
renderLinesAtOffset(this.fontSize * this.lineHeight);
}
if (this.textDecoration.indexOf('line-through') > -1) {
renderLinesAtOffset(this.fontSize * this.lineHeight - this.fontSize / 2);
}
if (this.textDecoration.indexOf('overline') > -1) {
renderLinesAtOffset(this.fontSize * this.lineHeight - this.fontSize);
}
},
/**
* @private
*/
_getFontDeclaration: function() {
return [
// node-canvas needs "weight style", while browsers need "style weight"
(fabric.isLikelyNode ? this.fontWeight : this.fontStyle),
(fabric.isLikelyNode ? this.fontStyle : this.fontWeight),
this.fontSize + 'px',
(fabric.isLikelyNode ? ('"' + this.fontFamily + '"') : this.fontFamily)
].join(' ');
},
/**
2010-10-19 20:27:24 +00:00
* Renders text instance on a specified context
* @param {CanvasRenderingContext2D} ctx Context to render on
* @param {Boolean} [noTransform] When true, context is not transformed
*/
render: function(ctx, noTransform) {
// do not render if object is not visible
if (!this.visible) return;
ctx.save();
this._render(ctx);
if (!noTransform && this.active) {
this.drawBorders(ctx);
this.drawControls(ctx);
2010-06-09 22:34:55 +00:00
}
ctx.restore();
2010-06-09 22:34:55 +00:00
},
/**
* Returns object representation of an instance
* @param {Array} [propertiesToInclude] Any properties that you might want to additionally include in the output
* @return {Object} Object representation of an instance
*/
toObject: function(propertiesToInclude) {
var object = extend(this.callSuper('toObject', propertiesToInclude), {
text: this.text,
fontSize: this.fontSize,
fontWeight: this.fontWeight,
fontFamily: this.fontFamily,
fontStyle: this.fontStyle,
lineHeight: this.lineHeight,
textDecoration: this.textDecoration,
textAlign: this.textAlign,
path: this.path,
textBackgroundColor: this.textBackgroundColor,
useNative: this.useNative
});
if (!this.includeDefaultValues) {
this._removeDefaultValues(object);
}
return object;
},
/* _TO_SVG_START_ */
/**
2012-12-13 14:36:43 +00:00
* Returns SVG representation of an instance
* @param {Function} [reviver] Method for further parsing of svg representation.
2012-12-15 16:05:23 +00:00
* @return {String} svg representation of an instance
*/
toSVG: function(reviver) {
var markup = [ ],
textLines = this.text.split(/\r?\n/),
lineTopOffset = this.useNative
? this.fontSize * this.lineHeight
: (-this._fontAscent - ((this._fontAscent / 5) * this.lineHeight)),
textLeftOffset = -(this.width/2),
textTopOffset = this.useNative
? this.fontSize - 1
: (this.height/2) - (textLines.length * this.fontSize) - this._totalLineHeight,
textAndBg = this._getSVGTextAndBg(lineTopOffset, textLeftOffset, textLines),
shadowSpans = this._getSVGShadows(lineTopOffset, textLines);
// move top offset by an ascent
textTopOffset += (this._fontAscent ? ((this._fontAscent / 5) * this.lineHeight) : 0);
markup.push(
'<g transform="', this.getSvgTransform(), '">',
textAndBg.textBgRects.join(''),
'<text ',
(this.fontFamily ? 'font-family="' + this.fontFamily.replace(/"/g,'\'') + '" ': ''),
(this.fontSize ? 'font-size="' + this.fontSize + '" ': ''),
(this.fontStyle ? 'font-style="' + this.fontStyle + '" ': ''),
(this.fontWeight ? 'font-weight="' + this.fontWeight + '" ': ''),
(this.textDecoration ? 'text-decoration="' + this.textDecoration + '" ': ''),
'style="', this.getSvgStyles(), '" ',
/* svg starts from left/bottom corner so we normalize height */
'transform="translate(', toFixed(textLeftOffset, 2), ' ', toFixed(textTopOffset, 2), ')">',
shadowSpans.join(''),
textAndBg.textSpans.join(''),
'</text>',
'</g>'
);
return reviver ? reviver(markup.join('')) : markup.join('');
},
2012-12-15 16:05:23 +00:00
/**
* @private
* @param {Number} lineTopOffset Line top offset
* @param {Array} textLines Array of all text lines
* @return {Array}
2012-12-15 16:05:23 +00:00
*/
_getSVGShadows: function(lineTopOffset, textLines) {
var shadowSpans = [],
i, len,
lineTopOffsetMultiplier = 1;
if (!this.shadow || !this._boundaries) {
return shadowSpans;
}
for (i = 0, len = textLines.length; i < len; i++) {
if (textLines[i] !== '') {
var lineLeftOffset = (this._boundaries && this._boundaries[i]) ? this._boundaries[i].left : 0;
shadowSpans.push(
'<tspan x="',
toFixed((lineLeftOffset + lineTopOffsetMultiplier) + this.shadow.offsetX, 2),
((i === 0 || this.useNative) ? '" y' : '" dy'), '="',
toFixed(this.useNative
? ((lineTopOffset * i) - this.height / 2 + this.shadow.offsetY)
: (lineTopOffset + (i === 0 ? this.shadow.offsetY : 0)), 2),
'" ',
this._getFillAttributes(this.shadow.color), '>',
fabric.util.string.escapeXml(textLines[i]),
'</tspan>');
lineTopOffsetMultiplier = 1;
} else {
// in some environments (e.g. IE 7 & 8) empty tspans are completely ignored, using a lineTopOffsetMultiplier
// prevents empty tspans
lineTopOffsetMultiplier++;
}
}
return shadowSpans;
},
2012-12-15 16:05:23 +00:00
/**
* @private
* @param {Number} lineTopOffset Line top offset
* @param {Number} textLeftOffset Text left offset
* @param {Array} textLines Array of all text lines
* @return {Object}
2012-12-15 16:05:23 +00:00
*/
_getSVGTextAndBg: function(lineTopOffset, textLeftOffset, textLines) {
var textSpans = [ ], textBgRects = [ ], i, lineLeftOffset, len, lineTopOffsetMultiplier = 1;
// bounding-box background
if (this.backgroundColor && this._boundaries) {
textBgRects.push(
'<rect ',
this._getFillAttributes(this.backgroundColor),
' x="',
toFixed(-this.width / 2, 2),
'" y="',
toFixed(-this.height / 2, 2),
'" width="',
toFixed(this.width, 2),
'" height="',
toFixed(this.height, 2),
'"></rect>');
}
// text and text-background
for (i = 0, len = textLines.length; i < len; i++) {
if (textLines[i] !== '') {
lineLeftOffset = (this._boundaries && this._boundaries[i]) ? toFixed(this._boundaries[i].left, 2) : 0;
textSpans.push(
'<tspan x="',
lineLeftOffset, '" ',
(i === 0 || this.useNative ? 'y' : 'dy'), '="',
toFixed(this.useNative ? ((lineTopOffset * i) - this.height / 2) : (lineTopOffset * lineTopOffsetMultiplier), 2) , '" ',
// doing this on <tspan> elements since setting opacity on containing <text> one doesn't work in Illustrator
this._getFillAttributes(this.fill), '>',
fabric.util.string.escapeXml(textLines[i]),
'</tspan>'
);
lineTopOffsetMultiplier = 1;
}
else {
// in some environments (e.g. IE 7 & 8) empty tspans are completely ignored, using a lineTopOffsetMultiplier
// prevents empty tspans
lineTopOffsetMultiplier++;
}
if (!this.textBackgroundColor || !this._boundaries) continue;
textBgRects.push(
'<rect ',
this._getFillAttributes(this.textBackgroundColor),
' x="',
toFixed(textLeftOffset + this._boundaries[i].left, 2),
'" y="',
/* an offset that seems to straighten things out */
toFixed((lineTopOffset * i) - this.height / 2, 2),
'" width="',
toFixed(this._boundaries[i].width, 2),
'" height="',
toFixed(this._boundaries[i].height, 2),
'"></rect>');
}
return {
textSpans: textSpans,
textBgRects: textBgRects
};
},
2012-12-15 16:05:23 +00:00
/**
* Adobe Illustrator (at least CS5) is unable to render rgba()-based fill values
* we work around it by "moving" alpha channel into opacity attribute and setting fill's alpha to 1
*
* @private
* @param {Any} value
* @return {String}
2012-12-15 16:05:23 +00:00
*/
_getFillAttributes: function(value) {
var fillColor = (value && typeof value === 'string') ? new fabric.Color(value) : '';
if (!fillColor || !fillColor.getSource() || fillColor.getAlpha() === 1) {
return 'fill="' + value + '"';
}
return 'opacity="' + fillColor.getAlpha() + '" fill="' + fillColor.setAlpha(1).toRgb() + '"';
},
/* _TO_SVG_END_ */
/**
* Sets specified property to a specified value
* @param {String} key
* @param {Any} value
* @return {fabric.Text} thisArg
* @chainable
*/
_set: function(key, value) {
if (key === 'fontFamily' && this.path) {
2012-09-07 17:15:42 +00:00
this.path = this.path.replace(/(.*?)([^\/]*)(\.font\.js)/, '$1' + value + '$3');
}
this.callSuper('_set', key, value);
if (key in this._dimensionAffectingProps) {
this._initDimensions();
this.setCoords();
}
},
/**
* Returns complexity of an instance
* @return {Number} complexity
*/
complexity: function() {
return 1;
}
2010-06-09 22:34:55 +00:00
});
/* _FROM_SVG_START_ */
/**
2012-11-23 12:38:13 +00:00
* List of attribute names to account for when parsing SVG element (used by {@link fabric.Text.fromElement})
* @static
* @memberOf fabric.Text
* @see: http://www.w3.org/TR/SVG/text.html#TextElement
*/
fabric.Text.ATTRIBUTE_NAMES = fabric.SHARED_ATTRIBUTES.concat(
'x y font-family font-style font-weight font-size text-decoration'.split(' '));
/**
* Returns fabric.Text instance from an SVG element (<b>not yet implemented</b>)
2010-06-09 22:34:55 +00:00
* @static
* @memberOf fabric.Text
* @param {SVGElement} element Element to parse
* @param {Object} [options] Options object
* @return {fabric.Text} Instance of fabric.Text
2010-06-09 22:34:55 +00:00
*/
fabric.Text.fromElement = function(element, options) {
if (!element) {
return null;
}
var parsedAttributes = fabric.parseAttributes(element, fabric.Text.ATTRIBUTE_NAMES);
options = fabric.util.object.extend((options ? fabric.util.object.clone(options) : { }), parsedAttributes);
var text = new fabric.Text(element.textContent, options);
/*
Adjust positioning:
x/y attributes in SVG correspond to the bottom-left corner of text bounding box
top/left properties in Fabric correspond to center point of text bounding box
*/
text.set({
left: text.getLeft() + text.getWidth() / 2,
top: text.getTop() - text.getHeight() / 2
});
return text;
};
/* _FROM_SVG_END_ */
/**
* Returns fabric.Text instance from an object representation
* @static
* @memberOf fabric.Text
* @param object {Object} object Object to create an instance from
* @return {fabric.Text} Instance of fabric.Text
*/
fabric.Text.fromObject = function(object) {
return new fabric.Text(object.text, clone(object));
};
fabric.util.createAccessors(fabric.Text);
})(typeof exports !== 'undefined' ? exports : this);