Skip to content

Commit

Permalink
getting closer on fonts. may split to separate branch
Browse files Browse the repository at this point in the history
  • Loading branch information
bwhitman committed May 5, 2024
1 parent eedd98b commit 2c91750
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 37 deletions.
3 changes: 3 additions & 0 deletions tulip/esp32s3/esp32_common.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ list(APPEND MICROPY_SOURCE_PORT
gccollect.c
fatfs_port.c
machine_bitstream.c
machine_sdcard.c
machine_timer.c
machine_pin.c
machine_touchpad.c
Expand Down Expand Up @@ -140,6 +141,7 @@ list(APPEND MICROPY_SOURCE_EXTMOD
${TULIP_SHARED_DIR}/sounds.c
${TULIP_SHARED_DIR}/sequencer.c
${TULIP_SHARED_DIR}/lodepng.c
${TULIP_SHARED_DIR}/lvgl_u8g2.c
${TULIP_SHARED_DIR}/u8fontdata.c
${TULIP_SHARED_DIR}/u8g2_fonts.c
${TULIP_SHARED_DIR}/memorypcm.c
Expand Down Expand Up @@ -267,6 +269,7 @@ target_compile_definitions(${MICROPY_TARGET} PUBLIC
target_compile_options(${MICROPY_TARGET} PUBLIC
-Wno-clobbered
-Wno-uninitialized
-Wno-dangling-pointer
-Wno-deprecated-declarations
-Wno-missing-field-initializers
-Wno-unused-const-variable
Expand Down
6 changes: 5 additions & 1 deletion tulip/esp32s3/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,11 @@
#define MICROPY_PY_NETWORK_WLAN (1)
#endif
#ifndef MICROPY_HW_ENABLE_SDCARD
#define MICROPY_HW_ENABLE_SDCARD (0) // TODO
#ifdef TDECK
#define MICROPY_HW_ENABLE_SDCARD (1)
#else
#define MICROPY_HW_ENABLE_SDCARD (0)
#endif
#endif
#define MICROPY_HW_SOFTSPI_MIN_DELAY (0)
#define MICROPY_HW_SOFTSPI_MAX_BAUDRATE (esp_rom_get_cpu_ticks_per_us() * 1000000 / 200) // roughly
Expand Down
30 changes: 20 additions & 10 deletions tulip/shared/lvgl_u8g2.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,24 @@ lv_font_t lv_u8g2_font_helvB14_tr;
bool my_get_glyph_dsc_cb(const lv_font_t * font, lv_font_glyph_dsc_t * dsc_out, uint32_t unicode_letter, uint32_t unicode_letter_next)
{
uint32_t font_no = *((uint32_t*) font->user_data);
dsc_out->adv_w = u8g2_glyph_width(font_no, unicode_letter); /*Horizontal space required by the glyph in [px]*/
dsc_out->box_h = 8; /*Height of the bitmap in [px]*/
dsc_out->box_w = 6; /*Width of the bitmap in [px]*/
u8g2_font_t ufont;
ufont.font = NULL;
ufont.font_decode.fg_color = 1;
ufont.font_decode.is_transparent = 1;
ufont.font_decode.dir = 0;
u8g2_SetFont(&ufont, tulip_fonts[font_no]);
uint8_t databuf[500];
u8g2_DrawGlyph_target(&ufont, unicode_letter, databuf);
u8g2_font_decode_t font_decode = u8g2_GetGlyphInfo(&ufont, unicode_letter);
fprintf(stderr, "%c width is %d height %d x %d y %d\n", unicode_letter, font_decode.glyph_width,font_decode.glyph_height, font_decode.x, font_decode.y );
dsc_out->adv_w = font_decode.glyph_width; /*Horizontal space required by the glyph in [px]*/
if(font_decode.glyph_width==0) {
dsc_out->adv_w = font_decode.x; /*Horizontal space required by the glyph in [px]*/
}
dsc_out->box_h = font_decode.glyph_height; /*Height of the bitmap in [px]*/
dsc_out->box_w = font_decode.glyph_width; /*Width of the bitmap in [px]*/
dsc_out->ofs_x = 0; /*X offset of the bitmap in [pf]*/
dsc_out->ofs_y = 3; /*Y offset of the bitmap measured from the as line*/
dsc_out->ofs_y = font_decode.glyph_height-font_decode.y; /*Y offset of the bitmap measured from the as line*/
dsc_out->format= LV_FONT_GLYPH_FORMAT_A1;
dsc_out->gid.index = unicode_letter;
return true; /*true: glyph found; false: glyph was not found*/
Expand All @@ -28,33 +41,30 @@ bool my_get_glyph_dsc_cb(const lv_font_t * font, lv_font_glyph_dsc_t * dsc_out,
//uint8_t bitmap[8] = {123,149,126,29,49,151, 123, 23};
/* Get the bitmap of `unicode_letter` from `font`. */

int16_t u8g2_DrawGlyph_target(u8g2_font_t *u8g2, uint16_t encoding, uint8_t * target);

const void * my_get_glyph_bitmap_cb(lv_font_glyph_dsc_t * g_dsc, lv_draw_buf_t * draw_buf)
{
const lv_font_t * font_p = g_dsc->resolved_font;
uint32_t font_no = *((uint32_t*) font_p->user_data);

u8g2_font_t ufont;
ufont.font = NULL;
ufont.font_decode.fg_color = 1;
ufont.font_decode.is_transparent = 1;
ufont.font_decode.dir = 0;
uint32_t gid = g_dsc->gid.index;
u8g2_SetFont(&ufont, tulip_fonts[font_no]);

for(uint16_t i=0;i<g_dsc->box_w*g_dsc->box_h;i++) { draw_buf->data[i] = 0; }
u8g2_DrawGlyph_target(&ufont, gid, draw_buf->data);

// put it in draw_buf
return draw_buf;
}

uint32_t font_no= 5;
void setup_lvgl_u8g2() {
uint32_t font_no = 5;
lv_u8g2_font_helvB14_tr.get_glyph_dsc = my_get_glyph_dsc_cb; /*Set a callback to get info about glyphs*/
lv_u8g2_font_helvB14_tr.get_glyph_bitmap = my_get_glyph_bitmap_cb; /*Set a callback to get bitmap of a glyph*/
lv_u8g2_font_helvB14_tr.line_height = 14; /*The real line height where any text fits*/
lv_u8g2_font_helvB14_tr.base_line = 10; ///ascent_A // base_line; /*Base line measured from the top of line_height*/
lv_u8g2_font_helvB14_tr.base_line = 0; ///ascent_A // base_line; /*Base line measured from the top of line_height*/
lv_u8g2_font_helvB14_tr.user_data = &font_no; /*Optionally some extra user data*/
}

89 changes: 63 additions & 26 deletions tulip/shared/u8g2_fonts.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,8 @@ uint8_t u8g2_a_height(uint8_t font_no) {
ufont.font_decode.fg_color = 1;
ufont.font_decode.is_transparent = 1;
ufont.font_decode.dir = 0;
//fprintf(stderr, "getting height for %d\n", font_no);
u8g2_SetFont(&ufont, tulip_fonts[font_no]);
uint8_t height = u8g2_GetFontCapitalAHeight(&ufont);
//fprintf(stderr, "height for %d is %d\n", font_no, height);
return height;
}

Expand All @@ -176,10 +174,20 @@ uint8_t u8g2_glyph_width(uint8_t font_no, uint16_t glyph) {
//fprintf(stderr, "getting width for %d %c\n", font_no, glyph);
u8g2_SetFont(&ufont, tulip_fonts[font_no]);
uint8_t width = u8g2_GetGlyphWidth(&ufont, glyph);
//fprintf(stderr, "width for %d %c is %d\n", font_no, glyph, width);
return width;
}

uint8_t u8g2_glyph_height(uint8_t font_no, uint16_t glyph) {
u8g2_font_t ufont;
ufont.font = NULL;
ufont.font_decode.fg_color = 1;
ufont.font_decode.is_transparent = 1;
ufont.font_decode.dir = 0;
u8g2_SetFont(&ufont, tulip_fonts[font_no]);
uint8_t height = u8g2_GetGlyphHeight(&ufont, glyph);
return height;
}


static uint8_t u8g2_font_decode_get_unsigned_bits(u8g2_font_decode_t *f, uint8_t cnt) U8X8_NOINLINE;
static uint8_t u8g2_font_decode_get_unsigned_bits(u8g2_font_decode_t *f, uint8_t cnt)
Expand Down Expand Up @@ -285,7 +293,7 @@ static int16_t u8g2_add_vector_x(int16_t dx, int8_t x, int8_t y, uint8_t dir)



void drawLine_target(short x0, short y0,short x1, short y1, uint8_t *target, uint16_t target_width) {
void drawLine_target(short x0, short y0,short x1, short y1, uint8_t *target, uint16_t target_width, uint16_t target_height) {
short steep = abs(y1 - y0) > abs(x1 - x0);
if (steep) {
swap(x0, y0);
Expand All @@ -312,9 +320,9 @@ void drawLine_target(short x0, short y0,short x1, short y1, uint8_t *target, uin

for (; x0<=x1; x0++) {
if (steep) {
target[y0 + (x0*target_width)] = 0xff;
target[y0 + ((target_height+x0)*target_width)] = 0xff;
} else {
target[x0 + (y0*target_width)] = 0xff;
target[x0 + ((target_height+y0)*target_width)] = 0xff;
}
err -= dy;
if (err < 0) {
Expand All @@ -324,11 +332,11 @@ void drawLine_target(short x0, short y0,short x1, short y1, uint8_t *target, uin
}
}

void drawFastHLine_target(int16_t x, int16_t y, int16_t w, uint8_t*target, uint16_t target_width) {
drawLine_target(x, y, x + w - 1, y, target, target_width);
void drawFastHLine_target(int16_t x, int16_t y, int16_t w, uint8_t*target, uint16_t target_width, uint16_t target_height) {
drawLine_target(x, y, x + w - 1, y, target, target_width, target_height);
}
void drawFastVLine_target(short x0, short y0, short h, uint8_t*target, uint16_t target_width) {
drawLine_target(x0, y0, x0, y0+h-1, target, target_width);
void drawFastVLine_target(short x0, short y0, short h, uint8_t*target, uint16_t target_width, uint16_t target_height) {
drawLine_target(x0, y0, x0, y0+h-1, target, target_width, target_height);
}


Expand All @@ -353,22 +361,22 @@ void u8g2_draw_hv_line(u8g2_font_t *u8g2, int16_t x, int16_t y, int16_t len, uin

}

void u8g2_draw_hv_line_target(u8g2_font_t *u8g2, int16_t x, int16_t y, int16_t len, uint8_t dir, uint8_t * target,uint16_t target_width) U8X8_NOINLINE;
void u8g2_draw_hv_line_target(u8g2_font_t *u8g2, int16_t x, int16_t y, int16_t len, uint8_t dir, uint8_t * target,uint16_t target_width)
void u8g2_draw_hv_line_target(u8g2_font_t *u8g2, int16_t x, int16_t y, int16_t len, uint8_t dir, uint8_t * target,uint16_t target_width, uint16_t target_height) U8X8_NOINLINE;
void u8g2_draw_hv_line_target(u8g2_font_t *u8g2, int16_t x, int16_t y, int16_t len, uint8_t dir, uint8_t * target,uint16_t target_width, uint16_t target_height)
{
switch(dir)
{
case 0:
drawFastHLine_target(x,y,len,target,target_width);
drawFastHLine_target(x,y,len,target,target_width, target_height);
break;
case 1:
drawFastHLine_target(x,y,len,target, target_width);
drawFastHLine_target(x,y,len,target, target_width, target_height);
break;
case 2:
drawFastHLine_target(x-len+1,y,len,target, target_width);
drawFastHLine_target(x-len+1,y,len,target, target_width, target_height);
break;
case 3:
drawFastHLine_target(x,y-len+1,len,target, target_width);
drawFastHLine_target(x,y-len+1,len,target, target_width, target_height);
break;
}

Expand Down Expand Up @@ -467,7 +475,7 @@ static void u8g2_font_decode_len(u8g2_font_t *u8g2, uint8_t len, uint8_t is_fore



static void u8g2_font_decode_len_target(u8g2_font_t *u8g2, uint8_t len, uint8_t is_foreground, uint8_t * target,uint16_t target_width)
static void u8g2_font_decode_len_target(u8g2_font_t *u8g2, uint8_t len, uint8_t is_foreground, uint8_t * target)
{
uint8_t cnt; /* total number of remaining pixels, which have to be drawn */
uint8_t rem; /* remaining pixel to the right edge of the glyph */
Expand Down Expand Up @@ -515,9 +523,11 @@ static void u8g2_font_decode_len_target(u8g2_font_t *u8g2, uint8_t len, uint8_t
/* draw foreground and background (if required) */
if ( current > 0 ) /* avoid drawing zero length lines, issue #4 */
{
u8g2_draw_hv_line_target(u8g2, x, y, current, decode->dir, target, target_width);
}

if ( is_foreground )
{
u8g2_draw_hv_line_target(u8g2, x, y, current, decode->dir, target, decode->glyph_width, decode->glyph_height);
}
}
/* check, whether the end of the run length code has been reached */
if ( cnt < rem )
break;
Expand Down Expand Up @@ -609,7 +619,7 @@ static int8_t u8g2_font_decode_glyph(u8g2_font_t *u8g2, const uint8_t *glyph_dat
return d;
}

static int8_t u8g2_font_decode_glyph_target(u8g2_font_t *u8g2, const uint8_t *glyph_data, uint8_t * target, uint16_t target_width)
static int8_t u8g2_font_decode_glyph_target(u8g2_font_t *u8g2, const uint8_t *glyph_data, uint8_t * target)
{
uint8_t a, b;
int8_t x, y;
Expand Down Expand Up @@ -643,8 +653,8 @@ static int8_t u8g2_font_decode_glyph_target(u8g2_font_t *u8g2, const uint8_t *gl
b = u8g2_font_decode_get_unsigned_bits(decode, u8g2->font_info.bits_per_1);
do
{
u8g2_font_decode_len_target(u8g2, a, 0, target, target_width);
u8g2_font_decode_len_target(u8g2, b, 1, target, target_width);
u8g2_font_decode_len_target(u8g2, a, 0, target);
u8g2_font_decode_len_target(u8g2, b, 1, target);
} while( u8g2_font_decode_get_unsigned_bits(decode, 1) != 0 );

if ( decode->y >= h )
Expand Down Expand Up @@ -748,12 +758,10 @@ static int16_t u8g2_font_draw_glyph_target(u8g2_font_t *u8g2, uint16_t encoding,
u8g2->font_decode.target_x = 0;
u8g2->font_decode.target_y = 0;

uint8_t width = u8g2_GetGlyphWidth(u8g2, encoding);

const uint8_t *glyph_data = u8g2_font_get_glyph_data(u8g2, encoding);
if ( glyph_data != NULL )
{
dx = u8g2_font_decode_glyph_target(u8g2, glyph_data, target, width);
dx = u8g2_font_decode_glyph_target(u8g2, glyph_data, target);
}
return dx;
}
Expand Down Expand Up @@ -784,6 +792,35 @@ int8_t u8g2_GetGlyphWidth(u8g2_font_t *u8g2, uint16_t requested_encoding)

return u8g2_font_decode_get_signed_bits(&(u8g2->font_decode), u8g2->font_info.bits_per_delta_x);
}
/* side effect: updates u8g2->font_decode and u8g2->glyph_x_offset */
/* actually u8g2_GetGlyphWidth returns the glyph delta x and glyph width itself is set as side effect */
int8_t u8g2_GetGlyphHeight(u8g2_font_t *u8g2, uint16_t requested_encoding)
{
const uint8_t *glyph_data = u8g2_font_get_glyph_data(u8g2, requested_encoding);
if ( glyph_data == NULL )
return 0;

u8g2_font_setup_decode(u8g2, glyph_data);
u8g2->glyph_x_offset = u8g2_font_decode_get_signed_bits(&(u8g2->font_decode), u8g2->font_info.bits_per_char_x);
u8g2_font_decode_get_signed_bits(&(u8g2->font_decode), u8g2->font_info.bits_per_char_y);

/* glyph width is here: u8g2->font_decode.glyph_width */
return u8g2->font_decode.glyph_height;
//return u8g2_font_decode_get_signed_bits(&(u8g2->font_decode), u8g2->font_info.bits_per_delta_x);
}

u8g2_font_decode_t u8g2_GetGlyphInfo(u8g2_font_t *u8g2, uint16_t requested_encoding)
{
const uint8_t *glyph_data = u8g2_font_get_glyph_data(u8g2, requested_encoding);

u8g2_font_setup_decode(u8g2, glyph_data);
u8g2->glyph_x_offset = u8g2_font_decode_get_signed_bits(&(u8g2->font_decode), u8g2->font_info.bits_per_char_x);
u8g2_font_decode_get_signed_bits(&(u8g2->font_decode), u8g2->font_info.bits_per_char_y);
u8g2_font_decode_get_signed_bits(&(u8g2->font_decode), u8g2->font_info.bits_per_delta_x);
return u8g2->font_decode;
}




void u8g2_SetFontMode(u8g2_font_t *u8g2, uint8_t is_transparent)
Expand Down
4 changes: 4 additions & 0 deletions tulip/shared/u8g2_fonts.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ typedef struct _u8g2_font_t u8g2_font_t;

uint8_t u8g2_IsGlyph(u8g2_font_t *u8g2, uint16_t requested_encoding);
int8_t u8g2_GetGlyphWidth(u8g2_font_t *u8g2, uint16_t requested_encoding);
int8_t u8g2_GetGlyphHeight(u8g2_font_t *u8g2, uint16_t requested_encoding);
void u8g2_SetFontMode(u8g2_font_t *u8g2, uint8_t is_transparent);
void u8g2_SetFontDirection(u8g2_font_t *u8g2, uint8_t dir);
int16_t u8g2_DrawGlyph(u8g2_font_t *u8g2, int16_t x, int16_t y, uint16_t encoding);
Expand All @@ -136,6 +137,9 @@ void u8g2_SetForegroundColor(u8g2_font_t *u8g2, uint16_t fg);
void u8g2_SetBackgroundColor(u8g2_font_t *u8g2, uint16_t bg);
uint8_t u8g2_a_height(uint8_t font_no);
uint8_t u8g2_glyph_width(uint8_t font_no, uint16_t glyph);
uint8_t u8g2_glyph_height(uint8_t font_no, uint16_t glyph);
u8g2_font_decode_t u8g2_GetGlyphInfo(u8g2_font_t *u8g2, uint16_t requested_encoding);
int16_t u8g2_DrawGlyph_target(u8g2_font_t *u8g2, uint16_t encoding, uint8_t * target);


/* start font list */
Expand Down

0 comments on commit 2c91750

Please sign in to comment.