numberlink.number_renderΒΆ
Bitmap text rendering utilities.
This module provides lightweight bitmap font data and helper functions to render small text overlays into RGB image
arrays. The functions are intended for debug rendering and simple user interfaces where external font dependencies are
undesirable. Use render_bitmap_text_to_array() to draw left-aligned text, render_bitmap_text_centered() to
draw centered text, and build_endpoint_labels() to compute label positions for grid endpoints.
The module exposes a compact bitmap font mapping in BITMAP_FONT.
Module ContentsΒΆ
FunctionsΒΆ
Render |
|
Render |
|
Compute label specifications for grid endpoints. |
DataΒΆ
APIΒΆ
- numberlink.number_render.render_bitmap_text_to_array(text: str, topleft: Coord, color: RGBInt, img: numpy.typing.NDArray[numpy.uint8], scale: int = 2, outline_color: RGBInt | None = (0, 0, 0), outline_thickness: int = 1) int[source]ΒΆ
Render
textinto an RGB image array at a top-left position.Draw a string using the
BITMAP_FONTmapping into the provided image array. The function writes directly intoimgand returns the horizontal advance in pixels from the originaltopleftto the end of the rendered text.The image array uses the (row, column, channel) indexing convention. The
topleftargument uses an (x, y) coordinate convention wherexis the column andyis the row. Userender_bitmap_text_centered()to draw text centered at a point instead of at a top-left corner.- Parameters:
text (str) β Text to render. Characters not present in
BITMAP_FONTare rendered as space characters.topleft (
tuple) β Pixel coordinates for the top-left corner as(x, y)wherexis column andyis row.color (tuple[int, int, int]) β RGB color tuple with values in the range
0to255.img (
numpy.ndarray) β Target RGB array with shape(H, W, 3)and dtypenumpy.uint8. The array is modified in place.scale (int, optional) β Pixel size used for each bitmap cell. Min value is
1. Larger values scale the glyphs uniformly. Defaults to2.outline_color (tuple[int, int, int] or None, optional) β RGB tuple used to draw an outline around glyphs. If
None, no outline is drawn. Defaults to(0, 0, 0).outline_thickness (int, optional) β Outline thickness in pixels measured in bitmap units. Values less than
1disable outline drawing. Defaults to1.
- Returns:
Horizontal width in pixels of the rendered string.
- Return type:
See also
render_bitmap_text_centered()for centered text rendering.Note
The function modifies
imgin place and performs clipping when the glyphs fall outside the image bounds.
- numberlink.number_render.render_bitmap_text_centered(text: str, center: Coord, color: RGBInt, img: numpy.typing.NDArray[numpy.uint8], scale: int = 2, outline_color: RGBInt | None = (0, 0, 0), outline_thickness: int = 1) None[source]ΒΆ
Render
textcentered at the given pixel coordinates.Compute a top-left origin that centers the rendered text at
centerand delegate torender_bitmap_text_to_array()for actual drawing. Thecenterargument uses an (x, y) convention wherexis column andyis row.- Parameters:
text (str) β Text to render.
center (
tuple) β Pixel coordinates for the text center as(x, y)wherexis column andyis row.color (tuple[int, int, int]) β RGB color tuple with values in the range
0to255.img (
numpy.ndarray) β Target RGB array with shape(H, W, 3)and dtypenumpy.uint8. The array is modified in place.scale (int, optional) β Pixel size used for each bitmap cell. Defaults to
2.outline_color (tuple[int, int, int] or None, optional) β RGB tuple used to draw an outline around glyphs or
Noneto disable outlining. Defaults to(0, 0, 0).outline_thickness (int, optional) β Outline thickness in pixels measured in bitmap units. Defaults to
1.
See also
render_bitmap_text_to_array()for the drawing implementation.
- numberlink.number_render.build_endpoint_labels(endpoints: collections.abc.Iterable[tuple[Coord, Coord]] | numpy.typing.NDArray[numpy.int_], pixels_per_cell_h: int, pixels_per_cell_w: int, min_scale: int | None = None, max_scale: int | None = None, gridline_thickness: int = 0) list[tuple[str, Coord, RGBInt, int]][source]ΒΆ
Compute label specifications for grid endpoints.
Accept either a NumPy array of shape
(num_colors, 2, 2)with endpoint coordinates in(row, col)format or an iterable of((r0, c0), (r1, c1))pairs. For each endpoint this function computes a text label string, a pixel center position suitable forrender_bitmap_text_to_array(), and an adaptive bitmap scale that fits within the cell size.The returned color tuple is a suggested foreground color for the text. The caller may override this when drawing. The function does not perform any drawing itself.
- Parameters:
endpoints (collections.abc.Iterable or
numpy.ndarray) β Endpoint coordinate pairs. Supported formats are an array with shape(num_colors, 2, 2)or an iterable of pairs.pixels_per_cell_h (int) β Height of a single grid cell in pixels.
pixels_per_cell_w (int) β Width of a single grid cell in pixels.
min_scale (int or None, optional) β Minimum bitmap scale to suggest. If
Noneno minimum is enforced. Defaults toNone.max_scale (int or None, optional) β Maximum bitmap scale to suggest. If
Noneno maximum is enforced. Defaults toNone.gridline_thickness (int, optional) β Thickness of grid lines in pixels. Offsets label centers away from gridline pixels. Defaults to
0.
- Returns:
A list of tuples
(text, center, color, scale)wheretextis the string to draw,centeris the pixel(x, y)center forrender_bitmap_text_centered(),coloris a suggested RGB foreground tuple, andscaleis an integer bitmap scale.- Return type:
list[tuple[str, tuple[int, int], tuple[int, int, int], int]]
See also
render_bitmap_text_centered()andrender_bitmap_text_to_array()for drawing functions that consume these specifications.