Debian Codegraph @

googlefonts-qa/scripts/set-vertical-metrics.py

raw
1 #MenuTitle: Set Vertical Metric Params
2 # -*- coding: utf-8 -*-
3
4 font = Glyphs.font
5
6 print(font)
7
8 __doc__="""
9 Assumes the masters keep the same vertical metrics.
10
11 I am not sure whether winAscent and winDescent should be different between masters,
12 otherwise, but you should check if that's the case before using this script
13 on a font where min/max heights are different between styles.
14
15 Useful when created; may or may not match the latest Google Fonts vertical metrics standards.
16 """
17
18 font = Glyphs.font
19
20
21
22 caps = ["A", "Aacute", "Abreve", "Acircumflex", "Adieresis", "Agrave", "Amacron", "Aogonek", "Aring", "Aringacute", "Atilde", "AE", "AEacute", "B", "C", "Cacute", "Ccaron", "Ccedilla", "Ccircumflex", "Cdotaccent", "D", "Eth", "Dcaron", "Dcroat", "Ddotbelow", "E", "Eacute", "Ebreve", "Ecaron", "Ecircumflex", "Edieresis", "Edotaccent", "Edotbelow", "Egrave", "Emacron", "Eogonek", "Etilde", "F", "G", "Gbreve", "Gcircumflex", "Gcommaaccent", "Gdotaccent", "H", "Hbar", "Hcircumflex", "Hdotbelow", "I", "IJ", "Iacute", "Ibreve", "Icircumflex", "Idieresis", "Idotaccent", "Idotbelow", "Igrave", "Imacron", "Iogonek", "Itilde", "J", "Jcircumflex", "K", "Kcommaaccent", "L", "Lacute", "Lcaron", "Lcommaaccent", "Ldot", "Lslash", "M", "N", "Nacute", "Ncaron", "Ncommaaccent", "Ndotaccent", "Eng", "Ntilde", "O", "Oacute", "Obreve", "Ocircumflex", "Odieresis", "Odotbelow", "Ograve", "Ohungarumlaut", "Omacron", "Oogonek", "Oslash", "Oslashacute", "Otilde", "OE", "P", "Thorn", "Q", "R", "Racute", "Rcaron", "Rcommaaccent", "Rdotbelow", "S", "Sacute", "Scaron", "Scircumflex", "Sdotbelow", "Schwa", "T", "Tbar", "Tcaron", "Tdotbelow", "U", "Uacute", "Ubreve", "Ucircumflex", "Udieresis", "Udotbelow", "Ugrave", "Uhungarumlaut", "Umacron", "Uogonek", "Uring", "Utilde", "V", "W", "Wacute", "Wcircumflex", "Wdieresis", "Wgrave", "X", "Y", "Yacute", "Ycircumflex", "Ydieresis", "Ygrave", "Ytilde", "Z", "Zacute", "Zcaron", "Zdotaccent", "Zdotbelow", "uni015E", "uni0162", "uni01C4", "uni01C5", "uni01C7", "uni01C8", "uni01CA", "uni01CB", "uni01F1", "uni01F2", "uni0218", "uni021A" ]
23 lowercase = ["a", "aacute", "abreve", "acircumflex", "adieresis", "agrave", "amacron", "aogonek", "aring", "aringacute", "atilde", "ae", "aeacute", "b", "c", "cacute", "ccaron", "ccedilla", "ccircumflex", "cdotaccent", "d", "eth", "dcaron", "dcroat", "ddotbelow", "e", "eacute", "ebreve", "ecaron", "ecircumflex", "edieresis", "edotaccent", "edotbelow", "egrave", "emacron", "eogonek", "etilde", "schwa", "f", "g", "gbreve", "gcircumflex", "gcommaaccent", "gdotaccent", "h", "hbar", "hcircumflex", "hdotbelow", "i", "dotlessi", "iacute", "ibreve", "icircumflex", "idieresis", "idotbelow", "igrave", "ij", "imacron", "iogonek", "itilde", "j", "dotlessj", "jcircumflex", "k", "kcommaaccent", "kgreenlandic", "l", "lacute", "lcaron", "lcommaaccent", "ldot", "lslash", "m", "n", "nacute", "napostrophe", "ncaron", "ncommaaccent", "ndotaccent", "eng", "ntilde", "o", "oacute", "obreve", "ocircumflex", "odieresis", "odotbelow", "ograve", "ohungarumlaut", "omacron", "oogonek", "oslash", "oslashacute", "otilde", "oe", "p", "thorn", "q", "r", "racute", "rcaron", "rcommaaccent", "rdotbelow", "s", "sacute", "scaron", "scircumflex", "sdotbelow", "germandbls", "t", "tbar", "tcaron", "tdotbelow", "u", "uacute", "ubreve", "ucircumflex", "udieresis", "udotbelow", "ugrave", "uhungarumlaut", "umacron", "uni015F", "uni0163", "uni01C6", "uni01C9", "uni01CC", "uni01F3", "uni0219", "uni021B", "uogonek", "uring", "utilde", "v", "w", "wacute", "wcircumflex", "wdieresis", "wgrave", "x", "y", "yacute", "ycircumflex", "ydieresis", "ygrave", "ytilde", "z", "zacute", "zcaron", "zdotaccent", "zdotbelow", "c_t", "f_b", "f_f", "f_f_b", "f_f_h", "f_f_i", "f_f_j", "f_f_k", "f_f_l", "f_f_t", "f_h", "f_i", "f_j", "f_k", "f_l", "f_t", "s_t", ]
24
25 # starter values
26 mainMaxDescent = 0
27 mainMaxDescentGlyph = ""
28 maxDescent = 0
29 mainMaxAscent = 0
30 mainMaxAscentGlyph = ""
31 maxAscent = 0
32
33 # find highest and lowest point in font
34 for glyph in font.glyphs:
35
36 # get total yMax and yMin, for win values
37 for layer in glyph.layers:
38
39 # get descender of current layer
40 descent = layer.bounds.origin.y
41
42 # get ascender of current layer
43 ascent = layer.bounds.size.height + descent
44
45 # if descent/ascent of current layer is greater than previous max descents/ascents, update the max descent/ascent
46 if descent <= maxDescent:
47 maxDescent = descent
48 maxDescentGlyph = glyph.name
49
50 if ascent >= maxAscent:
51 maxAscent = ascent
52 maxAscentGlyph = glyph.name
53
54 # get descender of current layer
55 descent = layer.bounds.origin.y
56
57 # get ascender of current layer (total height of layer, subtracting value of descender)
58 ascent = layer.bounds.size.height + descent
59
60 # get maximums of only letters in list vars, for typo and hhea values
61 if glyph.name in caps:
62
63 for layer in glyph.layers:
64 if ascent >= mainMaxAscent:
65 mainMaxAscent = ascent
66 mainMaxAscentGlyph = glyph.name
67
68
69 if glyph.name in lowercase:
70 # if descent/ascent of current layer is greater than previous max descents/ascents, update the max descent/ascent
71 if descent <= mainMaxDescent:
72 mainMaxDescent = descent
73 mainMaxDescentGlyph = glyph.name
74
75
76
77
78 # check values for sanity
79 print(maxDescentGlyph, maxDescent, maxAscentGlyph, maxAscent)
80
81 # make lineGap so that the total of `ascent + descent + lineGap` equals 120% of UPM size
82
83 UPM = font.upm
84
85 totalSize = maxAscent + abs(maxDescent)
86
87 # lineGap = int((UPM * 1.2)) - totalSize
88
89 # print(UPM, UPM * 1.2, totalSize, lineGap)
90
91 ## use highest/lowest points to set custom parameters for winAscent and winDescent
92 ## following vertical metric schema from https://github.com/googlefonts/gf-docs/tree/master/VerticalMetrics (actually, that source must be updated to better recommendations found at https://github.com/googlefonts/fontbakery/issues/2164#issuecomment-436595886)
93
94 font.customParameters["Use Typo Metrics"] = True
95
96 for master in font.masters:
97
98 # Win Ascent/Descent = Font bbox yMax/yMin
99 master.customParameters["winAscent"] = maxAscent
100 master.customParameters["winDescent"] = abs(maxDescent)
101
102 typoLineGap = 0
103 master.customParameters["typoLineGap"] = typoLineGap
104 master.customParameters["hheaLineGap"] = typoLineGap
105
106 typoDescender = mainMaxDescent
107 master.customParameters["typoDescender"] = typoDescender
108 master.customParameters["hheaDescender"] = typoDescender
109
110 typoAscender = mainMaxAscent
111 master.customParameters["typoAscender"] = typoAscender
112 master.customParameters["hheaAscender"] = typoAscender
113