-
Notifications
You must be signed in to change notification settings - Fork 41
/
boxDrawing.py
930 lines (759 loc) · 22.4 KB
/
boxDrawing.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
#MenuTitle: boxDrawing
import math
from boxDrawingRecipes import recipes
__doc__ = '''
This script will draw the Unicode ranges "Box Drawing Characters" (U+2500 to
U+257F) and "Block Elements" (U+2580 to U+259F). It makes use of the FontParts
Python library (https://github.com/robotools/fontParts).
The script was successfully tested in RoboFont, Glyphs and on the command line.
It is possible to run this script straight from the command line, given that
the FontParts Python module is installed. The drawing itself is done using
combinations of simple drawing instructions; listed in the external
boxDrawingRecipes.py module.
'''
# ----------------------------------------------------------------
# Edit these values if you want (currently set to match Source Code Pro):
WIDTH = 600 # Glyph width.
HEIGHT = 1400 # Height for line elements, including overlap.
MEDIAN = 300 # Median line.
STROKE = 160 # General stroke weight.
FAT = 2 # Multiplication factor for drawing 'fat' strokes.
RADIUS = WIDTH / 2 # Radius for arc elements.
BLOCK_HEIGHT = 1400 # Height for block elements.
EM_HEIGHT = 1200 # Height for elements that don't connect
# vertically, such as dashed strokes.
FAT_STROKE = STROKE * FAT # STROKE thickness for 'fat' lines.
BUTT = STROKE # Horizontal overlap.
# Bezier point distance for drawing circles.
KAPPA = 4 * (math.sqrt(2) - 1) / 3
# These values are for block elements,
# and are dependent of the values above:
BLOCK_ORIGIN = (0, MEDIAN - BLOCK_HEIGHT / 2)
BLOCK_TOP = (WIDTH, MEDIAN + BLOCK_HEIGHT / 2)
# Nothing below here _needs_ to be edited, but feel free to do so:
# ----------------------------------------------------------------
# Checking which application we are in:
IN_RF = False
IN_FL = False
IN_GLYPHS = False
IN_SHELL = False
if not any((IN_RF, IN_FL, IN_GLYPHS)):
try:
import mojo.roboFont
IN_RF = True
except ImportError:
pass
if not any((IN_RF, IN_FL, IN_GLYPHS)):
try:
import GlyphsApp
IN_GLYPHS = True
except ImportError:
pass
if not any((IN_RF, IN_FL, IN_GLYPHS)):
try:
import flsys
IN_FL = True
except ImportError:
pass
if not any((IN_RF, IN_FL, IN_GLYPHS)):
IN_SHELL = True
import os
import time
from fontParts.fontshell import RFont
if IN_GLYPHS:
try:
import GSPen
from objectsGS import CurrentFont
except ImportError:
print(
'The files GSPen.py and objectsGS.py are needed for '
'Robofab to work in Glyphs. Please get them at '
'https://github.com/schriftgestalt/Glyphs-Scripts')
try:
test = getattr(GSLayer, "removeOverlap")
if not callable(test):
raise
except:
raise AttributeError(
'Please update your objectsGS.py file. '
'Download the latest verion at: '
'https://github.com/schriftgestalt/Glyphs-Scripts')
# Check if a font is open -- if not, create a new one.
if IN_SHELL:
f = RFont()
else:
f = CurrentFont()
if f is None:
f = RFont()
if f is not None and IN_GLYPHS:
Font.disableUpdateInterface()
def roundInt(float):
return int(round(float))
def floatRange(x, y, step):
'''
Variation on range(), since step values for dashed lines may be floats.
'''
while x < y:
yield x
x += step
def drawRect(pen, BL, BR, TR, TL):
'''
General drawing function for a rectangle.
'''
pen.moveTo(BL)
pen.lineTo(BR)
pen.lineTo(TR)
pen.lineTo(TL)
pen.closePath()
def drawPoly(pen, *coords):
'''
General drawing function for a polygon.
'''
if len(set(coords)) >= 3:
pen.moveTo(coords[0])
for pointIndex, pointCoords in enumerate(coords):
# print pointCoords
if pointIndex > 0:
if not pointCoords == coords[pointIndex - 1]:
pen.lineTo(pointCoords)
pen.closePath()
def drawArc(
pen, start1, start2, end1, end2,
IAstart, IApoint1, IApoint2, IAend,
OAstart, OApoint1, OApoint2, OAend
):
'''
General drawing function for an arc.
'''
pen.moveTo(start1)
pen.lineTo(start2)
pen.lineTo(IAstart)
pen.curveTo(IApoint1, IApoint2, IAend)
pen.lineTo(end1)
pen.lineTo(end2)
pen.lineTo(OAstart)
pen.curveTo(OApoint1, OApoint2, OAend)
pen.closePath()
def horLine(pen, start, end, stroke, buttL=BUTT, buttR=BUTT):
'''
General drawing function for a horizontal line.
'''
startX = start[0]
startY = start[1]
endX = end[0]
endY = end[1]
BL = (startX - buttL / 2, endY - stroke / 2)
BR = (endX + buttR / 2, endY - stroke / 2)
TR = (endX + buttR / 2, endY + stroke / 2)
TL = (startX - buttL / 2, startY + stroke / 2)
drawRect(pen, BL, BR, TR, TL)
def vertLine(pen, start, end, stroke, buttB=0, buttT=0):
'''
General drawing function for a vertical line.
'''
startX = start[0]
startY = start[1]
endY = end[1]
BL = (startX - stroke / 2, startY - buttB / 2)
BR = (startX + stroke / 2, startY - buttB / 2)
TR = (startX + stroke / 2, endY + buttT / 2)
TL = (startX - stroke / 2, endY + buttT / 2)
drawRect(pen, BL, BR, TR, TL)
def box(pen, start=BLOCK_ORIGIN, end=BLOCK_TOP):
'''
A box.
'''
startX = start[0]
startY = start[1]
endX = end[0]
endY = end[1]
BL = (startX, startY)
BR = (endX, startY)
TR = (endX, endY)
TL = (startX, endY)
drawRect(pen, BL, BR, TR, TL)
def dashedHorLine(pen, step, width=WIDTH, stroke=STROKE):
'''
Dashed horizontal bar.
'''
stepLength = width / step
gap = stepLength / step
for w in floatRange(0, width, stepLength):
if w + stepLength - gap < width:
w = w + gap / 2 # centering the dashed line in the glyph
horLine(
boxPen,
(w, MEDIAN),
(w + stepLength - gap, MEDIAN),
stroke,
buttL=0,
buttR=0
)
def dashedVertLine(pen, step, length=EM_HEIGHT, stroke=STROKE):
'''
Dashed vertical bar.
'''
stepLength = length / step
gap = stepLength / step
top = MEDIAN + EM_HEIGHT / 2
for h in floatRange(MEDIAN - length / 2, MEDIAN + length / 2, stepLength):
if h + stepLength - gap < top:
h += gap / 2
vertLine(
boxPen,
(WIDTH / 2, h),
(WIDTH / 2, h + stepLength - gap),
stroke
)
def dot(pen, center, radius):
'''
A dot.
'''
x, y = center
pen.moveTo(
(x - radius, y)
)
pen.curveTo(
(x - radius, y - radius * KAPPA),
(x - radius * KAPPA, y - radius),
(x, y - radius)
)
pen.curveTo(
(x + radius * KAPPA, y - radius),
(x + radius, y - radius * KAPPA),
(x + radius, y)
)
pen.curveTo(
(x + radius, y + radius * KAPPA),
(x + radius * KAPPA, y + radius),
(x, y + radius)
)
pen.curveTo(
(x - radius * KAPPA, y + radius),
(x - radius, y + radius * KAPPA),
(x - radius, y)
)
pen.closePath()
def polkaShade(pen, shade):
'''
Shading patterns, consisting of polka dots.
Not used in any of the drawing recipes, but perhaps useful for somebody.
'''
vstep = 100
hstep = 200
if shade == '25':
radius = 24
if shade == '50':
radius = 36
if shade == '75':
radius = 54
for w in range(0, WIDTH, hstep):
for h in range(
roundInt(MEDIAN - BLOCK_HEIGHT / 2),
roundInt(MEDIAN + BLOCK_HEIGHT / 2),
vstep * 2
):
dot(
boxPen,
(w, h),
radius
)
dot(
boxPen,
(w + hstep / 2, h + vstep),
radius * 1.5
)
def shade(pen, shade):
'''
Shading patterns, consisting of little boxes.
Not used in any of the drawing recipes, but maybe useful for somebody.
Reliable way to crash makeOTF (in 2016).
'''
vstep = 50
hstep = 100
if shade == '25':
boxWidth = 20
boxHeight = 30
if shade == '50':
boxWidth = 40
boxHeight = 50
if shade == '75':
boxWidth = 45
boxHeight = 70
for w in range(0, WIDTH, hstep):
for h in range(
MEDIAN - BLOCK_HEIGHT / 2,
MEDIAN + BLOCK_HEIGHT / 2,
vstep * 2
):
box(
boxPen,
(w, h),
(w + boxWidth, h + boxHeight)
)
box(
boxPen,
(w + hstep / 2, h + vstep),
(w + boxWidth + hstep / 2, h + boxHeight + vstep)
)
def stripedShade(pen, shade):
'''
Shading patterns, consisting of diagonal lines.
This function assumes a bunch of right triangles being moved across
the width of the glyph. The law of sines is used for start- and end
point calculations.
'''
if shade == '25':
step = WIDTH / 3
if shade == '50':
step = WIDTH / 6
if shade == '75':
step = WIDTH / 12
stroke = WIDTH / 30
# angle = math.asin(2 / math.hypot(1, 2)) # 1 : 2 ratio
angle = math.radians(45) # 1 : 1 ratio
yShift = MEDIAN - BLOCK_HEIGHT / 2
hypotenuse = BLOCK_HEIGHT / math.sin(angle)
# leftmost point:
leftmost_x = 0 - math.cos(angle) * hypotenuse - stroke
xValues = []
for xValue in floatRange(leftmost_x, WIDTH + stroke, step):
xValues.append(xValue)
xValues.append(xValue + stroke)
drawList = []
for (raw_x1, raw_x2) in zip(xValues[:-1:2], xValues[1::2]):
bot_x1 = roundInt(raw_x1)
bot_x2 = roundInt(raw_x2)
top_x1 = roundInt(raw_x1 + hypotenuse * math.cos(angle))
top_x2 = roundInt(raw_x2 + hypotenuse * math.cos(angle))
bot_y1 = 0
bot_y2 = 0
top_y1 = BLOCK_HEIGHT
top_y2 = BLOCK_HEIGHT
if bot_x1 <= 0:
bot_x1 = 0
bot_y1 = roundInt(math.tan(angle) * abs(raw_x1))
if bot_x2 <= 0:
bot_x2 = 0
bot_y2 = roundInt(math.tan(angle) * abs(raw_x2))
if top_x1 >= WIDTH:
top_x1 = WIDTH
top_y1 = roundInt(math.tan(angle) * abs(raw_x1 - WIDTH))
if top_x2 >= WIDTH:
top_x2 = WIDTH
top_y2 = roundInt(math.tan(angle) * abs(raw_x2 - WIDTH))
if top_y1 <= bot_y1:
top_y1 = bot_y1 = BLOCK_HEIGHT
if top_x1 <= bot_x1:
top_x1 = bot_x1 = WIDTH
if bot_x2 >= WIDTH:
bot_x2 = WIDTH
top_y2 = 0
stripe = (
(bot_x1, bot_y1),
(bot_x2, bot_y2),
(top_x2, top_y2),
(top_x1, top_y1),
)
drawList.append(shiftCoords(stripe, 0, yShift))
for (BL, BR, TR, TL) in drawList:
drawPoly(pen, BL, BR, TR, TL)
def verticalShade(pen, shade):
'''
Boring shading patterns, consisting of vertical lines.
'''
if shade == '25':
step = WIDTH / 3
if shade == '50':
step = WIDTH / 6
if shade == '75':
step = WIDTH / 12
stroke = WIDTH / 30
for xValue in floatRange(0, WIDTH, step):
y_bot = MEDIAN - HEIGHT / 2
y_top = y_bot + HEIGHT
x_left = xValue
x_rght = xValue + stroke
drawRect(
pen,
(x_left, y_bot),
(x_rght, y_bot),
(x_rght, y_top),
(x_left, y_top)
)
def diagonal(pen, start, end, direction):
'''
Diagonal line in two possible directions; either bottomUp or topDown.
'''
diagonalLength = math.hypot(WIDTH, EM_HEIGHT)
angle1 = math.asin(WIDTH / diagonalLength)
angle2 = math.pi / 2 - angle1
xDist = STROKE / 2 / math.cos(angle1)
yDist = STROKE / 2 / math.cos(angle2)
startX = start[0]
startY = start[1]
endX = end[0]
endY = end[1]
TL1 = (startX + xDist, startY)
TL2 = (startX, startY)
TL3 = (startX, startY - yDist)
BR1 = (endX - xDist, endY)
BR2 = (endX, endY)
BR3 = (endX, endY + yDist)
BL1 = (startX, startY + yDist)
BL2 = (startX, startY)
BL3 = (startX + xDist, startY)
TR1 = (endX, endY - yDist)
TR2 = (endX, endY)
TR3 = (endX - xDist, endY)
if direction == 'topDown':
pen.moveTo(TL1)
pen.lineTo(TL2)
pen.lineTo(TL3)
pen.lineTo(BR1)
pen.lineTo(BR2)
pen.lineTo(BR3)
pen.closePath()
if direction == 'bottomUp':
pen.moveTo(BL1)
pen.lineTo(BL2)
pen.lineTo(BL3)
pen.lineTo(TR1)
pen.lineTo(TR2)
pen.lineTo(TR3)
pen.closePath()
def arc(pen, start, end, side, stroke, radius, butt=0):
'''
Rounded corner.
'''
if side == 'TL':
yflip = 1
xflip = 1
if side == 'BL':
yflip = -1
xflip = 1
if side == 'TR':
yflip = 1
xflip = -1
if side == 'BR':
yflip = -1
xflip = -1
startX = start[0]
startY = start[1]
endX = end[0]
endY = end[1]
cStartX = startX
cStartY = endY - (radius * yflip)
cEndX = startX + (radius * xflip)
cEndY = endY
start1 = (startX - (stroke / 2 * xflip), startY)
start2 = (startX + (stroke / 2 * xflip), startY)
end1 = (endX + (butt / 2 * xflip), endY - (stroke / 2 * yflip))
end2 = (endX + (butt / 2 * xflip), endY + (stroke / 2 * yflip))
IAstart = (cStartX + (stroke / 2 * xflip), cStartY)
IApoint1 = (
cStartX + (stroke / 2 * xflip),
cStartY + ((radius - stroke / 2) * KAPPA * yflip)
)
IApoint2 = (
cEndX - ((radius - stroke / 2) * KAPPA * xflip),
cEndY - (stroke / 2 * yflip)
)
IAend = (cEndX, cEndY - (stroke / 2 * yflip))
OAstart = (cEndX, cEndY + (stroke / 2 * yflip))
OApoint1 = (
cEndX - ((radius + stroke / 2) * KAPPA * xflip),
cEndY + (stroke / 2 * yflip)
)
OApoint2 = (
cStartX - (stroke / 2 * xflip),
cStartY + ((radius + stroke / 2) * KAPPA * yflip)
)
OAend = (cStartX - (stroke / 2 * xflip), cStartY)
drawArc(
pen,
start1, start2,
end1, end2,
IAstart,
IApoint1, IApoint2,
IAend,
OAstart,
OApoint1, OApoint2,
OAend
)
def horBar(fatness=1, median=MEDIAN, buttL=BUTT, buttR=BUTT):
'''
Horizontal bar.
'''
horLine(
boxPen,
(0, median),
(WIDTH, median),
STROKE * fatness,
buttL, buttR
)
def vertBar(fatness=1, buttB=0, buttT=0):
'''
Vertical bar.
'''
vertLine(
boxPen,
(WIDTH / 2, MEDIAN - HEIGHT / 2),
(WIDTH / 2, MEDIAN + HEIGHT / 2),
STROKE * fatness,
buttB, buttT
)
def horHalfBar(side, fatness=1, median=MEDIAN, buttL=BUTT, buttR=BUTT):
'''
Halfwidth horizontal bar, left or right.
'''
if side == 'left':
if buttR == BUTT != STROKE:
buttR = 0
horLine(
boxPen,
(0, median),
(WIDTH / 2, median),
STROKE * fatness,
buttL, buttR
)
elif side == 'right':
if buttL == BUTT != STROKE:
buttL = 0
horLine(
boxPen,
(WIDTH / 2, median),
(WIDTH, median),
STROKE * fatness,
buttL, buttR
)
def vertHalfBar(fold, fatness=1, buttB=0, buttT=0):
'''
Half-height vertical bar, top or bottom.
'''
if fold == 'top':
vertLine(
boxPen,
(WIDTH / 2, MEDIAN),
(WIDTH / 2, MEDIAN + HEIGHT / 2),
STROKE * fatness,
buttB, buttT
)
if fold == 'bottom':
vertLine(
boxPen,
(WIDTH / 2, MEDIAN - HEIGHT / 2),
(WIDTH / 2, MEDIAN),
STROKE * fatness,
buttB, buttT
)
def horSplitBar(fatness=1, buttL=BUTT, buttR=BUTT):
'''
Double-stroked horizontal bar, left or right.
'''
topMedian = MEDIAN + STROKE * fatness
bottomMedian = MEDIAN - STROKE * fatness
horBar(fatness, topMedian, buttL, buttR)
horBar(fatness, bottomMedian, buttL, buttR)
def vertSplitBar(fatness=1, buttB=0, buttT=0):
'''
Double-stroked vertical bar, top or bottom.
'''
leftX = WIDTH / 2 - (STROKE * fatness)
rightX = WIDTH / 2 + (STROKE * fatness)
vertLine(
boxPen,
(leftX, MEDIAN - HEIGHT / 2),
(leftX, MEDIAN + HEIGHT / 2),
STROKE * fatness,
buttB, buttT
)
vertLine(
boxPen,
(rightX, MEDIAN - HEIGHT / 2),
(rightX, MEDIAN + HEIGHT / 2),
STROKE * fatness,
buttB, buttT
)
def horSplitHalfBar(side, fatness=1, buttL=BUTT, buttR=BUTT):
'''
Double-stroked halfwidth horizontal bar, left or right.
'''
topMedian = MEDIAN + STROKE * fatness
bottomMedian = MEDIAN - STROKE * fatness
horHalfBar(side, fatness, topMedian, buttL, buttR)
horHalfBar(side, fatness, bottomMedian, buttL, buttR)
def vertSplitHalfBar(fold, fatness=1, buttB=0, buttT=0):
'''
Double-stroked half-height vertical bar, top or bottom.
'''
leftX = WIDTH / 2 - STROKE * fatness
rightX = WIDTH / 2 + STROKE * fatness
if fold == 'top':
vertLine(
boxPen,
(leftX, MEDIAN),
(leftX, MEDIAN + HEIGHT / 2),
STROKE * fatness,
buttB, buttT
)
vertLine(
boxPen,
(rightX, MEDIAN),
(rightX, MEDIAN + HEIGHT / 2),
STROKE * fatness,
buttB, buttT
)
if fold == 'bottom':
vertLine(
boxPen,
(leftX, MEDIAN - HEIGHT / 2),
(leftX, MEDIAN),
STROKE * fatness,
buttB, buttT
)
vertLine(
boxPen,
(rightX, MEDIAN - HEIGHT / 2),
(rightX, MEDIAN),
STROKE * fatness,
buttB, buttT
)
def outerCorner(side, fold, fatness=1, cornerMedian=MEDIAN):
'''
Outer part of a double-stroked corner.
'''
if fold == 'top':
cornerMedian -= STROKE * fatness
if fold == 'bottom':
cornerMedian += STROKE * fatness
if side == 'right':
horHalfBar(
side,
median=cornerMedian,
buttL=3 * STROKE,
buttR=BUTT
)
x = WIDTH / 2 - STROKE * fatness
if side == 'left':
horHalfBar(
side,
median=cornerMedian,
buttL=BUTT,
buttR=3 * STROKE
)
x = WIDTH / 2 + STROKE * fatness
if fold == 'top':
cornerMedian += STROKE * fatness
vertLine(
boxPen,
(x, cornerMedian),
(x, cornerMedian + HEIGHT / 2),
STROKE * fatness,
buttB=3 * STROKE
)
if fold == 'bottom':
cornerMedian -= STROKE * fatness
vertLine(
boxPen,
(x, cornerMedian - HEIGHT / 2),
(x, cornerMedian),
STROKE * fatness,
buttT=3 * STROKE
)
def innerCorner(side, fold, fatness=1, cornerMedian=MEDIAN):
'''
Inner part of a double-stroked corner.
'''
if fold == 'top':
cornerMedian += STROKE * fatness
if fold == 'bottom':
cornerMedian -= STROKE * fatness
if side == 'right':
horHalfBar(
side,
median=cornerMedian,
buttL=-1 * STROKE,
buttR=BUTT
)
x = WIDTH / 2 + STROKE * fatness
if side == 'left':
horHalfBar(
side,
median=cornerMedian,
buttL=BUTT,
buttR=-1 * STROKE
)
x = WIDTH / 2 - STROKE * fatness
if fold == 'top':
cornerMedian -= STROKE * fatness
vertLine(
boxPen,
(x, cornerMedian),
(x, cornerMedian + HEIGHT / 2),
STROKE * fatness,
buttB=-1 * STROKE
)
if fold == 'bottom':
cornerMedian += STROKE * fatness
vertLine(
boxPen,
(x, cornerMedian - HEIGHT / 2),
(x, cornerMedian),
STROKE * fatness,
buttT=-1 * STROKE
)
def shiftCoords(coordList, xShift=0, yShift=0):
return [(x + xShift, y + yShift) for (x, y) in coordList]
if f is not None:
print('Drawing boxes ...')
generatedGlyphs = []
# Keeping track of the glyph order
for name, uni in sorted(recipes, key=lambda x: int(x[1], 16)):
# sorting the dictionary by the Unicode value of the glyph.
generatedGlyphs.append(name)
commands = recipes[name, uni]
print(name)
g = f.newGlyph(name, clear=True)
g.width = WIDTH
g.unicode = int(uni, 16)
boxPen = g.getPen()
for command in commands:
exec(command)
if not IN_SHELL:
g.removeOverlap()
g.correctDirection()
g.str = int(uni, 16)
if not any([IN_GLYPHS, IN_FL]):
g.changed()
else:
g.update()
if not any([IN_GLYPHS, IN_FL]):
f.changed()
else:
f.update()
if IN_SHELL:
timeString = time.strftime("%Y-%m-%d_%H%M%S", time.localtime())
fileName = '%s_boxes.ufo' % timeString
# fileName = 'boxes.ufo'
f.lib['public.glyphOrder'] = generatedGlyphs
outputPath = os.sep.join((os.path.curdir, fileName))
f.save(outputPath)
print('\nFind your UFO file at %s' % os.path.abspath(outputPath))
if IN_FL:
fl.UpdateFont(fl.ifont)
if IN_RF:
# Modifying the glyph order, so it looks like the glyphs
# have been appended at the end of the font.
glyphOrder = f.lib['public.glyphOrder']
if not set(generatedGlyphs) <= set(glyphOrder):
oldGlyphOrder = [g for g in glyphOrder if g not in generatedGlyphs]
newGlyphOrder = oldGlyphOrder + generatedGlyphs
f.glyphOrder = newGlyphOrder
f.lib['public.glyphOrder'] = newGlyphOrder
if IN_GLYPHS:
# Update glyph names to comply with Glyphs' standard.
for glyphName in generatedGlyphs:
Font.glyphs[glyphName].updateGlyphInfo()
Font.enableUpdateInterface()
print('\nDone.')