#include #include #include #include #include #include #ifdef DMALLOC #include #endif /* Single-height routines */ // Space between chars #define SPACE 1 int fr_textwidth(const char *text) { const unsigned char *p = text; int length = 0; while (*p != 0) { length += font_data[*p].width; length += SPACE; p++; } return length; } void fr_render(const char *text, unsigned char *buffer, int maxwidth) { const unsigned char *p1 = text; unsigned char *p2 = buffer, *end_p = buffer + maxwidth; int i; while (*p1 != 0) { for (i = 0; i < font_data[*p1].width; i++) { if (p2 >= end_p) return; *p2 = font_data[*p1].data[i]; p2++; } p2 += SPACE; p1++; } } void fr_render_fixed(const char *text, unsigned char *buffer, int maxwidth) { const unsigned char *p1 = text; unsigned char *p2 = buffer, *end_p = buffer + maxwidth; int i; while (*p1 != 0) { for (i = 0; i < 5; i++) { if (p2 >= end_p) return; *p2 = font_data[*p1].data[i]; p2++; } p2 += SPACE; p1++; } } // One line title with inverted background void fr_title(const char *title, int page, int pagetotal, unsigned char *buffer) { char cbuf[16], tbuf[32]; int tlen, clen; snprintf(cbuf, 16, "%d/%d", page, pagetotal); clen = fr_textwidth(cbuf); snprintf(tbuf, 32, "%s", title); tlen = fr_textwidth(tbuf); /* Trim title if too long */ int len = strlen(title); while (tlen + clen + 7 > LCD_W) { // 7px for margins & space char t2buf[32]; len--; snprintf(t2buf, len, "%s", title); str_rtrim(t2buf); snprintf(tbuf, 32, "%s...", t2buf); tlen = fr_textwidth(tbuf); } fr_render(tbuf, buffer + 2, tlen); fr_render(cbuf, buffer + LCD_W - clen - 2, clen); int i; for (i = 0; i < LCD_W; i++) { buffer[i] ^= 0xFF; } } // Aligned text void fr_ralign(char *text, unsigned char *buf, int width) { int textw = fr_textwidth(text); fr_render(text, buf + width - textw, textw); } void fr_malign(char *text, unsigned char *buf, int width) { int textw = fr_textwidth(text); fr_render(text, buf + width / 2 - textw / 2, textw); } /* Double-height routines */ #define DHSPACE 2 struct dhchar *dh_getchar(char c) { int i; for (i = 0; i < DH_COUNT; i++) { if (dh_data[i].character == c) return &dh_data[i]; } return &dh_data[0]; } int dh_textwidth(const char *text) { const unsigned char *p = text; struct dhchar *c; int length = 0; while (*p != 0) { c = dh_getchar(*p); length += c->width; length += DHSPACE; p++; } return length; } void dh_render(const char *text, unsigned char *buffer, int maxwidth) { const unsigned char *p1 = text; unsigned char *p2 = buffer, *end_p = buffer + maxwidth; struct dhchar *c; int i; while (*p1 != 0) { c = dh_getchar(*p1); for (i = 0; i < c->width; i++) { if (p2 >= end_p) return; *p2 = c->line1[i]; *(p2 + LCD_W) = c->line2[i]; p2++; } p2 += DHSPACE; p1++; } } void dh_malign(char *text, unsigned char *buf, int width) { int textw = dh_textwidth(text); dh_render(text, buf + width / 2 - textw / 2, textw); }