10 #include <librsvg/rsvg.h>
11 #ifndef LIBRSVG_CHECK_VERSION
12 # include <librsvg/librsvg-features.h>
14 #if LIBRSVG_CHECK_VERSION(2,36,2)
16 # include <librsvg/rsvg-cairo.h>
19 #define DATA32 unsigned int
21 static RsvgHandle *rsvg = NULL;
23 static int height = 0;
24 static RsvgDimensionData dim;
26 static inline Eina_Bool evas_image_load_file_is_svg(const char *file)
28 int i, len = strlen(file);
29 Eina_Bool is_gz = EINA_FALSE;
31 for (i = len - 1; i > 0; i--)
37 else if (strcasecmp(file + i + 1, "gz") == 0)
44 if (i < 1) return EINA_FALSE;
46 if (i >= len) return EINA_FALSE;
47 if (strncasecmp(file + i, "svg", 3) != 0) return EINA_FALSE;
51 if (file[i] == '.') return EINA_TRUE;
52 else return EINA_FALSE;
56 if (file[i] == '\0') return EINA_TRUE;
57 else if (((file[i] == 'z') || (file[i] == 'Z')) && (!file[i + 1])) return EINA_TRUE;
58 else return EINA_FALSE;
63 _svg_init(const char *file)
66 # if !defined(GLIB_VERSION_2_36)
73 if (!evas_image_load_file_is_svg(file)) return 0;
75 rsvg = rsvg_handle_new_from_file(file, NULL);
86 rsvg_handle_close(rsvg, NULL);
89 // Maybe it's not crashing anymore, let's try it.
96 read_svg_header(int scale_down, double dpi, int size_w, int size_h)
98 rsvg_handle_set_dpi(rsvg, 75.0);
99 rsvg_handle_get_dimensions(rsvg, &dim);
103 if ((width < 1) || (height < 1)) return 0;
108 height /= scale_down;
112 width = (width * dpi) / 75;
113 height = (height * dpi) / 75;
115 else if (size_w > 0 && size_h > 0)
120 h = (size_w * height) / width;
124 w = (size_h * width) / height;
129 if (width < 1) width = 1;
130 if (height < 1) height = 1;
138 cairo_surface_t *surface;
141 shm_alloc(width * height * (sizeof(DATA32)));
142 if (!shm_addr) return 0;
144 memset(shm_addr, 0, width * height * sizeof (DATA32));
145 surface = cairo_image_surface_create_for_data((unsigned char *)shm_addr, CAIRO_FORMAT_ARGB32,
146 width, height, width * sizeof(DATA32));;
147 if (!surface) return 0;
149 cr = cairo_create(surface);
152 cairo_scale(cr, (double) width / dim.em, (double) height / dim.ex);
153 rsvg_handle_render_cairo(rsvg, cr);
154 cairo_surface_destroy(surface);
160 int main(int argc, char **argv)
167 int size_w = 0, size_h = 0;
169 if (argc < 2) return -1;
172 for (i = 2; i < argc; ++i)
174 if (!strcmp(argv[i], "-head"))
176 else if (!strcmp(argv[i], "-key"))
177 { // not used by svg loader
179 // const char *key = argv[i];
181 else if (!strcmp(argv[i], "-opt-scale-down-by"))
184 scale_down = atoi(argv[i]);
186 else if (!strcmp(argv[i], "-opt-dpi"))
189 dpi = ((double)atoi(argv[i])) / 1000.0;
191 else if (!strcmp(argv[i], "-opt-size"))
194 size_w = atoi(argv[i]);
196 size_h = atoi(argv[i]);
202 if (!_svg_init(file)) return -1;
203 if (!read_svg_header(scale_down, dpi, size_w, size_h)) return -1;
207 printf("size %d %d\n", width, height);
215 printf("size %d %d\n", width, height);
217 if (shm_fd >= 0) printf("shmfile %s\n", shmfile);
221 if (fwrite(shm_addr, width * height * sizeof(DATA32), 1, stdout) != 1)
223 // nothing much to do, the receiver will simply ignore the
224 // data as it's too short
225 //D("fwrite failed (%d bytes): %m\n", width * height * sizeof(DATA32));