pygame.font
pygame module for loading and rendering fonts
— initialize the font module
— uninitialize the font module
— true if the font module is initialized
— get the filename of the default font
— gets SDL_ttf version
— get all available fonts
— find a specific font on the system
— create a Font object from the system fonts
— create a new Font object from a file

字体模块允许将TrueType字体呈现为Surface对象。该模块构建在sdl_ttf库之上,该库随所有普通的pyGame安装一起提供。

使用字体完成的大部分工作都是通过使用实际的Font对象来完成的。该模块本身仅具有支持创建Font对象的例程 pygame.font.Font()create a new Font object from a file 。

从系统加载字体,方法是使用 pygame.font.SysFont()create a Font object from the system fonts 功能。还有一些其他功能可以帮助查找系统字体。

PYGAME自带一个内置的默认字体,Freesans黑体。这始终可以通过以下方式访问 None 作为字体名称。

在PYGAME 2.0.3之前的版本中,PYGAMe.FONT接受任何的USC-2/UTF-16字符(‘\u0001’到‘\uffff’)。在2.0.3之后,使用SDL_TTF 2.0.15构建的pygame.font可以接受任何有效的UCS-4/UTF-32字符(就像表情符号一样,如果字体有它们的话)(‘\U00000001’到‘\U0010FFFF’))。有关这方面的更多信息,请参阅 Font.render() 。

在pyGame 2.0.3之前,可以通过使用 pygame.freetypeEnhanced pygame module for loading and rendering computer fonts 基于 pygame.ftfont 来模拟Font模块。这可以通过在第一次导入之前定义环境变量PYGAME_FREETYPE来使用 pygamethe top level pygame package 。既然这个问题 pygame.ftfont 解决方案不再存在,它可能会在未来被删除。

pygame.font.init()
initialize the font module
init() -> None

此方法由自动调用 pygame.init() 。它初始化字体模块。在执行任何其他功能之前,必须先初始化该模块。

多次调用此函数是安全的。

pygame.font.quit()
uninitialize the font module
quit() -> None

手动取消初始化SDL_TTF的字体系统。这将由自动调用 pygame.quit() 。

即使字体当前未初始化,调用此函数也是安全的。

pygame.font.get_init()
true if the font module is initialized
get_init() -> bool

测试字体模块是否初始化。

pygame.font.get_default_font()
get the filename of the default font
get_default_font() -> string

返回系统字体的文件名。这不是文件的完整路径。该文件通常与字体模块位于同一目录中,但也可以捆绑在单独的归档中。

pygame.font.get_sdl_ttf_version()
gets SDL_ttf version
get_sdl_ttf_version(linked=True) -> (major, minor, patch)

返回标识sdl_ttf版本的整数数组。Sdl_ttf是用C编写的底层字体呈现库,pyGame的字体模块依赖于它。如果‘Linked’为True(默认),则该函数返回链接的TTF库的版本。否则,此函数将返回编译时使用的TTF pyGame版本

New in pygame 2.1.3.

pygame.font.get_fonts()
get all available fonts
get_fonts() -> list of strings

返回系统上所有可用字体的列表。字体名称将设置为小写,删除所有空格和标点符号。这在大多数系统上都有效,但如果找不到字体,有些系统会返回一个空列表。

pygame.font.match_font()
find a specific font on the system
match_font(name, bold=False, italic=False) -> path

返回系统上字体文件的完整路径。如果粗体或斜体设置为True,则会尝试查找正确的字体系列。

字体名称还可以是可迭代的字体名称、逗号分隔的字体名称字符串或逗号分隔的字体名称字节,在这种情况下,将按顺序搜索名称集。如果没有找到任何给定的名称,则不返回任何名称。

New in pygame 2.0.1: 接受可迭代的字体名称。

示例:

print pygame.font.match_font('bitstreamverasans')
# output is: /usr/share/fonts/truetype/ttf-bitstream-vera/Vera.ttf
# (but only if you have Vera on your system)
pygame.font.SysFont()
create a Font object from the system fonts
SysFont(name, size, bold=False, italic=False) -> Font

