1# ##### BEGIN GPL LICENSE BLOCK #####
2#
3#  This program is free software; you can redistribute it and/or
4#  modify it under the terms of the GNU General Public License
5#  as published by the Free Software Foundation; either version 2
6#  of the License, or (at your option) any later version.
7#
8#  This program is distributed in the hope that it will be useful,
9#  but WITHOUT ANY WARRANTY; without even the implied warranty of
10#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11#  GNU General Public License for more details.
12#
13#  You should have received a copy of the GNU General Public License
14#  along with this program; if not, write to the Free Software Foundation,
15#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16#
17# ##### END GPL LICENSE BLOCK #####
18
19# <pep8-80 compliant>
20
21import bpy
22
23
24def export(filepath, face_data, colors, width, height, opacity):
25    with open(filepath, 'w', encoding='utf-8') as file:
26        for text in get_file_parts(face_data, colors, width, height, opacity):
27            file.write(text)
28
29def get_file_parts(face_data, colors, width, height, opacity):
30    yield from header(width, height)
31    if opacity > 0.0:
32        name_by_color = {}
33        yield from prepare_colors(colors, name_by_color)
34        yield from draw_colored_polygons(face_data, name_by_color, width, height)
35    yield from draw_lines(face_data, width, height)
36    yield from footer()
37
38
39def header(width, height):
40    yield "%!PS-Adobe-3.0 EPSF-3.0\n"
41    yield f"%%Creator: Blender {bpy.app.version_string}\n"
42    yield "%%Pages: 1\n"
43    yield "%%Orientation: Portrait\n"
44    yield f"%%BoundingBox: 0 0 {width} {height}\n"
45    yield f"%%HiResBoundingBox: 0.0 0.0 {width:.4f} {height:.4f}\n"
46    yield "%%EndComments\n"
47    yield "%%Page: 1 1\n"
48    yield "0 0 translate\n"
49    yield "1.0 1.0 scale\n"
50    yield "0 0 0 setrgbcolor\n"
51    yield "[] 0 setdash\n"
52    yield "1 setlinewidth\n"
53    yield "1 setlinejoin\n"
54    yield "1 setlinecap\n"
55
56def prepare_colors(colors, out_name_by_color):
57    for i, color in enumerate(colors):
58        name = f"COLOR_{i}"
59        yield "/%s {" % name
60        out_name_by_color[color] = name
61
62        yield "gsave\n"
63        yield "%.3g %.3g %.3g setrgbcolor\n" % color
64        yield "fill\n"
65        yield "grestore\n"
66        yield "0 setgray\n"
67        yield "} def\n"
68
69def draw_colored_polygons(face_data, name_by_color, width, height):
70    for uvs, color in face_data:
71        yield from draw_polygon_path(uvs, width, height)
72        yield "closepath\n"
73        yield "%s\n" % name_by_color[color]
74
75def draw_lines(face_data, width, height):
76    for uvs, _ in face_data:
77        yield from draw_polygon_path(uvs, width, height)
78        yield "closepath\n"
79        yield "stroke\n"
80
81def draw_polygon_path(uvs, width, height):
82    yield "newpath\n"
83    for j, uv in enumerate(uvs):
84        uv_scale = (uv[0] * width, uv[1] * height)
85        if j == 0:
86            yield "%.5f %.5f moveto\n" % uv_scale
87        else:
88            yield "%.5f %.5f lineto\n" % uv_scale
89
90def footer():
91    yield "showpage\n"
92    yield "%%EOF\n"
93