89 lines
1.9 KiB
JavaScript
89 lines
1.9 KiB
JavaScript
/**
|
|
* Text Utilities
|
|
*/
|
|
(function () {
|
|
|
|
var
|
|
F = Flotr,
|
|
D = F.DOM,
|
|
_ = F._,
|
|
|
|
Text = function (o) {
|
|
this.o = o;
|
|
};
|
|
|
|
Text.prototype = {
|
|
|
|
dimensions : function (text, canvasStyle, htmlStyle, className) {
|
|
|
|
if (!text) return { width : 0, height : 0 };
|
|
|
|
return (this.o.html) ?
|
|
this.html(text, this.o.element, htmlStyle, className) :
|
|
this.canvas(text, canvasStyle);
|
|
},
|
|
|
|
canvas : function (text, style) {
|
|
|
|
if (!this.o.textEnabled) return;
|
|
style = style || {};
|
|
|
|
var
|
|
metrics = this.measureText(text, style),
|
|
width = metrics.width,
|
|
height = style.size || F.defaultOptions.fontSize,
|
|
angle = style.angle || 0,
|
|
cosAngle = Math.cos(angle),
|
|
sinAngle = Math.sin(angle),
|
|
widthPadding = 2,
|
|
heightPadding = 6,
|
|
bounds;
|
|
|
|
bounds = {
|
|
width: Math.abs(cosAngle * width) + Math.abs(sinAngle * height) + widthPadding,
|
|
height: Math.abs(sinAngle * width) + Math.abs(cosAngle * height) + heightPadding
|
|
};
|
|
|
|
return bounds;
|
|
},
|
|
|
|
html : function (text, element, style, className) {
|
|
|
|
var div = D.create('div');
|
|
|
|
D.setStyles(div, { 'position' : 'absolute', 'top' : '-10000px' });
|
|
D.insert(div, '<div style="'+style+'" class="'+className+' flotr-dummy-div">' + text + '</div>');
|
|
D.insert(this.o.element, div);
|
|
|
|
return D.size(div);
|
|
},
|
|
|
|
measureText : function (text, style) {
|
|
|
|
var
|
|
context = this.o.ctx,
|
|
metrics;
|
|
|
|
if (!context.fillText || (F.isIphone && context.measure)) {
|
|
return { width : context.measure(text, style)};
|
|
}
|
|
|
|
style = _.extend({
|
|
size: F.defaultOptions.fontSize,
|
|
weight: 1,
|
|
angle: 0
|
|
}, style);
|
|
|
|
context.save();
|
|
context.font = (style.weight > 1 ? "bold " : "") + (style.size*1.3) + "px sans-serif";
|
|
metrics = context.measureText(text);
|
|
context.restore();
|
|
|
|
return metrics;
|
|
}
|
|
};
|
|
|
|
Flotr.Text = Text;
|
|
|
|
})();
|