]> gitweb.michael.orlitzky.com - libsvgtiny.git/blob - examples/svgtiny_display_x11.c
6758befb1ce8c9bf612c4f96f205c1f8a5265c16
[libsvgtiny.git] / examples / svgtiny_display_x11.c
1 /*
2 * This file is part of Libsvgtiny
3 * Licensed under the MIT License,
4 * http://opensource.org/licenses/mit-license.php
5 * Copyright 2009-2010 James Bursa <james@semichrome.net>
6 */
7
8 /*
9 * This example loads an SVG using libsvgtiny and then displays it in an X11
10 * window using cairo.
11 *
12 * Functions of interest for libsvgtiny use are:
13 * main() - loads an SVG using svgtiny_create() and svgtiny_parse()
14 * event_diagram_expose() - renders the SVG by stepping through the shapes
15 *
16 * Compile using:
17 * gcc -g -W -Wall -o svgtiny_display_x11 svgtiny_display_x11.c \
18 * `pkg-config --cflags --libs libsvgtiny cairo` -lX11
19 */
20
21
22 #include <libgen.h>
23 #include <math.h>
24 #include <stdbool.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <unistd.h>
31 #include <X11/Xlib.h>
32 #include <X11/keysym.h>
33 #include <cairo.h>
34 #include <cairo-xlib.h>
35 #include "svgtiny.h"
36
37
38 struct svgtiny_diagram *diagram;
39 Display *display;
40 Window diagram_window;
41 Atom wm_protocols_atom, wm_delete_window_atom;
42 char *svg_path;
43 float scale = 1.0;
44 bool quit = false;
45
46
47 void gui_init(void);
48 void gui_quit(void);
49 void update_window_title(void);
50 void gui_poll(void);
51 void event_diagram_key_press(XKeyEvent *key_event);
52 void event_diagram_expose(const XExposeEvent *expose_event);
53 void render_path(cairo_t *cr, float scale, struct svgtiny_shape *path);
54 void die(const char *message);
55
56
57 /**
58 * Main program.
59 */
60 int main(int argc, char *argv[])
61 {
62 FILE *fd;
63 struct stat sb;
64 char *buffer;
65 size_t size;
66 size_t n;
67 svgtiny_code code;
68
69 if (argc != 2) {
70 fprintf(stderr, "Usage: %s FILE\n", argv[0]);
71 return 1;
72 }
73 svg_path = argv[1];
74
75 /* load file into memory buffer */
76 fd = fopen(svg_path, "rb");
77 if (!fd) {
78 perror(svg_path);
79 return 1;
80 }
81
82 if (stat(svg_path, &sb)) {
83 perror(svg_path);
84 return 1;
85 }
86 size = sb.st_size;
87
88 buffer = malloc(size);
89 if (!buffer) {
90 fprintf(stderr, "Unable to allocate %lld bytes\n",
91 (long long) size);
92 return 1;
93 }
94
95 n = fread(buffer, 1, size, fd);
96 if (n != size) {
97 perror(svg_path);
98 return 1;
99 }
100
101 fclose(fd);
102
103 /* create svgtiny object */
104 diagram = svgtiny_create();
105 if (!diagram) {
106 fprintf(stderr, "svgtiny_create failed\n");
107 return 1;
108 }
109
110 /* parse */
111 code = svgtiny_parse(diagram, buffer, size, svg_path, 1000, 1000);
112 if (code != svgtiny_OK) {
113 fprintf(stderr, "svgtiny_parse failed: ");
114 switch (code) {
115 case svgtiny_OUT_OF_MEMORY:
116 fprintf(stderr, "svgtiny_OUT_OF_MEMORY");
117 break;
118 case svgtiny_LIBDOM_ERROR:
119 fprintf(stderr, "svgtiny_LIBDOM_ERROR");
120 break;
121 case svgtiny_NOT_SVG:
122 fprintf(stderr, "svgtiny_NOT_SVG");
123 break;
124 case svgtiny_SVG_ERROR:
125 fprintf(stderr, "svgtiny_SVG_ERROR: line %i: %s",
126 diagram->error_line,
127 diagram->error_message);
128 break;
129 default:
130 fprintf(stderr, "unknown svgtiny_code %i", code);
131 break;
132 }
133 fprintf(stderr, "\n");
134 }
135
136 free(buffer);
137
138 /*printf("viewbox 0 0 %u %u\n",
139 diagram->width, diagram->height);*/
140
141 gui_init();
142
143 while (!quit) {
144 gui_poll();
145 }
146
147 gui_quit();
148 svgtiny_free(diagram);
149
150 return 0;
151 }
152
153
154 /**
155 * Initialize X11 interface.
156 */
157 void gui_init(void)
158 {
159 display = XOpenDisplay(NULL);
160 if (!display)
161 die("XOpenDisplay failed: is DISPLAY set?");
162
163 diagram_window = XCreateSimpleWindow(display,
164 DefaultRootWindow(display),
165 0, 0, diagram->width, diagram->height, 0, 0, 0);
166
167 update_window_title();
168
169 XMapWindow(display, diagram_window);
170 XSelectInput(display, diagram_window,
171 KeyPressMask |
172 ButtonPressMask |
173 ExposureMask |
174 StructureNotifyMask);
175
176 wm_protocols_atom = XInternAtom(display, "WM_PROTOCOLS", False);
177 wm_delete_window_atom = XInternAtom(display, "WM_DELETE_WINDOW", False);
178 XSetWMProtocols(display, diagram_window, &wm_delete_window_atom, 1);
179 }
180
181
182 /**
183 * Free X11 interface.
184 */
185 void gui_quit(void)
186 {
187 XCloseDisplay(display);
188 }
189
190
191 /**
192 * Update window title to show current state.
193 */
194 void update_window_title(void)
195 {
196 char title[100];
197 char *svg_path_copy;
198 char *base_name;
199
200 svg_path_copy = strdup(svg_path);
201 if (!svg_path_copy) {
202 fprintf(stderr, "out of memory\n");
203 return;
204 }
205
206 base_name = basename(svg_path_copy);
207
208 snprintf(title, sizeof title, "%s (%i%%) - svgtiny",
209 base_name, (int) roundf(scale * 100.0));
210
211 XStoreName(display, diagram_window, title);
212
213 free(svg_path_copy);
214 }
215
216
217 /**
218 * Handle an X11 event.
219 */
220 void gui_poll(void)
221 {
222 XEvent event;
223 XNextEvent(display, &event);
224
225 switch (event.type) {
226 case KeyPress:
227 if (event.xkey.window == diagram_window) {
228 event_diagram_key_press(&event.xkey);
229 }
230 break;
231 case Expose:
232 if (event.xexpose.window == diagram_window) {
233 event_diagram_expose(&event.xexpose);
234 }
235 break;
236 case ClientMessage:
237 if (event.xclient.message_type == wm_protocols_atom &&
238 event.xclient.format == 32 &&
239 (Atom) event.xclient.data.l[0] ==
240 wm_delete_window_atom)
241 quit = true;
242 break;
243 default:
244 /*printf("unknown event %i\n", event.type);*/
245 break;
246 }
247 }
248
249
250 /**
251 * Handle an X11 KeyPress event in the diagram window.
252 */
253 void event_diagram_key_press(XKeyEvent *key_event)
254 {
255 KeySym key_sym;
256 float new_scale = scale;
257 unsigned int width, height;
258
259 key_sym = XLookupKeysym(key_event, 0);
260
261 switch (key_sym) {
262 case XK_q:
263 case XK_Escape:
264 quit = true;
265 break;
266
267 case XK_minus:
268 case XK_KP_Subtract:
269 new_scale -= 0.1;
270 break;
271
272 case XK_equal:
273 case XK_plus:
274 case XK_KP_Add:
275 new_scale += 0.1;
276 break;
277
278 case XK_1:
279 case XK_KP_Multiply:
280 case XK_KP_1:
281 new_scale = 1;
282 break;
283
284 case XK_2:
285 case XK_KP_2:
286 new_scale = 2;
287 break;
288
289 default:
290 break;
291 }
292
293 if (new_scale < 0.1)
294 new_scale = 0.1;
295 else if (5 < new_scale)
296 new_scale = 5;
297
298 if (new_scale == scale)
299 return;
300
301 scale = new_scale;
302 width = diagram->width * scale;
303 height = diagram->height * scale;
304 if (width < 400)
305 width = 400;
306 if (height < 400)
307 height = 400;
308 XResizeWindow(display, diagram_window, width, height);
309 XClearArea(display, diagram_window, 0, 0, 0, 0, True);
310 update_window_title();
311 }
312
313
314 /**
315 * Handle an X11 Expose event of the diagram window.
316 */
317 void event_diagram_expose(const XExposeEvent *expose_event)
318 {
319 cairo_surface_t *surface;
320 cairo_t *cr;
321 cairo_status_t status;
322 unsigned int i;
323
324 if (expose_event->count != 0)
325 return;
326
327 surface = cairo_xlib_surface_create(display, diagram_window,
328 DefaultVisual(display, DefaultScreen(display)),
329 diagram->width * scale, diagram->height * scale);
330 if (!surface) {
331 fprintf(stderr, "cairo_xlib_surface_create failed\n");
332 return;
333 }
334
335 cr = cairo_create(surface);
336 status = cairo_status(cr);
337 if (status != CAIRO_STATUS_SUCCESS) {
338 fprintf(stderr, "cairo_create failed: %s\n",
339 cairo_status_to_string(status));
340 cairo_destroy(cr);
341 cairo_surface_destroy(surface);
342 return;
343 }
344
345 cairo_set_source_rgb(cr, 1, 1, 1);
346 cairo_paint(cr);
347
348 for (i = 0; i != diagram->shape_count; i++) {
349 if (diagram->shape[i].path) {
350 render_path(cr, scale, &diagram->shape[i]);
351
352 } else if (diagram->shape[i].text) {
353 cairo_set_source_rgb(cr,
354 svgtiny_RED(diagram->shape[i].stroke) / 255.0,
355 svgtiny_GREEN(diagram->shape[i].stroke) / 255.0,
356 svgtiny_BLUE(diagram->shape[i].stroke) / 255.0);
357 cairo_move_to(cr,
358 scale * diagram->shape[i].text_x,
359 scale * diagram->shape[i].text_y);
360 cairo_show_text(cr, diagram->shape[i].text);
361 }
362 }
363
364 status = cairo_status(cr);
365 if (status != CAIRO_STATUS_SUCCESS) {
366 fprintf(stderr, "cairo error: %s\n",
367 cairo_status_to_string(status));
368 cairo_destroy(cr);
369 cairo_surface_destroy(surface);
370 return;
371 }
372
373 cairo_destroy(cr);
374 cairo_surface_destroy(surface);
375 }
376
377
378 /**
379 * Render an svgtiny path using cairo.
380 */
381 void render_path(cairo_t *cr, float scale, struct svgtiny_shape *path)
382 {
383 unsigned int j;
384
385 cairo_new_path(cr);
386 for (j = 0; j != path->path_length; ) {
387 switch ((int) path->path[j]) {
388 case svgtiny_PATH_MOVE:
389 cairo_move_to(cr,
390 scale * path->path[j + 1],
391 scale * path->path[j + 2]);
392 j += 3;
393 break;
394 case svgtiny_PATH_CLOSE:
395 cairo_close_path(cr);
396 j += 1;
397 break;
398 case svgtiny_PATH_LINE:
399 cairo_line_to(cr,
400 scale * path->path[j + 1],
401 scale * path->path[j + 2]);
402 j += 3;
403 break;
404 case svgtiny_PATH_BEZIER:
405 cairo_curve_to(cr,
406 scale * path->path[j + 1],
407 scale * path->path[j + 2],
408 scale * path->path[j + 3],
409 scale * path->path[j + 4],
410 scale * path->path[j + 5],
411 scale * path->path[j + 6]);
412 j += 7;
413 break;
414 default:
415 printf("error ");
416 j += 1;
417 }
418 }
419 if (path->fill != svgtiny_TRANSPARENT) {
420 cairo_set_source_rgb(cr,
421 svgtiny_RED(path->fill) / 255.0,
422 svgtiny_GREEN(path->fill) / 255.0,
423 svgtiny_BLUE(path->fill) / 255.0);
424 cairo_fill_preserve(cr);
425 }
426 if (path->stroke != svgtiny_TRANSPARENT) {
427 cairo_set_source_rgb(cr,
428 svgtiny_RED(path->stroke) / 255.0,
429 svgtiny_GREEN(path->stroke) / 255.0,
430 svgtiny_BLUE(path->stroke) / 255.0);
431 cairo_set_line_width(cr, scale * path->stroke_width);
432 cairo_stroke_preserve(cr);
433 }
434 }
435
436
437 /**
438 * Exit with fatal error.
439 */
440 void die(const char *message)
441 {
442 fprintf(stderr, "%s\n", message);
443 exit(1);
444 }
445