mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-06 04:42:15 -05:00
Merge pull request #10 from fsih/lineSpacing
Be able to import line spacing
This commit is contained in:
commit
4ce55ae0d2
2 changed files with 18 additions and 1 deletions
|
@ -257,12 +257,13 @@ new function() {
|
||||||
// because they break <tspan> mutliline formatting (below)
|
// because they break <tspan> mutliline formatting (below)
|
||||||
var node = SvgElement.create('text', getTransform(item._matrix, false),
|
var node = SvgElement.create('text', getTransform(item._matrix, false),
|
||||||
formatter);
|
formatter);
|
||||||
|
node.setAttribute('font-size', item.fontSize);
|
||||||
for (var i = 0; i < item._lines.length; i++) {
|
for (var i = 0; i < item._lines.length; i++) {
|
||||||
// Scratch-specific: Use <tspan> for multiline text,
|
// Scratch-specific: Use <tspan> for multiline text,
|
||||||
// right now only supports left justified (x=0)
|
// right now only supports left justified (x=0)
|
||||||
var tspanNode = SvgElement.create('tspan', {
|
var tspanNode = SvgElement.create('tspan', {
|
||||||
x: '0',
|
x: '0',
|
||||||
dy: i === 0 ? '0' : item._style.getLeading() + 'px'
|
dy: i === 0 ? '0' : item.getLeading() + 'px'
|
||||||
}, formatter);
|
}, formatter);
|
||||||
tspanNode.textContent = item._lines[i];
|
tspanNode.textContent = item._lines[i];
|
||||||
node.appendChild(tspanNode);
|
node.appendChild(tspanNode);
|
||||||
|
|
|
@ -302,23 +302,39 @@ new function() {
|
||||||
// lengthAdjust:
|
// lengthAdjust:
|
||||||
|
|
||||||
// Scratch-specific: Do not use x/y attributes because they break multiline usage.
|
// Scratch-specific: Do not use x/y attributes because they break multiline usage.
|
||||||
|
var fontSize = parseFloat(node.getAttribute("font-size"));
|
||||||
if (node.childElementCount === 0) {
|
if (node.childElementCount === 0) {
|
||||||
var text = new PointText();
|
var text = new PointText();
|
||||||
text.setContent(node.textContent.trim() || '');
|
text.setContent(node.textContent.trim() || '');
|
||||||
// Scratch-specific: Scratch2 SVGs are offset by 1 leading vertically.
|
// Scratch-specific: Scratch2 SVGs are offset by 1 leading vertically.
|
||||||
// Scratch3 SVGs use <tspan> method for all text (below)
|
// Scratch3 SVGs use <tspan> method for all text (below)
|
||||||
text.translate(0, text._style.getLeading());
|
text.translate(0, text._style.getLeading());
|
||||||
|
if (!isNaN(fontSize)) text.setFontSize(fontSize);
|
||||||
return text;
|
return text;
|
||||||
} else {
|
} else {
|
||||||
// Scratch3 SVGs always use <tspan>'s for multiline string support.
|
// Scratch3 SVGs always use <tspan>'s for multiline string support.
|
||||||
// Does not support x/y attribute or tspan positioning beyond left justified.
|
// Does not support x/y attribute or tspan positioning beyond left justified.
|
||||||
var lines = [];
|
var lines = [];
|
||||||
|
var spacing = 1.2;
|
||||||
for (var i = 0; i < node.children.length; i++) {
|
for (var i = 0; i < node.children.length; i++) {
|
||||||
var child = node.children[i];
|
var child = node.children[i];
|
||||||
lines.push(child.textContent);
|
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();
|
var text = new PointText();
|
||||||
|
if (!isNaN(fontSize)) text.setFontSize(fontSize);
|
||||||
text.setContent(lines.join('\n') || '');
|
text.setContent(lines.join('\n') || '');
|
||||||
|
text.setLeading(text.fontSize * spacing);
|
||||||
return text;
|
return text;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue