Font Drawing (blf)¶
This module provides access to blenders text drawing functions.
Hello World Text Example¶
Blender Game Engine example of using the blf module. For this module to work we
need to use the OpenGL wrapper bgl
as well.
# import game engine modules
from bge import render
from bge import logic
# import stand alone modules
import bgl
import blf
def init():
"""init function - runs once"""
# create a new font object, use external ttf file
font_path = logic.expandPath('//Zeyada.ttf')
# store the font indice - to use later
logic.font_id = blf.load(font_path)
# set the font drawing routine to run every frame
scene = logic.getCurrentScene()
scene.post_draw = [write]
def write():
"""write on screen"""
width = render.getWindowWidth()
height = render.getWindowHeight()
# OpenGL setup
bgl.glMatrixMode(bgl.GL_PROJECTION)
bgl.glLoadIdentity()
bgl.gluOrtho2D(0, width, 0, height)
bgl.glMatrixMode(bgl.GL_MODELVIEW)
bgl.glLoadIdentity()
# BLF drawing routine
font_id = logic.font_id
blf.position(font_id, (width * 0.2), (height * 0.3), 0)
blf.size(font_id, 50, 72)
blf.draw(font_id, "Hello World")
-
blf.
aspect
(fontid, aspect)¶ Set the aspect for drawing text.
Parameters: - fontid (int) – The id of the typeface as returned by
blf.load()
, for default font use 0. - aspect (float) – The aspect ratio for text drawing to use.
- fontid (int) – The id of the typeface as returned by
-
blf.
blur
(fontid, radius)¶ Set the blur radius for drawing text.
Parameters: - fontid (int) – The id of the typeface as returned by
blf.load()
, for default font use 0. - radius (int) – The radius for blurring text (in pixels).
- fontid (int) – The id of the typeface as returned by
-
blf.
clipping
(fontid, xmin, ymin, xmax, ymax)¶ Set the clipping, enable/disable using CLIPPING.
Parameters: - fontid (int) – The id of the typeface as returned by
blf.load()
, for default font use 0. - xmin (float) – Clip the drawing area by these bounds.
- ymin (float) – Clip the drawing area by these bounds.
- xmax (float) – Clip the drawing area by these bounds.
- ymax (float) – Clip the drawing area by these bounds.
- fontid (int) – The id of the typeface as returned by
-
blf.
dimensions
(fontid, text)¶ Return the width and height of the text.
Parameters: - fontid (int) – The id of the typeface as returned by
blf.load()
, for default font use 0. - text (string) – the text to draw.
Returns: the width and height of the text.
Return type: tuple of 2 floats
- fontid (int) – The id of the typeface as returned by
-
blf.
disable
(fontid, option)¶ Disable option.
Parameters: - fontid (int) – The id of the typeface as returned by
blf.load()
, for default font use 0. - option (int) – One of ROTATION, CLIPPING, SHADOW or KERNING_DEFAULT.
- fontid (int) – The id of the typeface as returned by
-
blf.
draw
(fontid, text)¶ Draw text in the current context.
Parameters: - fontid (int) – The id of the typeface as returned by
blf.load()
, for default font use 0. - text (string) – the text to draw.
- fontid (int) – The id of the typeface as returned by
-
blf.
enable
(fontid, option)¶ Enable option.
Parameters: - fontid (int) – The id of the typeface as returned by
blf.load()
, for default font use 0. - option (int) – One of ROTATION, CLIPPING, SHADOW or KERNING_DEFAULT.
- fontid (int) – The id of the typeface as returned by
-
blf.
load
(filename)¶ Load a new font.
Parameters: filename (string) – the filename of the font. Returns: the new font’s fontid or -1 if there was an error. Return type: integer
-
blf.
position
(fontid, x, y, z)¶ Set the position for drawing text.
Parameters: - fontid (int) – The id of the typeface as returned by
blf.load()
, for default font use 0. - x (float) – X axis position to draw the text.
- y (float) – Y axis position to draw the text.
- z (float) – Z axis position to draw the text.
- fontid (int) – The id of the typeface as returned by
-
blf.
rotation
(fontid, angle)¶ Set the text rotation angle, enable/disable using ROTATION.
Parameters: - fontid (int) – The id of the typeface as returned by
blf.load()
, for default font use 0. - angle (float) – The angle for text drawing to use.
- fontid (int) – The id of the typeface as returned by
-
blf.
shadow
(fontid, level, r, g, b, a)¶ Shadow options, enable/disable using SHADOW .
Parameters: - fontid (int) – The id of the typeface as returned by
blf.load()
, for default font use 0. - level (int) – The blur level, can be 3, 5 or 0.
- r (float) – Shadow color (red channel 0.0 - 1.0).
- g (float) – Shadow color (green channel 0.0 - 1.0).
- b (float) – Shadow color (blue channel 0.0 - 1.0).
- a (float) – Shadow color (alpha channel 0.0 - 1.0).
- fontid (int) – The id of the typeface as returned by
-
blf.
shadow_offset
(fontid, x, y)¶ Set the offset for shadow text.
Parameters: - fontid (int) – The id of the typeface as returned by
blf.load()
, for default font use 0. - x (float) – Vertical shadow offset value in pixels.
- y (float) – Horizontal shadow offset value in pixels.
- fontid (int) – The id of the typeface as returned by
-
blf.
size
(fontid, size, dpi)¶ Set the size and dpi for drawing text.
Parameters: - fontid (int) – The id of the typeface as returned by
blf.load()
, for default font use 0. - size (int) – Point size of the font.
- dpi (int) – dots per inch value to use for drawing.
- fontid (int) – The id of the typeface as returned by
-
blf.
unload
(filename)¶ Unload an existing font.
Parameters: filename (string) – the filename of the font.
-
blf.
CLIPPING
¶ constant value 2
-
blf.
KERNING_DEFAULT
¶ constant value 8
-
blf.
ROTATION
¶ constant value 1
-
blf.
SHADOW
¶ constant value 4