Skip to content

Commit

Permalink
Merge pull request #759 from ArthurG94/758-fix-svg-attr-parsing
Browse files Browse the repository at this point in the history
#758 Add SVG text interpretation and improve CSS extraction
  • Loading branch information
jonobr1 authored Feb 13, 2025
2 parents 1a523ee + 18e0047 commit dfd3c44
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 16 deletions.
21 changes: 17 additions & 4 deletions build/two.js
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,7 @@ var Two = (() => {
canvas: "CanvasRenderer"
},
Version: "v0.8.16",
PublishDate: "2025-02-13T20:00:59.976Z",
PublishDate: "2025-02-13T23:13:24.615Z",
Identifier: "two-",
Resolution: 12,
AutoCalculateImportedMatrices: true,
Expand Down Expand Up @@ -6807,7 +6807,9 @@ var Two = (() => {
if (typeof name === "undefined" || typeof value === "undefined") {
continue;
}
styles[name] = value.replace(/\s/, "");
const trimmedName = name.replace(/\s/g, "");
const trimmedValue = value.replace(/\s/g, "");
styles[trimmedName] = trimmedValue;
}
return styles;
}
Expand Down Expand Up @@ -7082,7 +7084,13 @@ var Two = (() => {
break;
case "font-size":
if (elem instanceof Text) {
elem.size = value;
if (value.match("[a-z%]$") && !value.endsWith("px")) {
error = new TwoError(
"only pixel values are supported with the " + key + " attribute."
);
console.warn(error.name, error.message);
}
elem.size = parseFloat(value);
}
break;
case "font-weight":
Expand Down Expand Up @@ -7712,7 +7720,12 @@ var Two = (() => {
text: function(node, parentStyles) {
const alignment = getAlignment(node.getAttribute("text-anchor")) || "left";
const baseline = getBaseline(node) || "baseline";
const message = node.textContent;
let message = "";
if (node.childNodes.length > 0 && node.childNodes[0].tagName === "TSPAN") {
message = node.childNodes[0].textContent;
} else {
message = node.textContent;
}
const text = new Text(message);
applySvgAttributes.call(this, node, text, parentStyles);
text.alignment = alignment;
Expand Down
4 changes: 2 additions & 2 deletions build/two.min.js

Large diffs are not rendered by default.

21 changes: 17 additions & 4 deletions build/two.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,7 @@ var Constants = {
canvas: "CanvasRenderer"
},
Version: "v0.8.16",
PublishDate: "2025-02-13T20:00:59.976Z",
PublishDate: "2025-02-13T23:13:24.615Z",
Identifier: "two-",
Resolution: 12,
AutoCalculateImportedMatrices: true,
Expand Down Expand Up @@ -6827,7 +6827,9 @@ function extractCSSText(text, styles) {
if (typeof name === "undefined" || typeof value === "undefined") {
continue;
}
styles[name] = value.replace(/\s/, "");
const trimmedName = name.replace(/\s/g, "");
const trimmedValue = value.replace(/\s/g, "");
styles[trimmedName] = trimmedValue;
}
return styles;
}
Expand Down Expand Up @@ -7102,7 +7104,13 @@ function applySvgAttributes(node, elem, parentStyles) {
break;
case "font-size":
if (elem instanceof Text) {
elem.size = value;
if (value.match("[a-z%]$") && !value.endsWith("px")) {
error = new TwoError(
"only pixel values are supported with the " + key + " attribute."
);
console.warn(error.name, error.message);
}
elem.size = parseFloat(value);
}
break;
case "font-weight":
Expand Down Expand Up @@ -7732,7 +7740,12 @@ var read = {
text: function(node, parentStyles) {
const alignment = getAlignment(node.getAttribute("text-anchor")) || "left";
const baseline = getBaseline(node) || "baseline";
const message = node.textContent;
let message = "";
if (node.childNodes.length > 0 && node.childNodes[0].tagName === "TSPAN") {
message = node.childNodes[0].textContent;
} else {
message = node.textContent;
}
const text = new Text(message);
applySvgAttributes.call(this, node, text, parentStyles);
text.alignment = alignment;
Expand Down
26 changes: 22 additions & 4 deletions src/utils/interpret-svg.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,13 @@ function extractCSSText(text, styles) {
if (typeof name === 'undefined' || typeof value === 'undefined') {
continue;
}
styles[name] = value.replace(/\s/, '');
}

//Delete whitespace and line breaks from name and value
const trimmedName = name.replace(/\s/g, '');
const trimmedValue = value.replace(/\s/g, '');

styles[trimmedName] = trimmedValue;
}
return styles;
}

Expand Down Expand Up @@ -487,7 +491,13 @@ function applySvgAttributes(node, elem, parentStyles) {
break;
case 'font-size':
if (elem instanceof Text) {
elem.size = value;
if (value.match('[a-z%]$') && !value.endsWith('px')) {
error = new TwoError(
'only pixel values are supported with the ' + key + ' attribute.'
);
console.warn(error.name, error.message);
}
elem.size = parseFloat(value);
}
break;
case 'font-weight':
Expand Down Expand Up @@ -1310,7 +1320,15 @@ export const read = {
text: function (node, parentStyles) {
const alignment = getAlignment(node.getAttribute('text-anchor')) || 'left';
const baseline = getBaseline(node) || 'baseline';
const message = node.textContent;
let message = '';

// Detect tspan for getting text content.
// If not, svg indentation apears in text content
if (node.childNodes.length > 0 && node.childNodes[0].tagName === 'TSPAN') {
message = node.childNodes[0].textContent;
} else {
message = node.textContent;
}

const text = new Text(message);

Expand Down
14 changes: 14 additions & 0 deletions tests/images/interpretation/text.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 24 additions & 2 deletions tests/suite/svg-interpreter.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit dfd3c44

Please sign in to comment.