Esempio n. 1
0
def set_truetype_font(path: str, tile_width: int, tile_height: int) -> None:
    """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 :any:`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.

    .. versionadded:: 9.2

    .. deprecated:: 11.13
        This function does not support contexts.
        Use :any:`load_truetype_font` instead.
    """
    if not os.path.exists(path):
        raise RuntimeError("File not found:\n\t%s" %
                           (os.path.realpath(path), ))
    if lib.TCOD_tileset_load_truetype_(path.encode(), tile_width, tile_height):
        raise RuntimeError(ffi.string(lib.TCOD_get_error()))
Esempio n. 2
0
def _check_warn(error: int, stacklevel: int = 2) -> int:
    """Like _check, but raises a warning on positive error codes."""
    if _check(error) > 0:
        warnings.warn(
            ffi.string(lib.TCOD_get_error()).decode(),
            RuntimeWarning,
            stacklevel=stacklevel + 1,
        )
    return error
Esempio n. 3
0
def load_truetype_font(path: str, tile_width: int,
                       tile_height: int) -> Tileset:
    """Return a new Tileset from a `.ttf` or `.otf` file.

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

    This function is provisional.  The API may change.
    """
    if not os.path.exists(path):
        raise RuntimeError("File not found:\n\t%s" %
                           (os.path.realpath(path), ))
    cdata = lib.TCOD_load_truetype_font_(path.encode(), tile_width,
                                         tile_height)
    if not cdata:
        raise RuntimeError(ffi.string(lib.TCOD_get_error()))
    return Tileset._claim(cdata)
Esempio n. 4
0
def load_bdf(path: str) -> Tileset:
    """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 <https://www.cl.cam.ac.uk/~mgk25/ucs-fonts.html>`_
    package.

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

    .. versionadded:: 11.10
    """
    if not os.path.exists(path):
        raise RuntimeError("File not found:\n\t%s" % (os.path.realpath(path),))
    cdata = lib.TCOD_load_bdf(path.encode())
    if not cdata:
        raise RuntimeError(ffi.string(lib.TCOD_get_error()).decode())
    return Tileset._claim(cdata)
Esempio n. 5
0
 def from_sdl_event(cls, sdl_event: Any) -> "TextInput":
     self = cls(ffi.string(sdl_event.text.text, 32).decode("utf8"))
     self.sdl_event = sdl_event
     return self
Esempio n. 6
0
def _unpack_char_p(char_p: Any) -> str:
    if char_p == ffi.NULL:
        return ""
    return ffi.string(char_p).decode()  # type: ignore
Esempio n. 7
0
def _raise_tcod_error() -> NoReturn:
    """Raise an error from libtcod, this function assumes an error exists."""
    raise RuntimeError(ffi.string(lib.TCOD_get_error()).decode("utf-8"))
Esempio n. 8
0
def _check(error: int) -> int:
    """Detect and convert a libtcod error code it into an exception."""
    if error < 0:
        raise RuntimeError(ffi.string(lib.TCOD_get_error()).decode())
    return error
Esempio n. 9
0
def _pycall_cli_output(catch_reference: Any, output: Any) -> None:
    """Callback for the libtcod context CLI.  Catches the CLI output."""
    catch = ffi.from_handle(catch_reference)  # type: List[str]
    catch.append(ffi.string(output).decode("utf-8"))