def add_font(self, family, style='<STR_LIT>', fname='<STR_LIT>', uni=False):
family = family.lower()<EOL>if (fname == '<STR_LIT>'):<EOL><INDENT>fname = family.replace('<STR_LIT:U+0020>','<STR_LIT>') + style.lower() + '<STR_LIT>'<EOL><DEDENT>if (family == '<STR_LIT>'):<EOL><INDENT>family = '<STR_LIT>'<EOL><DEDENT>style = style.upper()<EOL>if (style == '<STR_LIT>'):<EOL><INDENT>style = '<STR_LIT>'<EOL><DEDENT>fontkey = family+style<EOL>if fontkey in self.fonts:<EOL><INDENT>return<EOL><DEDENT>if (uni):<EOL><INDENT>global SYSTEM_TTFONTS, FPDF_CACHE_MODE, FPDF_CACHE_DIR<EOL>if os.path.exists(fname):<EOL><INDENT>ttffilename = fname<EOL><DEDENT>elif (FPDF_FONT_DIR and<EOL>os.path.exists(os.path.join(FPDF_FONT_DIR, fname))):<EOL><INDENT>ttffilename = os.path.join(FPDF_FONT_DIR, fname)<EOL><DEDENT>elif (SYSTEM_TTFONTS and<EOL>os.path.exists(os.path.join(SYSTEM_TTFONTS, fname))):<EOL><INDENT>ttffilename = os.path.join(SYSTEM_TTFONTS, fname)<EOL><DEDENT>else:<EOL><INDENT>raise RuntimeError("<STR_LIT>" % fname)<EOL><DEDENT>name = '<STR_LIT>'<EOL>if FPDF_CACHE_MODE == <NUM_LIT:0>:<EOL><INDENT>unifilename = os.path.splitext(ttffilename)[<NUM_LIT:0>] + '<STR_LIT>'<EOL><DEDENT>elif FPDF_CACHE_MODE == <NUM_LIT:2>:                <EOL><INDENT>unifilename = os.path.join(FPDF_CACHE_DIR,hashpath(ttffilename) + "<STR_LIT>")<EOL><DEDENT>else:<EOL><INDENT>unifilename = None<EOL><DEDENT>if unifilename and os.path.exists(unifilename):<EOL><INDENT>fh = open(unifilename, "<STR_LIT:rb>")<EOL>try:<EOL><INDENT>font_dict = pickle.load(fh)<EOL><DEDENT>finally:<EOL><INDENT>fh.close()<EOL><DEDENT><DEDENT>else:<EOL><INDENT>ttf = TTFontFile()<EOL>ttf.getMetrics(ttffilename)<EOL>desc = {<EOL>'<STR_LIT>': int(round(ttf.ascent, <NUM_LIT:0>)),<EOL>'<STR_LIT>': int(round(ttf.descent, <NUM_LIT:0>)),<EOL>'<STR_LIT>': int(round(ttf.capHeight, <NUM_LIT:0>)),<EOL>'<STR_LIT>': ttf.flags,<EOL>'<STR_LIT>': "<STR_LIT>" % (<EOL>int(round(ttf.bbox[<NUM_LIT:0>], <NUM_LIT:0>)),<EOL>int(round(ttf.bbox[<NUM_LIT:1>], <NUM_LIT:0>)),<EOL>int(round(ttf.bbox[<NUM_LIT:2>], <NUM_LIT:0>)),<EOL>int(round(ttf.bbox[<NUM_LIT:3>], <NUM_LIT:0>))),<EOL>'<STR_LIT>': int(ttf.italicAngle),<EOL>'<STR_LIT>': int(round(ttf.stemV, <NUM_LIT:0>)),<EOL>'<STR_LIT>': int(round(ttf.defaultWidth, <NUM_LIT:0>)),<EOL>}<EOL>font_dict = {<EOL>'<STR_LIT:name>': re.sub('<STR_LIT>', '<STR_LIT>', ttf.fullName),<EOL>'<STR_LIT:type>': '<STR_LIT>',<EOL>'<STR_LIT>': desc,<EOL>'<STR_LIT>': round(ttf.underlinePosition),<EOL>'<STR_LIT>': round(ttf.underlineThickness),<EOL>'<STR_LIT>': ttffilename,<EOL>'<STR_LIT>': fontkey,<EOL>'<STR_LIT>': os.stat(ttffilename).st_size,<EOL>'<STR_LIT>': ttf.charWidths,<EOL>}<EOL>if unifilename:<EOL><INDENT>try:<EOL><INDENT>fh = open(unifilename, "<STR_LIT:wb>")<EOL>pickle.dump(font_dict, fh)<EOL>fh.close()<EOL><DEDENT>except IOError:<EOL><INDENT>if not exception().errno == errno.EACCES:<EOL><INDENT>raise  <EOL><DEDENT><DEDENT><DEDENT>del ttf<EOL><DEDENT>if hasattr(self,'<STR_LIT>'):<EOL><INDENT>sbarr = list(range(<NUM_LIT:0>,<NUM_LIT>))   <EOL><DEDENT>else:<EOL><INDENT>sbarr = list(range(<NUM_LIT:0>,<NUM_LIT:32>))<EOL><DEDENT>self.fonts[fontkey] = {<EOL>'<STR_LIT:i>': len(self.fonts)+<NUM_LIT:1>, '<STR_LIT:type>': font_dict['<STR_LIT:type>'],<EOL>'<STR_LIT:name>': font_dict['<STR_LIT:name>'], '<STR_LIT>': font_dict['<STR_LIT>'],<EOL>'<STR_LIT>': font_dict['<STR_LIT>'], '<STR_LIT>': font_dict['<STR_LIT>'],<EOL>'<STR_LIT>': font_dict['<STR_LIT>'],<EOL>'<STR_LIT>': font_dict['<STR_LIT>'], '<STR_LIT>': fontkey,<EOL>'<STR_LIT>': sbarr, '<STR_LIT>': unifilename,<EOL>}<EOL>self.font_files[fontkey] = {'<STR_LIT>': font_dict['<STR_LIT>'],<EOL>'<STR_LIT:type>': "<STR_LIT>", '<STR_LIT>': ttffilename}<EOL>self.font_files[fname] = {'<STR_LIT:type>': "<STR_LIT>"}<EOL><DEDENT>else:<EOL><INDENT>fontfile = open(fname)<EOL>try:<EOL><INDENT>font_dict = pickle.load(fontfile)<EOL><DEDENT>finally:<EOL><INDENT>fontfile.close()<EOL><DEDENT>self.fonts[fontkey] = {'<STR_LIT:i>': len(self.fonts)+<NUM_LIT:1>}<EOL>self.fonts[fontkey].update(font_dict)<EOL>if (diff):<EOL><INDENT>d = <NUM_LIT:0><EOL>nb = len(self.diffs)<EOL>for i in range(<NUM_LIT:1>, nb+<NUM_LIT:1>):<EOL><INDENT>if(self.diffs[i] == diff):<EOL><INDENT>d = i<EOL>break<EOL><DEDENT><DEDENT>if (d == <NUM_LIT:0>):<EOL><INDENT>d = nb + <NUM_LIT:1><EOL>self.diffs[d] = diff<EOL><DEDENT>self.fonts[fontkey]['<STR_LIT>'] = d<EOL><DEDENT>filename = font_dict.get('<STR_LIT:filename>')<EOL>if (filename):<EOL><INDENT>if (type == '<STR_LIT>'):<EOL><INDENT>self.font_files[filename]={'<STR_LIT>': originalsize}<EOL><DEDENT>else:<EOL><INDENT>self.font_files[filename]={'<STR_LIT>': size1,<EOL>'<STR_LIT>': size2}<EOL><DEDENT><DEDENT><DEDENT>
Add a TrueType or Type1 font