Debian Codegraph @

tools/script-menu/nameBySlot.py

raw
1 from __future__ import print_function
2 __license__ = """
3 This file is part of GNU FreeFont.
4
5 GNU FreeFont is free software: you can redistribute it and/or modify it under
6 the terms of the GNU General Public License as published by the Free Software
7 Foundation, either version 3 of the License, or (at your option) any later
8 version.
9
10 GNU FreeFont is distributed in the hope that it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License along with
15 GNU FreeFont. If not, see <http://www.gnu.org/licenses/>.
16 """
17 __author__ = "Stevan White"
18 __email__ = "[email protected]"
19 __copyright__ = "Copyright 2009, 2010, 2018 Stevan White"
20 __date__ = "$Date:: 2020-11-18 00:40:52 +0100#$"
21 __version__ = "$Revision: 4248 $"
22
23 __doc__ = """
24 For use from the FontForge Script Menu.
25 Add it to the Scripts Menu using the Preferences dialog.
26
27 Sets the name and unicode values of the selected range of slots to the
28 encoding, that is
29 Name: uniXXXX
30 Unicode: u+XXXX
31 where XXXX is the n-digit hex value for the slot encoding.
32
33 Careful! it changes the value whether it was previously set or not.
34
35 Detailed info is printed to standard output (see by launching FontForge
36 from a console).
37 """
38 import fontforge
39
40 def explain_error_and_quit( e ):
41 if e:
42 print( 'Error:', e )
43 exit( 1 )
44
45 try:
46 glyphs = fontforge.activeFont().selection.byGlyphs
47 for g in glyphs:
48 if g.encoding <= 0xFFFF:
49 newname = 'uni%0.4X' %( g.encoding )
50 elif g.encoding <= 0xFFFFF:
51 newname = 'uni%0.5X' %( g.encoding )
52 elif g.encoding <= 0xFFFFFF:
53 newname = 'uni%0.6X' %( g.encoding )
54 elif g.encoding <= 0xFFFFFFF:
55 newname = 'uni%0.7X' %( g.encoding )
56 elif g.encoding <= 0xFFFFFFFF:
57 newname = 'uni%0.8X' %( g.encoding )
58 print( "naming", g.glyphname, 'as', newname )
59 g.glyphname = newname
60 g.unicode = g.encoding
61 except ValueError as e:
62 explain_error_and_quit( e )
63