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

SVG Text Fixes? #7345

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open

SVG Text Fixes? #7345

wants to merge 27 commits into from

Conversation

StickmanRed
Copy link

Resolves #7084, #7321

Changes

Added "toEditorSVG" function taking the SVG element and returning an adjusted version accounting for some text offset issues.

Reason for changes

This could be useful to those importing SVGs from other programs.

Tests

This was tested on Chrome with SVGs from scratchblocks, the "Save blocks to image" addon, and the Scratch editor. The images' text did appear as they were imported in both the editor and the stage. When editing the image in Scratch, the text stays as it originally appeared; however, when changing the contents of the text, it sometimes becomes displaced, which I believe is a consequence of the "text-anchor" property.

I haven't found any other bugs in the code, but if there are errors or if it can be rewritten, the program loops through the text elements, adds "<tspan>" tags inside each element if there aren't any, and adds "x", "y", and "dominant-baseline" changes to "transform: translate"

StickmanRed and others added 14 commits April 4, 2024 19:15
Added fix for text misalignment. (Hopefully.)
I forgot to add the most important part... =P
I forgot how to HTML
Add a working version that successfully fixes often-encountered text issues!
Remove console.log debugging statements
Add fully working version. The errors had nothing to do with font-size... =P
Regex + Prettier
@Samq64
Copy link
Member

Samq64 commented Apr 8, 2024

Could the addon's description be updated to mention scratchblocks?

@StickmanRed
Copy link
Author

Could the addon's description be updated to mention scratchblocks?

would it be something like this? (edited content in addon.json)

Copy link
Member

@Samq64 Samq64 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still needs some work:

  • The green flag and dropdown arrows are missing sometimes
  • The text after an input is now to high instead of too low
  • The SVG is not centered on the canvas
Screenshot

image

addons/fix-uploaded-svgs/userscript.js Outdated Show resolved Hide resolved
addons/fix-uploaded-svgs/userscript.js Outdated Show resolved Hide resolved
@StickmanRed
Copy link
Author

StickmanRed commented Apr 8, 2024

This still needs some work:

  • The green flag and dropdown arrows are missing sometimes
  • The text after an input is now to high instead of too low
  • The SVG is not centered on the canvas

I'll fix the second bug first, since that shouldn't happen... the other two weren't really my focus, being not text-related, but maybe the first bug could be patched using some code from "blocks2image"...

Turns out it was just regex... =/
@StickmanRed
Copy link
Author

StickmanRed commented Apr 9, 2024

  • The green flag and dropdown arrows are missing sometimes
  • The text after an input is now to high instead of too low
  • The SVG is not centered on the canvas

For the first bug, a future version could replace the "<use>" elements with their respective references. I'm not sure how to fix the third bug you mentioned, since it has to do with the "transform: scale" property being removed from the top "<g>" element...

Edit: Actually, I added a quick concept that should fix the first problem... I'm still unsure about the last one though

StickmanRed and others added 2 commits April 8, 2024 21:20
Added image fixes for scratchblocks
Copy link
Member

@Samq64 Samq64 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scratchblocks import fine except for the offset now. I'll need to do some more testing to make sure it doesn't accidentally mess up other text SVGs

addons/fix-uploaded-svgs/userscript.js Outdated Show resolved Hide resolved
@Samq64 Samq64 added type: enhancement New feature for the project scope: addon Related to one or multiple addons labels Apr 9, 2024
@WorldLanguages
Copy link
Member

I'll need to do some more testing to make sure it doesn't accidentally mess up other text SVGs

How can we be sure this won't break existing SVGs that upload just fine? It will be hard to test all cases.

StickmanRed and others added 2 commits April 9, 2024 12:47
Goodbye parser + regex. Also, I added cool operator (??). This is just what I needed!
@StickmanRed
Copy link
Author

I'll need to do some more testing to make sure it doesn't accidentally mess up other text SVGs

How can we be sure this won't break existing SVGs that upload just fine? It will be hard to test all cases.

