Embed the TTF font from the named file into the document.
(self, filename, characters)
| 878 | end""" |
| 879 | |
| 880 | def embedTTF(self, filename, characters): |
| 881 | """Embed the TTF font from the named file into the document.""" |
| 882 | |
| 883 | font = get_font(filename) |
| 884 | fonttype = rcParams['pdf.fonttype'] |
| 885 | |
| 886 | def cvt(length, upe=font.units_per_EM, nearest=True): |
| 887 | "Convert font coordinates to PDF glyph coordinates" |
| 888 | value = length / upe * 1000 |
| 889 | if nearest: |
| 890 | return np.round(value) |
| 891 | # Perhaps best to round away from zero for bounding |
| 892 | # boxes and the like |
| 893 | if value < 0: |
| 894 | return floor(value) |
| 895 | else: |
| 896 | return ceil(value) |
| 897 | |
| 898 | def embedTTFType3(font, characters, descriptor): |
| 899 | """The Type 3-specific part of embedding a Truetype font""" |
| 900 | widthsObject = self.reserveObject('font widths') |
| 901 | fontdescObject = self.reserveObject('font descriptor') |
| 902 | fontdictObject = self.reserveObject('font dictionary') |
| 903 | charprocsObject = self.reserveObject('character procs') |
| 904 | differencesArray = [] |
| 905 | firstchar, lastchar = 0, 255 |
| 906 | bbox = [cvt(x, nearest=False) for x in font.bbox] |
| 907 | |
| 908 | fontdict = { |
| 909 | 'Type': Name('Font'), |
| 910 | 'BaseFont': ps_name, |
| 911 | 'FirstChar': firstchar, |
| 912 | 'LastChar': lastchar, |
| 913 | 'FontDescriptor': fontdescObject, |
| 914 | 'Subtype': Name('Type3'), |
| 915 | 'Name': descriptor['FontName'], |
| 916 | 'FontBBox': bbox, |
| 917 | 'FontMatrix': [.001, 0, 0, .001, 0, 0], |
| 918 | 'CharProcs': charprocsObject, |
| 919 | 'Encoding': { |
| 920 | 'Type': Name('Encoding'), |
| 921 | 'Differences': differencesArray}, |
| 922 | 'Widths': widthsObject |
| 923 | } |
| 924 | |
| 925 | # Make the "Widths" array |
| 926 | from encodings import cp1252 |
| 927 | # The "decoding_map" was changed |
| 928 | # to a "decoding_table" as of Python 2.5. |
| 929 | if hasattr(cp1252, 'decoding_map'): |
| 930 | def decode_char(charcode): |
| 931 | return cp1252.decoding_map[charcode] or 0 |
| 932 | else: |
| 933 | def decode_char(charcode): |
| 934 | return ord(cp1252.decoding_table[charcode]) |
| 935 | |
| 936 | def get_char_width(charcode): |
| 937 | s = decode_char(charcode) |
no test coverage detected