]>
gitweb.michael.orlitzky.com - libsvgtiny.git/blob - examples/svgtiny_display_x11.c
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>
9 * This example loads an SVG using libsvgtiny and then displays it in an X11
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
17 * gcc -g -W -Wall -o svgtiny_display_x11 svgtiny_display_x11.c \
18 * `pkg-config --cflags --libs libsvgtiny cairo` -lX11
28 #include <sys/types.h>
32 #include <X11/keysym.h>
34 #include <cairo-xlib.h>
38 struct svgtiny_diagram
*diagram
;
40 Window diagram_window
;
41 Atom wm_protocols_atom
, wm_delete_window_atom
;
49 void update_window_title(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
);
60 int main(int argc
, char *argv
[])
70 fprintf(stderr
, "Usage: %s FILE\n", argv
[0]);
75 /* load file into memory buffer */
76 fd
= fopen(svg_path
, "rb");
82 if (stat(svg_path
, &sb
)) {
88 buffer
= malloc(size
);
90 fprintf(stderr
, "Unable to allocate %lld bytes\n",
95 n
= fread(buffer
, 1, size
, fd
);
103 /* create svgtiny object */
104 diagram
= svgtiny_create();
106 fprintf(stderr
, "svgtiny_create failed\n");
111 code
= svgtiny_parse(diagram
, buffer
, size
, svg_path
, 1000, 1000);
112 if (code
!= svgtiny_OK
) {
113 fprintf(stderr
, "svgtiny_parse failed: ");
115 case svgtiny_OUT_OF_MEMORY
:
116 fprintf(stderr
, "svgtiny_OUT_OF_MEMORY");
118 case svgtiny_LIBDOM_ERROR
:
119 fprintf(stderr
, "svgtiny_LIBDOM_ERROR");
121 case svgtiny_NOT_SVG
:
122 fprintf(stderr
, "svgtiny_NOT_SVG");
124 case svgtiny_SVG_ERROR
:
125 fprintf(stderr
, "svgtiny_SVG_ERROR: line %i: %s",
127 diagram
->error_message
);
130 fprintf(stderr
, "unknown svgtiny_code %i", code
);
133 fprintf(stderr
, "\n");
138 /*printf("viewbox 0 0 %u %u\n",
139 diagram->width, diagram->height);*/
148 svgtiny_free(diagram
);
155 * Initialize X11 interface.
159 display
= XOpenDisplay(NULL
);
161 die("XOpenDisplay failed: is DISPLAY set?");
163 diagram_window
= XCreateSimpleWindow(display
,
164 DefaultRootWindow(display
),
165 0, 0, diagram
->width
, diagram
->height
, 0, 0, 0);
167 update_window_title();
169 XMapWindow(display
, diagram_window
);
170 XSelectInput(display
, diagram_window
,
174 StructureNotifyMask
);
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);
183 * Free X11 interface.
187 XCloseDisplay(display
);
192 * Update window title to show current state.
194 void update_window_title(void)
200 svg_path_copy
= strdup(svg_path
);
201 if (!svg_path_copy
) {
202 fprintf(stderr
, "out of memory\n");
206 base_name
= basename(svg_path_copy
);
208 snprintf(title
, sizeof title
, "%s (%i%%) - svgtiny",
209 base_name
, (int) roundf(scale
* 100.0));
211 XStoreName(display
, diagram_window
, title
);
218 * Handle an X11 event.
223 XNextEvent(display
, &event
);
225 switch (event
.type
) {
227 if (event
.xkey
.window
== diagram_window
) {
228 event_diagram_key_press(&event
.xkey
);
232 if (event
.xexpose
.window
== diagram_window
) {
233 event_diagram_expose(&event
.xexpose
);
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
)
244 /*printf("unknown event %i\n", event.type);*/
251 * Handle an X11 KeyPress event in the diagram window.
253 void event_diagram_key_press(XKeyEvent
*key_event
)
256 float new_scale
= scale
;
257 unsigned int width
, height
;
259 key_sym
= XLookupKeysym(key_event
, 0);
295 else if (5 < new_scale
)
298 if (new_scale
== scale
)
302 width
= diagram
->width
* scale
;
303 height
= diagram
->height
* scale
;
308 XResizeWindow(display
, diagram_window
, width
, height
);
309 XClearArea(display
, diagram_window
, 0, 0, 0, 0, True
);
310 update_window_title();
315 * Handle an X11 Expose event of the diagram window.
317 void event_diagram_expose(const XExposeEvent
*expose_event
)
319 cairo_surface_t
*surface
;
321 cairo_status_t status
;
324 if (expose_event
->count
!= 0)
327 surface
= cairo_xlib_surface_create(display
, diagram_window
,
328 DefaultVisual(display
, DefaultScreen(display
)),
329 diagram
->width
* scale
, diagram
->height
* scale
);
331 fprintf(stderr
, "cairo_xlib_surface_create failed\n");
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
));
341 cairo_surface_destroy(surface
);
345 cairo_set_source_rgb(cr
, 1, 1, 1);
348 for (i
= 0; i
!= diagram
->shape_count
; i
++) {
349 if (diagram
->shape
[i
].path
) {
350 render_path(cr
, scale
, &diagram
->shape
[i
]);
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);
358 scale
* diagram
->shape
[i
].text_x
,
359 scale
* diagram
->shape
[i
].text_y
);
360 cairo_show_text(cr
, diagram
->shape
[i
].text
);
364 status
= cairo_status(cr
);
365 if (status
!= CAIRO_STATUS_SUCCESS
) {
366 fprintf(stderr
, "cairo error: %s\n",
367 cairo_status_to_string(status
));
369 cairo_surface_destroy(surface
);
374 cairo_surface_destroy(surface
);
379 * Render an svgtiny path using cairo.
381 void render_path(cairo_t
*cr
, float scale
, struct svgtiny_shape
*path
)
386 for (j
= 0; j
!= path
->path_length
; ) {
387 switch ((int) path
->path
[j
]) {
388 case svgtiny_PATH_MOVE
:
390 scale
* path
->path
[j
+ 1],
391 scale
* path
->path
[j
+ 2]);
394 case svgtiny_PATH_CLOSE
:
395 cairo_close_path(cr
);
398 case svgtiny_PATH_LINE
:
400 scale
* path
->path
[j
+ 1],
401 scale
* path
->path
[j
+ 2]);
404 case svgtiny_PATH_BEZIER
:
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]);
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
);
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
);
438 * Exit with fatal error.
440 void die(const char *message
)
442 fprintf(stderr
, "%s\n", message
);