home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_1097 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-10-31  |  4.6 KB  |  127 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import sys
  5. import os
  6. import tempfile
  7. from calibre.ebooks.rtf2xml import copy
  8.  
  9. class Fonts:
  10.     
  11.     def __init__(self, in_file, bug_handler, default_font_num, copy = None, run_level = 1):
  12.         self._Fonts__file = in_file
  13.         self._Fonts__bug_handler = bug_handler
  14.         self._Fonts__copy = copy
  15.         self._Fonts__default_font_num = default_font_num
  16.         self._Fonts__write_to = tempfile.mktemp()
  17.         self._Fonts__run_level = run_level
  18.  
  19.     
  20.     def __initiate_values(self):
  21.         self._Fonts__special_font_dict = {
  22.             'Symbol': 0,
  23.             'Wingdings': 0,
  24.             'Zapf Dingbats': 0 }
  25.         self._Fonts__special_font_list = [
  26.             'Symbol',
  27.             'Wingdings',
  28.             'Zapf Dingbats']
  29.         self._Fonts__state = 'default'
  30.         self._Fonts__state_dict = {
  31.             'default': self._Fonts__default_func,
  32.             'font_table': self._Fonts__font_table_func,
  33.             'after_font_table': self._Fonts__after_font_table_func,
  34.             'font_in_table': self._Fonts__font_in_table_func }
  35.         self._Fonts__font_table = { }
  36.         self._Fonts__wrote_ind_font = 0
  37.  
  38.     
  39.     def __default_func(self, line):
  40.         if self._Fonts__token_info == 'mi<mk<fonttb-beg':
  41.             self._Fonts__state = 'font_table'
  42.         
  43.         self._Fonts__write_obj.write(line)
  44.  
  45.     
  46.     def __font_table_func(self, line):
  47.         if self._Fonts__token_info == 'mi<mk<fonttb-end':
  48.             self._Fonts__state = 'after_font_table'
  49.         elif self._Fonts__token_info == 'mi<mk<fontit-beg':
  50.             self._Fonts__state = 'font_in_table'
  51.             self._Fonts__font_num = self._Fonts__default_font_num
  52.             self._Fonts__text_line = ''
  53.         
  54.  
  55.     
  56.     def __font_in_table_func(self, line):
  57.         if self._Fonts__token_info == 'mi<mk<fontit-end':
  58.             self._Fonts__wrote_ind_font = 1
  59.             self._Fonts__state = 'font_table'
  60.             self._Fonts__text_line = self._Fonts__text_line[:-1]
  61.             self._Fonts__font_table[self._Fonts__font_num] = self._Fonts__text_line
  62.             self._Fonts__write_obj.write('mi<tg<empty-att_<font-in-table<name>%s<num>%s\n' % (self._Fonts__text_line, self._Fonts__font_num))
  63.         elif self._Fonts__token_info == 'cw<ci<font-style':
  64.             self._Fonts__font_num = line[20:-1]
  65.         elif self._Fonts__token_info == 'tx<nu<__________' or self._Fonts__token_info == 'tx<ut<__________':
  66.             self._Fonts__text_line += line[17:-1]
  67.         elif self._Fonts__token_info == 'mi<mk<fonttb-end':
  68.             self._Fonts__found_end_font_table_func()
  69.             self._Fonts__state = 'after_font_table'
  70.         
  71.  
  72.     
  73.     def __found_end_font_table_func(self):
  74.         if not self._Fonts__wrote_ind_font:
  75.             self._Fonts__write_obj.write('mi<tg<empty-att_<font-in-table<name>Times<num>0\n')
  76.         
  77.  
  78.     
  79.     def __after_font_table_func(self, line):
  80.         if self._Fonts__token_info == 'cw<ci<font-style':
  81.             font_num = line[20:-1]
  82.             font_name = self._Fonts__font_table.get(font_num)
  83.             if font_name == None:
  84.                 if self._Fonts__run_level > 3:
  85.                     msg = 'no value for %s in self.__font_table\n' % font_num
  86.                     raise self._Fonts__bug_handler, msg
  87.                 self._Fonts__run_level > 3
  88.             elif font_name in self._Fonts__special_font_list:
  89.                 self._Fonts__special_font_dict[font_name] = 1
  90.             
  91.             self._Fonts__write_obj.write('cw<ci<font-style<nu<%s\n' % font_name)
  92.         else:
  93.             self._Fonts__write_obj.write(line)
  94.  
  95.     
  96.     def convert_fonts(self):
  97.         self._Fonts__initiate_values()
  98.         read_obj = open(self._Fonts__file, 'r')
  99.         self._Fonts__write_obj = open(self._Fonts__write_to, 'w')
  100.         line_to_read = 1
  101.         while line_to_read:
  102.             line_to_read = read_obj.readline()
  103.             line = line_to_read
  104.             self._Fonts__token_info = line[:16]
  105.             action = self._Fonts__state_dict.get(self._Fonts__state)
  106.             if action == None:
  107.                 sys.stderr.write('no no matching state in module fonts.py\n')
  108.                 sys.stderr.write(self._Fonts__state + '\n')
  109.             
  110.             action(line)
  111.         read_obj.close()
  112.         self._Fonts__write_obj.close()
  113.         default_font_name = self._Fonts__font_table.get(self._Fonts__default_font_num)
  114.         if not default_font_name:
  115.             default_font_name = 'Not Defined'
  116.         
  117.         self._Fonts__special_font_dict['default-font'] = default_font_name
  118.         copy_obj = copy.Copy(bug_handler = self._Fonts__bug_handler)
  119.         if self._Fonts__copy:
  120.             copy_obj.copy_file(self._Fonts__write_to, 'fonts.data')
  121.         
  122.         copy_obj.rename(self._Fonts__write_to, self._Fonts__file)
  123.         os.remove(self._Fonts__write_to)
  124.         return self._Fonts__special_font_dict
  125.  
  126.  
  127.