• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

kakwa / ficlip / 6317839087

26 Sep 2023 08:14PM UTC coverage: 86.936%. Remained the same
6317839087

push

github

kakwa
move FI_SEG_FLAG back in public header for know

925 of 1064 relevant lines covered (86.94%)

254.09 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.62
/src/utils.c
1
/* This file is part of the ficlip clipping library
2
 *
3
 * ficlip is licensed under MIT.
4
 *
5
 * Copyright 2017, Pierre-Francois Carpentier
6
 */
7

8
#include <stdlib.h>
9
#include <stdio.h>
10
#include <string.h>
11
#include <stdbool.h>
12
#include "ficlip.h"
13
#include <math.h>
14

15
void fi_point_draw_d(FI_POINT_D pt, FILE *out) {
2,460✔
16
    fprintf(out, "%.4f,%.4f ", pt.x, pt.y);
2,460✔
17
}
2,460✔
18

19
/* really bad parser for path declaration, but will make life far easier for
20
 * testing
21
 */
22
int fi_parse_path(const char *in, int s_in, FI_PATH **out) {
44✔
23
    *out = NULL;
44✔
24
    int i;
25
    char *n_start = NULL;
44✔
26
    size_t n_len = 0;
44✔
27
    int pc = 0;
44✔
28
    int ret = 0;
44✔
29

30
    FI_PATH *out_current = NULL;
44✔
31
    FI_SEG_TYPE type = FI_SEG_END;
44✔
32

33
    for (i = 0; i < s_in; i++) {
6,022✔
34
        switch (in[i]) {
5,982✔
35
        case '\n':
888✔
36
        case ',':
37
        case ' ':
38
            if (n_start != NULL && n_len != 0) {
1,552✔
39
                char *tmp = calloc(1, n_len + 1);
664✔
40
                strncpy(tmp, n_start, n_len);
664✔
41
                double coord = atof(tmp);
664✔
42
                free(tmp);
664✔
43
                n_start = NULL;
664✔
44
                n_len = 0;
664✔
45
                switch (type) {
46
                case FI_SEG_END:
×
47
                    break;
×
48
                case FI_SEG_MOVE:
84✔
49
                    switch (pc) {
50
                    case 0:
42✔
51
                        out_current->meta->last->section.points[0].x = coord;
42✔
52
                        pc++;
42✔
53
                        break;
42✔
54
                    case 1:
42✔
55
                        out_current->meta->last->section.points[0].y = coord;
42✔
56
                        pc = 0;
42✔
57
                        break;
42✔
58
                    default:
×
59
                        ret = ERR_PARSING_FAIL;
×
60
                        break;
×
61
                    }
62
                    break;
84✔
63
                case FI_SEG_LINE:
166✔
64
                    switch (pc) {
65
                    case 0:
84✔
66
                        out_current->meta->last->section.points[0].x = coord;
84✔
67
                        pc++;
84✔
68
                        break;
84✔
69
                    case 1:
82✔
70
                        out_current->meta->last->section.points[0].y = coord;
82✔
71
                        pc = 0;
82✔
72
                        break;
82✔
73
                    default:
×
74
                        ret = ERR_PARSING_FAIL;
×
75
                        break;
×
76
                    }
77
                    break;
166✔
78
                case FI_SEG_ARC:
162✔
79
                    switch (pc) {
80
                    case 0:
24✔
81
                        out_current->meta->last->section.points[0].x = coord;
24✔
82
                        pc++;
24✔
83
                        break;
24✔
84
                    case 1:
24✔
85
                        out_current->meta->last->section.points[0].y = coord;
24✔
86
                        pc++;
24✔
87
                        break;
24✔
88
                    case 2:
24✔
89
                        out_current->meta->last->section.points[1].x = coord;
24✔
90
                        pc++;
24✔
91
                        break;
24✔
92
                    case 3:
24✔
93
                        if (coord)
24✔
94
                            out_current->meta->last->section.flag |=
12✔
95
                                FI_LARGE_ARC;
96
                        pc++;
24✔
97
                        break;
24✔
98
                    case 4:
22✔
99
                        if (coord)
22✔
100
                            out_current->meta->last->section.flag |= FI_SWEEP;
18✔
101
                        pc++;
22✔
102
                        break;
22✔
103
                    case 5:
22✔
104
                        out_current->meta->last->section.points[2].x = coord;
22✔
105
                        pc++;
22✔
106
                        break;
22✔
107
                    case 6:
22✔
108
                        out_current->meta->last->section.points[2].y = coord;
22✔
109
                        pc = 0;
22✔
110
                        break;
22✔
111
                    default:
×
112
                        ret = ERR_PARSING_FAIL;
×
113
                        break;
×
114
                    }
115
                    break;
162✔
116
                case FI_SEG_QUA_BEZIER:
48✔
117
                    switch (pc) {
118
                    case 0:
12✔
119
                        out_current->meta->last->section.points[0].x = coord;
12✔
120
                        pc++;
12✔
121
                        break;
12✔
122
                    case 1:
12✔
123
                        out_current->meta->last->section.points[0].y = coord;
12✔
124
                        pc++;
12✔
125
                        break;
12✔
126
                    case 2:
12✔
127
                        out_current->meta->last->section.points[1].x = coord;
12✔
128
                        pc++;
12✔
129
                        break;
12✔
130
                    case 3:
12✔
131
                        out_current->meta->last->section.points[1].y = coord;
12✔
132
                        pc = 0;
12✔
133
                        break;
12✔
134
                    default:
×
135
                        ret = ERR_PARSING_FAIL;
×
136
                        break;
×
137
                    }
138
                    break;
48✔
139
                case FI_SEG_CUB_BEZIER:
204✔
140
                    switch (pc) {
141
                    case 0:
34✔
142
                        out_current->meta->last->section.points[0].x = coord;
34✔
143
                        pc++;
34✔
144
                        break;
34✔
145
                    case 1:
34✔
146
                        out_current->meta->last->section.points[0].y = coord;
34✔
147
                        pc++;
34✔
148
                        break;
34✔
149
                    case 2:
34✔
150
                        out_current->meta->last->section.points[1].x = coord;
34✔
151
                        pc++;
34✔
152
                        break;
34✔
153
                    case 3:
34✔
154
                        out_current->meta->last->section.points[1].y = coord;
34✔
155
                        pc++;
34✔
156
                        break;
34✔
157
                    case 4:
34✔
158
                        out_current->meta->last->section.points[2].x = coord;
34✔
159
                        pc++;
34✔
160
                        break;
34✔
161
                    case 5:
34✔
162
                        out_current->meta->last->section.points[2].y = coord;
34✔
163
                        pc = 0;
34✔
164
                        break;
34✔
165
                    default:
×
166
                        ret = ERR_PARSING_FAIL;
×
167
                        break;
×
168
                    }
169
                    break;
204✔
170
                }
171
            } else {
172
                n_start = NULL;
224✔
173
                n_len = 0;
224✔
174
            }
175
            break;
888✔
176
        case 'M':
44✔
177
            ret = fi_append_new_seg(&out_current, FI_SEG_MOVE);
44✔
178
            type = FI_SEG_MOVE;
44✔
179
            pc = 0;
44✔
180
            break;
44✔
181
        case 'L':
84✔
182
            ret = fi_append_new_seg(&out_current, FI_SEG_LINE);
84✔
183
            type = FI_SEG_LINE;
84✔
184
            pc = 0;
84✔
185
            break;
84✔
186
        case 'A':
24✔
187
            ret = fi_append_new_seg(&out_current, FI_SEG_ARC);
24✔
188
            type = FI_SEG_ARC;
24✔
189
            pc = 0;
24✔
190
            break;
24✔
191
        case 'Q':
12✔
192
            ret = fi_append_new_seg(&out_current, FI_SEG_QUA_BEZIER);
12✔
193
            type = FI_SEG_QUA_BEZIER;
12✔
194
            pc = 0;
12✔
195
            break;
12✔
196
        case 'C':
2,038✔
197
            ret = fi_append_new_seg(&out_current, FI_SEG_CUB_BEZIER);
2,038✔
198
            type = FI_SEG_CUB_BEZIER;
2,038✔
199
            pc = 0;
2,038✔
200
            break;
2,038✔
201
        case 'Z':
40✔
202
            ret = fi_append_new_seg(&out_current, FI_SEG_END);
40✔
203
            type = FI_SEG_END;
40✔
204
            pc = 0;
40✔
205
            break;
40✔
206
        case '-':
2,850✔
207
        case '0':
208
        case '1':
209
        case '2':
210
        case '3':
211
        case '4':
212
        case '5':
213
        case '6':
214
        case '7':
215
        case '8':
216
        case '9':
217
        case '.':
218
            if (n_start == NULL) {
2,850✔
219
                n_start = (char *)&in[i];
666✔
220
            }
221
            n_len++;
2,850✔
222
            break;
2,850✔
223
        default:
2✔
224
            ret = ERR_PARSING_FAIL;
2✔
225
            break;
2✔
226
        }
227
        if (ret) {
5,982✔
228
            fi_free_path(out_current);
4✔
229
            return ret;
4✔
230
        }
231
    }
232
    (*out) = out_current;
40✔
233
    return 0;
40✔
234
}
235

