Adding word-wrap support for gdispDrawStringBox() and gdispFillStringBox()

Thanks to Koryagin Dmitry for this contribution
This commit is contained in:
Joel Bodenmann 2015-10-30 15:37:55 +01:00
parent 1c7948b926
commit ee2b82271d
4 changed files with 66 additions and 5 deletions

View File

@ -73,6 +73,7 @@
//#define GDISP_NEED_MULTITHREAD FALSE //#define GDISP_NEED_MULTITHREAD FALSE
//#define GDISP_NEED_STREAMING FALSE //#define GDISP_NEED_STREAMING FALSE
//#define GDISP_NEED_TEXT FALSE //#define GDISP_NEED_TEXT FALSE
// #define GDISP_NEED_TEXT_WORDWRAP FALSE
// #define GDISP_NEED_ANTIALIAS FALSE // #define GDISP_NEED_ANTIALIAS FALSE
// #define GDISP_NEED_UTF8 FALSE // #define GDISP_NEED_UTF8 FALSE
// #define GDISP_NEED_TEXT_KERNING FALSE // #define GDISP_NEED_TEXT_KERNING FALSE

View File

@ -20,6 +20,17 @@
#define GDISP_STARTUP_LOGO_TIMEOUT 0 #define GDISP_STARTUP_LOGO_TIMEOUT 0
#endif #endif
// For internal use only.
#if GDISP_NEED_TEXT_WORDWRAP
typedef struct wrapParameters {
GDisplay* g;
coord_t x;
coord_t y;
font_t font;
justify_t justify;
} wrapParameters_t;
#endif
/*===========================================================================*/ /*===========================================================================*/
/* Driver local variables. */ /* Driver local variables. */
/*===========================================================================*/ /*===========================================================================*/
@ -3156,6 +3167,19 @@ void gdispGDrawBox(GDisplay *g, coord_t x, coord_t y, coord_t cx, coord_t cy, co
#undef GD #undef GD
} }
/* Callback to render string boxes with word wrap. */
#if GDISP_NEED_TEXT_WORDWRAP
static bool mf_line_callback(mf_str line, uint16_t count, void *state) {
wrapParameters_t* wrapParameters = (wrapParameters_t*)state;
mf_render_aligned(wrapParameters->font, wrapParameters->x, wrapParameters->y, wrapParameters->justify, line, count, fillcharglyph, wrapParameters->g);
wrapParameters->y += wrapParameters->font->baseline_y;
return TRUE;
}
#endif
void gdispGDrawChar(GDisplay *g, coord_t x, coord_t y, uint16_t c, font_t font, color_t color) { void gdispGDrawChar(GDisplay *g, coord_t x, coord_t y, uint16_t c, font_t font, color_t color) {
MUTEX_ENTER(g); MUTEX_ENTER(g);
g->t.font = font; g->t.font = font;
@ -3247,7 +3271,19 @@ void gdispGDrawBox(GDisplay *g, coord_t x, coord_t y, coord_t cx, coord_t cy, co
} }
y += (cy+1 - font->height)/2; y += (cy+1 - font->height)/2;
mf_render_aligned(font, x, y, justify, str, 0, drawcharglyph, g); /* Render */
#if GDISP_NEED_TEXT_WORDWRAP
wrapParameters_t wrapParameters;
wrapParameters.x = x;
wrapParameters.y = y;
wrapParameters.font = font;
wrapParameters.justify = justify;
wrapParameters.g = g;
mf_wordwrap(font, cx, str, mf_line_callback, &wrapParameters);
#else
mf_render_aligned(font, x, y, justify, str, 0, fillcharglyph, g);
#endif
autoflush(g); autoflush(g);
MUTEX_EXIT(g); MUTEX_EXIT(g);
@ -3285,7 +3321,18 @@ void gdispGDrawBox(GDisplay *g, coord_t x, coord_t y, coord_t cx, coord_t cy, co
y += (cy+1 - font->height)/2; y += (cy+1 - font->height)/2;
/* Render */ /* Render */
#if GDISP_NEED_TEXT_WORDWRAP
wrapParameters_t wrapParameters;
wrapParameters.x = x;
wrapParameters.y = y;
wrapParameters.font = font;
wrapParameters.justify = justify;
wrapParameters.g = g;
mf_wordwrap(font, cx, str, mf_line_callback, &wrapParameters);
#else
mf_render_aligned(font, x, y, justify, str, 0, fillcharglyph, g); mf_render_aligned(font, x, y, justify, str, 0, fillcharglyph, g);
#endif
} }
autoflush(g); autoflush(g);

View File

@ -386,6 +386,14 @@
* @name GDISP Text Rendering Options * @name GDISP Text Rendering Options
* @{ * @{
*/ */
/**
* @brief Enable advanced word-wrapping.
* @details Only has an effect with @p gdispGDrawStringBox() and @p gdispGFillStringBox()
* @details Defaults to FALSE
*/
#ifndef GDISP_NEED_TEXT_WORDWRAP
#define GDISP_NEED_TEXT_WORDWRAP FALSE
#endif
/** /**
* @brief Enable UTF-8 support for text rendering. * @brief Enable UTF-8 support for text rendering.
* @details Defaults to FALSE * @details Defaults to FALSE

View File

@ -25,16 +25,21 @@
/* Mapping from uGFX settings to mcufont settings */ /* Mapping from uGFX settings to mcufont settings */
#if GDISP_NEED_UTF8 #if GDISP_NEED_UTF8
#define MF_ENCODING MF_ENCODING_UTF8 #define MF_ENCODING MF_ENCODING_UTF8
#else #else
#define MF_ENCODING MF_ENCODING_ASCII #define MF_ENCODING MF_ENCODING_ASCII
#endif
#if GDISP_NEED_TEXT_WORDWRAP
#define MF_USE_ADVANCED_WORDWRAP 1
#else
#define MF_USE_ADVANCED_WORDWRAP 0
#endif #endif
#define MF_USE_KERNING GDISP_NEED_TEXT_KERNING #define MF_USE_KERNING GDISP_NEED_TEXT_KERNING
#define MF_FONT_FILE_NAME "src/gdisp/fonts/fonts.h" #define MF_FONT_FILE_NAME "src/gdisp/fonts/fonts.h"
/* These are not used for now */ /* These are not used for now */
#define MF_USE_ADVANCED_WORDWRAP 0
#define MF_USE_JUSTIFY 0 #define MF_USE_JUSTIFY 0
/******************************************************* /*******************************************************