Debian Codegraph @

FL/Fl_SVG_Image.cxx

raw symbols SCIP
1 #include <FL/Enumerations.H>
2 #if 10*FL_MAJOR_VERSION + FL_MINOR_VERSION < 14
3
4 #define HAVE_LIBZ 1
5 #define HAVE_LONG_LONG 1
6
7 #include <FL/Fl_SVG_Image.H>
8 #include <FL/fl_utf8.h>
9 #include <FL/fl_draw.H>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #if defined(HAVE_LIBZ)
13 #include <zlib.h>
14 #endif
15
16 #if !defined(HAVE_LONG_LONG)
17 static double strtoll(const char *str, char **endptr, int base) {
18 return (double)strtol(str, endptr, base);
19 }
20 #endif
21
22 #define NANOSVG_ALL_COLOR_KEYWORDS // Include full list of color keywords.
23 #define NANOSVG_IMPLEMENTATION // Expands implementation
24 #include "FL/nanosvg.h"
25
26 #define NANOSVGRAST_IMPLEMENTATION // Expands implementation
27 #include "FL/altsvgrast.h"
28
29
30 /** The constructor loads the SVG image from the given .svg/.svgz filename or in-memory data.
31 \param filename The full path and name of a .svg or .svgz file, or NULL.
32 \param svg_data A pointer to the memory location of the SVG image data.
33 This parameter allows to load an SVG image from in-memory data, and is used when \p filename is NULL.
34 \note In-memory SVG data is parsed by the object constructor and is not used after construction.
35 */
36 Fl_SVG_Image::Fl_SVG_Image(const char *filename, const char *svg_data) : Fl_RGB_Image(NULL, 0, 0, 4) {
37 init_(filename, svg_data, NULL);
38 }
39
40
41 // private constructor
42 Fl_SVG_Image::Fl_SVG_Image(Fl_SVG_Image *source) : Fl_RGB_Image(NULL, 0, 0, 4) {
43 init_(NULL, NULL, source);
44 }
45
46
47 /** The destructor frees all memory and server resources that are used by the SVG image. */
48 Fl_SVG_Image::~Fl_SVG_Image() {
49 if ( --counted_svg_image_->ref_count <= 0) {
50 nsvgDelete(counted_svg_image_->svg_image);
51 delete counted_svg_image_;
52 }
53 }
54
55
56 float Fl_SVG_Image::svg_scaling_(int W, int H) {
57 float f1 = float(W) / int(counted_svg_image_->svg_image->width+0.5);
58 float f2 = float(H) / int(counted_svg_image_->svg_image->height+0.5);
59 return (f1 < f2) ? f1 : f2;
60 }
61
62 #if defined(HAVE_LIBZ)
63
64 static char *svg_inflate(const char *fname) {
65 FILE *in = fl_fopen(fname, "r");
66 if (!in) return NULL;
67 unsigned char header[2];
68 if (fread(header, 2, 1, in) < 1) { // FIXME: can't read file header
69 header[0] = header[1] = 0; // FIXME: continuing anyway ?
70 }
71 int direct = (header[0] != 0x1f || header[1] != 0x8b);
72 fseek(in, 0, SEEK_END);
73 long size = ftell(in);
74 fclose(in);
75 int fd = fl_open(fname, 0);
76 if (fd < 0) return NULL;
77 gzFile gzf = gzdopen(fd, "r");
78 if (!gzf) return NULL;
79 int l;
80 long out_size = direct ? size + 1 : 3*size + 1;
81 char *out = (char*)malloc(out_size);
82 char *p = out;
83 do {
84 if ((!direct) && p + size > out + out_size) {
85 out_size += size;
86 char *tmp = (char*)realloc(out, out_size + 1);
87 p = tmp + (p - out);
88 out = tmp;
89 }
90 l = gzread(gzf, p, size);
91 if (l > 0) {
92 p += l; *p = 0;
93 }
94 } while ((!direct) && l >0);
95 gzclose(gzf);
96 if (!direct) out = (char*)realloc(out, (p-out)+1);
97 return out;
98 }
99 #endif
100
101 void Fl_SVG_Image::init_(const char *filename, const char *in_filedata, Fl_SVG_Image *copy_source) {
102 if (copy_source) {
103 filename = in_filedata = NULL;
104 counted_svg_image_ = copy_source->counted_svg_image_;
105 counted_svg_image_->ref_count++;
106 } else {
107 counted_svg_image_ = new counted_NSVGimage;
108 counted_svg_image_->svg_image = NULL;
109 counted_svg_image_->ref_count = 1;
110 }
111 char *filedata = NULL;
112 to_desaturate_ = false;
113 average_weight_ = 1;
114 proportional = true;
115 if (filename) {
116 #if defined(HAVE_LIBZ)
117 filedata = svg_inflate(filename);
118 #else
119 FILE *fp = fl_fopen(filename, "rb");
120 if (fp) {
121 fseek(fp, 0, SEEK_END);
122 long size = ftell(fp);
123 fseek(fp, 0, SEEK_SET);
124 filedata = (char*)malloc(size+1);
125 if (filedata) {
126 if (fread(filedata, 1, size, fp) == size) {
127 filedata[size] = '\0';
128 } else {
129 free(filedata);
130 filedata = NULL;
131 }
132 }
133 fclose(fp);
134 }
135 #endif // HAVE_LIBZ
136 if (!filedata) ld(ERR_FILE_ACCESS);
137 } else {
138 // XXX: Make internal copy -- nsvgParse() modifies filedata during parsing (!)
139 filedata = in_filedata ? strdup(in_filedata) : NULL;
140 }
141 if (filedata) {
142 counted_svg_image_->svg_image = nsvgParse(filedata, "px", 96);
143 free(filedata); // made with svg_inflate|malloc|strdup
144 if (counted_svg_image_->svg_image->width == 0 || counted_svg_image_->svg_image->height == 0) {
145 d(-1);
146 ld(ERR_FORMAT);
147 } else {
148 w(counted_svg_image_->svg_image->width + 0.5);
149 h(counted_svg_image_->svg_image->height + 0.5);
150 }
151 } else if (copy_source) {
152 w(copy_source->w());
153 h(copy_source->h());
154 }
155 rasterized_ = false;
156 }
157
158
159 void Fl_SVG_Image::rasterize_(int W, int H) {
160 static NSVGrasterizer *rasterizer = nsvgCreateRasterizer();
161 double fx, fy;
162 if (proportional) {
163 fx = svg_scaling_(W, H);
164 fy = fx;
165 } else {
166 fx = (double)W / counted_svg_image_->svg_image->width;
167 fy = (double)H / counted_svg_image_->svg_image->height;
168 }
169 array = new uchar[W*H*4];
170 nsvgAltRasterize(rasterizer, counted_svg_image_->svg_image, 0, 0, fx, fy, (uchar* )array, W, H, W*4);
171 alloc_array = 1;
172 data((const char * const *)&array, 1);
173 d(4);
174 if (to_desaturate_) Fl_RGB_Image::desaturate();
175 if (average_weight_ < 1) Fl_RGB_Image::color_average(average_color_, average_weight_);
176 rasterized_ = true;
177 raster_w_ = W;
178 raster_h_ = H;
179 //printf("rasterize to %dx%d\n",W, H);
180 }
181
182
183 Fl_Image *Fl_SVG_Image::copy(int W, int H) {
184 Fl_SVG_Image *svg2 = new Fl_SVG_Image(this);
185 svg2->to_desaturate_ = to_desaturate_;
186 svg2->average_weight_ = average_weight_;
187 svg2->average_color_ = average_color_;
188 svg2->proportional = proportional;
189 svg2->w(W); svg2->h(H);
190 return svg2;
191 }
192
193
194 /** Have the svg data (re-)rasterized using the given width and height values.
195 By default, the resulting image w() and h() will preserve the width/height ratio
196 of the SVG data.
197 If \ref proportional was set to \c false, the image is rasterized to the given \c width
198 and \c height values.*/
199 void Fl_SVG_Image::resize(int width, int height) {
200 if (ld() < 0) {
201 return;
202 }
203 int w1 = width, h1 = height;
204 if (proportional) {
205 float f = svg_scaling_(width, height);
206 w1 = int( int(counted_svg_image_->svg_image->width+0.5)*f + 0.5 );
207 h1 = int( int(counted_svg_image_->svg_image->height+0.5)*f + 0.5 );
208 }
209 w(w1); h(h1);
210 if (rasterized_ && w1 == raster_w_ && h1 == raster_h_) return;
211 if (array) {
212 delete[] array;
213 array = NULL;
214 }
215 uncache();
216 rasterize_(w1, h1);
217 }
218
219
220 void Fl_SVG_Image::draw(int X, int Y, int W, int H, int cx, int cy) {
221 float f = 1;
222 /*if (Fl_Surface_Device::surface() == Fl_Display_Device::display_device()) {
223 f = Fl::screen_driver()->retina_factor() * fl_graphics_driver->scale();
224 }*/
225 int w1 = w(), h1 = h();
226 /* When f > 1, there may be several pixels per drawing unit in an area
227 of size w() x h() of the display. This occurs, e.g., with Apple retina displays
228 and when the display is rescaled.
229 The SVG is rasterized to the area dimension in pixels. The image is then drawn
230 scaled to its size expressed in drawing units. With this procedure,
231 the SVG image is drawn using the full resolution of the display.
232 */
233 resize(f*w(), f*h());
234 if (f == 1) {
235 Fl_RGB_Image::draw(X, Y, W, H, cx, cy);
236 } else {
237 bool need_clip = (cx || cy || W != w1 || H != h1);
238 if (need_clip) fl_push_clip(X, Y, W, H);
239 fl_graphics_driver->draw_scaled(this, X-cx, Y-cy, w1, h1);
240 if (need_clip) fl_pop_clip();
241 w(w1); h(h1); // restore the previous image size
242 }
243 }
244
245
246 void Fl_SVG_Image::desaturate() {
247 to_desaturate_ = true;
248 Fl_RGB_Image::desaturate();
249 }
250
251
252 void Fl_SVG_Image::color_average(Fl_Color c, float i) {
253 average_color_ = c;
254 average_weight_ = i;
255 Fl_RGB_Image::color_average(c, i);
256 }
257
258
259 int Fl_SVG_Image::draw_scaled(int X, int Y, int W, int H) {
260 w(W);
261 h(H);
262 draw(X, Y, W, H, 0, 0);
263 return 1;
264 }
265
266 #endif // 100*FL_MAJOR_VERSION + 10*FL_MINOR_VERSION < 140