Theoretically, the code should only add ignored properties to "transform" (offsets created by "x", "y", and "dominant-baseline"), place text nodes inside "<span>" tags (I'm not sure why Scratch shifts other text down), and substitute for "<use>" tags (added specifically to accommodate scratchblocks). Besides a bad concept execution on my part, I don't know of any new errors it could cause...

@Samq64 Samq64 added the status: needs review [Use when there's already 1 approval] Review needed on the PR label Apr 10, 2024
@Samq64
Copy link
Member

Samq64 commented Apr 10, 2024

Also would it be easy to fix Scratch 2 block shadows?

@Samq64
Copy link
Member

Samq64 commented Apr 10, 2024

How can we be sure this won't break existing SVGs that upload just fine? It will be hard to test all cases.

Theoretically, the code should only add ignored properties to "transform" (offsets created by "x", "y", and "dominant-baseline"), place text nodes inside "" tags (I'm not sure why Scratch shifts other text down), and substitute for "" tags (added specifically to accommodate scratchblocks). Besides a bad concept execution on my part, I don't know of any new errors it could cause...

Also the addon disabled by default but it would be ironic if it breaks other SVGs.

@StickmanRed
Copy link
Author

Also would it be easy to fix Scratch 2 block shadows?

I'm not exactly sure... the only solution that I can think of is to insert every element with the "filter" property into its individual SVG "<image>", which should keep the shadows but will render the blocks uneditable...

@DNin01
Copy link
Member

DNin01 commented Apr 16, 2024

I'll need to do some more testing to make sure it doesn't accidentally mess up other text SVGs

How can we be sure this won't break existing SVGs that upload just fine? It will be hard to test all cases.

One thing we could try is programming a notification that appears when this addon makes changes to an SVG. Just an idea. We could show to users, just so they know the addon is doing something in case it breaks an SVG…?

But even if it's just a console.log(), it could be useful information.

@StickmanRed
Copy link
Author

I'll need to do some more testing to make sure it doesn't accidentally mess up other text SVGs

How can we be sure this won't break existing SVGs that upload just fine? It will be hard to test all cases.

One thing we could try is programming a notification that appears when this addon makes changes to an SVG. Just an idea. We could show to users, just so they know the addon is doing something in case it breaks an SVG…?

But even if it's just a console.log(), it could be useful information.

I guess that's an option, but, after reviewing and using the addon a few times, I'm convinced that it shouldn't break anything that isn't already broken...

@WorldLanguages
Copy link
Member

For this addon, we can go two ways

  1. Keep it NOT enabled-by-default, and keep experimenting with SVG fixes
  2. We could enable it for all users, which would benefit the most people, but we would need to be more careful, and consider showing UI notifying the user that the SVG has been altered. Possibly an Undo button to restore it to the original SVG.

A third alternative is enabling the addon by default, but instead of fixing SVGs by default, we add a little tooltip, after one or more SVGs were uploaded, with an "SVG fix" button.

@WorldLanguages
Copy link
Member

WorldLanguages commented Apr 18, 2024

@StickmanRed You're probably right on the fact this doesn't break any SVGs, but as I usually use SVGs as "just a vector format" (and usually do not read XML sources directly) I'm not familiar with all aspects of the SVG specification. Ideally we could look out for someone else who's somewhat familiar for a second look?

}

// This iframe is needed to correct "dominant-baseline"
const iframe = document.createElement("iframe");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious, have you considered using window.DOMParser? It's not that important, but maybe you didn't know about it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I haven't... could that be used as an alternative to iframe?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably. Using an iframe and destroying it afterwards is still fine, though.

@StickmanRed
Copy link
Author

@StickmanRed You're probably right on the fact this doesn't break any SVGs, but as I usually use SVGs as "just a vector format" (and usually do not read XML sources directly) I'm not familiar with all aspects of the SVG specification. Ideally we could look out for someone else who's somewhat familiar for a second look?

Is there anyone to ask who could evaluate the changes, or should I wait for someone to review them?

@WorldLanguages WorldLanguages added the status: awaiting answer/followup A comment will be sent if there's no activity for 7 days, for issues that have this label. label Apr 28, 2024
@scratchaddons-bot
Copy link
Contributor

Ping! There has been no activity for 7 days.

@scratchaddons-bot scratchaddons-bot bot added the status: stale Issue or PR marked stale by a bot label May 8, 2024
@scratchaddons-bot scratchaddons-bot bot removed the status: stale Issue or PR marked stale by a bot label May 17, 2024
@scratchaddons-bot
Copy link
Contributor

Ping! There has been no activity for 7 days.

@scratchaddons-bot scratchaddons-bot bot added the status: stale Issue or PR marked stale by a bot label May 24, 2024
@@ -1,6 +1,6 @@
{
"name": "SVG uploads fix",
"description": "Fixes a bug in the Scratch editor that makes uploaded SVG files created with some image editors, such as Affinity Designer, broken.",
"description": "Fixes a bug in the Scratch editor that breaks some uploaded SVG files, such as those created with Affinity Designer or generated by scratchblocks.",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to be specifically mentioning third-party software? Do we know what platforms' SVGs experience this bug? I know this is somewhat unrelated, but now is a good time to maybe revise this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's fine when put certain ways like "such as Affinity Designer", but "such as those created with Affinity Designer" way of wording it makes it feel a little more like free advertising. It's probably a matter of how specifically the product is being mentioned - "such as" is further away from the product names in the proposed description.

@scratchaddons-bot scratchaddons-bot bot removed the status: stale Issue or PR marked stale by a bot label May 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
scope: addon Related to one or multiple addons status: awaiting answer/followup A comment will be sent if there's no activity for 7 days, for issues that have this label. status: needs review [Use when there's already 1 approval] Review needed on the PR type: enhancement New feature for the project
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants