]> gitweb.michael.orlitzky.com - libsvgtiny.git/blob - test/decode_svg.c
Fix reallocation of path buffer length to cope with differng path lengths
[libsvgtiny.git] / test / decode_svg.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 2008 James Bursa <james@semichrome.net>
6 */
7
8 #include <stdio.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <unistd.h>
12 #include <stdlib.h>
13 #include "svgtiny.h"
14
15
16 int main(int argc, char *argv[])
17 {
18 FILE *fd;
19 float scale = 1.0;
20 struct stat sb;
21 char *buffer;
22 size_t size;
23 size_t n;
24 struct svgtiny_diagram *diagram;
25 svgtiny_code code;
26
27 if (argc != 2 && argc != 3) {
28 fprintf(stderr, "Usage: %s FILE [SCALE]\n", argv[0]);
29 return 1;
30 }
31
32 /* load file into memory buffer */
33 fd = fopen(argv[1], "rb");
34 if (!fd) {
35 perror(argv[1]);
36 return 1;
37 }
38
39 if (stat(argv[1], &sb)) {
40 perror(argv[1]);
41 return 1;
42 }
43 size = sb.st_size;
44
45 buffer = malloc(size);
46 if (!buffer) {
47 fprintf(stderr, "Unable to allocate %lld bytes\n",
48 (long long) size);
49 return 1;
50 }
51
52 n = fread(buffer, 1, size, fd);
53 if (n != size) {
54 perror(argv[1]);
55 return 1;
56 }
57
58 fclose(fd);
59
60 /* read scale argument */
61 if (argc == 3) {
62 scale = atof(argv[2]);
63 if (scale == 0)
64 scale = 1.0;
65 }
66
67 /* create svgtiny object */
68 diagram = svgtiny_create();
69 if (!diagram) {
70 fprintf(stderr, "svgtiny_create failed\n");
71 return 1;
72 }
73
74 /* parse */
75 code = svgtiny_parse(diagram, buffer, size, argv[1], 1000, 1000);
76 if (code != svgtiny_OK) {
77 fprintf(stderr, "svgtiny_parse failed: ");
78 switch (code) {
79 case svgtiny_OUT_OF_MEMORY:
80 fprintf(stderr, "svgtiny_OUT_OF_MEMORY");
81 break;
82 case svgtiny_LIBDOM_ERROR:
83 fprintf(stderr, "svgtiny_LIBDOM_ERROR");
84 break;
85 case svgtiny_NOT_SVG:
86 fprintf(stderr, "svgtiny_NOT_SVG");
87 break;
88 case svgtiny_SVG_ERROR:
89 fprintf(stderr, "svgtiny_SVG_ERROR: line %i: %s",
90 diagram->error_line,
91 diagram->error_message);
92 break;
93 default:
94 fprintf(stderr, "unknown svgtiny_code %i", code);
95 break;
96 }
97 fprintf(stderr, "\n");
98 }
99
100 free(buffer);
101
102 printf("viewbox 0 0 %g %g\n",
103 scale * diagram->width, scale * diagram->height);
104
105 for (unsigned int i = 0; i != diagram->shape_count; i++) {
106 if (diagram->shape[i].fill == svgtiny_TRANSPARENT)
107 printf("fill none ");
108 else
109 printf("fill #%.6x ", diagram->shape[i].fill);
110 if (diagram->shape[i].stroke == svgtiny_TRANSPARENT)
111 printf("stroke none ");
112 else
113 printf("stroke #%.6x ", diagram->shape[i].stroke);
114 printf("stroke-width %g ",
115 scale * diagram->shape[i].stroke_width);
116 if (diagram->shape[i].path) {
117 printf("path '");
118 for (unsigned int j = 0;
119 j != diagram->shape[i].path_length; ) {
120 switch ((int) diagram->shape[i].path[j]) {
121 case svgtiny_PATH_MOVE:
122 printf("M %g %g ",
123 scale * diagram->shape[i].path[j + 1],
124 scale * diagram->shape[i].path[j + 2]);
125 j += 3;
126 break;
127 case svgtiny_PATH_CLOSE:
128 printf("Z ");
129 j += 1;
130 break;
131 case svgtiny_PATH_LINE:
132 printf("L %g %g ",
133 scale * diagram->shape[i].path[j + 1],
134 scale * diagram->shape[i].path[j + 2]);
135 j += 3;
136 break;
137 case svgtiny_PATH_BEZIER:
138 printf("C %g %g %g %g %g %g ",
139 scale * diagram->shape[i].path[j + 1],
140 scale * diagram->shape[i].path[j + 2],
141 scale * diagram->shape[i].path[j + 3],
142 scale * diagram->shape[i].path[j + 4],
143 scale * diagram->shape[i].path[j + 5],
144 scale * diagram->shape[i].path[j + 6]);
145 j += 7;
146 break;
147 default:
148 printf("error ");
149 j += 1;
150 }
151 }
152 printf("' ");
153 } else if (diagram->shape[i].text) {
154 printf("text %g %g '%s' ",
155 scale * diagram->shape[i].text_x,
156 scale * diagram->shape[i].text_y,
157 diagram->shape[i].text);
158 }
159 printf("\n");
160 }
161
162 svgtiny_free(diagram);
163
164 return 0;
165 }
166