mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-01 02:38:43 -05:00
Be able to import leading
This commit is contained in:
parent
a9de479c8c
commit
15470a90b7
2 changed files with 18 additions and 1 deletions
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue