Skip to content

Commit

Permalink
Add letter spacing support for svg (fabricjs#3718)
Browse files Browse the repository at this point in the history
* add letter spacing support for svg

* fix letter spacing for Parser

* Update itext.svg_export.js

* Update text.class.js
  • Loading branch information
AvaelKross authored and asturur committed Apr 1, 2018
1 parent 82cb298 commit 827fcd5
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
'font-size': 'fontSize',
'font-style': 'fontStyle',
'font-weight': 'fontWeight',
'letter-spacing': 'charSpacing',
'paint-order': 'paintFirst',
'stroke-dasharray': 'strokeDashArray',
'stroke-linecap': 'strokeLineCap',
Expand Down Expand Up @@ -110,6 +111,10 @@
else if (attr === 'textAnchor' /* text-anchor */) {
value = value === 'start' ? 'left' : value === 'end' ? 'right' : 'center';
}
else if (attr === 'charSpacing') {
// parseUnit returns px and we convert it to em
parsed = parseUnit(value, fontSize) / fontSize * 1000;
}
else if (attr === 'paintFirst') {
var fillIndex = value.indexOf('fill');
var strokeIndex = value.indexOf('stroke');
Expand Down
2 changes: 1 addition & 1 deletion src/shapes/text.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -1319,7 +1319,7 @@
* @see: http://www.w3.org/TR/SVG/text.html#TextElement
*/
fabric.Text.ATTRIBUTE_NAMES = fabric.SHARED_ATTRIBUTES.concat(
'x y dx dy font-family font-style font-weight font-size text-decoration text-anchor'.split(' '));
'x y dx dy font-family font-style font-weight font-size letter-spacing text-decoration text-anchor'.split(' '));

/**
* Default SVG font size
Expand Down
2 changes: 2 additions & 0 deletions test/unit/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@
elTextWithAttrs.setAttribute('font-style', 'italic');
elTextWithAttrs.setAttribute('font-weight', 'bold');
elTextWithAttrs.setAttribute('font-size', '123');
elTextWithAttrs.setAttribute('letter-spacing', '1em');
elTextWithAttrs.setAttribute('text-decoration', 'underline');
elTextWithAttrs.setAttribute('text-anchor', 'middle');

Expand All @@ -246,6 +247,7 @@
fontFamily: 'Monaco',
paintFirst: 'fill',
fontStyle: 'italic',
charSpacing: 1000,
fontWeight: 'bold',
fontSize: 123,
underline: true,
Expand Down

0 comments on commit 827fcd5

Please sign in to comment.