236
void fi_start_svg_doc(FILE *out, double width, double height) {
8✔
237
    fprintf(out,
8✔
238
            "<?xml version=\"1.0\"  encoding=\"UTF-8\" standalone=\"no\"?>\n");
239
    fprintf(out,
8✔
240
            "<svg version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" "
241
            "xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"%.4f\" "
242
            "height=\"%.4f\">\n",
243
            width, height);
244
    return;
8✔
245
}
246

247
void fi_end_svg_doc(FILE *out) {
8✔
248
    fprintf(out, "</svg>\n");
8✔
249
    return;
8✔
250
}
251

252
void fi_start_svg_path(FILE *out) {
32✔
253
    fprintf(out, "<path d=\"");
32✔
254
    return;
32✔
255
}
256

257
void fi_end_svg_path(FILE *out, double stroke_width, char *stroke_color,
32✔
258
                     char *fill_color, char *fill_opacity) {
259
    fprintf(out, "\" stroke-width=\"%.4fpx\" ", stroke_width);
32✔
260
    if (stroke_color)
32✔
261
        fprintf(out, "stroke=\"%s\" ", stroke_color);
32✔
262
    if (fill_color)
32✔
263
        fprintf(out, "fill=\"%s\" ", fill_color);
32✔
264
    if (fill_opacity)
32✔
265
        fprintf(out, "fill-opacity=\"%s\" ", fill_opacity);
×
266
    fprintf(out, " />\n");
32✔
267
    return;
32✔
268
}
269

