-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy patheffects.py
More file actions
390 lines (331 loc) · 13.5 KB
/
effects.py
File metadata and controls
390 lines (331 loc) · 13.5 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
388
389
390
# encoding: utf-8
import os
import re
from contextlib import contextmanager
from ..lib.cocoa import *
from plotdevice import DeviceError
from ..util import _copy_attr, _copy_attrs, numlike
from .colors import Color
from .geometry import Point
from . import _cg_context, _cg_layer, _cg_port
_ctx = None
__all__ = ("Effect", "Shadow", "Stencil",)
# blend modes
_BLEND=dict(
# basics
normal=kCGBlendModeNormal,
clear=kCGBlendModeClear,
copy=kCGBlendModeCopy,
# pdf
multiply=kCGBlendModeMultiply,
screen=kCGBlendModeScreen,
overlay=kCGBlendModeOverlay,
darken=kCGBlendModeDarken,
lighten=kCGBlendModeLighten,
colordodge=kCGBlendModeColorDodge,
colorburn=kCGBlendModeColorBurn,
softlight=kCGBlendModeSoftLight,
hardlight=kCGBlendModeHardLight,
difference=kCGBlendModeDifference,
exclusion=kCGBlendModeExclusion,
hue=kCGBlendModeHue,
saturation=kCGBlendModeSaturation,
color=kCGBlendModeColor,
luminosity=kCGBlendModeLuminosity,
# nextstep
sourcein=kCGBlendModeSourceIn,
sourceout=kCGBlendModeSourceOut,
sourceatop=kCGBlendModeSourceAtop,
destinationover=kCGBlendModeDestinationOver,
destinationin=kCGBlendModeDestinationIn,
destinationout=kCGBlendModeDestinationOut,
destinationatop=kCGBlendModeDestinationAtop,
xor=kCGBlendModeXOR,
plusdarker=kCGBlendModePlusDarker,
pluslighter=kCGBlendModePlusLighter,
)
BLEND_MODES = """ normal, clear, copy, xor, multiply, screen,
overlay, darken, lighten, difference, exclusion,
color-dodge, color-burn, soft-light, hard-light,
hue, saturation, color, luminosity,
source-in, source-out, source-atop, plusdarker, pluslighter
destination-over, destination-in, destination-out, destination-atop"""
### Effects objects ###
class Frob(object):
"""A FoRmatting OBject encapsulates changes to the graphics context state.
It can be appended to the current canvas for a one-shot change or pushed onto the
canvas to perform a reset once the associated with block completes.
"""
_grobs = None
def append(self, grob):
if self._grobs is None:
self._grobs = []
self._grobs.append(grob)
def _draw(self):
# apply state changes only to contained grobs
with _cg_context(), self.applied():
if not self._grobs:
return
for grob in self._grobs:
grob._draw()
@property
def contents(self):
return self._grobs or []
class Effect(Frob):
kwargs = ('blend','alpha','shadow')
def __init__(self, *args, **kwargs):
self._fx = {}
if kwargs.pop('rollback', False):
self._rollback = {eff:getattr(_ctx._effects, eff) for eff in kwargs}
for eff, val in kwargs.items():
self._fx[eff] = Effect._validate(eff, val)
def __repr__(self):
return 'Effect(%r)'%self._fx
def __enter__(self):
# if this isn't the first pass through the context manager, snapshot the current
# state for all the effects we're changing so they can be restored in __exit__
if not hasattr(self, '_rollback'):
self._rollback = {eff:val for eff,val in _ctx._effects._fx.items() if eff in self._fx}
# concat ourseves as a new canvas container
_ctx.canvas.push(self)
# reset the global per-object effects state within the block (since the effects
# will be applied to a transparency layer encapsulating all drawing)
for eff in self._fx:
_ctx._effects._fx.pop(eff, None)
return
def __exit__(self, type, value, tb):
# step back out to the pre-effects canvas container
_ctx.canvas.pop()
# restore the per-object effects state to what it was before the `with` block
for eff, val in self._rollback.items():
setattr(_ctx._effects, eff, val)
del self._rollback
def set(self, *effs):
"""Add compositing effects to the drawing context"""
# apply effects specified in the args (or all by default)
if not effs:
effs = Effect.kwargs
fx = {k:v for k,v in self._fx.items() if k in effs}
if 'alpha' in fx:
CGContextSetAlpha(_cg_port(), fx['alpha']);
if 'blend' in fx:
CGContextSetBlendMode(_cg_port(), _BLEND[fx['blend']])
if 'shadow' in fx:
shadow = Shadow(None) if fx['shadow'] is None else fx['shadow']
shadow._nsShadow.set() # don't mess with cg for shadows
# i *think* it's better to skip the transparency layer when only blending,
# but am bracing for the discovery that it's not...
return bool(fx) and fx.keys() != ('blend',)
# return bool(fx) # return whether any state was just changed
@contextmanager
def applied(self):
"""Apply compositing effects (if any) to any drawing inside the `with` block"""
if self._fx:
if self.set('blend', 'alpha'):
with _cg_layer():
if not self.set('shadow'):
yield # if there's no shadow, we don't need a second layer
else:
with _cg_layer():
yield # if there is, we do
else:
# no blend or alpha changes, but since _fx exists there must be a shadow
self.set('shadow')
with _cg_layer():
yield
else:
# nothing to be done
yield
def copy(self):
new = Effect()
new._fx = dict(self._fx)
return new
@classmethod
def _validate(self, eff, val):
if val is None:
pass
elif eff=='alpha' and not (numlike(val) and 0<=val<=1):
badalpha = 'alpha() value must be a number between 0 and 1.0'
raise DeviceError(badalpha)
elif eff=='blend':
val = re.sub(r'[_\- ]','', val).lower()
if val not in _BLEND:
badblend = '"%s" is not a recognized blend mode.\nUse one of:\n%s'%(val, BLEND_MODES)
raise DeviceError(badblend)
elif eff=='shadow':
if isinstance(val, Shadow):
val = val.copy()
else:
val = Shadow(*val)
return val
def _get_alpha(self):
return self._fx.get('alpha', 1.0)
def _set_alpha(self, a):
if a is None:
self._fx.pop('alpha', None)
else:
self._fx['alpha'] = Effect._validate('alpha', a)
alpha = property(_get_alpha, _set_alpha)
def _get_blend(self):
return self._fx.get('blend', 'normal')
def _set_blend(self, mode):
if mode is None:
self._fx.pop('blend', None)
else:
self._fx['blend'] = Effect._validate('blend', mode)
blend = property(_get_blend, _set_blend)
def _get_shadow(self):
return self._fx.get('shadow', None)
def _set_shadow(self, spec):
if spec is None:
self._fx.pop('shadow', None)
else:
self._fx['shadow'] = Effect._validate('shadow', spec)
shadow = property(_get_shadow, _set_shadow)
class Shadow(object):
kwargs = ('color','blur','offset')
def __init__(self, *args, **kwargs):
super(Shadow, self).__init__()
if args and isinstance(args[0], Shadow):
self._nsShadow = _copy_attr(args[0]._nsShadow)
for attr, val in kwargs.items():
if attr not in Shadow.kwargs: continue
setattr(self, attr, val)
else:
self._nsShadow = NSShadow.alloc().init()
for attr, val in zip(Shadow.kwargs, args):
kwargs.setdefault(attr, val)
self.color = Color(kwargs.get('color', ('#000', .75)))
self.blur = kwargs.get('blur', 10 if self.color.a else 0)
offset = kwargs.get('offset', self.blur/2.0)
if numlike(offset):
offset = [offset]
if len(offset)==1:
offset *= 2
self.offset = offset
def __repr__(self):
return "Shadow(%r, blur=%r, offset=%r)" % (self.color, self.blur, tuple(self.offset))
def copy(self):
return Shadow(self)
def _get_color(self):
return Color(self._nsShadow.shadowColor())
def _set_color(self, spec):
if isinstance(spec, Color):
self._nsShadow.setShadowColor_(spec.nsColor)
elif spec is None:
self._nsShadow.setShadowColor_(None)
else:
if not isinstance(spec, (tuple,list)):
spec = tuple([spec])
self._nsShadow.setShadowColor_(Color(*spec).nsColor)
color = property(_get_color, _set_color)
def _get_blur(self):
return self._nsShadow.shadowBlurRadius()
def _set_blur(self, blur):
self._nsShadow.setShadowBlurRadius_(blur)
blur = property(_get_blur, _set_blur)
def _get_offset(self):
x,y = self._nsShadow.shadowOffset()
return Point(x,-y)
def _set_offset(self, offset):
if numlike(offset):
x = y = offset
else:
x,y = offset
self._nsShadow.setShadowOffset_((x,-y))
offset = property(_get_offset, _set_offset)
class Stencil(Frob):
def __init__(self, stencil, invert=False, channel=None):
from .text import Text
from .bezier import Bezier
from .image import Image
if isinstance(stencil, Text):
self.path = stencil.path
self.evenodd = invert
if isinstance(stencil, Bezier):
self.path = stencil.copy()
self.evenodd = invert
elif isinstance(stencil, Image):
# default to using alpha if available and darkness if not
if not channel:
channel = 'alpha' if stencil._nsBitmap.hasAlpha() else 'black'
if channel=='black':
invert = not invert
self.channel = channel
self.invert = invert
self.bmp = stencil
def set(self):
port = _cg_port()
if hasattr(self, 'path'):
path_xf = self.path._screen_transform
cg_path = path_xf.apply(self.path).cgPath
CGContextBeginPath(port)
if self.evenodd:
# if inverted, knock the path out of a full-screen rect and clip with that
CGContextAddRect(port, ((0,0),(_ctx.WIDTH, _ctx.HEIGHT)))
CGContextAddPath(port, cg_path)
CGContextEOClip(port)
else:
# otherwise just color between the lines
CGContextAddPath(port, cg_path)
CGContextClip(port)
elif hasattr(self, 'bmp'):
# run the filter chain and render to a cg-image
singlechannel = ciFilter(self.channel, self.bmp._ciImage)
greyscale = ciFilter(self.invert, singlechannel)
ci_ctx = CIContext.contextWithOptions_(None)
maskRef = ci_ctx.createCGImage_fromRect_(greyscale, ((0,0), self.bmp.size))
# turn the image into an ‘imagemask’ cg-image
cg_mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
CGImageGetHeight(maskRef),
CGImageGetBitsPerComponent(maskRef),
CGImageGetBitsPerPixel(maskRef),
CGImageGetBytesPerRow(maskRef),
CGImageGetDataProvider(maskRef), None, False);
# the mask is sitting at (0,0) until transformed to screen coords
xf = self.bmp._screen_transform
xf.concat() # apply transforms before clipping...
CGContextClipToMask(port, ((0,0), self.bmp.size), cg_mask)
xf.inverse.concat() # ...restore the previous state after
@contextmanager
def applied(self):
self.set()
yield
class ClippingPath(Stencil):
pass # NodeBox compat...
### core-image filters for channel separation and inversion ###
def ciFilter(opt, img):
_filt = _inversionFilter if isinstance(opt, bool) else _channelFilter
return _filt(opt, img)
def _channelFilter(channel, img):
"""Generate a greyscale image by isolating a single r/g/b/a channel"""
rgb = ('red', 'green', 'blue')
if channel=='alpha':
transmat = [(0, 0, 0, 1)] * 3
transmat += [ (0,0,0,0), (0,0,0,1) ]
elif channel in rgb:
rgb_row = [0,0,0]
rgb_row.insert(rgb.index(channel), 1.0)
transmat = [tuple(rgb_row)] * 3
transmat += [ (0,0,0,0), (0,0,0,1) ]
elif channel in ('black', 'white'):
transmat = [(.333, .333, .333, 0)] * 3
transmat += [ (0,0,0,0), (0,0,0,1) ]
return _matrixFilter(transmat, img)
def _inversionFilter(identity, img):
"""Conditionally turn black to white and up to down"""
# set up a matrix that's either identity or an r/g/b inversion
polarity = -1.0 if not identity else 1.0
bias = 0 if polarity>0 else 1
transmat = [(polarity, 0, 0, 0), (0, polarity, 0, 0), (0, 0, polarity, 0),
(0, 0, 0, 0), (bias, bias, bias, 1)]
return _matrixFilter(transmat, img)
def _matrixFilter(matrix, img):
"""Apply a color transform to a CIImage and return the filtered result"""
vectors = ("inputRVector", "inputGVector", "inputBVector", "inputAVector", "inputBiasVector")
opts = {k:CIVector.vectorWithX_Y_Z_W_(*v) for k,v in zip(vectors, matrix)}
opts[kCIInputImageKey] = img
remap = CIFilter.filterWithName_("CIColorMatrix")
for k,v in opts.items():
remap.setValue_forKey_(v, k)
return remap.valueForKey_("outputImage")