-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathtypography.py
More file actions
387 lines (307 loc) · 11 KB
/
Copy pathtypography.py
File metadata and controls
387 lines (307 loc) · 11 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
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# encoding: utf-8
import sys
import re
from collections import namedtuple
from operator import attrgetter
from plotdevice.util import odict
from ..lib.cocoa import *
from plotdevice import DeviceError
from .atoms import StyleMixin
from .colors import Color
from ..util import _copy_attrs
from ..lib.foundry import *
_ctx = None
__all__ = ("Family", "Font", "Stylesheet", )
class Font(object):
def __init__(self, *args, **kwargs):
# handle the bootstrap case where we're initializing the ctx's font
if args==(None,):
self._face = font_face("HelveticaNeue")
self._metrics = dict(size=24.0, leading=1.2, tracking=0,
indent=0, margin=(0,0), spacing=(0,0),
hyphenate=0, align="left")
self._features = {}
return
# accept Font objects or spec dicts as first positional arg
first = args[0] if args else None
if isinstance(first, Font):
# make a copy of the existing font obj
_copy_attrs(first, self, ('_face','_metrics','_features'))
return
elif hasattr(first, 'items'):
# treat dict as output of a prior call to fontspec()
new_spec = dict(first)
else:
# check for invalid kwarg names
StyleMixin.validate(kwargs)
# validate & standardize the kwargs first
new_spec = fontspec(*args, **kwargs)
# collect the attrs from the current font to fill in any gaps
cur_spec = _ctx._font._spec
# if called with positional args we're starting over; inherit just the weight and size
if args and 'family' in new_spec:
cur_spec['width'] = None
cur_spec['variant'] = None
cur_spec['italic'] = False
# convert weight & width to integer values
for axis, num_axis in dict(weight='wgt', width='wid').items():
cur_spec[num_axis] = getattr(_ctx._font._face, num_axis)
if axis in new_spec:
name, val = standardized(axis, new_spec[axis])
new_spec.update({axis:name, num_axis:val})
# merge in changes from the new spec
spec = cur_spec.copy()
spec.update(new_spec) # our criteria
# use the combined spec to pick a face then break it into attributes
self._face = best_face(spec)
self._metrics = line_metrics(spec)
self._features = aat_features(spec)
def __repr__(self):
spec = [self.family, self.weight]
if self._face.variant:
spec.insert(2, self._face.variant)
spec.insert(1, '/' if self._face.italic else '|')
spec.insert(1, ("%.1fpt"%self._metrics['size']).replace('.0pt','pt'))
return ('Font(%s)'%" ".join(spec))
def __enter__(self):
if not hasattr(self, '_rollback'):
self._rollback = _ctx._font
_ctx._font = self
return self
def __exit__(self, type, value, tb):
_ctx._font = self._rollback
del self._rollback
def copy(self):
return Font(self)
### face introspection ###
@property
def family(self):
return self._face.family
@property
def weight(self):
return self._face.weight
@property
def width(self):
return self._face.width
@property
def variant(self):
return self._face.variant
@property
def italic(self):
return self._face.italic
@property
def face(self):
return self._face.psname
### family introspection ###
@property
def weights(self):
return Family(self.family).weights
@property
def widths(self):
return Family(self.family).widths
@property
def variants(self):
return Family(self.family).variants
@property
def siblings(self):
return Family(self.family).fonts
### line metrics ###
@property
def ascender(self):
return self._nsFont.ascender()
@property
def descender(self):
return self._nsFont.descender()
@property
def xheight(self):
return self._nsFont.xHeight()
@property
def capheight(self):
return self._nsFont.capHeight()
### line layout ###
@property
def size(self):
return self._metrics['size']
@property
def leading(self):
return self._metrics['leading']
@property
def tracking(self):
return self._metrics['tracking']
@property
def hyphenate(self):
return self._metrics['hyphenate']
@property
def indent(self):
return self._metrics['indent']
@property
def align(self):
return self._metrics['align']
@property
def margin(self):
return self._metrics['margin']
@property
def spacing(self):
return self._metrics['spacing']
### OpenType features ###
@property
def features(self):
return dict(self._features)
### internals ###
@property
def _nsFont(self):
fd = NSFontDescriptor.fontDescriptorWithName_size_(self._face.psname, self._metrics['size'])
if self._features:
fd = fd.fontDescriptorByAddingAttributes_(aat_attrs(self._features))
return NSFont.fontWithDescriptor_textTransform_(fd,None)
@property
def _spec(self):
spec = {axis:getattr(self._face, axis) for axis in ('family','weight','width','variant','italic')}
spec.update(self._features)
spec.update(self._metrics)
return spec
@classmethod
def validate(cls, kwargs):
known = StyleMixin.fontOpts + StyleMixin.aatOpts
remaining = [arg for arg in kwargs.keys() if arg not in known]
if remaining:
unknown = "Unknown Font argument%s: %s" % ('' if len(remaining)==1 else 's', ", ".join(remaining))
raise DeviceError(unknown)
LineLayout = namedtuple('Layout', ['align','leading','hyphenate','indent','margin','spacing'])
class Layout(LineLayout):
def __new__(cls, font):
self = super(Layout, cls).__new__(cls, *[getattr(font, k) for k in LineLayout._fields])
return self
def __enter__(self):
if not hasattr(self, '_rollback'):
self._rollback = _ctx._font
return self
def __exit__(self, type, value, tb):
_ctx._font = self._rollback
del self._rollback
@classmethod
def validate(cls, kwargs):
known = LineLayout._fields + ('lineheight',)
remaining = [arg for arg in kwargs.keys() if arg not in known]
if remaining:
unknown = "Unknown Layout argument%s: %s" % ('' if len(remaining)==1 else 's', ", ".join(remaining))
raise DeviceError(unknown)
class Family(object):
def __init__(self, famname):
if isinstance(famname, Font):
famname = famname.family
self._name = family_name(famname)
self._faces = odict( (f.psname,f) for f in family_members(self._name) )
self.encoding = font_encoding(list(self._faces.keys())[0])
def __repr__(self):
contents = ['"%s"'%self._name, ]
for group in 'weights', 'widths', 'variants', 'faces':
n = len(getattr(self, group))
if n:
contents.append('%i %s%s' % (n, group[:-1], '' if n==1 else 's'))
return ('Family(%s)'%", ".join(contents))
@property
def name(self):
return self._name
@property
def faces(self):
return odict(self._faces)
@property
def fonts(self):
return odict( (k,Font(face=v.psname)) for k,v in self._faces.items())
@property
def has_italic(self):
for f in self._faces.values():
if f.italic:
return True
return False
@property
def weights(self):
w_names = []
for f in sorted(self._faces.values(), key=attrgetter('wgt')):
if f.weight not in w_names:
w_names.append(f.weight)
return tuple(w_names)
@property
def variants(self):
v_names = []
for f in sorted(self._faces.values(), key=attrgetter('wgt')):
if f.variant not in v_names:
v_names.append(f.variant)
if any(v_names) and None in v_names:
return tuple([None, *filter(None, v_names)])
return tuple(v_names)
@property
def widths(self):
w_names = []
for f in sorted(self._faces.values(), key=attrgetter('wid')):
if f.width not in w_names:
w_names.append(f.width)
if w_names==[None]:
return ()
return tuple(w_names)
@classmethod
def find(cls, like=None, encoding="western"):
all_fams = family_names()
if like:
all_fams = [name for name in all_fams if sanitized(like) in sanitized(name)]
if encoding is all:
return all_fams
regions = {}
for fam in all_fams:
fnt = family_members(fam, names=True)[0]
enc = font_encoding(fnt)
regions[enc] = regions.get(enc, []) + [fam]
try:
return regions[encoding.title()]
except:
if like:
nosuchzone = "Couldn't find any fonts matching %r with an encoding of %r" % (like, encoding)
else:
nosuchzone = "Couldn't find any fonts with an encoding of %r, choose from: %r" % (encoding, regions.keys())
raise DeviceError(nosuchzone)
class Stylesheet(object):
kwargs = StyleMixin.opts
def __init__(self, styles=None):
self._styles = dict(styles or {})
def __repr__(self):
return "Stylesheet(%r)"%(self._styles)
def __iter__(self):
return iter(self._styles.keys())
def __len__(self):
return len(self._styles)
def __getitem__(self, key):
item = self._styles.get(key)
return dict(item) if item else None
def __setitem__(self, key, val):
if val is None:
del self[key]
elif hasattr(val, 'items'):
self.style(key, **val)
else:
badtype = 'Stylesheet: when directly assigning styles, pass them as dictionaries (not %s)'%type(val)
raise DeviceError(badtype)
def __delitem__(self, key):
if key in self._styles:
del self._styles[key]
def copy(self):
return Stylesheet(self._styles)
@property
def styles(self):
return dict(self._styles)
def style(self, name, *args, **kwargs):
if not kwargs and any(a in (None,'inherit') for a in args[:1]):
del self[name]
elif args or kwargs:
spec = {}
spec.update(self._styles.get( kwargs.get('style'), {} ))
spec.update(fontspec(*args, **kwargs))
color = kwargs.get('fill')
if color and not isinstance(color, Color):
if isinstance(color, (str, int, float)):
color = (color,)
color = Color(*color)
if color:
spec['fill'] = color
self._styles[name] = spec
return self[name]