-
Notifications
You must be signed in to change notification settings - Fork 19
/
svg.py
1612 lines (1381 loc) · 47.8 KB
/
svg.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
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# encoding: utf-8
##### SVG.PY ######################################################################################
__version__ = '1.4'
__license__ = 'BSD'
__credits__ = ['Tom De Smedt', 'Guy De Pauw']
__email__ = '[email protected]'
__author__ = 'Textgain'
__copyright__ = 'Textgain'
###################################################################################################
# Copyright (c) 2016, Textgain
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation and/or
# other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
# WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
###################################################################################################
# svg.py is a pure-Python SVG renderer with functions for 2D drawing (e.g., colored lines & shapes),
# geometry (e.g., distance, angle, tangent, bounds) and transformation (e.g., rotate, scale).
import sys
import os
import io
import inspect
import colorsys
import codecs
import unicodedata
import re
import hashlib
import mimetypes
import base64
import struct
import random; _random=random
import math
import time
import operator
PY2 = sys.version.startswith('2')
#--------------------------------------------------------------------------------------------------
def dataurl(path, default='application/octet-stream'):
""" Returns the data URI string for the given file.
"""
type = mimetypes.guess_type(path)[0] or default
s = open(path, 'rb')
s = s.read()
s = base64.b64encode(s)
s = s.decode('utf-8')
s = 'data:%s;base64,%s' % (type, s)
return s
def sha(s, n=16):
""" Returns the crypytographic hash of the string.
"""
s = s.encode('utf-8')
s = hashlib.sha256(s)
s = s.hexdigest()
s = s[:n]
return s
#---- GEOMETRY ------------------------------------------------------------------------------------
from math import sqrt, atan2, cos, sin, degrees, radians
def angle(x0, y0, x1, y1):
""" Returns the angle between two points.
"""
return degrees(atan2(y1 - y0, x1 - x0)) # CCW
def distance(x0, y0, x1, y1):
""" Returns the distance between two points.
"""
return sqrt((x1 - x0) ** 2 + (y1 - y0) ** 2)
def coordinates(x, y, distance, angle):
""" Returns the point at distance and angle from x, y.
"""
return x + cos(radians(angle)) * distance, \
y + sin(radians(angle)) * distance
def polar(x, y):
""" Returns the point as a (distance, angle) vector.
"""
return distance(0, 0, x, y), \
angle(0, 0, x, y)
def lerp(t, x0, y0, x1, y1):
""" Returns the point at t (0.0-1.0) on the line between two points.
"""
# linear interpolation
return (1 - t) * x0 + x1 * t, \
(1 - t) * y0 + y1 * t
def berp(t, x0, y0, x1, y1, x2, y2, x3, y3, split=False):
""" Returns the point at t (0.0-1.0) on the Bézier curve.
"""
# repeat interpolation (De Casteljau)
ax, ay = lerp(t, x0, y0, x1, y1)
bx, by = lerp(t, x1, y1, x2, y2)
cx, cy = lerp(t, x2, y2, x3, y3)
dx, dy = lerp(t, ax, ay, bx, by)
ex, ey = lerp(t, bx, by, cx, cy)
fx, fy = lerp(t, dx, dy, ex, ey)
if split:
return (dx, dy, ex, ey, fx, fy), (ax, ay, cx, cy)
else:
return (dx, dy, ex, ey, fx, fy)
def smoothstep(v, min=0.0, max=1.0):
""" Returns a smooth transition (0.0-1.0) for v between min and max.
"""
# polynomial interpolation (Hermite)
if v < min:
return min
if v > max:
return max
else:
v = float(v - min) / (max - min)
v = v * v * (3 - 2 * v)
return v
def clamp(v, min=0.0, max=1.0):
""" Returns the value between min and max.
"""
if v < min:
return min
if v > max:
return max
else:
return v
def zoom(points=[], radius=1.0):
""" Returns the scaled list of points.
"""
p = [polar(x, y) for x, y in points]
r = radius / max(d for d, a in p)
p = [coordinates(0, 0, d * r, a) for d, a in p]
return p
def bounds(points=[]):
""" Returns the polygon's bounding rectangle (x, y, w, h).
"""
x0 = float('+inf')
y0 = float('+inf')
x1 = float('-inf')
y1 = float('-inf')
for x, y in points:
x0 = min(x0, x)
y0 = min(y0, y)
x1 = max(x1, x)
y1 = max(y1, y)
return x0, y0, x1 - x0, y1 - y0
def pip(x, y, points=[]):
""" Returns True if the point is inside the polygon.
"""
# ray casting
odd = False
n = len(points)
for i in range(n):
j = (i + 1) % n
x0 = points[i][0]
y0 = points[i][1]
x1 = points[j][0]
y1 = points[j][1]
if (y0 < y and y1 >= y) \
or (y1 < y and y0 >= y):
if x0 + (y-y0) / (y1-y0) * (x1-x0) < x:
odd = not odd
return odd
#---- ITERATION -----------------------------------------------------------------------------------
def polyline(x0, y0, x1, y1, x2, y2, x3, y3, n=10):
""" Returns an iterator of n+1 points (i.e., n line segments).
"""
for t in range(n + 1):
yield berp(float(t) / n, x0, y0, x1, y1, x2, y2, x3, y3)[-2:]
def pairwise(a):
""" Returns an iterator of consecutive pairs.
"""
# pairwise((1, 2, 3)) => (1, 2), (2, 3)
for v in a:
try:
yield prev, v
except UnboundLocalError:
pass
prev = v
def cumulative(a):
""" Returns an iterator of the cumulative sum.
"""
# cumulative((1, 2, 3)) => 1, 3, 6
n = 0
for v in a:
n += v
yield n
def relative(a):
""" Returns an iterator of relative values (sum 1).
"""
# relative((1, 2, 3)) => 0.16, 0.33, 0.5
a = list(a)
n = sum(a)
n = float(n) or 1
for v in a:
yield v / n
#---- NUMBERS -------------------------------------------------------------------------------------
# The number formatting functions are useful for chart visualizations.
if not PY2:
unicode = str
class short(unicode):
""" Returns the given number as a short string.
"""
# short(1234) => 1.2K
def __new__(cls, v, precision=1, format=None):
if isinstance(v, (str, unicode)) and v.isdigit():
v = int(v)
if isinstance(v, (str, unicode)):
v = float(v)
if v >= 1000 or type(v) is float:
f = '%.*f'.replace('*', str(precision))
else:
f = '%i'
if v < 1e+3:
n = 1, ''
if v >= 1e+3:
n = 1e+3, 'K'
if v >= 1e+6 - 2e+5:
n = 1e+6, 'M'
if v >= 1e+9 - 2e+8:
n = 1e+9, 'B'
if format == '%':
n = 1e-2, '%'
if format == '*':
n = 1, u'○○○'
if format == '*' and v >= 1e+1:
n = 1, u'●○○'
if format == '*' and v >= 1e+2:
n = 1, u'●●○'
if format == '*' and v >= 1e+3:
n = 1, u'●●●'
s = v / n[0]
s = f % s
s = s if format != '*' else ''
s = s + '.'
s = s.split('.')
s = s[0], s[1].rstrip('0')
s = '.'.join(s)
s = s.rstrip('.')
s = s + n[1]
s = unicode.__new__(cls, s) # '1.1K'
s.v = v # 1100
s.n = n[0] # 1000.0
s.u = n[1] # 'K'
s.p = precision # 1
s.f = format # None
return s
def __add__(self, v):
return short(self.v + v, self.p, self.f)
def __sub__(self, v):
return short(self.v - v, self.p, self.f)
def __int__(self):
return int(float(self))
def __float__(self):
return self.v * self.n
def rate(v, precision=0):
return short(v, precision, format='%')
def rank(v, precision=0):
return short(v, precision, format='*')
def ceil(v, m=1.0):
""" Returns the upper rounded float,
by order of magnitude.
"""
# ceil(12 , 1.0) => 20 (10 , 20 , ... )
# ceil(123, 1.0) => 200 (100, 200, ... )
# ceil(1.3, 0.7) => 1.4 (0.7, 1.4, ... )
# ceil(0.3, 0.2) => 0.4 (0.2, 0.4, ... )
n = abs(v) or 1
n = math.log10(n)
n = math.pow(10, int(n)) * m
v = math.ceil(v / n) * n
return v
def floor(v, m=1.0):
""" Returns the lower rounded float,
by order of magnitude.
"""
# floor(123, 1.0) => 100
n = abs(v) or 1
n = math.log10(n)
n = math.pow(10, int(n)) * m
v = math.floor(v / n) * n
return v
def circa(v, step=0.05, ease=True):
""" Returns the nearest round float.
"""
# circa(0.03) => 0.03
# circa(0.04) => 0.04
# circa(0.06) => 0.05
# circa(0.07) => 0.05
# circa(0.08) => 0.10
# circa(0.09) => 0.10
n = math.log10(step)
n = min(n, 0)
n = abs(n)
n = int(n) + 1
v = float(v)
if ease and v <= step:
return round(v, n)
else:
return round(
round(v / step) * step, n + 1)
ca = circa
def format(v):
""" Returns a number string format,
by order of magnitude.
"""
if type(v) is short:
return '%s'
if abs(v) > 2 and float(v).is_integer():
return '%.0f'
if abs(v) > 100:
return '%.0f' # 0, 100, 200, ...
if abs(v) > 2:
return '%.1f' # 0, 0.1, 0.2, ...
else:
return '%.2f'
def steps(v1, v2):
""" Returns steps from int v1 to v2.
"""
r = abs(v2 - v1)
r = str(r).strip('0.')
r = int(r or 0)
if r == 1:
return 5
if not r % 5: # 0, 5, 10, 15, 20, 25
return 5
if not r % 4: # 0, 6, 12, 18, 24
return 4
if not r % 3: # 0, 7, 14, 21
return 3
else:
return 2
#---- STATISTICS ----------------------------------------------------------------------------------
def avg(a):
""" Returns the average (mean) of the given values.
"""
a = list(a)
n = len(a) or 1
return sum(a) / float(n)
def sd(a):
""" Returns the standard deviation of given values.
"""
a = list(a)
n = len(a) or 1
m = avg(a)
return sqrt(sum((v - m) ** 2 for v in a) / n)
def peaks(a, z=1):
""" Returns a list of indices of values that are
more than z standard deviations above the mean.
"""
a = list(a)
m = avg(a)
s = sd(a) or 1
p = ((v - m) / s for v in a)
p = (i for i, v in enumerate(p) if v > z)
p = sorted(p, key=lambda i: -a[i])
return p
# print(peaks([0, 0, 0, 10, 100, 1, 0], z=1))
#---- COLOR ---------------------------------------------------------------------------------------
# The Color object represents shape fill and stroke (outline) colors in terms of R, G, B, A values.
# The Color object can also be created with H, S, B values (i.e., hue, saturation, brightness), and
# rotated on the RYB color wheel (painter's model) to find aesthetical color combinations.
ops = {
'+': operator.add,
'*': operator.mul,
}
RGB = 'rgb'
HSB = 'hsb'
RYB = (
0.000, # 0° = H 0.00
0.022, # 15° = H 0.02
0.047, # 30° = H 0.05
0.072, # 45° = H 0.07 ...
0.094,
0.114,
0.133,
0.150,
0.167,
0.225,
0.286,
0.342,
0.383,
0.431,
0.475,
0.519,
0.567,
0.608,
0.65 ,
0.697,
0.742,
0.783,
0.828,
0.914,
1.000,
)
class Color(object):
def __init__(self, *v, **k):
""" A color with RGBA values between 0.0-1.0.
"""
if v and isinstance(v[0], (list, tuple)): # Color(list)
v = v[0]
if len(v) == 0: # Color()
r, g, b, a = 0, 0, 0, 0
elif len(v) == 1 and v[0] is None: # Color(None)
r, g, b, a = 0, 0, 0, 0
elif len(v) == 1 and isinstance(v[0], Color): # Color(Color)
r, g, b, a = v[0].r, v[0].g, v[0].b, v[0].a
elif len(v) == 1: # Color(k)
r, g, b, a = v[0], v[0], v[0], 1
elif len(v) == 2: # Color(k, a)
r, g, b, a = v[0], v[0], v[0], v[1]
elif len(v) == 3: # Color(r, g, b)
r, g, b, a = v[0], v[1], v[2], 1
elif len(v) == 4: # Color(r, g, b, a)
r, g, b, a = v[0], v[1], v[2], v[3]
if k.get('mode') == HSB: # Color(h, s, b, a, mode=HSB)
r, g, b = colorsys.hsv_to_rgb(r, g, b)
n = k.get('base', 1)
self.r = float(r) / n
self.g = float(g) / n
self.b = float(b) / n
self.a = float(a) / n
def __eq__(self, clr):
return self.rgba == clr.rgba
@property
def rgb(self):
return self.r, self.g, self.b
@property
def rgba(self):
return self.r, self.g, self.b, self.a
@property
def hsba(self):
return colorsys.rgb_to_hsv(self.r, self.g, self.b) + (self.a,)
def rotate(self, angle=180):
""" Returns the color rotated on the RYB color wheel.
"""
# 1) Get the angle of the hue on the RYB color wheel:
h, s, b, a = self.hsba
i = next(i for i, v in enumerate(RYB) if v > h)
x = RYB[i-1]
y = RYB[i]
t = (h - x) / (y - x)
d = angle + i * 15 - 15 * (1 - t)
# 2) Get the hue at the new angle:
i = int(d // 15)
x = RYB[i]
y = RYB[i+1]
t = d % 15 / 15
h = x + t * (y - x)
return Color(h, s, b, a, mode=HSB)
def __str__(self):
return 'rgba(%.0f, %.0f, %.0f, %.2f)' % (
self.r * 255,
self.g * 255,
self.b * 255,
self.a
)
def __repr__(self):
return 'Color(r=%.2f, g=%.2f, b=%.2f, a=%.2f)' % (
self.r,
self.g,
self.b,
self.a
)
def __iter__(self):
return iter((
self.r,
self.g,
self.b,
self.a
))
TRANSPARENT, BLACK, WHITE = Color(0, 0), Color(0), Color(1)
def luminance(clr):
""" Returns the perceived brightness of the color.
"""
return clr.r * 0.3 + clr.g * 0.6 + clr.b * 0.1 # Y
def darker(clr, t=0.1):
""" Returns the color mixed with black.
"""
return Color(*(mix(t, clr, BLACK).rgb) + (clr.a,))
def lighter(clr, t=0.1):
""" Returns the color mixed with white.
"""
return Color(*(mix(t, clr, WHITE).rgb) + (clr.a,))
def complement(clr):
""" Returns the complementary color.
"""
return clr.rotate(180)
def adjust(clr, h=None, s=None, b=None, a=None, contrast=1.0, op='+'):
""" Returns the adjusted color.
"""
# 1) Adjust coloring:
clr = zip(clr.hsba, (h, s, b, a))
clr = map(lambda v: v[0] if v[1] is None else ops[op](*v), clr)
clr = map(clamp, clr)
clr = Color(*clr, mode=HSB)
# 2) Adjust contrast:
clr = zip(clr.rgba, (contrast, contrast, contrast, 1))
clr = map(lambda v: v[0] * v[1] - v[1] * 0.5 + 0.5, clr)
clr = map(clamp, clr)
clr = Color(*clr, mode=RGB)
return clr
def mix(t, clr1, clr2, **k):
""" Returns the interpolated color between the given colors at t (0.0-1.0).
"""
return Color(*((1 - t) * v1 + v2 * t for v1, v2 in zip(clr1, clr2)), **k)
class Gradient(list):
def __init__(self, clr1, clr2, *clr3, angle=0):
""" A smooth transition between the given colors.
"""
self.extend((clr1, clr2) + clr3)
self.angle = angle
def __call__(self, n=100):
""" Returns an iterator of n interpolated colors.
"""
for i in range(n):
try:
yield self.at(t=i / float(n - 1))
except ZeroDivisionError:
yield self.at(t=0.5)
def at(self, t):
""" Returns an interpolated color at t (0.0-1.0).
"""
if t <= 0:
return self[0]
if t >= 1:
return self[-1]
n = len(self) - 1
t = divmod(t, 1.0 / n)
i = int(t[0])
t = n * t[1]
return mix(t, self[i], self[i+1])
def __str__(self):
n = len(self)
n = float(n) - 1
s = '<linearGradient gradientTransform="rotate(%.1f 0.5 0.5)">\n' % self.angle
for i, clr in enumerate(self):
s += '<stop offset="%.2f"' % (i / n)
s += ' stop-color="%s" />' % clr
s += '\n'
s += '</linearGradient>\n'
return s
def __repr__(self):
return 'Gradient(%s)' % tuple(self)
def mono(clr):
""" Returns a monochromatic gradient.
"""
h, s, b, a = clr.hsba
s = s * 0.6
s = s + 0.3
g1 = Color(h, 1.00, 0.30, a, mode=HSB) # deep
g2 = Color(h, s , 0.90, a, mode=HSB)
g3 = Color(h, 0.30, 1.00, a, mode=HSB) # pale
g = Gradient(g1, g2, g3)
return g
def swatch(clr, n=10, f=mono):
""" Returns a list of colors.
"""
if isinstance(clr, Color) and n <= 1:
clr = [clr]
if isinstance(clr, Color):
clr = f(clr)
if isinstance(clr, Gradient):
clr = list(clr(n))
if isinstance(clr, list):
clr = list(clr) + clr[-1:] * (n - len(clr)) # repeat last
return clr
#---- FONT ----------------------------------------------------------------------------------------
# The font table defines the width of ASCII characters at size 100px, for a select number of fonts:
EMOJI = {
u'😊', u'☺️', u'😉', u'😌', u'😏', u'😎', u'😍', u'😘', u'😴', u'😀', u'😃', u'😄',
u'😅', u'😇', u'😂', u'😭', u'😢', u'😱', u'😳', u'😬', u'😜', u'😛', u'😁', u'😐',
u'😕', u'🤔', u'😧', u'😦', u'😒', u'😞', u'😔', u'😫', u'😩', u'😠', u'😡', u'🤢',
u'❤️', u'💔', u'💘', u'💕', u'👍', u'👎', u'🙏', u'👊', u'🖕', u'☝️', u'🔫', u'💣',
}
CHARS = {ch: i for i, ch in enumerate(map(chr, range(32, 127)))} # {' ': 0, '!': 1, ...}
FONTS = {
# ! " # $ % & ' ( ) * + , - . / 0 1 2 3 4
('Arial', 'normal'): [
28, 28, 36, 56, 56, 89, 67, 19, 33, 33, 39, 58, 28, 33, 28, 28, 56, 56, 56, 56, 56,
56, 56, 56, 56, 56, 28, 28, 58, 58, 58, 56, 99, 67, 67, 72, 72, 67, 61, 78, 72, 28,
50, 67, 56, 83, 72, 78, 67, 78, 72, 67, 61, 72, 67, 95, 67, 67, 61, 28, 28, 28, 47,
56, 33, 56, 56, 50, 56, 56, 28, 56, 56, 22, 22, 50, 22, 83, 56, 56, 56, 56, 33, 50,
28, 56, 50, 72, 50, 50, 50, 33, 26, 33, 58, 0],
('Arial', 'bold'): [
28, 33, 48, 56, 56, 89, 72, 24, 33, 33, 39, 58, 28, 33, 28, 28, 56, 56, 56, 56, 56,
56, 56, 56, 56, 56, 33, 33, 58, 58, 58, 61, 98, 72, 72, 72, 72, 67, 61, 78, 72, 28,
56, 72, 61, 83, 72, 78, 67, 78, 72, 67, 61, 72, 67, 95, 67, 67, 61, 33, 28, 33, 58,
56, 33, 56, 61, 56, 61, 56, 33, 61, 61, 28, 28, 56, 28, 89, 61, 61, 61, 61, 39, 56,
33, 61, 56, 78, 56, 56, 50, 39, 28, 39, 58, 0],
('Times New Roman', 'normal'): [
25, 33, 41, 50, 50, 83, 78, 18, 33, 33, 50, 56, 25, 33, 25, 28, 50, 50, 50, 50, 50,
50, 50, 50, 50, 50, 28, 28, 56, 56, 56, 45, 92, 72, 67, 67, 72, 61, 56, 72, 72, 33,
39, 72, 61, 89, 72, 72, 56, 72, 67, 56, 61, 72, 72, 95, 72, 72, 61, 33, 28, 33, 47,
50, 33, 45, 50, 45, 50, 45, 33, 50, 50, 28, 28, 50, 28, 78, 50, 50, 50, 50, 33, 39,
28, 50, 50, 72, 50, 50, 45, 48, 20, 48, 54, 0],
('Times New Roman', 'bold'): [
25, 33, 56, 50, 50, 99, 83, 28, 33, 33, 50, 57, 25, 33, 25, 28, 50, 50, 50, 50, 50,
50, 50, 50, 50, 50, 33, 33, 57, 57, 57, 50, 93, 72, 67, 72, 72, 67, 61, 78, 78, 39,
50, 78, 67, 95, 72, 78, 61, 78, 72, 56, 67, 72, 72, 99, 72, 72, 67, 33, 28, 33, 58,
50, 33, 50, 56, 45, 56, 45, 33, 50, 56, 28, 33, 56, 28, 83, 56, 50, 56, 56, 45, 39,
33, 56, 50, 72, 50, 50, 45, 40, 22, 40, 52, 0],
}
def textsize(s, fontname, fontsize, fontweight='normal'):
""" Returns the approximate (width, height) of the given string.
"""
fontsize = round(fontsize) # 9.9 => '10px'
if isinstance(s, u''.__class__):
b = s
f = unicodedata.combining
b = unicodedata.normalize('NFKD', b) # 'touché' => 'touche´'
b = ''.join(c for c in b if not f(c))
b = b.replace(u'“', '"')
b = b.replace(u'”', '"')
b = b.replace(u'‘', "'")
b = b.replace(u'’', "'")
b = b.replace(u'ø', 'o')
b = b.encode('ascii', 'replace')
b = b.decode('utf-8')
else:
b = s # # PY2 str
b = b.replace('\n', ' ')
m = FONTS[fontname, fontweight]
w = fontsize * 0.01 * sum(m[CHARS.get(ch, -1)] for ch in b) + \
fontsize * 1.00 * sum(1 for e in EMOJI if e in s) - \
fontsize * 0.15 * s.count(u' ') # NARROW NO-BREAK SPACE
h = fontsize
return w, h
# print(textsize(u'touché 😁', 'Arial', 10))
def truncate(s, width, font, fontsize, fontweight='normal', placeholder='...'):
""" Returns the string up to the given width.
"""
f = lambda s: textsize(s, font, fontsize, fontweight)[0] > width
if f(s):
s = s + placeholder
while s != placeholder and f(s):
s = s[:-len(placeholder)]
s = s[:-1]
s = s + placeholder
if f(s):
return ''
else:
return s
# print truncate('supercalifragilisticexpialidocious', 100, 'Arial', 12)
#---- BÉZIER PATH ---------------------------------------------------------------------------------
MOVETO, LINETO, CURVETO, CLOSE = 'moveto', 'lineto', 'curveto', 'close'
class Point(object):
def __init__(self, x, y):
self.x = x
self.y = y
def __iter__(self):
return iter((
self.x,
self.y
))
class PathElement(Point):
def __init__(self, cmd, *pt):
""" A point in a path, with control points for curve segments.
"""
if cmd == MOVETO:
pt = pt * 3
if cmd == LINETO:
pt = pt * 3
if cmd == CLOSE:
pt = (0, 0, 0, 0, 0, 0)
self.cmd = cmd
self.ctrl1 = Point(pt[0], pt[1])
self.ctrl2 = Point(pt[2], pt[3])
self.x = pt[4]
self.y = pt[5]
def __str__(self):
if self.cmd == MOVETO:
return 'M%.1f,%.1f' % (self.x, self.y)
if self.cmd == LINETO:
return 'L%.1f,%.1f' % (self.x, self.y)
if self.cmd == CURVETO:
return 'C%.1f,%.1f,%.1f,%.1f,%.1f,%.1f' % tuple(self)
if self.cmd == CLOSE:
return 'Z'
def __repr__(self):
return 'PathElement(cmd=%s, x=%.1f, y=%.1f)' % (
self.cmd.upper(),
self.x,
self.y
)
def __iter__(self):
return iter((
self.ctrl1.x,
self.ctrl1.y,
self.ctrl2.x,
self.ctrl2.y,
self.x,
self.y
))
class BezierPath(list):
def moveto(self, x, y):
""" Adds a new point to the path at x, y.
"""
self.append(PathElement(MOVETO, x, y))
def lineto(self, x, y):
""" Adds a line from the previous point to x, y.
"""
self.append(PathElement(LINETO, x, y))
def curveto(self, ctrl1x, ctrl1y, ctrl2x, ctrl2y, x, y):
""" Adds a curve from the previous point to x, y,
with control points that define the curvature.
"""
self.append(PathElement(CURVETO, ctrl1x, ctrl1y, ctrl2x, ctrl2y, x, y))
def close(self):
""" Adds a line from the previous point to the last MOVETO.
"""
self.append(PathElement(CLOSE))
def rect(self, x, y, w, h):
""" Adds a rectangle to the path.
"""
self.moveto(x, y)
self.lineto(x + w, y)
self.lineto(x + w, y + h)
self.lineto(x, y + h)
self.lineto(x, y)
def ellipse(self, x, y, w, h):
""" Adds an ellipse to the path.
"""
w = w * 0.5
h = h * 0.5
i = w * 0.552 # 4 / 3 * (√2 - 1)
j = h * 0.552
self.moveto(x - w, y)
self.curveto(x - w, y - j, x - i, y - h, x, y - h)
self.curveto(x + i, y - h, x + w, y - j, x + w, y)
self.curveto(x + w, y + j, x + i, y + h, x, y + h)
self.curveto(x - i, y + h, x - w, y + j, x - w, y)
self.close()
@property
def length(self):
""" Returns the length of the path.
"""
return sum(self.lengths)
@property
def lengths(self):
""" Returns the length of the path segments.
"""
for pt in self:
if pt.cmd == MOVETO:
yield 0.0; o=pt
if pt.cmd == LINETO:
yield distance(x, y, pt.x, pt.y)
if pt.cmd == CURVETO: # rectification
yield sum(distance(*(pt1 + pt2)) for pt1, pt2 in pairwise(polyline(x, y, *pt, n=20)))
if pt.cmd == CLOSE:
yield distance(x, y, o.x, o.y)
if pt.cmd != CLOSE:
x = pt.x
y = pt.y
def point(self, t, _lengths=None):
""" Returns a synthetic PathElement at t (0.0-1.0) on the path.
"""
for (t1, pt1), (t2, pt2) in segmented(self, _lengths):
if pt1.cmd == MOVETO:
o = pt1
if pt2.cmd == MOVETO:
o = pt2
if t1 <= t <= t2:
t = (t - t1) / (t2 - t1 or 1)
if pt2.cmd == LINETO:
return PathElement(pt2.cmd, *lerp(t, pt1.x, pt1.y, pt2.x, pt2.y))
if pt2.cmd == CURVETO:
return PathElement(pt2.cmd, *berp(t, pt1.x, pt1.y, *pt2))
if pt2.cmd == CLOSE:
return PathElement(pt2.cmd, *lerp(t, pt1.x, pt1.y, o.x, o.y))
def points(self, n=10, t1=0.0, t2=1.0):
""" Returns an iterator of n synthetic PathElements on the path.
"""
cached = list(self.lengths)
for t in range(n):
# n=1: t 0.0
# n=2: t 0.0, 1.0
# n=3: t 0.0, 0.5, 1.0
t = t / float(n - 1 if n > 1 else n)
t = t * (t2 - t1) + t1
if 0 <= t <= 1:
yield self.point(t, _lengths=cached)
else:
return
def __repr__(self):
return 'BezierPath(length=%.0f)' % self.length
Path = BezierPath
def segmented(path, _lengths=None):
""" Returns an iterator of consecutive (t1, pt1), (t2, pt2) pairs,
where t1 and t2 is the relative start and end of each segment.
"""
p = _lengths or path.lengths # (100, 300, 600)
p = relative(p) # (0.1, 0.3, 0.6)
p = cumulative(p) # (0.1, 0.4, 1.0)
p = zip(p, path)
p = pairwise(p)
return p
# p = Path()
# p.moveto(100, 100)
# p.lineto(200, 200)
# p.lineto(300, 300)
# p.close()
# drawpath(p, stroke=color(0), strokewidth=1)
# for pt in p.points(p):
# ellipse(pt.x, pt.y, 3, 3)
def fit(points=[], k=0.5):
""" Returns a path from the list of (x, y) tuples, with curvature k.
"""
p = Path()
a = list(map(tuple, points))
k = max(k, 0.0)
k = min(k, 1.0) * 100
if len(a) > 0:
dx0, dy0 = a[0]
p.moveto(dx0, dy0)
for (x0, y0), (x1, y1), (x2, y2) in zip(a, a[1:], a[2:]):
r1 = angle(x1, y1, x0, y0)
r2 = angle(x1, y1, x2, y2)
r = (r2 + r1) / 2
w = (r2 < r1) and -1 or +1
dx1, dy1 = coordinates(x1, y1, k, r - 90 * w)
p.curveto(dx0, dy0, dx1, dy1, x1, y1)
dx0, dy0 = coordinates(x1, y1, k, r + 90 * w)
if len(a) > 1:
p.curveto(dx0, dy0, *a[-1] * 2)
return p
#---- FILTER -------------------------------------------------------------------------------------
class Filter(str):
def __new__(cls, s):
return str.__new__(cls,
'<filter color-interpolation-filters="sRGB">\n%s\n</filter>' % s.strip())
class blur(Filter):
def __new__(cls, radius=10):
""" Returns a filter that renders the element with a blur.
"""
return Filter.__new__(cls,
'<feGaussianBlur stdDeviation="%.2f" />' % radius)
class dropshadow(Filter):
def __new__(cls, dx=10, dy=10, radius=10):
""" Returns a filter that renders the element with a blurred shadow.
"""
return Filter.__new__(cls,
'<feDropShadow stdDeviation="%.2f" dx="%.1f" dy="%.1f" />' % (radius, dx, dy))
class colorize(Filter):
def __new__(cls, h=0.0, s=1.0, b=1.0, contrast=1.0):
""" Returns a filter that adjusts the element's colors.
"""
b = -contrast * 0.5 + 0.5, \
contrast * b
return Filter.__new__(cls, '\n'.join((
'<feColorMatrix type="hueRotate" values="%.2f" />' % (h * 360),
'<feColorMatrix type="saturate" values="%.2f" />' % s,
'<feComponentTransfer>',
'<feFuncR type="linear" intercept="%.2f" slope="%.2f" />' % b,
'<feFuncG type="linear" intercept="%.2f" slope="%.2f" />' % b,
'<feFuncB type="linear" intercept="%.2f" slope="%.2f" />' % b,
'</feComponentTransfer>')))
class blend(Filter):
def __new__(cls, clr, mode='color'):
""" Returns a filter that adjusts the element's color (monotone).
"""