Debian Codegraph @

texmf-dist/doc/fonts/gnu-freefont/tools/test/checkGlyphNumbers.py

raw
1 #!/usr/bin/env ../utility/fontforge-interp.sh
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, 2011 Stevan White"
20 __date__ = "$Date: 2011-11-27 01:33:15 +0100 (Sun, 27 Nov 2011) $"
21 __version__ = "$Revision: 1907 $"
22
23 __doc__ = """
24 For most unicode ranges, glyph slot numbers should be the same as the
25 Unicode value.
26 The Private Use ranges are the exception: those characters should have a
27 definate non-Unicode number: -1
28
29 This script checks that this is the case, and prints out a warning
30 whenever it isn't.
31 """
32
33 import fontforge
34 import sys
35
36 problem = False
37
38 def inPrivateUseRange( glyph ):
39 e = glyph.encoding
40
41 return ( ( e >= 0xE800 and e <= 0xF8FF )
42 or ( e >= 0xFF000 and e <= 0xFFFFD )
43 or ( e >= 0x100000 and e <= 0x10FFFD ) )
44
45 def isSpecialTrueType( glyph ):
46 """ Fontforge treats three control characters as the special
47 TrueType characters recommended by that standard
48 """
49 e = glyph.encoding
50
51 return e == 0 or e == 1 or e == 0xD
52
53 from os import path
54 def checkGlyphNumbers( fontDir, fontFile ):
55 if isinstance( fontFile, ( list, tuple ) ):
56 print "In directory " + fontDir
57 for fontName in fontFile:
58 checkGlyphNumbers( fontDir, fontName )
59 return
60
61 print "Checking slot numbers in " + fontFile
62 font = fontforge.open( path.join( fontDir, fontFile ) )
63
64 g = font.selection.all()
65 g = font.selection.byGlyphs
66
67 valid = True
68 for glyph in g:
69 if isSpecialTrueType( glyph ):
70 # FIXME really should complain if it DOESNT exist
71 pass
72 elif inPrivateUseRange( glyph ):
73 if glyph.unicode != -1:
74 print "Glyph at slot " + str( glyph.encoding ) \
75 + " is Private Use but has Unicode"
76 problem = True
77 else:
78 if glyph.encoding != glyph.unicode:
79 print "Glyph at slot " + str( glyph.encoding ) \
80 + " has wrong Unicode"
81 problem = True
82
83 # --------------------------------------------------------------------------
84 args = sys.argv[1:]
85
86 if len( args ) < 1 or len( args[0].strip() ) == 0:
87 checkGlyphNumbers( '../../sfd/',
88 ( 'FreeSerif.sfd', 'FreeSerifItalic.sfd',
89 'FreeSerifBold.sfd', 'FreeSerifBoldItalic.sfd',
90 'FreeSans.sfd', 'FreeSansOblique.sfd',
91 'FreeSansBold.sfd', 'FreeSansBoldOblique.sfd',
92 'FreeMono.sfd', 'FreeMonoOblique.sfd',
93 'FreeMonoBold.sfd', 'FreeMonoBoldOblique.sfd' ) )
94 else:
95 checkGlyphNumbers( args[0], args[1:] )
96
97 if problem:
98 sys.exit( 1 )