pygame.freetype
Enhanced pygame module for loading and rendering computer fonts
— Return the latest FreeType error
— Return the FreeType version
— Initialize the underlying FreeType library.
— Shut down the underlying FreeType library.
— Returns True if the FreeType module is currently initialized.
— DEPRECATED: Use get_init() instead.
— Return the glyph case size
— Return the default pixel size in dots per inch
— Set the default pixel size in dots per inch for the module
— create a Font object from the system fonts
— Get the filename of the default font
— Create a new Font instance from a supported font file.

这个 pygame.freetype 模块是以下组件的替代品 pygame.fontpygame module for loading and rendering fonts 。它具有原始版本的所有功能,外加许多新功能。但是,IS完全不依赖于SDL_TTF库。它直接在FreeType2库上实现。这个 pygame.freetype 模块本身不向后兼容 pygame.fontpygame module for loading and rendering fonts 。相反,请使用 pygame.ftfont 模块作为临时替换的 pygame.fontpygame module for loading and rendering fonts 。

FreeType支持的所有字体文件格式都可以通过 pygame.freetype ,即 TTF 、类型1、 CFF 、OpenType、 SFNT , PCF , FNT , BDF , PFR 和Type42字体。具有UTF-32代码点的所有字形均可访问(请参见 Font.ucs4 )。

大多数有关字体的工作都是使用 Font 实例。该模块本身只有用于初始化和创建 Font 物体。方法从系统加载字体。 SysFont() 功能。

还提供了对位图字体的额外支持。可以列出可用的位图大小(请参见 Font.get_sizes() )。仅用于位图字体 Font 可以为您设置大小(请参阅 Font.size 财产)。

目前,未定义的字符代码被替换为 .notdef (未定义的)字符。在将来的版本中,如何处理未定义的代码可能会变得可配置。

PYGAME带有内置的默认字体。通过将None作为字体名传递给 Font 构造函数。

额外的渲染功能可用于 pygame.freetype.FontCreate a new Font instance from a supported font file. 直接用于表面渲染(请参见 Font.render_to() )、字符字距调整(请参见 Font.kerning )、垂直布局(请参见 Font.vertical )、呈现文本的旋转(请参见 Font.rotation )和强烈的风格(请参见 Font.strong )。某些属性是可配置的,例如强样式强度(请参见 Font.strength )和下划线位置(请参见 Font.underline_adjustment )。文本可以由文本框的右上角或文本基线定位(请参见 Font.origin )。最后,可以分别调整字体的垂直和水平大小(请参见 Font.size )。这个 pygame.examples.freetype_misc 示例显示了正在使用的这些功能。

PyGame包不能导入 freetype 加载时会自动启动。必须显式导入此模块才能使用。**

import pygame
import pygame.freetype

New in pygame 1.9.2: freetype

pygame.freetype.get_error()
Return the latest FreeType error
get_error() -> str
get_error() -> None

返回在FreeType2库中发生的最后一个错误的描述,或者 None 如果没有发生任何错误。

pygame.freetype.get_version()
Return the FreeType version
get_version() -> (int, int, int)

返回此模块使用的FreeType库的版本。

请注意, freetype 模块依赖于FreeType2库。它不能与原始的FreeType1.0一起编译。因此,元组的第一个元素始终是“2”。

pygame.freetype.init()
Initialize the underlying FreeType library.
init(cache_size=64, resolution=72) -> None

此函数初始化基础FreeType库,并且必须在尝试使用 freetype 模块。

然而, pygame.init()initialize all imported pygame modules 将自动调用此函数,如果 freetype 模块已导入。多次调用此函数是安全的。

或者,您可以指定一个缺省值 cache_size 对于字形缓存:模块在任何给定时间将缓存的字形的最大数量。极小的值将自动调整以提高性能。也是默认像素 决议 ,以每英寸点数为单位,可以给出调整字体比例的方法。

pygame.freetype.quit()
Shut down the underlying FreeType library.
quit() -> None

此函数用于关闭 freetype 模块。调用此函数后,不应调用与 freetype 模块,因为它们很可能失败或可能产生不可预测的结果。即使模块尚未初始化,调用此函数也是安全的。

