diff --git a/ChangeLog.txt b/ChangeLog.txt index 658d99ecc..5e16125e5 100755 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -89,3 +89,6 @@ nuttx/include/nuttx/nx. * apps/examples/usbstorage: Added instrumentation to monitor memory usage to check for memory leaks in the USB storage driver. + * apps/examples/nxhello/nxhello_bkgd.c: Fix handling of allocated glyph + memory. + diff --git a/examples/README.txt b/examples/README.txt index ba04e216f..06e84ce45 100644 --- a/examples/README.txt +++ b/examples/README.txt @@ -269,6 +269,8 @@ examples/nx CONFIG_EXAMPLES_NX_BPP. CONFIG_EXAMPLES_NX_TBCOLOR -- The color of the toolbar. Default depends on CONFIG_EXAMPLES_NX_BPP. + CONFIG_EXAMPLES_NX_FONTID - Selects the font (see font ID numbers in + include/nuttx/nx/nxfonts.h) CONFIG_EXAMPLES_NX_FONTCOLOR -- The color of the fonts. Default depends on CONFIG_EXAMPLES_NX_BPP. CONFIG_EXAMPLES_NX_BPP -- Pixels per pixel to use. Valid options @@ -341,6 +343,8 @@ examplex/nxhello driver for use in the test: Default: 0 CONFIG_EXAMPLES_NXHELLO_BGCOLOR -- The color of the background. Default depends on CONFIG_EXAMPLES_NXHELLO_BPP. + CONFIG_EXAMPLES_NXHELLO_FONTID - Selects the font (see font ID numbers in + include/nuttx/nx/nxfonts.h) CONFIG_EXAMPLES_NXHELLO_FONTCOLOR -- The color of the fonts used in the background window. Default depends on CONFIG_EXAMPLES_NXHELLO_BPP. CONFIG_EXAMPLES_NXHELLO_BPP -- Pixels per pixel to use. Valid options @@ -427,10 +431,14 @@ examples/nxtext driver for use in the test: Default: 0 CONFIG_EXAMPLES_NXTEXT_BGCOLOR -- The color of the background. Default depends on CONFIG_EXAMPLES_NXTEXT_BPP. + CONFIG_EXAMPLES_NXTEXT_BGFONTID - Selects the font to use in the + background text (see font ID numbers in include/nuttx/nx/nxfonts.h) CONFIG_EXAMPLES_NXTEXT_BGFONTCOLOR -- The color of the fonts used in the background window. Default depends on CONFIG_EXAMPLES_NXTEXT_BPP. CONFIG_EXAMPLES_NXTEXT_PUCOLOR -- The color of the pop-up window. Default depends on CONFIG_EXAMPLES_NXTEXT_BPP. + CONFIG_EXAMPLES_NXTEXT_PUFONTID - Selects the font to use in the pop-up + windows (see font ID numbers in include/nuttx/nx/nxfonts.h) CONFIG_EXAMPLES_NXTEXT_PUFONTCOLOR -- The color of the fonts used in the background window. Default depends on CONFIG_EXAMPLES_NXTEXT_BPP. CONFIG_EXAMPLES_NXTEXT_BPP -- Pixels per pixel to use. Valid options diff --git a/examples/nx/nx_internal.h b/examples/nx/nx_internal.h index b733a185f..a0fae77a9 100644 --- a/examples/nx/nx_internal.h +++ b/examples/nx/nx_internal.h @@ -45,8 +45,10 @@ #include #include #include + #include #include +#include /**************************************************************************** * Definitions @@ -106,6 +108,10 @@ # endif #endif +#ifndef CONFIG_EXAMPLES_NX_FONTID +# define CONFIG_EXAMPLES_NX_FONTID NXFONT_DEFAULT +#endif + #ifndef CONFIG_EXAMPLES_NX_FONTCOLOR # if CONFIG_EXAMPLES_NX_BPP == 24 || CONFIG_EXAMPLES_NX_BPP == 32 # define CONFIG_EXAMPLES_NX_FONTCOLOR 0x00000000 diff --git a/examples/nx/nx_main.c b/examples/nx/nx_main.c index d0beda92e..db17ea2f2 100644 --- a/examples/nx/nx_main.c +++ b/examples/nx/nx_main.c @@ -647,7 +647,7 @@ int MAIN_NAME(int argc, char *argv[]) /* Get the default font handle */ - g_fonthandle = nxf_getfonthandle(NXFONT_DEFAULT); + g_fonthandle = nxf_getfonthandle(CONFIG_EXAMPLES_NX_FONTID); if (!g_fonthandle) { message(MAIN_NAME_STRING ": Failed to get font handle: %d\n", errno); diff --git a/examples/nxhello/nxhello.h b/examples/nxhello/nxhello.h index 93c6d8fac..169285856 100644 --- a/examples/nxhello/nxhello.h +++ b/examples/nxhello/nxhello.h @@ -46,8 +46,9 @@ #include #include -#include #include +#include +#include /**************************************************************************** * Definitions @@ -77,6 +78,10 @@ # endif #endif +#ifndef CONFIG_EXAMPLES_NXHELLO_FONTID +# define CONFIG_EXAMPLES_NXHELLO_FONTID NXFONT_DEFAULT +#endif + #ifndef CONFIG_EXAMPLES_NXHELLO_FONTCOLOR # if CONFIG_EXAMPLES_NXHELLO_BPP == 24 || CONFIG_EXAMPLES_NXHELLO_BPP == 32 # define CONFIG_EXAMPLES_NXHELLO_FONTCOLOR 0x00000000 diff --git a/examples/nxhello/nxhello_bkgd.c b/examples/nxhello/nxhello_bkgd.c index 43bfc0516..93b7798e1 100644 --- a/examples/nxhello/nxhello_bkgd.c +++ b/examples/nxhello/nxhello_bkgd.c @@ -233,7 +233,7 @@ static void nxhello_center(FAR struct nxgl_point_s *pos, { /* Add the font size */ - width += fbm->metric.width; + width += fbm->metric.width + fbm->metric.xoffset; } else { @@ -349,6 +349,7 @@ void nxhello_hello(NXWINDOW hwnd) FAR struct nxgl_rect_s dest; FAR const void *src[CONFIG_NX_NPLANES]; unsigned int glyphsize; + unsigned int mxstride; int ret; /* Get information about the font we are going to use */ @@ -357,8 +358,9 @@ void nxhello_hello(NXWINDOW hwnd) /* Allocate a bit of memory to hold the largest rendered font */ - glyphsize = (unsigned int)fontset->mxheight * (unsigned int)fontset->mxwidth; - glyph = (FAR uint8_t*)malloc(glyphsize); + mxstride = (fontset->mxwidth * CONFIG_EXAMPLES_NXHELLO_BPP + 7) >> 3; + glyphsize = (unsigned int)fontset->mxheight * mxstride; + glyph = (FAR uint8_t*)malloc(glyphsize); /* NOTE: no check for failure to allocate the memory. In a real application * you would need to handle that event. @@ -388,7 +390,7 @@ void nxhello_hello(NXWINDOW hwnd) fwidth = fbm->metric.width + fbm->metric.xoffset; fheight = fbm->metric.height + fbm->metric.yoffset; - fstride = (fwidth * CONFIG_EXAMPLES_NXHELLO_BPP + 7) / 8; + fstride = (fwidth * CONFIG_EXAMPLES_NXHELLO_BPP + 7) >> 3; /* Initialize the glyph memory to the background color */ @@ -434,4 +436,8 @@ void nxhello_hello(NXWINDOW hwnd) pos.x += fontset->spwidth; } } + + /* Free the allocated glyph */ + + free(glyph); } diff --git a/examples/nxhello/nxhello_main.c b/examples/nxhello/nxhello_main.c index d83b354d7..e71df98d1 100644 --- a/examples/nxhello/nxhello_main.c +++ b/examples/nxhello/nxhello_main.c @@ -232,7 +232,7 @@ int MAIN_NAME(int argc, char *argv[]) /* Get the default font handle */ - g_nxhello.hfont = nxf_getfonthandle(NXFONT_DEFAULT); + g_nxhello.hfont = nxf_getfonthandle(CONFIG_EXAMPLES_NXHELLO_FONTID); if (!g_nxhello.hfont) { message(MAIN_NAME_STRING ": Failed to get font handle: %d\n", errno); diff --git a/examples/nxtext/nxtext_bkgd.c b/examples/nxtext/nxtext_bkgd.c index a87425112..90cc19165 100644 --- a/examples/nxtext/nxtext_bkgd.c +++ b/examples/nxtext/nxtext_bkgd.c @@ -136,7 +136,7 @@ static void nxbg_redrawrect(NXWINDOW hwnd, FAR const struct nxgl_rect_s *rect) for (i = 0; i < g_bgstate.nchars; i++) { - nxtext_fillchar(hwnd, rect, &g_bgstate, &g_bgstate.bm[i]); + nxtext_fillchar(hwnd, rect, &g_bgstate, g_bghfont, &g_bgstate.bm[i]); } } @@ -271,7 +271,7 @@ static inline void nxbg_movedisplay(NXWINDOW hwnd, int bottom, int lineheight) bm = &g_bgstate.bm[i]; if (bm->pos.y <= rect.pt2.y && bm->pos.y + g_bgstate.fheight >= rect.pt1.y) { - nxtext_fillchar(hwnd, &rect, &g_bgstate, bm); + nxtext_fillchar(hwnd, &rect, &g_bgstate, g_bghfont, bm); } } } @@ -402,7 +402,7 @@ FAR struct nxtext_state_s *nxbg_getstate(void) * state structure */ - fontset = nxf_getfontset(g_fonthandle); + fontset = nxf_getfontset(g_bghfont); g_bgstate.fheight = fontset->mxheight; g_bgstate.fwidth = fontset->mxwidth; g_bgstate.spwidth = fontset->spwidth; @@ -462,6 +462,6 @@ void nxbg_write(NXWINDOW hwnd, FAR const uint8_t *buffer, size_t buflen) /* Finally, we can output the character */ - nxtext_putc(hwnd, &g_bgstate, (uint8_t)*buffer++); + nxtext_putc(hwnd, &g_bgstate, g_bghfont, (uint8_t)*buffer++); } } diff --git a/examples/nxtext/nxtext_internal.h b/examples/nxtext/nxtext_internal.h index 98579ffdf..caf8cd9fc 100644 --- a/examples/nxtext/nxtext_internal.h +++ b/examples/nxtext/nxtext_internal.h @@ -45,8 +45,10 @@ #include #include #include + #include #include +#include /**************************************************************************** * Definitions @@ -76,6 +78,10 @@ # endif #endif +#ifndef CONFIG_EXAMPLES_NXTEXT_PUFONTID +# define CONFIG_EXAMPLES_NXTEXT_PUFONTID NXFONT_DEFAULT +#endif + #ifndef CONFIG_EXAMPLES_NXTEXT_PUCOLOR # if CONFIG_EXAMPLES_NXTEXT_BPP == 24 || CONFIG_EXAMPLES_NXTEXT_BPP == 32 # define CONFIG_EXAMPLES_NXTEXT_PUCOLOR 0x00dcdcdc @@ -86,6 +92,10 @@ # endif #endif +#ifndef CONFIG_EXAMPLES_NXTEXT_BGFONTID +# define CONFIG_EXAMPLES_NXTEXT_BGFONTID NXFONT_DEFAULT +#endif + #ifndef CONFIG_EXAMPLES_NXTEXT_BGFONTCOLOR # if CONFIG_EXAMPLES_NXTEXT_BPP == 24 || CONFIG_EXAMPLES_NXTEXT_BPP == 32 # define CONFIG_EXAMPLES_NXTEXT_BGFONTCOLOR 0x00000000 @@ -263,9 +273,10 @@ extern NXHANDLE g_hnx; extern NXHANDLE g_bgwnd; -/* The font handle */ +/* The font handlse */ -extern NXHANDLE g_fonthandle; +extern NXHANDLE g_bghfont; +extern NXHANDLE g_puhfont; /* NX callback vtables */ @@ -311,9 +322,9 @@ extern int nxpu_close(NXWINDOW hwnd); extern void nxtext_home(FAR struct nxtext_state_s *st); extern void nxtext_newline(FAR struct nxtext_state_s *st); extern void nxtext_putc(NXWINDOW hwnd, FAR struct nxtext_state_s *st, - uint8_t ch); + NXHANDLE hfont, uint8_t ch); extern void nxtext_fillchar(NXWINDOW hwnd, FAR const struct nxgl_rect_s *rect, - FAR struct nxtext_state_s *st, + FAR struct nxtext_state_s *st, NXHANDLE hfont, FAR const struct nxtext_bitmap_s *bm); #endif /* __EXAMPLES_NXTEXT_NXTEXT_INTERNAL_H */ diff --git a/examples/nxtext/nxtext_main.c b/examples/nxtext/nxtext_main.c index 2ff941d0c..b7e2a3356 100644 --- a/examples/nxtext/nxtext_main.c +++ b/examples/nxtext/nxtext_main.c @@ -135,9 +135,10 @@ static const char *g_bgmsg[BGMSG_LINES] = NXHANDLE g_hnx = NULL; -/* The font handle */ +/* The font handles */ -NXHANDLE g_fonthandle = NULL; +NXHANDLE g_bghfont = NULL; +NXHANDLE g_puhfont = NULL; /* The screen resolution */ @@ -375,12 +376,20 @@ int MAIN_NAME(int argc, char **argv) goto errout; } - /* Get the default font handle */ + /* Get the configured font handles */ - g_fonthandle = nxf_getfonthandle(NXFONT_DEFAULT); - if (!g_fonthandle) + g_bghfont = nxf_getfonthandle(CONFIG_EXAMPLES_NXTEXT_BGFONTID); + if (!g_bghfont) { - message(MAIN_NAME_STRING ": Failed to get font handle: %d\n", errno); + message(MAIN_NAME_STRING ": Failed to get background font handle: %d\n", errno); + g_exitcode = NXEXIT_FONTOPEN; + goto errout; + } + + g_puhfont = nxf_getfonthandle(CONFIG_EXAMPLES_NXTEXT_PUFONTID); + if (!g_puhfont) + { + message(MAIN_NAME_STRING ": Failed to get pop-up font handle: %d\n", errno); g_exitcode = NXEXIT_FONTOPEN; goto errout; } diff --git a/examples/nxtext/nxtext_popup.c b/examples/nxtext/nxtext_popup.c index c0c66f4ef..f00f6a040 100644 --- a/examples/nxtext/nxtext_popup.c +++ b/examples/nxtext/nxtext_popup.c @@ -196,7 +196,7 @@ static inline void nxpu_fillwindow(NXWINDOW hwnd, nxtext_home(st); for (i = 0; i < st->nchars; i++) { - nxtext_fillchar(hwnd, rect, st, &st->bm[i]); + nxtext_fillchar(hwnd, rect, st, g_puhfont, &st->bm[i]); } #endif } @@ -265,7 +265,7 @@ static inline void nxpu_puts(NXWINDOW hwnd, FAR struct nxtext_state_s *st, nxtext_home(st); while (nch--) { - nxtext_putc(hwnd, st, *ch++); + nxtext_putc(hwnd, st, g_puhfont, *ch++); } } @@ -304,7 +304,7 @@ static inline void nxpu_initstate(void) */ #ifdef CONFIG_NX_KBD - fontset = nxf_getfontset(g_fonthandle); + fontset = nxf_getfontset(g_puhfont); g_pustate.fheight = fontset->mxheight; g_pustate.fwidth = fontset->mxwidth; g_pustate.spwidth = fontset->spwidth; diff --git a/examples/nxtext/nxtext_putc.c b/examples/nxtext/nxtext_putc.c index 5c1749269..dfecfe33f 100644 --- a/examples/nxtext/nxtext_putc.c +++ b/examples/nxtext/nxtext_putc.c @@ -326,13 +326,13 @@ nxtext_renderglyph(FAR struct nxtext_state_s *st, * Name: nxtext_fontsize ****************************************************************************/ -static int nxtext_fontsize(uint8_t ch, FAR struct nxgl_size_s *size) +static int nxtext_fontsize(NXHANDLE hfont, uint8_t ch, FAR struct nxgl_size_s *size) { FAR const struct nx_fontbitmap_s *fbm; /* No, it is not cached... Does the code map to a font? */ - fbm = nxf_getbitmap(g_fonthandle, ch); + fbm = nxf_getbitmap(hfont, ch); if (fbm) { /* Yes.. return the font size */ @@ -350,7 +350,7 @@ static int nxtext_fontsize(uint8_t ch, FAR struct nxgl_size_s *size) ****************************************************************************/ static FAR struct nxtext_glyph_s * -nxtext_getglyph(FAR struct nxtext_state_s *st, uint8_t ch) +nxtext_getglyph(NXHANDLE hfont, FAR struct nxtext_state_s *st, uint8_t ch) { FAR struct nxtext_glyph_s *glyph; FAR const struct nx_fontbitmap_s *fbm; @@ -362,7 +362,7 @@ nxtext_getglyph(FAR struct nxtext_state_s *st, uint8_t ch) { /* No, it is not cached... Does the code map to a font? */ - fbm = nxf_getbitmap(g_fonthandle, ch); + fbm = nxf_getbitmap(hfont, ch); if (fbm) { /* Yes.. render the glyph */ @@ -384,7 +384,7 @@ nxtext_getglyph(FAR struct nxtext_state_s *st, uint8_t ch) ****************************************************************************/ static FAR const struct nxtext_bitmap_s * -nxtext_addchar(FAR struct nxtext_state_s *st, uint8_t ch) +nxtext_addchar(NXHANDLE hfont, FAR struct nxtext_state_s *st, uint8_t ch) { FAR struct nxtext_bitmap_s *bm = NULL; FAR struct nxtext_glyph_s *glyph; @@ -403,7 +403,7 @@ nxtext_addchar(FAR struct nxtext_state_s *st, uint8_t ch) /* Find (or create) the matching glyph */ - glyph = nxtext_getglyph(st, ch); + glyph = nxtext_getglyph(hfont, st, ch); if (!glyph) { /* No, there is no font for this code. Just mark this as a space. */ @@ -479,7 +479,7 @@ void nxtext_newline(FAR struct nxtext_state_s *st) * ****************************************************************************/ -void nxtext_putc(NXWINDOW hwnd, FAR struct nxtext_state_s *st, uint8_t ch) +void nxtext_putc(NXWINDOW hwnd, FAR struct nxtext_state_s *st, NXHANDLE hfont, uint8_t ch) { FAR const struct nxtext_bitmap_s *bm; @@ -498,10 +498,10 @@ void nxtext_putc(NXWINDOW hwnd, FAR struct nxtext_state_s *st, uint8_t ch) else { - bm = nxtext_addchar(st, ch); + bm = nxtext_addchar(hfont, st, ch); if (bm) { - nxtext_fillchar(hwnd, NULL, st, bm); + nxtext_fillchar(hwnd, NULL, st, hfont, bm); } } } @@ -517,7 +517,7 @@ void nxtext_putc(NXWINDOW hwnd, FAR struct nxtext_state_s *st, uint8_t ch) void nxtext_fillchar(NXWINDOW hwnd, FAR const struct nxgl_rect_s *rect, FAR struct nxtext_state_s *st, - FAR const struct nxtext_bitmap_s *bm) + NXHANDLE hfont, FAR const struct nxtext_bitmap_s *bm) { FAR struct nxtext_glyph_s *glyph; struct nxgl_rect_s bounds; @@ -534,7 +534,7 @@ void nxtext_fillchar(NXWINDOW hwnd, FAR const struct nxgl_rect_s *rect, /* Get the size of the font glyph (which may not have been created yet) */ - ret = nxtext_fontsize(bm->code, &fsize); + ret = nxtext_fontsize(hfont, bm->code, &fsize); if (ret < 0) { /* This would mean that there is no bitmap for the character code and @@ -576,7 +576,7 @@ void nxtext_fillchar(NXWINDOW hwnd, FAR const struct nxgl_rect_s *rect, /* Find (or create) the glyph that goes with this font */ - glyph = nxtext_getglyph(st, bm->code); + glyph = nxtext_getglyph(hfont, st, bm->code); if (!glyph) { /* Shouldn't happen */