Skip to content

A start on SVG "quirks mode"#56

Merged
tmickel merged 6 commits intoscratchfoundation:developfrom
tmickel:feature/quirks-mode
Nov 7, 2016
Merged

A start on SVG "quirks mode"#56
tmickel merged 6 commits intoscratchfoundation:developfrom
tmickel:feature/quirks-mode

Conversation

@tmickel
Copy link
Copy Markdown
Collaborator

@tmickel tmickel commented Oct 24, 2016

I definitely don't think this is complete, but hopefully it can provide some thinking for how to approach Scratch 2.0 quirks.

Example project:
https://scratch.mit.edu/projects/127228166/

Before "quirks mode":
screen shot 2016-10-24 at 11 56 40 am
After "quirks mode":
screen shot 2016-10-24 at 11 56 45 am

Differences:

  • Scratch 2.0 appears to ignore SVG width, height, and viewBox properties. Many SVGs exported from Scratch 2.0 appear to draw things in negative coordinates or otherwise outside the given viewBox, so we need to fix those in order to use the browser's SVG renderer. In this implementation, the bounds of the SVG are directly measured by asking the browser to draw it as a DOM element, and then taking getBBox measurements, before re-drawing the SVG again with corrected measurements. I tried to do this several different ways - this was the best way I found so far.
  • Transforms text in a bunch of ways - strips out x and y properties that Scratch 2.0 appears to set incorrectly and otherwise ignore, fixes non-standard line break treatment, and adds in fonts (which was actually quite tricky).