pygame.freetype.get_init()
Returns True if the FreeType module is currently initialized.
get_init() -> bool

退货 True 如果 pygame.freetype 模块当前已初始化。

New in pygame 1.9.5.

pygame.freetype.was_init()
DEPRECATED: Use get_init() instead.
was_init() -> bool

已弃用:返回 True 如果 pygame.freetype 模块当前已初始化。使用 get_init() 取而代之的是。

pygame.freetype.get_cache_size()
Return the glyph case size
get_cache_size() -> long

看见 pygame.freetype.init()Initialize the underlying FreeType library. 。

pygame.freetype.get_default_resolution()
Return the default pixel size in dots per inch
get_default_resolution() -> long

返回模块的默认像素大小,以每英寸点数为单位。默认值为72 DPI。

pygame.freetype.set_default_resolution()
Set the default pixel size in dots per inch for the module
set_default_resolution([resolution])

设置模块的默认像素大小,以每英寸点数为单位。如果省略可选参数或设置为零,则分辨率将重置为72 dpi。

pygame.freetype.SysFont()
create a Font object from the system fonts
SysFont(name, size, bold=False, italic=False) -> Font

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

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

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

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

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

pygame.freetype.Font
Create a new Font instance from a supported font file.
Font(file, size=0, font_index=0, resolution=0, ucs4=False) -> Font
Font(pathlib.Path) -> Font
— Proper font name.
— Font file path
— The default point size used in rendering
— Return the size and offset of rendered text
— Return the glyph metrics for the given text
— The unscaled height of the font in font units
— The unscaled ascent of the font in font units
— The unscaled descent of the font in font units
— The scaled ascent of the font in pixels
— The scaled descent of the font in pixels
— The scaled height of the font in pixels
— The scaled bounding box height of the font in pixels
— return the available sizes of embedded bitmaps
— Return rendered text as a surface
— Render text onto an existing surface
— Return rendered text as a string of bytes
— Render text into an array of ints
— The font's style flags
— The state of the font's underline style flag
— The state of the font's strong style flag
— The state of the font's oblique style flag
— The state of the font's wide style flag
— The strength associated with the strong or wide font styles
— Adjustment factor for the underline position
— Gets whether the font is fixed-width
— the number of available bitmap sizes for the font
— Gets whether the font is scalable
— allow the use of embedded bitmaps in an outline font file
— Font anti-aliasing mode
— Character kerning mode
— Font vertical mode
— text rotation in degrees counterclockwise
— default foreground color
— default background color
— Font render to text origin mode
— padded boundary mode
— Enable UCS-4 mode
— Pixel resolution in dots per inch

论据 file 可以是表示字体文件名的字符串、包含该字体的类似文件的对象,也可以为NONE;如果没有,则使用默认的PYGAME字体。

可选的,一个 size 参数来设置默认大小(以磅为单位),以确定呈现字符的大小。大小也可以显式传递给每个方法调用。由于缓存系统的工作方式,在构造函数上指定默认大小并不意味着与在每次函数调用时手动传递大小相比,性能有所提高。如果字体为位图,并且没有 size 则将默认大小设置为该字体的第一个可用大小。

如果字体文件具有多个字体,则可以使用 索引 争论。对于超出范围的字体索引值引发异常。

可选的 决议 参数设置用于缩放此Font实例的字形的像素大小(以每英寸点数为单位)。如果为0,则由以下设置的默认模块值 init() ,被使用。Font对象的分辨率只能通过重新初始化Font实例来更改。

可选的 ucs4 参数是一个整数,设置默认的文本转换模式:0(False)识别UTF-16代理项对、任何其他值(True),以将Unicode文本视为没有代理项对的UCS-4。看见 Font.ucs4 。

name
Proper font name.
name -> string

只读。返回字体文件中记录的字体的真实(长)名称。

path
Font file path
path -> unicode

只读。返回加载的字体文件的路径

size
The default point size used in rendering
size -> float
size -> (float, float)

