Python usa FPDF para exportar array de larguras de fonte

  1. Crie um novo projeto FontDump e importe a biblioteca FPDF. Arquivo -> Configuração-> Projcet: FontDump-> Projcet Interpreter-> Pip de duplo clique
  2. Crie um novo arquivo FontDump.py e importe a biblioteca FPDF
  3. Como você conhece todas as funções ou métodos do FPDF? Dê uma olhada na biblioteca na próxima semana em nosso diretório de projeto D: \ Python \ FontDump \ env \ Lib \ site-packages \ fpdf Há um arquivo ttfonts.py, dê uma olhada no conteúdo dentro
    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.....

    Parece que self.charWidths é a lista de larguras de fonte de que precisamos, mas o que quero dizer aqui é que esta é uma lista de tamanho de 65536

  4. Abaixo está um código para obter a largura de uma determinada fonte para obter a largura da fonte 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()

    Saída: (a premissa é que você deve colocar o arquivo Helvetica.ttf no diretório onde seu arquivo python está localizado)

    
    [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]

     

Acho que você gosta

Origin blog.csdn.net/yangkunhenry/article/details/103436411
Recomendado
Clasificación