Debian Codegraph @

tools/generate/tweeks/set_isFixedPitch.py

raw
1 #!/usr/bin/env python
2 from __future__ import print_function
3 __license__ = """
4 This file is part of GNU FreeFont.
5
6 GNU FreeFont is free software: you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free Software
8 Foundation, either version 3 of the License, or (at your option) any later
9 version.
10
11 GNU FreeFont is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along with
16 GNU FreeFont. If not, see <http://www.gnu.org/licenses/>.
17 """
18 __author__ = "Stevan White"
19 __email__ = "[email protected]"
20 __copyright__ = "Copyright 2012, 2015, 2018 Stevan White"
21 __date__ = "$Date: 2018-06-03 22:20:36 +0200 (So, 03. Jun 2018) $"
22 __version__ = "$Revision: 3921 $"
23
24 __doc__ = """Sets the OpenType 'post' table flag 'isFixedPitch' to True.
25 A cludge to work around some misunderstandings in FontForge.
26 """
27 from sys import argv, stdout as out, stderr as err, exit
28 from OpenType.fontdirectory import getDirectoryEntriesByTag
29 from OpenType.requiredtables import postTable, headTable
30 from OpenType.checksum import get_file32Bit_checkSumAdjustment
31
32 argc = len( argv )
33
34 if argc > 1:
35 filePath = argv[argc - 1]
36 if not filePath:
37 print( "Usage: python set_isfixedpitch <filename>", file=err )
38 exit( 1 )
39
40 try:
41 #FIXME this is very clumsy, reading the whole file in and writing it
42 # to set a single bit. The structure is there to do it better,
43 if not "Mono" in filePath:
44 exit( 1 )
45
46 infile = open( filePath, 'r+b' )
47 buf = bytearray( infile.read() )
48
49 entries_by_tag = getDirectoryEntriesByTag( buf )
50
51 entry = entries_by_tag[ 'post' ]
52 t = postTable( buf, entry.offset )
53 # ======================= TEST
54 #print( "post checksum-orig", hex( entry.checkSum ) )
55 cs = t.getChecksum()
56 #print( "post checksum-calc", hex( cs ) )
57 # =======================
58 t.isFixedPitch = True
59
60 t.writeInto( buf, entry.offset )
61 # =======================
62
63 entry.checkSum = t.getChecksum()
64 entry_off = entry.getOffset() # offset to directory entry itself
65 entry.writeInto( buf, entry_off ) # write into dir. entry buffer
66
67 infile.close()
68
69 print( "Setting 'post' flag 'isFixedPitch'", filePath )
70
71 entry = entries_by_tag[ 'head' ]
72 ht = headTable( buf, entry.offset )
73 ht.checkSumAdjustment = 0
74 entry.checkSum = ht.getChecksum()
75 ht.writeInto( buf, entry.offset )
76
77 outfile = open( filePath, 'wb' )
78 outfile.write( str( buf ) )
79 outfile.flush()
80
81 csa = get_file32Bit_checkSumAdjustment( filePath )
82 outfile = open( filePath, 'wb' )
83 ht.checkSumAdjustment = csa
84 ht.writeInto( buf, entry.offset )
85 outfile.write( str( buf ) )
86 outfile.flush()
87
88 except Exception as e:
89 print( "set_isfixedpitch, file ", filePath, file=err )
90 print( e, file=err )
91 print( "set_isfixedpitch, file ", filePath, file=out )
92 print( e, file=out )
93 exit( 1 )
94 exit( 0 )