- 新建FontDump工程,import FPDF库。File ->Setting->Projcet:FontDump->Projcet Interpreter->Double click pip
- 新建FontDump.py文件,导入库FPDF
- 怎么知道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的列表
-
下面是个获取某种字体宽度的代码 获取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]
Python使用FPDF导出字体 widths array
最新推荐文章于 2022-10-02 23:34:24 发布