There is no doubt in my mind that this has bugs, is incomplete (I'm sure there are more quirks), and maybe these could be done better some other way. Would be happy to have more ideas!

@tmickel tmickel modified the milestone: November 14 Oct 26, 2016
Copy link
Copy Markdown
Contributor

@cwillisf cwillisf left a comment

Choose a reason for hiding this comment

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

Nice work! The code looks pretty good -- I have one nitpick that could go either way. How many browsers have you tested this with?

I'm worried about license compatibility so I'm going to mark this as "Request changes" for the moment, but if it turns out that the licenses are in fact compatible then you can consider this approved.

Comment thread src/svg-quirks-mode/fonts/LICENSE.txt Outdated

Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/

This comment was marked as abuse.

This comment was marked as abuse.

This comment was marked as abuse.

This comment was marked as abuse.

This comment was marked as abuse.

This comment was marked as abuse.

Comment thread src/svg-quirks-mode/svg-renderer.js Outdated
document.body.appendChild(svgSpot);
svgSpot.appendChild(this._svgTag);
// Take the bounding box, and then destroy the element.
let bbox = this._svgTag.getBBox();

This comment was marked as abuse.

This comment was marked as abuse.

@cwillisf
Copy link
Copy Markdown
Contributor

@tmickel do you know of a way to tell (from code) that a particular SVG was created in the Scratch 2.0 style? I'm imagining a world where some of our SVGs require this special treatment but others can be render as-is... how do we tell the difference?

@tmickel
Copy link
Copy Markdown
Collaborator Author

tmickel commented Oct 31, 2016

@cwillisf Unfortunately I'm not sure if there's an easy way to accomplish what you're talking about. The Scratch 2.0 paint editor does insert a comment into SVGs, along the lines of <!-- Scratch paint editor made this -->. However, all current SVGs are rendered in Scratch 2.0 with these quirks, whether or not they've been processed through the 2.0 paint editor. It seems possible to me that people have adjusted their externally-edited/created SVGs to deal with the quirks - by trial and error, or whatever. So, I'd recommend that we render all vector graphics from 2.0 with this.

But, it would make sense to me for future assets and imports to use a different renderer (ideally a pure browser renderer that respects viewboxes and whatever, maybe). In that case we should probably explicitly mark new assets to distinguish when we can use a "standards mode" renderer? Any thoughts @thisandagain ?

@thisandagain
Copy link
Copy Markdown
Contributor

Thanks for putting this together @tmickel . This looks like a great start.

I like the idea of tagging new SVGs so we can use a less "quirky" rendering approach and (hopefully) split with some of the pain that we see in 2.0:
scratchfoundation/scratch-flash#963
scratchfoundation/scratch-flash#1175
scratchfoundation/scratch-flash#1200
scratchfoundation/scratch-flash#1103
scratchfoundation/scratch-flash#411
scratchfoundation/scratch-flash#135

@ed-cooper
Copy link
Copy Markdown

How do you plan to handle the case for a SVG file being imported?

For example, an SVG file made in Inkscape or something similar would work best being rendered by the browser, but an SVG that had previously been downloaded from Scratch might work best in quirks mode.

My suggestion would be to treat all imported SVGs as normal SVGs (ie not quirks mode)

Also:

people have adjusted their externally-edited/created SVGs to deal with the quirks - by trial and error, or whatever.

I've been using Inkscape for SVGs for quite a while now. Whilst Scratch 2.0s support for more advanced SVG features (eg. shadows) is ... interesting, I never had any quirks mode problems myself

@tmickel
Copy link
Copy Markdown
Collaborator Author

tmickel commented Oct 31, 2016

@chooper100 Any SVG that could have previously been rendered by Scratch 2.0 would be rendered in quirks mode. That includes SVGs created by external tools, and SVGs created in the paint editor. Treating import-SVGs differently would no doubt lead to broken projects, because they are rendered in 2.0 with these quirks and possibly more. I'm not sure I understand what you're saying, I don't think these are treated differently in scratch-flash if they're made in external tools. Do you have an example of an SVG created in Inkscape, for example, where the viewBox is smaller than the graphic, and Scratch renders the clipped graphic?

@cwillisf
Copy link
Copy Markdown
Contributor

cwillisf commented Nov 1, 2016

My interpretation of @chooper100's comments:

  • When Scratch 3.0 loads a Scratch 2.0 project, the SVGs should render in "quirks mode"
  • When Scratch 3.0 imports an SVG independently, it should render without "quirks mode"
    Is that what you had in mind, @chooper100?

I think we can continue to discuss when (and when not) to use Scratch 2.0 quirks-mode SVG rendering independently of this PR. As long as we want to do it in at least one case, we'll need some code like this :)

@ed-cooper
Copy link
Copy Markdown

@tmickel Yes, I meant what @cwillisf said

I've never had a problem where an Inkscape SVG has been clipped

@tmickel
Copy link
Copy Markdown
Collaborator Author

tmickel commented Nov 4, 2016

@cwillisf Comments addressed I think - any other thoughts?

By the way, I tested in Chrome, Firefox, and Safari. I didn't test in IE or Edge.

@tmickel
Copy link
Copy Markdown
Collaborator Author

tmickel commented Nov 4, 2016

Update:

  • In my VM, it looks like IE 11 wasn't even rendering SVGs before this pull request. Not sure why - there are no console complaints.
  • Edge was rendering SVGs before this pull request, but not after. Also no console complaints.

Copy link
Copy Markdown
Contributor

@cwillisf cwillisf left a comment

Choose a reason for hiding this comment

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

👍
For the record, my SVG code was broken in IE from the beginning. I'm curious what's up with Edge, but IMO it shouldn't hold up this PR.

@cwillisf cwillisf assigned tmickel and unassigned cwillisf Nov 7, 2016
@tmickel tmickel merged commit b35f684 into scratchfoundation:develop Nov 7, 2016
@tmickel tmickel deleted the feature/quirks-mode branch November 7, 2016 22:41
@CosmicWebServices
Copy link
Copy Markdown

👏

cwillisf pushed a commit to cwillisf/scratch-render that referenced this pull request Jan 12, 2018
* Start of an SVG rendering quirks mode

* Provide font defaults in SVG quirks mode

* Remove svg-to-image dependency

* Remove fonts and use package fonts instead.

* try/finally for getBBox
cwillisf pushed a commit to cwillisf/scratch-render that referenced this pull request Jan 12, 2018
* Start of an SVG rendering quirks mode

* Provide font defaults in SVG quirks mode

* Remove svg-to-image dependency

* Remove fonts and use package fonts instead.

* try/finally for getBBox
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants