Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions bricks/_common/sources.mk
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ PBIO_SRC_C = $(addprefix lib/pbio/,\
src/drivebase.c \
src/error.c \
src/geometry.c \
src/image/font_liberationsans_regular_14.c \
src/image/font_terminus_normal_16.c \
src/image/image.c \
src/imu.c \
src/int_math.c \
Expand Down
116 changes: 116 additions & 0 deletions lib/pbio/include/pbio/font.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// SPDX-License-Identifier: MIT
// Copyright (c) 2025 Nicolas Schodet

/**
* @addtogroup Font Text font.
* @{
*/

#ifndef _PBIO_FONT_H_
#define _PBIO_FONT_H_

#include <pbio/config.h>

#include <stdint.h>

/**
* Single glyph.
*/
typedef struct _pbio_font_glyph_t {
/**
* Width of glyph bitmap.
*/
uint8_t width;
/**
* Height of glyph bitmap.
*/
uint8_t height;
/**
* Advance to next character in horizontal direction.
*/
uint8_t advance;
/**
* Horizontal distance between pen position and leftmost bitmap column.
* Positive values goes left.
*/
int8_t left;
/**
* Vertical distance between baseline and topmost bitmap row. Positive
* values goes up.
*/
int8_t top;
/**
* Index of bitmap start in data table.
*/
uint16_t data_index;
/**
* Start index of kerning values in kerning table. The Stop index is the
* start index of the next glyph.
*/
uint16_t kerning_index;
} pbio_font_glyph_t;

/**
* Kerning information for a glyph.
*/
typedef struct _pbio_font_kerning_t {
/**
* Previous glyph.
*/
uint8_t previous;
/**
* Kerning from previous glyph to current one.
*/
int8_t kerning;
} pbio_font_kerning_t;

/**
* Font.
*
* Every bitmap data is composed of one or several rows as indicated in the
* glyph structure, with the top row first. Eight pixels are packed into
* a bytes, with the first leftmost pixel in the most significant bit. The
* bitmap width is padded to the next multiple of eight.
*/
typedef struct _pbio_font_t {
/**
* First glyph.
*/
uint8_t first;
/**
* Last glyph.
*/
uint8_t last;
/**
* Line height, number of pixels between consecutive baselines.
*/
uint8_t line_height;
/**
* Maximum value of glyph top.
*/
int8_t top_max;
/**
* Glyph table. Must contain a dummy last glyph for the last kerning stop
* index.
*/
const pbio_font_glyph_t *glyphs;
/**
* Data table with bitmaps.
*/
const uint8_t *data;
/**
* Kerning table, may be NULL if no kerning.
*/
const pbio_font_kerning_t *kernings;
} pbio_font_t;

#if PBIO_CONFIG_IMAGE

extern const pbio_font_t pbio_font_liberationsans_regular_14;
extern const pbio_font_t pbio_font_terminus_normal_16;

#endif // PBIO_CONFIG_IMAGE

#endif // _PBIO_FONT_H_

/** @} */
30 changes: 30 additions & 0 deletions lib/pbio/include/pbio/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
#define _PBIO_IMAGE_H_

#include <pbio/config.h>
#include <pbio/font.h>

#include <stdint.h>
#include <stddef.h>

/**
* Image container.
Expand Down Expand Up @@ -51,6 +53,28 @@ typedef struct _pbio_image_t {
int stride;
} pbio_image_t;

/**
* Coordinates of a rectangle.
*/
typedef struct _pbio_image_rect_t {
/**
* X coordinate.
*/
int x;
/**
* Y coordinate.
*/
int y;
/**
* Number of columns.
*/
int width;
/**
* Number of rows.
*/
int height;
} pbio_image_rect_t;

#if PBIO_CONFIG_IMAGE

void pbio_image_init(pbio_image_t *image, uint8_t *pixels, int width,
Expand Down Expand Up @@ -99,6 +123,12 @@ void pbio_image_draw_circle(pbio_image_t *image, int x, int y, int r,
void pbio_image_fill_circle(pbio_image_t *image, int x, int y, int r,
uint8_t value);

void pbio_image_draw_text(pbio_image_t *image, const pbio_font_t *font, int x,
int y, const char *text, size_t text_len, uint8_t value);

void pbio_image_bbox_text(const pbio_font_t *font, const char *text,
size_t text_len, pbio_image_rect_t *rect);

#endif // PBIO_CONFIG_IMAGE

#endif // _PBIO_IMAGE_H_
Expand Down
25 changes: 25 additions & 0 deletions lib/pbio/src/image/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this Makefile to generate font files.

FONTCONVERT = ../../tools/fontconvert.py

LIBERATION_LICENSE = "OFL-1.1-RFN"
LIBERATION_COPYRIGHT = \
--copyright "Digitized data copyright (c) 2010 Google Corporation" \
--copyright " with Reserved Font Arimo, Tinos and Cousine." \
--copyright "Copyright (c) 2012 Red Hat, Inc." \
--copyright " with Reserved Font Name Liberation."
LIBERATION_SANS_REGULAR = /usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf

TERMINUS_LICENSE = "OFL-1.1-RFN"
TERMINUS_COPYRIGHT = \
--copyright "Copyright (c) 2010-2014 Dimitar Toshkov Zhekov," \
--copyright ' with Reserved Font Name "Terminus Font".'
TERMINUS_NORMAL = /usr/share/fonts/opentype/terminus/terminus-normal.otb

all: font_liberationsans_regular_14.c font_terminus_normal_16.c

font_liberationsans_regular_14.c: $(LIBERATION_SANS_REGULAR) $(FONTCONVERT)
python3 $(FONTCONVERT) $< 14 --license $(LIBERATION_LICENSE) $(LIBERATION_COPYRIGHT) > $@

font_terminus_normal_16.c: $(TERMINUS_NORMAL) $(FONTCONVERT)
python3 $(FONTCONVERT) $< 16 --license $(TERMINUS_LICENSE) $(TERMINUS_COPYRIGHT) > $@
Loading
Loading