fabric.js/src/text.class.js

263 lines
6.7 KiB
JavaScript
Raw Normal View History

//= require "object.class"
(function(global) {
2010-06-09 22:34:55 +00:00
"use strict";
var fabric = global.fabric || (global.fabric = { }),
extend = fabric.util.object.extend,
clone = fabric.util.object.clone;
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;
}
if (!fabric.Object) {
fabric.warn('fabric.Text requires fabric.Object');
2010-06-09 22:34:55 +00:00
return;
}
/**
* @class Text
* @extends fabric.Object
*/
fabric.Text = fabric.util.createClass(fabric.Object, /** @scope fabric.Text.prototype */ {
2010-06-09 22:34:55 +00:00
fontsize: 20,
fontweight: 100,
fontfamily: 'Modernist_One_400',
textDecoration: '',
textShadow: null,
fontStyle: '',
path: null,
2010-06-09 22:34:55 +00:00
/**
* @property
* @type String
*/
2010-06-09 22:34:55 +00:00
type: 'text',
/**
* Constructor
* @method initialize
* @param {String} text
* @param {Object} [options]
* @return {fabric.Text} thisArg
*/
2010-06-09 22:34:55 +00:00
initialize: function(text, options) {
2010-10-19 20:27:24 +00:00
this._initStateProperties();
2010-06-09 22:34:55 +00:00
this.text = text;
this.setOptions(options);
this.theta = this.angle * Math.PI / 180;
2010-06-09 22:34:55 +00:00
this.width = this.getWidth();
this.setCoords();
},
/**
* Creates `stateProperties` list on an instance, and adds `fabric.Text` -specific ones to it
* (such as "fontfamily", "fontweight", etc.)
2010-10-19 20:27:24 +00:00
* @private
* @method _initStateProperties
*/
2010-10-19 20:27:24 +00:00
_initStateProperties: function() {
this.stateProperties = this.stateProperties.concat();
this.stateProperties.push(
'fontfamily',
'fontweight',
'path',
'text',
'textDecoration',
'textShadow',
'fontStyle'
);
fabric.util.removeFromArray(this.stateProperties, 'width');
2010-06-09 22:34:55 +00:00
},
/**
* Returns string representation of an instance
* @method toString
* @return {String} String representation of text object
*/
2010-06-09 22:34:55 +00:00
toString: function() {
return '#<fabric.Text ('+ this.complexity() +'): ' +
JSON.stringify({ text: this.text, fontfamily: this.fontfamily }) + '>';
2010-06-09 22:34:55 +00:00
},
/**
* @private
* @method _render
* @param {CanvasRenderingContext2D} ctx Context to render on
*/
2010-06-09 22:34:55 +00:00
_render: function(context) {
var o = Cufon.textOptions || (Cufon.textOptions = { });
// export options to be used by cufon.js
o.left = this.left;
o.top = this.top;
o.context = context;
o.color = this.fill;
var el = this._initDummyElement();
// set "cursor" to top/left corner
this.transform(context);
// draw text
Cufon.replaceElement(el, {
separate: 'none',
fontFamily: this.fontfamily,
enableTextDecoration: true,
textDecoration: this.textDecoration,
textShadow: this.textShadow,
fontStyle: this.fontStyle
2010-06-09 22:34:55 +00:00
});
// update width, height
this.width = o.width;
this.height = o.height;
// need to set coords _after_ the width/height was retreived from Cufon
this.setCoords();
2010-06-09 22:34:55 +00:00
},
/**
* @private
* @method _initDummyElement
*/
2010-06-09 22:34:55 +00:00
_initDummyElement: function() {
var el = document.createElement('div'),
container = document.createElement('div');
// Cufon doesn't play nice with textDecoration=underline if element doesn't have a parent
container.appendChild(el);
2010-06-09 22:34:55 +00:00
el.innerHTML = this.text;
// need to specify these manually, since Jaxer doesn't support retrieving computed style
el.style.fontSize = '40px';
el.style.fontWeight = '400';
el.style.letterSpacing = 'normal';
el.style.color = '#000000';
el.style.fontWeight = '600';
el.style.fontFamily = 'Verdana';
return el;
},
/**
2010-10-19 20:27:24 +00:00
* Renders text instance on a specified context
* @method render
* @param ctx {CanvasRenderingContext2D} context to render on
*/
2010-06-09 22:34:55 +00:00
render: function(context) {
context.save();
this._render(context);
if (this.active) {
this.drawBorders(context);
this.drawCorners(context);
}
context.restore();
},
/**
2010-10-19 20:27:24 +00:00
* Returns object representation of an instance
2010-06-09 22:34:55 +00:00
* @method toObject
* @return {Object} Object representation of text object
2010-06-09 22:34:55 +00:00
*/
toObject: function() {
return extend(this.callSuper('toObject'), {
text: this.text,
fontsize: this.fontsize,
fontweight: this.fontweight,
fontfamily: this.fontfamily,
fontStyle: this.fontStyle,
textDecoration: this.textDecoration,
textShadow: this.textShadow,
path: this.path
2010-06-09 22:34:55 +00:00
});
},
/**
2010-10-19 20:27:24 +00:00
* Sets "color" of an instance (alias of `set('fill', &hellip;)`)
2010-06-09 22:34:55 +00:00
* @method setColor
* @param {String} value
* @return {fabric.Text} thisArg
2010-06-09 22:34:55 +00:00
* @chainable
*/
setColor: function(value) {
this.set('fill', value);
return this;
},
/**
2010-10-19 20:27:24 +00:00
* Sets fontsize of an instance and updates its coordinates
2010-06-09 22:34:55 +00:00
* @method setFontsize
* @param {Number} value
* @return {fabric.Text} thisArg
2010-06-09 22:34:55 +00:00
* @chainable
*/
setFontsize: function(value) {
this.set('fontsize', value);
this.setCoords();
return this;
},
/**
2010-10-19 20:27:24 +00:00
* Returns actual text value of an instance
2010-06-09 22:34:55 +00:00
* @method getText
* @return {String}
*/
getText: function() {
return this.text;
},
/**
* Sets text of an instance, and updates its coordinates
2010-06-09 22:34:55 +00:00
* @method setText
* @param {String} value
* @return {fabric.Text} thisArg
* @chainable
2010-06-09 22:34:55 +00:00
*/
setText: function(value) {
this.set('text', value);
this.setCoords();
return this;
},
/**
* Sets specified property to a specified value
* @method set
* @param {String} name
* @param {Any} value
* @return {fabric.Text} thisArg
* @chainable
*/
2010-06-09 22:34:55 +00:00
set: function(name, value) {
this[name] = value;
if (name === 'fontfamily') {
this.path = this.path.replace(/(.*?)([^\/]*)(\.font\.js)/, '$1' + value + '$3');
}
return this;
}
});
/**
* Returns fabric.Text instance from an object representation
2010-06-09 22:34:55 +00:00
* @static
* @method fromObject
* @param {Object} object to create an instance from
* @return {fabric.Text} an instance
2010-06-09 22:34:55 +00:00
*/
fabric.Text.fromObject = function(object) {
return new fabric.Text(object.text, clone(object));
2010-06-09 22:34:55 +00:00
};
/**
2010-10-19 20:27:24 +00:00
* Returns fabric.Text instance from an SVG element (<b>not yet implemented</b>)
2010-06-09 22:34:55 +00:00
* @static
* @method fabric.Text.fromElement
* @return {fabric.Text} an instance
2010-06-09 22:34:55 +00:00
*/
fabric.Text.fromElement = function(element) {
2010-06-09 22:34:55 +00:00
// TODO (kangax): implement this
};
})(this);