首页 > 解决方案 > 如何确定字体是否支持字符

问题描述

我正在使用pygame,我想显示字符⊕,但是当我运行代码时,它显示了一个矩形,我尝试了许多其他字体,它们都是带问号的矩形或矩形。其他特殊字符(如 ±)起作用了,所以我不确定问题出在哪里。我搜索了它,网站上有另一个类似的问题,它说 python 3.x 对特殊字符没有问题,只要它们受字体支持。然后我检查了支持⊕的字体,大多数字体都支持。

tl;博士

你如何在pygame上显示⊕?

标签: fontspygamespecial-characters

解决方案


好吧,这个问题原来是困难的。当 Linux 从字体渲染未实现的字形时(至少在 PyGame 中),您会得到一个空的“边框矩形”。这个矩形位于字体的基线处,两边都有空格,但顶部没有空格。 可能在其他操作环境中也类似。(最初我假设边框围绕字形图像没有空间,并且很容易检测到。)

所以现在我们可以通过检查字形的内部四分之一来检查字形渲染到什么东西,看看它是否包含任何非空像素。

显然,这是一种启发式方法,并且可能会在,主要是后代的角色上失败。但是我在一堆符号和希腊字母上对其进行了测试,它似乎工作正常。它确实适用于 OP 的测试字形。

该算法不是集中检查每个像素,而是首先尝试检查中心水平像素,然后检查中心垂直像素。如果这些都没有产生结果,检查每个内部像素。如果找到一个像素,我们将停止检查并返回。

def glyphInFont( glyph, font ):
    """ Given a glyph and a font, use a pixel-finding heuristic to determine
        if the glyph renders to something other than an "empty border" non-existant
        font symbol.  Returns True if it renders to something. """

    result = False
    WHITE  = ( 255, 255, 255 )   # can be any colour pair with constrast
    BLACK  = (   0,   0,   0 )

    try:
        text_image = font.render( glyph, True, WHITE, BLACK )
        text_rect  = text_image.get_rect()
        x_centre = text_rect.width // 2
        y_centre = text_rect.height // 2

        # On Linux at least, non-renderable glyphs have a border.
        # work out a 50% search box, centred inside the gluph
        box_top    = y_centre - ( text_rect.height // 4 )
        box_bottom = y_centre + ( text_rect.height // 4 )
        box_left   = x_centre - ( text_rect.width // 4 )
        box_right  = x_centre + ( text_rect.width // 4 )

        # Trace a Horizontal line through the middle of the bitmap 
        # looking for non-black pixels
        for x in range( box_left, box_right ):
            if ( text_image.get_at( ( x, y_centre ) ) != BLACK ):
                result = True
                break

        # If not found already, trace a line vertically
        if ( result == False ):
            for y in range( box_top, box_bottom ):
                if ( text_image.get_at( ( x_centre, y ) ) != BLACK ):
                    result = True
                    break

        # If still not found, check every pixel in the centre-box
        if ( result == False ):
            for y in range( box_top, box_bottom ):
                for x in range( box_left, box_right ):
                    if ( text_image.get_at( ( x, y ) ) != BLACK ):
                        result = True
                        break

    except UnicodeError as uce:
        # Glyph-ID not supported
        pass  # False goes through

    return result

使用“ Badboom ”字体进行测试,无论 Linux 默认的 SysFont 是什么。

截屏

这个参考程序检查字形是否以给定的字体呈现,如果为真则绘制它,否则绘制一个“x”。

import pygame

# Window size
WINDOW_WIDTH    = 400
WINDOW_HEIGHT   = 400
WINDOW_SURFACE  = pygame.HWSURFACE|pygame.DOUBLEBUF|pygame.RESIZABLE

DARK_BLUE = (   3,   5,  54 )
YELLOW    = ( 255, 255,   0 )
RED       = ( 255,   0,   0 )

symbols = [ '⊕', 'Δ', 'Ψ', '' ]


def glyphInFont( glyph, font ):
    """ Given a glyph and a font, use a pixel-finding heuristic to determine
        if the glyph renders to something other than an "empty border" non-existant
        font symbol.  Returns True if it renders to something. """

    result = False
    WHITE  = ( 255, 255, 255 )   # can be any colour pair with constrast
    BLACK  = (   0,   0,   0 )

    try:
        text_image = font.render( glyph, True, WHITE, BLACK )
        text_rect  = text_image.get_rect()
        x_centre = text_rect.width // 2
        y_centre = text_rect.height // 2

        # On Linux at least, non-renderable glyphs have a border.
        # work out a 50% search box, centred inside the gluph
        box_top    = y_centre - ( text_rect.height // 4 )
        box_bottom = y_centre + ( text_rect.height // 4 )
        box_left   = x_centre - ( text_rect.width // 4 )
        box_right  = x_centre + ( text_rect.width // 4 )

        # Trace a Horizontal line through the middle of the bitmap 
        # looking for non-black pixels
        for x in range( box_left, box_right ):
            if ( text_image.get_at( ( x, y_centre ) ) != BLACK ):
                result = True
                break

        # If not found already, trace a line vertically
        if ( result == False ):
            for y in range( box_top, box_bottom ):
                if ( text_image.get_at( ( x_centre, y ) ) != BLACK ):
                    result = True
                    break

        # If still not found, check every pixel in the centre-box
        if ( result == False ):
            for y in range( box_top, box_bottom ):
                for x in range( box_left, box_right ):
                    if ( text_image.get_at( ( x, y ) ) != BLACK ):
                        result = True
                        break

    except UnicodeError as uce:
        # Glyph-ID not supported
        pass  # False goes through

    return result


### initialisation
pygame.init()
pygame.mixer.init()
window = pygame.display.set_mode( ( WINDOW_WIDTH, WINDOW_HEIGHT ), WINDOW_SURFACE )
pygame.display.set_caption("Glyph Check")


### Make some fonts
font1 = pygame.font.Font( 'badaboom.ttf', 64 )   # ref: https://www.1001freefonts.com/badaboom.font
font2 = pygame.font.SysFont( None, 64 )

### Main Loop
clock = pygame.time.Clock()
done = False
while not done:

    # Handle user-input
    for event in pygame.event.get():
        if ( event.type == pygame.QUIT ):
            done = True

    # Update the window, but not more than 60fps
    window.fill( DARK_BLUE )

    # Simply to layout nicely on the screen
    cursor_x = 50
    cursor_y = (WINDOW_HEIGHT // 2 ) - 100

    # Loop through the symbol list, rendering the symbol if it exists
    # or an "x" otherwise
    #
    # NOTE: This is inefficiant demo code! 
    #       It's bad to keep rendering fonts every frame
    for glyph in symbols:
        cursor_x += 50
        
        if ( glyphInFont( glyph, font1 ) ):
            window.blit( font1.render( glyph, True, YELLOW ), ( cursor_x, cursor_y ) )
        else:
            # does not exist
            window.blit( font1.render( 'x', True, YELLOW ), ( cursor_x, cursor_y ) )

        if ( glyphInFont( glyph, font2 ) ):
            window.blit( font2.render( glyph, True, YELLOW ), ( cursor_x, cursor_y+100 ) )
        else:
            # does not exist
            window.blit( font2.render( 'x', True, YELLOW ), ( cursor_x, cursor_y+100 ) )

    pygame.display.flip()

    # Clamp FPS
    clock.tick( 3 ) # slow update

pygame.quit()

请注意,“快乐便便”表情符号字形 [] 似乎在这两种字体中都不起作用,并导致 中的异常font.render(),该异常被捕获并视为失败。


推荐阅读