获取或设置文本度量值和呈现的默认大小。它可以是单磅大小,以Python的形式给出 int 或 float ,或字体ppem(宽度、高度) tuple 。大小值为非负值。零大小或零宽度表示未定义的大小。在这种情况下,必须将大小作为方法参数提供,否则会引发异常。宽度为零但高度非零是ValueError。

对于可伸缩字体,单个数字值等同于宽度等于高度的元组。字体可以在高度设置大于宽度时垂直拉伸,也可以在宽度设置大于高度时水平拉伸。对于嵌入的位图,如 get_sizes() 中,使用公称宽度和公称高度选择可用的尺寸。

不可缩放的位图字体的字体大小不同。在方法调用期间,它必须与方法返回的可用大小之一匹配 get_sizes() 。如果不是,则会引发异常。如果大小是单个数字,则首先将该大小与磅大小值进行匹配。如果不匹配,则选择具有相同公称宽度和高度的可用尺寸。

get_rect()
Return the size and offset of rendered text
get_rect(text, style=STYLE_DEFAULT, rotation=0, size=0) -> rect

的最终尺寸和原点(以像素为单位)。 text 使用可选的 size 在积分方面, 风格 ,以及 旋转 。对于其他相关呈现属性以及未给出的任何可选参数, Font 实例被使用。

返回一个 Rect 实例,包含文本边框的宽度和高度以及文本原点的位置。原点在对齐单独呈现的文本片段时非常有用。它给出了文本开头的基线位置和方向角。请参阅 render_to() 方法以获取一个示例。

如果 text 是一个字符(字节)字符串,则其编码被假定为 LATIN1 。

可选地, text 可以是 None ,它将返回传递给上一个 get_rect() , render() , render_to() , render_raw() ,或 render_raw_to() 打电话。看见 render_to() 了解更多详细信息。

get_metrics()
Return the glyph metrics for the given text
get_metrics(text, size=0) -> [(...), ...]

中每个字符的字形度量值 text 。

字形指标以元组列表的形式返回。每个元组给出了单个字符字形的度量。字形指标包括:

(min_x, max_x, min_y, max_y, horizontal_advance_x, horizontal_advance_y)

边界框min_x、max_x、min_y和max_y值作为int类型的栅格适配像素坐标返回。超前值是浮点值。

计算是使用字体的默认大小(以磅为单位)完成的。属性指定另一个磅大小。 size 论点。

针对当前的旋转、强烈和倾斜设置调整度量值。

如果文本是字符(字节)字符串,则假定其编码为 LATIN1 。

height
The unscaled height of the font in font units
height -> int

只读。获取字体的高度。这是字体中所有字形的平均值。

ascender
The unscaled ascent of the font in font units
ascender -> int

只读。返回从字体基线到边界框顶部的单位数。

descender
The unscaled descent of the font in font units
descender -> int

只读。返回字体下降的高度,以字体单位表示。下降值是从字体基线到边界框底部的单位数。

get_sized_ascender()
The scaled ascent of the font in pixels
get_sized_ascender(<size>=0) -> int

返回从字体基线到边界框顶部的单位数。它不会针对强势或旋转进行调整。

get_sized_descender()
The scaled descent of the font in pixels
get_sized_descender(<size>=0) -> int

返回从字体基线到边界框顶部的像素数。它不会针对强势或旋转进行调整。

get_sized_height()
The scaled height of the font in pixels
get_sized_height(<size>=0) -> int

返回字体的高度。这是字体中所有字形的平均值。它不会针对强势或旋转进行调整。

get_sized_glyph_height()
The scaled bounding box height of the font in pixels
get_sized_glyph_height(<size>=0) -> int

返回字体的字形边框高度,以像素为单位。这是字体中所有字形的平均值。它不会针对强势或旋转进行调整。

get_sizes()
return the available sizes of embedded bitmaps
get_sizes() -> [(int, int, int, float, float), ...]
get_sizes() -> []

返回元组记录列表,每个记录对应一个支持的磅大小。每个元组包含点大小、以像素为单位的高度、以像素为单位的宽度、以分数像素为单位的水平ppem(标称宽度)和以分数像素为单位的垂直ppem(标称高度)。

