| 1 |
// |
| 2 |
// "$Id: Fl_SVG_Image.H 12539 2017-11-03 09:04:56Z manolo $" |
| 3 |
// |
| 4 |
// SVG Image header file for the Fast Light Tool Kit (FLTK). |
| 5 |
// |
| 6 |
// Copyright 2017 by Bill Spitzak and others. |
| 7 |
// |
| 8 |
// This library is free software. Distribution and use rights are outlined in |
| 9 |
// the file "COPYING" which should have been included with this file. If this |
| 10 |
// file is missing or damaged, see the license at: |
| 11 |
// |
| 12 |
// http://www.fltk.org/COPYING.php |
| 13 |
// |
| 14 |
// Please report all bugs and problems on the following page: |
| 15 |
// |
| 16 |
// http://www.fltk.org/str.php |
| 17 |
// |
| 18 |
|
| 19 |
#ifndef FL_SVG_IMAGE_H |
| 20 |
#define FL_SVG_IMAGE_H |
| 21 |
|
| 22 |
#include <FL/Fl_Image.H> |
| 23 |
|
| 24 |
struct NSVGimage; |
| 25 |
|
| 26 |
/** The Fl_SVG_Image class supports loading, caching and drawing of scalable vector graphics (SVG) images. |
| 27 |
The FLTK library performs parsing and rasterization of SVG data using a modified version |
| 28 |
of the \c nanosvg software (https://github.com/memononen/nanosvg) © 2013-14 Mikko Mononen |
| 29 |
([email protected]). The software modification allows the option to change the image ratio |
| 30 |
while performing rasterization. |
| 31 |
|
| 32 |
Use Fl_Image::fail() to check if the Fl_SVG_Image failed to load. fail() returns ERR_FILE_ACCESS |
| 33 |
if the file could not be opened or read, and ERR_FORMAT if the SVG format could not be decoded. |
| 34 |
If the image has loaded correctly, w(), h(), and d() should return values greater than zero. |
| 35 |
|
| 36 |
Rasterization is not done until the image is first drawn or resize() is called. Therefore, |
| 37 |
\ref array is NULL until then. The delayed rasterization ensures an Fl_Shared_Image based on |
| 38 |
an SVG image and scaled to its display size by Fl_Shared_Image::scale() will be |
| 39 |
always rasterized to the exact screen resolution. |
| 40 |
|
| 41 |
The Fl_SVG_Image class draws images computed by \c nanosvg: one known limitation is that text |
| 42 |
within \c <text\></text\> blocks is not rendered. |
| 43 |
|
| 44 |
The FLTK library can optionally be built without SVG support; in that case, |
| 45 |
class Fl_SVG_Image is unavailable. |
| 46 |
|
| 47 |
Example of displaying a hard-coded svg file: |
| 48 |
\code |
| 49 |
#include <FL/Fl.H> |
| 50 |
#include <FL/Fl_Window.H> |
| 51 |
#include <FL/Fl_Box.H> |
| 52 |
#include <FL/Fl_SVG_Image.H> |
| 53 |
|
| 54 |
// A black rotated rectangle |
| 55 |
const char *svg_data = "<svg viewBox=\"0 0 200 200\" version = \"1.1\">\n" |
| 56 |
"<rect x=\"25\" y=\"50\" width=\"150\" height=\"100\" fill=\"black\" " |
| 57 |
"transform=\"rotate(45 100 100)\"> </svg>\n"; |
| 58 |
|
| 59 |
int main(int argc, char **argv) { |
| 60 |
Fl_SVG_Image *svg = new Fl_SVG_Image(0, svg_data); // create SVG object |
| 61 |
Fl_Window *win = new Fl_Window(720, 486, "svg test"); |
| 62 |
Fl_Box *box = new Fl_Box(0,0,win->w(),win->h()); |
| 63 |
box->image(svg); // assign svg object to Fl_Box |
| 64 |
win->end(); |
| 65 |
win->show(argc,argv); |
| 66 |
return(Fl::run()); |
| 67 |
} |
| 68 |
\endcode |
| 69 |
|
| 70 |
Example of displaying an svg image from a file: |
| 71 |
\code |
| 72 |
#include <errno.h> // errno |
| 73 |
#include <string.h> // strerror |
| 74 |
#include <FL/Fl.H> |
| 75 |
#include <FL/Fl_Window.H> |
| 76 |
#include <FL/Fl_Box.H> |
| 77 |
#include <FL/Fl_SVG_Image.H> |
| 78 |
#include <FL/fl_message.H> |
| 79 |
int main(int argc, char **argv) { |
| 80 |
Fl_Window *win = new Fl_Window(720, 486, "svg test"); |
| 81 |
Fl_Box *box = new Fl_Box(0,0,win->w(),win->h()); |
| 82 |
|
| 83 |
// Load svg image from disk, assign to a box |
| 84 |
const char *svgpath = "/var/tmp/simple.svg"; |
| 85 |
Fl_SVG_Image *svg = new Fl_SVG_Image(svgpath); // load SVG object from disk |
| 86 |
switch ( svg->fail() ) { |
| 87 |
case Fl_Image::ERR_FILE_ACCESS: |
| 88 |
// File couldn't load? show path + os error to user |
| 89 |
fl_alert("%s: %s", svgpath, strerror(errno)); |
| 90 |
return 1; |
| 91 |
case Fl_Image::ERR_FORMAT: |
| 92 |
// Parsing error |
| 93 |
fl_alert("%s: couldn't decode image", svgpath); |
| 94 |
return 1; |
| 95 |
} |
| 96 |
box->image(svg); // assign svg object to box |
| 97 |
|
| 98 |
win->end(); |
| 99 |
win->show(argc,argv); |
| 100 |
return(Fl::run()); |
| 101 |
} |
| 102 |
\endcode |
| 103 |
|
| 104 |
*/ |
| 105 |
class FL_EXPORT Fl_SVG_Image : public Fl_RGB_Image { |
| 106 |
private: |
| 107 |
typedef struct { |
| 108 |
NSVGimage* svg_image; |
| 109 |
int ref_count; |
| 110 |
} counted_NSVGimage; |
| 111 |
counted_NSVGimage* counted_svg_image_; |
| 112 |
bool rasterized_; |
| 113 |
int raster_w_, raster_h_; |
| 114 |
bool to_desaturate_; |
| 115 |
Fl_Color average_color_; |
| 116 |
float average_weight_; |
| 117 |
float svg_scaling_(int W, int H); |
| 118 |
void rasterize_(int W, int H); |
| 119 |
void init_(const char *filename, const char *filedata, Fl_SVG_Image *copy_source); |
| 120 |
Fl_SVG_Image(Fl_SVG_Image *source); |
| 121 |
protected: |
| 122 |
virtual int draw_scaled(int X, int Y, int W, int H); |
| 123 |
public: |
| 124 |
/** Set this to \c false to allow image re-scaling that alters the image aspect ratio. |
| 125 |
Upon object creation, proportional is set to \c true, and the aspect ratio is kept constant.*/ |
| 126 |
bool proportional; |
| 127 |
Fl_SVG_Image(const char *filename, const char *svg_data = NULL); |
| 128 |
virtual ~Fl_SVG_Image(); |
| 129 |
virtual Fl_Image *copy(int W, int H); |
| 130 |
void resize(int width, int height); |
| 131 |
virtual void desaturate(); |
| 132 |
virtual void color_average(Fl_Color c, float i); |
| 133 |
virtual void draw(int X, int Y, int W, int H, int cx = 0, int cy = 0); |
| 134 |
void draw(int X, int Y) { draw(X, Y, w(), h(), 0, 0); } |
| 135 |
}; |
| 136 |
|
| 137 |
#endif // FL_SVG_IMAGE_H |
| 138 |
|
| 139 |
// |
| 140 |
// End of "$Id: Fl_SVG_Image.H 12539 2017-11-03 09:04:56Z manolo $". |
| 141 |
// |