174 lines · 8.5 KB · PythonRaw
#!/usr/bin/env python3"""Draw the editing fan's command pictures into the app's catalogue. python3 scripts/draw-command-icons.py [name ...]Each glyph is an act's bare object, `act-<object>` — one of the kit's`ACT_OBJECTS` (crates/cotton-core/src/definitions/commands.rs), named bywhat it shows (tick, clock, cross, tray…) beside the painted objects (stamp,bin, folder…). Each is drawn here as the hand-inked glyphs of theediting set are (`edit-act-repeat`, `edit-act-status`): one marker in the set'sink, round ends, a rough dry edge, on a 512 px clear sheet with the glyph inits middle 56%. Written as the imageset `act-<name>` in CottonPaper'sMedia.xcassets; with names, only those are drawn."""import jsonimport mathimport pathlibimport randomimport sysfrom PIL import Image, ImageChops, ImageDraw, ImageFilterCATALOGUE = pathlib.Path(__file__).resolve().parent.parent / 'apps/loom/Sources/CottonPaper/Media.xcassets'INK = (59, 56, 53)SIDE = 512SCALE = 4WIDTH = 0.042 # the marker, as a share of the sheet (edit-act-status: 18–23 px)def arc(cx, cy, r, a0, a1, ry=None): """Points round an ellipse from a0 to a1 degrees (0 = right, 90 = down).""" ry = r if ry is None else ry steps = max(8, int(abs(a1 - a0) / 3)) return [(cx + r * math.cos(math.radians(a0 + (a1 - a0) * i / steps)), cy + ry * math.sin(math.radians(a0 + (a1 - a0) * i / steps))) for i in range(steps + 1)]def head(path, size=0.09, spread=40): """The chevron at a path's end, its two barbs back along the path.""" (x0, y0), (x1, y1) = path[-3], path[-1] angle = math.atan2(y1 - y0, x1 - x0) barb = lambda turn: [(x1 - size * math.cos(angle + math.radians(turn)), y1 - size * math.sin(angle + math.radians(turn))), (x1, y1)] return [barb(spread), barb(-spread)]def rounded(x0, y0, x1, y1, r): """A rounded rectangle as one closed path.""" return (arc(x0 + r, y0 + r, r, 180, 270) + arc(x1 - r, y0 + r, r, 270, 360) + arc(x1 - r, y1 - r, r, 0, 90) + arc(x0 + r, y1 - r, r, 90, 180) + [(x0, y0 + r)])def star(cx, cy, r_out, r_in): points = [] for i in range(11): r = r_out if i % 2 == 0 else r_in a = math.radians(-90 + 36 * i) points.append((cx + r * math.cos(a), cy + r * math.sin(a))) return pointsdef arrow(x0, y0, x1, y1, size=0.09): line = [(x0, y0), ((x0 + x1) / 2, (y0 + y1) / 2), (x1, y1)] return [line] + head(line, size)# Each glyph: strokes in the unit square, drawn in its middle (0.22–0.78).GLYPHS = { # Mark done, Approve, Keep, Star a message: the tick. 'tick': lambda: [[(0.24, 0.52), (0.42, 0.70), (0.78, 0.30)]], # Remind me, Re-key: the clock. 'clock': lambda: [arc(0.5, 0.5, 0.28, 0, 360), [(0.5, 0.33), (0.5, 0.5), (0.63, 0.59)]], # File, Back up, Add person: on, into another place. 'arrow': lambda: [[(0.62, 0.26), (0.78, 0.26), (0.78, 0.74), (0.62, 0.74)], *arrow(0.22, 0.5, 0.64, 0.5)], # Clear, Hide, Remove, Erase, Block, Junk: struck out. 'cross': lambda: [[(0.28, 0.28), (0.72, 0.72)], [(0.72, 0.28), (0.28, 0.72)]], # Decline, Not this, Refuse: down into the tray. 'tray': lambda: [[(0.22, 0.56), (0.22, 0.78), (0.78, 0.78), (0.78, 0.56)], *arrow(0.5, 0.22, 0.5, 0.63)], # Unfile: up out of the tray. 'send': lambda: [[(0.22, 0.56), (0.22, 0.78), (0.78, 0.78), (0.78, 0.56)], *arrow(0.5, 0.63, 0.5, 0.22)], # Unarchive, Restore, Reopen: back the way it came. 'undo': lambda: (lambda bend: [bend, *head(bend)])(arc(0.52, 0.52, 0.26, 70, -160)), # Try again: once more round. 'retry': lambda: (lambda bend: [bend, *head(bend)])(arc(0.5, 0.5, 0.27, 110, 400)), # Sync now: round and round. 'cycle': lambda: (lambda top, foot: [top, *head(top), foot, *head(foot)])( arc(0.5, 0.5, 0.27, 190, 335), arc(0.5, 0.5, 0.27, 10, 155)), # Open chat, Open person, Open from mail: out of the page. 'open': lambda: [[(0.48, 0.26), (0.24, 0.26), (0.24, 0.76), (0.74, 0.76), (0.74, 0.52)], *arrow(0.46, 0.54, 0.78, 0.22, 0.1)], 'pin': lambda: [rounded(0.36, 0.22, 0.64, 0.30, 0.03), [(0.40, 0.30), (0.38, 0.52)], [(0.60, 0.30), (0.62, 0.52)], [(0.28, 0.54), (0.72, 0.54)], [(0.5, 0.54), (0.5, 0.80)]], # Mute: the bell struck through. 'muted': lambda: [[(0.26, 0.68), (0.32, 0.60), *arc(0.5, 0.48, 0.18, 180, 360)[1:-1], (0.68, 0.60), (0.74, 0.68), (0.26, 0.68)], [(0.32, 0.60), (0.32, 0.48)], [(0.68, 0.60), (0.68, 0.48)], arc(0.5, 0.71, 0.07, 20, 160), [(0.5, 0.24), (0.5, 0.30)], [(0.22, 0.22), (0.78, 0.80)]], # Drop: the slip let fall to the floor. 'drop': lambda: [[(0.44, 0.20), (0.74, 0.30), (0.64, 0.56), (0.34, 0.46), (0.44, 0.20)], [(0.47, 0.33), (0.63, 0.39)], [(0.24, 0.56), (0.30, 0.66)], [(0.38, 0.62), (0.42, 0.70)], [(0.24, 0.80), (0.76, 0.80)]], 'person': lambda: [arc(0.5, 0.36, 0.13, 0, 360), arc(0.5, 0.80, 0.27, 180, 360, 0.24)], 'pencil': lambda: [[(0.24, 0.76), (0.28, 0.62), (0.64, 0.26), (0.74, 0.36), (0.38, 0.72), (0.24, 0.76)], [(0.56, 0.34), (0.66, 0.44)]], 'archive': lambda: [rounded(0.22, 0.26, 0.78, 0.40, 0.03), [(0.27, 0.40), (0.27, 0.76), (0.73, 0.76), (0.73, 0.40)], [(0.42, 0.52), (0.58, 0.52)]], 'lens': lambda: [arc(0.44, 0.44, 0.19, 0, 360), [(0.58, 0.58), (0.77, 0.77)]], 'page': lambda: [[(0.30, 0.22), (0.56, 0.22), (0.72, 0.38), (0.72, 0.78), (0.30, 0.78), (0.30, 0.22)], [(0.56, 0.22), (0.56, 0.38), (0.72, 0.38)], [(0.40, 0.54), (0.62, 0.54)], [(0.40, 0.66), (0.62, 0.66)]], 'bubble': lambda: [[*rounded(0.22, 0.26, 0.78, 0.64, 0.08)[:-1], (0.22, 0.34)], [(0.36, 0.64), (0.30, 0.78), (0.48, 0.64)]], 'picture': lambda: [rounded(0.22, 0.26, 0.78, 0.74, 0.04), [(0.30, 0.66), (0.44, 0.48), (0.56, 0.60), (0.62, 0.53), (0.71, 0.66)], arc(0.63, 0.38, 0.05, 0, 360)], 'copy': lambda: [rounded(0.36, 0.36, 0.76, 0.78, 0.04), [(0.26, 0.64), (0.24, 0.64), (0.24, 0.22), (0.62, 0.22), (0.64, 0.26)]], 'device': lambda: [rounded(0.33, 0.22, 0.67, 0.78, 0.06), [(0.46, 0.70), (0.54, 0.70)]], 'plus': lambda: [[(0.5, 0.24), (0.5, 0.76)], [(0.24, 0.5), (0.76, 0.5)]],}def draw(name): rng = random.Random(name) side = SIDE * SCALE mask = Image.new('L', (side, side)) pen = ImageDraw.Draw(mask) radius = WIDTH * side / 2 for stroke in GLYPHS[name](): points = [(x * side + rng.uniform(-3, 3), y * side + rng.uniform(-3, 3)) for x, y in stroke] # A marker's ink: a dab every few pixels, its width breathing a little. for (x0, y0), (x1, y1) in zip(points, points[1:]): steps = max(1, int(math.hypot(x1 - x0, y1 - y0) / 3)) for i in range(steps + 1): t = i / steps r = radius * (1 + 0.05 * math.sin((x0 + t * (x1 - x0) + y0 + t * (y1 - y0)) / 37)) x, y = x0 + t * (x1 - x0), y0 + t * (y1 - y0) pen.ellipse((x - r, y - r, x + r, y + r), fill=255) # The dry edge: the stroke's soft edge cut by paper grain. grain = Image.effect_noise((side, side), 90).filter(ImageFilter.GaussianBlur(2.2)) soft = mask.filter(ImageFilter.GaussianBlur(SCALE * 1.6)) edge = ImageChops.add(soft, grain, scale=1, offset=-128) edge = edge.point(lambda v: 0 if v < 105 else 255 if v > 150 else int((v - 105) * 255 / 45)) speck = Image.effect_noise((side, side), 40).point(lambda v: 255 if v > 70 else 205) alpha = ImageChops.multiply(edge, speck).resize((SIDE, SIDE), Image.LANCZOS) sheet = Image.new('RGBA', (SIDE, SIDE), INK + (0,)) sheet.putalpha(alpha) return sheetdef write(name, sheet): imageset = CATALOGUE / f'act-{name}.imageset' imageset.mkdir(exist_ok=True) target = imageset / f'act-{name}.png' sheet.save(target, optimize=True) contents = { 'images': [ {'filename': target.name, 'idiom': 'universal', 'scale': '3x'}, {'filename': target.name, 'idiom': 'mac', 'scale': '2x'}, ], 'info': {'author': 'xcode', 'version': 1}, 'properties': {'preserves-vector-representation': False}, } (imageset / 'Contents.json').write_text(json.dumps(contents, indent=2) + '\n') print(f'{name} -> {imageset.name}')def main(names): for name in names or GLYPHS: if name not in GLYPHS: raise SystemExit(f'{name}: not one of {sorted(GLYPHS)}') write(name, draw(name))if __name__ == '__main__': main(sys.argv[1:])