tcod.tileset - Font Loading Functions

Tileset and font related functions.

Tilesets can be loaded as a whole from tile-sheets or True-Type fonts, or they can be put together from multiple tile images by loading them separately using Tileset.set_tile.

A major restriction with libtcod is that all tiles must be the same size and tiles can’t overlap when rendered. For sprite-based rendering it can be useful to use an alternative library for graphics rendering while continuing to use python-tcod’s pathfinding and field-of-view algorithms.

class tcod.tileset.Tileset(tile_width: int, tile_height: int)[source]

A collection of graphical tiles.

This class is provisional, the API may change in the future.

__contains__(codepoint: int) → bool[source]

Test if a tileset has a codepoint with n in tileset.

get_tile(codepoint: int) → numpy.ndarray[source]

Return a copy of a tile for the given codepoint.

If the tile does not exist yet then a blank array will be returned.

The tile will have a shape of (height, width, rgba) and a dtype of uint8. Note that most grey-scale tiles will only use the alpha channel and will usually have a solid white color channel.

remap(codepoint: int, x: int, y: int = 0) → None[source]

Reassign a codepoint to a character in this tileset.

codepoint is the Unicode codepoint to assign.

x and y is the position of the tilesheet to assign to codepoint. This is the tile position itself, not the pixel position of the tile. Large values of x will wrap to the next row, so using x by itself is equivalent to Tile Index in the Character Table Reference.

This is normally used on loaded tilesheets. Other methods of Tileset creation won’t have reliable tile indexes.

New in version 11.12.

render(console: tcod.console.Console) → numpy.ndarray[source]

Render an RGBA array, using console with this tileset.

console is the Console object to render, this can not be the root console.

The output array will be a np.uint8 array with the shape of: (con_height * tile_height, con_width * tile_width, 4).

New in version 11.9.

set_tile(codepoint: int, tile: numpy.ndarray) → None[source]

Upload a tile into this array.

The tile can be in 32-bit color (height, width, rgba), or grey-scale (height, width). The tile should have a dtype of np.uint8.

This data may need to be sent to graphics card memory, this is a slow operation.

tile_height

The height of the tile in pixels.

tile_shape

The shape (height, width) of the tile in pixels.

tile_width

The width of the tile in pixels.

tcod.tileset.get_default() → tcod.tileset.Tileset[source]

Return a reference to the default Tileset.

New in version 11.10.

Deprecated since version 11.13: The default tileset is deprecated. With contexts this is no longer needed.

tcod.tileset.load_bdf(path: str) → tcod.tileset.Tileset[source]

Return a new Tileset from a .bdf file.

For the best results the font should be monospace, cell-based, and single-width. As an example, a good set of fonts would be the Unicode fonts and tools for X11 package.

Pass the returned Tileset to tcod.tileset.set_default and it will take effect when tcod.console_init_root is called.

New in version 11.10.

tcod.tileset.load_tilesheet(path: str, columns: int, rows: int, charmap: Optional[Iterable[int]]) → tcod.tileset.Tileset[source]

Return a new Tileset from a simple tilesheet image.

path is the file path to a PNG file with the tileset.

columns and rows is the shape of the tileset. Tiles are assumed to take up the entire space of the image.

charmap is the character mapping to use. This is a list or generator of codepoints which map the tiles like this: charmap[tile_index] = codepoint. For common tilesets charmap should be tcod.tileset.CHARMAP_CP437. Generators will be sliced so itertools.count can be used which will give all tiles the same codepoint as their index, but this will not map tiles onto proper Unicode. If None is used then no tiles will be mapped, you will need to use Tileset.remap to assign codepoints to this Tileset.

New in version 11.12.

tcod.tileset.load_truetype_font(path: str, tile_width: int, tile_height: int) → tcod.tileset.Tileset[source]

Return a new Tileset from a .ttf or .otf file.

Same as set_truetype_font, but returns a Tileset instead. You can send this Tileset to set_default.

This function is provisional. The API may change.

tcod.tileset.set_default(tileset: tcod.tileset.Tileset) → None[source]

Set the default tileset.

The display will use this new tileset immediately.

New in version 11.10.

Deprecated since version 11.13: The default tileset is deprecated. With contexts this is no longer needed.

tcod.tileset.set_truetype_font(path: str, tile_width: int, tile_height: int) → None[source]

Set the default tileset from a .ttf or .otf file.

path is the file path for the font file.

tile_width and tile_height are the desired size of the tiles in the new tileset. The font will be scaled to fit the given tile_height and tile_width.

This function must be called before tcod.console_init_root. Once the root console is setup you may call this funtion again to change the font. The tileset can be changed but the window will not be resized automatically.

New in version 9.2.

Deprecated since version 11.13: This function does not support contexts. Use load_truetype_font instead.

tcod.tileset.CHARMAP_CP437 = [0, 9786, 9787, 9829, 9830, 9827, 9824, 8226, 9688, 9675, 9689, 9794, 9792, 9834, 9835, 9788, 9658, 9668, 8597, 8252, 182, 167, 9644, 8616, 8593, 8595, 8594, 8592, 8735, 8596, 9650, 9660, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 199, 252, 233, 226, 228, 224, 229, 231, 234, 235, 232, 239, 238, 236, 196, 197, 201, 230, 198, 244, 246, 242, 251, 249, 255, 214, 220, 162, 163, 165, 8359, 402, 225, 237, 243, 250, 241, 209, 170, 186, 191, 8976, 172, 189, 188, 161, 171, 187, 9617, 9618, 9619, 9474, 9508, 9569, 9570, 9558, 9557, 9571, 9553, 9559, 9565, 9564, 9563, 9488, 9492, 9524, 9516, 9500, 9472, 9532, 9566, 9567, 9562, 9556, 9577, 9574, 9568, 9552, 9580, 9575, 9576, 9572, 9573, 9561, 9560, 9554, 9555, 9579, 9578, 9496, 9484, 9608, 9604, 9612, 9616, 9600, 945, 223, 915, 960, 931, 963, 181, 964, 934, 920, 937, 948, 8734, 966, 949, 8745, 8801, 177, 8805, 8804, 8992, 8993, 247, 8776, 176, 8729, 183, 8730, 8319, 178, 9632, 160]

A code page 437 character mapping.

See Code Page 437 for more info and a table of glyphs.

New in version 11.12.

tcod.tileset.CHARMAP_TCOD = [32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 91, 92, 93, 94, 95, 96, 123, 124, 125, 126, 9617, 9618, 9619, 9474, 9472, 9532, 9508, 9524, 9500, 9516, 9492, 9484, 9488, 9496, 9624, 9629, 9600, 9622, 9626, 9616, 9623, 8593, 8595, 8592, 8594, 9650, 9660, 9668, 9658, 8597, 8596, 9744, 9745, 9675, 9673, 9553, 9552, 9580, 9571, 9577, 9568, 9574, 9562, 9556, 9559, 9565, 0, 0, 0, 0, 0, 0, 0, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 0, 0, 0, 0, 0, 0, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 0, 0, 0, 0, 0, 0]

The layout used by older libtcod fonts, in Unicode.

This layout is non-standard, and it’s not recommend to make a font for it, but you might need it to load an existing font made for libtcod.

This character map is in Unicode, so old code using the non-Unicode tcod.CHAR_* constants will need to be updated.

See Deprecated TCOD Layout for a table of glyphs used in this character map.

New in version 11.12.