返回从系统字体加载的新Font对象。字体将与请求的粗体和斜体标志匹配。PYGAME使用一小部分常见的字体别名。如果您要求的特定字体不可用,则可以使用合理的替代字体。如果没有找到合适的系统字体,这将退回到加载默认的pyGame字体。

字体名称还可以是可迭代的字体名称、逗号分隔的字体名称字符串或逗号分隔的字体名称字节,在这种情况下,将按顺序搜索名称集。

New in pygame 2.0.1: 接受可迭代的字体名称。

pygame.font.Font
create a new Font object from a file
Font(filename, size) -> Font
Font(pathlib.Path, size) -> Font
Font(object, size) -> Font
— Gets or sets whether the font should be rendered in (faked) bold.
— Gets or sets whether the font should be rendered in (faked) italics.
— Gets or sets whether the font should be rendered with an underline.
— draw text on a new Surface
— determine the amount of space needed to render text
— control if text is rendered with an underline
— check if text will be rendered with an underline
— enable fake rendering of bold text
— check if text will be rendered bold
— enable fake rendering of italic text
— gets the metrics for each character in the passed string
— check if the text will be rendered italic
— get the line space of the font text
— get the height of the font
— get the ascent of the font
— get the descent of the font

从给定的文件名或Python文件对象加载新字体。大小是以像素为单位的字体高度。如果文件名为 None 将加载pyGame默认字体。如果无法从给定的参数加载字体,则会引发异常。一旦创建了字体,大小就不能更改。

字体对象主要用于将文本呈现为新的Surface对象。呈现器可以模拟粗体或斜体特征,但最好从具有实际斜体或粗体字形的字体加载。

bold
Gets or sets whether the font should be rendered in (faked) bold.
bold -> bool

字体是否应以粗体显示。

设置为True时,将启用文本的粗体呈现。这是一种虚假的字体延伸,在许多字体上看起来都不太好。如果可能,从一个真正的粗体字体文件中加载字体。虽然是粗体,但字体的宽度将不同于正常情况。这可以与斜体和下划线模式混合使用。

New in pygame 2.0.0.

italic
Gets or sets whether the font should be rendered in (faked) italics.
italic -> bool

字体是否应以斜体呈现。

设置为True时,将启用斜体文本的假呈现。这是对字体的虚假倾斜,在许多字体类型上看起来都不太好。如果可能的话,从一个真正的斜体字体文件中加载字体。使用斜体时,字体的宽度将与正常情况下不同。这可以与粗体和下划线模式混合使用。

New in pygame 2.0.0.

underline
Gets or sets whether the font should be rendered with an underline.
underline -> bool

字体是否应显示为下划线。

设置为True时,所有呈现的字体都将包括下划线。无论字体大小,下划线始终为一像素厚。这可以与粗体和斜体模式混合使用。

New in pygame 2.0.0.

render()
draw text on a new Surface
render(text, antialias, color, background=None) -> Surface

这将创建一个在其上呈现指定文本的新曲面。 pygame.fontpygame module for loading and rendering fonts 不提供在现有曲面上直接绘制文本的方法:相反,您必须使用 Font.render() 要创建文本的图像(曲面),然后将该图像blit到另一个曲面上。

文本只能为一行:不呈现换行符。空字符(‘x00’)引发TypeError。同时接受Unicode和char(字节)字符串。对于Unicode字符串,以前仅支持UCS-2字符(‘\u0001’到‘\uffff’),任何更大的Unicode码点都将引发UnicodeError。现在,支持UCS-4范围内的字符。对于字符字符串a LATIN1 假定为编码。抗锯齿参数是一个布尔值:如果为True,则字符将具有平滑边缘。颜色参数是文本的颜色 [例如:(0,0,255)表示蓝色] 。可选的背景参数是用于文本背景的颜色。如果没有传递背景,文本外部的区域将是透明的。

返回的Surface将具有容纳文本所需的尺寸。(与由 Font.size() )。如果为文本传递空字符串,将返回一个零像素宽度和字体高度的空白表面。

