Debian Codegraph @

misc/tools/ufo-color-glyphs.py

raw symbols SCIP
1 #!/usr/bin/env python
2 # encoding: utf8
3 #
4 # Grab http://www.unicode.org/Public/UCD/latest/ucd/UnicodeData.txt
5 #
6 from __future__ import print_function
7 import os, sys
8 from argparse import ArgumentParser
9 from robofab.objects.objectsRF import OpenFont
10 from unicode_util import parseUnicodeDataFile, MainCategories as UniMainCategories
11
12 lightBlueColor = (0.86, 0.92, 0.97, 1.0)
13 lightTealColor = (0.8, 0.94, 0.95, 1.0)
14 lightYellowColor = (0.97, 0.95, 0.83, 1.0)
15 lightPurpleColor = (0.93, 0.9, 0.98, 1.0)
16 lightGreyColor = (0.94, 0.94, 0.94, 1.0)
17 mediumGreyColor = (0.87, 0.87, 0.87, 1.0)
18 lightGreenColor = (0.89, 0.96, 0.92, 1.0)
19 mediumGreenColor = (0.77, 0.95, 0.76, 1.0)
20 lightRedColor = (0.98, 0.89, 0.89, 1.0)
21 lightOrangeColor = (1.0, 0.89, 0.82, 1.0)
22 redColor = (1, 0.3, 0.3, 1)
23
24 colorsByGlyphName = [
25 (set('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'), lightBlueColor), # light blue 1
26 ]
27
28 colorsByUCMainCategory = {
29 # UniMainCategories.Letter: (1, 1, 1, 1),
30 UniMainCategories.Mark: lightRedColor,
31 UniMainCategories.Punctuation: lightGreyColor,
32 UniMainCategories.Format: lightGreyColor,
33 UniMainCategories.Number: lightGreenColor,
34 UniMainCategories.Symbol: lightTealColor,
35 UniMainCategories.Separator: lightPurpleColor,
36 UniMainCategories.Control: redColor,
37 UniMainCategories.Surrogate: redColor,
38 UniMainCategories.PrivateUse: lightYellowColor,
39 UniMainCategories.Unassigned: lightYellowColor,
40 UniMainCategories.Other: lightOrangeColor,
41 }
42
43
44 def colorForGlyph(name, unicodes, ucd):
45 for nameSet, color in colorsByGlyphName:
46 if name in nameSet:
47 return color
48
49 for uc in unicodes:
50 cp = ucd.get(uc)
51 if cp is None:
52 continue
53 return colorsByUCMainCategory.get(cp.mainCategory)
54
55 if len(unicodes) == 0:
56 if name.find('.cn') != -1:
57 # pure component
58 return mediumGreenColor
59 else:
60 # precomposed
61 return mediumGreyColor
62
63 return None
64
65
66 def main():
67 argparser = ArgumentParser(
68 description='Set robofont color marks on glyphs based on unicode categories')
69
70 argparser.add_argument(
71 '-dry', dest='dryRun', action='store_const', const=True, default=False,
72 help='Do not modify anything, but instead just print what would happen.')
73
74 argparser.add_argument(
75 '-ucd', dest='ucdFile', metavar='<file>', type=str,
76 help='UnicodeData.txt file from http://www.unicode.org/')
77
78 argparser.add_argument(
79 'fontPaths', metavar='<ufofile>', type=str, nargs='+', help='UFO fonts to update')
80
81 args = argparser.parse_args()
82 dryRun = args.dryRun
83 markLibKey = 'com.typemytype.robofont.mark'
84
85 ucd = {}
86 if args.ucdFile:
87 ucd = parseUnicodeDataFile(args.ucdFile)
88
89 for fontPath in args.fontPaths:
90 font = OpenFont(fontPath)
91 for g in font:
92 rgba = colorForGlyph(g.name, g.unicodes, ucd)
93 if rgba is None:
94 if markLibKey in g.lib:
95 del g.lib[markLibKey]
96 else:
97 g.lib[markLibKey] = [float(n) for n in rgba]
98
99 print('Write', fontPath)
100 if not dryRun:
101 font.save()
102
103
104 if __name__ == '__main__':
105 main()