render()
Return rendered text as a surface
render(text, fgcolor=None, bgcolor=None, style=STYLE_DEFAULT, rotation=0, size=0) -> (Surface, Rect)

返回一个新的 Surface ,文本以‘fgcolor’给出的颜色呈现给它。如果未给出前景色,则默认前景色, fgcolor 是使用的。如果 bgcolor 如果给定,则表面将填充此颜色。如果未指定背景色,则曲面背景为透明,零Alpha。正常情况下,返回的表面具有32位像素大小。但是,如果 bgcolor 是 None 并且反走样被禁用,返回单色8位Colorkey表面,其中Colorkey被设置为背景颜色。

返回值是一个元组:新的表面和给出呈现文本的大小和原点的边界矩形。

如果为文本传递空字符串,则返回的RECT为字体的宽度和高度为零。

可选 烟雾色 , 风格 , 旋转 ,以及 size 参数重写为 Font 实例。

如果 text 是字符(字节)字符串,则其编码被假定为 LATIN1 。

可选地, text 可以是 None ,这将呈现传递给上一个 get_rect() , render() , render_to() , render_raw() ,或 render_raw_to() 打电话。看见 render_to() 有关详细信息,请参阅。

render_to()
Render text onto an existing surface
render_to(surf, dest, text, fgcolor=None, bgcolor=None, style=STYLE_DEFAULT, rotation=0, size=0) -> Rect

Renders the string text to the pygame.Surfacepygame object for representing images surf, at position dest, a (x, y) surface coordinate pair. If either x or y is not an integer it is converted to one if possible. Any sequence where the first two items are x and y positional elements is accepted, including a Rect instance. As with render(), optional fgcolor, style, rotation, and size argument are available.

如果背景色 BG颜色 则首先用该颜色填充文本边框。接下来,文本将被删除。背景填充和文本呈现都涉及完整的Alpha BLIT。也就是说,前景、背景和目标目标表面的Alpha值都会影响斑点。

返回值是一个矩形,它提供了呈现文本在图面中的大小和位置。

如果为文本传递空字符串,则返回 Rect 字体的宽度和高度为零。RECT将测试为FALSE。

可选地, text 可以设置 None ,它将重新呈现传递给上一个 render_to() , get_rect() , render() , render_raw() ,或 render_raw_to() 打电话。基本上,此功能是对使用 render_to() 结合使用 get_rect() 。示例:

def word_wrap(surf, text, font, color=(0, 0, 0)):
    font.origin = True
    words = text.split(' ')
    width, height = surf.get_size()
    line_spacing = font.get_sized_height() + 2
    x, y = 0, line_spacing
    space = font.get_rect(' ')
    for word in words:
        bounds = font.get_rect(word)
        if x + bounds.width + bounds.x >= width:
            x, y = 0, y + line_spacing
        if x + bounds.width + bounds.x >= width:
            raise ValueError("word too wide for the surface")
        if y + bounds.height - bounds.y >= height:
            raise ValueError("text to long for the surface")
        font.render_to(surf, (x, y), None, color)
        x += bounds.width + space.width
    return x, y

什么时候 render_to() 使用相同的字体属性 调用- size , style , strength , wide , antialiased , vertical , rotation , kerning ,以及 use_bitmap_strikes - AS get_rect() , render_to() 将使用由以下计算得出的布局 get_rect() 。否则, render_to() 属性之后,如果使用文本字符串调用或上述属性之一已更改,将重新计算布局 get_rect() 打电话。

如果 text 是字符(字节)字符串,则其编码被假定为 LATIN1 。

render_raw()
Return rendered text as a string of bytes
render_raw(text, style=STYLE_DEFAULT, rotation=0, size=0, invert=False) -> (bytes, (int, int))

喜欢 render() 而是将像素作为8位灰阶值的字节串返回。前景颜色为255,背景颜色为0,可用作前景图案的Alpha蒙版。

render_raw_to()
Render text into an array of ints
render_raw_to(array, text, dest=None, style=STYLE_DEFAULT, rotation=0, size=0, invert=False) -> Rect

