11import setAttributes from '../utils/setAttributes' ;
22import normalizeColor from '../utils/normalizeColor' ;
33
4+ /**
5+ * Wrap each line of given text in a `<tspan>` element and append these
6+ * lines to the given SVGTextElement
7+ *
8+ * @param {SVGTextElement } textElement A text element to hold the split text
9+ * @param {String } textContent String to render with line breaks
10+ */
11+ function insertLineBreaks ( textElement , textContent ) {
12+ const lines = ( textContent || '' ) . split ( '\n' ) ;
13+ // can't use dy attribute here since we want empty lines to take up space as well,
14+ // so we will update y manually based on font size
15+ const x = textElement . getAttribute ( 'x' ) ;
16+ let y = Number ( textElement . getAttribute ( 'y' ) ) ;
17+ const size = Number ( textElement . getAttribute ( 'font-size' ) ) ;
18+ for ( const line of lines ) {
19+ const tspan = document . createElementNS ( 'http://www.w3.org/2000/svg' , 'tspan' ) ;
20+ tspan . setAttribute ( 'y' , y . toString ( ) ) ;
21+ tspan . setAttribute ( 'x' , x ) ;
22+ tspan . innerHTML = line ;
23+ textElement . appendChild ( tspan ) ;
24+
25+ y += size ;
26+ }
27+ }
28+
429/**
530 * Create SVGTextElement from an annotation definition.
631 * This is used for anntations of type `textbox`.
@@ -20,7 +45,8 @@ export default function renderText(a) {
2045 transform : `rotate(${ a . rotation } )` ,
2146 style : 'white-space: pre'
2247 } ) ;
23- text . innerHTML = a . content ;
48+
49+ insertLineBreaks ( text , a . content ) ;
2450
2551 let g = document . createElementNS ( 'http://www.w3.org/2000/svg' , 'g' ) ;
2652 g . appendChild ( text ) ;
0 commit comments