######################################################################### # # # # # copyright 2002 Paul Henry Tremblay # # # # This program is distributed in the hope that it will be useful, # # but WITHOUT ANY WARRANTY; without even the implied warranty of # # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # # General Public License for more details. # # # # # ######################################################################### import sys, os from ebook_converter.ebooks.rtf2xml import copy from ebook_converter.ptempfile import better_mktemp from . import open_for_read, open_for_write class Fonts: """ Change lines with font info from font numbers to the actual font names. """ def __init__(self, in_file, bug_handler, default_font_num, copy=None, run_level=1, ): """ Required: 'file'--file to parse 'default_font_num'--the default font number Optional: 'copy'-- whether to make a copy of result for debugging 'temp_dir' --where to output temporary results (default is directory from which the script is run.) Returns: nothing """ self.__file = in_file self.__bug_handler = bug_handler self.__copy = copy self.__default_font_num = default_font_num self.__write_to = better_mktemp() self.__run_level = run_level def __initiate_values(self): """ Initiate all values. """ self.__special_font_dict = { 'Symbol' : 0, 'Wingdings' : 0, 'Zapf Dingbats' : 0, } self.__special_font_list = [ 'Symbol', 'Wingdings', 'Zapf Dingbats' ] self.__state = 'default' self.__state_dict = { 'default' : self.__default_func, 'font_table' : self.__font_table_func, 'after_font_table' : self.__after_font_table_func, 'font_in_table' : self.__font_in_table_func, } self.__font_table = {} # individual font written self.__wrote_ind_font = 0 def __default_func(self, line): """ Requires: line Returns: nothing Handle all lines before the font table. Check for the beginning of the font table. If found, change the state. Print out all lines. """ if self.__token_info == 'mi value pair to the font-table dictionary. Also create an empty tag with the name and number as attributes. Preamture end of font table """ # cw%s%s\n' % (self.__text_line, self.__font_num) ) elif self.__token_info == 'cwTimes0\n') def __after_font_table_func(self, line): """ Required: line Returns: nothing Logic: Check the self.__token_info. If this matches a token with font info, then extract the number from the line, and look up the font name in the font dictionary. If no name exists for that number, print out an error. Otherwise print out the same line, except with the name rather than the number. If the line does not contain font info, simply print it out to the file. """ if self.__token_info == 'cw 3: msg = 'no value for %s in self.__font_table\n' % font_num raise self.__bug_handler(msg) else: # self.__special_font_dict if font_name in self.__special_font_list: self.__special_font_dict[font_name] = 1 self.__write_obj.write( 'cw