Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for letter spacing #2066

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions dist/paper.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6538,6 +6538,11 @@ declare namespace paper {
*/
justification: string

/**
* The letter spacing of text content.
*/
letterSpacing: string


/**
* Style objects don't need to be created directly. Just pass an object to
Expand Down Expand Up @@ -6657,6 +6662,11 @@ declare namespace paper {
*/
justification: string

/**
* The letter spacing of text content.
*/
letterSpacing: string


}

Expand Down
2 changes: 2 additions & 0 deletions gulp/typescript/typescript-definition-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,7 @@ style.fontWeight;
style.fontSize;
style.leading;
style.justification;
style.letterSpacing;


//
Expand Down Expand Up @@ -977,6 +978,7 @@ textItem.fontWeight;
textItem.fontSize;
textItem.leading;
textItem.justification;
textItem.letterSpacing;


//
Expand Down
20 changes: 19 additions & 1 deletion src/style/Style.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ var Style = Base.extend(new function() {
fontWeight: 'normal',
fontSize: 12,
leading: null,
letterSpacing: 0,
// Paragraphs
justification: 'left'
}),
Expand Down Expand Up @@ -392,7 +393,7 @@ var Style = Base.extend(new function() {
if (/pt|em|%|px/.test(fontSize))
fontSize = this.getView().getPixelSize(fontSize);
return leading != null ? leading : fontSize * 1.2;
}
},

// DOCS: why isn't the example code showing up?
/**
Expand Down Expand Up @@ -702,4 +703,21 @@ var Style = Base.extend(new function() {
* @values 'left', 'right', 'center'
* @default 'left'
*/

/**
* The letter spacing of text content.
*
* @name Style#letterSpacing
* @type Number|String
* @default 0
*/
getLetterSpacing: function getLetterSpacing() {
// Override letterSpacing to include px
// as the default unit, when not provided
var letterSpacing = getLetterSpacing.base.call(this);
if (typeof letterSpacing === 'number') {
return letterSpacing + 'px';
}
return letterSpacing;
}
});
1 change: 1 addition & 0 deletions src/svg/SvgStyles.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ var SvgStyles = Base.each({
center: 'middle',
right: 'end'
}],
letterSpacing: ['letter-spacing', 'string'],
// Item
opacity: ['opacity', 'number'],
blendMode: ['mix-blend-mode', 'style']
Expand Down
3 changes: 2 additions & 1 deletion src/text/PointText.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ var PointText = TextItem.extend(/** @lends PointText# */{
shadowColor = ctx.shadowColor;
ctx.font = style.getFontStyle();
ctx.textAlign = style.getJustification();
ctx.letterSpacing = style.getLetterSpacing();
for (var i = 0, l = lines.length; i < l; i++) {
// See Path._draw() for explanation about ctx.shadowColor
ctx.shadowColor = shadowColor;
Expand All @@ -108,7 +109,7 @@ var PointText = TextItem.extend(/** @lends PointText# */{
numLines = lines.length,
justification = style.getJustification(),
leading = style.getLeading(),
width = this.getView().getTextWidth(style.getFontStyle(), lines),
width = this.getView().getTextWidth(style.getFontStyle(), lines, style.getLetterSpacing()),
x = 0;
// Adjust for different justifications.
if (justification !== 'left')
Expand Down
8 changes: 8 additions & 0 deletions src/text/TextItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,14 @@ var TextItem = Item.extend(/** @lends TextItem# */{
* @default 'left'
*/

/**
* The letter spacing of text content.
*
* @name Style#letterSpacing
* @type Number|String
* @default 0
*/

/**
* @bean
* @private
Expand Down
5 changes: 4 additions & 1 deletion src/view/CanvasView.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,19 @@ var CanvasView = View.extend(/** @lends CanvasView# */{
return pixels;
},

getTextWidth: function(font, lines) {
getTextWidth: function(font, lines, letterSpacing) {
var ctx = this._context,
prevFont = ctx.font,
prevLetterSpacing = ctx.letterSpacing,
width = 0;
ctx.font = font;
ctx.letterSpacing = letterSpacing;
// Measure the real width of the text. Unfortunately, there is no sane
// way to measure text height with canvas.
for (var i = 0, l = lines.length; i < l; i++)
width = Math.max(width, ctx.measureText(lines[i]).width);
ctx.font = prevFont;
ctx.letterSpacing = prevLetterSpacing;
return width;
},

Expand Down
2 changes: 1 addition & 1 deletion test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ var compareItem = function(actual, expected, message, options, properties) {
'strokeColor', 'strokeCap', 'strokeJoin', 'dashArray',
'dashOffset', 'miterLimit'];
if (expected instanceof TextItem)
styles.push('fontSize', 'font', 'leading', 'justification');
styles.push('fontSize', 'font', 'leading', 'justification', 'letterSpacing');
compareProperties(actual.style, expected.style, styles,
message + ' (#style)', options);
}
Expand Down
12 changes: 12 additions & 0 deletions test/tests/Item_Bounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,18 @@ test('text.bounds', function() {
equals(text.bounds, new Rectangle(50, 87.4, 76.25, 16.8), 'text.bounds', { tolerance: 1.0 });
});

test('text.bounds with letterSpacing', function() {
var text = new PointText({
fontFamily: 'Arial, Helvetica',
fontSize: 14,
letterSpacing: 10,
fillColor: 'black',
point: [50, 100],
content: 'Hello World!'
});
equals(text.bounds, new Rectangle(50, 87.4, 196, 16.8), 'text.bounds', { tolerance: 1.0 });
});

test('path.bounds', function() {
var path = new Path([
new Segment(new Point(121, 334), new Point(-19, 38), new Point(30.7666015625, -61.53369140625)),
Expand Down
1 change: 1 addition & 0 deletions test/tests/Item_Cloning.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ test('PointText#clone()', function() {
fontSize: 20
};
pointText.justification = 'center';
pointText.letterSpacing = '10px';
cloneAndCompare(pointText);
});

Expand Down