gpt4 book ai didi

fonts - 如何确定字体是否支持字符

转载 作者:行者123 更新时间:2023-12-04 12:57:39 26 4
gpt4 key购买 nike

我正在使用pygame,我想显示字符⊕,但是当我运行代码时,它显示的是一个矩形,我尝试了许多其他字体,它们都是矩形或带问号的矩形。其他特殊字符如 ± 有效,所以我不确定问题是什么。我搜索了一下,网站上还有另一个与此类似的问题,它说 python 3.x 对特殊字符没有问题,只要它们受字体支持。然后我检查了支持⊕的字体,大多数字体都支持。
tl;博士
你如何在pygame上显示⊕?

最佳答案

好吧,这个问题变得很困难。当 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 是什么。
screenshot
此引用程序检查字形是否以给定的字体呈现,如果为 True,则绘制它,否则绘制“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()
请注意,“happy poo”表情符号 [💩] 似乎不适用于任何一种字体,并导致 font.render() 中的异常,它被捕获并被视为失败。

关于fonts - 如何确定字体是否支持字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64395597/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com