File: strokefont.py

package info (click to toggle)
bedstead 3.252-1
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 560 kB
  • sloc: ansic: 4,373; python: 337; makefile: 133; sh: 71
file content (96 lines) | stat: -rw-r--r-- 3,128 bytes parent folder | download | duplicates (2)
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
# SPDX-License-Identifier: CC0-1.0

from __future__ import print_function

import fontforge
from math import radians
from psMat import translate
from sys import argv

class Stroker(object):
    def __init__(self, fontname):
        self.fontname = fontname
        self.dotwidth = max(0, 100 - self.nibwidth)
        self.dotheight = max(0, 100 - self.nibheight)
    def modify_font(self, f):
        f.strokedfont = False

        for g in f.glyphs():
            for i, c in enumerate(list(g.layers[1])):
                newc = self.adjust_contour(c)
                if newc != None: g.layers[1] += newc
            g.stroke(*self.nib)
            g.removeOverlap()
            g.addExtrema()
            g.transform(translate(0, self.nibheight/2.0))

        f.familyname = self.familyname
        f.fontname = self.fontname
        f.fullname = self.fullname
        f.appendSFNTName('English (US)', 'Preferred Family', self.familyname)
        f.private['StdHW'] = self.nibheight
        f.private['StdVW'] = self.nibwidth
        def adjustblue(y): return y - 100 + self.nibheight
        bv = list(f.private['BlueValues'])
        bv[1:] = map(adjustblue, bv[1:])
        f.private['BlueValues'] = tuple(bv)
        f.uwidth = self.nibheight
        f.os2_weight = self.ttfweight
        f.weight = self.weight
        return f
    def adjust_contour(self, c):
        # This function just expands dots.
        if len(c) != 1: return None
        if self.dotwidth == 0 and self.dotheight == 0: return None
        newc = fontforge.contour()
        newc.moveTo(c[0].x + self.dotwidth/2, c[0].y)
        newc.lineTo(c[0].x, c[0].y + self.dotheight/2)
        newc.lineTo(c[0].x - self.dotwidth/2, c[0].y)
        newc.lineTo(c[0].x, c[0].y - self.dotheight/2)
        newc.closed = True
        return newc
        
class Plotter(Stroker):
    familyname = "Bedstead Plotter"
    def __init__(self, penwidth, weight, ttfweight):
        self.nib = ['circular', penwidth, 'round', 'round']
        self.nibwidth = self.nibheight = penwidth
        self.fontname = "BedsteadPlotter-" + weight
        self.fullname = "%s %s" % (self.familyname, weight)
        self.ttfweight = ttfweight
        self.weight = weight
        super(Plotter, self).__init__("BedsteadPlotter-" + weight)

class Chiseltip(Stroker):
    familyname = "Bedstead Chiseltip"
    fullname = "Bedstead Chiseltip"
    weight = "Medium"
    ttfweight = 500
    def __init__(self):
        chisel = fontforge.contour()
        chisel.moveTo(-56, 7)
        chisel.lineTo(39, 41)
        chisel.lineTo(56, -7)
        chisel.lineTo(-39, -41)
        chisel.closed = True
        self.nib = ['polygonal', chisel]
        self.nibwidth = 112
        self.nibheight = 82
        super(Chiseltip, self).__init__("BedsteadChiseltip")

modes = {
    'plotter-thin': Plotter(10, "Thin", 100),
    'plotter-light': Plotter(50, "Light", 300),
    'plotter-medium': Plotter(100, "Medium", 500),
    'plotter-bold': Plotter(150, "Bold", 700),
    'chiseltip': Chiseltip(),
}

mode = modes[argv[1]]

f = fontforge.open(argv[2])

mode.modify_font(f)

f.save(argv[3])