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