-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexporter_ps.cpp
More file actions
65 lines (58 loc) · 2.15 KB
/
exporter_ps.cpp
File metadata and controls
65 lines (58 loc) · 2.15 KB
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
/*
* Vectorix -- line-based image vectorizer
* (c) 2016 Jan Hadrava <had@atrey.karlin.mff.cuni.cz>
*/
#include <cstdio>
#include <locale>
#include "v_image.h"
#include "exporter_ps.h"
// Post Script exporter
namespace vectorix {
void exporter_ps::write_header() {
fprintf(fd, "%%!PS-Adobe-3.0 EPSF-3.0\n");
int w = image->width;
int h = image->height;
fprintf(fd, "%%%%BoundingBox: 0 0 %d %d\n", w, h);
fprintf(fd, "gsave\n"); // Save current drawing state (needed by EPS)
};
void exporter_ps::write_footer() {
fprintf(fd, "grestore\n"); // Restore drawing state
fprintf(fd, "%%EOF\n");
};
void exporter_ps::write_line(const v_line &line) {
auto segment = line.segment.cbegin();
auto h = image->height;
fprintf(fd, "%f %f moveto\n", segment->main.x, h - segment->main.y); // y coordinate has to be transformed
v_pt cn = segment->control_next;
int count = 1;
p width = segment->width;
p opacity = segment->opacity;
v_co color = segment->color;
segment++;
while (segment != line.segment.cend()) {
fprintf(fd, "%f %f %f %f %f %f curveto\n", cn.x, h - cn.y, segment->control_prev.x, h - segment->control_prev.y, segment->main.x, h - segment->main.y); // draw bezier curve
cn = segment->control_next;
count ++;
// average width, opacity and color
width += segment->width;
opacity += segment->opacity;
color += segment->color;
segment++;
}
color /= count;
if (line.get_type() == v_line_type::stroke) {
fprintf(fd, "1 setlinecap\n"); // line end is round
fprintf(fd, "1 setlinejoin\n"); // line join is round
fprintf(fd, "%f setlinewidth\n", width/count); // average width
fprintf(fd, "%f %f %f setrgbcolor stroke\n", color.val[0]/255.f, color.val[1]/255.f, color.val[2]/255.f); // average color
}
else {
if ((line.get_group() == v_line_group::group_normal) || (line.get_group() == v_line_group::group_first)) {
group_col = color; //save color of first area in a group
}
if ((line.get_group() == v_line_group::group_normal) || (line.get_group() == v_line_group::group_last)) {
fprintf(fd, "%f %f %f setrgbcolor fill\n", group_col.val[0]/255.f, group_col.val[1]/255.f, group_col.val[2]/255.f); // fill with color
}
}
};
}; // namespace