Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#758 Add SVG text interpretation and improve CSS extraction #759

Merged
merged 5 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

Loading