def embedTTF(self, filename, characters):
font = FT2Font(str(filename))<EOL>fonttype = rcParams['<STR_LIT>']<EOL>def cvt(length, upe=font.units_per_EM, nearest=True):<EOL><INDENT>"<STR_LIT>"<EOL>value = length / upe * <NUM_LIT:1000><EOL>if nearest: return round(value)<EOL>if value < <NUM_LIT:0>: return floor(value)<EOL>else: return ceil(value)<EOL><DEDENT>def embedTTFType3(font, characters, descriptor):<EOL><INDENT>"""<STR_LIT>"""<EOL>widthsObject = self.reserveObject('<STR_LIT>')<EOL>fontdescObject = self.reserveObject('<STR_LIT>')<EOL>fontdictObject = self.reserveObject('<STR_LIT>')<EOL>charprocsObject = self.reserveObject('<STR_LIT>')<EOL>differencesArray = []<EOL>firstchar, lastchar = <NUM_LIT:0>, <NUM_LIT:255><EOL>bbox = [cvt(x, nearest=False) for x in font.bbox]<EOL>fontdict = {<EOL>'<STR_LIT>'            : Name('<STR_LIT>'),<EOL>'<STR_LIT>'        : ps_name,<EOL>'<STR_LIT>'       : firstchar,<EOL>'<STR_LIT>'        : lastchar,<EOL>'<STR_LIT>'  : fontdescObject,<EOL>'<STR_LIT>'         : Name('<STR_LIT>'),<EOL>'<STR_LIT:Name>'            : descriptor['<STR_LIT>'],<EOL>'<STR_LIT>'        : bbox,<EOL>'<STR_LIT>'      : [ <NUM_LIT>, <NUM_LIT:0>, <NUM_LIT:0>, <NUM_LIT>, <NUM_LIT:0>, <NUM_LIT:0> ],<EOL>'<STR_LIT>'       : charprocsObject,<EOL>'<STR_LIT>'        : {<EOL>'<STR_LIT>'        : Name('<STR_LIT>'),<EOL>'<STR_LIT>' : differencesArray},<EOL>'<STR_LIT>'          : widthsObject<EOL>}<EOL>from encodings import cp1252<EOL>if hasattr(cp1252, '<STR_LIT>'):<EOL><INDENT>def decode_char(charcode):<EOL><INDENT>return cp1252.decoding_map[charcode] or <NUM_LIT:0><EOL><DEDENT><DEDENT>else:<EOL><INDENT>def decode_char(charcode):<EOL><INDENT>return ord(cp1252.decoding_table[charcode])<EOL><DEDENT><DEDENT>def get_char_width(charcode):<EOL><INDENT>str = decode_char(charcode)<EOL>width = font.load_char(str, flags=LOAD_NO_SCALE|LOAD_NO_HINTING).horiAdvance<EOL>return cvt(width)<EOL><DEDENT>widths = [ get_char_width(charcode) for charcode in range(firstchar, lastchar+<NUM_LIT:1>) ]<EOL>descriptor['<STR_LIT>'] = max(widths)<EOL>cmap = font.get_charmap()<EOL>glyph_ids = []<EOL>differences = []<EOL>multi_byte_chars = set()<EOL>for c in characters:<EOL><INDENT>ccode = c<EOL>gind = cmap.get(ccode) or <NUM_LIT:0><EOL>glyph_ids.append(gind)<EOL>glyph_name = font.get_glyph_name(gind)<EOL>if ccode <= <NUM_LIT:255>:<EOL><INDENT>differences.append((ccode, glyph_name))<EOL><DEDENT>else:<EOL><INDENT>multi_byte_chars.add(glyph_name)<EOL><DEDENT><DEDENT>differences.sort()<EOL>last_c = -<NUM_LIT:2><EOL>for c, name in differences:<EOL><INDENT>if c != last_c + <NUM_LIT:1>:<EOL><INDENT>differencesArray.append(c)<EOL><DEDENT>differencesArray.append(Name(name))<EOL>last_c = c<EOL><DEDENT>rawcharprocs = ttconv.get_pdf_charprocs(filename, glyph_ids)<EOL>charprocs = {}<EOL>charprocsRef = {}<EOL>for charname, stream in list(rawcharprocs.items()):<EOL><INDENT>charprocDict = { '<STR_LIT>': len(stream) }<EOL>if charname in multi_byte_chars:<EOL><INDENT>charprocDict['<STR_LIT>'] = Name('<STR_LIT>')<EOL>charprocDict['<STR_LIT>'] = Name('<STR_LIT>')<EOL>charprocDict['<STR_LIT>'] = bbox<EOL>stream = stream[stream.find("<STR_LIT>") + <NUM_LIT:2>:]<EOL><DEDENT>charprocObject = self.reserveObject('<STR_LIT>')<EOL>self.beginStream(charprocObject.id, None, charprocDict)<EOL>self.currentstream.write(stream)<EOL>self.endStream()<EOL>if charname in multi_byte_chars:<EOL><INDENT>name = self._get_xobject_symbol_name(filename, charname)<EOL>self.multi_byte_charprocs[name] = charprocObject<EOL><DEDENT>else:<EOL><INDENT>charprocs[charname] = charprocObject<EOL><DEDENT><DEDENT>self.writeObject(fontdictObject, fontdict)<EOL>self.writeObject(fontdescObject, descriptor)<EOL>self.writeObject(widthsObject, widths)<EOL>self.writeObject(charprocsObject, charprocs)<EOL>return fontdictObject<EOL><DEDENT>def embedTTFType42(font, characters, descriptor):<EOL><INDENT>"""<STR_LIT>"""<EOL>fontdescObject = self.reserveObject('<STR_LIT>')<EOL>cidFontDictObject = self.reserveObject('<STR_LIT>')<EOL>type0FontDictObject = self.reserveObject('<STR_LIT>')<EOL>cidToGidMapObject = self.reserveObject('<STR_LIT>')<EOL>fontfileObject = self.reserveObject('<STR_LIT>')<EOL>wObject = self.reserveObject('<STR_LIT>')<EOL>toUnicodeMapObject = self.reserveObject('<STR_LIT>')<EOL>cidFontDict = {<EOL>'<STR_LIT>'           : Name('<STR_LIT>'),<EOL>'<STR_LIT>'        : Name('<STR_LIT>'),<EOL>'<STR_LIT>'       : ps_name,<EOL>'<STR_LIT>'  : {<EOL>'<STR_LIT>'   : '<STR_LIT>',<EOL>'<STR_LIT>'   : '<STR_LIT>',<EOL>'<STR_LIT>' : <NUM_LIT:0> },<EOL>'<STR_LIT>' : fontdescObject,<EOL>'<STR_LIT>'              : wObject,<EOL>'<STR_LIT>'    : cidToGidMapObject<EOL>}<EOL>type0FontDict = {<EOL>'<STR_LIT>'            : Name('<STR_LIT>'),<EOL>'<STR_LIT>'         : Name('<STR_LIT>'),<EOL>'<STR_LIT>'        : ps_name,<EOL>'<STR_LIT>'        : Name('<STR_LIT>'),<EOL>'<STR_LIT>' : [cidFontDictObject],<EOL>'<STR_LIT>'       : toUnicodeMapObject<EOL>}<EOL>descriptor['<STR_LIT>'] = fontfileObject<EOL>length1Object = self.reserveObject('<STR_LIT>')<EOL>self.beginStream(<EOL>fontfileObject.id,<EOL>self.reserveObject('<STR_LIT>'),<EOL>{'<STR_LIT>': length1Object})<EOL>fontfile = open(filename, '<STR_LIT:rb>')<EOL>length1 = <NUM_LIT:0><EOL>while True:<EOL><INDENT>data = fontfile.read(<NUM_LIT>)<EOL>if not data: break<EOL>length1 += len(data)<EOL>self.currentstream.write(data)<EOL><DEDENT>fontfile.close()<EOL>self.endStream()<EOL>self.writeObject(length1Object, length1)<EOL>cid_to_gid_map = ['<STR_LIT>'] * <NUM_LIT><EOL>cmap = font.get_charmap()<EOL>unicode_mapping = []<EOL>widths = []<EOL>max_ccode = <NUM_LIT:0><EOL>for c in characters:<EOL><INDENT>ccode = c<EOL>gind = cmap.get(ccode) or <NUM_LIT:0><EOL>glyph = font.load_char(ccode, flags=LOAD_NO_HINTING)<EOL>widths.append((ccode, glyph.horiAdvance / <NUM_LIT:6>))<EOL>if ccode < <NUM_LIT>:<EOL><INDENT>cid_to_gid_map[ccode] = chr(gind)<EOL><DEDENT>max_ccode = max(ccode, max_ccode)<EOL><DEDENT>widths.sort()<EOL>cid_to_gid_map = cid_to_gid_map[:max_ccode + <NUM_LIT:1>]<EOL>last_ccode = -<NUM_LIT:2><EOL>w = []<EOL>max_width = <NUM_LIT:0><EOL>unicode_groups = []<EOL>for ccode, width in widths:<EOL><INDENT>if ccode != last_ccode + <NUM_LIT:1>:<EOL><INDENT>w.append(ccode)<EOL>w.append([width])<EOL>unicode_groups.append([ccode, ccode])<EOL><DEDENT>else:<EOL><INDENT>w[-<NUM_LIT:1>].append(width)<EOL>unicode_groups[-<NUM_LIT:1>][<NUM_LIT:1>] = ccode<EOL><DEDENT>max_width = max(max_width, width)<EOL>last_ccode = ccode<EOL><DEDENT>unicode_bfrange = []<EOL>for start, end in unicode_groups:<EOL><INDENT>unicode_bfrange.append(<EOL>"<STR_LIT>" %<EOL>(start, end,<EOL>"<STR_LIT:U+0020>".join(["<STR_LIT>" % x for x in range(start, end+<NUM_LIT:1>)])))<EOL><DEDENT>unicode_cmap = (self._identityToUnicodeCMap %<EOL>(len(unicode_groups),<EOL>"<STR_LIT:\n>".join(unicode_bfrange)))<EOL>cid_to_gid_map = "<STR_LIT>".join(cid_to_gid_map).encode("<STR_LIT>")<EOL>self.beginStream(cidToGidMapObject.id,<EOL>None,<EOL>{'<STR_LIT>':  len(cid_to_gid_map)})<EOL>self.currentstream.write(cid_to_gid_map)<EOL>self.endStream()<EOL>self.beginStream(toUnicodeMapObject.id,<EOL>None,<EOL>{'<STR_LIT>': unicode_cmap})<EOL>self.currentstream.write(unicode_cmap)<EOL>self.endStream()<EOL>descriptor['<STR_LIT>'] = max_width<EOL>self.writeObject(cidFontDictObject, cidFontDict)<EOL>self.writeObject(type0FontDictObject, type0FontDict)<EOL>self.writeObject(fontdescObject, descriptor)<EOL>self.writeObject(wObject, w)<EOL>return type0FontDictObject<EOL><DEDENT>ps_name = Name(font.get_sfnt()[(<NUM_LIT:1>,<NUM_LIT:0>,<NUM_LIT:0>,<NUM_LIT:6>)])<EOL>pclt = font.get_sfnt_table('<STR_LIT>')or { '<STR_LIT>': <NUM_LIT:0>, '<STR_LIT>': <NUM_LIT:0> }<EOL>post = font.get_sfnt_table('<STR_LIT>')or { '<STR_LIT>': (<NUM_LIT:0>,<NUM_LIT:0>) }<EOL>ff = font.face_flags<EOL>sf = font.style_flags<EOL>flags = <NUM_LIT:0><EOL>symbolic = False <EOL>if ff & FIXED_WIDTH: flags |= <NUM_LIT:1> << <NUM_LIT:0><EOL>if <NUM_LIT:0>: flags |= <NUM_LIT:1> << <NUM_LIT:1> <EOL>if symbolic: flags |= <NUM_LIT:1> << <NUM_LIT:2><EOL>else: flags |= <NUM_LIT:1> << <NUM_LIT:5><EOL>if sf & ITALIC: flags |= <NUM_LIT:1> << <NUM_LIT:6><EOL>if <NUM_LIT:0>: flags |= <NUM_LIT:1> << <NUM_LIT:16> <EOL>if <NUM_LIT:0>: flags |= <NUM_LIT:1> << <NUM_LIT> <EOL>if <NUM_LIT:0>: flags |= <NUM_LIT:1> << <NUM_LIT> <EOL>descriptor = {<EOL>'<STR_LIT>'        : Name('<STR_LIT>'),<EOL>'<STR_LIT>'    : ps_name,<EOL>'<STR_LIT>'       : flags,<EOL>'<STR_LIT>'    : [ cvt(x, nearest=False) for x in font.bbox ],<EOL>'<STR_LIT>'      : cvt(font.ascender, nearest=False),<EOL>'<STR_LIT>'     : cvt(font.descender, nearest=False),<EOL>'<STR_LIT>'   : cvt(pclt['<STR_LIT>'], nearest=False),<EOL>'<STR_LIT>'     : cvt(pclt['<STR_LIT>']),<EOL>'<STR_LIT>' : post['<STR_LIT>'][<NUM_LIT:1>], <EOL>'<STR_LIT>'       : <NUM_LIT:0> <EOL>}<EOL>if is_opentype_cff_font(filename):<EOL><INDENT>fonttype = <NUM_LIT><EOL>warnings.warn(("<STR_LIT>" +<EOL>"<STR_LIT>") %<EOL>os.path.basename(filename))<EOL><DEDENT>if fonttype == <NUM_LIT:3>:<EOL><INDENT>return embedTTFType3(font, characters, descriptor)<EOL><DEDENT>elif fonttype == <NUM_LIT>:<EOL><INDENT>return embedTTFType42(font, characters, descriptor)<EOL><DEDENT>
Embed the TTF font from the named file into the document.