Easiest way to embed svg into binary

Do you have a typical platform dependent issue you're battling with ? Ask it here. Make sure you mention your platform, compiler, and wxWidgets version.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7477
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Easiest way to embed svg into binary

Post by ONEEYEMAN »

Hi, ALL,
According to the documentation FromSVGResource() is implemented only for MSW and OSX, which actually make sense.

So maybe I need to make a static library that will contain all graphics and load it when in needed.

What do you recommend?

Thank you.
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: Easiest way to embed svg into binary

Post by PB »

Why not use the same what you used with your bitmaps (PNG, XPM)?

AFAICT, there is not much choice, it is either embed in source (e.g, via bin2c, see the toolbar sample) or use a file.
User avatar
doublemax
Moderator
Moderator
Posts: 19159
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Easiest way to embed svg into binary

Post by doublemax »

As SVG files are just text files, you could also just copy-and-paste them into a string literal.
Use the source, Luke!
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7477
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Easiest way to embed svg into binary

Post by ONEEYEMAN »

Hi, PB,
PB wrote: Sat Apr 09, 2022 6:49 pm Why not use the same what you used with your bitmaps (PNG, XPM)?
I used an XPM and just included the file and then made it from that.

So, you are saying I can just #include the svg file and use that in wxBitmapBundle::FromSVG()?
PB wrote: Sat Apr 09, 2022 6:49 pm AFAICT, there is not much choice, it is either embed in source (e.g, via bin2c, see the toolbar sample) or use a file.
Yeah, unices are not supporting the resource files.

Thank you.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7477
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Easiest way to embed svg into binary

Post by ONEEYEMAN »

doublemax,
doublemax wrote: Sat Apr 09, 2022 9:03 pm As SVG files are just text files, you could also just copy-and-paste them into a string literal.
And how would the code look like?

Thank you.
User avatar
doublemax
Moderator
Moderator
Posts: 19159
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Easiest way to embed svg into binary

Post by doublemax »

ONEEYEMAN wrote: Mon Apr 11, 2022 1:36 am doublemax,
doublemax wrote: Sat Apr 09, 2022 9:03 pm As SVG files are just text files, you could also just copy-and-paste them into a string literal.
And how would the code look like?

Thank you.

Code: Select all

const char arrowSVG[] = R"(<?xml version="1.0" encoding="UTF-8"?> <svg width="21px" height="21px" viewBox="0 0 21 21" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">     <polygon points="8.394 2.9995 7 4.3945 11.388 10.2185 7 16.0435 8.394 17.4375 15.613 10.2185" fill="#FFFFFF" fill-rule="evenodd"></polygon> </svg>)";
The R"()" is C++11, without it you'd have to escape all quotes.
Use the source, Luke!
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7477
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Easiest way to embed svg into binary

Post by ONEEYEMAN »

doublemax,
I wil probably do just that.
I'll put all of them in the header file, include that header in the source code and try it.

Otherwise my sources will be really huge and I'd like to avoid that.

Besides if PCH is available the compile time will be much better.

Thank you.
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: Easiest way to embed svg into binary

Post by PB »

ONEEYEMAN wrote: Mon Apr 11, 2022 5:10 pm I'll put all of them in the header file, include that header in the source code and try it.
Is that really a best idea to balloon a header file that will probably need to be included from every source file that creates an SVG-based bitmap bundle?

Would it not be better to declare embedded SVGs as extern in a header file and define them in a source file?
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7477
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Easiest way to embed svg into binary

Post by ONEEYEMAN »

PB,
As I said - in this case the source code will be huge (probably un-necessary).
And in my case all those files will be stored in the same place.

On top of that - the compilation time will be better as they will be inside the .h file.

But I maybe wrong...

Thank you.
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: Easiest way to embed svg into binary

Post by PB »

ONEEYEMAN wrote: Mon Apr 11, 2022 7:51 pm As I said - in this case the source code will be huge (probably un-necessary).
And in my case all those files will be stored in the same place.
I am no expert. IMO:
1) Where the SVGs are, in the header or the source file, will not have any effect on file size.
2) The large header will need to be processed during compilation, at best it will "only" significantly enlarge the PCH.
3) The source file will be preprocessed and compiled only once, unless its contents changes.