270
void fi_draw_path(FI_PATH *in, FILE *out) {
38✔
271
    FI_PATH *tmp = in;
38✔
272
    while (tmp != NULL) {
2,444✔
273
        FI_SEG_TYPE type = tmp->section.type;
2,406✔
274
        FI_SEG_TYPE flag = tmp->section.flag;
2,406✔
275
        FI_POINT_D *pt = tmp->section.points;
2,406✔
276
        switch (type) {
2,406✔
277
        case FI_SEG_END:
34✔
278
            fprintf(out, "Z ");
34✔
279
            break;
34✔
280
        case FI_SEG_MOVE:
38✔
281
            fprintf(out, "M ");
38✔
282
            fi_point_draw_d(pt[0], out);
38✔
283
            break;
38✔
284
        case FI_SEG_LINE:
2,270✔
285
            fprintf(out, "L ");
2,270✔
286
            fi_point_draw_d(pt[0], out);
2,270✔
287
            break;
2,270✔
288
        case FI_SEG_ARC:
22✔
289
            fprintf(out, "A ");
22✔
290
            fprintf(out, "%.4f ", pt[0].x);
22✔
291
            fprintf(out, "%.4f ", pt[0].y);
22✔
292
            fprintf(out, "%.4f ", pt[1].x);
22✔
293
            if (flag & FI_LARGE_ARC)
22✔
294
                fprintf(out, "1 ");
10✔
295
            else
296
                fprintf(out, "0 ");
12✔
297
            if (flag & FI_SWEEP)
22✔
298
                fprintf(out, "1 ");
14✔
299
            else
300
                fprintf(out, "0 ");
8✔
301
            fi_point_draw_d(pt[2], out);
22✔
302
            break;
22✔
303
        case FI_SEG_QUA_BEZIER:
12✔
304
            fprintf(out, "Q ");
12✔
305
            fi_point_draw_d(pt[0], out);
12✔
306
            fi_point_draw_d(pt[1], out);
12✔
307
            break;
12✔
308

309
        case FI_SEG_CUB_BEZIER:
30✔
310
            fprintf(out, "C ");
30✔
311
            fi_point_draw_d(pt[0], out);
30✔
312
            fi_point_draw_d(pt[1], out);
30✔
313
            fi_point_draw_d(pt[2], out);
30✔
314
            break;
30✔
315
        }
316
        tmp = tmp->next;
2,406✔
317
    }
318
}
38✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc