diff options
author | inmarket <andrewh@inmarket.com.au> | 2013-07-28 17:08:45 +1000 |
---|---|---|
committer | inmarket <andrewh@inmarket.com.au> | 2013-07-28 17:08:45 +1000 |
commit | 3977ee687ffff23e49dcac0ea9a7c3e8652248f0 (patch) | |
tree | c5be0359998987d29b6be213413c896fe4d6b07f /include/gdisp | |
parent | f84bc2a3f6b82b0f05319fd7c609f8b30929d788 (diff) | |
download | uGFX-3977ee687ffff23e49dcac0ea9a7c3e8652248f0.tar.gz uGFX-3977ee687ffff23e49dcac0ea9a7c3e8652248f0.tar.bz2 uGFX-3977ee687ffff23e49dcac0ea9a7c3e8652248f0.zip |
First cut - beautiful new font handling by PetteriAimonen
Diffstat (limited to 'include/gdisp')
-rw-r--r-- | include/gdisp/fonts.h | 67 | ||||
-rw-r--r-- | include/gdisp/gdisp.h | 35 | ||||
-rw-r--r-- | include/gdisp/lld/emulation.c | 258 | ||||
-rw-r--r-- | include/gdisp/lld/gdisp_lld.h | 4 | ||||
-rw-r--r-- | include/gdisp/lld/gdisp_lld_msgs.h | 4 | ||||
-rw-r--r-- | include/gdisp/options.h | 55 |
6 files changed, 169 insertions, 254 deletions
diff --git a/include/gdisp/fonts.h b/include/gdisp/fonts.h deleted file mode 100644 index 76bd0926..00000000 --- a/include/gdisp/fonts.h +++ /dev/null @@ -1,67 +0,0 @@ -/* - * This file is subject to the terms of the GFX License. If a copy of - * the license was not distributed with this file, you can obtain one at: - * - * http://ugfx.org/license.html - */ - -/** - * @file include/gdisp/fonts.h - * @brief GDISP internal font definitions. - * @details This is not generally needed by an application. It is used - * by the low level drivers that need to understand a font. - * - * @addtogroup GDISP - * @{ - */ - -#ifndef _GDISP_FONTS_H -#define _GDISP_FONTS_H - -/* Don't test against GFX_USE_GDISP as we may want to use this in other non-GDISP utilities. */ - -/** - * @brief The type of a font column. - * @note Set by defining @p GDISP_MAX_FNT_HEIGHT appropriately. - */ -#if GDISP_MAX_FONT_HEIGHT == 16 - typedef uint16_t fontcolumn_t; -#elif GDISP_MAX_FONT_HEIGHT == 32 - typedef uint32_t fontcolumn_t; -#else - #error "GDISP: GDISP_MAX_FONT_HEIGHT must be either 16 or 32" -#endif - -/** - * @brief Internal font structure. - * @note This structure is followed by: - * 1. An array of character widths (uint8_t) - * 2. An array of column data offsets (relative to the font structure) - * 3. Each characters array of column data (fontcolumn_t) - * Each sub-structure must be padded to a multiple of 8 bytes - * to allow the tables to work across many different compilers. - */ -struct font { - const char * name; - uint8_t height; - uint8_t charPadding; - uint8_t lineSpacing; - uint8_t descenderHeight; - uint8_t minWidth; - uint8_t maxWidth; - char minChar; - char maxChar; - uint8_t xscale; - uint8_t yscale; - const uint8_t *widthTable; - const uint16_t *offsetTable; - const fontcolumn_t *dataTable; -}; - -#define _getCharWidth(f,c) (((c) < (f)->minChar || (c) > (f)->maxChar) ? 0 : (f)->widthTable[(c) - (f)->minChar]) -#define _getCharOffset(f,c) ((f)->offsetTable[(c) - (f)->minChar]) -#define _getCharData(f,c) (&(f)->dataTable[_getCharOffset(f, c)]) - -#endif /* _GDISP_FONTS_H */ -/** @} */ - diff --git a/include/gdisp/gdisp.h b/include/gdisp/gdisp.h index ac741d3b..25194c4f 100644 --- a/include/gdisp/gdisp.h +++ b/include/gdisp/gdisp.h @@ -55,7 +55,11 @@ typedef struct point_t { /** * @brief Type for the text justification. */ -typedef enum justify {justifyLeft, justifyCenter, justifyRight} justify_t; +typedef enum justify { + justifyLeft = 0, + justifyCenter = 1, + justifyRight = 2 +} justify_t; /** * @brief Type for the font metric. */ @@ -63,7 +67,7 @@ typedef enum fontmetric {fontHeight, fontDescendersHeight, fontLineSpacing, font /** * @brief The type of a font. */ -typedef const struct font *font_t; +typedef const struct mf_font_s* font_t; /** * @brief Type for the screen orientation. */ @@ -489,7 +493,7 @@ extern "C" { * * @api */ - void gdispDrawChar(coord_t x, coord_t y, char c, font_t font, color_t color); + void gdispDrawChar(coord_t x, coord_t y, uint16_t c, font_t font, color_t color); /** * @brief Draw a text character with a filled background. @@ -502,7 +506,7 @@ extern "C" { * * @api */ - void gdispFillChar(coord_t x, coord_t y, char c, font_t font, color_t color, color_t bgcolor); + void gdispFillChar(coord_t x, coord_t y, uint16_t c, font_t font, color_t color, color_t bgcolor); #endif /* Read a pixel Function */ @@ -762,6 +766,17 @@ void gdispDrawBox(coord_t x, coord_t y, coord_t cx, coord_t cy, color_t color); void gdispCloseFont(font_t font); /** + * @brief Make a scaled copy of an existing font. + * @details Allocates memory for new font metadata using gfxAlloc, remember to close font after use! + * @return A new font or NULL if out of memory. + * + * @param[in] font The base font to use. + * @param[in] scale_x The scale factor in horizontal direction. + * @param[in] scale_y The scale factor in vertical direction. + */ + font_t gdispScaleFont(font_t font, uint8_t scale_x, uint8_t scale_y); + + /** * @brief Get the name of the specified font. * @returns The name of the font. * @@ -772,6 +787,18 @@ void gdispDrawBox(coord_t x, coord_t y, coord_t cx, coord_t cy, color_t color); const char *gdispGetFontName(font_t font); #endif +/** + * @brief Blend two colors together according to opacity/alpha. + * @return The blended color. + * + * @param[in] fg Foreground color + * @param[in] bg Background color + * @param[in] alpha Opacity of the foreground color (0-255) + * + * @api + */ +color_t gdispBlendColor(color_t fg, color_t bg, uint8_t alpha); + /* Extra Arc Functions */ #if GDISP_NEED_ARC || defined(__DOXYGEN__) diff --git a/include/gdisp/lld/emulation.c b/include/gdisp/lld/emulation.c index c897a92d..716a3988 100644 --- a/include/gdisp/lld/emulation.c +++ b/include/gdisp/lld/emulation.c @@ -462,180 +462,130 @@ GDISPDriver GDISP; #endif #if GDISP_NEED_TEXT && !GDISP_HARDWARE_TEXT - #include "gdisp/fonts.h" + #include "mcufont.h" #endif #if GDISP_NEED_TEXT && !GDISP_HARDWARE_TEXT - void gdisp_lld_draw_char(coord_t x, coord_t y, char c, font_t font, color_t color) { - const fontcolumn_t *ptr; - fontcolumn_t column; - coord_t width, height, xscale, yscale; - coord_t i, j, xs, ys; - - /* Check we actually have something to print */ - width = _getCharWidth(font, c); - if (!width) return; - - xscale = font->xscale; - yscale = font->yscale; - height = font->height * yscale; - width *= xscale; - - ptr = _getCharData(font, c); - - /* Loop through the data and display. The font data is LSBit first, down the column */ - for(i=0; i < width; i+=xscale) { - /* Get the font bitmap data for the column */ - column = *ptr++; - - /* Draw each pixel */ - for(j=0; j < height; j+=yscale, column >>= 1) { - if (column & 0x01) { - for(xs=0; xs < xscale; xs++) - for(ys=0; ys < yscale; ys++) - gdisp_lld_draw_pixel(x+i+xs, y+j+ys, color); - } + static void gdisp_lld_draw_char_callback(int16_t x, int16_t y, uint8_t count, uint8_t alpha, void *state) { + color_t color = *(color_t*)state; + if (alpha == 255) + { + if (count == 1) + gdisp_lld_draw_pixel(x, y, color); + else + gdisp_lld_fill_area(x, y, count, 1, color); + } +#if GDISP_NEED_ANTIALIAS + else + { + while (count--) + { + color_t oldColor; + + oldColor = gdisp_lld_get_pixel_color(x, y); + oldColor = gdispBlendColor(color, oldColor, alpha); + gdisp_lld_draw_pixel(x, y, oldColor); + + x++; } } +#endif + } + + void gdisp_lld_draw_char(coord_t x, coord_t y, uint16_t c, font_t font, color_t color) { + mf_render_character(font, x, y, c, gdisp_lld_draw_char_callback, &color); } #endif #if GDISP_NEED_TEXT && !GDISP_HARDWARE_TEXTFILLS - void gdisp_lld_fill_char(coord_t x, coord_t y, char c, font_t font, color_t color, color_t bgcolor) { - coord_t width, height; - coord_t xscale, yscale; + typedef struct { + int16_t x0; + int16_t y0; + int16_t x1; + int16_t y1; + int16_t x; + int16_t y; + color_t fg; + color_t bg; + } gdisp_lld_fill_char_state_t; + + static void gdisp_lld_fill_char_callback(int16_t x, int16_t y, uint8_t count, uint8_t alpha, void *state) { + gdisp_lld_fill_char_state_t *s = state; - /* Check we actually have something to print */ - width = _getCharWidth(font, c); - if (!width) return; - - xscale = font->xscale; - yscale = font->yscale; - height = font->height * yscale; - width *= xscale; - - /* Method 1: Use background fill and then draw the text */ - #if GDISP_HARDWARE_TEXT || GDISP_SOFTWARE_TEXTFILLDRAW - - /* Fill the area */ - gdisp_lld_fill_area(x, y, width, height, bgcolor); - - /* Draw the text */ - gdisp_lld_draw_char(x, y, c, font, color); - - /* Method 2: Create a single column bitmap and then blit it */ - #elif GDISP_HARDWARE_BITFILLS && GDISP_SOFTWARE_TEXTBLITCOLUMN + if (y > s->y && s->x != s->x0) { - const fontcolumn_t *ptr; - fontcolumn_t column; - coord_t i, j, xs, ys; - - /* Working buffer for fast non-transparent text rendering [patch by Badger] - This needs to be larger than the largest character we can print. - Assume the max is double sized by one column. - */ - static pixel_t buf[sizeof(fontcolumn_t)*8*2]; - - #if GDISP_NEED_VALIDATION - /* Check our buffer is big enough */ - if ((unsigned)height > sizeof(buf)/sizeof(buf[0])) return; - #endif - - ptr = _getCharData(font, c); - - /* Loop through the data and display. The font data is LSBit first, down the column */ - for(i = 0; i < width; i+=xscale) { - /* Get the font bitmap data for the column */ - column = *ptr++; - - /* Draw each pixel */ - for(j = 0; j < height; j+=yscale, column >>= 1) { - if (column & 0x01) { - for(ys=0; ys < yscale; ys++) - gdispPackPixels(buf, 1, j+ys, 0, color); - } else { - for(ys=0; ys < yscale; ys++) - gdispPackPixels(buf, 1, j+ys, 0, bgcolor); - } - } - - for(xs=0; xs < xscale; xs++) - gdisp_lld_blit_area_ex(x+i+xs, y, 1, height, 0, 0, 1, buf); - } + /* Fill background to the end of current line */ + gdisp_lld_fill_area(s->x, s->y, s->x1 - s->x, 1, s->bg); + s->y++; + s->x = s->x0; } - - /* Method 3: Create a character bitmap and then blit it */ - #elif GDISP_HARDWARE_BITFILLS + + if (y > s->y) + { + /* Fill background for given number of full lines */ + gdisp_lld_fill_area(s->x0, s->y, s->x1 - s->x0, y - s->y, s->bg); + s->y = y; + } + + if (x > s->x) + { + /* Fill background from previous X position */ + gdisp_lld_fill_area(s->x, s->y, x - s->x, 1, s->bg); + s->x = x; + } + + if (!count) + return; + + if (y == s->y) { - const fontcolumn_t *ptr; - fontcolumn_t column; - coord_t i, j, xs, ys; + s->x += count; - /* Working buffer for fast non-transparent text rendering [patch by Badger] - This needs to be larger than the largest character we can print. - Assume the max is double sized. - */ - static pixel_t buf[20*(sizeof(fontcolumn_t)*8)*2]; - - #if GDISP_NEED_VALIDATION - /* Check our buffer is big enough */ - if ((unsigned)(width * height) > sizeof(buf)/sizeof(buf[0])) return; - #endif - - ptr = _getCharData(font, c); - - /* Loop through the data and display. The font data is LSBit first, down the column */ - for(i = 0; i < width; i+=xscale) { - /* Get the font bitmap data for the column */ - column = *ptr++; - - /* Draw each pixel */ - for(j = 0; j < height; j+=yscale, column >>= 1) { - if (column & 0x01) { - for(xs=0; xs < xscale; xs++) - for(ys=0; ys < yscale; ys++) - gdispPackPixels(buf, width, i+xs, j+ys, color); - } else { - for(xs=0; xs < xscale; xs++) - for(ys=0; ys < yscale; ys++) - gdispPackPixels(buf, width, i+xs, j+ys, bgcolor); - } - } + if (s->x == s->x1) + { + s->x = s->x0; + s->y++; } - - /* [Patch by Badger] Write all in one stroke */ - gdisp_lld_blit_area_ex(x, y, width, height, 0, 0, width, buf); } - - /* Method 4: Draw pixel by pixel */ - #else + + /* Draw the foreground */ + if (alpha == 255 || alpha == 0) { - const fontcolumn_t *ptr; - fontcolumn_t column; - coord_t i, j, xs, ys; - - ptr = _getCharData(font, c); - - /* Loop through the data and display. The font data is LSBit first, down the column */ - for(i = 0; i < width; i+=xscale) { - /* Get the font bitmap data for the column */ - column = *ptr++; + if (count == 1) + gdisp_lld_draw_pixel(x, y, alpha ? s->fg : s->bg); + else + gdisp_lld_fill_area(x, y, count, 1, alpha ? s->fg : s->bg); + } +#if GDISP_NEED_ANTIALIAS + else + { + while (count--) + { + color_t color; + color = gdispBlendColor(s->fg, s->bg, alpha); + gdisp_lld_draw_pixel(x, y, color); - /* Draw each pixel */ - for(j = 0; j < height; j+=yscale, column >>= 1) { - if (column & 0x01) { - for(xs=0; xs < xscale; xs++) - for(ys=0; ys < yscale; ys++) - gdisp_lld_draw_pixel(x+i+xs, y+j+ys, color); - } else { - for(xs=0; xs < xscale; xs++) - for(ys=0; ys < yscale; ys++) - gdisp_lld_draw_pixel(x+i+xs, y+j+ys, bgcolor); - } - } + x++; } } - #endif +#endif + } + + void gdisp_lld_fill_char(coord_t x, coord_t y, uint16_t c, font_t font, color_t color, color_t bgcolor) { + gdisp_lld_fill_char_state_t state; + + state.x0 = x; + state.y0 = y; + state.x1 = x + mf_character_width(font, c) + font->baseline_x; + state.y1 = y + font->height; + state.x = x; + state.y = y; + state.fg = color; + state.bg = bgcolor; + + mf_render_character(font, x, y, c, gdisp_lld_fill_char_callback, &state); + + gdisp_lld_fill_char_callback(state.x0, state.y1, 0, 0, &state); } #endif diff --git a/include/gdisp/lld/gdisp_lld.h b/include/gdisp/lld/gdisp_lld.h index afd166ae..9c4b3947 100644 --- a/include/gdisp/lld/gdisp_lld.h +++ b/include/gdisp/lld/gdisp_lld.h @@ -281,8 +281,8 @@ extern "C" { /* Text Rendering Functions */ #if GDISP_NEED_TEXT - extern void gdisp_lld_draw_char(coord_t x, coord_t y, char c, font_t font, color_t color); - extern void gdisp_lld_fill_char(coord_t x, coord_t y, char c, font_t font, color_t color, color_t bgcolor); + extern void gdisp_lld_draw_char(coord_t x, coord_t y, uint16_t c, font_t font, color_t color); + extern void gdisp_lld_fill_char(coord_t x, coord_t y, uint16_t c, font_t font, color_t color, color_t bgcolor); #endif /* Pixel readback */ diff --git a/include/gdisp/lld/gdisp_lld_msgs.h b/include/gdisp/lld/gdisp_lld_msgs.h index f2e0e66e..5f4bed9a 100644 --- a/include/gdisp/lld/gdisp_lld_msgs.h +++ b/include/gdisp/lld/gdisp_lld_msgs.h @@ -155,7 +155,7 @@ typedef union gdisp_lld_msg { gfxQueueItem qi; gdisp_msgaction_t action; // GDISP_LLD_MSG_DRAWCHAR coord_t x, y; - char c; + uint16_t c; font_t font; color_t color; } drawchar; @@ -163,7 +163,7 @@ typedef union gdisp_lld_msg { gfxQueueItem qi; gdisp_msgaction_t action; // GDISP_LLD_MSG_FILLCHAR coord_t x, y; - char c; + uint16_t c; font_t font; color_t color; color_t bgcolor; diff --git a/include/gdisp/options.h b/include/gdisp/options.h index 30587530..918c7aea 100644 --- a/include/gdisp/options.h +++ b/include/gdisp/options.h @@ -182,6 +182,36 @@ #endif /** * @} + * + * @name GDISP Text Rendering Options + * @{ + */ + /** + * @brief Enable UTF-8 support for text rendering. + * @details Defaults to FALSE + */ + #ifndef GDISP_NEED_UTF8 + #define GDISP_NEED_UTF8 FALSE + #endif + + /** + * @brief Enable kerning for font rendering (improves character placement). + * @details Defaults to FALSE + */ + #ifndef GDISP_NEED_TEXT_KERNING + #define GDISP_NEED_TEXT_KERNING FALSE + #endif + + /** + * @brief Enable antialiased font support + * @details Defaults to FALSE + */ + #ifndef GDISP_NEED_ANTIALIAS + #define GDISP_NEED_ANTIALIAS FALSE + #endif + +/** + * @} * * @name GDISP Multi-Threading Options * @{ @@ -220,21 +250,6 @@ * @brief Predefined built in fonts * @note Turning off the ones you are not using can save program size. */ - #ifndef GDISP_INCLUDE_FONT_SMALL - #define GDISP_INCLUDE_FONT_SMALL TRUE - #endif - #ifndef GDISP_INCLUDE_FONT_LARGER - #define GDISP_INCLUDE_FONT_LARGER TRUE - #endif - #ifndef GDISP_INCLUDE_FONT_UI1 - #define GDISP_INCLUDE_FONT_UI1 TRUE - #endif - #ifndef GDISP_INCLUDE_FONT_UI2 - #define GDISP_INCLUDE_FONT_UI2 TRUE - #endif - #ifndef GDISP_INCLUDE_FONT_LARGENUMBERS - #define GDISP_INCLUDE_FONT_LARGENUMBERS TRUE - #endif /** * @} @@ -242,16 +257,6 @@ * @name GDISP Optional Sizing Parameters * @{ */ - /** - * @brief The maximum height of a font. - * @details Either 16 or 32. Defaults to 16 - * @note Setting this to 32 causes the font tables to take - * twice the internal program memory. Don't do it unless - * you realy must define an unscaled font larger than 16 bits high. - */ - #ifndef GDISP_MAX_FONT_HEIGHT - #define GDISP_MAX_FONT_HEIGHT 16 - #endif /** * @} * |