Python使用FPDF導出字體 widths array

  1. 新建FontDump工程,import FPDF庫。File ->Setting->Projcet:FontDump->Projcet Interpreter->Double click pip
  2. 新建FontDump.py文件,導入庫FPDF
  3. 怎麼知道FPDF的所有函數或者說方法呢?看下剛剛下週的庫到我們的工程目錄下D:\Python\FontDump\env\Lib\site-packages\fpdf 有ttfonts.py這個文件,看看這裏面的內容
    class TTFontFile:
    
        def __init__(self):
            self.maxStrLenRead = 200000    # Maximum size of glyf table to read in as string (otherwise reads each glyph from file)
    
        def getMetrics(self, file):
            self.filename = file
            self.fh = open(file,'rb')
            self._pos = 0
            self.charWidths = []
            self.glyphPos = {}
            self.charToGlyph = {}
            self.tables = {}
            self.otables = {}
            self.ascent = 0
            self.descent = 0
            self.TTCFonts = {}
            self.version = version = self.read_ulong()
            if (version==0x4F54544F):
                die("Postscript outlines are not supported")
            if (version==0x74746366):
                die("ERROR - TrueType Fonts Collections not supported")
            if (version not in (0x00010000,0x74727565)):
                die("Not a TrueType font: version=" + version)
            self.readTableDirectory()
            self.extractInfo()
            self.fh.close()
       .....omit.....

    看起來self.charWidths就是我們需要的字體的寬度列表,不過這裏要說的是這是個65536 size的列表

  4. 下面是個獲取某種字體寬度的代碼 獲取helvetica.ttf 字體的寬度

    from fpdf.ttfonts import TTFontFile
    import os
    
    def main():
        mypath = "D:\Python\FontDump"
        ttf = TTFontFile()
        #ttffile = os.path.join(mypath,"Arial.ttf")
        ttffile = os.path.join(mypath,"Helvetica.ttf")
        ttf.getMetrics(ttffile)
        widlist = ttf.charWidths
        widlist_len = len(widlist)
        wlx1 = widlist[0:256]
        i_max = (len(wlx1)+1)/8
    
        for i in range(int(i_max)):
            if i > i_max:
                break
            wlx1_tmp = wlx1[i*8:(i+1)*8]
            print(wlx1_tmp)
    
    
    if __name__ == "__main__":
        main()

    輸出:(前提是你要在你這個python文件的所在目錄中放入那個Helvetica.ttf的文件)

    
    [339, 0, 0, 0, 0, 0, 0, 0]
    [0, 0, 0, 0, 0, 0, 0, 0]
    [0, 0, 0, 0, 0, 0, 0, 0]
    [0, 0, 0, 0, 0, 0, 0, 0]
    [278, 278, 355, 556, 556, 889, 667, 191]
    [333, 333, 389, 584, 278, 333, 278, 278]
    [556, 556, 556, 556, 556, 556, 556, 556]
    [556, 556, 278, 278, 584, 584, 584, 556]
    [1015, 667, 667, 722, 722, 667, 611, 778]
    [722, 278, 500, 667, 556, 833, 722, 778]
    [667, 778, 722, 667, 611, 722, 667, 944]
    [667, 667, 611, 278, 278, 278, 469, 556]
    [333, 556, 556, 500, 556, 556, 278, 556]
    [556, 222, 222, 500, 222, 833, 556, 556]
    [556, 556, 333, 500, 278, 556, 500, 722]
    [500, 500, 500, 334, 260, 334, 584, 0]
    [0, 0, 0, 0, 0, 0, 0, 0]
    [0, 0, 0, 0, 0, 0, 0, 0]
    [0, 0, 0, 0, 0, 0, 0, 0]
    [0, 0, 0, 0, 0, 0, 0, 0]
    [278, 333, 556, 556, 556, 556, 260, 556]
    [333, 737, 370, 556, 584, 333, 737, 333]
    [400, 584, 333, 333, 333, 556, 537, 278]
    [333, 333, 365, 556, 834, 834, 834, 611]
    [667, 667, 667, 667, 667, 667, 1000, 722]
    [667, 667, 667, 667, 278, 278, 278, 278]
    [722, 722, 778, 778, 778, 778, 778, 584]
    [778, 722, 722, 722, 722, 667, 667, 611]
    [556, 556, 556, 556, 556, 556, 889, 500]
    [556, 556, 556, 556, 278, 278, 278, 278]
    [556, 556, 556, 556, 556, 556, 556, 584]
    [611, 556, 556, 556, 556, 500, 556, 500]

     

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章