Skip to main content
 首页 » 编程设计

python之如何确定字体是符号字体还是文本字体

2025年01月19日9thcjp

我正在构建一个 kivy 应用程序,需要包含一个字体选择器。由于我找不到适合 kivy 的,所以我正在构建自己的。但是我找不到一种方法来判断一种字体是否适合我(即,我可以用它来造句而不是一串符号)。我正在使用 Pillow ImageFont。有什么办法可以区分 python 中的符号字体和文本字体吗?

请您参考如下方法:

我一直在努力,现在我对自己的工作更满意了。仍然不完美,但下面的代码只为我的 Ubuntu 盒子上支持文本的字体提供了一个误报:

def isValidFont(f, fontNum=0, debug=False): 
    """ 
    Determine if a font is a valid font for displaying text 
 
    This code makes a best guess as to whether the font supports text. 
    It does this by writing a 'W' using the font, and inspecting the result. 
 
    :param f: full path to the font file 
    :param fontNum: index into the font file (for a file containing a collection of fonts) 
    :param debug: If True, files will be written for each font with information useful for debugging 
    :return: True if font appears to support text, False otherwise 
    """ 
 
    # size of test image 
    width = 40 
    height = 40 
 
    font = ImageFont.truetype(f, index=fontNum, size=height-6) 
    fontName = font.getname() 
    tmpImg = Image.new('1', (width,height)) #default fill is 0 (black) 
 
    # draw a single 'W' into the test image (sized and positioned to fit inside test image) 
    # this codes depends on the character drawn being a 'W' 
    dr = ImageDraw.Draw(tmpImg) 
    dr.text((3, 3), 'W', font=font, fill=(1)) 
 
    if debug: 
        # save test image for this font 
        fname = str(fontName) + '.bmp' 
        tmpImg.save(fname) 
 
    # get the data from the image as a list of 1's and 0's (one value per pixel) 
    img_data = list(tmpImg.getdata()) 
 
    if debug: 
        # write the image data to a file 
        fname = str(fontName) + '.txt' 
        fd = open(fname, mode='w') 
        for row in range(height): 
            fd.write(str(img_data[row*width : (row+1)*width]) + '\n') 
        fd.close() 
 
    # if the image is all black (0's), this is not a valid text font 
    if sum(img_data) == 0: 
        return False 
 
    # build a simplified version of the image data 
    compressedList = [] 
    for i in range(height): 
        prev_elem = None 
        thisRow = [] 
        for j in range(width): 
            index = i*width + j 
            elem = img_data[index] # this is the element at (i,j) 
            if prev_elem is None: 
                # first element in this row, just append to "thisRow" 
                thisRow.append(elem) 
                prev_elem = elem 
            elif elem == prev_elem: 
                # if this element is same as previous (and it's a one), just increment the value in "thisRow" 
                if elem == 1: 
                    thisRow[len(thisRow)-1] += 1 
            else: 
                # just append the element to "thisRow" 
                thisRow.append(elem) 
                prev_elem = elem 
        # finished row #i, append it to "compressedList" 
        compressedList.append(thisRow) 
 
    # a bit more compressing 
    for row in compressedList: 
        # eliminate leading zeros from each row 
        while len(row) > 0 and row[0] == 0: 
            del row[0] 
 
        # eliminate trailing zeros from each row 
        while len(row) > 0: 
            index = len(row)-1 
            if row[index] == 0: 
                del row[index] 
            else: 
                break 
 
    # eliminate leading empty rows 
    while len(compressedList[0]) == 0: 
        del compressedList[0] 
 
    # eliminate trailing empty rows 
    index = len(compressedList)-1 
    while len(compressedList[index]) == 0: 
        del compressedList[index] 
        index = len(compressedList)-1 
 
    if debug: 
        # save the compressed format 
        fname = str(fontName) + '_c.txt' 
        fd = open(fname, mode='w') 
        for row in compressedList: 
            fd.write(str(row) + '\n') 
        fd.close() 
 
    # this is where the decision is actually made 
    for row in compressedList: 
        if len(row) > 3: # characteristic of a 'W', a simple box will have maximum rowLen of 3 
            return True 
    return False