Skip to content

Commit

Permalink
PicoVector: Improve text rendering and control.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gadgetoid committed Oct 8, 2024
1 parent 2d556d1 commit 6f2393a
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 8 deletions.
14 changes: 9 additions & 5 deletions libraries/pico_vector/alright-fonts.h
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,7 @@ int get_max_line_width(af_face_t *face, const char *text, af_text_metrics_t *tm)
return max_width;
}


void af_render(af_face_t *face, const char *text, af_text_metrics_t *tm) {
void af_render(af_face_t *face, const char *text, size_t tlen, af_text_metrics_t *tm) {
pp_mat3_t *old = pp_transform(NULL);

float line_height = (tm->line_height * 128.0f) / 100.0f;
Expand All @@ -290,8 +289,9 @@ void af_render(af_face_t *face, const char *text, af_text_metrics_t *tm) {
caret.x = 0;
caret.y = 0;

char *done = (char *)text + tlen;
char *end = strchr(text, '\n');
if (!end) end = (char *)text + strlen(text);
if (!end) end = done;

while(true) {
int line_width = get_line_width(face, text, tm);
Expand Down Expand Up @@ -327,9 +327,9 @@ void af_render(af_face_t *face, const char *text, af_text_metrics_t *tm) {
}

text = end + 1;
if (*text == '\0') break;
if (*text == '\0' || text > done) break;
end = strchr(text, '\n');
if (!end) end = (char *)text + strlen(text);
if (!end) end = (char *)text + tlen;

caret.x = 0;
caret.y += line_height;
Expand All @@ -340,6 +340,10 @@ void af_render(af_face_t *face, const char *text, af_text_metrics_t *tm) {
pp_transform(old);
}

void _af_render(af_face_t *face, const char *text, af_text_metrics_t *tm) {
af_render(face, text, strlen(text), tm);
}

pp_rect_t af_measure(af_face_t *face, const char *text, af_text_metrics_t *tm) {
pp_rect_t result;
bool first = true;
Expand Down
2 changes: 1 addition & 1 deletion libraries/pico_vector/pico_vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ namespace pimoroni {

text_metrics.transform = t;

af_render(text_metrics.face, text.data(), &text_metrics);
af_render(text_metrics.face, text.data(), text.size(), &text_metrics);

return caret;
/*
Expand Down
12 changes: 12 additions & 0 deletions libraries/pico_vector/pico_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@ namespace pimoroni {
text_metrics.size = font_size;
}

void set_font_word_spacing(unsigned int font_wordspacing) {
text_metrics.word_spacing = font_wordspacing;
}

void set_font_letter_spacing(unsigned int font_letterspacing) {
text_metrics.letter_spacing = font_letterspacing;
}

void set_font_line_height(unsigned int font_line_height) {
text_metrics.line_height = font_line_height;
}

bool set_font(std::string_view font_path, unsigned int font_size) {
if(text_metrics.face) {
af_free(text_metrics.face->glyphs);
Expand Down
8 changes: 8 additions & 0 deletions micropython/modules/picovector/picovector.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,13 @@ MP_DEFINE_CONST_OBJ_TYPE(

static MP_DEFINE_CONST_FUN_OBJ_3(TRANSFORM_rotate_obj, TRANSFORM_rotate);
static MP_DEFINE_CONST_FUN_OBJ_3(TRANSFORM_translate_obj, TRANSFORM_translate);
static MP_DEFINE_CONST_FUN_OBJ_3(TRANSFORM_scale_obj, TRANSFORM_scale);
static MP_DEFINE_CONST_FUN_OBJ_1(TRANSFORM_reset_obj, TRANSFORM_reset);

static const mp_rom_map_elem_t TRANSFORM_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_rotate), MP_ROM_PTR(&TRANSFORM_rotate_obj) },
{ MP_ROM_QSTR(MP_QSTR_translate), MP_ROM_PTR(&TRANSFORM_translate_obj) },
{ MP_ROM_QSTR(MP_QSTR_scale), MP_ROM_PTR(&TRANSFORM_scale_obj) },
{ MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&TRANSFORM_reset_obj) },
};

Expand All @@ -80,6 +82,9 @@ MP_DEFINE_CONST_OBJ_TYPE(
static MP_DEFINE_CONST_FUN_OBJ_KW(VECTOR_text_obj, 4, VECTOR_text);
static MP_DEFINE_CONST_FUN_OBJ_3(VECTOR_set_font_obj, VECTOR_set_font);
static MP_DEFINE_CONST_FUN_OBJ_2(VECTOR_set_font_size_obj, VECTOR_set_font_size);
static MP_DEFINE_CONST_FUN_OBJ_2(VECTOR_set_font_word_spacing_obj, VECTOR_set_font_word_spacing);
static MP_DEFINE_CONST_FUN_OBJ_2(VECTOR_set_font_letter_spacing_obj, VECTOR_set_font_letter_spacing);
static MP_DEFINE_CONST_FUN_OBJ_2(VECTOR_set_font_line_height_obj, VECTOR_set_font_line_height);
static MP_DEFINE_CONST_FUN_OBJ_2(VECTOR_set_antialiasing_obj, VECTOR_set_antialiasing);
static MP_DEFINE_CONST_FUN_OBJ_2(VECTOR_set_transform_obj, VECTOR_set_transform);
static MP_DEFINE_CONST_FUN_OBJ_2(VECTOR_set_clip_obj, VECTOR_set_clip);
Expand All @@ -89,6 +94,9 @@ static MP_DEFINE_CONST_FUN_OBJ_2(VECTOR_draw_obj, VECTOR_draw);
static const mp_rom_map_elem_t VECTOR_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_set_font), MP_ROM_PTR(&VECTOR_set_font_obj) },
{ MP_ROM_QSTR(MP_QSTR_set_font_size), MP_ROM_PTR(&VECTOR_set_font_size_obj) },
{ MP_ROM_QSTR(MP_QSTR_set_font_word_spacing), MP_ROM_PTR(&VECTOR_set_font_word_spacing_obj) },
{ MP_ROM_QSTR(MP_QSTR_set_font_letter_spacing), MP_ROM_PTR(&VECTOR_set_font_letter_spacing_obj) },
{ MP_ROM_QSTR(MP_QSTR_set_font_line_height), MP_ROM_PTR(&VECTOR_set_font_line_height_obj) },
{ MP_ROM_QSTR(MP_QSTR_set_antialiasing), MP_ROM_PTR(&VECTOR_set_antialiasing_obj) },
{ MP_ROM_QSTR(MP_QSTR_set_transform), MP_ROM_PTR(&VECTOR_set_transform_obj) },
{ MP_ROM_QSTR(MP_QSTR_set_clip), MP_ROM_PTR(&VECTOR_set_clip_obj) },
Expand Down
38 changes: 36 additions & 2 deletions micropython/modules/picovector/picovector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,17 @@ mp_obj_t TRANSFORM_translate(mp_obj_t self_in, mp_obj_t x_in, mp_obj_t y_in) {
return mp_const_none;
}

mp_obj_t TRANSFORM_scale(mp_obj_t self_in, mp_obj_t x_in, mp_obj_t y_in) {
_TRANSFORM_obj_t *transform = MP_OBJ_TO_PTR2(self_in, _TRANSFORM_obj_t);

picovector_point_type o_x = mp_picovector_get_point_type(x_in);
picovector_point_type o_y = mp_picovector_get_point_type(y_in);

pp_mat3_scale(&transform->transform, o_x, o_y);

return mp_const_none;
}

mp_obj_t TRANSFORM_reset(mp_obj_t self_in) {
_TRANSFORM_obj_t *transform = MP_OBJ_TO_PTR2(self_in, _TRANSFORM_obj_t);
transform->transform = pp_mat3_identity();
Expand Down Expand Up @@ -583,12 +594,34 @@ mp_obj_t VECTOR_set_font_size(mp_obj_t self_in, mp_obj_t size) {
(void)self;

int font_size = mp_obj_get_int(size);
(void)font_size;
// TODO: Implement when Alright Fonts rewrite is ready
self->vector->set_font_size(font_size);
return mp_const_none;
}

mp_obj_t VECTOR_set_font_word_spacing(mp_obj_t self_in, mp_obj_t spacing) {
_VECTOR_obj_t *self = MP_OBJ_TO_PTR2(self_in, _VECTOR_obj_t);
(void)self;

self->vector->set_font_word_spacing(mp_obj_get_int(spacing));
return mp_const_none;
}

mp_obj_t VECTOR_set_font_letter_spacing(mp_obj_t self_in, mp_obj_t spacing) {
_VECTOR_obj_t *self = MP_OBJ_TO_PTR2(self_in, _VECTOR_obj_t);
(void)self;

self->vector->set_font_letter_spacing(mp_obj_get_int(spacing));
return mp_const_none;
}

mp_obj_t VECTOR_set_font_line_height(mp_obj_t self_in, mp_obj_t spacing) {
_VECTOR_obj_t *self = MP_OBJ_TO_PTR2(self_in, _VECTOR_obj_t);
(void)self;

self->vector->set_font_line_height(mp_obj_get_int(spacing));
return mp_const_none;
}

mp_obj_t VECTOR_set_clip(mp_obj_t self_in, mp_obj_t clip_in) {
_VECTOR_obj_t *self = MP_OBJ_TO_PTR2(self_in, _VECTOR_obj_t);
(void)self;
Expand Down Expand Up @@ -657,6 +690,7 @@ mp_obj_t VECTOR_text(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args)
}

pp_mat3_translate(&tt, (float)x, (float)y);
//pp_mat3_mul(&tt, _pp_transform);

//mp_printf(&mp_plat_print, "self->vector->text()\n");
//__printf_debug_flush();
Expand Down
4 changes: 4 additions & 0 deletions micropython/modules/picovector/picovector.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ extern mp_obj_t POLYGON__del__(mp_obj_t self_in);
extern mp_obj_t TRANSFORM_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args);
extern mp_obj_t TRANSFORM_rotate(mp_obj_t self_in, mp_obj_t angle_in, mp_obj_t origin_in);
extern mp_obj_t TRANSFORM_translate(mp_obj_t self_in, mp_obj_t x_in, mp_obj_t y_in);
extern mp_obj_t TRANSFORM_scale(mp_obj_t self_in, mp_obj_t x_in, mp_obj_t y_in);
extern mp_obj_t TRANSFORM_reset(mp_obj_t self_in);

/* Vector */
Expand All @@ -35,6 +36,9 @@ extern mp_obj_t VECTOR_make_new(const mp_obj_type_t *type, size_t n_args, size_t
extern mp_obj_t VECTOR_text(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
extern mp_obj_t VECTOR_set_font(mp_obj_t self_in, mp_obj_t font, mp_obj_t size);
extern mp_obj_t VECTOR_set_font_size(mp_obj_t self_in, mp_obj_t size);
extern mp_obj_t VECTOR_set_font_word_spacing(mp_obj_t self_in, mp_obj_t size);
extern mp_obj_t VECTOR_set_font_letter_spacing(mp_obj_t self_in, mp_obj_t size);
extern mp_obj_t VECTOR_set_font_line_height(mp_obj_t self_in, mp_obj_t size);
extern mp_obj_t VECTOR_set_antialiasing(mp_obj_t self_in, mp_obj_t aa);
extern mp_obj_t VECTOR_set_transform(mp_obj_t self_in, mp_obj_t transform_in);
extern mp_obj_t VECTOR_set_clip(mp_obj_t self_in, mp_obj_t clip_in);
Expand Down

0 comments on commit 6f2393a

Please sign in to comment.