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

Split SvgRenderer into multiple functions #213

Merged
merged 7 commits into from
Feb 25, 2021

Conversation

adroitwhiz
Copy link
Contributor

@adroitwhiz adroitwhiz commented Jan 8, 2021

Depends on #212

Resolves

Resolves #211

Proposed Changes

This PR splits up the varied functionality of SvgRenderer (quirks-mode SVG conversion, SVG parsing, SVG serialization) into multiple individually exported functions, and deprecates the SvgRenderer class.

Reason for Changes

The SvgRenderer class previously contained a lot of disparate functionality, most of which was called from one specific part of the codebase (e.g. the VM's costume loading called into the "quirks mode conversion" part, the renderer called the drawing part).

This PR separates all that functionality into different separately-exported parts:

  • loadSvgString loads an SVG string into an element, normalizing it (and optionally doing quirks-mode conversion).
  • serializeSvgToString replaces SvgRenderer.toString, serializing an SVG element and optionally inlining fonts. I'm not sure if this wafer-thin wrapper around XMLSerializer needs to live here--it's called once by V2SVGAdapter when converting a V2 SVG string (without inlining fonts) and once in the renderer when setting an SVGSkin (inlining fonts).
  • V2SVGAdapter is a simple wrapper function that loads an SVG string with "quirks mode" then re-serializes it. Despite being a one-liner, it should probably be here because a few different places in the codebase attach it (including tests in the renderer, VM, and GUI).
    • That's SVG in caps--the convention seems to be varied but V2SVGAdapter is what the SvgRenderer class was imported as in most tests, despite SvgRenderer, inlineSvgFonts, and SvgElement using the Svg convention. Should I rename this to V2SvgAdapter to standardize this?

Functionality relating to actually, well, rendering the SVG will be moved back to the SVGSkin class in scratch-render, since that's the only place it's ever used.

Test Coverage

Tested manually :/

As far as I can tell, no public Scratch repository is calling this.
This will allow us to make these methods static / move them into a
separate "fixup" function
The VM uses the SvgRenderer solely to convert SVGs from version 2.
This allows the VM to simply import loadSvgString without bringing in
the entire SvgRenderer along with it. The next commit will move toString
to a separate file as well, so the VM's dependency on SvgRenderer can be
entirely removed.
I may be going a bit overboard with the exports here.
The details of what "normalizing" an SVG does should be easily viewable
to those using the publicly exported `loadSvgString` function.
src/index.js Outdated Show resolved Hide resolved
@adroitwhiz adroitwhiz requested a review from fsih February 22, 2021 23:12
Copy link
Contributor

@fsih fsih left a comment

Choose a reason for hiding this comment

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

LG, thank you!

@fsih fsih merged commit 30b250f into scratchfoundation:develop Feb 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Split up the SvgRenderer monolith
2 participants