'Embed the TTF font from the named file into the document.'

def embedTTF(self, filename, characters):

    font = get_font(filename)
    fonttype = rcParams[u'pdf.fonttype']
    def cvt(length, upe=font.units_per_EM, nearest=True):
        u'Convert    font    coordinates    to    PDF    glyph    coordinates'
        value = ((length / upe) * 1000)
        if nearest:
            return np.round(value)
        if (value < 0):
            return floor(value)
        else:
            return ceil(value)
    def embedTTFType3(font, characters, descriptor):
        u'The    Type    3-specific    part    of    embedding    a    Truetype    font'
        widthsObject = self.reserveObject(u'font    widths')
        fontdescObject = self.reserveObject(u'font    descriptor')
        fontdictObject = self.reserveObject(u'font    dictionary')
        charprocsObject = self.reserveObject(u'character    procs')
        differencesArray = []
        (firstchar, lastchar) = (0, 255)
        bbox = [cvt(x, nearest=False) for x in font.bbox]
        fontdict = {u'Type': Name(u'Font'), u'BaseFont': ps_name, u'FirstChar': firstchar, u'LastChar': lastchar, u'FontDescriptor': fontdescObject, u'Subtype': Name(u'Type3'), u'Name': descriptor[u'FontName'], u'FontBBox': bbox, u'FontMatrix': [0.001, 0, 0, 0.001, 0, 0], u'CharProcs': charprocsObject, u'Encoding': {u'Type': Name(u'Encoding'), u'Differences': differencesArray}, u'Widths': widthsObject}
        from encodings import cp1252
        if hasattr(cp1252, u'decoding_map'):
            def decode_char(charcode):
                return (cp1252.decoding_map[charcode] or 0)
        else:
            def decode_char(charcode):
                return ord(cp1252.decoding_table[charcode])
        def get_char_width(charcode):
            s = decode_char(charcode)
            width = font.load_char(s, flags=(LOAD_NO_SCALE | LOAD_NO_HINTING)).horiAdvance
            return cvt(width)
        widths = [get_char_width(charcode) for charcode in range(firstchar, (lastchar + 1))]
        descriptor[u'MaxWidth'] = max(widths)
        glyph_ids = []
        differences = []
        multi_byte_chars = set()
        for c in characters:
            ccode = c
            gind = font.get_char_index(ccode)
            glyph_ids.append(gind)
            glyph_name = font.get_glyph_name(gind)
            if (ccode <= 255):
                differences.append((ccode, glyph_name))
            else:
                multi_byte_chars.add(glyph_name)
        differences.sort()
        last_c = (-2)
        for (c, name) in differences:
            if (c != (last_c + 1)):
                differencesArray.append(c)
            differencesArray.append(Name(name))
            last_c = c
        rawcharprocs = ttconv.get_pdf_charprocs(filename.encode(sys.getfilesystemencoding()), glyph_ids)
        charprocs = {}
        for charname in sorted(rawcharprocs):
            stream = rawcharprocs[charname]
            charprocDict = {u'Length': len(stream)}
            if (charname in multi_byte_chars):
                charprocDict[u'Type'] = Name(u'XObject')
                charprocDict[u'Subtype'] = Name(u'Form')
                charprocDict[u'BBox'] = bbox
                stream = stream[(stream.find('d1') + 2):]
            charprocObject = self.reserveObject(u'charProc')
            self.beginStream(charprocObject.id, None, charprocDict)
            self.currentstream.write(stream)
            self.endStream()
            if (charname in multi_byte_chars):
                name = self._get_xobject_symbol_name(filename, charname)
                self.multi_byte_charprocs[name] = charprocObject
            else:
                charprocs[charname] = charprocObject
        self.writeObject(fontdictObject, fontdict)
        self.writeObject(fontdescObject, descriptor)
        self.writeObject(widthsObject, widths)
        self.writeObject(charprocsObject, charprocs)
        return fontdictObject
    def embedTTFType42(font, characters, descriptor):
        u'The    Type    42-specific    part    of    embedding    a    Truetype    font'
        fontdescObject = self.reserveObject(u'font    descriptor')
        cidFontDictObject = self.reserveObject(u'CID    font    dictionary')
        type0FontDictObject = self.reserveObject(u'Type    0    font    dictionary')
        cidToGidMapObject = self.reserveObject(u'CIDToGIDMap    stream')
        fontfileObject = self.reserveObject(u'font    file    stream')
        wObject = self.reserveObject(u'Type    0    widths')
        toUnicodeMapObject = self.reserveObject(u'ToUnicode    map')
        cidFontDict = {u'Type': Name(u'Font'), u'Subtype': Name(u'CIDFontType2'), u'BaseFont': ps_name, u'CIDSystemInfo': {u'Registry': u'Adobe', u'Ordering': u'Identity', u'Supplement': 0}, u'FontDescriptor': fontdescObject, u'W': wObject, u'CIDToGIDMap': cidToGidMapObject}
        type0FontDict = {u'Type': Name(u'Font'), u'Subtype': Name(u'Type0'), u'BaseFont': ps_name, u'Encoding': Name(u'Identity-H'), u'DescendantFonts': [cidFontDictObject], u'ToUnicode': toUnicodeMapObject}
        descriptor[u'FontFile2'] = fontfileObject
        length1Object = self.reserveObject(u'decoded    length    of    a    font')
        self.beginStream(fontfileObject.id, self.reserveObject(u'length    of    font    stream'), {u'Length1': length1Object})
        with open(filename, u'rb') as fontfile:
            length1 = 0
            while True:
                data = fontfile.read(4096)
                if (not data):
                    break
                length1 += len(data)
                self.currentstream.write(data)
        self.endStream()
        self.writeObject(length1Object, length1)
        cid_to_gid_map = ([u'\x00'] * 65536)
        widths = []
        max_ccode = 0
        for c in characters:
            ccode = c
            gind = font.get_char_index(ccode)
            glyph = font.load_char(ccode, flags=(LOAD_NO_SCALE | LOAD_NO_HINTING))
            widths.append((ccode, cvt(glyph.horiAdvance)))
            if (ccode < 65536):
                cid_to_gid_map[ccode] = unichr(gind)
            max_ccode = max(ccode, max_ccode)
        widths.sort()
        cid_to_gid_map = cid_to_gid_map[:(max_ccode + 1)]
        last_ccode = (-2)
        w = []
        max_width = 0
        unicode_groups = []
        for (ccode, width) in widths:
            if (ccode != (last_ccode + 1)):
                w.append(ccode)
                w.append([width])
                unicode_groups.append([ccode, ccode])
            else:
                w[(-1)].append(width)
                unicode_groups[(-1)][1] = ccode
            max_width = max(max_width, width)
            last_ccode = ccode
        unicode_bfrange = []
        for (start, end) in unicode_groups:
            unicode_bfrange.append((u'<%04x>    <%04x>    [%s]' % (start, end, u'    '.join([(u'<%04x>' % x) for x in range(start, (end + 1))]))))
        unicode_cmap = (self._identityToUnicodeCMap % (len(unicode_groups), u'\n'.join(unicode_bfrange))).encode(u'ascii')
        cid_to_gid_map = u''.join(cid_to_gid_map).encode(u'utf-16be')
        self.beginStream(cidToGidMapObject.id, None, {u'Length': len(cid_to_gid_map)})
        self.currentstream.write(cid_to_gid_map)
        self.endStream()
        self.beginStream(toUnicodeMapObject.id, None, {u'Length': unicode_cmap})
        self.currentstream.write(unicode_cmap)
        self.endStream()
        descriptor[u'MaxWidth'] = max_width
        self.writeObject(cidFontDictObject, cidFontDict)
        self.writeObject(type0FontDictObject, type0FontDict)
        self.writeObject(fontdescObject, descriptor)
        self.writeObject(wObject, w)
        return type0FontDictObject
    sfnt = font.get_sfnt()
    try:
        ps_name = sfnt[(1, 0, 0, 6)].decode(u'macroman')
    except KeyError:
        ps_name = sfnt[(3, 1, 1033, 6)].decode(u'utf-16be')
    ps_name = ps_name.encode(u'ascii', u'replace')
    ps_name = Name(ps_name)
    pclt = (font.get_sfnt_table(u'pclt') or {u'capHeight': 0, u'xHeight': 0})
    post = (font.get_sfnt_table(u'post') or {u'italicAngle': (0, 0)})
    ff = font.face_flags
    sf = font.style_flags
    flags = 0
    symbolic = False
    if (ff & FIXED_WIDTH):
        flags |= (1 << 0)
    if 0:
        flags |= (1 << 1)
    if symbolic:
        flags |= (1 << 2)
    else:
        flags |= (1 << 5)
    if (sf & ITALIC):
        flags |= (1 << 6)
    if 0:
        flags |= (1 << 16)
    if 0:
        flags |= (1 << 17)
    if 0:
        flags |= (1 << 18)
    descriptor = {u'Type': Name(u'FontDescriptor'), u'FontName': ps_name, u'Flags': flags, u'FontBBox': [cvt(x, nearest=False) for x in font.bbox], u'Ascent': cvt(font.ascender, nearest=False), u'Descent': cvt(font.descender, nearest=False), u'CapHeight': cvt(pclt[u'capHeight'], nearest=False), u'XHeight': cvt(pclt[u'xHeight']), u'ItalicAngle': post[u'italicAngle'][1], u'StemV': 0}
    if is_opentype_cff_font(filename):
        fonttype = 42
        msg = u"'%s'    can    not    be    subsetted    into    a    Type    3    font.    The    entire    font    will    be    embedded    in    the    output."
        warnings.warn((msg % os.path.basename(filename)))
    if (fonttype == 3):
        return embedTTFType3(font, characters, descriptor)
    elif (fonttype == 42):
        return embedTTFType42(font, characters, descriptor)
