/
svgViewer.c
309 lines (278 loc) · 8.21 KB
/
svgViewer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
*
* Copyright © 2005,2006,2007 Red Hat, Inc.
* Copyright © 2011 C. A. Fitzgerald <courtney.adam.fitzgerald@gmail.com>
* Copyright © 2012 Michele Bini <michele.bini@gmail.com>
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* Authors:
* - Kristian Høgsberg <krh@redhat.com>
* - Carl Worth <cworth@redhat.com>
* - Behdad Esfahbod <besfahbo@redhat.com>
* - C. A. Fitzgerald <courtney.adam.fitzgerald@gmail.com> (http://github.com/riotopsys)
* - Michele Bini <michele.bini@gmail.com> (http;//github.com/rev22)
*/
#include <stdio.h>
#include <stdlib.h>
#include <librsvg/rsvg.h>
#include <librsvg/rsvg-cairo.h>
#include <getopt.h>
#include <errno.h>
#include "./cairosdl/cairosdl.h"
#define PIXELS_PER_POINT 1
static int verbose_flag = 0;
static int debug_flag = 0;
static int stretch_flag = 0;
static int fullscreen_flag = 0;
static const struct option long_options[] = {
/* These options set a flag. */
{"verbose", no_argument, &verbose_flag, 1},
{"debug", no_argument, &debug_flag, 1},
{"stretch", no_argument, &stretch_flag, 1},
{"fullscreen", no_argument, &fullscreen_flag, 1},
{"zoom", required_argument, NULL, 'z'},
{0, 0, 0, 0}
};
#define FAIL(x...) do { fprintf(stderr, x); exit(EXIT_FAILURE); } while (0)
#define DEBUG(x...) do { if (debug_flag) fprintf(stderr, x); } while (0)
struct svgviewer_view {
double x, y;
double zoom;
unsigned int pixel_width, pixel_height;
};
RsvgDimensionData dim;
void view_transform(cairo_t *c, struct svgviewer_view *v) {
double pw = (double)v->pixel_width;
double ph = (double)v->pixel_height;
double iw = (double)dim.width;
double ih = (double)dim.height;
double cx = iw * 0.5;
double cy = ih * 0.5;
double zx = pw / iw;
double zy = ph / ih;
cx += v->x * dim.width;
cy += v->y * dim.height;
//cairo_translate(c, - pw * 0.5, - ph * 0.5);
if (stretch_flag) {
cairo_scale(c, zx *= v->zoom, zy *= v->zoom);
cx *= zx; cy *= zy;
cx = (pw * 0.5 - cx) / zx;
cy = (ph * 0.5 - cy) / zy;
} else {
double z = (zx < zy) ? zx : zy;
z *= v->zoom;
cairo_scale(c, z, z);
cx *= z; cy *= z;
cx = (pw * 0.5 - cx) / z;
cy = (ph * 0.5 - cy) / z;
}
cairo_translate(c, cx, cy);
}
int main(int argc, char *argv[]) {
SDL_Surface *screen;
GError *error = NULL;
RsvgHandle *handle;
struct svgviewer_view vw;
char *filename;
//const char *output_filename = argv[2];
cairo_surface_t *surface;
cairo_t *cr;
cairo_t *cr2;
cairo_status_t status;
int c;
int rerender = 0;
SDL_VideoInfo* info;
vw.zoom = 1;
/* Process options */
while (1) {
int option_index = 0;
c = getopt_long (argc, argv, "d:v:",
long_options, &option_index);
/* Detect the end of the options. */
if (c == -1) break;
switch (c) {
case 0:
break;
case 'z':
errno = 0;
vw.zoom = strtod(optarg, NULL);
DEBUG("Zoom set to %g\n", vw.zoom);
if (errno) FAIL("Usage: -z or --zoom requires a floating point argument");
break;
default:
abort();
};
}
if (argc != optind+1) FAIL("Usage: %s OPTIONS input_file.svg\n", argv[0]);
filename = argv[optind];
g_type_init();
rsvg_set_default_dpi(72.0);
handle = rsvg_handle_new_from_file(filename, &error);
if (error != NULL)
FAIL(error->message);
rsvg_handle_get_dimensions(handle, &dim);
vw.x = 0; vw.y = 0;
vw.zoom = 1;
/* Initialize SDL */
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0) {
fprintf(stderr, "Unable to initialize SDL: %s\n",
SDL_GetError());
exit(1);
}
int videomodeflags;
if (fullscreen_flag) {
screen = SDL_SetVideoMode(0, 0, 32,
videomodeflags =
SDL_FULLSCREEN | SDL_SWSURFACE | SDL_RESIZABLE);
} else {
const SDL_VideoInfo* info = SDL_GetVideoInfo();
if ((dim.width >= info->current_w) ||
(dim.height >= info->current_h)) {
screen = SDL_SetVideoMode(0, 0, 32,
videomodeflags =
SDL_SWSURFACE | SDL_RESIZABLE);
} else {
screen = SDL_SetVideoMode(dim.width, dim.height, 32,
videomodeflags =
SDL_SWSURFACE | SDL_RESIZABLE);
}
}
if (screen == NULL) {
fprintf(stderr, "Unable to set video mode: %s\n", SDL_GetError());
exit(1);
}
vw.pixel_width = screen->w;
vw.pixel_height = screen->h;
surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, vw.pixel_width, vw.pixel_height);
cr2 = cairo_create (surface);
//cairo_translate(cr2, ((double)dim.width)/-2, ((double)dim.height)/-2);
//surface = cairo_pdf_surface_create (output_filename, width, height);
// SDL_LockSurface(screen);
//cairo_scale (cr, screen->w, screen->h);
cairo_save(cr2);
view_transform(cr2, &vw);
rsvg_handle_render_cairo(handle, cr2);
status = cairo_status(cr2);
if (status) FAIL(cairo_status_to_string(status));
cairo_restore(cr2);
cr = cairosdl_create(screen);
// cairo_set_source_rgb(cr, 1, 1, 1);
// cairo_paint(cr);
cairo_save(cr);
//cairo_scale(cr, .5, .5);
//cairo_translate(cr, width/2, height/2 );
//cairo_rotate( cr, 3.14/2 );
//cairo_translate(cr, -width/2, -height/2 );
cairo_set_source_surface (cr, surface, 0, 0);
cairo_paint(cr);
cairo_restore(cr);
status = cairo_status(cr);
if (status)
FAIL(cairo_status_to_string(status));
// SDL_UnlockSurface(screen);
SDL_UpdateRect(screen, 0, 0, 0, 0);
{
SDL_Event event;
while (SDL_WaitEvent(&event)) {
switch (event.type) {
case SDL_KEYDOWN:
switch (event.key.keysym.sym) {
case SDLK_UP:
vw.y -= 0.1 / vw.zoom;
rerender = 1;
break;
case SDLK_DOWN:
vw.y += 0.1 / vw.zoom;
rerender = 1;
break;
case SDLK_LEFT:
vw.x -= 0.1 / vw.zoom;
rerender = 1;
break;
case SDLK_RIGHT:
vw.x += 0.1 / vw.zoom;
rerender = 1;
break;
case SDLK_a:
vw.zoom *= 1.025;
rerender = 1;
break;
case SDLK_z:
vw.zoom /= 1.025;
rerender = 1;
break;
case SDLK_ESCAPE:
goto exit;
default:
break;
}
break;
case SDL_VIDEORESIZE:
{
vw.pixel_width = event.resize.w;
vw.pixel_height = event.resize.h;
cairo_destroy(cr2);
cairo_surface_destroy(surface);
surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, vw.pixel_width, vw.pixel_height);
cr2 = cairo_create(surface);
//cairo_translate(cr2, ((double)dim.width)/-2, ((double)dim.height)/-2);
cairo_save(cr2);
view_transform(cr2, &vw);
rsvg_handle_render_cairo(handle, cr2);
cairo_restore(cr2);
screen = SDL_SetVideoMode(event.resize.w, event.resize.h, 32, videomodeflags);
cairosdl_destroy(cr);
cr = cairosdl_create(screen);
cairo_save(cr);
cairo_set_source_surface(cr, surface, 0, 0);
cairo_paint(cr);
cairo_restore(cr);
};
break;
case SDL_QUIT:
goto exit;
default:
break;
}
if (rerender) {
cairo_save(cr2);
view_transform(cr2, &vw);
rsvg_handle_render_cairo(handle, cr2);
status = cairo_status(cr2);
if (status) FAIL(cairo_status_to_string(status));
cairo_restore(cr2);
cairo_set_source_surface(cr, surface, 0, 0);
cairo_paint(cr);
rerender = 0;
}
SDL_UpdateRect(screen, 0, 0, 0, 0);
};
};
exit:
cairosdl_destroy(cr);
if (status)
FAIL(cairo_status_to_string(status));
cairo_destroy (cr2);
cairo_surface_destroy(surface);
SDL_Quit();
exit(EXIT_SUCCESS);
}