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

Add a PNG output to LegendRenderer #393

Merged
merged 9 commits into from
Sep 14, 2022
Merged

Conversation

jahow
Copy link
Contributor

@jahow jahow commented Jul 18, 2022

This PR refactors the LegendRenderer class to externalize all "rendering" logic (i.e. using D3 and SVG) to a separate SvgOutput class with atomic operations (add image, add label, etc.).

Then, another PngOutput class is added and LegendRenderer can essentially use both interchangeably. Also added a renderAsImage method to the LegendRenderer API in order to obtain an image without providing a container, useful when the legend is intended to be downloaded instead of being added to the DOM.

To use PNG output:

const renderer = new LegendRenderer({
  // ...
});
renderer.render(target, 'png');

// or
const canvas = await renderer.renderAsImage('png'); // returns a HTMLCanvasElement

The important part is that the default behavior of LegendRenderer (generating an SVG and appending it) is almost entirely unchanged. The only difference is about the root svg namespace which is sometimes added when it was not before (see commits).

Caveat: labels are not shortened in the PNG output. This will need a bit more refactoring so I thought I'd leave that for later.

Remaining to do:

  • rewrite tests
  • add a small test app to compare different outputs side by side

Also I think it would be nice to set up some wider regression tests so that it's easier to add more features, e.g. automatic width adjustment, more options to customize layout, etc.!

Fixes #390

@jansule
Copy link
Contributor

jansule commented Jul 19, 2022

From a first glance, this looks really good! Thanks @jahow

Could you let us know, when this is ready for review?

@jahow
Copy link
Contributor Author

jahow commented Jul 19, 2022

Sure, hopefully in the coming days!

@jahow
Copy link
Contributor Author

jahow commented Sep 11, 2022

Making slow progress on this but I haven't forgotten 🤓

@jahow
Copy link
Contributor Author

jahow commented Sep 12, 2022

@jansule the test coverage is now pretty good, this is ready for review.

I couldn't make a test app yet but this is in my plans for later. At least this PR should keep the default behaviour unchanged and not break anything!

This implements atomic operations like adding text or images, as well as
generating the final image.
This means that LegendRenderer does not use SVG or D3 at all and only rely
on the output implementation.

This change should not change the output or behaviour of LegendRenderer in any
way, except for one this:
the 'xmlns' attribute is now set every time an image is added to the svg legend,
and not only with remote legends.
LegendRenderer can now give an image without a container, and the output
type can be changed (SVG by default).
Containers are ignored in PNG, and a custom logic is added to extend the
canvas when an image goes outside of the frame.
Remove dependencies to d3, check for output calls instead
Copy link
Contributor

@jansule jansule left a comment

Choose a reason for hiding this comment

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

This is great work @jahow! Thanks

src/LegendRenderer/LegendRenderer.ts Outdated Show resolved Hide resolved
src/LegendRenderer/PngOutput.spec.ts Outdated Show resolved Hide resolved
jahow and others added 2 commits September 13, 2022 11:33
Co-authored-by: Jan Suleiman <jansule@users.noreply.github.com>
@jahow
Copy link
Contributor Author

jahow commented Sep 13, 2022

@jansule this is good to merge!

@jansule jansule merged commit ddc40b9 into geostyler:master Sep 14, 2022
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.

Add a PNG/JPEG output
2 participants