-
Notifications
You must be signed in to change notification settings - Fork 29
/
holder.py
executable file
·317 lines (266 loc) · 12.4 KB
/
holder.py
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#!/usr/bin/python3
# holder.py
# -*- coding: utf-8 -*-
#
# The python script in this file makes the various parts of a model planisphere.
#
# Copyright (C) 2014-2024 Dominic Ford <https://dcford.org.uk/>
#
# This code is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# You should have received a copy of the GNU General Public License along with
# this file; if not, write to the Free Software Foundation, Inc., 51 Franklin
# Street, Fifth Floor, Boston, MA 02110-1301, USA
# ----------------------------------------------------------------------------
"""
Render the holder for the planisphere.
"""
from math import pi, sin, cos, atan2, asin, hypot
from numpy import arange
from typing import Dict, List, Tuple
from constants import radius, transform, pos
from constants import unit_deg, unit_rev, unit_cm, unit_mm, r_1, r_2, fold_gap, central_hole_size, line_width_base
from graphics_context import BaseComponent, GraphicsContext
from settings import fetch_command_line_arguments
from text import text
class Holder(BaseComponent):
"""
Render the holder for the planisphere.
"""
def default_filename(self) -> str:
"""
Return the default filename to use when saving this component.
"""
return "holder"
def bounding_box(self, settings: dict) -> Dict[str, float]:
"""
Return the bounding box of the canvas area used by this component.
:param settings:
A dictionary of settings required by the renderer.
:return:
Dictionary with the elements 'x_min', 'x_max', 'y_min' and 'y_max' set
"""
h: float = r_1 + fold_gap
return {
'x_min': -r_1 - 4 * unit_mm,
'x_max': r_1 + 4 * unit_mm,
'y_min': -r_2 - h - 4 * unit_mm,
'y_max': h + 1.2 * unit_cm
}
def do_rendering(self, settings: dict, context: GraphicsContext) -> None:
"""
This method is required to actually render this item.
:param settings:
A dictionary of settings required by the renderer.
:param context:
A GraphicsContext object to use for drawing
:return:
None
"""
is_southern: bool = settings['latitude'] < 0
latitude: float = abs(settings['latitude'])
language: str = settings['language']
context.set_font_size(0.9)
a: float = 6 * unit_cm
h: float = r_1 + fold_gap
# Draw dotted line for folding the bottom of the planisphere
context.begin_path()
context.move_to(x=-r_1, y=0)
context.line_to(x=r_1, y=0)
context.stroke(dotted=True)
# Draw the rectangular back and lower body of the planisphere
context.begin_path()
context.move_to(x=-r_1, y=a)
context.line_to(x=-r_1, y=-a)
context.move_to(x=r_1, y=a)
context.line_to(x=r_1, y=-a)
context.stroke(dotted=False)
# Draw the curved upper part of the body of the planisphere
theta: float = unit_rev / 2 - atan2(r_1, h - a)
context.begin_path()
context.arc(centre_x=0, centre_y=-h, radius=r_2, arc_from=-theta - pi / 2, arc_to=theta - pi / 2)
context.move_to(x=-r_2 * sin(theta), y=-h - r_2 * cos(theta))
context.line_to(x=-r_1, y=-a)
context.move_to(x=r_2 * sin(theta), y=-h - r_2 * cos(theta))
context.line_to(x=r_1, y=-a)
context.stroke()
# Shade the viewing window which needs to be cut out
x0: Tuple[float, float] = (0, h)
context.begin_path()
i: int
az: float
for i, az in enumerate(arange(0, 360.5, 1)):
pp: Tuple[float, float] = transform(alt=0, az=az, latitude=latitude)
r: float = radius(dec=pp[1] / unit_deg, latitude=latitude)
p: Dict[str, float] = pos(r=r, t=pp[0])
if i == 0:
context.move_to(x0[0] + p['x'], -x0[1] + p['y'])
else:
context.line_to(x0[0] + p['x'], -x0[1] + p['y'])
context.stroke()
context.fill(color=(0, 0, 0, 0.2))
# Display instructions for cutting out the viewing window
instructions: str = text[language]["cut_out_instructions"]
context.set_color(color=(0, 0, 0, 1))
context.text_wrapped(text=instructions,
width=4 * unit_cm, justify=0,
x=0, y=-h - r_1 * 0.35,
h_align=0, v_align=0, rotation=0)
# Cardinal points
def cardinal(dir: str, ang: float) -> None:
pp: Tuple[float, float] = transform(alt=0, az=ang - 0.01, latitude=latitude)
r: float = radius(dec=pp[1] / unit_deg, latitude=latitude)
p: Dict[str, float] = pos(r, pp[0])
pp2: Tuple[float, float] = transform(alt=0, az=ang + 0.01, latitude=latitude)
r2: float = radius(dec=pp2[1] / unit_deg, latitude=latitude)
p2: Dict[str, float] = pos(r=r2, t=pp2[0])
p3: List[float] = [p2[i] - p[i] for i in ('x', 'y')]
tr: float = -unit_rev / 4 - atan2(p3[0], p3[1])
context.text(text=dir, x=x0[0] + p['x'], y=-x0[1] + p['y'],
h_align=0, v_align=1, gap=unit_mm, rotation=tr)
# Write the cardinal points around the horizon of the viewing window
context.set_font_style(bold=True)
txt: str = text[language]['cardinal_points']['w']
cardinal(txt, 180 if not is_southern else 0)
txt = text[language]['cardinal_points']['s']
cardinal(txt, 270 if not is_southern else 90)
txt = text[language]['cardinal_points']['e']
cardinal(txt, 0 if not is_southern else 180)
txt = text[language]['cardinal_points']['n']
cardinal(txt, 90 if not is_southern else 270)
context.set_font_style(bold=False)
# Clock face, which lines up with the date scale on the star wheel
theta: float = unit_rev / 24 * 7 # 5pm -> 7am means we cover 7 hours on either side of midnight
dash: float = unit_rev / 24 / 4 # Draw fat dashes at 15 minute intervals
# Outer edge of dashed scale
r_3: float = r_2 - 2 * unit_mm
# Inner edge of dashed scale
r_4: float = r_2 - 3 * unit_mm
# Radius of dashes for marking hours
r_5: float = r_2 - 4 * unit_mm
# Radius of text marking hours
r_6: float = r_2 - 5.5 * unit_mm
# Inner and outer curves around dashed scale
context.begin_path()
context.arc(centre_x=0, centre_y=-h, radius=r_3, arc_from=-theta - pi / 2, arc_to=theta - pi / 2)
context.begin_sub_path()
context.arc(centre_x=0, centre_y=-h, radius=r_4, arc_from=-theta - pi / 2, arc_to=theta - pi / 2)
context.stroke()
# Draw a fat dashed line with one dash every 15 minutes
for i in arange(-theta, theta, 2 * dash):
context.begin_path()
context.arc(centre_x=0, centre_y=-h, radius=(r_3 + r_4) / 2, arc_from=i - pi / 2, arc_to=i + dash - pi / 2)
context.stroke(line_width=(r_3 - r_4) / line_width_base)
# Write the hours
for hr in arange(-7, 7.1, 1):
txt: str = "{:.0f}{}".format(hr if (hr > 0) else hr + 12,
"AM" if (hr > 0) else "PM")
if language == "fr":
txt = "{:02d}h00".format(int(hr if (hr > 0) else hr + 24))
if hr == 0:
txt = ""
t: float = unit_rev / 24 * hr * (-1 if not is_southern else 1)
# Stroke a dash and write the number of the hour
context.begin_path()
context.move_to(x=r_3 * sin(t), y=-h - r_3 * cos(t))
context.line_to(x=r_5 * sin(t), y=-h - r_5 * cos(t))
context.stroke(line_width=1)
context.text(text=txt, x=r_6 * sin(t), y=-h - r_6 * cos(t), h_align=0, v_align=0, gap=0, rotation=t)
# Back edge
b: float = unit_cm
t1: float = atan2(h - a, r_1)
t2: float = asin(b / hypot(r_1, h - a))
context.begin_path()
context.move_to(x=-r_1, y=a)
context.line_to(x=-b * sin(t1 + t2), y=h + b * cos(t1 + t2))
context.move_to(x=r_1, y=a)
context.line_to(x=b * sin(t1 + t2), y=h + b * cos(t1 + t2))
context.arc(centre_x=0, centre_y=h, radius=b, arc_from=unit_rev / 2 - (t1 + t2) - pi / 2,
arc_to=unit_rev / 2 + (t1 + t2) - pi / 2)
context.stroke(line_width=1)
# For latitudes not too close to the pole, we have enough space to fit instructions onto the planisphere
if latitude < 56:
# Big bold title
context.set_font_size(3.0)
txt: str = text[language]['title']
context.set_font_style(bold=True)
context.text(
text="{} {:.0f}\u00B0{}".format(txt, float(latitude), "N" if not is_southern else "S"),
x=0, y=-4.8 * unit_cm,
h_align=0, v_align=0, gap=0, rotation=0)
context.set_font_style(bold=False)
# First column of instructions
context.set_font_size(2)
context.text(
text="1",
x=-5.0 * unit_cm, y=-4.0 * unit_cm,
h_align=0, v_align=0, gap=0, rotation=0)
context.set_font_size(1)
context.text_wrapped(
text=text[language]['instructions_1'],
x=-5.0 * unit_cm, y=-3.4 * unit_cm, width=4.5 * unit_cm, justify=-1,
h_align=0, v_align=1, rotation=0)
# Second column of instructions
context.set_font_size(2)
context.text(
text="2",
x=0, y=-4.0 * unit_cm,
h_align=0, v_align=0, gap=0, rotation=0)
context.set_font_size(1)
context.text_wrapped(
text=text[language]['instructions_2'].format(cardinal="north" if not is_southern else "south"),
x=0, y=-3.4 * unit_cm, width=4.5 * unit_cm, justify=-1,
h_align=0, v_align=1, rotation=0)
# Third column of instructions
context.set_font_size(2)
context.text(
text="3",
x=5.0 * unit_cm, y=-4.0 * unit_cm,
h_align=0, v_align=0, gap=0, rotation=0)
context.set_font_size(1)
context.text_wrapped(
text=text[language]['instructions_3'],
x=5.0 * unit_cm, y=-3.4 * unit_cm, width=4.5 * unit_cm, justify=-1,
h_align=0, v_align=1, rotation=0)
else:
# For planispheres for use at high latitudes, we don't have much space, so don't show instructions.
# We just display a big bold title
context.set_font_size(3.0)
txt = text[language]['title']
context.set_font_style(bold=True)
context.text(
text="%s %d\u00B0%s" % (txt, latitude, "N" if not is_southern else "S"),
x=0, y=-1.8 * unit_cm,
h_align=0, v_align=0, gap=0, rotation=0)
context.set_font_style(bold=False)
# Write explanatory text on the back of the planisphere
context.set_font_size(1.1)
context.text_wrapped(
text=text[language]['instructions_4'],
x=0, y=5.5 * unit_cm, width=12 * unit_cm, justify=-1,
h_align=0, v_align=1, rotation=0.5 * unit_rev)
# Display web link and copyright text
txt = text[language]['more_info']
context.set_font_size(0.9)
context.text(text=txt, x=0, y=-0.5 * unit_cm, h_align=0, v_align=0, gap=0, rotation=0)
context.set_font_size(0.9)
context.text(text=txt, x=0, y=0.5 * unit_cm, h_align=0, v_align=0, gap=0, rotation=pi)
# Draw central hole
context.begin_path()
context.circle(centre_x=0, centre_y=h, radius=central_hole_size)
context.stroke()
# Do it right away if we're run as a script
if __name__ == "__main__":
# Fetch command line arguments passed to us
arguments = fetch_command_line_arguments(default_filename=Holder().default_filename())
# Render the holder for the planisphere
Holder(settings={
'latitude': arguments['latitude'],
'language': 'en'
}).render_to_file(
filename=arguments['filename'],
img_format=arguments['img_format']
)