Skip to content

Commit 6f2393a

Browse files
committed
PicoVector: Improve text rendering and control.
1 parent 2d556d1 commit 6f2393a

File tree

6 files changed

+70
-8
lines changed

6 files changed

+70
-8
lines changed

libraries/pico_vector/alright-fonts.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,7 @@ int get_max_line_width(af_face_t *face, const char *text, af_text_metrics_t *tm)
273273
return max_width;
274274
}
275275

276-
277-
void af_render(af_face_t *face, const char *text, af_text_metrics_t *tm) {
276+
void af_render(af_face_t *face, const char *text, size_t tlen, af_text_metrics_t *tm) {
278277
pp_mat3_t *old = pp_transform(NULL);
279278

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

292+
char *done = (char *)text + tlen;
293293
char *end = strchr(text, '\n');
294-
if (!end) end = (char *)text + strlen(text);
294+
if (!end) end = done;
295295

296296
while(true) {
297297
int line_width = get_line_width(face, text, tm);
@@ -327,9 +327,9 @@ void af_render(af_face_t *face, const char *text, af_text_metrics_t *tm) {
327327
}
328328

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

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

343+
void _af_render(af_face_t *face, const char *text, af_text_metrics_t *tm) {
344+
af_render(face, text, strlen(text), tm);
345+
}
346+
343347
pp_rect_t af_measure(af_face_t *face, const char *text, af_text_metrics_t *tm) {
344348
pp_rect_t result;
345349
bool first = true;

libraries/pico_vector/pico_vector.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ namespace pimoroni {
6565

6666
text_metrics.transform = t;
6767

68-
af_render(text_metrics.face, text.data(), &text_metrics);
68+
af_render(text_metrics.face, text.data(), text.size(), &text_metrics);
6969

7070
return caret;
7171
/*

libraries/pico_vector/pico_vector.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,18 @@ namespace pimoroni {
6767
text_metrics.size = font_size;
6868
}
6969

70+
void set_font_word_spacing(unsigned int font_wordspacing) {
71+
text_metrics.word_spacing = font_wordspacing;
72+
}
73+
74+
void set_font_letter_spacing(unsigned int font_letterspacing) {
75+
text_metrics.letter_spacing = font_letterspacing;
76+
}
77+
78+
void set_font_line_height(unsigned int font_line_height) {
79+
text_metrics.line_height = font_line_height;
80+
}
81+
7082
bool set_font(std::string_view font_path, unsigned int font_size) {
7183
if(text_metrics.face) {
7284
af_free(text_metrics.face->glyphs);

micropython/modules/picovector/picovector.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,13 @@ MP_DEFINE_CONST_OBJ_TYPE(
5757

5858
static MP_DEFINE_CONST_FUN_OBJ_3(TRANSFORM_rotate_obj, TRANSFORM_rotate);
5959
static MP_DEFINE_CONST_FUN_OBJ_3(TRANSFORM_translate_obj, TRANSFORM_translate);
60+
static MP_DEFINE_CONST_FUN_OBJ_3(TRANSFORM_scale_obj, TRANSFORM_scale);
6061
static MP_DEFINE_CONST_FUN_OBJ_1(TRANSFORM_reset_obj, TRANSFORM_reset);
6162

6263
static const mp_rom_map_elem_t TRANSFORM_locals_dict_table[] = {
6364
{ MP_ROM_QSTR(MP_QSTR_rotate), MP_ROM_PTR(&TRANSFORM_rotate_obj) },
6465
{ MP_ROM_QSTR(MP_QSTR_translate), MP_ROM_PTR(&TRANSFORM_translate_obj) },
66+
{ MP_ROM_QSTR(MP_QSTR_scale), MP_ROM_PTR(&TRANSFORM_scale_obj) },
6567
{ MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&TRANSFORM_reset_obj) },
6668
};
6769

@@ -80,6 +82,9 @@ MP_DEFINE_CONST_OBJ_TYPE(
8082
static MP_DEFINE_CONST_FUN_OBJ_KW(VECTOR_text_obj, 4, VECTOR_text);
8183
static MP_DEFINE_CONST_FUN_OBJ_3(VECTOR_set_font_obj, VECTOR_set_font);
8284
static MP_DEFINE_CONST_FUN_OBJ_2(VECTOR_set_font_size_obj, VECTOR_set_font_size);
85+
static MP_DEFINE_CONST_FUN_OBJ_2(VECTOR_set_font_word_spacing_obj, VECTOR_set_font_word_spacing);
86+
static MP_DEFINE_CONST_FUN_OBJ_2(VECTOR_set_font_letter_spacing_obj, VECTOR_set_font_letter_spacing);
87+
static MP_DEFINE_CONST_FUN_OBJ_2(VECTOR_set_font_line_height_obj, VECTOR_set_font_line_height);
8388
static MP_DEFINE_CONST_FUN_OBJ_2(VECTOR_set_antialiasing_obj, VECTOR_set_antialiasing);
8489
static MP_DEFINE_CONST_FUN_OBJ_2(VECTOR_set_transform_obj, VECTOR_set_transform);
8590
static MP_DEFINE_CONST_FUN_OBJ_2(VECTOR_set_clip_obj, VECTOR_set_clip);
@@ -89,6 +94,9 @@ static MP_DEFINE_CONST_FUN_OBJ_2(VECTOR_draw_obj, VECTOR_draw);
8994
static const mp_rom_map_elem_t VECTOR_locals_dict_table[] = {
9095
{ MP_ROM_QSTR(MP_QSTR_set_font), MP_ROM_PTR(&VECTOR_set_font_obj) },
9196
{ MP_ROM_QSTR(MP_QSTR_set_font_size), MP_ROM_PTR(&VECTOR_set_font_size_obj) },
97+
{ MP_ROM_QSTR(MP_QSTR_set_font_word_spacing), MP_ROM_PTR(&VECTOR_set_font_word_spacing_obj) },
98+
{ MP_ROM_QSTR(MP_QSTR_set_font_letter_spacing), MP_ROM_PTR(&VECTOR_set_font_letter_spacing_obj) },
99+
{ MP_ROM_QSTR(MP_QSTR_set_font_line_height), MP_ROM_PTR(&VECTOR_set_font_line_height_obj) },
92100
{ MP_ROM_QSTR(MP_QSTR_set_antialiasing), MP_ROM_PTR(&VECTOR_set_antialiasing_obj) },
93101
{ MP_ROM_QSTR(MP_QSTR_set_transform), MP_ROM_PTR(&VECTOR_set_transform_obj) },
94102
{ MP_ROM_QSTR(MP_QSTR_set_clip), MP_ROM_PTR(&VECTOR_set_clip_obj) },

micropython/modules/picovector/picovector.cpp

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,17 @@ mp_obj_t TRANSFORM_translate(mp_obj_t self_in, mp_obj_t x_in, mp_obj_t y_in) {
507507
return mp_const_none;
508508
}
509509

510+
mp_obj_t TRANSFORM_scale(mp_obj_t self_in, mp_obj_t x_in, mp_obj_t y_in) {
511+
_TRANSFORM_obj_t *transform = MP_OBJ_TO_PTR2(self_in, _TRANSFORM_obj_t);
512+
513+
picovector_point_type o_x = mp_picovector_get_point_type(x_in);
514+
picovector_point_type o_y = mp_picovector_get_point_type(y_in);
515+
516+
pp_mat3_scale(&transform->transform, o_x, o_y);
517+
518+
return mp_const_none;
519+
}
520+
510521
mp_obj_t TRANSFORM_reset(mp_obj_t self_in) {
511522
_TRANSFORM_obj_t *transform = MP_OBJ_TO_PTR2(self_in, _TRANSFORM_obj_t);
512523
transform->transform = pp_mat3_identity();
@@ -583,12 +594,34 @@ mp_obj_t VECTOR_set_font_size(mp_obj_t self_in, mp_obj_t size) {
583594
(void)self;
584595

585596
int font_size = mp_obj_get_int(size);
586-
(void)font_size;
587-
// TODO: Implement when Alright Fonts rewrite is ready
588597
self->vector->set_font_size(font_size);
589598
return mp_const_none;
590599
}
591600

601+
mp_obj_t VECTOR_set_font_word_spacing(mp_obj_t self_in, mp_obj_t spacing) {
602+
_VECTOR_obj_t *self = MP_OBJ_TO_PTR2(self_in, _VECTOR_obj_t);
603+
(void)self;
604+
605+
self->vector->set_font_word_spacing(mp_obj_get_int(spacing));
606+
return mp_const_none;
607+
}
608+
609+
mp_obj_t VECTOR_set_font_letter_spacing(mp_obj_t self_in, mp_obj_t spacing) {
610+
_VECTOR_obj_t *self = MP_OBJ_TO_PTR2(self_in, _VECTOR_obj_t);
611+
(void)self;
612+
613+
self->vector->set_font_letter_spacing(mp_obj_get_int(spacing));
614+
return mp_const_none;
615+
}
616+
617+
mp_obj_t VECTOR_set_font_line_height(mp_obj_t self_in, mp_obj_t spacing) {
618+
_VECTOR_obj_t *self = MP_OBJ_TO_PTR2(self_in, _VECTOR_obj_t);
619+
(void)self;
620+
621+
self->vector->set_font_line_height(mp_obj_get_int(spacing));
622+
return mp_const_none;
623+
}
624+
592625
mp_obj_t VECTOR_set_clip(mp_obj_t self_in, mp_obj_t clip_in) {
593626
_VECTOR_obj_t *self = MP_OBJ_TO_PTR2(self_in, _VECTOR_obj_t);
594627
(void)self;
@@ -657,6 +690,7 @@ mp_obj_t VECTOR_text(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args)
657690
}
658691

659692
pp_mat3_translate(&tt, (float)x, (float)y);
693+
//pp_mat3_mul(&tt, _pp_transform);
660694

661695
//mp_printf(&mp_plat_print, "self->vector->text()\n");
662696
//__printf_debug_flush();

micropython/modules/picovector/picovector.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ extern mp_obj_t POLYGON__del__(mp_obj_t self_in);
2626
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);
2727
extern mp_obj_t TRANSFORM_rotate(mp_obj_t self_in, mp_obj_t angle_in, mp_obj_t origin_in);
2828
extern mp_obj_t TRANSFORM_translate(mp_obj_t self_in, mp_obj_t x_in, mp_obj_t y_in);
29+
extern mp_obj_t TRANSFORM_scale(mp_obj_t self_in, mp_obj_t x_in, mp_obj_t y_in);
2930
extern mp_obj_t TRANSFORM_reset(mp_obj_t self_in);
3031

3132
/* Vector */
@@ -35,6 +36,9 @@ extern mp_obj_t VECTOR_make_new(const mp_obj_type_t *type, size_t n_args, size_t
3536
extern mp_obj_t VECTOR_text(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
3637
extern mp_obj_t VECTOR_set_font(mp_obj_t self_in, mp_obj_t font, mp_obj_t size);
3738
extern mp_obj_t VECTOR_set_font_size(mp_obj_t self_in, mp_obj_t size);
39+
extern mp_obj_t VECTOR_set_font_word_spacing(mp_obj_t self_in, mp_obj_t size);
40+
extern mp_obj_t VECTOR_set_font_letter_spacing(mp_obj_t self_in, mp_obj_t size);
41+
extern mp_obj_t VECTOR_set_font_line_height(mp_obj_t self_in, mp_obj_t size);
3842
extern mp_obj_t VECTOR_set_antialiasing(mp_obj_t self_in, mp_obj_t aa);
3943
extern mp_obj_t VECTOR_set_transform(mp_obj_t self_in, mp_obj_t transform_in);
4044
extern mp_obj_t VECTOR_set_clip(mp_obj_t self_in, mp_obj_t clip_in);

0 commit comments

Comments
 (0)