1.10
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
SvgImage Class Reference

An SVG image. More...

#include <svgimage.h>

Public Member Functions

 SvgImage ()
 
 SvgImage (const std::string &uri, const SizeF &size={})
 
 SvgImage (const SvgImage &)=delete
 
 SvgImage (SvgImage &&) noexcept
 
 ~SvgImage () noexcept
 
EGT_NODISCARD RectF id_box (const std::string &id) const
 Get the position and size of an element in the SVG.
 
EGT_NODISCARD bool id_exists (const std::string &id) const
 Test if an element exists in the SVG image.
 
EGT_NODISCARD Image id_image (const std::string &id) const
 Render the image of the specific element in the SVG file.
 
 operator Image () const
 Overload to convert to an Image.
 
SvgImageoperator= (const SvgImage &)=delete
 
SvgImageoperator= (SvgImage &&) noexcept
 
EGT_NODISCARD Image render (const std::string &id={}, const RectF &rect={}) const
 Render the SVG optionally with only the specified element id in the SVG file.
 
EGT_NODISCARD SizeF size () const
 Get the SVG size.
 
void size (const SizeF &size)
 Set the size you want any rendered Image to be.
 
void uri (const std::string &uri)
 Set the URI.
 

Protected Member Functions

EGT_NODISCARD shared_cairo_surface_t do_render (const std::string &id={}, const RectF &rect={}) const
 Render and return a new surface.
 
void load ()
 Load the SVG file.
 

Protected Attributes

std::unique_ptr< SvgImpl > m_impl
 Implementation pointer.
 
SizeF m_size
 User set size.
 
std::string m_uri
 User set URI.
 

Detailed Description

An SVG image.

This class is not just for working with a single Image. It allows loading an SVG file or data once, and then rendering any number of images at different sizes or made up of different elements from the SVG file.

SvgImage svg("file.svg");
An SVG image.
Definition svgimage.h:57

For using individual elements from a SVG file and render them separately the document must be created with element IDs. In the dashboard example for instance, one of the dash_background.svg element has the attribute:

id="right_blink"

Several methods in the SvgImage class allow the use of such elements when the id parameter is present. Element IDs must follow the syntax of an URL fragment identifier. For example, use "#right_blink" (hash right_blink) to obtain a handle on the SVG element mentioned just above; which gives:

SvgImage custom1svg("file:dash_background.svg", SizeF(200, 0));
auto img_box = custom1svg.id_box("#right_blink");
SizeType< float, detail::Compatible::normal > SizeF
Helper type alias.
Definition geometry.h:582
Note
Some standard effects in SVG files are expensive to render. For example, some patterns and gradients. In those cases, you can convert elements in the SVG itself to raster images to speed up rendering here.

Constructor & Destructor Documentation

◆ SvgImage() [1/4]

SvgImage ( )

◆ SvgImage() [2/4]

SvgImage ( const std::string &  uri,
const SizeF size = {} 
)
explicit
Parameters
uriResource path.
See also
Resources
Parameters
[in]sizeSize of the image.
See also
SvgImage::size() for more information about specifying size.

◆ SvgImage() [3/4]

SvgImage ( const SvgImage )
delete

◆ SvgImage() [4/4]

SvgImage ( SvgImage &&  )
noexcept

◆ ~SvgImage()

~SvgImage ( )
noexcept

Member Function Documentation

◆ do_render()

EGT_NODISCARD shared_cairo_surface_t do_render ( const std::string &  id = {},
const RectF rect = {} 
) const
protected

Render and return a new surface.

◆ id_box()

EGT_NODISCARD RectF id_box ( const std::string &  id) const

Get the position and size of an element in the SVG.

Parameters
[in]idThe id of the SVG element.
Element IDs must follow the syntax of an URL fragment identifier. For example, use "#right_blink" (hash right_blink) to obtain a handle on the SVG element with id="right_blink".

◆ id_exists()

EGT_NODISCARD bool id_exists ( const std::string &  id) const

Test if an element exists in the SVG image.

Parameters
[in]idThe id of the SVG element.
Element IDs must follow the syntax of an URL fragment identifier. For example, use "#right_blink" (hash right_blink) to obtain a handle on the SVG element with id="right_blink".
Returns
true if found

◆ id_image()

EGT_NODISCARD Image id_image ( const std::string &  id) const
inline

Render the image of the specific element in the SVG file.

This is the same as calling:

render("id", svg.id_box("id"))
EGT_NODISCARD Image render(const std::string &id={}, const RectF &rect={}) const
Render the SVG optionally with only the specified element id in the SVG file.
Note
The size of the returned image will be the size of the specific element in the SVG file.
See also
render().

◆ load()

void load ( )
protected

Load the SVG file.

◆ operator Image()

operator Image ( ) const

Overload to convert to an Image.

This is the same as calling render().

◆ operator=() [1/2]

SvgImage & operator= ( const SvgImage )
delete

◆ operator=() [2/2]

SvgImage & operator= ( SvgImage &&  )
noexcept

◆ render()

EGT_NODISCARD Image render ( const std::string &  id = {},
const RectF rect = {} 
) const

Render the SVG optionally with only the specified element id in the SVG file.

Note
If no rect is specified or an empty rect is specified, the raster Image returned will be the same size as the entire SVG.
Parameters
[in]idOptional id of the SVG element to render. If no id is specified, all elements in the SVG will be rendered.
Element IDs must follow the syntax of an URL fragment identifier. For example, use "#right_blink" (hash right_blink) to obtain a handle on the SVG element with id="right_blink".
[in]rectOptional rect to clip to.

◆ size() [1/2]

EGT_NODISCARD SizeF size ( ) const

Get the SVG size.

◆ size() [2/2]

void size ( const SizeF size)
inline

Set the size you want any rendered Image to be.

This is somewhat special. If you specify zero for one of the dimensions, the other dimension will be automatically computed keeping the same aspect ratio.

If both dimensions are zero, the default size will be loaded from the SVG file.

◆ uri()

void uri ( const std::string &  uri)

Set the URI.

If this results in changing the URI for this instance, this will cause a re-load of the SVG file

Member Data Documentation

◆ m_impl

std::unique_ptr<SvgImpl> m_impl
protected

Implementation pointer.

◆ m_size

SizeF m_size
protected

User set size.

Use size() to get the calculated size.

◆ m_uri

std::string m_uri
protected

User set URI.