2011-05-16 09:15:47 -04:00
|
|
|
/*
|
|
|
|
* Paper.js
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-05-16 09:15:47 -04:00
|
|
|
* This file is part of Paper.js, a JavaScript Vector Graphics Library,
|
|
|
|
* based on Scriptographer.org and designed to be largely API compatible.
|
|
|
|
* http://paperjs.org/
|
|
|
|
* http://scriptographer.org/
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-05-16 09:15:47 -04:00
|
|
|
* Copyright (c) 2011, Juerg Lehni & Jonathan Puckey
|
|
|
|
* http://lehni.org/ & http://jonathanpuckey.com/
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-07-01 06:17:45 -04:00
|
|
|
* Distributed under the MIT license. See LICENSE file for details.
|
|
|
|
*
|
2011-05-16 09:15:47 -04:00
|
|
|
* All rights reserved.
|
|
|
|
*/
|
|
|
|
|
2011-06-22 18:56:05 -04:00
|
|
|
/**
|
|
|
|
* @name PointText
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-22 18:56:05 -04:00
|
|
|
* @class A PointText item represents a piece of typography in your Paper.js
|
|
|
|
* project which starts from a certain point and extends by the amount of
|
|
|
|
* characters contained in it.
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-22 18:56:05 -04:00
|
|
|
* @extends TextItem
|
|
|
|
*/
|
|
|
|
var PointText = this.PointText = TextItem.extend(/** @lends PointText# */{
|
2012-11-04 01:43:18 -05:00
|
|
|
_type: 'pointtext',
|
2011-05-23 13:13:51 -04:00
|
|
|
/**
|
|
|
|
* Creates a point text item
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-05-23 13:13:51 -04:00
|
|
|
* @param {Point} point the position where the text will start
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-22 18:56:05 -04:00
|
|
|
* @example
|
|
|
|
* var text = new PointText(new Point(50, 100));
|
2011-06-20 19:12:47 -04:00
|
|
|
* text.justification = 'center';
|
2011-06-22 18:56:05 -04:00
|
|
|
* text.fillColor = 'black';
|
|
|
|
* text.content = 'The contents of the point text';
|
2011-05-23 13:13:51 -04:00
|
|
|
*/
|
2011-12-20 17:33:53 -05:00
|
|
|
initialize: function(pointOrMatrix) {
|
|
|
|
this.base(pointOrMatrix);
|
|
|
|
this._point = this._matrix.getTranslation();
|
2011-05-16 09:15:47 -04:00
|
|
|
},
|
|
|
|
|
2011-05-19 16:56:49 -04:00
|
|
|
clone: function() {
|
2011-12-20 17:33:53 -05:00
|
|
|
return this._clone(new PointText(this._matrix));
|
2011-05-19 16:56:49 -04:00
|
|
|
},
|
|
|
|
|
2011-05-23 13:13:51 -04:00
|
|
|
/**
|
|
|
|
* The PointText's anchor point
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-05-23 13:13:51 -04:00
|
|
|
* @type Point
|
|
|
|
* @bean
|
|
|
|
*/
|
2011-05-16 09:15:47 -04:00
|
|
|
getPoint: function() {
|
2011-08-02 11:08:00 -04:00
|
|
|
// Se Item#getPosition for an explanation why we create new LinkedPoint
|
|
|
|
// objects each time.
|
|
|
|
return LinkedPoint.create(this, 'setPoint',
|
|
|
|
this._point.x, this._point.y);
|
2011-05-16 09:15:47 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
setPoint: function(point) {
|
2011-08-03 17:37:56 -04:00
|
|
|
this.translate(Point.read(arguments).subtract(this._point));
|
2011-05-16 09:15:47 -04:00
|
|
|
},
|
2011-06-30 06:01:51 -04:00
|
|
|
|
2011-12-18 10:54:21 -05:00
|
|
|
_transform: function(matrix) {
|
|
|
|
// Transform _point:
|
2011-08-02 11:08:00 -04:00
|
|
|
matrix._transformPoint(this._point, this._point);
|
2011-05-16 09:15:47 -04:00
|
|
|
},
|
2011-06-30 06:01:51 -04:00
|
|
|
|
2011-05-16 09:15:47 -04:00
|
|
|
draw: function(ctx) {
|
2011-06-19 18:28:41 -04:00
|
|
|
if (!this._content)
|
2011-05-16 09:15:47 -04:00
|
|
|
return;
|
2011-12-19 16:40:14 -05:00
|
|
|
this._setStyles(ctx);
|
2011-12-19 17:07:14 -05:00
|
|
|
var style = this._style,
|
|
|
|
leading = this.getLeading(),
|
|
|
|
lines = this._lines;
|
2011-12-19 17:05:22 -05:00
|
|
|
ctx.font = style.getFontStyle();
|
2011-06-20 13:53:36 -04:00
|
|
|
ctx.textAlign = this.getJustification();
|
2011-12-19 17:07:14 -05:00
|
|
|
for (var i = 0, l = lines.length; i < l; i++) {
|
|
|
|
var line = lines[i];
|
|
|
|
if (style._fillColor)
|
2011-11-23 12:12:41 -05:00
|
|
|
ctx.fillText(line, 0, 0);
|
2011-12-19 17:07:14 -05:00
|
|
|
if (style._strokeColor)
|
2011-11-23 12:12:41 -05:00
|
|
|
ctx.strokeText(line, 0, 0);
|
|
|
|
ctx.translate(0, leading);
|
2011-05-16 09:15:47 -04:00
|
|
|
}
|
2012-11-23 15:49:07 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
// TODO: Should we be drawing the selection like this? Perhaps a small
|
|
|
|
// rectangle for the starting point of the text and a colored line on
|
|
|
|
// the baseline for its width?
|
|
|
|
drawSelected: function(ctx, matrix) {
|
|
|
|
Item.drawSelectedBounds(this._getBounds(), ctx, matrix);
|
2011-05-16 09:15:47 -04:00
|
|
|
}
|
2011-11-23 12:12:41 -05:00
|
|
|
}, new function() {
|
|
|
|
var context = null;
|
|
|
|
|
|
|
|
return {
|
2012-12-15 11:19:10 -05:00
|
|
|
_getBounds: function(getter, matrix) {
|
2011-11-23 12:12:41 -05:00
|
|
|
// Create an in-memory canvas on which to do the measuring
|
|
|
|
if (!context)
|
2011-11-26 05:40:38 -05:00
|
|
|
context = CanvasProvider.getCanvas(
|
|
|
|
Size.create(1, 1)).getContext('2d');
|
2011-11-23 12:12:41 -05:00
|
|
|
var justification = this.getJustification(),
|
|
|
|
x = 0;
|
|
|
|
// Measure the real width of the text. Unfortunately, there is no
|
|
|
|
// sane way to measure text height with canvas
|
2011-12-19 17:05:22 -05:00
|
|
|
context.font = this._style.getFontStyle();
|
2011-11-23 12:12:41 -05:00
|
|
|
var width = 0;
|
|
|
|
for (var i = 0, l = this._lines.length; i < l; i++)
|
2011-11-26 05:40:38 -05:00
|
|
|
width = Math.max(width, context.measureText(
|
|
|
|
this._lines[i]).width);
|
2011-11-23 12:12:41 -05:00
|
|
|
// Adjust for different justifications
|
2011-11-26 05:40:38 -05:00
|
|
|
if (justification !== 'left')
|
2011-11-23 12:12:41 -05:00
|
|
|
x -= width / (justification === 'center' ? 2: 1);
|
2011-11-26 05:40:38 -05:00
|
|
|
var leading = this.getLeading(),
|
|
|
|
count = this._lines.length,
|
|
|
|
// Until we don't have baseline measuring, assume leading / 4 as
|
|
|
|
// a rough guess:
|
|
|
|
bounds = Rectangle.create(x,
|
|
|
|
count ? leading / 4 + (count - 1) * leading : 0,
|
|
|
|
width, -count * leading);
|
|
|
|
return matrix ? matrix._transformBounds(bounds, bounds) : bounds;
|
2011-11-23 12:12:41 -05:00
|
|
|
}
|
|
|
|
};
|
2011-05-16 09:15:47 -04:00
|
|
|
});
|