Expose SVG renderer?

54 views
Skip to first unread message

Eric Jensen

unread,
Sep 9, 2022, 6:57:29 AM9/9/22
to wx-u...@googlegroups.com
Hi,

i think it would be nice if the SVG renderer would be exposed as a
dedicated class rather than being hidden inside wxBitmapBundle.

Currently my main issue is that sometimes i need to render SVGs loaded
by the user whose aspect ratio i don't know in advance. But
wxBitmapBundle expects a fully qualified size, i can't pass -1 as
width or height, and have the renderer calculate it using the SVG
aspect ratio.

A dedicated SVG renderer would also open the path to using different
SVG implementations. It could also provide an interface to render
directly into a wxDC or wxGraphicsContext, even if it may be
implemented by rendering into a bitmap first if rendering directly is not
supported.

Regards,
Eric

Vadim Zeitlin

unread,
Sep 10, 2022, 10:46:14 AM9/10/22
to wx-u...@googlegroups.com
On Fri, 9 Sep 2022 12:57:28 +0200 Eric Jensen wrote:

EJ> i think it would be nice if the SVG renderer would be exposed as a
EJ> dedicated class rather than being hidden inside wxBitmapBundle.

Sure, the reason it wasn't done before was because I wanted to do
something as quickly as possible for 3.2.0. Now that it's released, we can
enhance SVG support further.

EJ> Currently my main issue is that sometimes i need to render SVGs loaded
EJ> by the user whose aspect ratio i don't know in advance. But
EJ> wxBitmapBundle expects a fully qualified size, i can't pass -1 as
EJ> width or height, and have the renderer calculate it using the SVG
EJ> aspect ratio.

I find the need to pass the size to FromSVG() annoying too, but I didn't
know what to do without it. I still don't really understand how having a
custom renderer would help with it, to be honest. But the idea of letting
specify just a single component of the image seems useful and I think it
would be nice to relax the check in FromSVG() to allow it. Shouldn't this
be doable with nanoSVG?

EJ> A dedicated SVG renderer would also open the path to using different
EJ> SVG implementations. It could also provide an interface to render
EJ> directly into a wxDC or wxGraphicsContext

I think we should forget about wxDC, but yes, rendering to wxGC directly
would make sense.

Regards,
VZ

--
TT-Solutions: wxWidgets consultancy and technical support
http://www.tt-solutions.com/

Quentin Cosendey

unread,
Sep 16, 2022, 3:16:55 AM9/16/22
to wx-u...@googlegroups.com
Hello,

I really think that it would be good to be able to draw a SVG directly
to a wxGraphicsContext.
For one of my projects, I have a custom component and would be happy to
switch to SVG, but don't know well if I'm getting it properly. For the
moment, I'm basically doing this:

wxImageBundle bundle = wxImageBundle::FromSVGFile("image.svg",
wxSize(100, 100));
wxBitmap bitmap = bundle.GetBitmap(wxSize(desiredWidth, desiredHeight));
graphicsContext->DrawBitmap(bitmap, desiredX, desiredY, desiredWidth,
desiredHeight);

Except that, of course, I load the SVG file only once and keep the
wxBitmapBundle object.

Desired width/height are known, they are computed based on available
client size. They are more or less fixed once computed, but may change
over time if the user resizes the window and/or change a few display
options of the program.

As far as what is possible right now witht the current wxWidgets
version, am I doing it right ?

For that kind of use, in fact, ideally, I don't care if the SVG is first
rendered on a bitmap first, or directly drawn without temporary bitmap.
IN that sense, being able to use a SVG renderer outside of
wxBitmapBundle would be good, I think. It would be probably even better
to be able to render a SVG by using directly wxGaphicsContext methods,
it should be possible, but it's clear that it isn't a so easy task as it
looks like.

I still don't understand well what to pass as size in
wxBitmapBundle::SVGFromFile, the 100x100 here is like a random choice
because I know in advance that the image is a square, but that's all. I
shouldn't be obliged to pass an hard-coded size here, if one day the
image has to be replaced by something rectangular.

I still don't understand why it would be bad to take the width/height or
viewBox attributes of the SVG as defaults when wxDefaultSize is passed.
And if, and only if no size is passed and there is no width/height or
viewBox to base on, then log an error.

Thank you.
Reply all
Reply to author
Forward
0 new messages