comparison OrthancFramework/Resources/Fonts/GenerateFont.py @ 4044:d25f4c0fa160 framework

splitting code into OrthancFramework and OrthancServer
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 10 Jun 2020 20:30:34 +0200
parents Resources/Fonts/GenerateFont.py@94f4a18a79cc
children bf7b9edf6b81
comparison
equal deleted inserted replaced
4043:6c6239aec462 4044:d25f4c0fa160
1 #!/usr/bin/python
2
3 # Orthanc - A Lightweight, RESTful DICOM Store
4 # Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
5 # Department, University Hospital of Liege, Belgium
6 # Copyright (C) 2017-2020 Osimis S.A., Belgium
7 #
8 # This program is free software: you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License as
10 # published by the Free Software Foundation, either version 3 of the
11 # License, or (at your option) any later version.
12 #
13 # In addition, as a special exception, the copyright holders of this
14 # program give permission to link the code of its release with the
15 # OpenSSL project's "OpenSSL" library (or with modified versions of it
16 # that use the same license as the "OpenSSL" library), and distribute
17 # the linked executables. You must obey the GNU General Public License
18 # in all respects for all of the code used other than "OpenSSL". If you
19 # modify file(s) with this exception, you may extend this exception to
20 # your version of the file(s), but you are not obligated to do so. If
21 # you do not wish to do so, delete this exception statement from your
22 # version. If you delete this exception statement from all source files
23 # in the program, then also delete it here.
24 #
25 # This program is distributed in the hope that it will be useful, but
26 # WITHOUT ANY WARRANTY; without even the implied warranty of
27 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28 # General Public License for more details.
29 #
30 # You should have received a copy of the GNU General Public License
31 # along with this program. If not, see <http://www.gnu.org/licenses/>.
32
33
34
35
36 # sudo pip install freetype-py
37
38
39
40 import freetype
41 import json
42 import os
43 import sys
44 import unicodedata
45
46
47 if len(sys.argv) != 3:
48 print('Usage: %s <Font> <Size>\n' % sys.argv[0])
49 print('Example: %s /usr/share/fonts/truetype/ubuntu-font-family/UbuntuMono-B.ttf 16\n' % sys.argv[0])
50 sys.exit(-1)
51
52
53
54 FONT = sys.argv[1]
55 PIXEL_SIZE = int(sys.argv[2])
56 CHARSET = 'latin-1'
57
58
59 # Load the font
60 face = freetype.Face(FONT)
61 face.set_char_size(PIXEL_SIZE * 64)
62
63 # Generate all the characters between 0 and 255
64 characters = ''.join(map(chr, range(0, 256)))
65
66 # Interpret the string using the required charset
67 characters = characters.decode(CHARSET, 'ignore')
68
69 # Keep only non-control characters
70 characters = filter(lambda c: unicodedata.category(c)[0] != 'C', characters)
71
72 font = {
73 'Name' : os.path.basename(FONT),
74 'Size' : PIXEL_SIZE,
75 'Characters' : {}
76 }
77
78
79 def PrintCharacter(c):
80 pos = 0
81 for i in range(c['Height']):
82 s = ''
83 for j in range(c['Width']):
84 if c['Bitmap'][pos] > 127:
85 s += '*'
86 else:
87 s += ' '
88 pos += 1
89 print s
90
91
92 for c in characters:
93 face.load_char(c)
94
95 info = {
96 'Width' : face.glyph.bitmap.width,
97 'Height' : face.glyph.bitmap.rows,
98 'Advance' : face.glyph.metrics.horiAdvance / 64,
99 'Top' : -face.glyph.metrics.horiBearingY / 64,
100 'Bitmap' : face.glyph.bitmap.buffer,
101 }
102
103 font['Characters'][ord(c)] = info
104
105 #PrintCharacter(info)
106
107 minTop = min(map(lambda (k, v): v['Top'], font['Characters'].iteritems()))
108 for c in font['Characters']:
109 font['Characters'][c]['Top'] -= minTop
110
111 font['MaxAdvance'] = max(map(lambda (k, v): v['Advance'], font['Characters'].iteritems()))
112 font['MaxHeight'] = max(map(lambda (k, v): v['Height'], font['Characters'].iteritems()))
113
114 print json.dumps(font)