| 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 2017, 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 'OS/2' table fsSelection bitfield: |
| 25 |
USE_TYPO_METRICS bit to True. |
| 26 |
ITALIC bit to True for Italic or Oblique faces (per the docs). |
| 27 |
OBLIQUE bit to True for Oblique faces (per the docs). |
| 28 |
|
| 29 |
A cludge to work around some misunderstandings in FontForge. |
| 30 |
""" |
| 31 |
from sys import argv, stdout as out, stderr as err, exit |
| 32 |
from OpenType.fontdirectory import getDirectoryEntriesByTag |
| 33 |
from OpenType.requiredtables import OS_2Table, headTable |
| 34 |
from OpenType.checksum import get_file32Bit_checkSumAdjustment |
| 35 |
import traceback |
| 36 |
|
| 37 |
argc = len( argv ) |
| 38 |
|
| 39 |
if argc > 1: |
| 40 |
filePath = argv[argc - 1] |
| 41 |
if not filePath: |
| 42 |
print( "Usage: python correct_fsSelection <filename>", file=err ) |
| 43 |
exit( 1 ) |
| 44 |
|
| 45 |
try: |
| 46 |
#FIXME this is very clumsy, reading the whole file in and writing it |
| 47 |
# to set a single bit. The structure is there to do it better, |
| 48 |
|
| 49 |
infile = open( filePath, 'r+b' ) |
| 50 |
buf = bytearray( infile.read() ) |
| 51 |
|
| 52 |
entries_by_tag = getDirectoryEntriesByTag( buf ) |
| 53 |
|
| 54 |
entry = entries_by_tag[ 'OS/2' ] |
| 55 |
t = OS_2Table( buf, entry.offset ) # read table from font buffer |
| 56 |
# ======================= TEST |
| 57 |
#print( "OS/2 checksum-orig", hex( entry.checkSum ) ) |
| 58 |
cs = t.getChecksum() |
| 59 |
#print( "OS/2 checksum-calc", hex( cs ) ) |
| 60 |
#print( "OS/2 Table -------------------", t ) |
| 61 |
# ======================= |
| 62 |
# big-endian: bit 0 is at the beginning |
| 63 |
ITALIC = 0b0000000000000001 |
| 64 |
USE_TYPO_METRICS = 0b0000000010000000 |
| 65 |
OBLIQUE = 0b0000001000000000 |
| 66 |
t.fsSelection = t.fsSelection | USE_TYPO_METRICS |
| 67 |
# Offical OpenType docs say of the ITALIC flag: |
| 68 |
# "Font contains italic or oblique characters" |
| 69 |
if "Italic" in filePath or "Oblique" in filePath: |
| 70 |
t.fsSelection = t.fsSelection | ITALIC |
| 71 |
# "Font contains oblique characters" |
| 72 |
if "Oblique" in filePath: |
| 73 |
t.fsSelection = t.fsSelection | OBLIQUE |
| 74 |
|
| 75 |
t.writeInto( buf, entry.offset ) # write into font buffer |
| 76 |
# ======================= |
| 77 |
|
| 78 |
entry.checkSum = t.getChecksum() |
| 79 |
entry_off = entry.getOffset() # offset to directory entry itself |
| 80 |
entry.writeInto( buf, entry_off ) # write into dir. entry buffer |
| 81 |
|
| 82 |
infile.close() |
| 83 |
|
| 84 |
print( "Correcting OS/2 fsSelection bits in file", filePath ) |
| 85 |
|
| 86 |
entry = entries_by_tag[ 'head' ] |
| 87 |
ht = headTable( buf, entry.offset ) |
| 88 |
ht.checkSumAdjustment = 0 |
| 89 |
entry.checkSum = ht.getChecksum() |
| 90 |
ht.writeInto( buf, entry.offset ) |
| 91 |
|
| 92 |
outfile = open( filePath, 'wb' ) |
| 93 |
outfile.write( str( buf ) ) |
| 94 |
outfile.flush() |
| 95 |
|
| 96 |
csa = get_file32Bit_checkSumAdjustment( filePath ) |
| 97 |
outfile = open( filePath, 'wb' ) |
| 98 |
ht.checkSumAdjustment = csa |
| 99 |
ht.writeInto( buf, entry.offset ) |
| 100 |
outfile.write( str( buf ) ) |
| 101 |
outfile.flush() |
| 102 |
|
| 103 |
except Exception as e: |
| 104 |
print( "correct_fsSelection, file ", filePath, file=err ) |
| 105 |
print( e, file=err ) |
| 106 |
traceback.print_exc() |
| 107 |
print( "correct_fsSelection, file ", filePath, file=out ) |
| 108 |
print( e, file=out ) |
| 109 |
exit( 1 ) |
| 110 |
exit( 0 ) |