根据使用的背景和抗锯齿的类型,这将返回不同类型的曲面。出于性能原因,最好知道将使用哪种类型的映像。如果未使用抗锯齿,则返回的图像将始终是具有双色调色板的8位图像。如果背景是透明的,则会设置一个Colorkey。抗锯齿图像渲染为24位 RGB 图像。如果背景是透明的,则会包含像素Alpha。

优化:如果您知道文本(在屏幕上)的最终目标将始终具有纯色背景,并且文本是抗锯齿的,则可以通过指定背景色来提高性能。这将导致生成的图像通过Colorkey而不是Alpha值(效率低得多)来维护透明度信息。

如果呈现‘\n’,则将呈现未知字符。通常是一个长方形。相反,您需要自己处理换行符。

字体呈现不是线程安全的:任何时候都只有一个线程可以呈现文本。

Changed in pygame 2.0.3: 呈现UCS4 Unicode是有效的,不会引发异常。使用 if hasattr(pygame.font, "UCS4"): 看看PYGAME是否支持渲染UCS4 Unicode,包括更多语言和表情符号。

size()
determine the amount of space needed to render text
size(text) -> (width, height)

返回呈现文本所需的尺寸。这可用于在呈现文本之前帮助确定文本所需的位置。它还可以用于文字换行和其他布局效果。

请注意,大多数字体使用字距调整来调整特定字母对的宽度。例如,“ae”的宽度并不总是与“a”+“e”的宽度匹配。

set_underline()
control if text is rendered with an underline
set_underline(bool) -> None

启用后,所有呈现的字体都将包含下划线。无论字体大小,下划线始终为一像素厚。这可以与粗体和斜体模式混合使用。

备注

这与 underline 属性。

get_underline()
check if text will be rendered with an underline
get_underline() -> bool

启用字体下划线时返回True。

备注

这与 underline 属性。

set_bold()
enable fake rendering of bold text
set_bold(bool) -> None

启用文本的粗体呈现。这是一种虚假的字体延伸,在许多字体上看起来都不太好。如果可能,从一个真正的粗体字体文件中加载字体。虽然是粗体,但字体的宽度将不同于正常情况。这可以与斜体和下划线模式混合使用。

备注

这与 bold 属性。

get_bold()
check if text will be rendered bold
get_bold() -> bool

启用字体粗体呈现模式时返回True。

备注

这与 bold 属性。

set_italic()
enable fake rendering of italic text
set_italic(bool) -> None

启用斜体文本的假呈现。这是对字体的虚假倾斜,在许多字体类型上看起来都不太好。如果可能的话,从一个真正的斜体字体文件中加载字体。使用斜体时,字体的宽度将与正常情况下不同。这可以与粗体和下划线模式混合使用。

备注

这与 italic 属性。

metrics()
gets the metrics for each character in the passed string
metrics(text) -> list

该列表包含每个字符的元组,其中包含最小 X 偏移量,最大 X 偏移量,最小 Y 偏移量,最大 Y 字符的偏移量和前进偏移量(方位角加宽度)。 [(Minx,Maxx,Miny,Maxy,Advance),(Minx,Maxx,Miny,Maxy,Advance),] 。列表中不会为每个无法识别的字符输入任何内容。

get_italic()
check if the text will be rendered italic
get_italic() -> bool

启用字体斜体呈现模式时返回True。

备注

这与 italic 属性。

get_linesize()
get the line space of the font text
get_linesize() -> int

返回具有字体的一行文本的高度(以像素为单位)。呈现多行文本时,这是建议的行间空格量。

get_height()
get the height of the font
get_height() -> int

返回以像素为单位的实际呈现文本的高度。这是字体中每个字形的平均大小。

get_ascent()
get the ascent of the font
get_ascent() -> int

返回字体上升的高度,以像素为单位。上升是指从字体基线到字体顶部的像素数。

get_descent()
get the descent of the font
get_descent() -> int

返回字体下降的高度,以像素为单位。下降是从字体基线到字体底部的像素数。




Edit on GitHub