Merge pull request #10 from fsih/lineSpacing

Be able to import line spacing
This commit is contained in:
DD Liu 2018-05-21 11:31:55 -04:00 committed by GitHub
commit 4ce55ae0d2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 1 deletions

View file

@ -257,12 +257,13 @@ new function() {
// because they break <tspan> 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 <tspan> 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);

View file

@ -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 <tspan> method for all text (below)
text.translate(0, text._style.getLeading());
if (!isNaN(fontSize)) text.setFontSize(fontSize);
return text;
} else {
// Scratch3 SVGs always use <tspan>'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;
}
}