Implement Style#fontFamily and #fontWeight as a replacement for #font.

Closes #60.
This commit is contained in:
Jürg Lehni 2013-12-08 21:12:51 +01:00
parent 24ce35cc6e
commit 3cd141170a
6 changed files with 113 additions and 14 deletions

View file

@ -37,6 +37,8 @@
* var text = new PointText(view.center);
* text.content = 'Hello world.';
* text.style = {
* fontFamily: 'Courier New',
* fontWeight: 'Bold',
* fontSize: 20,
* fillColor: 'red',
* justification: 'center'
@ -86,8 +88,10 @@ var Style = Base.extend(new function() {
// Selection
selectedColor: undefined,
// Characters
font: 'sans-serif',
fontFamily: 'sans-serif',
fontWeight: 'normal',
fontSize: 12,
font: null, // deprecated, links to fontFamily
leading: null,
// Paragraphs
justification: 'left'
@ -98,8 +102,10 @@ var Style = Base.extend(new function() {
strokeCap: /*#=*/ Change.STROKE,
strokeJoin: /*#=*/ Change.STROKE,
miterLimit: /*#=*/ Change.STROKE,
font: /*#=*/ Change.GEOMETRY,
fontFamily: /*#=*/ Change.GEOMETRY,
fontWeight: /*#=*/ Change.GEOMETRY,
fontSize: /*#=*/ Change.GEOMETRY,
font: /*#=*/ Change.GEOMETRY, // deprecated, links to fontFamily
leading: /*#=*/ Change.GEOMETRY,
justification: /*#=*/ Change.GEOMETRY
};
@ -265,12 +271,6 @@ var Style = Base.extend(new function() {
// Overrides
getLeading: function getLeading() {
// Override leading to return fontSize * 1.2 by default.
var leading = getLeading.base.call(this);
return leading != null ? leading : this.getFontSize() * 1.2;
},
getFontStyle: function() {
var size = this.getFontSize();
// To prevent an obscure iOS 7 crash, we have to convert the size to a
@ -278,7 +278,27 @@ var Style = Base.extend(new function() {
// This nonsensical statement would also prevent the bug, prooving that
// the issue is not the regular expression itself, but something deeper
// down in the optimizer: if (size === 0) size = 0;
return size + (/[a-z]/i.test(size + '') ? ' ' : 'px ') + this.getFont();
return this.getFontWeight()
+ ' ' + size + (/[a-z]/i.test(size + '') ? ' ' : 'px ')
+ this.getFontFamily();
},
/**
* @private
* @deprecated use {@link #getFontFamily()} instead.
*/
getFont: '#getFontFamily',
/**
* @private
* @deprecated use {@link #setFontFamily(font)} instead.
*/
setFont: '#setFontFamily',
getLeading: function getLeading() {
// Override leading to return fontSize * 1.2 by default.
var leading = getLeading.base.call(this);
return leading != null ? leading : this.getFontSize() * 1.2;
}
// DOCS: why isn't the example code showing up?
@ -511,13 +531,22 @@ var Style = Base.extend(new function() {
/**
* {@grouptitle Character Style}
*
* The font to be used in text content.
* The font-family to be used in text content.
*
* @name Style#font
* @name Style#fontFamily
* @default 'sans-serif'
* @type String
*/
/**
*
* The font-weight to be used in text content.
*
* @name Style#fontWeight
* @default 'normal'
* @type String
*/
/**
* The font size of text content, as {@Number} in pixels, or as {@String}
* with optional units {@code 'px'}, {@code 'pt'} and {@code 'em'}.
@ -527,6 +556,16 @@ var Style = Base.extend(new function() {
* @type Number|String
*/
/**
*
* The font-family to be used in text content, as one {@String}.
* @deprecated use {@link #fontFamily} instead.
*
* @name Style#font
* @default 'sans-serif'
* @type String
*/
/**
* The text leading of text content.
*

View file

@ -22,7 +22,8 @@ var SVGStyles = Base.each({
dashArray: ['stroke-dasharray', 'array'],
dashOffset: ['stroke-dashoffset', 'number'],
// Text
font: ['font-family', 'string'],
fontFamily: ['font-family', 'string'],
fontWeight: ['font-weight', 'string'],
fontSize: ['font-size', 'number'],
justification: ['text-anchor', 'lookup', {
left: 'start',

View file

@ -49,6 +49,8 @@ var PointText = TextItem.extend(/** @lends PointText# */{
* point: [50, 50],
* content: 'The contents of the point text',
* fillColor: 'black',
* fontFamily: 'Courier New',
* fontWeight: 'Bold',
* fontSize: 25
* });
*/

View file

@ -98,6 +98,62 @@ var TextItem = Item.extend(/** @lends TextItem# */{
return !this._content;
},
/**
* {@grouptitle Character Style}
*
* The font-family to be used in text content.
*
* @name Style#fontFamily
* @default 'sans-serif'
* @type String
*/
/**
*
* The font-weight to be used in text content.
*
* @name Style#fontWeight
* @default 'normal'
* @type String
*/
/**
* The font size of text content, as {@Number} in pixels, or as {@String}
* with optional units {@code 'px'}, {@code 'pt'} and {@code 'em'}.
*
* @name Style#fontSize
* @default 10
* @type Number|String
*/
/**
*
* The font-family to be used in text content, as one {@String}.
* @deprecated use {@link #fontFamily} instead.
*
* @name Style#font
* @default 'sans-serif'
* @type String
*/
/**
* The text leading of text content.
*
* @name Style#leading
* @default fontSize * 1.2
* @type Number|String
*/
/**
* {@grouptitle Paragraph Style}
*
* The justification of text paragraphs.
*
* @name Style#justification
* @default 'left'
* @type String('left', 'right', 'center')
*/
/**
* @private
* @deprecated use {@link #getStyle()} instead.