2011-05-16 09:15:47 -04:00
|
|
|
/*
|
2013-01-28 21:03:27 -05:00
|
|
|
* Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
|
2011-05-16 09:15:47 -04:00
|
|
|
* http://paperjs.org/
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2013-01-28 21:03:27 -05:00
|
|
|
* Copyright (c) 2011 - 2013, Juerg Lehni & Jonathan Puckey
|
2011-05-16 09:15:47 -04:00
|
|
|
* 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
|
|
|
|
*/
|
2013-05-27 15:48:58 -04:00
|
|
|
var PointText = TextItem.extend(/** @lends PointText# */{
|
2011-05-23 13:13:51 -04:00
|
|
|
/**
|
|
|
|
* Creates a point text item
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2012-12-25 11:57:04 -05:00
|
|
|
* @name PointText#initialize
|
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
|
|
|
*
|
2013-04-21 09:59:51 -04:00
|
|
|
* @example {@paperscript}
|
|
|
|
* var text = new PointText(new Point(200, 50));
|
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';
|
2013-04-21 09:59:51 -04:00
|
|
|
*
|
|
|
|
* @example {@paperscript}
|
|
|
|
* // Using object notation:
|
|
|
|
* var text = new PointText({
|
|
|
|
* point: [50, 50],
|
|
|
|
* content: 'The contents of the point text',
|
|
|
|
* fillColor: 'black',
|
|
|
|
* fontSize: 25
|
|
|
|
* });
|
2011-05-23 13:13:51 -04:00
|
|
|
*/
|
2013-05-27 15:48:58 -04:00
|
|
|
initialize: function PointText() {
|
|
|
|
TextItem.apply(this, arguments);
|
|
|
|
},
|
2011-05-16 09:15:47 -04:00
|
|
|
|
2011-05-19 16:56:49 -04:00
|
|
|
clone: function() {
|
2012-12-25 11:57:04 -05:00
|
|
|
return this._clone(new PointText());
|
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.
|
2012-12-25 11:57:04 -05:00
|
|
|
var point = this._matrix.getTranslation();
|
|
|
|
return LinkedPoint.create(this, 'setPoint', point.x, point.y);
|
2011-05-16 09:15:47 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
setPoint: function(point) {
|
2012-12-25 11:57:04 -05:00
|
|
|
this.translate(Point.read(arguments).subtract(
|
|
|
|
this._matrix.getTranslation()));
|
2011-05-16 09:15:47 -04:00
|
|
|
},
|
2011-06-30 06:01:51 -04:00
|
|
|
|
2013-04-18 19:58:35 -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,
|
2013-04-09 19:46:20 -04:00
|
|
|
lines = this._lines,
|
|
|
|
leading = style.getLeading();
|
2011-12-19 17:05:22 -05:00
|
|
|
ctx.font = style.getFontStyle();
|
2013-04-09 19:46:20 -04:00
|
|
|
ctx.textAlign = style.getJustification();
|
2011-12-19 17:07:14 -05:00
|
|
|
for (var i = 0, l = lines.length; i < l; i++) {
|
|
|
|
var line = lines[i];
|
2013-04-19 22:31:29 -04:00
|
|
|
if (style.getFillColor())
|
2011-11-23 12:12:41 -05:00
|
|
|
ctx.fillText(line, 0, 0);
|
2013-04-19 22:31:29 -04:00
|
|
|
if (style.getStrokeColor())
|
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
|
|
|
}
|
|
|
|
}
|
2011-11-23 12:12:41 -05:00
|
|
|
}, new function() {
|
2013-02-12 19:06:24 -05:00
|
|
|
var measureCtx = null;
|
2011-11-23 12:12:41 -05:00
|
|
|
|
|
|
|
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
|
2013-02-12 19:06:24 -05:00
|
|
|
if (!measureCtx)
|
|
|
|
measureCtx = CanvasProvider.getContext(1, 1);
|
2013-04-09 19:46:20 -04:00
|
|
|
var style = this._style,
|
|
|
|
lines = this._lines,
|
|
|
|
count = lines.length,
|
|
|
|
justification = style.getJustification(),
|
|
|
|
leading = style.getLeading(),
|
2011-11-23 12:12:41 -05:00
|
|
|
x = 0;
|
|
|
|
// Measure the real width of the text. Unfortunately, there is no
|
|
|
|
// sane way to measure text height with canvas
|
2013-04-09 19:46:20 -04:00
|
|
|
measureCtx.font = style.getFontStyle();
|
2011-11-23 12:12:41 -05:00
|
|
|
var width = 0;
|
2013-04-09 19:46:20 -04:00
|
|
|
for (var i = 0; i < count; i++)
|
|
|
|
width = Math.max(width, measureCtx.measureText(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);
|
2013-04-09 19:46:20 -04:00
|
|
|
// Until we don't have baseline measuring, assume leading / 4 as a
|
|
|
|
// rough guess:
|
|
|
|
var bounds = Rectangle.create(x,
|
2011-11-26 05:40:38 -05:00
|
|
|
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
|
|
|
});
|