comparison OrthancServer/Resources/Fonts/GenerateFont.py @ 4120:304842a0d152 framework-lgpl

adding missing license headers
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 07 Jul 2020 19:37:25 +0200
parents OrthancFramework/Resources/Fonts/GenerateFont.py@bf7b9edf6b81
children d9473bd5ed43
comparison
equal deleted inserted replaced
4119:bf7b9edf6b81 4120:304842a0d152
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 # This program is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20
21
22
23 # sudo pip install freetype-py
24
25
26
27 import freetype
28 import json
29 import os
30 import sys
31 import unicodedata
32
33
34 if len(sys.argv) != 3:
35 print('Usage: %s <Font> <Size>\n' % sys.argv[0])
36 print('Example: %s /usr/share/fonts/truetype/ubuntu-font-family/UbuntuMono-B.ttf 16\n' % sys.argv[0])
37 sys.exit(-1)
38
39
40
41 FONT = sys.argv[1]
42 PIXEL_SIZE = int(sys.argv[2])
43 CHARSET = 'latin-1'
44
45
46 # Load the font
47 face = freetype.Face(FONT)
48 face.set_char_size(PIXEL_SIZE * 64)
49
50 # Generate all the characters between 0 and 255
51 characters = ''.join(map(chr, range(0, 256)))
52
53 # Interpret the string using the required charset
54 characters = characters.decode(CHARSET, 'ignore')
55
56 # Keep only non-control characters
57 characters = filter(lambda c: unicodedata.category(c)[0] != 'C', characters)
58
59 font = {
60 'Name' : os.path.basename(FONT),
61 'Size' : PIXEL_SIZE,
62 'Characters' : {}
63 }
64
65
66 def PrintCharacter(c):
67 pos = 0
68 for i in range(c['Height']):
69 s = ''
70 for j in range(c['Width']):
71 if c['Bitmap'][pos] > 127:
72 s += '*'
73 else:
74 s += ' '
75 pos += 1
76 print s
77
78
79 for c in characters:
80 face.load_char(c)
81
82 info = {
83 'Width' : face.glyph.bitmap.width,
84 'Height' : face.glyph.bitmap.rows,
85 'Advance' : face.glyph.metrics.horiAdvance / 64,
86 'Top' : -face.glyph.metrics.horiBearingY / 64,
87 'Bitmap' : face.glyph.bitmap.buffer,
88 }
89
90 font['Characters'][ord(c)] = info
91
92 #PrintCharacter(info)
93
94 minTop = min(map(lambda (k, v): v['Top'], font['Characters'].iteritems()))
95 for c in font['Characters']:
96 font['Characters'][c]['Top'] -= minTop
97
98 font['MaxAdvance'] = max(map(lambda (k, v): v['Advance'], font['Characters'].iteritems()))
99 font['MaxHeight'] = max(map(lambda (k, v): v['Height'], font['Characters'].iteritems()))
100
101 print json.dumps(font)