2012-07-09 21:08:30 +08:00
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
// Text Path
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
/**
|
|
|
|
* Path constructor.
|
|
|
|
* @author Jason Follas
|
|
|
|
* @constructor
|
2012-07-28 10:51:18 +08:00
|
|
|
* @augments Kinetic.Shape
|
2012-07-09 21:08:30 +08:00
|
|
|
* @param {Object} config
|
|
|
|
*/
|
2012-07-28 10:51:18 +08:00
|
|
|
Kinetic.Plugins.TextPath = Kinetic.Shape.extend({
|
2012-07-28 00:25:36 +08:00
|
|
|
init : function (config) {
|
|
|
|
|
|
|
|
this.setDefaultAttrs({
|
|
|
|
fontFamily : 'Calibri',
|
|
|
|
fontSize : 12,
|
|
|
|
fontStyle : 'normal',
|
|
|
|
detectionType : 'path',
|
|
|
|
text : ''
|
|
|
|
});
|
|
|
|
|
|
|
|
this.dummyCanvas = document.createElement('canvas');
|
|
|
|
this.shapeType = "TextPath";
|
|
|
|
this.dataArray = [];
|
|
|
|
var that = this;
|
|
|
|
|
|
|
|
config.drawFunc = function (context) {
|
|
|
|
var charArr = this.charArr;
|
2012-07-09 21:08:30 +08:00
|
|
|
|
2012-07-28 00:25:36 +08:00
|
|
|
context.font = this.attrs.fontStyle + ' ' + this.attrs.fontSize + 'pt ' + this.attrs.fontFamily;
|
|
|
|
context.textBaseline = 'middle';
|
|
|
|
context.textAlign = 'left';
|
|
|
|
context.save();
|
2012-07-09 21:08:30 +08:00
|
|
|
|
2012-07-28 00:25:36 +08:00
|
|
|
var glyphInfo = this.glyphInfo;
|
2012-07-09 21:08:30 +08:00
|
|
|
|
2012-07-28 00:25:36 +08:00
|
|
|
for (var i = 0; i < glyphInfo.length; i++) {
|
|
|
|
context.save();
|
|
|
|
|
|
|
|
var p0 = glyphInfo[i].p0;
|
|
|
|
var p1 = glyphInfo[i].p1;
|
|
|
|
var ht = parseFloat(this.attrs.fontSize);
|
|
|
|
|
|
|
|
context.translate(p0.x, p0.y);
|
|
|
|
|
|
|
|
context.rotate(glyphInfo[i].rotation);
|
|
|
|
|
|
|
|
this.fillText(context, glyphInfo[i].text);
|
|
|
|
this.strokeText(context, glyphInfo[i].text);
|
|
|
|
|
|
|
|
context.restore();
|
|
|
|
|
|
|
|
//// To assist with debugging visually, uncomment following
|
|
|
|
// context.beginPath();
|
|
|
|
// if (i % 2)
|
2012-07-09 21:08:30 +08:00
|
|
|
// context.strokeStyle = 'cyan';
|
2012-07-28 00:25:36 +08:00
|
|
|
// else
|
2012-07-09 21:08:30 +08:00
|
|
|
// context.strokeStyle = 'green';
|
2012-07-28 00:25:36 +08:00
|
|
|
|
|
|
|
// context.moveTo(p0.x, p0.y);
|
|
|
|
// context.lineTo(p1.x, p1.y);
|
|
|
|
// context.stroke();
|
|
|
|
}
|
2012-07-09 21:08:30 +08:00
|
|
|
|
2012-07-28 00:25:36 +08:00
|
|
|
context.restore();
|
|
|
|
};
|
|
|
|
|
|
|
|
// call super constructor
|
|
|
|
this._super(config);
|
|
|
|
|
2012-07-28 10:51:18 +08:00
|
|
|
this.dataArray = Kinetic.Plugins.PathHelper.parsePathData(this.attrs.data);
|
2012-07-28 00:25:36 +08:00
|
|
|
|
|
|
|
this.on('dataChange', function () {
|
2012-07-28 10:51:18 +08:00
|
|
|
that.dataArray = Kinetic.Plugins.PathHelper.parsePathData(that.attrs.data);
|
2012-07-28 00:25:36 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
// update text data for certain attr changes
|
2012-07-28 10:51:18 +08:00
|
|
|
var attrs = ['text', 'textStroke', 'textStrokeWidth'];
|
2012-07-28 00:25:36 +08:00
|
|
|
for (var n = 0; n < attrs.length; n++) {
|
|
|
|
var attr = attrs[n];
|
|
|
|
this.on(attr + 'Change', that._setTextData);
|
2012-07-09 21:08:30 +08:00
|
|
|
}
|
2012-07-28 00:25:36 +08:00
|
|
|
|
|
|
|
that._setTextData();
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* get text width in pixels
|
2012-07-28 10:51:18 +08:00
|
|
|
* @name getTextWidth
|
|
|
|
* @methodOf Kinetic.Plugins.TextPath.prototype
|
2012-07-28 00:25:36 +08:00
|
|
|
*/
|
|
|
|
getTextWidth : function () {
|
|
|
|
return this.textWidth;
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* get text height in pixels
|
2012-07-28 10:51:18 +08:00
|
|
|
* @name getTextHeight
|
|
|
|
* @methodOf Kinetic.Plugins.TextPath.prototype
|
2012-07-28 00:25:36 +08:00
|
|
|
*/
|
|
|
|
getTextHeight : function () {
|
|
|
|
return this.textHeight;
|
|
|
|
},
|
|
|
|
_getTextSize : function (text) {
|
|
|
|
var dummyCanvas = this.dummyCanvas;
|
|
|
|
var context = dummyCanvas.getContext('2d');
|
|
|
|
|
|
|
|
context.save();
|
|
|
|
|
|
|
|
context.font = this.attrs.fontStyle + ' ' + this.attrs.fontSize + 'pt ' + this.attrs.fontFamily;
|
|
|
|
var metrics = context.measureText(text);
|
|
|
|
|
2012-07-09 21:08:30 +08:00
|
|
|
context.restore();
|
|
|
|
|
2012-07-28 00:25:36 +08:00
|
|
|
return {
|
|
|
|
width : metrics.width,
|
|
|
|
height : parseInt(this.attrs.fontSize, 10)
|
|
|
|
};
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* set text data.
|
|
|
|
*/
|
|
|
|
_setTextData : function () {
|
2012-07-09 21:08:30 +08:00
|
|
|
|
2012-07-28 00:25:36 +08:00
|
|
|
var that = this;
|
|
|
|
var size = this._getTextSize(this.attrs.text);
|
|
|
|
this.textWidth = size.width;
|
|
|
|
this.textHeight = size.height;
|
2012-07-09 21:08:30 +08:00
|
|
|
|
2012-07-28 00:25:36 +08:00
|
|
|
this.glyphInfo = [];
|
2012-07-09 21:08:30 +08:00
|
|
|
|
2012-07-28 00:25:36 +08:00
|
|
|
var charArr = this.attrs.text.split('');
|
2012-07-09 21:08:30 +08:00
|
|
|
|
2012-07-28 00:25:36 +08:00
|
|
|
var p0,
|
|
|
|
p1,
|
|
|
|
pathCmd;
|
2012-07-09 21:08:30 +08:00
|
|
|
|
2012-07-28 00:25:36 +08:00
|
|
|
var pIndex = -1;
|
|
|
|
var currentT = 0;
|
2012-07-09 21:08:30 +08:00
|
|
|
|
2012-07-28 00:25:36 +08:00
|
|
|
var getNextPathSegment = function () {
|
|
|
|
currentT = 0;
|
|
|
|
var pathData = that.dataArray;
|
|
|
|
|
|
|
|
for (var i = pIndex + 1; i < pathData.length; i++) {
|
|
|
|
if (pathData[i].pathLength > 0) {
|
|
|
|
pIndex = i;
|
|
|
|
|
|
|
|
return pathData[i];
|
|
|
|
} else if (pathData[i].command == 'M') {
|
|
|
|
p0 = {
|
|
|
|
x : pathData[i].points[0],
|
|
|
|
y : pathData[i].points[1]
|
|
|
|
};
|
2012-07-09 21:08:30 +08:00
|
|
|
}
|
|
|
|
}
|
2012-07-28 00:25:36 +08:00
|
|
|
|
|
|
|
return {};
|
|
|
|
};
|
2012-07-09 21:08:30 +08:00
|
|
|
|
2012-07-28 00:25:36 +08:00
|
|
|
var findSegmentToFitCharacter = function (c, before) {
|
2012-07-09 21:08:30 +08:00
|
|
|
|
2012-07-28 00:25:36 +08:00
|
|
|
var glyphWidth = that._getTextSize(c).width;
|
|
|
|
|
|
|
|
var currLen = 0;
|
|
|
|
var attempts = 0;
|
|
|
|
var needNextSegment = false;
|
|
|
|
|
|
|
|
p1 = undefined;
|
|
|
|
while (Math.abs(glyphWidth - currLen) / glyphWidth > 0.01 && attempts < 25) {
|
|
|
|
attempts++;
|
|
|
|
var cumulativePathLength = currLen;
|
|
|
|
while (pathCmd === undefined) {
|
|
|
|
pathCmd = getNextPathSegment();
|
|
|
|
|
|
|
|
if (pathCmd && cumulativePathLength + pathCmd.pathLength < glyphWidth) {
|
|
|
|
cumulativePathLength += pathCmd.pathLength;
|
|
|
|
pathCmd = undefined;
|
2012-07-09 21:08:30 +08:00
|
|
|
}
|
2012-07-28 00:25:36 +08:00
|
|
|
}
|
|
|
|
|
2012-07-28 10:51:18 +08:00
|
|
|
if (pathCmd === {} || p0 === undefined)
|
2012-07-28 00:25:36 +08:00
|
|
|
return undefined;
|
|
|
|
|
|
|
|
var needNewSegment = false;
|
|
|
|
|
|
|
|
switch (pathCmd.command) {
|
|
|
|
case 'L':
|
2012-07-28 10:51:18 +08:00
|
|
|
if (Kinetic.Plugins.PathHelper.getLineLength(p0.x, p0.y, pathCmd.points[0], pathCmd.points[1]) > glyphWidth) {
|
|
|
|
p1 = Kinetic.Plugins.PathHelper.getPointOnLine(glyphWidth, p0.x, p0.y, pathCmd.points[0], pathCmd.points[1], p0.x, p0.y);
|
2012-07-28 00:25:36 +08:00
|
|
|
} else
|
2012-07-09 21:08:30 +08:00
|
|
|
pathCmd = undefined;
|
|
|
|
break;
|
|
|
|
case 'A':
|
2012-07-28 00:25:36 +08:00
|
|
|
|
2012-07-09 21:08:30 +08:00
|
|
|
var start = pathCmd.points[4]; // 4 = theta
|
2012-07-28 00:25:36 +08:00
|
|
|
var dTheta = pathCmd.points[5]; // 5 = dTheta
|
|
|
|
var end = pathCmd.points[4] + dTheta;
|
2012-07-09 21:08:30 +08:00
|
|
|
|
2012-07-28 00:25:36 +08:00
|
|
|
if (currentT === 0)
|
|
|
|
currentT = start + 0.00000001; // Just in case start is 0
|
2012-07-09 21:08:30 +08:00
|
|
|
else if (glyphWidth > currLen)
|
|
|
|
currentT += (Math.PI / 180.0) * dTheta / Math.abs(dTheta);
|
|
|
|
else
|
2012-07-28 00:25:36 +08:00
|
|
|
currentT -= Math.PI / 360.0 * dTheta / Math.abs(dTheta);
|
2012-07-09 21:08:30 +08:00
|
|
|
|
|
|
|
if (Math.abs(currentT) > Math.abs(end)) {
|
2012-07-28 00:25:36 +08:00
|
|
|
|
2012-07-09 21:08:30 +08:00
|
|
|
currentT = end;
|
|
|
|
needNewSegment = true;
|
|
|
|
}
|
2012-07-28 00:25:36 +08:00
|
|
|
|
2012-07-28 10:51:18 +08:00
|
|
|
p1 = Kinetic.Plugins.PathHelper.getPointOnEllipticalArc(pathCmd.points[0], pathCmd.points[1], pathCmd.points[2], pathCmd.points[3], currentT, pathCmd.points[6]);
|
2012-07-09 21:08:30 +08:00
|
|
|
break;
|
|
|
|
case 'C':
|
2012-07-28 00:25:36 +08:00
|
|
|
if (currentT === 0) {
|
2012-07-09 21:08:30 +08:00
|
|
|
if (glyphWidth > pathCmd.pathLength)
|
|
|
|
currentT = 0.00000001;
|
|
|
|
else
|
|
|
|
currentT = glyphWidth / pathCmd.pathLength;
|
2012-07-28 00:25:36 +08:00
|
|
|
} else if (glyphWidth > currLen)
|
2012-07-09 21:08:30 +08:00
|
|
|
currentT += (glyphWidth - currLen) / pathCmd.pathLength;
|
|
|
|
else
|
|
|
|
currentT -= (currLen - glyphWidth) / pathCmd.pathLength;
|
2012-07-28 00:25:36 +08:00
|
|
|
|
2012-07-09 21:08:30 +08:00
|
|
|
if (currentT > 1.0) {
|
|
|
|
currentT = 1.0;
|
|
|
|
needNewSegment = true;
|
|
|
|
}
|
2012-07-28 00:25:36 +08:00
|
|
|
|
2012-07-28 10:51:18 +08:00
|
|
|
p1 = Kinetic.Plugins.PathHelper.getPointOnCubicBezier(currentT, pathCmd.start.x, pathCmd.start.y, pathCmd.points[0], pathCmd.points[1], pathCmd.points[2], pathCmd.points[3], pathCmd.points[4], pathCmd.points[5]);
|
2012-07-09 21:08:30 +08:00
|
|
|
break;
|
|
|
|
case 'Q':
|
2012-07-28 00:25:36 +08:00
|
|
|
if (currentT === 0)
|
2012-07-09 21:08:30 +08:00
|
|
|
currentT = glyphWidth / pathCmd.pathLength;
|
|
|
|
else if (glyphWidth > currLen)
|
|
|
|
currentT += (glyphWidth - currLen) / pathCmd.pathLength;
|
|
|
|
else
|
|
|
|
currentT -= (currLen - glyphWidth) / pathCmd.pathLength;
|
2012-07-28 00:25:36 +08:00
|
|
|
|
2012-07-09 21:08:30 +08:00
|
|
|
if (currentT > 1.0) {
|
|
|
|
currentT = 1.0;
|
|
|
|
needNewSegment = true;
|
|
|
|
}
|
2012-07-28 00:25:36 +08:00
|
|
|
|
2012-07-28 10:51:18 +08:00
|
|
|
p1 = Kinetic.Plugins.PathHelper.getPointOnQuadraticBezier(currentT, pathCmd.start.x, pathCmd.start.y, pathCmd.points[0], pathCmd.points[1], pathCmd.points[2], pathCmd.points[3]);
|
2012-07-28 00:25:36 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p1 !== undefined) {
|
2012-07-28 10:51:18 +08:00
|
|
|
currLen = Kinetic.Plugins.PathHelper.getLineLength(p0.x, p0.y, p1.x, p1.y);
|
2012-07-28 00:25:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (needNewSegment) {
|
|
|
|
needNewSegment = false;
|
|
|
|
pathCmd = undefined;
|
|
|
|
}
|
2012-07-09 21:08:30 +08:00
|
|
|
}
|
2012-07-28 00:25:36 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
for (var i = 0; i < charArr.length; i++) {
|
2012-07-09 21:08:30 +08:00
|
|
|
|
2012-07-28 00:25:36 +08:00
|
|
|
// Find p1 such that line segment between p0 and p1 is approx. width of glyph
|
|
|
|
findSegmentToFitCharacter(charArr[i]);
|
2012-07-09 21:08:30 +08:00
|
|
|
|
2012-07-28 00:25:36 +08:00
|
|
|
if (p0 === undefined || p1 === undefined)
|
|
|
|
break;
|
2012-07-09 21:08:30 +08:00
|
|
|
|
2012-07-28 10:51:18 +08:00
|
|
|
var width = Kinetic.Plugins.PathHelper.getLineLength(p0.x, p0.y, p1.x, p1.y);
|
2012-07-28 00:25:36 +08:00
|
|
|
|
|
|
|
// Note: Since glyphs are rendered one at a time, any kerning pair data built into the font will not be used.
|
|
|
|
// Can foresee having a rough pair table built in that the developer can override as needed.
|
|
|
|
|
|
|
|
var kern = 0; // placeholder for future implementation
|
|
|
|
|
2012-07-28 10:51:18 +08:00
|
|
|
var midpoint = Kinetic.Plugins.PathHelper.getPointOnLine(kern + width / 2.0, p0.x, p0.y, p1.x, p1.y);
|
2012-07-28 00:25:36 +08:00
|
|
|
|
|
|
|
var rotation = Math.atan2((p1.y - p0.y), (p1.x - p0.x));
|
|
|
|
this.glyphInfo.push({
|
|
|
|
transposeX : midpoint.x,
|
|
|
|
transposeY : midpoint.y,
|
|
|
|
text : charArr[i],
|
|
|
|
rotation : rotation,
|
|
|
|
p0 : p0,
|
|
|
|
p1 : p1
|
|
|
|
});
|
|
|
|
|
|
|
|
p0 = p1;
|
|
|
|
}
|
2012-07-09 21:08:30 +08:00
|
|
|
}
|
2012-07-28 00:25:36 +08:00
|
|
|
});
|
2012-07-09 21:08:30 +08:00
|
|
|
|
|
|
|
// add setters and getters
|
2012-07-28 10:51:18 +08:00
|
|
|
Kinetic.Node.addGettersSetters(Kinetic.Plugins.TextPath, ['fontFamily', 'fontSize', 'fontStyle', 'textFill', 'textStroke', 'textStrokeWidth', 'text']);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set font family
|
|
|
|
* @name setFontFamily
|
2012-07-29 00:04:04 +08:00
|
|
|
* @methodOf Kinetic.Plugins.TextPath.prototype
|
2012-07-28 10:51:18 +08:00
|
|
|
* @param {String} fontFamily
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set font size
|
|
|
|
* @name setFontSize
|
2012-07-29 00:04:04 +08:00
|
|
|
* @methodOf Kinetic.Plugins.TextPath.prototype
|
2012-07-28 10:51:18 +08:00
|
|
|
* @param {int} fontSize
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set font style. Can be "normal", "italic", or "bold". "normal" is the default.
|
|
|
|
* @name setFontStyle
|
2012-07-29 00:04:04 +08:00
|
|
|
* @methodOf Kinetic.Plugins.TextPath.prototype
|
2012-07-28 10:51:18 +08:00
|
|
|
* @param {String} fontStyle
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set text fill color
|
|
|
|
* @name setTextFill
|
2012-07-29 00:04:04 +08:00
|
|
|
* @methodOf Kinetic.Plugins.TextPath.prototype
|
2012-07-28 10:51:18 +08:00
|
|
|
* @param {String} textFill
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set text stroke color
|
|
|
|
* @name setFontStroke
|
2012-07-29 00:04:04 +08:00
|
|
|
* @methodOf Kinetic.Plugins.TextPath.prototype
|
2012-07-28 10:51:18 +08:00
|
|
|
* @param {String} textStroke
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set text stroke width
|
|
|
|
* @name setTextStrokeWidth
|
2012-07-29 00:04:04 +08:00
|
|
|
* @methodOf Kinetic.Plugins.TextPath.prototype
|
2012-07-28 10:51:18 +08:00
|
|
|
* @param {int} textStrokeWidth
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set text
|
|
|
|
* @name setText
|
2012-07-29 00:04:04 +08:00
|
|
|
* @methodOf Kinetic.Plugins.TextPath.prototype
|
2012-07-28 10:51:18 +08:00
|
|
|
* @param {String} text
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get font family
|
|
|
|
* @name getFontFamily
|
2012-07-29 00:04:04 +08:00
|
|
|
* @methodOf Kinetic.Plugins.TextPath.prototype
|
2012-07-28 10:51:18 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get font size
|
|
|
|
* @name getFontSize
|
2012-07-29 00:04:04 +08:00
|
|
|
* @methodOf Kinetic.Plugins.TextPath.prototype
|
2012-07-28 10:51:18 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get font style
|
|
|
|
* @name getFontStyle
|
2012-07-29 00:04:04 +08:00
|
|
|
* @methodOf Kinetic.Plugins.TextPath.prototype
|
2012-07-28 10:51:18 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get text fill color
|
|
|
|
* @name getTextFill
|
2012-07-29 00:04:04 +08:00
|
|
|
* @methodOf Kinetic.Plugins.TextPath.prototype
|
2012-07-28 10:51:18 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get text stroke color
|
|
|
|
* @name getTextStroke
|
2012-07-29 00:04:04 +08:00
|
|
|
* @methodOf Kinetic.Plugins.TextPath.prototype
|
2012-07-28 10:51:18 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get text stroke width
|
|
|
|
* @name getTextStrokeWidth
|
2012-07-29 00:04:04 +08:00
|
|
|
* @methodOf Kinetic.Plugins.TextPath.prototype
|
2012-07-28 10:51:18 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get text
|
|
|
|
* @name getText
|
2012-07-29 00:04:04 +08:00
|
|
|
* @methodOf Kinetic.Plugins.TextPath.prototype
|
2012-07-28 10:51:18 +08:00
|
|
|
*/
|