mirror of
https://github.com/Hopiu/fabric.js.git
synced 2026-05-12 15:53:10 +00:00
Fixed "enum" notation `fabric.Object.setGradient` is now chainable Doc additions Add `fabric.Object.setPatternFill`unit test Add chainable tests to some methods
1060 lines
30 KiB
JavaScript
1060 lines
30 KiB
JavaScript
(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');
|
|
|
|
if (fabric.Text) {
|
|
fabric.warn('fabric.Text is already defined');
|
|
return;
|
|
}
|
|
|
|
var stateProperties = fabric.Object.prototype.stateProperties.concat();
|
|
stateProperties.push(
|
|
'fontFamily',
|
|
'fontWeight',
|
|
'fontSize',
|
|
'text',
|
|
'textDecoration',
|
|
'textAlign',
|
|
'fontStyle',
|
|
'lineHeight',
|
|
'backgroundColor',
|
|
'textBackgroundColor',
|
|
'useNative',
|
|
'path'
|
|
);
|
|
|
|
/**
|
|
* Text class
|
|
* @class fabric.Text
|
|
* @extends fabric.Object
|
|
* @return {fabric.Text} thisArg
|
|
*/
|
|
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
|
|
},
|
|
|
|
/**
|
|
* 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',
|
|
|
|
/**
|
|
* Font size (in pixels)
|
|
* @type Number
|
|
* @default
|
|
*/
|
|
fontSize: 40,
|
|
|
|
/**
|
|
* Font weight (e.g. bold, normal, 400, 600, 800)
|
|
* @type {(Number|String)}
|
|
* @default
|
|
*/
|
|
fontWeight: 'normal',
|
|
|
|
/**
|
|
* Font family
|
|
* @type String
|
|
* @default
|
|
*/
|
|
fontFamily: 'Times New Roman',
|
|
|
|
/**
|
|
* Text decoration Possible values: "", "underline", "overline" or "line-through".
|
|
* @type String
|
|
* @default
|
|
*/
|
|
textDecoration: '',
|
|
|
|
/**
|
|
* 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: '',
|
|
|
|
/**
|
|
* Line height
|
|
* @type Number
|
|
* @default
|
|
*/
|
|
lineHeight: 1.3,
|
|
|
|
/**
|
|
* Background color of an entire text box
|
|
* @type String
|
|
* @default
|
|
*/
|
|
backgroundColor: '',
|
|
|
|
/**
|
|
* Background color of text lines
|
|
* @type String
|
|
* @default
|
|
*/
|
|
textBackgroundColor: '',
|
|
|
|
/**
|
|
* URL of a font file, when using Cufon
|
|
* @type String | null
|
|
* @default
|
|
*/
|
|
path: null,
|
|
|
|
/**
|
|
* Indicates whether canvas native text methods should be used to render text (otherwise, Cufon is used)
|
|
* @type Boolean
|
|
* @default
|
|
*/
|
|
useNative: true,
|
|
|
|
/**
|
|
* 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,
|
|
|
|
/**
|
|
* 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
|
|
*/
|
|
initialize: function(text, options) {
|
|
options = options || { };
|
|
|
|
this.text = text;
|
|
this.__skipDimension = true;
|
|
this.setOptions(options);
|
|
this.__skipDimension = false;
|
|
this._initDimensions();
|
|
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
|
|
*/
|
|
toString: function() {
|
|
return '#<fabric.Text (' + this.complexity() +
|
|
'): { "text": "' + this.text + '", "fontFamily": "' + this.fontFamily + '" }>';
|
|
},
|
|
|
|
/**
|
|
* @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);
|
|
}
|
|
|
|
if (typeof Cufon === 'undefined' || this.useNative === true) {
|
|
this._renderViaNative(ctx);
|
|
}
|
|
else {
|
|
this._renderViaCufon(ctx);
|
|
}
|
|
},
|
|
|
|
/**
|
|
* @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;
|
|
},
|
|
|
|
/**
|
|
* @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) {
|
|
if (this.fill) {
|
|
ctx.fillStyle = this.fill.toLive
|
|
? this.fill.toLive(ctx)
|
|
: this.fill;
|
|
}
|
|
if (this.stroke) {
|
|
ctx.lineWidth = this.strokeWidth;
|
|
ctx.lineCap = this.strokeLineCap;
|
|
ctx.lineJoin = this.strokeLineJoin;
|
|
ctx.miterLimit = this.strokeMiterLimit;
|
|
ctx.strokeStyle = this.stroke.toLive
|
|
? this.stroke.toLive(ctx)
|
|
: this.stroke;
|
|
}
|
|
ctx.textBaseline = 'alphabetic';
|
|
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) {
|
|
var maxWidth = ctx.measureText(textLines[0]).width;
|
|
|
|
for (var i = 1, len = textLines.length; i < len; i++) {
|
|
var currentLineWidth = ctx.measureText(textLines[i]).width;
|
|
if (currentLineWidth > maxWidth) {
|
|
maxWidth = currentLineWidth;
|
|
}
|
|
}
|
|
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) {
|
|
|
|
// 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 && (this.originX === 'left' || this.originX === 'center')) {
|
|
return 0;
|
|
}
|
|
return -this.width / 2;
|
|
},
|
|
|
|
/**
|
|
* @private
|
|
* @return {Number} Top offset
|
|
*/
|
|
_getTopOffset: function() {
|
|
if (fabric.isLikelyNode) {
|
|
if (this.originY === 'center') {
|
|
return -this.height / 2;
|
|
}
|
|
else if (this.originY === 'bottom') {
|
|
return -this.height;
|
|
}
|
|
return 0;
|
|
}
|
|
// in browser, text drawing always starts at vertical center
|
|
return -this.height / 2;
|
|
},
|
|
|
|
/**
|
|
* @private
|
|
* @param {CanvasRenderingContext2D} ctx Context to render on
|
|
* @param {Array} textLines Array of all text lines
|
|
*/
|
|
_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;
|
|
|
|
/** @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);
|
|
}
|
|
}
|
|
|
|
var fractionOfFontSize = this.fontSize / 4;
|
|
|
|
if (this.textDecoration.indexOf('underline') > -1) {
|
|
renderLinesAtOffset(this.fontSize * this.lineHeight);
|
|
}
|
|
if (this.textDecoration.indexOf('line-through') > -1) {
|
|
renderLinesAtOffset(this.fontSize * this.lineHeight - fractionOfFontSize);
|
|
}
|
|
if (this.textDecoration.indexOf('overline') > -1) {
|
|
renderLinesAtOffset(fractionOfFontSize);
|
|
}
|
|
},
|
|
|
|
/**
|
|
* @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(' ');
|
|
},
|
|
|
|
/**
|
|
* 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);
|
|
}
|
|
ctx.restore();
|
|
},
|
|
|
|
/**
|
|
* 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) {
|
|
return 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,
|
|
backgroundColor: this.backgroundColor,
|
|
textBackgroundColor: this.textBackgroundColor,
|
|
useNative: this.useNative
|
|
});
|
|
},
|
|
|
|
/* _TO_SVG_START_ */
|
|
/**
|
|
* Returns SVG representation of an instance
|
|
* @return {String} svg representation of an instance
|
|
*/
|
|
toSVG: function() {
|
|
var 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);
|
|
|
|
return [
|
|
'<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>'
|
|
].join('');
|
|
},
|
|
|
|
/**
|
|
* @private
|
|
* @param {Number} lineTopOffset Line top offset
|
|
* @param {Array} textLines Array of all text lines
|
|
* @return {Array}
|
|
*/
|
|
_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;
|
|
},
|
|
|
|
/**
|
|
* @private
|
|
* @param {Number} lineTopOffset Line top offset
|
|
* @param {Number} textLeftOffset Text left offset
|
|
* @param {Array} textLines Array of all text lines
|
|
* @return {Object}
|
|
*/
|
|
_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
|
|
};
|
|
},
|
|
|
|
/**
|
|
* 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}
|
|
*/
|
|
_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) {
|
|
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;
|
|
}
|
|
});
|
|
|
|
/* _FROM_SVG_START_ */
|
|
/**
|
|
* 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>)
|
|
* @static
|
|
* @memberOf fabric.Text
|
|
* @param {SVGElement} element Element to parse
|
|
* @param {Object} [options] Options object
|
|
* @return {fabric.Text} Instance of fabric.Text
|
|
*/
|
|
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);
|