Fl_SVG_Image.H 5.0 KB

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