呈现到公开数组结构接口的数组对象。该数组必须是包含整数项的二维数组。默认设置 dest 价值, None ,相当于位置(0,0)。看见 render_to() 。与其他渲染方法一样, text 可以是 None 呈现先前传递给另一个方法的文本字符串。

返回值为 pygame.Rect()pygame object for storing rectangular coordinates 给出所呈现文本的大小和位置。

style
The font's style flags
style -> int

获取或设置字体的默认样式。此默认样式将用于所有文本呈现和大小计算,除非特别覆盖呈现或 get_rect() 打电话。样式值可以是以下一个或多个常量的逐位OR:

STYLE_NORMAL
STYLE_UNDERLINE
STYLE_OBLIQUE
STYLE_STRONG
STYLE_WIDE
STYLE_DEFAULT

这些常量可以在自由类型常量模块中找到。或者,可以通过访问各个样式属性(下划线、斜线、强)来修改或获取默认样式。

这个 STYLE_OBLIQUE 和 STYLE_STRONG 样式仅适用于可缩放字体。尝试为位图字体设置这两种字体都会引发AttributeError。尝试为非活动字体设置其中一个,由返回 Font.__new__() ,引发运行错误。

分配 STYLE_DEFAULT 发送到 style 属性保持该属性不变,因为该属性定义了默认设置。这个 style 财产将永远不会归还 STYLE_DEFAULT 。

underline
The state of the font's underline style flag
underline -> bool

获取或设置绘制文本时是否给字体加下划线。此默认样式值将用于所有文本呈现和大小计算,除非在呈现或 get_rect() 通过‘style’参数调用。

strong
The state of the font's strong style flag
strong -> bool

获取或设置绘制文本时字体是否为粗体。此默认样式值将用于所有文本呈现和大小计算,除非在呈现或 get_rect() 通过‘style’参数调用。

oblique
The state of the font's oblique style flag
oblique -> bool

获取或设置字体是否将呈现为倾斜。此默认样式值将用于所有文本呈现和大小计算,除非在呈现或 get_rect() 呼叫,通过 风格 参数。

仅可缩放(轮廓)字体支持倾斜样式。尝试在位图字体上设置此样式将引发AttributeError。如果字体对象处于非活动状态,则由 Font.__new__() ,则设置此属性会引发运行错误。

wide
The state of the font's wide style flag
wide -> bool

获取或设置绘制文本时是否水平拉伸字体。它产生的结果类似于 pygame.font.Fontcreate a new Font object from a file 粗体。此样式不适用于旋转文本。

strength
The strength associated with the strong or wide font styles
strength -> float

对于强变换或宽变换,字体字形的大小被放大的量,作为未变换大小的一部分。对于宽型,只有水平尺寸增加。对于强文本,水平和垂直维度都会放大。0.08333(1/12)的宽式力量相当于 pygame.font.Fontcreate a new Font object from a file 大胆的风格。默认值为0.02778(1/36)。

仅可缩放(轮廓)字体支持强度样式。尝试在位图字体上设置此属性将引发AttributeError。如果字体对象处于非活动状态,则由 Font.__new__() ,则对此属性的赋值会引发运行错误。

underline_adjustment
Adjustment factor for the underline position
underline_adjustment -> float

获取或设置一个系数,该系数在为正时乘以字体的下划线偏移量以调整下划线位置。负值会将下划线转换为删除线或上划线。它与上升者相乘。接受的值范围在-2.0和2.0之间(包括-2.0和2.0)。值0.5与Tango下划线非常匹配。值为1.0将模拟 pygame.font.Fontcreate a new Font object from a file 下划线。

fixed_width
Gets whether the font is fixed-width
fixed_width -> bool

只读。退货 True 如果字体包含固定宽度的字符(例如Courier、Bitstream Vera Sans Mono、Andale Mono)。

fixed_sizes
the number of available bitmap sizes for the font
fixed_sizes -> int

只读。返回字体包含位图字符图像的磅数。如果为零,则字体不是位图字体。可伸缩字体可以包含作为Strike的预先呈现的磅值。

