Skip to content

Commit 23c2ec9

Browse files
committed
refine code
1 parent 193316a commit 23c2ec9

2 files changed

Lines changed: 24 additions & 19 deletions

File tree

xkcd-script/generator/pt5_svg_to_font.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
import parse
1919
import unicodedata
2020

21+
SPACE = 0
22+
RSPACE = 20
23+
2124
fnames = sorted(glob.glob('../generated/characters/char_*.svg'))
2225

2326
characters = []
@@ -237,9 +240,8 @@ def pad_glyph(c):
237240
# Put horizontal padding around the glyph. I choose a number here that looks reasonable,
238241
# there are far more sophisticated means of doing this (like looking at the original image,
239242
# and calculating how much space there should be).
240-
space = 0
241-
rspace = 20
242-
c.font['_pad_space'].width = space + rspace
243+
space = SPACE
244+
rspace = RSPACE
243245
bbox = c.boundingBox()
244246
if c.glyphname in list('gjpqy'):
245247
# Recalculate the bounding box by excluding the tail of the glyph
@@ -332,6 +334,7 @@ def charname(char):
332334
# Information to be conveyed to the next stage.
333335
# I wanted to use font.persistent, but it causes an error. Instead, I use a dummy glyph.
334336
font.createChar(-1, '_pad_space')
337+
font['_pad_space'].width = SPACE + RSPACE
335338

336339
# Per-character size scaling applied after changeWeight, to fine-tune individual glyphs
337340
# that end up slightly too large despite correct stroke weight.

xkcd-script/generator/pt7_font_properties.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def getkern(left, right):
114114
return None
115115

116116
a = font['_pad_space'].width
117-
a = max(a - 20, 0)
117+
a = a - 20
118118

119119
# The same combination will be overwritten, so the one written last will take effect.
120120
# autoKern looks at the outline, so even if you change the padding, it absorbs all of it.
@@ -124,6 +124,8 @@ def getkern(left, right):
124124
kern(60+a, ['s'], set(lower) - {'i', 'j', 'f', 't', 'x'}, onlyCloser=True, damper=0.75) # loosen by damper
125125
# Overwrite sf and st. (From experience, it is often just right to adopt the larger of the two
126126
# separation required by the glyphs on the left and right.)
127+
# The horizontal bars of 'r' and 'f' get caught between the dot and the stem of 'i', causing
128+
# unexpected behavior, so 'i' and 'j' are excluded.
127129
kern(80+a, set(lower) - {'i', 'j'}, ['f', 't'], onlyCloser=True, damper=0.75)
128130
kern(90+a, set(lower) - {'i', 'j'}, ['x'], onlyCloser=True, damper=0.75) # kx is fine, fx is tight
129131
kern(80+a, ['x'], set(lower) - {'i', 'j'}, onlyCloser=True, damper=0.75)
@@ -135,29 +137,29 @@ def getkern(left, right):
135137
kern(100+a + diff_ro_re, ['r'], ['e'], onlyCloser=True, damper=0.75)
136138
# including uppercase
137139
# Set *Y altogether first: CY, OY, etc. will have appropriate values set in the latter part.
138-
kern(105, roman, ['Y', 'T'], onlyCloser=True, damper=0.75)
139-
kern(100, caps, ['f'], onlyCloser=True, damper=0.75)
140+
kern(105+a, roman, ['Y', 'T'], onlyCloser=True, damper=0.75)
141+
kern(100+a, caps, ['f'], onlyCloser=True, damper=0.75)
140142
# F/E are separated from T/J so they can use a tighter target gap.
141-
kern(110, ['F'], set(roman) - {'j'}, onlyCloser=True, damper=0.75) # keep FO≈-60
143+
kern(110+a, ['F'], set(roman) - {'j'}, onlyCloser=True, damper=0.75) # keep FO≈-60
142144
# Since F and z mesh together and the kerning becomes too large,
143145
# reuse the kerning value of one of the round letterforms.
144146
diff_Fo_Fz = getkern('F', 'o') - getkern('F', 'z')
145-
kern(110 + int(diff_Fo_Fz / 0.75), ['F'], ['z'], onlyCloser=True, damper=0.75)
146-
kern(90, ['E'], set(roman) - {'j'}, onlyCloser=True, damper=0.75) # keep ES≈-30
147-
kern(45, ['E'], ['V'], onlyCloser=True, touch=True)
148-
kern(115, ['T', 'J'], set(roman) - {'j'}, onlyCloser=True, damper=0.75) # keep Tr≈-105
149-
kern(105, ['Y'], set(roman) - {'j'}, onlyCloser=True, damper=0.75)
150-
kern(85, ['V'], caps, onlyCloser=True, damper=0.75)
147+
kern(110+a + int(diff_Fo_Fz / 0.75), ['F'], ['z'], onlyCloser=True, damper=0.75)
148+
kern(90+a, ['E'], set(roman) - {'j'}, onlyCloser=True, damper=0.75) # keep ES≈-30
149+
kern(45+a, ['E'], ['V'], onlyCloser=True, touch=True)
150+
kern(115+a, ['T', 'J'], set(roman) - {'j'}, onlyCloser=True, damper=0.75) # keep Tr≈-105
151+
kern(105+a, ['Y'], set(roman) - {'j'}, onlyCloser=True, damper=0.75)
152+
kern(85+a, ['V'], caps, onlyCloser=True, damper=0.75)
151153
# C: loosen from the default (was too tight for Cj).
152154
# Compared to E, the lower curve of C tends to come close to the next character,
153155
# but this is considered an intentional design.
154-
kern(60, ['C'], set(roman) - {'j'}, onlyCloser=True, damper=0.75) # keep CK≈-15
155-
kern(25, ['C'], ['V'], onlyCloser=True, touch=True)
156-
kern(60, ['O'], set(roman) - {'j'}, onlyCloser=True, damper=0.75) # loosen
157-
kern(100, ['P'], set(roman) - {'j'}, onlyCloser=True, damper=0.75)
156+
kern(60+a, ['C'], set(roman) - {'j'}, onlyCloser=True, damper=0.75) # keep CK≈-15
157+
kern(25+a, ['C'], ['V'], onlyCloser=True, touch=True)
158+
kern(60+a, ['O'], set(roman) - {'j'}, onlyCloser=True, damper=0.75) # loosen
159+
kern(100+a, ['P'], set(roman) - {'j'}, onlyCloser=True, damper=0.75)
158160
diff_Po_Pe = getkern('P', 'o') - getkern('P', 'e')
159-
kern(100 + int(diff_Po_Pe / 0.75), ['P'], ['e'], onlyCloser=True, damper=0.75)
160-
kern(35, ['L'], set(roman) - {'j'}, onlyCloser=True, touch=True)
161+
kern(100+a + int(diff_Po_Pe / 0.75), ['P'], ['e'], onlyCloser=True, damper=0.75)
162+
kern(35+a, ['L'], set(roman) - {'j'}, onlyCloser=True, touch=True)
161163

162164

163165
autokern(font)

0 commit comments

Comments
 (0)