But as I said before, this is basically the same problem requiring the same solution as embedding PNGs or XPMs.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7477
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Easiest way to embed svg into binary

Post by ONEEYEMAN »

doublemax,
I tried it, but the compilation failed:

Code: Select all

#pragma once

const char query[] = R"(<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN"
 "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg version="1.0" xmlns="http://www.w3.org/2000/svg"
 width="15.000000pt" height="16.000000pt" viewBox="0 0 15.000000 16.000000"
 preserveAspectRatio="xMidYMid meet">

<g transform="translate(0.000000,16.000000) scale(0.100000,-0.100000)" fill="#000000" stroke="none">
<path d="M12 138 c-7 -7 -12 -34 -12 -60 0 -42 3 -48 24 -54 14 -3 33 -4 43
    -1 10 3 5 5 -14 6 -17 0 -35 6 -39 12 -4 8 4 9 27 5 l34 -5 -32 11 c-49 16
    -40 25 17 18 28 -4 50 -3 50 1 0 4 -21 9 -47 11 -65 4 -66 23 -1 22 l53 -2
    -48 7 c-26 3 -47 11 -47 16 0 6 21 13 47 16 l48 6 -45 1 c-26 1 -51 -3 -58
    -10z"/>
    <path d="M122 88 c-2 -44 -6 -58 -19 -61 -15 -4 -14 -5 2 -6 12 0 27 -7 35
    -14 12 -10 12 -9 3 3 -7 9 -14 43 -16 75 l-3 60 -2 -57z"/>
    </g>
    </svg>
    )";
    
I tried to make it with multiple lines.

Got following error:

Code: Select all

svgs.h(10): error C2146: syntax error: missing ';' before identifier 'fill'
As said - I tried to put it inside the header file and include it in the source.

Thank you.

P.S.: I'm going to attach the SVG for reference.
Attachments
query.svg.cpp
(865 Bytes) Downloaded 42 times
User avatar
doublemax
Moderator
Moderator
Posts: 19159
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Easiest way to embed svg into binary

Post by doublemax »

Unfortunately that SVG contains )" before "fill", but this is the closing sequence for the R"()". I guess this method is unsuitable for SVGs, sorry for putting you on the wrong track.

I just found this https://github.com/graphitemaster/incbin , which looks like it does what you need. But it does not support MSVC (in a convenient way), so i didn't test it yet.
Use the source, Luke!
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7477
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Easiest way to embed svg into binary

Post by ONEEYEMAN »

doublemax,
2 things:

1. Converting the PNG to SVG doesn't work (as you predicted). However, I tried to save PNG as SVG inside the editor. Will see how it will work out.

2. The error is now fixed based on this: https://stackoverflow.com/questions/718 ... ource-code and this: https://ideone.com/Mpnl3T.

Thank you.
ollydbg23
Super wx Problem Solver
Super wx Problem Solver
Posts: 438
Joined: Fri Dec 12, 2008 10:31 am

Re: Easiest way to embed svg into binary

Post by ollydbg23 »

ONEEYEMAN wrote: Wed Apr 13, 2022 2:35 pm
2. The error is now fixed based on this: https://stackoverflow.com/questions/718 ... ource-code and this: https://ideone.com/Mpnl3T.

Thank you.
Hi, the stackoverflow link is broken, can you fix the link? Thanks.
xamidi
Earned a small fee
Earned a small fee
Posts: 12
Joined: Sun Mar 13, 2022 1:30 am

Re: Easiest way to embed svg into binary

Post by xamidi »

I usually include SVGs simply as

Code: Select all

string svgName = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<svg [...]";
in source files, using text to C/C++ string converters such as: https://tomeko.net/online_tools/cpp_text_escape.php
Just copy the .svg file's contents, disable 'split output into multiple lines', click Convert and copy the generated text into the variable.
Post Reply