diff options
Diffstat (limited to 'devtools/create_bladerunner/subtitles/fontCreator/fonFileLib.py')
-rw-r--r-- | devtools/create_bladerunner/subtitles/fontCreator/fonFileLib.py | 70 |
1 files changed, 41 insertions, 29 deletions
diff --git a/devtools/create_bladerunner/subtitles/fontCreator/fonFileLib.py b/devtools/create_bladerunner/subtitles/fontCreator/fonFileLib.py index 0b76a1478c..d40efc6e29 100644 --- a/devtools/create_bladerunner/subtitles/fontCreator/fonFileLib.py +++ b/devtools/create_bladerunner/subtitles/fontCreator/fonFileLib.py @@ -10,31 +10,31 @@ imagePilLibFound = False try: import shutil except ImportError: - print "Error:: Shutil python library is required to be installed!" + print "[Error] Shutil python library is required to be installed!" else: shutilLibFound = True try: import struct except ImportError: - print "Error:: struct python library is required to be installed!" + print "[Error] struct python library is required to be installed!" else: structLibFound = True try: import Image except ImportError: - print "Error:: Image python library (PIL) is required to be installed!" + print "[Error] Image python library (PIL) is required to be installed!" else: imagePilLibFound = True if (not shutilLibFound) or (not structLibFound) or (not imagePilLibFound): - sys.stdout.write("Error:: Errors were found when trying to import required python libraries\n") + sys.stdout.write("[Error] Errors were found when trying to import required python libraries\n") sys.exit(1) from struct import * -my_module_version = "0.50" +my_module_version = "0.80" my_module_name = "fonFileLib" @@ -51,26 +51,29 @@ class FonHeader: class fonFile: m_header = FonHeader() - simpleFontFileName = "GENERIC.FON" + simpleFontFileName = 'GENERIC.FON' realNumOfCharactersInImageSegment = 0 # this is used for the workaround for the corrupted TAHOME18.FON nonEmptyCharacters = 0 glyphDetailEntriesLst = [] # list of 5-value tuples. Tuple values are (X-offset, Y-offset, Width, Height, Offset in Graphics segment) glyphPixelData = None # buffer of pixel data for glyphs - def __init__(self): + m_traceModeEnabled = False + + # traceModeEnabled is bool to enable more printed debug messages + def __init__(self, traceModeEnabled = True): del self.glyphDetailEntriesLst[:] self.glyphPixelData = None # buffer of pixel data for glyphs - self.simpleFontFileName = "GENERIC.FON" + self.simpleFontFileName = 'GENERIC.FON' self.realNumOfCharactersInImageSegment = 0 # this is used for the workaround for the corrupted TAHOME18.FON self.nonEmptyCharacters = 0 + self.m_traceModeEnabled = traceModeEnabled return def loadFonFile(self, fonBytesBuff, maxLength, fonFileName): self.simpleFontFileName = fonFileName - offsInFonFile = 0 localLstOfDataOffsets = [] del localLstOfDataOffsets[:] @@ -84,7 +87,8 @@ class fonFile: if self.simpleFontFileName == 'TAHOMA18.FON': # deal with corrupted original 'TAHOMA18.FON' file self.realNumOfCharactersInImageSegment = 176 - print "SPECIAL CASE. WORKAROUND FOR CORRUPTED %s FILE. Only %d characters supported!" % (self.simpleFontFileName, self.realNumOfCharactersInImageSegment) + if self.m_traceModeEnabled: + print "[Debug] SPECIAL CASE. WORKAROUND FOR CORRUPTED %s FILE. Only %d characters supported!" % (self.simpleFontFileName, self.realNumOfCharactersInImageSegment) else: self.realNumOfCharactersInImageSegment = self.header().maxEntriesInTableOfDetails @@ -100,15 +104,16 @@ class fonFile: self.header().graphicSegmentByteSize = tmpTuple[0] offsInFonFile += 4 - print "FON Header Info: " - print "Num of entries: %d\tGlyph max-Width: %d\tGlyph max-Height: %d\tGraphic Segment size %d" % (self.header().maxEntriesInTableOfDetails, self.header().maxGlyphWidth, self.header().maxGlyphHeight, self.header().graphicSegmentByteSize) - #print "Num of entries: %d\tGlyph max-Width: %d\tGlyph max-Height: %d\tGraphic Segment size %d" % (self.realNumOfCharactersInImageSegment, self.header().maxGlyphWidth, self.header().maxGlyphHeight, self.header().graphicSegmentByteSize) + if self.m_traceModeEnabled: + print "[Debug] Font file (FON) Header Info: " + print "[Debug] Number of entries: %d, Glyph max-Width: %d, Glyph max-Height: %d, Graphic Segment size: %d" % (self.header().maxEntriesInTableOfDetails, self.header().maxGlyphWidth, self.header().maxGlyphHeight, self.header().graphicSegmentByteSize) # # Glyph details table (each entry is 5 unsigned integers == 5*4 = 20 bytes) # For most characters, their ASCII value + 1 is the index of their glyph's entry in the details table. The 0 entry of this table is reserved # #tmpXOffset, tmpYOffset, tmpWidth, tmpHeight, tmpDataOffset - print "FON glyph details table: " + if self.m_traceModeEnabled: + print "[Debug] Font file (FON) glyph details table: " for idx in range(0, self.realNumOfCharactersInImageSegment): tmpTuple = struct.unpack_from('i', fonBytesBuff, offsInFonFile) # unsigned integer 4 bytes tmpXOffset = tmpTuple[0] @@ -131,15 +136,18 @@ class fonFile: offsInFonFile += 4 if tmpWidth == 0 or tmpHeight == 0: - print "Index: %d\t UNUSED *****************************************************************" % (idx) + if self.m_traceModeEnabled: + print "Index: %d\t UNUSED *****************************************************************" % (idx) else: self.nonEmptyCharacters += 1 - print "Index: %d\txOffs: %d\tyOffs: %d\twidth: %d\theight: %d\tdataOffs: %d" % (idx, tmpXOffset, tmpYOffset, tmpWidth, tmpHeight, tmpDataOffset) + if self.m_traceModeEnabled: + print "Index: %d\txOffs: %d\tyOffs: %d\twidth: %d\theight: %d\tdataOffs: %d" % (idx, tmpXOffset, tmpYOffset, tmpWidth, tmpHeight, tmpDataOffset) if tmpDataOffset not in localLstOfDataOffsets: localLstOfDataOffsets.append(tmpDataOffset) else: # This never happens in the original files. Offsets are "re-used" but not really because it happens only for empty (height = 0) characters which all seem to point to the next non-empty character - print "Index: %d\t RE-USING ANOTHER GLYPH *****************************************************************" % (idx) + if self.m_traceModeEnabled: + print "Index: %d\t RE-USING ANOTHER GLYPH *****************************************************************" % (idx) self.glyphDetailEntriesLst.append( ( tmpXOffset, tmpYOffset, tmpWidth, tmpHeight, tmpDataOffset) ) @@ -147,11 +155,13 @@ class fonFile: self.glyphPixelData = fonBytesBuff[offsInFonFile:] return True except: - print "Loading failure!" + print "[Error] Loading Font file (FON) %s failed!" % (self.simpleFontFileName) raise return False def outputFonToPNG(self): + print "[Info] Exporting font file (FON) to PNG: %s" % (self.simpleFontFileName + ".PNG") + targWidth = 0 targHeight = 0 paddingFromTopY = 2 @@ -159,7 +169,7 @@ class fonFile: if len(self.glyphDetailEntriesLst) == 0 or (len(self.glyphDetailEntriesLst) != self.realNumOfCharactersInImageSegment and len(self.glyphDetailEntriesLst) != self.header().maxEntriesInTableOfDetails) : - print "Error. Fon file load process did not complete correctly. Missing important data in structures. Cannot output image!" + print "[Error] Font file (FON) loading process did not complete correctly. Missing important data in structures. Cannot output image!" return # TODO asdf refine this code here. the dimensions calculation is very crude for now @@ -202,7 +212,7 @@ class fonFile: glyphDataOffs += 2 # if pixelColor > 0x8000: -# print "WEIRD CASE" # NEVER HAPPENS - TRANSPARENCY IS ON/OFF. There's no grades of transparency +# print "[Debug] WEIRD CASE" # NEVER HAPPENS - TRANSPARENCY IS ON/OFF. There's no grades of transparency rgbacolour = (0,0,0,0) if pixelColor == 0x8000: rgbacolour = (0,0,0,0) # alpha: 0.0 fully transparent @@ -219,9 +229,11 @@ class fonFile: imTargetGameFont.putpixel(( (drawIdx + 1) * (self.header().maxGlyphWidth + paddingBetweenGlyphsX ) + currX, paddingFromTopY + glyphYoffs + currY), rgbacolour) currX += 1 - - imTargetGameFont.save(os.path.join('.', self.simpleFontFileName + ".PNG"), "PNG") - + try: + imTargetGameFont.save(os.path.join('.', self.simpleFontFileName + ".PNG"), "PNG") + except Exception as e: + print '[Error] Unable to write to output PNG file. ' + str(e) + def header(self): return self.m_header # @@ -229,7 +241,7 @@ class fonFile: # if __name__ == '__main__': # main() - print "Running %s as main module" % (my_module_name) + print "[Debug] Running %s as main module" % (my_module_name) # assumes a file of name TAHOMA24.FON in same directory inFONFile = None #inFONFileName = 'TAHOMA24.FON' # USED IN CREDIT END-TITLES and SCORERS BOARD AT POLICE STATION @@ -240,22 +252,22 @@ if __name__ == '__main__': errorFound = False try: - print "Opening %s" % (inFONFileName) + print "[Info] Opening %s" % (inFONFileName) inFONFile = open(os.path.join('.',inFONFileName), 'rb') except: errorFound = True - print "Unexpected error:", sys.exc_info()[0] + print "[Error] Unexpected event:", sys.exc_info()[0] raise if not errorFound: allOfFonFileInBuffer = inFONFile.read() fonFileInstance = fonFile() if (fonFileInstance.loadFonFile(allOfFonFileInBuffer, len(allOfFonFileInBuffer), inFONFileName)): - print "FON file loaded successfully!" + print "[Info] Font file (FON) was loaded successfully!" fonFileInstance.outputFonToPNG() else: - print "Error while loading FON file!" + print "[Error] Error while loading Font file (FON)!" inFONFile.close() else: #debug - #print "Running %s imported from another module" % (my_module_name) + #print "[Debug] Running %s imported from another module" % (my_module_name) pass
\ No newline at end of file |