#!/usr/bin/python # # XBMArabicNumbers16x20.py - the source file for XBMArabicNumbers16x20 class # # Author: Alex Shindich (http://www.shindich.com/) # # This class is implemented based on the parser.py class that I downloaded # from http://www.ping.be/sabine-appelmans/home/python/index.html import string class XBMNumbers16x20: """This class creates XBM images for arabic numbers. Image size equals 16 * number of digits by 20. To use the class, create an instance of XBMNumbers16x20 and pass it the number to be printed out as a parameter. This class will print out as many digits as necessary to display the number. As an additional parameter you can pass it the minimum number of digits to be printed out. If the string representation of the number requires fewer digits then the minimum number, the string will be padded with zeros to the minimum number of digits. """ hex_bytes = [ [ 0xff,0xff, 0xff,0xff, 0xff,0xff, 0xff,0xff, 0xff,0xff, 0xff,0xff, 0xff,0xff, 0xff,0xff, 0xff,0xff, 0xff,0xff, ], [ 0x01,0xc0, 0x01,0xc0, 0x01,0xc0, 0x01,0xc0, 0x01,0xc0, 0x01,0xc0, 0x01,0xc0, 0x01,0xc0, 0x01,0xc0, 0x01,0xc0, ], [ 0xfd,0xdf, 0xfd,0xdf, 0xfd,0xdf, 0xfd,0xdf, 0xfd,0xdf, 0xfd,0xdf, 0xfd,0xdf, 0xfd,0xdf, 0xfd,0xdf, 0xfd,0xdf, ], [ 0x3d,0xde, 0x7d,0xde, 0x1d,0xde, 0x3d,0xdc, 0xfd,0xdd, 0x7d,0xd0, 0xfd,0xd1, 0x0d,0xd0, 0x1d,0xdc, 0x1d,0xde, ], [ 0xdd,0xdd, 0x1d,0xde, 0x0d,0xdc, 0xdd,0xd8, 0xfd,0xdc, 0x3d,0xd8, 0x7d,0xde, 0x0d,0xd8, 0xcd,0xd9, 0xcd,0xd9, ], [ 0xcd,0xd9, 0x7d,0xde, 0xe5,0xd8, 0xed,0xd9, 0x7d,0xdc, 0xbd,0xdf, 0x3d,0xdf, 0xf5,0xdb, 0xe5,0xd3, 0xed,0xd9, ], [ 0xed,0xdb, 0x7d,0xde, 0xf5,0xd9, 0xfd,0xd9, 0xbd,0xdc, 0x1d,0xdf, 0x9d,0xdf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0xd9, 0x3d,0xdf, ], [ 0x3d,0xde, 0x1d,0xd8, 0x05,0xd8, 0x0d,0xde, 0xfd,0xdc, 0x0d,0xde, 0x3d,0xdc, 0xbd,0xdf, 0x1d,0xdc, 0xc5,0xdf, ], [ 0xfd,0xdf, 0xfd,0xdf, 0xfd,0xdf, 0xfd,0xdf, 0xfd,0xdf, 0xfd,0xdf, 0xfd,0xdf, 0xfd,0xdf, 0xfd,0xdf, 0xfd,0xdf, ], [ 0x01,0xc0, 0x01,0xc0, 0x01,0xc0, 0x01,0xc0, 0x01,0xc0, 0x01,0xc0, 0x01,0xc0, 0x01,0xc0, 0x01,0xc0, 0x01,0xc0, ], [ 0xff,0xff, 0xff,0xff, 0xff,0xff, 0xff,0xff, 0xff,0xff, 0xff,0xff, 0xff,0xff, 0xff,0xff, 0xff,0xff, 0xff,0xff, ] ] def __init__ (self, number, minDigits=4,inverse=0): """This is a constructor for the class. Parameters: number - the number to be printed out minDigits - the minimum numbers of digits inverse - specify 1 if you want to inverse the font """ self.__contentType = 'image/x-bitmap' self.__numberStr = str (number) self.__inverse = inverse # Initialize some variables self.__charHeight = 20 self.__charWidth = 16 # Set default string length self.__digits = minDigits # Check to see if we have more then 4 digits stringLength = str if len (self.__numberStr) > self.__digits: self.__digits = len (self.__numberStr) else: self.__numberStr = string.zfill (self.__numberStr, self.__digits) def __CreateHeader (self): """This method produces the beginning of the XBM file""" return 'Content-type: %s\n\n'\ '#define counter_width %d\n'\ '#define counter_height %d\n'\ 'static char counter_bits[] = {\n'\ % (self.__contentType, self.__digits*self.__charWidth, self.__charHeight) def __CreateDigits (self): """ This method prints out the digits""" retString = '' i = 0 while i < 20 : for digit in self.__numberStr: digitToPrint = string.atoi(digit) hex1 = XBMNumbers16x20.hex_bytes [i][2*digitToPrint] hex2 = XBMNumbers16x20.hex_bytes [i][2*digitToPrint+1] if self.__inverse: hex1 = hex1 ^ 0xFF hex2 = hex2 ^ 0xFF retString = retString + '0x%02x,0x%02x, ' % (hex1, hex2) retString = retString + '\n' i = i + 1 return retString[:-3] + '\n' def __CreateFooter (self): """This method creates a footer for the XBM file""" return "}\n" def __repr__ (self): return self.__CreateHeader () + self.__CreateDigits () + self.__CreateFooter () if __name__ == '__main__': # This is a test for the class # This will print out 0001234567 in the inverse font XBMObj = XBMNumbers16x20 (1234567, inverse=1, minDigits=10) print XBMObj