scalable
Gets whether the font is scalable
scalable -> bool

只读。退货 True 如果字体包含轮廓字形。如果是这样,则点大小不限于可用位图大小。

use_bitmap_strikes
allow the use of embedded bitmaps in an outline font file
use_bitmap_strikes -> bool

某些可缩放字体包括用于特定磅值的嵌入位图。此属性控制是否使用这些位图敲击。设置它 False 若要禁用加载任何位图罢工,请执行以下操作。设置它 True ,默认情况下,允许非旋转渲染的位图敲击,而不是 wide 或 underline 。对于位图字体,此属性被忽略。

另请参阅 fixed_sizes 和 get_sizes() 。

antialiased
Font anti-aliasing mode
antialiased -> bool

获取或设置字体的消除锯齿模式。该默认为 True 在所有字体上,使用完整的8位混合呈现。

设置为 False 进行单色渲染。这应该会提供较小的速度收益并减少高速缓存大小。

kerning
Character kerning mode
kerning -> bool

获取或设置字体的字距调整模式。该默认为 False 在所有字体上,它们将在不进行字距调整的情况下呈现。

设置为 True 定位字形时,如果字体支持,则在字符对之间添加字距调整。

vertical
Font vertical mode
vertical -> bool

获取或设置字符是否垂直布局,而不是水平布局。在呈现汉字或其他一些垂直文字时可能很有用。

设置为 True 若要切换到竖排文本布局,请执行以下操作。默认为 False ,水平放置。

请注意, Font 类不会自动确定脚本方向。必须显式选择垂直布局。

还要注意,一些字体格式(特别是基于位图的字体)不包含垂直绘制字形所需的度量,因此在这些情况下绘制将产生未指定的结果。

rotation
text rotation in degrees counterclockwise
rotation -> int

获取或设置呈现文本的基线角度。角度以整数度表示。默认角度为0,沿X轴呈现水平文本,沿Y轴呈现垂直文本。正值会将这些轴逆时针旋转很多度。负角度对应于顺时针旋转。旋转值被标准化为0到359(包括0和359)范围内的值(例如,390->390-360->30、-45->360+-45->315、720->720-(2*360)->0)。

只有可缩放(轮廓)字体才能旋转。尝试更改位图字体的旋转时会引发AttributeError。尝试更改非活动字体实例的旋转,由返回 Font.__new__() ,引发运行错误。

fgcolor
default foreground color
fgcolor -> Color

获取或设置默认字形呈现颜色。它最初是不透明的黑色 - (0,0,0,255)。适用于 render() 和 render_to() 。

bgcolor
default background color
bgcolor -> Color

获取或设置默认背景呈现颜色。最初它是未设置的,默认情况下文本将以透明背景呈现。适用于 render() 和 render_to() 。

New in pygame 2.0.0.

origin
Font render to text origin mode
origin -> bool

如果已设置 True , render_to() 和 render_raw_to() 将会拿到 dest 设置为文本原点的位置,而不是边框的左上角。看见 get_rect() 有关详细信息,请参阅。

pad
padded boundary mode
pad -> bool

如果已设置 True ,则文本边界矩形将被放大以匹配 font.Font 。否则,边界矩形正好足够文本大小。

ucs4
Enable UCS-4 mode
ucs4 -> bool

获取或设置Unicode文本的解码。默认情况下,freetype模块对Unicode文本执行UTF-16代理项对解码。这允许0x10000和0x10FFFF之间的32位转义序列(‘Uxxxxxxx’)表示其对应的UTF-32代码点,这些代码点位于使用UCS-2 Unicode类型构建的Python解释器上(例如,在Windows上)。这还意味着UTF-16代理项区域(0xD800到0xDFFF)内的字符值被视为代理项对的一部分。格式错误的代理项对将引发UnicodeEncodeError。设置ucs4 True 关闭代理项对解码,从而允许访问使用四字节Unicode字符支持构建的Python解释器的完整UCS-4字符范围。

resolution
Pixel resolution in dots per inch
resolution -> int

只读。获取缩放此对象的字体标志符号时使用的像素大小 Font 实例。




Edit on GitHub