2017-03-15 01:07:58 +00:00
|
|
|
var Class = require('../../utils/Class');
|
2017-04-25 17:07:15 +00:00
|
|
|
// var GetObjectValue = require('../../utils/object/GetObjectValue');
|
|
|
|
var GetAdvancedValue = require('../../utils/object/GetAdvancedValue');
|
2017-03-20 16:09:01 +00:00
|
|
|
var MeasureText = require('./MeasureText');
|
2017-03-15 01:07:58 +00:00
|
|
|
|
2017-03-16 17:15:58 +00:00
|
|
|
// Key: [ Object Key, Default Value ]
|
|
|
|
|
|
|
|
var propertyMap = {
|
|
|
|
font: [ 'font', '16px Courier' ],
|
|
|
|
backgroundColor: [ 'backgroundColor', null ],
|
|
|
|
fill: [ 'fill', '#fff' ],
|
|
|
|
stroke: [ 'stroke', '#fff' ],
|
|
|
|
strokeThickness: [ 'strokeThickness', 0 ],
|
|
|
|
shadowOffsetX: [ 'shadow.offsetX', 0 ],
|
|
|
|
shadowOffsetY: [ 'shadow.offsetY', 0 ],
|
|
|
|
shadowColor: [ 'shadow.color', '#000' ],
|
|
|
|
shadowBlur: [ 'shadow.blur', 0 ],
|
|
|
|
shadowStroke: [ 'shadow.stroke', false ],
|
|
|
|
shadowFill: [ 'shadow.fill', false ],
|
|
|
|
align: [ 'align', 'left' ],
|
2017-03-20 16:09:01 +00:00
|
|
|
maxLines: [ 'maxLines', 0 ],
|
|
|
|
fixedWidth: [ 'fixedWidth', false ],
|
2017-03-21 20:25:15 +00:00
|
|
|
fixedHeight: [ 'fixedHeight', false ],
|
|
|
|
rtl: [ 'rtl', false ]
|
2017-03-16 17:15:58 +00:00
|
|
|
};
|
|
|
|
|
2017-03-15 01:07:58 +00:00
|
|
|
var TextStyle = new Class({
|
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
|
|
|
function TextStyle (text, style)
|
|
|
|
{
|
2017-03-16 21:59:50 +00:00
|
|
|
this.parent = text;
|
2017-03-15 01:07:58 +00:00
|
|
|
|
2017-03-21 20:25:15 +00:00
|
|
|
this.font;
|
|
|
|
this.backgroundColor;
|
|
|
|
this.fill;
|
|
|
|
this.stroke;
|
|
|
|
this.strokeThickness;
|
|
|
|
this.shadowOffsetX;
|
|
|
|
this.shadowOffsetY;
|
|
|
|
this.shadowColor;
|
|
|
|
this.shadowBlur;
|
|
|
|
this.shadowStroke;
|
|
|
|
this.shadowFill;
|
|
|
|
this.align;
|
|
|
|
this.maxLines;
|
|
|
|
this.fixedWidth;
|
|
|
|
this.fixedHeight;
|
|
|
|
this.rtl;
|
|
|
|
|
|
|
|
// Set to defaults
|
|
|
|
this.reset();
|
2017-03-16 17:15:58 +00:00
|
|
|
|
2017-04-25 17:07:15 +00:00
|
|
|
if (style)
|
2017-03-16 17:15:58 +00:00
|
|
|
{
|
2017-03-20 16:09:01 +00:00
|
|
|
for (var key in propertyMap)
|
|
|
|
{
|
2017-04-25 17:07:15 +00:00
|
|
|
// this[key] = GetObjectValue(style, propertyMap[key][0], this[key]);
|
|
|
|
this[key] = GetAdvancedValue(style, propertyMap[key][0], this[key]);
|
2017-03-20 16:09:01 +00:00
|
|
|
}
|
2017-03-16 17:15:58 +00:00
|
|
|
}
|
2017-03-20 16:09:01 +00:00
|
|
|
|
|
|
|
this.metrics = MeasureText(this);
|
2017-03-16 17:15:58 +00:00
|
|
|
},
|
2017-03-15 01:07:58 +00:00
|
|
|
|
2017-03-21 20:25:15 +00:00
|
|
|
reset: function ()
|
|
|
|
{
|
|
|
|
for (var key in propertyMap)
|
|
|
|
{
|
|
|
|
this[key] = propertyMap[key][1];
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2017-03-16 21:59:50 +00:00
|
|
|
syncFont: function (canvas, context)
|
|
|
|
{
|
2017-03-21 20:25:15 +00:00
|
|
|
if (this.rtl)
|
|
|
|
{
|
|
|
|
canvas.dir = 'rtl';
|
|
|
|
}
|
|
|
|
|
2017-03-16 21:59:50 +00:00
|
|
|
context.font = this.font;
|
|
|
|
context.textBaseline = 'alphabetic';
|
|
|
|
|
|
|
|
context.fillStyle = this.fill;
|
|
|
|
context.strokeStyle = this.stroke;
|
|
|
|
|
|
|
|
context.lineWidth = this.strokeThickness;
|
|
|
|
context.lineCap = 'round';
|
|
|
|
context.lineJoin = 'round';
|
|
|
|
},
|
|
|
|
|
2017-03-20 16:48:04 +00:00
|
|
|
syncShadow: function (context, enabled)
|
2017-03-16 21:59:50 +00:00
|
|
|
{
|
2017-03-20 16:48:04 +00:00
|
|
|
if (enabled)
|
2017-03-16 21:59:50 +00:00
|
|
|
{
|
2017-03-21 20:25:15 +00:00
|
|
|
context.shadowOffsetX = this.shadowOffsetX;
|
|
|
|
context.shadowOffsetY = this.shadowOffsetY;
|
|
|
|
context.shadowColor = this.shadowColor;
|
|
|
|
context.shadowBlur = this.shadowBlur;
|
2017-03-16 21:59:50 +00:00
|
|
|
}
|
2017-03-20 16:48:04 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
context.shadowOffsetX = 0;
|
|
|
|
context.shadowOffsetY = 0;
|
|
|
|
context.shadowColor = 0;
|
|
|
|
context.shadowBlur = 0;
|
|
|
|
}
|
2017-03-20 16:09:01 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
update: function (recalculateMetrics)
|
|
|
|
{
|
|
|
|
if (recalculateMetrics)
|
2017-03-16 21:59:50 +00:00
|
|
|
{
|
2017-03-20 16:09:01 +00:00
|
|
|
this.metrics = MeasureText(this);
|
2017-03-16 21:59:50 +00:00
|
|
|
}
|
2017-03-20 16:09:01 +00:00
|
|
|
|
2017-03-20 16:28:09 +00:00
|
|
|
return this.parent.updateText();
|
2017-03-16 21:59:50 +00:00
|
|
|
},
|
|
|
|
|
2017-03-16 17:15:58 +00:00
|
|
|
setStyle: function (style)
|
|
|
|
{
|
|
|
|
for (var key in propertyMap)
|
|
|
|
{
|
2017-04-25 17:07:15 +00:00
|
|
|
// this[key] = GetObjectValue(style, propertyMap[key][0], this[key]);
|
|
|
|
this[key] = GetAdvancedValue(style, propertyMap[key][0], this[key]);
|
2017-03-16 17:15:58 +00:00
|
|
|
}
|
2017-03-15 01:07:58 +00:00
|
|
|
|
2017-03-20 16:09:01 +00:00
|
|
|
return this.update(true);
|
2017-03-14 16:37:32 +00:00
|
|
|
},
|
2017-03-15 01:07:58 +00:00
|
|
|
|
2017-03-16 21:59:50 +00:00
|
|
|
setFont: function (font)
|
2017-03-15 01:07:58 +00:00
|
|
|
{
|
2017-03-16 21:59:50 +00:00
|
|
|
this.font = font;
|
2017-03-15 01:07:58 +00:00
|
|
|
|
2017-03-20 16:09:01 +00:00
|
|
|
return this.update(true);
|
|
|
|
},
|
|
|
|
|
|
|
|
setFixedSize: function (width, height)
|
|
|
|
{
|
|
|
|
this.fixedWidth = width;
|
|
|
|
this.fixedHeight = height;
|
|
|
|
|
|
|
|
if (width)
|
|
|
|
{
|
|
|
|
this.text.width = width;
|
|
|
|
}
|
2017-03-16 21:59:50 +00:00
|
|
|
|
2017-03-20 16:09:01 +00:00
|
|
|
if (height)
|
|
|
|
{
|
|
|
|
this.text.height = height;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.update(false);
|
2017-03-16 17:15:58 +00:00
|
|
|
},
|
|
|
|
|
2017-03-16 21:59:50 +00:00
|
|
|
setBackgroundColor: function (color)
|
2017-03-16 17:15:58 +00:00
|
|
|
{
|
2017-03-16 21:59:50 +00:00
|
|
|
this.backgroundColor = color;
|
2017-03-16 17:15:58 +00:00
|
|
|
|
2017-03-20 16:09:01 +00:00
|
|
|
return this.update(false);
|
2017-03-16 17:15:58 +00:00
|
|
|
},
|
2017-03-15 01:07:58 +00:00
|
|
|
|
2017-03-16 21:59:50 +00:00
|
|
|
setFill: function (color)
|
|
|
|
{
|
|
|
|
this.fill = color;
|
|
|
|
|
2017-03-20 16:09:01 +00:00
|
|
|
return this.update(false);
|
2017-03-16 21:59:50 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
setStroke: function (color, thickness)
|
|
|
|
{
|
|
|
|
if (color === undefined)
|
|
|
|
{
|
|
|
|
// Reset the stroke to zero (disabling it)
|
|
|
|
this.strokeThickness = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (thickness === undefined) { thickness = this.strokeThickness; }
|
|
|
|
|
|
|
|
this.stroke = color;
|
|
|
|
this.strokeThickness = thickness;
|
|
|
|
}
|
|
|
|
|
2017-03-20 16:09:01 +00:00
|
|
|
return this.update(true);
|
2017-03-16 21:59:50 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
setShadow: function (x, y, color, blur, shadowStroke, shadowFill)
|
|
|
|
{
|
2017-03-20 16:48:04 +00:00
|
|
|
if (x === undefined) { x = 0; }
|
2017-03-16 21:59:50 +00:00
|
|
|
if (y === undefined) { y = 0; }
|
|
|
|
if (color === undefined) { color = '#000'; }
|
|
|
|
if (blur === undefined) { blur = 0; }
|
|
|
|
if (shadowStroke === undefined) { shadowStroke = false; }
|
|
|
|
if (shadowFill === undefined) { shadowFill = false; }
|
|
|
|
|
|
|
|
this.shadowOffsetX = x;
|
|
|
|
this.shadowOffsetY = y;
|
|
|
|
this.shadowColor = color;
|
|
|
|
this.shadowBlur = blur;
|
|
|
|
this.shadowStroke = shadowStroke;
|
|
|
|
this.shadowFill = shadowFill;
|
|
|
|
|
2017-03-20 16:09:01 +00:00
|
|
|
return this.update(false);
|
|
|
|
},
|
|
|
|
|
|
|
|
setShadowOffset: function (x, y)
|
|
|
|
{
|
|
|
|
if (x === undefined) { x = 0; }
|
|
|
|
if (y === undefined) { y = x; }
|
|
|
|
|
|
|
|
this.shadowOffsetX = x;
|
|
|
|
this.shadowOffsetY = y;
|
2017-03-16 21:59:50 +00:00
|
|
|
|
2017-03-20 16:09:01 +00:00
|
|
|
return this.update(false);
|
|
|
|
},
|
|
|
|
|
|
|
|
setShadowColor: function (color)
|
|
|
|
{
|
|
|
|
if (color === undefined) { color = '#000'; }
|
|
|
|
|
|
|
|
this.shadowColor = color;
|
|
|
|
|
|
|
|
return this.update(false);
|
|
|
|
},
|
|
|
|
|
|
|
|
setShadowBlur: function (blur)
|
|
|
|
{
|
|
|
|
if (blur === undefined) { blur = 0; }
|
|
|
|
|
|
|
|
this.shadowBlur = blur;
|
|
|
|
|
|
|
|
return this.update(false);
|
|
|
|
},
|
|
|
|
|
|
|
|
setShadowStroke: function (enabled)
|
|
|
|
{
|
|
|
|
this.shadowStroke = enabled;
|
|
|
|
|
|
|
|
return this.update(false);
|
|
|
|
},
|
|
|
|
|
|
|
|
setShadowFill: function (enabled)
|
|
|
|
{
|
|
|
|
this.shadowFill = enabled;
|
|
|
|
|
|
|
|
return this.update(false);
|
2017-03-16 21:59:50 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
setAlign: function (align)
|
|
|
|
{
|
|
|
|
if (align === undefined) { align = 'left'; }
|
|
|
|
|
|
|
|
this.align = align;
|
|
|
|
|
2017-03-20 16:09:01 +00:00
|
|
|
return this.update(false);
|
2017-03-16 21:59:50 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
setMaxLines: function (max)
|
|
|
|
{
|
|
|
|
if (max === undefined) { max = 0; }
|
|
|
|
|
|
|
|
this.maxLines = max;
|
|
|
|
|
2017-03-20 16:09:01 +00:00
|
|
|
return this.update(false);
|
2017-03-16 21:59:50 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
destroy: function ()
|
|
|
|
{
|
|
|
|
this.parent = undefined;
|
|
|
|
}
|
|
|
|
|
2017-03-15 01:07:58 +00:00
|
|
|
});
|
2017-03-13 23:38:48 +00:00
|
|
|
|
|
|
|
module.exports = TextStyle;
|