implement letter spacing for Text Shape

This commit is contained in:
Anton Lavrenov 2016-09-20 16:31:30 -04:00
parent fe92be3ab4
commit 834c1362c6
5 changed files with 78 additions and 10 deletions

View File

@ -4,6 +4,10 @@ This project adheres to [Semantic Versioning](http://semver.org/).
## [Not released][Not released] ## [Not released][Not released]
## Added
- new properties for `Konva.Text`: `letterSpacing`
## [1.2.2][2016-09-15] ## [1.2.2][2016-09-15]
### Fixed ### Fixed

View File

@ -12893,7 +12893,7 @@
WORD = 'word', WORD = 'word',
CHAR = 'char', CHAR = 'char',
NONE = 'none', NONE = 'none',
ATTR_CHANGE_LIST = ['fontFamily', 'fontSize', 'fontStyle', 'fontVariant', 'padding', 'align', 'lineHeight', 'text', 'width', 'height', 'wrap'], ATTR_CHANGE_LIST = ['fontFamily', 'fontSize', 'fontStyle', 'fontVariant', 'padding', 'align', 'lineHeight', 'text', 'width', 'height', 'wrap', 'letterSpacing'],
// cached variables // cached variables
attrChangeListLen = ATTR_CHANGE_LIST.length, attrChangeListLen = ATTR_CHANGE_LIST.length,
@ -13044,6 +13044,7 @@
textArr = this.textArr, textArr = this.textArr,
textArrLen = textArr.length, textArrLen = textArr.length,
totalWidth = this.getWidth(), totalWidth = this.getWidth(),
letterSpacing = this.getLetterSpacing(),
n; n;
context.setAttr('font', this._getContextFont()); context.setAttr('font', this._getContextFont());
@ -13074,9 +13075,18 @@
context.translate((totalWidth - width - p * 2) / 2, 0); context.translate((totalWidth - width - p * 2) / 2, 0);
} }
this.partialText = text; if (letterSpacing > 0) {
for(var li = 0; li < text.length; li++) {
var letter = text[li];
this.partialText = letter;
context.fillStrokeShape(this);
context.translate(this._getTextSize(letter).width + letterSpacing, 0);
}
} else {
this.partialText = text;
context.fillStrokeShape(this); context.fillStrokeShape(this);
}
context.restore(); context.restore();
context.translate(0, lineHeightPx); context.translate(0, lineHeightPx);
} }
@ -13166,7 +13176,10 @@
return this.textArr.push({text: line, width: width}); return this.textArr.push({text: line, width: width});
}, },
_getTextWidth: function (text) { _getTextWidth: function (text) {
return dummyContext.measureText(text).width; var latterSpacing = this.getLetterSpacing();
var length = text.length;
return dummyContext.measureText(text).width +
(length ? latterSpacing * (length - 1) : 0);
}, },
_setTextData: function () { _setTextData: function () {
var lines = this.getText().split('\n'), var lines = this.getText().split('\n'),
@ -13419,6 +13432,16 @@
* text.wrap('word'); * text.wrap('word');
*/ */
Konva.Factory.addGetterSetter(Konva.Text, 'letterSpacing', 0);
/**
* set letter spacing property. Default value is 0.
* @name letterSpacing
* @method
* @memberof Konva.TextPath.prototype
* @param {Number} letterSpacing
*/
Konva.Factory.addGetter(Konva.Text, 'text', EMPTY_STRING); Konva.Factory.addGetter(Konva.Text, 'text', EMPTY_STRING);
Konva.Factory.addOverloadedGetterSetter(Konva.Text, 'text'); Konva.Factory.addOverloadedGetterSetter(Konva.Text, 'text');

4
konva.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -20,7 +20,7 @@
WORD = 'word', WORD = 'word',
CHAR = 'char', CHAR = 'char',
NONE = 'none', NONE = 'none',
ATTR_CHANGE_LIST = ['fontFamily', 'fontSize', 'fontStyle', 'fontVariant', 'padding', 'align', 'lineHeight', 'text', 'width', 'height', 'wrap'], ATTR_CHANGE_LIST = ['fontFamily', 'fontSize', 'fontStyle', 'fontVariant', 'padding', 'align', 'lineHeight', 'text', 'width', 'height', 'wrap', 'letterSpacing'],
// cached variables // cached variables
attrChangeListLen = ATTR_CHANGE_LIST.length, attrChangeListLen = ATTR_CHANGE_LIST.length,
@ -102,6 +102,7 @@
textArr = this.textArr, textArr = this.textArr,
textArrLen = textArr.length, textArrLen = textArr.length,
totalWidth = this.getWidth(), totalWidth = this.getWidth(),
letterSpacing = this.getLetterSpacing(),
n; n;
context.setAttr('font', this._getContextFont()); context.setAttr('font', this._getContextFont());
@ -132,9 +133,18 @@
context.translate((totalWidth - width - p * 2) / 2, 0); context.translate((totalWidth - width - p * 2) / 2, 0);
} }
this.partialText = text; if (letterSpacing > 0) {
for(var li = 0; li < text.length; li++) {
var letter = text[li];
this.partialText = letter;
context.fillStrokeShape(this);
context.translate(this._getTextSize(letter).width + letterSpacing, 0);
}
} else {
this.partialText = text;
context.fillStrokeShape(this); context.fillStrokeShape(this);
}
context.restore(); context.restore();
context.translate(0, lineHeightPx); context.translate(0, lineHeightPx);
} }
@ -224,7 +234,10 @@
return this.textArr.push({text: line, width: width}); return this.textArr.push({text: line, width: width});
}, },
_getTextWidth: function (text) { _getTextWidth: function (text) {
return dummyContext.measureText(text).width; var latterSpacing = this.getLetterSpacing();
var length = text.length;
return dummyContext.measureText(text).width +
(length ? latterSpacing * (length - 1) : 0);
}, },
_setTextData: function () { _setTextData: function () {
var lines = this.getText().split('\n'), var lines = this.getText().split('\n'),
@ -477,6 +490,16 @@
* text.wrap('word'); * text.wrap('word');
*/ */
Konva.Factory.addGetterSetter(Konva.Text, 'letterSpacing', 0);
/**
* set letter spacing property. Default value is 0.
* @name letterSpacing
* @method
* @memberof Konva.TextPath.prototype
* @param {Number} letterSpacing
*/
Konva.Factory.addGetter(Konva.Text, 'text', EMPTY_STRING); Konva.Factory.addGetter(Konva.Text, 'text', EMPTY_STRING);
Konva.Factory.addOverloadedGetterSetter(Konva.Text, 'text'); Konva.Factory.addOverloadedGetterSetter(Konva.Text, 'text');

View File

@ -82,6 +82,24 @@ suite('Text', function(){
assert.equal(text.getClassName(),'Text', 'getClassName should be Text'); assert.equal(text.getClassName(),'Text', 'getClassName should be Text');
}); });
// ======================================================
test('add text with letter spacing', function() {
var stage = addStage();
var layer = new Konva.Layer();
stage.add(layer);
var text = new Konva.Text({
text: 'hello'
});
layer.add(text);
layer.draw();
var oldWidth = text.width();
text.letterSpacing(10);
assert.equal(text.width(), oldWidth + 40);
layer.draw();
});
// ====================================================== // ======================================================
test('text getters and setters', function() { test('text getters and setters', function() {
var stage = addStage(); var stage = addStage();