forked from hughsie/fedora-appstream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
font_file.py
executable file
·171 lines (142 loc) · 5.49 KB
/
font_file.py
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Licensed under the GNU General Public License Version 2
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# Copyright (C) 2009-2013
# Richard Hughes <richard@hughsie.com>
#
import sys
from fontTools import ttLib
from PIL import Image, ImageOps, ImageFont, ImageDraw, ImageChops
# internal
from application import Application
from package import Package
from screenshot import Screenshot
def autocrop(im, alpha):
if alpha:
bg = Image.new("RGBA", im.size, alpha)
else:
bg = Image.new("RGBA", im.size)
diff = ImageChops.difference(im, bg)
bbox = diff.getbbox()
if bbox:
return im.crop(bbox)
return None
def get_font_name(font):
"""Get the short name from the font's names table"""
FONT_SPECIFIER_NAME_ID = 4
FONT_SPECIFIER_FAMILY_ID = 1
name = ""
family = ""
for record in font['name'].names:
if record.nameID == FONT_SPECIFIER_NAME_ID and not name:
if '\000' in record.string:
name = unicode(record.string, 'utf-16-be').encode('utf-8')
else:
name = record.string
elif record.nameID == FONT_SPECIFIER_FAMILY_ID and not family:
if '\000' in record.string:
family = unicode(record.string, 'utf-16-be').encode('utf-8')
else:
family = record.string
if name and family:
break
return name, family
class FontFile(Application):
def __init__(self, pkg, cfg):
Application.__init__(self, pkg, cfg)
self.type_id = 'font'
self.categories = [ 'Addons', 'Fonts' ]
self.thumbnail_screenshots = False
def create_icon(self, font, filename):
# create a large canvas to draw the font to -- we don't know the width yet
img_size_temp = (256, 256)
bg_color = (255,255,255)
fg_color = (0,0,0)
im_temp = Image.new("RGBA", img_size_temp)
draw = ImageDraw.Draw(im_temp)
font = ImageFont.truetype(font, 160)
draw.text((20, 20), "Aa", fg_color, font=font)
# crop to the smallest size
im_temp = autocrop(im_temp, None)
if not im_temp:
return False
# create a new image and paste the cropped image into the center
img = Image.new('RGBA', img_size_temp)
img_w, img_h = im_temp.size
bg_w, bg_h = img.size
offset = ((bg_w - img_w) / 2, (bg_h - img_h) / 2)
img.paste(im_temp, offset)
# rescale the image back to 64x64
img = img.resize((64,64), Image.ANTIALIAS)
img.save(filename, 'png')
return True
def create_screenshot(self, font_file, filename):
# create a large canvas to draw the font to
img_size_temp = (2560, 256)
bg_color = (255,255,255)
fg_color = (0,0,0)
border_width = 5
basewidth = self.cfg.get_int('FontScreenshotWidth')
text = 'How quickly daft jumping zebras vex.'
im_temp = Image.new("RGBA", img_size_temp, bg_color)
draw = ImageDraw.Draw(im_temp)
font = ImageFont.truetype(font_file, 40)
draw.text((20, 20), text, fg_color, font=font)
font = ImageFont.truetype(font_file, 60)
draw.text((20, 70), text, fg_color, font=font)
font = ImageFont.truetype(font_file, 80)
draw.text((20, 140), text, fg_color, font=font)
# crop to the smallest size
im_temp = autocrop(im_temp, (255,255,255))
if not im_temp:
return False
# create a new image and paste the cropped image with a border
img = Image.new('RGBA', (im_temp.size[0] + border_width * 2,
im_temp.size[1] + border_width * 2), bg_color)
img.paste(im_temp, (border_width, border_width))
# resize to a known width */
wpercent = (basewidth / float(img.size[0]))
hsize = int((float(img.size[1]) * float(wpercent)))
img = img.resize((basewidth, hsize), Image.ANTIALIAS)
self.screenshots.append(Screenshot(self.app_id, img))
return True
def parse_file(self, f):
tt = ttLib.TTFont(f)
self.names['C'] = get_font_name(tt)[0]
self.comments['C'] = "A font family from " + get_font_name(tt)[1]
icon_fullpath = './icons/' + self.app_id + '.png'
# generate a preview icon
if not self.create_icon(f, icon_fullpath):
return False
self.icon = self.app_id
self.cached_icon = True
# generate a screenshot
icon_fullpath = './screenshots/' + self.app_id + '.png'
if not self.create_screenshot(f, icon_fullpath):
return False
return True
def main():
pkg = Package(sys.argv[1])
app = FontFile(pkg, None)
app.app_id = 'test'
f = open('/tmp/test.xml', 'w')
app.write(f)
f.close()
sys.exit(0)
if __name__ == "__main__":
main()