FLTK 1.4.5: Fl_SVG_Image Class Reference

FLTK 1.4.5

Loading...
Searching...
No Matches
Fl_SVG_Image Class Reference

The Fl_SVG_Image class supports loading, caching and drawing of scalable vector graphics (SVG) images. More...

`#include <Fl_SVG_Image.H>`

Inheritance diagram for Fl_SVG_Image:

## Public Member Functions
Fl_SVG_Image * | as_svg_image () FL_OVERRIDE
| Returns whether an image is an Fl_SVG_Image or not.
void | color_average (Fl_Color c, float i) FL_OVERRIDE
| The color_average() method averages the colors in the image with the provided FLTK color value.
Fl_Image * | copy () const
Fl_Image * | copy (int W, int H) const FL_OVERRIDE
| Creates a resized copy of the image.
void | desaturate () FL_OVERRIDE
| The desaturate() method converts an image to grayscale.
void | draw (int X, int Y)
void | draw (int X, int Y, int W, int H, int cx=0, int cy=0) FL_OVERRIDE
| Draws the image to the current drawing surface with a bounding box.
| Fl_SVG_Image (const char *filename)
| Load an SVG image from a file.
| Fl_SVG_Image (const char *sharedname, const char *svg_data)
| Load an SVG image from memory.
| Fl_SVG_Image (const char *sharedname, const unsigned char *svg_data, size_t length)
| Load an SVG image from memory.
void | normalize () FL_OVERRIDE
| Makes sure the object is fully initialized.
void | resize (int width, int height)
| Have the svg data (re-)rasterized using the given `width` and `height` values.
virtual | ~Fl_SVG_Image ()
| The destructor frees all memory and server resources that are used by the SVG image.
Public Member Functions inherited from Fl_RGB_Image
int | cache_h ()
int | cache_w ()
Fl_Image * | copy () const
void | draw (int X, int Y)
| Fl_RGB_Image (const Fl_Pixmap *pxm, Fl_Color bg=FL_GRAY)
| The constructor creates a new RGBA image from the specified Fl_Pixmap.
| Fl_RGB_Image (const uchar *bits, int bits_length, int W, int H, int D, int LD)
| The constructor creates a new image from the specified data.
| Fl_RGB_Image (const uchar *bits, int W, int H, int D=3, int LD=0)
| The constructor creates a new image from the specified data.
void | label (Fl_Menu_Item *m) FL_OVERRIDE
| This method is an obsolete way to set the image attribute of a menu item.
void | label (Fl_Widget *w) FL_OVERRIDE
| This method is an obsolete way to set the image attribute of a widget or menu item.
void | uncache () FL_OVERRIDE
| If the image has been cached for display, delete the cache data.
| ~Fl_RGB_Image () FL_OVERRIDE
| The destructor frees all memory and server resources that are used by the image.
Public Member Functions inherited from Fl_Image
virtual class Fl_Shared_Image * | as_shared_image ()
| Returns whether an image is an Fl_Shared_Image or not.
Fl_Image * | copy () const
| Creates a copy of the image in the same size.
int | count () const
| Returns the number of data values associated with the image.
int | d () const
| Returns the image depth.
const char *const * | data () const
| Returns a pointer to the current image data array.
int | data_h () const
| Returns the height of the image data.
int | data_w () const
| Returns the width of the image data.
void | draw (int X, int Y)
| Draws the image to the current drawing surface.
int | fail () const
| Returns a value that is not 0 if there is currently no image available.
| Fl_Image (int W, int H, int D)
| The constructor creates an empty image with the specified width, height, and depth.
int | h () const
| Returns the current image drawing height in FLTK units.
void | inactive ()
| The inactive() method calls color_average(FL_BACKGROUND_COLOR, 0.33f) to produce an image that appears grayed out.
int | ld () const
| Returns the current line data size in bytes.
virtual void | release ()
| Releases an Fl_Image - the same as '`delete` `this'`.
virtual void | scale (int width, int height, int proportional=1, int can_expand=0)
| Sets the drawing size of the image.
int | w () const
| Returns the current image drawing width in FLTK units.
virtual | ~Fl_Image ()
| The destructor is a virtual method that frees all memory used by the image.

## Public Attributes
bool | proportional
| Set this to `false` to allow image re-scaling that alters the image aspect ratio.
Public Attributes inherited from Fl_RGB_Image
int | alloc_array
| If non-zero, the object's data array is delete[]'d when deleting the object.
const uchar * | array
| Points to the start of the object's data array.

## Additional Inherited Members
Static Public Member Functions inherited from Fl_RGB_Image
static size_t | max_size ()
| Returns the maximum allowed image size in bytes when creating an Fl_RGB_Image object.
static void | max_size (size_t size)
| Sets the maximum allowed image size in bytes when creating an Fl_RGB_Image object.
Static Public Member Functions inherited from Fl_Image
static Fl_Labeltype | define_FL_IMAGE_LABEL ()
static Fl_RGB_Scaling | RGB_scaling ()
| Returns the currently used RGB image scaling method.
static void | RGB_scaling (Fl_RGB_Scaling)
| Sets the RGB image scaling method used for copy(int, int).
static Fl_RGB_Scaling | scaling_algorithm ()
| Gets what algorithm is used when resizing a source image to draw it.
static void | scaling_algorithm (Fl_RGB_Scaling algorithm)
| Sets what algorithm is used when resizing a source image to draw it.
Static Public Attributes inherited from Fl_Image
static const int | ERR_FILE_ACCESS = -2
static const int | ERR_FORMAT = -3
static const int | ERR_MEMORY_ACCESS = -4
static const int | ERR_NO_IMAGE = -1
static bool | register_images_done = false
| True after fl_register_images() was called, false before.
Protected Member Functions inherited from Fl_Image
void | d (int D)
| Sets the current image depth.
void | data (const char *const *p, int c)
| Sets the current data pointer and count of pointers in the array.
void | draw_empty (int X, int Y)
| The protected method draw_empty() draws a box with an X in it.
int | draw_scaled (int X, int Y, int W, int H)
| Draw the image to the current drawing surface rescaled to a given width and height.
void | h (int H)
| Sets the height of the image data.
void | ld (int LD)
| Sets the current line data size in bytes.
void | w (int W)
| Sets the width of the image data.
Static Protected Member Functions inherited from Fl_Image
static void | labeltype (const Fl_Label *lo, int lx, int ly, int lw, int lh, Fl_Align la)
static void | measure (const Fl_Label *lo, int &lw, int &lh)

## Detailed Description

The Fl_SVG_Image class supports loading, caching and drawing of scalable vector graphics (SVG) images.

The FLTK library performs parsing and rasterization of SVG data using a modified version of the `nanosvg` software (https://github.com/memononen/nanosvg). The software modification allows the option to change the image ratio while performing rasterization.

Use Fl_Image::fail() to check if the Fl_SVG_Image failed to load. fail() returns ERR_FILE_ACCESS if the file could not be opened or read, and ERR_FORMAT if the SVG format could not be decoded. If the image has loaded correctly, w(), h(), and d() should return values greater than zero.

Rasterization is not done until the image is first drawn or resize() or normalize() is called. Therefore, array is NULL until then. The delayed rasterization ensures an Fl_SVG_Image is always rasterized to the exact screen resolution at which it is drawn.

The Fl_SVG_Image class draws images computed by `nanosvg` with the following known limitations

- text between `<text>` and </text> marks,
- `image` elements, and
- <use> statements

are not rendered.

The FLTK library can optionally be built without SVG support; in that case, class Fl_SVG_Image is unavailable.

Example of displaying a hard-coded svg file:

#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_SVG_Image.H>
// A black rotated rectangle
const char *svg_data = "<svg viewBox=\"0 0 200 200\" version = \"1.1\">\n"
"<rect x=\"25\" y=\"50\" width=\"150\" height=\"100\" fill=\"black\" "
"transform=\"rotate(45 100 100)\"> </svg>\n";
int main(int argc, char **argv) {
Fl_SVG_Image *svg = new Fl_SVG_Image(0, svg_data); // create SVG object
Fl_Window *win = new Fl_Window(720, 486, "svg test");
Fl_Box *box = new Fl_Box(0, 0, win->w(), win->h());
box->image(svg); // assign svg object to Fl_Box
win->end();
win->show(argc,argv);
return(Fl::run());
}
Fl.H
Fl static class.
Fl_Box.H
Fl_Box widget.
Fl_Window.H
Fl_Window widget .
Fl_Box
This widget simply draws its box, and possibly its label.
Definition Fl_Box.H:34
Fl_Group::end
void end()
Exactly the same as current(this->parent()).
Definition Fl_Group.cxx:73
Fl_Widget::image
Fl_Image * image()
Gets the image that is used as part of the widget label when in the active state.
Definition Fl_Widget.H:591
Fl_Widget::w
void w(int v)
Internal use only.
Definition Fl_Widget.H:154
Fl_Widget::h
void h(int v)
Internal use only.
Definition Fl_Widget.H:156
Fl_Window
This widget produces an actual window.
Definition Fl_Window.H:55
Fl_Window::show
void show() FL_OVERRIDE
Puts the window on the screen.
Definition Fl_Window.cxx:549
Fl::run
static int run()
Calls Fl::wait()repeatedly as long as any windows are displayed.
Definition Fl.cxx:651

Example of displaying an svg image from a file:

#include <errno.h> // errno
#include <string.h> // strerror
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_SVG_Image.H>
#include <FL/fl_message.H>
int main(int argc, char **argv) {
Fl_Window *win = new Fl_Window(720, 486, "svg test");
Fl_Box *box = new Fl_Box(0, 0, win->w(), win->h());
// Load svg image from disk, assign to a box
const char *svgpath = "/var/tmp/simple.svg";
Fl_SVG_Image *svg = new Fl_SVG_Image(svgpath); // load SVG object from disk
switch (svg->fail()) {
case Fl_Image::ERR_FILE_ACCESS:
// File couldn't load? show path + os error to user
fl_alert("%s: %s", svgpath, strerror(errno));
return 1;
case Fl_Image::ERR_FORMAT:
// Parsing error
fl_alert("%s: couldn't decode image", svgpath);
return 1;
}
box->image(svg); // assign svg object to box
win->end();
win->show(argc,argv);
return(Fl::run());
}
Fl_Image::fail
int fail() const
Returns a value that is not 0 if there is currently no image available.
Definition Fl_Image.cxx:203

Example of fitting an svg image to a resizable Fl_Box:

#include <FL/Fl_Window.H>
#include <FL/Fl_SVG_Image.H>
#include <FL/Fl_Box.H>
class resizable_box : public Fl_Box {
public:
resizable_box(int w, int h) : Fl_Box(0, 0, w, h, NULL) {}
virtual void resize(int x, int y, int w, int h) {
image()->scale(w, h, 1, 1); // p3 = proportional, p4 = can_expand
Fl_Box::resize(x, y, w, h);
}
};
int main(int argc, char **argv) {
Fl_Window *win = new Fl_Window(130, 130);
resizable_box *box = new resizable_box(win->w(), win->h());
Fl_SVG_Image *svg = new Fl_SVG_Image("/path/to/image.svg");
box->image(svg);
svg->scale(box->w(), box->h());
win->end();
win->resizable(win);
win->show(argc, argv);
return Fl::run();
}
Fl_Group::resizable
void resizable(Fl_Widget &o)
Sets the group's resizable widget.
Definition Fl_Group.H:156
Fl_Image::scale
virtual void scale(int width, int height, int proportional=1, int can_expand=0)
Sets the drawing size of the image.
Definition Fl_Image.cxx:290
Fl_Image::h
void h(int H)
Sets the height of the image data.
Definition Fl_Image.H:93
Fl_Image::w
void w(int W)
Sets the width of the image data.
Definition Fl_Image.H:87
Fl_Widget::resize
virtual void resize(int x, int y, int w, int h)
Changes the size or position of the widget.
Definition Fl_Widget.cxx:138

## Constructor & Destructor Documentation

## ◆ Fl_SVG_Image() [1/3]

Fl_SVG_Image::Fl_SVG_Image | ( | const char * | filename | ) |

Load an SVG image from a file.

This constructor loads the SVG image from a .svg or .svgz file. The reader recognizes if the data is compressed, and decompresses it if zlib is available (HAVE_LIBZ).

Parameters

  filename | the filename for a .svg or .svgz file

## ◆ Fl_SVG_Image() [2/3]

Fl_SVG_Image::Fl_SVG_Image | ( | const char * | sharedname,
|  | const char * | svg_data )

Load an SVG image from memory.

This constructor loads the SVG image from a block of memory. This version is commonly used for uncompressed text data, but the reader recognizes if the data is compressed, and decompresses it if zlib is available (HAVE_LIBZ).

Parameters

  sharedname | if not `NULL`, a shared image will be generated with this name
  svg_data | a pointer to the memory location of the SVG image data

Note
  In-memory SVG data is parsed by the object constructor and is no longer needed after construction.

## ◆ Fl_SVG_Image() [3/3]

Fl_SVG_Image::Fl_SVG_Image | ( | const char * | name,
|  | const unsigned char * | svg_data,
|  | size_t | length )

Load an SVG image from memory.

This constructor loads the SVG image from a block of memory. This version is commonly used for compressed binary data, but the reader recognizes if the data is uncompressed, and reads it as a text block.

Parameters

  name | if not `NULL`, a shared image will be generated with this name
  svg_data | a pointer to the memory location of the SVG image data
  length | of `svg_data` or `0` if the length is unknown. This will protect memory outside of the `svg_data` array from illegal read operations for compressed SVG data

Note
  In-memory SVG data is parsed by the object constructor and is no longer needed after construction.

## Member Function Documentation

## ◆ as_svg_image()

Fl_SVG_Image * Fl_SVG_Image::as_svg_image | ( |  | ) |
| inlinevirtual

Returns whether an image is an Fl_SVG_Image or not.

This virtual method returns a pointer to the Fl_SVG_Image if this object is an instance of Fl_SVG_Image or NULL if not.

Reimplemented from Fl_RGB_Image.

## ◆ color_average()

void Fl_SVG_Image::color_average | ( | Fl_Color | c,
|  | float | i )
| virtual

The color_average() method averages the colors in the image with the provided FLTK color value.

The first argument specifies the FLTK color to be used.

The second argument specifies the amount of the original image to combine with the color, so a value of 1.0 results in no color blend, and a value of 0.0 results in a constant image of the specified color.

An internal copy is made of the original image data before changes are applied, to avoid modifying the original image data in memory.

Reimplemented from Fl_RGB_Image.

## ◆ copy()

Fl_Image * Fl_SVG_Image::copy | ( | int | W,
|  | int | H ) const
| virtual

Creates a resized copy of the image.

It is recommended not to call this member function to reduce the size of an image to the size of the area where this image will be drawn, and to use Fl_Image::scale() instead.

The new image should be released when you are done with it.

Note: since FLTK 1.4.0 you can use Fl_Image::release() for all types of images (i.e. all subclasses of Fl_Image) instead of operator delete for Fl_Image's and Fl_Image::release() for Fl_Shared_Image's.

The new image data will be converted to the requested size. RGB images are resized using the algorithm set by Fl_Image::RGB_scaling().

For the new image the following equations are true:

- w() == data_w() == `W`
- h() == data_h() == `H`

Parameters

  [in] | W,H | Requested width and height of the new image

Note
  The returned image can be safely cast to the same image type as that of the source image provided this type is one of Fl_RGB_Image, Fl_SVG_Image, Fl_Pixmap, Fl_Bitmap, Fl_Tiled_Image, Fl_Anim_GIF_Image and Fl_Shared_Image. Returned objects copied from images of other, derived, image classes belong to the parent class appearing in this list. For example, the copy of an Fl_GIF_Image is an object of class Fl_Pixmap.
  Since FLTK 1.4.0 this method is 'const'. If you derive your own class from Fl_Image or any subclass your overridden methods of 'Fl_Image::copy() const' and 'Fl_Image::copy(int, int) const' must also be 'const' for inheritance to work properly. This is different than in FLTK 1.3.x and earlier where these methods have not been 'const'.

Reimplemented from Fl_RGB_Image.

## ◆ desaturate()

void Fl_SVG_Image::desaturate | ( |  | ) |
| virtual

The desaturate() method converts an image to grayscale.

If the image contains an alpha channel (depth = 4), the alpha channel is preserved.

An internal copy is made of the original image data before changes are applied, to avoid modifying the original image data in memory.

Reimplemented from Fl_RGB_Image.

## ◆ draw()

void Fl_SVG_Image::draw | ( | int | X,
|  | int | Y,
|  | int | W,
|  | int | H,
|  | int | cx = 0,
|  | int | cy = 0 )
| virtual

Draws the image to the current drawing surface with a bounding box.

Arguments X,Y,W,H specify a bounding box for the image, with the origin (upper-left corner) of the image offset by the `cx` and `cy` arguments.

Another way to see what part of the image gets drawn and where, is to consider this alternative writing producing the same output:

    fl_push_clip(X,Y,W,H);
    this->draw(X - cx, Y - cy);
    fl_pop_clip();

Repeated calls to this member function with the same image but varying W, H, cx, or cy arguments may be more efficiently processed using the above alternative writing.

Reimplemented from Fl_RGB_Image.

## ◆ normalize()

void Fl_SVG_Image::normalize | ( |  | ) |
| virtual

Makes sure the object is fully initialized.

This function rasterizes the SVG image, and consequently initializes its array member, if that was not done before.

Reimplemented from Fl_RGB_Image.

## ◆ resize()

void Fl_SVG_Image::resize | ( | int | width,
|  | int | height )

Have the svg data (re-)rasterized using the given `width` and `height` values.

By default, the resulting image w() and h() will be close to `width` and `height` while preserving the width/height ratio of the SVG data. If proportional was set to `false`, the image is rasterized to the exact `width` and `height` values. In both cases, data_w() and data_h() values are set to w() and h(), respectively.

## Member Data Documentation

## ◆ proportional

bool Fl_SVG_Image::proportional

Set this to `false` to allow image re-scaling that alters the image aspect ratio.

Upon object creation, proportional is set to `true`, and the aspect ratio is kept constant.

The documentation for this class was generated from the following files:

- Fl_SVG_Image.H
- Fl_SVG_Image.cxx