mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-03 19:45:44 -05:00
Export and import SVGs with multiline text
This commit is contained in:
parent
2af28e6c0e
commit
dd276b92f7
2 changed files with 34 additions and 6 deletions
|
@ -253,9 +253,20 @@ new function() {
|
||||||
}
|
}
|
||||||
|
|
||||||
function exportText(item) {
|
function exportText(item) {
|
||||||
var node = SvgElement.create('text', getTransform(item._matrix, true),
|
// Scratch-specific: do not use x/y attributes for text elements
|
||||||
|
// because they break <tspan> mutliline formatting (below)
|
||||||
|
var node = SvgElement.create('text', getTransform(item._matrix, false),
|
||||||
formatter);
|
formatter);
|
||||||
node.textContent = item._content;
|
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'
|
||||||
|
}, formatter);
|
||||||
|
tspanNode.textContent = item._lines[i];
|
||||||
|
node.appendChild(tspanNode);
|
||||||
|
}
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -300,10 +300,27 @@ new function() {
|
||||||
// TODO: Support for these is missing in Paper.js right now
|
// TODO: Support for these is missing in Paper.js right now
|
||||||
// rotate: character rotation
|
// rotate: character rotation
|
||||||
// lengthAdjust:
|
// lengthAdjust:
|
||||||
var text = new PointText(getPoint(node).add(
|
|
||||||
getPoint(node, 'dx', 'dy')));
|
// Scratch-specific: Do not use x/y attributes because they break multiline usage.
|
||||||
text.setContent(node.textContent.trim() || '');
|
if (node.childElementCount === 0) {
|
||||||
return text;
|
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());
|
||||||
|
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 = [];
|
||||||
|
for (var i = 0; i < node.children.length; i++) {
|
||||||
|
var child = node.children[i];
|
||||||
|
lines.push(child.textContent);
|
||||||
|
}
|
||||||
|
var text = new PointText();
|
||||||
|
text.setContent(lines.join('\n') || '');
|
||||||
|
return text;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue