diff --git a/src/svg/SvgExport.js b/src/svg/SvgExport.js index 0eaa6194..7af40699 100644 --- a/src/svg/SvgExport.js +++ b/src/svg/SvgExport.js @@ -257,12 +257,13 @@ new function() { // because they break mutliline formatting (below) var node = SvgElement.create('text', getTransform(item._matrix, false), formatter); + node.setAttribute('font-size', item.fontSize); for (var i = 0; i < item._lines.length; i++) { // Scratch-specific: Use for multiline text, // right now only supports left justified (x=0) var tspanNode = SvgElement.create('tspan', { x: '0', - dy: i === 0 ? '0' : item._style.getLeading() + 'px' + dy: i === 0 ? '0' : item.getLeading() + 'px' }, formatter); tspanNode.textContent = item._lines[i]; node.appendChild(tspanNode); diff --git a/src/svg/SvgImport.js b/src/svg/SvgImport.js index 3830c2ef..a7cc3e0d 100644 --- a/src/svg/SvgImport.js +++ b/src/svg/SvgImport.js @@ -302,23 +302,39 @@ new function() { // lengthAdjust: // Scratch-specific: Do not use x/y attributes because they break multiline usage. + var fontSize = parseFloat(node.getAttribute("font-size")); if (node.childElementCount === 0) { var text = new PointText(); text.setContent(node.textContent.trim() || ''); // Scratch-specific: Scratch2 SVGs are offset by 1 leading vertically. // Scratch3 SVGs use method for all text (below) text.translate(0, text._style.getLeading()); + if (!isNaN(fontSize)) text.setFontSize(fontSize); return text; } else { // Scratch3 SVGs always use 's for multiline string support. // Does not support x/y attribute or tspan positioning beyond left justified. var lines = []; + var spacing = 1.2; for (var i = 0; i < node.children.length; i++) { var child = node.children[i]; lines.push(child.textContent); + var dyString = child.getAttribute('dy'); + if (dyString) { + var dy = parseFloat(dyString); + if (!isNaN(dy)) { + if (dyString.endsWith('em')) { + spacing = dy; + } else if (dyString.endsWith('px') && !isNaN(fontSize)) { + spacing = dy / fontSize; + } + } + } } var text = new PointText(); + if (!isNaN(fontSize)) text.setFontSize(fontSize); text.setContent(lines.join('\n') || ''); + text.setLeading(text.fontSize * spacing); return text; } }