summaryrefslogtreecommitdiff
path: root/test/svgtiny_test.c
blob: 1b507fddfda7c2a811f9f379556dc26623debef0 (plain)
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
/*
 * This file is part of Libsvgtiny
 * Licensed under the MIT License,
 *                http://opensource.org/licenses/mit-license.php
 * Copyright 2008 James Bursa <james@semichrome.net>
 */

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include "svgtiny.h"


int main(int argc, char *argv[])
{
	FILE *fd;
	float scale = 1.0;
	struct stat sb;
	char *buffer;
	size_t size;
	size_t n;
	struct svgtiny_diagram *diagram;
	svgtiny_code code;

	if (argc != 2 && argc != 3) {
		fprintf(stderr, "Usage: %s FILE [SCALE]\n", argv[0]);
		return 1;
	}

	/* load file into memory buffer */
	fd = fopen(argv[1], "rb");
	if (!fd) {
		perror(argv[1]);
		return 1;
	}

	if (stat(argv[1], &sb)) {
		perror(argv[1]);
		return 1;
	}
	size = sb.st_size;

	buffer = malloc(size);
	if (!buffer) {
		fprintf(stderr, "Unable to allocate %lld bytes\n",
				(long long) size);
		return 1;
	}

	n = fread(buffer, 1, size, fd);
	if (n != size) {
		perror(argv[1]);
		return 1;
	}

	fclose(fd);

	/* read scale argument */
	if (argc == 3) {
		scale = atof(argv[2]);
		if (scale == 0)
			scale = 1.0;
	}

	/* create svgtiny object */
	diagram = svgtiny_create();
	if (!diagram) {
		fprintf(stderr, "svgtiny_create failed\n");
		return 1;
	}

	/* parse */
	code = svgtiny_parse(diagram, buffer, size, argv[1], 1000, 1000);
	if (code != svgtiny_OK) {
		fprintf(stderr, "svgtiny_parse failed: ");
		switch (code) {
		case svgtiny_OUT_OF_MEMORY:
			fprintf(stderr, "svgtiny_OUT_OF_MEMORY");
			break;
		case svgtiny_LIBDOM_ERROR:
			fprintf(stderr, "svgtiny_LIBDOM_ERROR");
			break;
		case svgtiny_NOT_SVG:
			fprintf(stderr, "svgtiny_NOT_SVG");
			break;
		case svgtiny_SVG_ERROR:
			fprintf(stderr, "svgtiny_SVG_ERROR: line %i: %s",
					diagram->error_line,
					diagram->error_message);
			break;
		default:
			fprintf(stderr, "unknown svgtiny_code %i", code);
			break;
		}
		fprintf(stderr, "\n");
	}

	free(buffer);

	printf("viewbox 0 0 %g %g\n",
			scale * diagram->width, scale * diagram->height);

	for (unsigned int i = 0; i != diagram->shape_count; i++) {
		if (diagram->shape[i].fill == svgtiny_TRANSPARENT)
			printf("fill none ");
		else
			printf("fill #%.6x ", diagram->shape[i].fill);
		if (diagram->shape[i].stroke == svgtiny_TRANSPARENT)
			printf("stroke none ");
		else
			printf("stroke #%.6x ", diagram->shape[i].stroke);
		printf("stroke-width %g ",
				scale * diagram->shape[i].stroke_width);
		if (diagram->shape[i].path) {
			printf("path '");
			for (unsigned int j = 0;
					j != diagram->shape[i].path_length; ) {
				switch ((int) diagram->shape[i].path[j]) {
				case svgtiny_PATH_MOVE:
					printf("M %g %g ",
					scale * diagram->shape[i].path[j + 1],
					scale * diagram->shape[i].path[j + 2]);
					j += 3;
					break;
				case svgtiny_PATH_CLOSE:
					printf("Z ");
					j += 1;
					break;
				case svgtiny_PATH_LINE:
					printf("L %g %g ",
					scale * diagram->shape[i].path[j + 1],
					scale * diagram->shape[i].path[j + 2]);
					j += 3;
					break;
				case svgtiny_PATH_BEZIER:
					printf("C %g %g %g %g %g %g ",
					scale * diagram->shape[i].path[j + 1],
					scale * diagram->shape[i].path[j + 2],
					scale * diagram->shape[i].path[j + 3],
					scale * diagram->shape[i].path[j + 4],
					scale * diagram->shape[i].path[j + 5],
					scale * diagram->shape[i].path[j + 6]);
					j += 7;
					break;
				default:
					printf("error ");
					j += 1;
				}
			}
			printf("' ");
		} else if (diagram->shape[i].text) {
			printf("text %g %g '%s' ",
					scale * diagram->shape[i].text_x,
					scale * diagram->shape[i].text_y,
					diagram->shape[i].text);
		}
		printf("\n");
	}

	svgtiny_free(diagram);

	return 0;
}