@@ -7,6 +7,8 @@ function isTextInUse(_value, _node, attrs) {
7
7
return ( attrs . text !== undefined ) ;
8
8
}
9
9
10
+ const FONT_ATTRIBUTES = [ 'font-weight' , 'font-family' , 'font-size' , 'letter-spacing' , 'text-transform' ] ;
11
+
10
12
const textAttributesNS = {
11
13
12
14
'line-height' : {
@@ -122,18 +124,28 @@ const textAttributesNS = {
122
124
var text = value . text ;
123
125
if ( text === undefined ) text = attrs . text ;
124
126
if ( text !== undefined ) {
127
+
125
128
const breakTextFn = value . breakText || breakText ;
126
129
const computedStyles = getComputedStyle ( node ) ;
130
+ const wrapFontAttributes = { } ;
131
+ // The font size attributes must be set on the node
132
+ // to get the correct text wrapping.
133
+ // TODO: set the native SVG attributes before special attributes
134
+ for ( let i = 0 ; i < FONT_ATTRIBUTES . length ; i ++ ) {
135
+ const name = FONT_ATTRIBUTES [ i ] ;
136
+ if ( name in attrs ) {
137
+ node . setAttribute ( name , attrs [ name ] ) ;
138
+ }
139
+ // Note: computedStyles is a live object
140
+ // i.e. the properties are evaluated when accessed.
141
+ wrapFontAttributes [ name ] = computedStyles [ name ] ;
142
+ }
127
143
128
- wrappedText = breakTextFn ( '' + text , size , {
129
- 'font-weight' : computedStyles . fontWeight ,
130
- 'font-family' : computedStyles . fontFamily ,
131
- 'text-transform' : computedStyles . textTransform ,
132
- 'font-size' : computedStyles . fontSize ,
133
- 'letter-spacing' : computedStyles . letterSpacing ,
134
- // The `line-height` attribute in SVG is JoinJS specific.
135
- 'lineHeight' : attrs [ 'line-height' ] ,
136
- } , {
144
+ // The `line-height` attribute in SVG is JoinJS specific.
145
+ // TODO: change the `lineHeight` to breakText option.
146
+ wrapFontAttributes . lineHeight = attrs [ 'line-height' ] ;
147
+
148
+ wrappedText = breakTextFn ( '' + text , size , wrapFontAttributes , {
137
149
// Provide an existing SVG Document here
138
150
// instead of creating a temporary one over again.
139
151
svgDocument : this . paper . svg ,
@@ -147,7 +159,11 @@ const textAttributesNS = {
147
159
wrappedText = '' ;
148
160
}
149
161
textAttributesNS . text . set . call ( this , wrappedText , refBBox , node , attrs ) ;
150
- }
162
+ } ,
163
+ // We expose the font attributes list to allow
164
+ // the user to take other custom font attributes into account
165
+ // when wrapping the text.
166
+ FONT_ATTRIBUTES
151
167
} ,
152
168
153
169
'title' : {
0 commit comments