fontモジュールを使用することで、新規にSurfaceを作成してその上にTrueTypeフォントの文字を描写することができます。このモジュール単体だけでは文字描写処理ができないので、SDL_ttfファイルを同じ階層に用意する必要があります。このpygame.fontモジュールを使用する前には、使用できる状態かを確認して初期化した方がよいでしょう。
文字を描写する処理の殆どは、pygame.font.Font命令で作成したFontオブジェクトを使用して実行することになります。このモジュール自体は自身を初期化する機能とpygame.font.Font命令でFontオブジェクトを使用する機能しか持っていません。
pygame.font.SysFont命令を使用してシステム上からfont情報を読み込むこともできます。他にもシステムフォントの検索に役立ついくつかの機能があります。
Pygameの内部には既定のフォントが付属しています。命令を実行する際にフォント名にNoneを引数として渡すことで、その既定フォントを読み込むことができます。
The font module allows for rendering TrueType fonts into a new Surface object. This module is optional and requires SDL_ttf as a dependency. You should test that pygame.font is available and initialized before attempting to use the module.
Most of the work done with fonts are done by using the actual Font objects. The module by itself only has routines to initialize the module and create Font objects with pygame.font.Font - create a new Font object from a file.
You can load fonts from the system by using the pygame.font.SysFont - create a Font object from the system fonts function. There are a few other functions to help lookup the system fonts.
Pygame comes with a builtin default font. This can always be accessed by passing None as the font name.
この命令はpygame.init命令を実行した時に自動的に呼び出されます。pygame.fontモジュールを初期化します。他の文字描写命令を実行する前には、モジュールの初期化を行わなくてはいけません。
この命令を複数回実行したとしても特に問題ありません。
This method is called automatically by pygame.init - initialize all imported pygame modules. It initializes the font module. The module must be initialized before any other functions will work.
It is safe to call this function more than once.
SDL_ttfのフォントシステムの初期化を手動で解除します。この命令はpygame.quit命令が実行された時に自動的に呼び出されます。
フォントが初期化されていない状態でこの命令を実行しても、特に問題はありません。
Manually uninitialize SDL_ttf's font system. This is called automatically by pygame.quit - uninitialize all pygame modules.
It is safe to call this function even if font is currently not initialized.
システムフォントのファイル名を戻り値として返します。取得されるのはファイルのある階層も含んだフルパスではなく、ファイルの名前のみです。 これらのファイルは大抵フォントモジュールとおなじ階層にありますが、別の階層に保存することもできます。
Return the filename of the system font. This is not the full path to the file. This file can usually be found in the same directory as the font module, but it can also be bundled in separate archives.
ユーザーの環境で使用できる全てのフォント名をlist型の戻り値として返します。フォント名はスペースや句読点が削除された小文字の状態で取得されます。この命令は殆どの環境で使用できますが、使用可能フォントが見つからない場合は空のリストが戻り値として返されることもあります。
Returns a list of all the fonts available on the system. The names of the fonts will be set to lowercase with all spaces and punctuation removed. This works on most systems, but some will return an empty list if they cannot find fonts.
ユーザーの使用環境にある指定したフォントファイルの、階層も含んだフルパスを戻り値として返します。bold引数やitalic引数にtrueを設定した場合は、それらの条件に最も近いフォントが取得されます。
複数のフォント名をコンマで区切ったlist型にして指定することで、それらのフォントをまとめて検索することができます。指定した名前のフォントが見つからなかった場合は、Noneが戻り値として取得されます。
使用例:
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)
Returns the full path to a font file on the system. If bold or italic are set to true, this will attempt to find the correct family of font.
The font name can actually be a comma separated list of font names to try. If none of the given names are found, None is returned.
Example:
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)
システムフォントから条件に合うフォントを読み込み、それを戻り値として取得します。bold(太字)やitalic(斜体)といったフラグを設定していればそれが反映されたフォントが作成されます。 条件に合うシステムフォントが見つからない場合は、代わりにデフォルトのpygameフォントが読み込まれます。複数のフォントをまとめて検索する場合は、探したいフォント名をコンマで区切ったlist型にして引数に指定します。
Return a new Font object that is loaded from the system fonts. The font will match the requested bold and italic flags. If a suitable system font is not found this will fallback on loading the default pygame font. The font name can be a comma separated list of font names to look for.
引数として渡されたファイル名やpythonのファイルオブジェクトから、フォントを新規に読み込みます。size引数で指定するのはピクセル単位でのフォントの高さです。filename引数にNone値が設定された場合は、Pygameの既定フォントを読み込みます。指定した名前のフォントが読み込めなかった場合は例外が発生します。一度フォントを作成すると、後から高さを変更することができません。
作成したFontオブジェクトは主にSurfaceへ文章を描写する際に使用します。太字や斜体といった描写条件を設定することもできますが、そういった設定をするよりは元々太字や斜体が設定されているフォントを読み込んだほうがいいでしょう。The rendered text can be regular strings or unicode.
Load a new font from a given filename or a python file object. The size is the height of the font in pixels. If the filename is None the Pygame default font will be loaded. If a font cannot be loaded from the arguments given an exception will be raised. Once the font is created the size cannot be changed.
Font objects are mainly used to render text into new Surface objects. The render can emulate bold or italic features, but it is better to load from a font with actual italic or bold glyphs. The rendered text can be regular strings or unicode.
この命令では、指定した文字の描写されたSurfaceを新規に作成します。 Pygameでは作成済みのSurfaceに直接文字を描写することができません。:描写するためにはFont.render命令を使用して文字の画像 (Surface)をその都度作成し、作成済みSurfaceにコピー描写しなければならないのです。
文字列は一行のみ描写することができます: のような改行文字は使用できません。 antialias引数にはtrueかfalseを設定します:trueを設定した場合は文字の角部分が滑らかに描写されます。 color引数では描写する文字の色を設定します:文字を青にするには(0,0,255)を設定します。追加で指定できるbackground引数では文字の背景色を設定します。background引数が設定されなかった場合、文字の背景は透明になります。
作成されるSurfaceのサイズは、描写する文字が収まりきるのに必要な大きさとなります。(この大きさはFont.size()命令で取得できる値と同じものです)。""のように空の文字を引数に設定した場合、幅が1ピクセルで高さがフォントの大きさに等しい空のSurfaceが作成されます。
設定した背景色やアンチエイリス使用の有無などによって、作成されるSurfacesのタイプも変わってきます。プログラムのパフォーマンスに関わるので、設定によってどういったタイプの画像描写が行われるのかを知っておいたほうがよいでしょう。 antialias引数にfalseを設定してアンチエイリスを使用しない場合、は二つのカラーパレットを持つ8bitの画像が作成され、背景色を透明にすると透明色が設定されます。antialias引数にtrueを設定してアンチエイリスを使用する場合は24bitのRGB形式の画像が作成され、背景色を透明にすると透明度が設定されます。
最適化について:文字を表示する描写面の背景色が常に一色に限定されており、なおかつ文字にアンチエイリスが使用されている場合は、文字の背景色を透明にするのではなく描写面の背景色と同じ色に設定することでパフォーマンスを向上させることができます。この最適化によって、文字を描写する際には処理に時間がかかるが透明度ではなくより軽い透明色を参照して透明処理を行うので 、動きがよくなるのです。
もじ改行文字' 'を画面に描写した場合、文字化けして判別不明の文字が表示されるでしょう。大抵は□マークが表示されます。文字を改行するためには自分で改行処理で作成する必要があります。
この文字描写処理はマルチスレッドに対応している処理ではありません:シングルスレッドでのみ処理を行うことができます。
This creates a new Surface with the specified text rendered on it. Pygame provides no way to directly draw text on an existing Surface: instead you must use Font.render - draw text on a new Surface to create an image (Surface) of the text, then blit this image onto another Surface.
The text can only be a single line: newline characters are not rendered. The antialias argument is a boolean: if true the characters will have smooth edges. The color argument is the color of the text [e.g.: (0,0,255) for blue]. The optional background argument is a color to use for the text background. If no background is passed the area outside the text will be transparent.
The Surface returned will be of the dimensions required to hold the text. (the same as those returned by Font.size()). If an empty string is passed for the text, a blank surface will be returned that is one pixel wide and the height of the font.
Depending on the type of background and antialiasing used, this returns different types of Surfaces. For performance reasons, it is good to know what type of image will be used. If antialiasing is not used, the return image will always be an 8bit image with a two color palette. If the background is transparent a colorkey will be set. Antialiased images are rendered to 24-bit RGB images. If the background is transparent a pixel alpha will be included.
Optimization: if you know that the final destination for the text (on the screen) will always have a solid background, and the text is antialiased, you can improve performance by specifying the background color. This will cause the resulting image to maintain transparency information by colorkey rather than (much less efficient) alpha values.
If you render ' ' a unknown char will be rendered. Usually a rectangle. Instead you need to handle new lines yourself.
Font rendering is not thread safe: only a single thread can render text any time.
文字を描写するのに必要な縦横サイズを戻り値として取得します。この命令を使用することで、文字を描写する際の表示位置を決める作業が楽になるでしょう。改行処理に使用できますし、他にも文字の配置変更が必要な特殊効果などで使用できるでしょう。
大抵のフォントでは、特定の文字の組み合わせを描写する際に文字間隔の調整が行われるので注意してください。例えば"a"と"e"の二文字を描写する場合、 " ae"と続けて描写する際には文字の間隔調整が行われるので、"a" と "e"を個別に描写する際と比べてサイズが変わることもあります。
Returns the dimensions needed to render the text. This can be used to help determine the positioning needed for text before it is rendered. It can also be used for wordwrapping and other layout effects.
Be aware that most fonts use kerning which adjusts the widths for specific letter pairs. For example, the width for "ae" will not always match the width for "a" + "e".
可能な場合、全ての文字に下線を付けて描写します。この下線の太さは常に1ピクセルとなり、この1ピクセルはフォントの大きさには含まれません。この設定は太字や斜体の設定を組み合わせて使用することができます。
When enabled, all rendered fonts will include an underline. The underline is always one pixel thick, regardless of font size. This can be mixed with the bold and italic modes.
文字に下線が付けられる場合はTrueが戻り値として返されます。
Return True when the font underline is enabled.
テキストの太字描写を有効にします。文字を引き伸ばして擬似的に太字に見せているだけなので、大抵のフォントで文字の見栄えが悪くなってしまいます。 可能ならば既に太字が設定されているフォントファイルを読み込んで描写したほうがよいでしょう。太字描写を有効にすると、通常の状態と比較して文字描写面の横幅が広くなります。この設定は斜体や下線の設定を組み合わせて使用することができます。
Enables the bold rendering of text. This is a fake stretching of the font that doesn't look good on many font types. If possible load the font from a real bold font file. While bold, the font will have a different width than when normal. This can be mixed with the italic and underline modes.
文字の太字設定が有効になっている場合は戻り値としてTrueが返されます。
Return True when the font bold rendering mode is enabled.
文字の擬似的な斜体描写を有効にします。文字を傾けて擬似的に斜体に見せているだけなので、大抵のフォントで文字の見栄えが悪くなってしまいます。 可能ならば既に斜体が設定されているフォントファイルを読み込んで描写したほうがよいでしょう。斜体描写を有効にすると、通常の状態と比較して文字描写面の横幅が広くなります。この設定は太字や下線の設定を組み合わせて使用することができます。
※原文では「Font.set_bold(bool)」と記載されていますが、誤りの可能性があるため「Font.set_italic(bool)」に変更しています。
Enables fake rendering of italic text. This is a fake skewing of the font that doesn't look good on many font types. If possible load the font from a real italic font file. While italic the font will have a different width than when normal. This can be mixed with the bold and underline modes.
The list contains tuples for each character, which contain the minimum X offset, the maximum X offset, the minimum Y offset, the maximum Y offset and the advance offset (bearing plus width) of the character. [(minx, maxx, miny, maxy, advance), (minx, maxx, miny, maxy, advance), ...]
The list contains tuples for each character, which contain the minimum X offset, the maximum X offset, the minimum Y offset, the maximum Y offset and the advance offset (bearing plus width) of the character. [(minx, maxx, miny, maxy, advance), (minx, maxx, miny, maxy, advance), ...]
文字の斜体設定が有効になっている場合は戻り値としてTrueが返されます。
Return True when the font italic rendering mode is enabled.
このフォントで描写する文字の、一行分の縦幅ピクセルを戻り値として取得します。 文字を改行したい場合は、この命令で取得した値分だけ文字の位置を下げて描写するのをお勧めします。
Return the height in pixels for a line of text with the font. When rendering multiple lines of text this is the recommended amount of space between lines.
描写する文字の縦幅ピクセルを戻り値として返します。ここで取得できる値はフォントで使用される文字イメージの平均サイズです。
Return the height in pixels of the actual rendered text. This is the average size for each glyph in the font.
フォントアセントの縦幅ピクセルを戻り値として返します。アセントとはフォントのベースラインから文字上部までのピクセル値です。
Return the height in pixels for the font ascent. The ascent is the number of pixels from the font baseline to the top of the font.
フォントディセントの縦幅ピクセルを戻り値として返します。ディセントとはフォントのベースラインから文字下部までのピクセル値です。
Return the height in pixels for the font descent. The descent is the number of pixels from the font baseline to the bottom of the font.