-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerate.py
More file actions
executable file
·286 lines (237 loc) · 8.23 KB
/
generate.py
File metadata and controls
executable file
·286 lines (237 loc) · 8.23 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
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
#!/usr/bin/env python3
'''
Generates text fallback fillers for the
Cataclysm Dark Days Ahead game tilesets.
'''
import csv
import json
import os
import pyvips
import re
import sys
from pathlib import Path
# settings
DIR = 'text_fallback_fillers'
GAME_DIR = '../Cataclysm-DDA'
FONT = 'Liberation Sans Narrow, Condensed'
SHADOW_BLEND_MODE = 'add'
BACKGROUND_BLEND_MODE = 'overlay'
SHADOW_OFFSET = {
'x': 1,
'y': 1
}
JSON_DUMP_ARGS = {}
DEFAULT_COLOR = 'black'
LIGHT_GRAY = (200, 200, 200)
PINK = (255, 192, 203)
TYPES = {
'default': {
'canvas_dimensions': (32, 32),
'text_box': {'width': 32, 'height': 32},
'font_size': 10,
'text_gravity': 'north-west',
'canvas_gravity': 'north-west',
},
'monster': {
'canvas_dimensions': (32, 64),
'text_box': {'width': 48, 'height': 32},
'font_size': 10,
'text_gravity': 'north-west',
'canvas_gravity': 'south',
'background': 'backgrounds/creature.png',
'vips_methods': {
'rot90': {},
}
}
}
# constants
COLOR_DEFS_DIR = 'data/raw/colors.json'
COLOR_PREFIXES = ('i', 'c', 'h')
UNDEFINED_COLOR = 'undefined'
ZWSP = '\u200b'
THIN_SPACE = '\u2009'
APOSTROPHE = '\u0301' # '\u00B4' # '\u02BC'
VOWELS = ('A', 'E', 'I', 'O', 'U', 'Y')
NO_BREAKS_BEFORE = (*VOWELS, ' ', None)
def context_loop(text):
'''
Loop through a list with previous and upcoming values
'''
text = list(text)
offsets = \
[None] + text[:-1],\
text,\
text[1:] + [None],\
text[2:] + [None, None]
return list(zip(*offsets)) # TODO: verify list is required
def shorten_text(text):
'''
Prepare text for measuring length without added special characters
'''
# removing uninformative prefixes
text = re.sub(r'^(A|a|The|pair of)\s', '', text)
# removing states in parentheses
text = re.sub(r'\([^\(]\)$', '', text)
# & is special in Pango text layout engine
text = text.replace('&', '|')
return text
def add_textbreaks(text, start=6):
'''
Workaround absense of pango.WRAP_CHAR support in pyvips
by adding zero-width spaces after defined index
where it's appropriate to break a word,
replace some characters with similar-looking but thinner ones.
'''
result = text[:start]
for prev1, character, next1, next2 in context_loop(text[start:]):
if character == ' ':
# U+200A HAIR SPACE is too thin in some cases
result += THIN_SPACE
elif character == '\'':
# accent is close enough but doesn't require horizontal space
result += APOSTROPHE
elif character == character.upper() or prev1 == ' ':
# do not break off one character.
# TODO: verify it's not covered by the NO_BREAKS_BEFORE check
result += character
else:
if next1 in NO_BREAKS_BEFORE or next2 in NO_BREAKS_BEFORE:
result += character
else:
result += character + ZWSP
return result
def convert_color_name_from_defs(name):
'''
Make color names from the Cataclysm-DDA/data/raw/ definitions
look closer to Cataclysm-DDA/data/json/ field values.
'''
new_name = re.sub('^L', 'light', name)
new_name = re.sub('^D', 'dark', new_name)
return new_name.lower()
def load_color_defs(filepath):
'''
Load default color definitions from CDDA
'''
with open(filepath) as fh:
color_defs = json.load(fh)[0]
output = { # these are not defined explicitly in the game files
'lightgray': LIGHT_GRAY,
'pink': PINK,
}
for color_name, value in color_defs.items():
if color_name == 'type':
continue
output[convert_color_name_from_defs(color_name)] = value
return output
def colors_from_field(value):
'''
Convert compound color from Cataclysm-DDA/data/json/ field value
to a tuple of two colors, dropping prefixes.
TODO: re-evaluate prefixes role.
'''
# removing underlines between parts of one color
value = re.sub(r'(light|dark)_', '\\1', value)
# split colors, removing prefixes
value = [p for p in value.split('_') if p not in COLOR_PREFIXES]
color_fg = value.pop(0)
if value:
color_bg = value.pop(0)
assert not value # there should be no more than 2 colors
else:
color_bg = UNDEFINED_COLOR
return (color_fg, color_bg)
def output(type_, id_, text, color_fg, color_bg, dir_tree=True):
'''
Create directory structure, generate and write files.
'''
# get type settings
if type_ in TYPES:
type_settings = TYPES[type_]
else:
type_settings = TYPES['default']
canvas_dimensions = type_settings['canvas_dimensions']
text_box = type_settings['text_box']
font_size = type_settings['font_size']
text_gravity = type_settings['text_gravity']
canvas_gravity = type_settings['canvas_gravity']
background = type_settings.get('background')
vips_methods = type_settings.get('vips_methods', {})
# setup output directory
size_dir = 'tff_' + 'x'.join(map(str, canvas_dimensions))
if dir_tree:
dirpath = Path(os.path.join(size_dir, type_, id_))
dirpath.mkdir(parents=True, exist_ok=True)
else:
dirpath = Path(DIR)
filepath_json = os.path.join(dirpath, f'{id_}.json')
filepath_png = os.path.join(dirpath, f'{id_}.png')
# write JSON
with open(filepath_json, 'w') as fp:
json_dict = [{'id': id_, 'fg': id_, 'bg': ''}]
json.dump(json_dict, fp, **JSON_DUMP_ARGS)
# prepare text
text = shorten_text(text)
if len(text) > 4:
text = add_textbreaks(text, 4)
# render colored text image
original_text = pyvips.Image.text(
text, font=f'{FONT} {font_size}',
dpi=72, **text_box)[0] # autofit_dpi=True)[0]
boxed_text = original_text.copy(interpretation='srgb')\
.gravity(text_gravity, *text_box.values())
text_color = boxed_text.new_from_image(color_fg)
text_image = text_color.bandjoin(boxed_text)
# render shadow
shadow_image = render_shadow(boxed_text, color_fg, color_bg)
# add shadow to the text image
final = text_image.composite2(
shadow_image, SHADOW_BLEND_MODE, **SHADOW_OFFSET)
# per-type operations
for method, method_arguments in vips_methods.items():
final = getattr(final, method)(**method_arguments)
final = final.gravity(canvas_gravity, *canvas_dimensions)
if background:
back_image = pyvips.Image.new_from_file(background)
final = final.composite2(back_image, BACKGROUND_BLEND_MODE)
final.write_to_file(filepath_png)
def render_shadow(text, color_fg, color_bg, method='mostly_black', blur=0.5):
'''
Generate a shadow image from a copy of rendered text image
'''
shadow = text.copy()
if method == 'mostly_black':
brightness = sum(color_fg) / 3
if brightness > 32:
bg_color = [0, 0, 0]
else:
bg_color = [255, 255, 255]
shadow_color = shadow.new_from_image(bg_color)
elif method == 'invert_all':
shadow_color = shadow.new_from_image(color_fg)\
.colourspace('b-w')\
.invert()\
.colourspace('srgb')
elif method == 'invert_undefined':
if color_bg == UNDEFINED_COLOR:
shadow_color = shadow.new_from_image(color_fg)\
.colourspace('b-w')\
.invert()\
.colourspace('srgb')
else:
shadow_color = shadow.new_from_image(color_bg)
return shadow_color.bandjoin(shadow).gaussblur(blur)
if __name__ == '__main__':
color_defs_filepath = os.path.join(GAME_DIR, COLOR_DEFS_DIR)
color_defs = load_color_defs(color_defs_filepath)
input_filepath = sys.argv[1]
with open(input_filepath) as fh:
reader = csv.reader(fh)
next(reader) # skip header
for row in reader:
type_, id_, name, color, looks_like, copy_from = row
color_fg, color_bg = colors_from_field(color or DEFAULT_COLOR)
color_fg = color_defs[color_fg]
color_bg = color_defs.get(color_bg, UNDEFINED_COLOR)
if id_ and name:
print(id_)
output(type_.lower(), id_, name, color_fg, color_bg)