2017-01-06 01:36:29 +01:00
|
|
|
/****************************************************************************
|
2018-05-29 21:21:26 +02:00
|
|
|
* libs/libnx/nxfonts/nxfonts_cache.c
|
2017-01-06 01:36:29 +01:00
|
|
|
*
|
2020-04-12 18:46:40 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2017-01-06 01:36:29 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2018-05-18 19:34:55 +02:00
|
|
|
#include <sys/types.h>
|
2017-01-06 01:36:29 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2017-10-08 16:13:47 +02:00
|
|
|
#include <nuttx/semaphore.h>
|
2017-01-06 01:36:29 +01:00
|
|
|
#include <nuttx/nx/nxfonts.h>
|
|
|
|
|
2017-01-06 16:07:25 +01:00
|
|
|
#include "nxcontext.h"
|
|
|
|
|
2017-01-06 01:36:29 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
2017-01-07 17:36:18 +01:00
|
|
|
|
2017-01-06 01:36:29 +01:00
|
|
|
/* This describes a rendering function */
|
|
|
|
|
|
|
|
typedef CODE int (*nxf_renderer_t)(FAR nxgl_mxpixel_t *dest, uint16_t height,
|
|
|
|
uint16_t width, uint16_t stride,
|
|
|
|
FAR const struct nx_fontbitmap_s *bm,
|
|
|
|
nxgl_mxpixel_t color);
|
|
|
|
|
|
|
|
/* This structure defines one font cache */
|
|
|
|
|
|
|
|
struct nxfonts_fcache_s
|
|
|
|
{
|
|
|
|
FAR struct nxfonts_fcache_s *flink; /* Supports a singly linked list */
|
|
|
|
NXHANDLE font; /* Font handle associated with fontid */
|
|
|
|
sem_t fsem; /* Serializes access to the font cache */
|
|
|
|
uint16_t fontid; /* ID of font in this cache */
|
|
|
|
int16_t fclients; /* Number of connected clients */
|
2017-01-06 16:07:25 +01:00
|
|
|
uint8_t maxglyphs; /* Maximum size of glyph[] array */
|
|
|
|
uint8_t nglyphs; /* Current size of glyph[] array */
|
2017-01-06 01:36:29 +01:00
|
|
|
uint8_t bpp; /* Bits per pixel */
|
|
|
|
nxgl_mxpixel_t fgcolor; /* Foreground color */
|
|
|
|
nxgl_mxpixel_t bgcolor; /* Background color */
|
|
|
|
nxf_renderer_t renderer; /* Font renderer */
|
|
|
|
|
|
|
|
/* Glyph cache data storage */
|
|
|
|
|
2017-01-06 16:07:25 +01:00
|
|
|
FAR struct nxfonts_glyph_s *head; /* Head of the list of glyphs */
|
|
|
|
FAR struct nxfonts_glyph_s *tail; /* Tail of the list of glyphs */
|
2017-01-06 01:36:29 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Head of a list of font caches */
|
|
|
|
|
|
|
|
static FAR struct nxfonts_fcache_s *g_fcaches;
|
2017-01-06 16:50:30 +01:00
|
|
|
static sem_t g_cachesem = SEM_INITIALIZER(1);
|
2017-01-06 01:36:29 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-01-06 16:50:30 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxf_list_lock and nxf_list_unlock
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Get/relinquish exclusive access to the font cache list
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void nxf_list_lock(void)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Get exclusive access to the font cache */
|
|
|
|
|
2017-10-08 16:13:47 +02:00
|
|
|
while ((ret = _SEM_WAIT(&g_cachesem)) < 0)
|
2017-01-06 16:50:30 +01:00
|
|
|
{
|
2017-10-08 16:13:47 +02:00
|
|
|
int errorcode = _SEM_ERRNO(ret);
|
2017-01-06 16:50:30 +01:00
|
|
|
DEBUGASSERT(errorcode == EINTR || errorcode == ECANCELED);
|
|
|
|
UNUSED(errorcode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-08 16:13:47 +02:00
|
|
|
#define nxf_list_unlock() (_SEM_POST(&g_cachesem))
|
2017-01-06 16:50:30 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxf_removecache
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Removes the entry 'glyph' from the font cache.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* The caller holds the font cache list semaphore.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline void nxf_removecache(FAR struct nxfonts_fcache_s *fcache,
|
|
|
|
FAR struct nxfonts_fcache_s *prev)
|
|
|
|
{
|
2019-03-08 18:52:49 +01:00
|
|
|
/* Remove the cache from the list. First check for removal from the head */
|
2017-01-06 16:50:30 +01:00
|
|
|
|
|
|
|
if (prev == NULL)
|
|
|
|
{
|
|
|
|
/* Replace the head with the node following glyph */
|
|
|
|
|
|
|
|
g_fcaches = fcache->flink;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No.. Remove from the head or from mid-list */
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
prev->flink = fcache->flink;
|
|
|
|
}
|
|
|
|
|
|
|
|
fcache->flink = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxf_cache_lock and nxf_cache_unlock
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Get/relinquish exclusive access to the font cache
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void nxf_cache_lock(FAR struct nxfonts_fcache_s *priv)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Get exclusive access to the font cache */
|
|
|
|
|
2017-10-08 16:13:47 +02:00
|
|
|
while ((ret = _SEM_WAIT(&priv->fsem)) < 0)
|
2017-01-06 16:50:30 +01:00
|
|
|
{
|
2017-10-08 16:13:47 +02:00
|
|
|
int errorcode = _SEM_ERRNO(ret);
|
2017-01-06 16:50:30 +01:00
|
|
|
DEBUGASSERT(errorcode == EINTR || errorcode == ECANCELED);
|
|
|
|
UNUSED(errorcode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-08 16:13:47 +02:00
|
|
|
#define nxf_cache_unlock(p) (_SEM_POST(&priv->fsem))
|
2017-01-06 16:50:30 +01:00
|
|
|
|
2017-01-06 01:36:29 +01:00
|
|
|
/****************************************************************************
|
2017-01-06 16:07:25 +01:00
|
|
|
* Name: nxf_removeglyph
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Removes the entry 'glyph' from the font cache.
|
|
|
|
*
|
2017-01-06 01:36:29 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2017-01-06 16:07:25 +01:00
|
|
|
static inline void nxf_removeglyph(FAR struct nxfonts_fcache_s *priv,
|
|
|
|
FAR struct nxfonts_glyph_s *glyph,
|
|
|
|
FAR struct nxfonts_glyph_s *prev)
|
2017-01-06 01:36:29 +01:00
|
|
|
{
|
2017-01-06 18:36:05 +01:00
|
|
|
ginfo("fcache=%p glyph=%p\n", priv, glyph);
|
|
|
|
|
2017-01-06 19:53:12 +01:00
|
|
|
/* Remove the glyph for the list. First check for removal from the head
|
|
|
|
* (which, I think, never actually happens).
|
|
|
|
*/
|
2017-01-06 16:07:25 +01:00
|
|
|
|
|
|
|
if (prev == NULL)
|
2017-01-06 01:36:29 +01:00
|
|
|
{
|
2017-01-06 16:07:25 +01:00
|
|
|
/* Replace the head with the node following glyph */
|
2017-01-06 01:36:29 +01:00
|
|
|
|
2017-01-06 16:07:25 +01:00
|
|
|
priv->head = glyph->flink;
|
2017-01-06 01:36:29 +01:00
|
|
|
|
2017-01-06 16:07:25 +01:00
|
|
|
/* If there is no node following glyph, then the list is empty */
|
2017-01-06 01:36:29 +01:00
|
|
|
|
2017-01-06 16:07:25 +01:00
|
|
|
if (priv->head == NULL)
|
|
|
|
{
|
|
|
|
priv->tail = NULL;
|
|
|
|
}
|
|
|
|
}
|
2017-01-06 01:36:29 +01:00
|
|
|
|
2017-01-06 16:07:25 +01:00
|
|
|
/* Check for removal from the tail (we know that the list cannot become
|
|
|
|
* empty in either of the next two cases).
|
2017-01-06 01:36:29 +01:00
|
|
|
*/
|
|
|
|
|
2017-01-06 16:07:25 +01:00
|
|
|
else if (glyph->flink == NULL)
|
2017-01-06 01:36:29 +01:00
|
|
|
{
|
2017-01-06 16:07:25 +01:00
|
|
|
priv->tail = prev;
|
|
|
|
prev->flink = NULL;
|
|
|
|
}
|
2017-01-06 01:36:29 +01:00
|
|
|
|
2017-01-06 19:53:12 +01:00
|
|
|
/* No.. Remove from mid-list */
|
2017-01-06 01:36:29 +01:00
|
|
|
|
2017-01-06 16:07:25 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
prev->flink = glyph->flink;
|
2017-01-06 01:36:29 +01:00
|
|
|
}
|
|
|
|
|
2017-01-06 16:07:25 +01:00
|
|
|
glyph->flink = NULL;
|
2017-01-06 01:36:29 +01:00
|
|
|
|
2017-01-06 16:07:25 +01:00
|
|
|
/* Decrement the count of glyphs in the font cache */
|
2017-01-06 01:36:29 +01:00
|
|
|
|
2017-01-06 16:07:25 +01:00
|
|
|
DEBUGASSERT(priv->nglyphs > 0);
|
|
|
|
priv->nglyphs--;
|
|
|
|
}
|
2017-01-06 01:36:29 +01:00
|
|
|
|
2017-01-06 16:07:25 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxf_addglyph
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Add the entry 'glyph' to the head font cache list.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2017-01-06 01:36:29 +01:00
|
|
|
|
2017-01-06 16:07:25 +01:00
|
|
|
static inline void nxf_addglyph(FAR struct nxfonts_fcache_s *priv,
|
|
|
|
FAR struct nxfonts_glyph_s *glyph)
|
|
|
|
{
|
2017-01-06 18:36:05 +01:00
|
|
|
ginfo("fcache=%p glyph=%p\n", priv, glyph);
|
|
|
|
|
2017-01-06 16:07:25 +01:00
|
|
|
/* Add the glyph to the head of the list */
|
2017-01-06 01:36:29 +01:00
|
|
|
|
2017-01-06 16:07:25 +01:00
|
|
|
glyph->flink = priv->head;
|
|
|
|
|
|
|
|
if (priv->head == NULL)
|
|
|
|
{
|
|
|
|
priv->tail = glyph;
|
2017-01-06 01:36:29 +01:00
|
|
|
}
|
|
|
|
|
2017-01-06 18:06:50 +01:00
|
|
|
priv->head = glyph;
|
2017-01-06 16:07:25 +01:00
|
|
|
|
|
|
|
/* Increment the count of glyphs in the font cache. */
|
2017-01-06 01:36:29 +01:00
|
|
|
|
2017-01-06 16:07:25 +01:00
|
|
|
DEBUGASSERT(priv->nglyphs < priv->maxglyphs);
|
|
|
|
priv->nglyphs++;
|
2017-01-06 01:36:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxf_findglyph
|
2017-01-06 16:07:25 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Find the glyph for the specific character 'ch' in the list of pre-
|
|
|
|
* rendered fonts in the font cache.
|
|
|
|
*
|
|
|
|
* This is logically a part of nxf_cache_getglyph(). nxf_cache_getglyph()
|
|
|
|
* will attempt to find the cached glyph before rendering a new one. So
|
|
|
|
* this function has two unexpected side-effects: (1) If the font cache
|
|
|
|
* is full and the font is not found, then the least-recently-used glyph
|
|
|
|
* is deleted to make space for the new glyph that will be allocated.
|
|
|
|
*
|
2017-01-06 18:06:50 +01:00
|
|
|
* (2) If the glyph is found, then it is moved to the head of the list of
|
2017-01-06 16:07:25 +01:00
|
|
|
* glyphs since it is now the most recently used (leaving the least
|
|
|
|
* recently used glyph at the tail of the list).
|
|
|
|
*
|
2017-01-06 16:50:30 +01:00
|
|
|
* Assumptions:
|
|
|
|
* The caller has exclusive access to the font cache.
|
|
|
|
*
|
2017-01-06 01:36:29 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static FAR struct nxfonts_glyph_s *
|
2019-03-08 18:52:49 +01:00
|
|
|
nxf_findglyph(FAR struct nxfonts_fcache_s *priv, uint8_t ch)
|
2017-01-06 01:36:29 +01:00
|
|
|
{
|
2017-01-06 16:07:25 +01:00
|
|
|
FAR struct nxfonts_glyph_s *glyph;
|
|
|
|
FAR struct nxfonts_glyph_s *prev;
|
2017-01-06 01:36:29 +01:00
|
|
|
|
2017-01-06 18:36:05 +01:00
|
|
|
ginfo("fcache=%p ch=%c (%02x)\n",
|
|
|
|
priv, (ch >= 32 && ch < 128) ? ch : '.', ch);
|
|
|
|
|
2017-01-06 16:07:25 +01:00
|
|
|
/* Try to find the glyph in the list of pre-rendered glyphs */
|
2017-01-06 01:36:29 +01:00
|
|
|
|
2020-04-12 19:18:02 +02:00
|
|
|
for (prev = NULL, glyph = priv->head;
|
|
|
|
glyph != NULL;
|
|
|
|
prev = glyph, glyph = glyph->flink)
|
2017-01-06 01:36:29 +01:00
|
|
|
{
|
2017-05-11 21:35:56 +02:00
|
|
|
/* Check if we found the glyph for this character */
|
2017-01-06 16:07:25 +01:00
|
|
|
|
|
|
|
if (glyph->code == ch)
|
2017-01-06 01:36:29 +01:00
|
|
|
{
|
2017-01-06 16:07:25 +01:00
|
|
|
/* This is now the most recently used glyph. Move it to the head
|
2017-01-06 19:53:12 +01:00
|
|
|
* of the list (if it is not already at the head of the list).
|
2017-01-06 16:07:25 +01:00
|
|
|
*/
|
2017-01-06 01:36:29 +01:00
|
|
|
|
2017-01-06 19:53:12 +01:00
|
|
|
if (prev != NULL)
|
|
|
|
{
|
|
|
|
nxf_removeglyph(priv, glyph, prev);
|
|
|
|
nxf_addglyph(priv, glyph);
|
|
|
|
}
|
2017-01-06 01:36:29 +01:00
|
|
|
|
|
|
|
/* And return the glyph that we found */
|
|
|
|
|
|
|
|
return glyph;
|
|
|
|
}
|
2017-01-06 16:07:25 +01:00
|
|
|
|
|
|
|
/* Is this the last glyph in the list? Has the cache reached its
|
2017-01-06 19:53:12 +01:00
|
|
|
* limit for the number of cached fonts?
|
2017-01-06 16:07:25 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (glyph->flink == NULL && priv->nglyphs >= priv->maxglyphs)
|
|
|
|
{
|
|
|
|
/* Yes.. then remove it from the list and free the glyph memory.
|
|
|
|
* We do this because we have all of the information in hand now
|
|
|
|
* and we will surely need to have this space later.
|
|
|
|
*/
|
|
|
|
|
|
|
|
nxf_removeglyph(priv, glyph, prev);
|
2019-03-08 18:52:49 +01:00
|
|
|
lib_free(glyph);
|
2017-01-06 16:07:25 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2017-01-06 01:36:29 +01:00
|
|
|
}
|
2017-01-06 16:07:25 +01:00
|
|
|
|
2017-01-06 01:36:29 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-01-06 16:07:25 +01:00
|
|
|
* Name: nxf_fillglyph
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Fill the glyph memory with the background color
|
|
|
|
*
|
2017-01-06 01:36:29 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2017-01-06 16:07:25 +01:00
|
|
|
static inline void nxf_fillglyph(FAR struct nxfonts_fcache_s *priv,
|
|
|
|
FAR struct nxfonts_glyph_s *glyph)
|
2017-01-06 01:36:29 +01:00
|
|
|
{
|
|
|
|
int row;
|
|
|
|
int col;
|
|
|
|
|
2017-11-18 19:29:34 +01:00
|
|
|
UNUSED(row); /* Not used in all configurations */
|
|
|
|
UNUSED(col);
|
|
|
|
|
2017-01-06 16:07:25 +01:00
|
|
|
/* Initialize the glyph memory to the background color. */
|
2017-01-06 01:36:29 +01:00
|
|
|
|
2017-11-18 17:19:34 +01:00
|
|
|
#if !defined(CONFIG_NXFONTS_DISABLE_1BPP) || !defined(CONFIG_NXFONTS_DISABLE_2BPP) || \
|
|
|
|
!defined(CONFIG_NXFONTS_DISABLE_4BPP) || !defined(CONFIG_NXFONTS_DISABLE_8BPP)
|
2017-01-06 01:36:29 +01:00
|
|
|
|
2017-01-06 16:07:25 +01:00
|
|
|
/* For pixel depths of 1, 2, 4, and 8, build up an 8-bit value containing
|
|
|
|
* multiple background colored pixels.
|
|
|
|
*/
|
2017-01-06 01:36:29 +01:00
|
|
|
|
2017-01-06 16:07:25 +01:00
|
|
|
if (priv->bpp <= 8)
|
2017-01-06 01:36:29 +01:00
|
|
|
{
|
2017-01-06 16:07:25 +01:00
|
|
|
uint8_t pixel = (uint8_t)priv->bgcolor;
|
|
|
|
FAR uint8_t *ptr;
|
2017-01-06 01:36:29 +01:00
|
|
|
|
2017-11-18 17:19:34 +01:00
|
|
|
#ifndef CONFIG_NXFONTS_DISABLE_1BPP
|
2017-01-06 16:07:25 +01:00
|
|
|
/* Pack a 1-bit pixel to 2 pixels */
|
2017-01-06 01:36:29 +01:00
|
|
|
|
2017-01-06 16:07:25 +01:00
|
|
|
if (priv->bpp < 2)
|
|
|
|
{
|
|
|
|
/* Pack 1-bit pixels into a 2-bits */
|
2017-01-06 01:36:29 +01:00
|
|
|
|
2017-01-06 16:07:25 +01:00
|
|
|
pixel &= 0x01;
|
|
|
|
pixel = (pixel) << 1 | pixel;
|
|
|
|
}
|
|
|
|
#endif
|
2017-01-06 01:36:29 +01:00
|
|
|
|
2017-11-18 17:19:34 +01:00
|
|
|
#if !defined(CONFIG_NXFONTS_DISABLE_1BPP) || !defined(CONFIG_NXFONTS_DISABLE_2BPP)
|
2017-01-06 16:07:25 +01:00
|
|
|
/* Pack a 2-bit pixel to a 4-bit nibble */
|
2017-01-06 01:36:29 +01:00
|
|
|
|
2017-01-06 16:07:25 +01:00
|
|
|
if (priv->bpp < 4)
|
|
|
|
{
|
|
|
|
/* Pack 2-bit pixels into a nibble */
|
2017-01-06 01:36:29 +01:00
|
|
|
|
2017-01-06 16:07:25 +01:00
|
|
|
pixel &= 0x03;
|
|
|
|
pixel = (pixel) << 2 | pixel;
|
|
|
|
}
|
|
|
|
#endif
|
2017-01-06 01:36:29 +01:00
|
|
|
|
2017-11-18 17:19:34 +01:00
|
|
|
#if !defined(CONFIG_NXFONTS_DISABLE_1BPP) || !defined(CONFIG_NXFONTS_DISABLE_2BPP) || \
|
|
|
|
!defined(CONFIG_NXFONTS_DISABLE_4BPP)
|
2017-01-06 16:07:25 +01:00
|
|
|
/* Pack the 4-bit nibble into a byte */
|
2017-01-06 01:36:29 +01:00
|
|
|
|
2017-01-06 16:07:25 +01:00
|
|
|
if (priv->bpp < 8)
|
|
|
|
{
|
|
|
|
pixel &= 0x0f;
|
|
|
|
pixel = (pixel) << 4 | pixel;
|
|
|
|
}
|
|
|
|
#endif
|
2017-01-06 01:36:29 +01:00
|
|
|
|
2017-01-06 16:07:25 +01:00
|
|
|
/* Then fill the glyph with the packed background color */
|
2017-01-06 01:36:29 +01:00
|
|
|
|
2017-01-06 16:07:25 +01:00
|
|
|
ptr = (FAR uint8_t *)glyph->bitmap;
|
2017-01-06 01:36:29 +01:00
|
|
|
for (row = 0; row < glyph->height; row++)
|
|
|
|
{
|
|
|
|
for (col = 0; col < glyph->stride; col++)
|
|
|
|
{
|
|
|
|
/* Transfer the packed bytes into the buffer */
|
|
|
|
|
|
|
|
*ptr++ = pixel;
|
|
|
|
}
|
|
|
|
}
|
2017-01-06 16:07:25 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
2017-01-06 01:36:29 +01:00
|
|
|
|
2017-11-18 17:19:34 +01:00
|
|
|
#if !defined(CONFIG_NXFONTS_DISABLE_16BPP)
|
2017-01-06 16:07:25 +01:00
|
|
|
if (priv->bpp == 16)
|
|
|
|
{
|
|
|
|
FAR uint16_t *ptr = (FAR uint16_t *)glyph->bitmap;
|
2017-01-06 01:36:29 +01:00
|
|
|
|
2017-01-06 16:07:25 +01:00
|
|
|
for (row = 0; row < glyph->height; row++)
|
|
|
|
{
|
|
|
|
/* Just copy the color value into the glyph memory */
|
|
|
|
|
|
|
|
for (col = 0; col < glyph->width; col++)
|
|
|
|
{
|
|
|
|
*ptr++ = priv->bgcolor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
|
2017-11-18 17:19:34 +01:00
|
|
|
#ifndef CONFIG_NXFONTS_DISABLE_24BPP
|
2017-01-06 16:07:25 +01:00
|
|
|
if (priv->bpp == 24)
|
|
|
|
{
|
2017-01-06 16:50:30 +01:00
|
|
|
FAR uint32_t *ptr = (FAR uint32_t *)glyph->bitmap;
|
|
|
|
FAR uint32_t pixel[3];
|
|
|
|
|
|
|
|
/* Get two 32-bit values for alternating 32 representations */
|
|
|
|
|
2020-04-12 19:18:02 +02:00
|
|
|
pixel[0] = (uint32_t)priv->bgcolor << 8 |
|
|
|
|
(uint32_t)priv->bgcolor >> 16;
|
|
|
|
pixel[1] = (uint32_t)priv->bgcolor << 16 |
|
|
|
|
(uint32_t)priv->bgcolor >> 8;
|
|
|
|
pixel[2] = (uint32_t)priv->bgcolor << 24 |
|
|
|
|
(uint32_t)priv->bgcolor;
|
2017-01-06 16:50:30 +01:00
|
|
|
|
|
|
|
for (row = 0; row < glyph->height; row++)
|
|
|
|
{
|
2017-01-06 18:06:50 +01:00
|
|
|
/* Copy the color value into the glyph memory */
|
2017-01-06 16:50:30 +01:00
|
|
|
|
2017-01-06 18:06:50 +01:00
|
|
|
col = 0;
|
|
|
|
for (; ; )
|
2017-01-06 16:50:30 +01:00
|
|
|
{
|
|
|
|
*ptr++ = pixel[0];
|
2017-01-06 18:06:50 +01:00
|
|
|
if (++col >= glyph->width)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-01-06 16:50:30 +01:00
|
|
|
*ptr++ = pixel[1];
|
2017-01-06 18:06:50 +01:00
|
|
|
if (++col >= glyph->width)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-01-06 16:50:30 +01:00
|
|
|
*ptr++ = pixel[2];
|
2017-01-06 18:06:50 +01:00
|
|
|
if (++col >= glyph->width)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2017-01-06 16:50:30 +01:00
|
|
|
}
|
|
|
|
}
|
2017-01-06 16:07:25 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
|
2017-11-18 17:19:34 +01:00
|
|
|
#if !defined(CONFIG_NXFONTS_DISABLE_32BPP)
|
2017-01-06 16:07:25 +01:00
|
|
|
if (priv->bpp == 32)
|
|
|
|
{
|
|
|
|
FAR uint32_t *ptr = (FAR uint32_t *)glyph->bitmap;
|
2017-01-06 01:36:29 +01:00
|
|
|
|
|
|
|
for (row = 0; row < glyph->height; row++)
|
|
|
|
{
|
|
|
|
/* Just copy the color value into the glyph memory */
|
|
|
|
|
|
|
|
for (col = 0; col < glyph->width; col++)
|
|
|
|
{
|
|
|
|
*ptr++ = priv->bgcolor;
|
|
|
|
}
|
|
|
|
}
|
2017-01-06 16:07:25 +01:00
|
|
|
}
|
|
|
|
else
|
2017-01-06 01:36:29 +01:00
|
|
|
#endif
|
2017-01-06 16:07:25 +01:00
|
|
|
{
|
2018-08-24 14:21:15 +02:00
|
|
|
DEBUGPANIC();
|
2017-01-06 16:07:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxf_renderglyph
|
2017-01-06 16:50:30 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Allocate memory for a new glyph and render the bitmap font into the
|
|
|
|
* allocated glyph memory.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* The caller holds the font cache semaphore.
|
|
|
|
*
|
2017-01-06 16:07:25 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline FAR struct nxfonts_glyph_s *
|
2019-03-08 18:52:49 +01:00
|
|
|
nxf_renderglyph(FAR struct nxfonts_fcache_s *priv,
|
|
|
|
FAR const struct nx_fontbitmap_s *fbm, uint8_t ch)
|
2017-01-06 16:07:25 +01:00
|
|
|
{
|
|
|
|
FAR struct nxfonts_glyph_s *glyph = NULL;
|
|
|
|
size_t bmsize;
|
|
|
|
unsigned int height;
|
|
|
|
unsigned int width;
|
|
|
|
unsigned int stride;
|
|
|
|
int ret;
|
|
|
|
|
2017-01-06 18:36:05 +01:00
|
|
|
ginfo("fcache=%p fbm=%p ch=%c (%02x)\n",
|
|
|
|
priv, fbm, (ch >= 32 && ch < 128) ? ch : '.', ch);
|
|
|
|
|
2017-01-06 16:07:25 +01:00
|
|
|
/* Get the size of the glyph */
|
|
|
|
|
|
|
|
width = fbm->metric.width + fbm->metric.xoffset;
|
|
|
|
height = fbm->metric.height + fbm->metric.yoffset;
|
|
|
|
|
|
|
|
/* Get the physical width of the glyph in bytes */
|
|
|
|
|
|
|
|
stride = (width * priv->bpp + 7) >> 3;
|
|
|
|
|
|
|
|
/* Allocate the glyph (always succeeds) */
|
|
|
|
|
|
|
|
bmsize = stride * height;
|
2019-03-08 15:43:16 +01:00
|
|
|
glyph = (FAR struct nxfonts_glyph_s *)
|
|
|
|
lib_malloc(SIZEOF_NXFONTS_GLYPH_S(bmsize));
|
2017-01-06 16:07:25 +01:00
|
|
|
|
|
|
|
if (glyph != NULL)
|
|
|
|
{
|
2020-04-12 19:18:02 +02:00
|
|
|
/* Save the character code, dimensions, and physical width of the
|
|
|
|
* glyph.
|
|
|
|
*/
|
2017-01-06 16:07:25 +01:00
|
|
|
|
|
|
|
glyph->code = ch;
|
|
|
|
glyph->width = width;
|
|
|
|
glyph->height = height;
|
|
|
|
glyph->stride = stride;
|
2017-01-06 01:36:29 +01:00
|
|
|
|
2017-01-06 16:07:25 +01:00
|
|
|
/* Initialize the glyph memory to the background color. */
|
|
|
|
|
|
|
|
nxf_fillglyph(priv, glyph);
|
|
|
|
|
|
|
|
/* Then render the glyph into the allocated, initialized memory */
|
2017-01-06 01:36:29 +01:00
|
|
|
|
|
|
|
ret = priv->renderer((FAR nxgl_mxpixel_t *)glyph->bitmap,
|
|
|
|
glyph->height, glyph->width, glyph->stride,
|
|
|
|
fbm, priv->fgcolor);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* Actually, the renderer never returns a failure */
|
|
|
|
|
|
|
|
gerr("ERROR: nxf_renderglyph: Renderer failed\n");
|
2017-01-06 16:07:25 +01:00
|
|
|
lib_free(glyph);
|
|
|
|
return NULL;
|
2017-01-06 01:36:29 +01:00
|
|
|
}
|
2017-01-06 16:07:25 +01:00
|
|
|
|
|
|
|
/* Add the new glyph to the font cache */
|
|
|
|
|
|
|
|
nxf_addglyph(priv, glyph);
|
2017-01-06 01:36:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return glyph;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxf_findcache
|
2017-01-06 16:50:30 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Find a font cache tht matches the font charcteristics.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* The caller holds the font cache list semaphore.
|
|
|
|
*
|
2017-01-06 01:36:29 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static FAR struct nxfonts_fcache_s *
|
2019-03-08 18:52:49 +01:00
|
|
|
nxf_findcache(enum nx_fontid_e fontid, nxgl_mxpixel_t fgcolor,
|
|
|
|
nxgl_mxpixel_t bgcolor, int bpp)
|
2017-01-06 01:36:29 +01:00
|
|
|
{
|
|
|
|
FAR struct nxfonts_fcache_s *fcache;
|
|
|
|
|
2020-11-20 12:56:26 +01:00
|
|
|
ginfo("fontid=%d fgcolor=%u bgcolor=%u bpp=%d\n",
|
2017-01-06 18:36:05 +01:00
|
|
|
fontid, fgcolor, bgcolor, bpp);
|
|
|
|
|
2017-01-06 01:36:29 +01:00
|
|
|
/* Search for a cache for this font characteristics */
|
|
|
|
|
|
|
|
for (fcache = g_fcaches; fcache != NULL; fcache = fcache->flink)
|
|
|
|
{
|
|
|
|
/* Does this font have the same characteristics? */
|
|
|
|
|
|
|
|
if (fcache->fontid == fontid &&
|
|
|
|
fcache->fgcolor == fgcolor &&
|
|
|
|
fcache->bgcolor == bgcolor &&
|
|
|
|
fcache->bpp == bpp)
|
|
|
|
{
|
|
|
|
/* Yes... return it */
|
|
|
|
|
2017-01-06 18:36:05 +01:00
|
|
|
ginfo("Returning fcache=%p\n", fcache);
|
2017-01-06 01:36:29 +01:00
|
|
|
return fcache;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-06 18:36:05 +01:00
|
|
|
ginfo("Not found\n");
|
2017-01-06 01:36:29 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxf_cache_connect
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Create a new font cache for the provided 'fontid'. If the cache
|
|
|
|
* already, then just increment a reference count return the handle for
|
|
|
|
* the existing font cache.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
2017-01-06 16:07:25 +01:00
|
|
|
* fontid - Identifies the font supported by this cache
|
|
|
|
* fgcolor - Foreground color
|
|
|
|
* bgcolor - Background color
|
|
|
|
* bpp - Bits per pixel
|
|
|
|
* maxglyphs - Maximum number of glyphs permitted in the cache
|
2017-01-06 01:36:29 +01:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2017-01-06 01:36:29 +01:00
|
|
|
* On success a non-NULL handle is returned that then may sequently be
|
|
|
|
* used with nxf_getglyph() to extract fonts from the font cache. NULL
|
|
|
|
* returned on any failure with the errno value set to indicate the nature
|
|
|
|
* of the error.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
FCACHE nxf_cache_connect(enum nx_fontid_e fontid,
|
|
|
|
nxgl_mxpixel_t fgcolor, nxgl_mxpixel_t bgcolor,
|
2017-01-06 16:07:25 +01:00
|
|
|
int bpp, int maxglyphs)
|
2017-01-06 01:36:29 +01:00
|
|
|
{
|
|
|
|
FAR struct nxfonts_fcache_s *priv;
|
|
|
|
int errcode;
|
|
|
|
|
2020-11-20 12:56:26 +01:00
|
|
|
ginfo("fontid=%d fgcolor=%u bgcolor=%u bpp=%d maxglyphs=%d\n",
|
2017-01-06 18:36:05 +01:00
|
|
|
fontid, fgcolor, bgcolor, bpp, maxglyphs);
|
|
|
|
|
2017-01-06 16:50:30 +01:00
|
|
|
/* Get exclusive access to the font cache list */
|
|
|
|
|
|
|
|
nxf_list_lock();
|
|
|
|
|
2017-01-06 01:36:29 +01:00
|
|
|
/* Find a font cache with the matching font characteristics */
|
|
|
|
|
|
|
|
priv = nxf_findcache(fontid, fgcolor, bgcolor, bpp);
|
|
|
|
if (priv == NULL)
|
|
|
|
{
|
|
|
|
/* There isn't one... we will have to create a new font cache for this
|
|
|
|
* client.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Allocate memory for the (empty) font cache */
|
|
|
|
|
|
|
|
priv = (FAR struct nxfonts_fcache_s *)
|
2020-04-12 19:18:02 +02:00
|
|
|
lib_zalloc(sizeof(struct nxfonts_fcache_s));
|
2017-01-06 01:36:29 +01:00
|
|
|
|
|
|
|
if (priv == NULL)
|
|
|
|
{
|
|
|
|
errcode = ENOMEM;
|
2017-01-06 16:50:30 +01:00
|
|
|
goto errout_with_lock;
|
2017-01-06 01:36:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the font cache */
|
|
|
|
|
|
|
|
priv->fontid = fontid;
|
2017-01-06 18:56:14 +01:00
|
|
|
priv->fclients = 1;
|
|
|
|
priv->maxglyphs = maxglyphs;
|
|
|
|
priv->bpp = bpp;
|
2017-01-06 01:36:29 +01:00
|
|
|
priv->fgcolor = fgcolor;
|
|
|
|
priv->bgcolor = bgcolor;
|
|
|
|
|
|
|
|
/* Select the rendering function */
|
|
|
|
|
|
|
|
/* Select renderer -- Some additional logic would be required to
|
|
|
|
* support pixel depths that are not directly addressable (1,2,4, and
|
|
|
|
* 24).
|
|
|
|
*/
|
|
|
|
|
2017-01-06 18:06:50 +01:00
|
|
|
switch (bpp)
|
2017-01-06 01:36:29 +01:00
|
|
|
{
|
2017-11-18 17:19:34 +01:00
|
|
|
#ifndef CONFIG_NXFONTS_DISABLE_1BPP
|
2017-01-06 18:06:50 +01:00
|
|
|
case 1:
|
2017-01-06 01:36:29 +01:00
|
|
|
priv->renderer = (nxf_renderer_t)nxf_convert_1bpp;
|
2017-01-06 18:06:50 +01:00
|
|
|
break;
|
2017-01-06 01:36:29 +01:00
|
|
|
#endif
|
2017-11-18 17:19:34 +01:00
|
|
|
#ifndef CONFIG_NXFONTS_DISABLE_2BPP
|
2017-01-06 18:06:50 +01:00
|
|
|
case 2:
|
2017-01-06 01:36:29 +01:00
|
|
|
priv->renderer = (nxf_renderer_t)nxf_convert_2bpp;
|
2017-01-06 18:06:50 +01:00
|
|
|
break;
|
2017-01-06 01:36:29 +01:00
|
|
|
#endif
|
2017-11-18 17:19:34 +01:00
|
|
|
#ifndef CONFIG_NXFONTS_DISABLE_4BPP
|
2017-01-06 18:06:50 +01:00
|
|
|
case 4:
|
2017-01-06 01:36:29 +01:00
|
|
|
priv->renderer = (nxf_renderer_t)nxf_convert_4bpp;
|
2017-01-06 18:06:50 +01:00
|
|
|
break;
|
2017-01-06 01:36:29 +01:00
|
|
|
#endif
|
2017-11-18 17:19:34 +01:00
|
|
|
#ifndef CONFIG_NXFONTS_DISABLE_8BPP
|
2017-01-06 18:06:50 +01:00
|
|
|
case 8:
|
2017-01-06 01:36:29 +01:00
|
|
|
priv->renderer = (nxf_renderer_t)nxf_convert_8bpp;
|
2017-01-06 18:06:50 +01:00
|
|
|
break;
|
2017-01-06 01:36:29 +01:00
|
|
|
#endif
|
2017-11-18 17:19:34 +01:00
|
|
|
#ifndef CONFIG_NXFONTS_DISABLE_16BPP
|
2017-01-06 18:06:50 +01:00
|
|
|
case 16:
|
2017-01-06 01:36:29 +01:00
|
|
|
priv->renderer = (nxf_renderer_t)nxf_convert_16bpp;
|
2017-01-06 18:06:50 +01:00
|
|
|
break;
|
2017-01-06 01:36:29 +01:00
|
|
|
#endif
|
2017-11-18 17:19:34 +01:00
|
|
|
#ifndef CONFIG_NXFONTS_DISABLE_24BPP
|
2017-01-06 18:06:50 +01:00
|
|
|
case 24:
|
2017-01-06 01:36:29 +01:00
|
|
|
priv->renderer = (nxf_renderer_t)nxf_convert_24bpp;
|
2017-01-06 18:06:50 +01:00
|
|
|
break;
|
2017-01-06 01:36:29 +01:00
|
|
|
#endif
|
2017-11-18 17:19:34 +01:00
|
|
|
#ifndef CONFIG_NXFONTS_DISABLE_32BPP
|
2017-01-06 18:06:50 +01:00
|
|
|
case 32:
|
2017-01-06 01:36:29 +01:00
|
|
|
priv->renderer = (nxf_renderer_t)nxf_convert_32bpp;
|
2017-01-06 18:06:50 +01:00
|
|
|
break;
|
2017-01-06 01:36:29 +01:00
|
|
|
#endif
|
2017-01-06 18:06:50 +01:00
|
|
|
default:
|
2017-01-06 01:36:29 +01:00
|
|
|
gerr("ERROR: Unsupported pixel depth: %d\n", bpp);
|
|
|
|
errcode = ENOSYS;
|
|
|
|
goto errout_with_fcache;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Select the font */
|
|
|
|
|
|
|
|
priv->font = nxf_getfonthandle(fontid);
|
|
|
|
if (priv->font == NULL)
|
|
|
|
{
|
|
|
|
errcode = get_errno();
|
|
|
|
gerr("ERROR: Failed to get font ID %d: %d\n", fontid, errcode);
|
|
|
|
goto errout_with_fcache;
|
|
|
|
}
|
2017-01-06 16:50:30 +01:00
|
|
|
|
2017-01-06 18:36:05 +01:00
|
|
|
/* Initialize the mutual exclusion semaphore */
|
|
|
|
|
2020-02-01 08:17:32 +01:00
|
|
|
_SEM_INIT(&priv->fsem, 0, 1);
|
2017-01-06 18:36:05 +01:00
|
|
|
|
2017-01-06 16:50:30 +01:00
|
|
|
/* Add the new font cache to the list of font caches */
|
|
|
|
|
|
|
|
priv->flink = g_fcaches;
|
|
|
|
g_fcaches = priv;
|
2017-01-06 01:36:29 +01:00
|
|
|
}
|
2017-01-06 16:07:25 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* A font cache with these characteristics already exists. Just make
|
|
|
|
* sure that it is as least a big as the size requested.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (priv->maxglyphs < maxglyphs)
|
|
|
|
{
|
|
|
|
priv->maxglyphs = maxglyphs;
|
|
|
|
}
|
2017-01-06 18:56:14 +01:00
|
|
|
|
|
|
|
/* Increment the number of clients of the font cache */
|
|
|
|
|
|
|
|
priv->fclients++;
|
2017-01-06 16:07:25 +01:00
|
|
|
}
|
2017-01-06 01:36:29 +01:00
|
|
|
|
2017-01-06 16:50:30 +01:00
|
|
|
nxf_list_unlock();
|
2017-01-06 18:36:05 +01:00
|
|
|
ginfo("fhandle=%p\n", priv);
|
2017-01-06 01:36:29 +01:00
|
|
|
return (FCACHE)priv;
|
|
|
|
|
|
|
|
errout_with_fcache:
|
2017-01-06 14:06:51 +01:00
|
|
|
lib_free(priv);
|
2019-03-08 18:52:49 +01:00
|
|
|
|
2017-01-06 16:50:30 +01:00
|
|
|
errout_with_lock:
|
|
|
|
nxf_list_unlock();
|
2017-01-06 01:36:29 +01:00
|
|
|
set_errno(errcode);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxf_cache_disconnect
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Decrement the reference count on the font cache and, if the reference
|
|
|
|
* count goes to zero, free all resources used by the font cache. The
|
|
|
|
* font handle is invalid upon return in either case.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
2020-04-12 19:18:02 +02:00
|
|
|
* fhandle - A font cache handle previously returned by
|
|
|
|
* nxf_cache_connect();
|
2017-01-06 01:36:29 +01:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2017-01-06 01:36:29 +01:00
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-01-06 16:50:30 +01:00
|
|
|
void nxf_cache_disconnect(FCACHE fhandle)
|
2017-01-06 01:36:29 +01:00
|
|
|
{
|
2017-01-06 16:50:30 +01:00
|
|
|
FAR struct nxfonts_fcache_s *priv = (FAR struct nxfonts_fcache_s *)fhandle;
|
|
|
|
FAR struct nxfonts_fcache_s *fcache;
|
|
|
|
FAR struct nxfonts_fcache_s *prev;
|
2017-01-06 16:07:25 +01:00
|
|
|
FAR struct nxfonts_glyph_s *glyph;
|
|
|
|
FAR struct nxfonts_glyph_s *next;
|
2017-01-06 01:36:29 +01:00
|
|
|
|
2017-01-06 18:36:05 +01:00
|
|
|
ginfo("fhandle=%p\n", fhandle);
|
|
|
|
|
2017-01-06 01:36:29 +01:00
|
|
|
DEBUGASSERT(priv != NULL && priv->fclients > 0);
|
|
|
|
|
|
|
|
/* Get exclusive access to the font cache */
|
|
|
|
|
2017-01-06 16:50:30 +01:00
|
|
|
nxf_cache_lock(priv);
|
2017-01-06 01:36:29 +01:00
|
|
|
|
|
|
|
/* Is this the last client of the font cache? */
|
|
|
|
|
|
|
|
if (priv->fclients <= 1)
|
|
|
|
{
|
2017-01-06 16:50:30 +01:00
|
|
|
/* Get exclusive access to the font cache list */
|
|
|
|
|
|
|
|
nxf_list_lock();
|
|
|
|
|
|
|
|
/* Remove the font cache from the list of caches. This is a singly
|
|
|
|
* linked list, so we must do this the hard way.
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (prev = NULL, fcache = g_fcaches;
|
|
|
|
fcache != priv && fcache != NULL;
|
2020-04-12 18:46:40 +02:00
|
|
|
fcache = fcache->flink)
|
|
|
|
{
|
|
|
|
}
|
2017-01-06 16:50:30 +01:00
|
|
|
|
2018-08-24 14:58:30 +02:00
|
|
|
DEBUGASSERT(fcache == priv);
|
2017-01-06 16:50:30 +01:00
|
|
|
nxf_removecache(fcache, prev);
|
|
|
|
nxf_list_unlock();
|
2017-01-06 01:36:29 +01:00
|
|
|
|
2017-01-06 16:07:25 +01:00
|
|
|
/* Free all allocated glyph memory */
|
2017-01-06 01:36:29 +01:00
|
|
|
|
2017-01-06 16:07:25 +01:00
|
|
|
for (glyph = priv->head; glyph != NULL; glyph = next)
|
2017-01-06 01:36:29 +01:00
|
|
|
{
|
2017-01-06 16:07:25 +01:00
|
|
|
next = glyph->flink;
|
|
|
|
lib_free(glyph);
|
2017-01-06 01:36:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Destroy the serializing semaphore... while we are holding it? */
|
|
|
|
|
2017-10-08 16:13:47 +02:00
|
|
|
_SEM_DESTROY(&priv->fsem);
|
2017-01-06 01:36:29 +01:00
|
|
|
|
2020-02-22 19:31:14 +01:00
|
|
|
/* Finally, free the font cache structure itself */
|
2017-01-06 01:36:29 +01:00
|
|
|
|
2017-01-06 16:07:25 +01:00
|
|
|
lib_free(priv);
|
2017-01-06 01:36:29 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No.. just decrement the number of clients connected to the font
|
|
|
|
* cache.
|
|
|
|
*/
|
|
|
|
|
|
|
|
priv->fclients--;
|
2017-01-06 16:50:30 +01:00
|
|
|
nxf_cache_unlock(priv);
|
2017-01-06 01:36:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxf_cache_getfonthandle
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Return the handle to the font set used by this instance of the font
|
|
|
|
* cache.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
2020-04-12 19:18:02 +02:00
|
|
|
* fhandle - A font cache handle previously returned by
|
|
|
|
* nxf_cache_connect();
|
2017-01-06 01:36:29 +01:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2017-01-06 01:36:29 +01:00
|
|
|
* Zero (OK) is returned if the metrics were
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* One success, a non-NULL font handle is returned.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-01-06 16:50:30 +01:00
|
|
|
NXHANDLE nxf_cache_getfonthandle(FCACHE fhandle)
|
2017-01-06 01:36:29 +01:00
|
|
|
{
|
2017-01-06 16:50:30 +01:00
|
|
|
FAR struct nxfonts_fcache_s *priv = (FAR struct nxfonts_fcache_s *)fhandle;
|
2017-01-06 01:36:29 +01:00
|
|
|
|
|
|
|
DEBUGASSERT(priv != NULL && priv->font != NULL);
|
|
|
|
return priv->font;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxf_cache_getglyph
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Get the font glyph for the character code 'ch' from the font cache. If
|
|
|
|
* the glyph for that character code does not exist in the font cache, it
|
|
|
|
* be rendered.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* On success, a non-NULL pointer to the rendered glyph in the font cache
|
|
|
|
* is returned. NULL is returned on any failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-04-12 19:18:02 +02:00
|
|
|
FAR const struct nxfonts_glyph_s *
|
|
|
|
nxf_cache_getglyph(FCACHE fhandle, uint8_t ch)
|
2017-01-06 01:36:29 +01:00
|
|
|
{
|
2017-01-06 16:50:30 +01:00
|
|
|
FAR struct nxfonts_fcache_s *priv = (FAR struct nxfonts_fcache_s *)fhandle;
|
2017-01-06 01:36:29 +01:00
|
|
|
FAR struct nxfonts_glyph_s *glyph;
|
|
|
|
FAR const struct nx_fontbitmap_s *fbm;
|
|
|
|
|
2017-01-06 18:36:05 +01:00
|
|
|
ginfo("ch=%c (%02x)\n", (ch >= 32 && ch < 128) ? ch : '.', ch);
|
|
|
|
|
2017-01-06 16:50:30 +01:00
|
|
|
/* Get exclusive access to the font cache */
|
|
|
|
|
|
|
|
nxf_cache_lock(priv);
|
|
|
|
|
2017-01-06 01:36:29 +01:00
|
|
|
/* First, try to find the glyph in the cache of pre-rendered glyphs */
|
|
|
|
|
|
|
|
glyph = nxf_findglyph(priv, ch);
|
2017-01-06 16:50:30 +01:00
|
|
|
if (glyph == NULL)
|
2017-01-06 01:36:29 +01:00
|
|
|
{
|
2017-01-06 16:50:30 +01:00
|
|
|
/* No, it is not cached... Does the code map to a font? */
|
2017-01-06 01:36:29 +01:00
|
|
|
|
2017-01-06 16:50:30 +01:00
|
|
|
fbm = nxf_getbitmap(priv->font, ch);
|
|
|
|
if (fbm)
|
|
|
|
{
|
2017-01-06 18:06:50 +01:00
|
|
|
/* Yes.. render the glyph for the font */
|
2017-01-06 01:36:29 +01:00
|
|
|
|
2017-01-06 16:50:30 +01:00
|
|
|
glyph = nxf_renderglyph(priv, fbm, ch);
|
|
|
|
}
|
2017-01-06 01:36:29 +01:00
|
|
|
}
|
|
|
|
|
2017-01-06 16:50:30 +01:00
|
|
|
nxf_cache_unlock(priv);
|
2017-01-06 01:36:29 +01:00
|
|
|
return glyph;
|
|
|
|
}
|