2002-09-12 13:33:59 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (c) 2002 by Greg Haerr <greg@censoft.com>
|
|
|
|
*
|
2008-06-28 18:10:04 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2002-09-12 13:33:59 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
/*
|
|
|
|
* Rockbox startup font initialization
|
|
|
|
* This file specifies which fonts get compiled-in and
|
|
|
|
* loaded at startup, as well as their mapping into
|
|
|
|
* the FONT_SYSFIXED, FONT_UI and FONT_MP3 ids.
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2010-08-23 18:17:54 +00:00
|
|
|
#include <stdlib.h>
|
2014-01-05 00:22:19 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
|
|
|
|
#include "config.h"
|
2011-02-27 22:44:30 +00:00
|
|
|
#include "system.h"
|
2014-01-05 00:22:19 +00:00
|
|
|
#include "kernel.h"
|
|
|
|
#include "lcd.h"
|
2002-09-12 13:33:59 +00:00
|
|
|
#include "font.h"
|
2002-09-20 08:07:51 +00:00
|
|
|
#include "file.h"
|
2011-09-24 13:19:34 +00:00
|
|
|
#include "core_alloc.h"
|
2002-09-12 13:33:59 +00:00
|
|
|
#include "debug.h"
|
|
|
|
#include "panic.h"
|
2005-12-06 13:27:15 +00:00
|
|
|
#include "rbunicode.h"
|
2009-11-24 20:41:42 +00:00
|
|
|
#include "diacritic.h"
|
2010-08-01 16:15:27 +00:00
|
|
|
#include "rbpaths.h"
|
2008-09-23 21:35:54 +00:00
|
|
|
|
2009-12-13 11:07:40 +00:00
|
|
|
#define MAX_FONTSIZE_FOR_16_BIT_OFFSETS 0xFFDB
|
|
|
|
|
|
|
|
/* max static loadable font buffer size */
|
|
|
|
#ifndef MAX_FONT_SIZE
|
|
|
|
#if LCD_HEIGHT > 64
|
2011-02-02 17:43:32 +00:00
|
|
|
#if MEMORYSIZE > 2
|
2009-12-13 11:07:40 +00:00
|
|
|
#define MAX_FONT_SIZE 60000
|
|
|
|
#else
|
|
|
|
#define MAX_FONT_SIZE 10000
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
#define MAX_FONT_SIZE 4000
|
|
|
|
#endif
|
|
|
|
#endif
|
2011-11-19 23:34:26 +00:00
|
|
|
#define GLYPHS_TO_CACHE 256
|
|
|
|
|
|
|
|
#if MEMORYSIZE < 4
|
|
|
|
#define FONT_HARD_LIMIT
|
|
|
|
#endif
|
2009-12-13 11:07:40 +00:00
|
|
|
|
|
|
|
#ifndef FONT_HEADER_SIZE
|
|
|
|
#define FONT_HEADER_SIZE 36
|
|
|
|
#endif
|
|
|
|
|
2008-09-23 21:35:54 +00:00
|
|
|
#ifndef BOOTLOADER
|
2005-12-06 13:27:15 +00:00
|
|
|
/* Font cache includes */
|
|
|
|
#include "font_cache.h"
|
|
|
|
#include "lru.h"
|
2008-09-23 21:35:54 +00:00
|
|
|
#endif
|
2002-09-12 13:33:59 +00:00
|
|
|
|
2002-09-20 08:07:51 +00:00
|
|
|
#ifndef O_BINARY
|
|
|
|
#define O_BINARY 0
|
|
|
|
#endif
|
|
|
|
|
2011-10-16 13:18:46 +00:00
|
|
|
/* Define this to try loading /.rockbox/.glyphcache *
|
|
|
|
* when a font specific file fails. This requires the *
|
|
|
|
* user to copy and rename a font glyph cache file */
|
|
|
|
//#define TRY_DEFAULT_GLYPHCACHE
|
|
|
|
|
2002-09-20 08:07:51 +00:00
|
|
|
/* compiled-in font */
|
|
|
|
extern struct font sysfont;
|
2002-09-12 13:33:59 +00:00
|
|
|
|
2008-09-23 21:35:54 +00:00
|
|
|
#ifndef BOOTLOADER
|
|
|
|
|
2011-09-24 13:19:34 +00:00
|
|
|
struct buflib_alloc_data {
|
2014-01-15 12:37:40 +00:00
|
|
|
struct font font; /* must be the first member! */
|
|
|
|
int handle_locks; /* is the buflib handle currently locked? */
|
|
|
|
int refcount; /* how many times has this font been loaded? */
|
2011-09-24 14:52:16 +00:00
|
|
|
unsigned char buffer[];
|
2011-09-24 13:19:34 +00:00
|
|
|
};
|
|
|
|
static int buflib_allocations[MAXFONTS];
|
2011-10-16 13:18:46 +00:00
|
|
|
|
|
|
|
static int cache_fd;
|
|
|
|
static struct font* cache_pf;
|
2011-09-24 13:19:34 +00:00
|
|
|
|
2011-11-27 15:24:53 +00:00
|
|
|
static void glyph_cache_save(int font_id);
|
|
|
|
|
2011-09-24 13:19:34 +00:00
|
|
|
static int buflibmove_callback(int handle, void* current, void* new)
|
|
|
|
{
|
|
|
|
(void)handle;
|
|
|
|
struct buflib_alloc_data *alloc = (struct buflib_alloc_data*)current;
|
2011-10-07 19:29:18 +00:00
|
|
|
ptrdiff_t diff = new - current;
|
2011-09-24 13:19:34 +00:00
|
|
|
|
2011-10-14 20:35:52 +00:00
|
|
|
if (alloc->handle_locks > 0)
|
2011-09-24 13:19:34 +00:00
|
|
|
return BUFLIB_CB_CANNOT_MOVE;
|
|
|
|
|
2011-10-07 19:29:18 +00:00
|
|
|
#define UPDATE(x) if (x) { x = PTR_ADD(x, diff); }
|
|
|
|
|
|
|
|
UPDATE(alloc->font.bits);
|
|
|
|
UPDATE(alloc->font.offset);
|
|
|
|
UPDATE(alloc->font.width);
|
|
|
|
|
|
|
|
UPDATE(alloc->font.buffer_start);
|
|
|
|
UPDATE(alloc->font.buffer_end);
|
|
|
|
UPDATE(alloc->font.buffer_position);
|
|
|
|
|
|
|
|
UPDATE(alloc->font.cache._index);
|
|
|
|
UPDATE(alloc->font.cache._lru._base);
|
2011-09-24 13:19:34 +00:00
|
|
|
|
|
|
|
return BUFLIB_CB_OK;
|
|
|
|
}
|
|
|
|
static void lock_font_handle(int handle, bool lock)
|
|
|
|
{
|
2011-11-19 23:34:26 +00:00
|
|
|
if ( handle < 0 )
|
|
|
|
return;
|
2011-09-24 13:19:34 +00:00
|
|
|
struct buflib_alloc_data *alloc = core_get_data(handle);
|
2011-10-14 20:35:52 +00:00
|
|
|
if ( lock )
|
|
|
|
alloc->handle_locks++;
|
|
|
|
else
|
|
|
|
alloc->handle_locks--;
|
2011-09-24 13:19:34 +00:00
|
|
|
}
|
2002-09-12 13:33:59 +00:00
|
|
|
|
2011-10-21 18:05:52 +00:00
|
|
|
void font_lock(int font_id, bool lock)
|
|
|
|
{
|
2011-11-27 15:36:03 +00:00
|
|
|
if( font_id < 0 || font_id >= MAXFONTS )
|
2011-10-21 18:05:52 +00:00
|
|
|
return;
|
|
|
|
if( buflib_allocations[font_id] >= 0 )
|
|
|
|
lock_font_handle(buflib_allocations[font_id], lock);
|
|
|
|
}
|
|
|
|
|
2012-05-02 21:22:28 +00:00
|
|
|
static struct buflib_callbacks buflibops = {buflibmove_callback, NULL, NULL };
|
2002-09-12 13:33:59 +00:00
|
|
|
|
2011-09-24 13:19:34 +00:00
|
|
|
static inline struct font *pf_from_handle(int handle)
|
|
|
|
{
|
|
|
|
struct buflib_alloc_data *alloc = core_get_data(handle);
|
|
|
|
struct font *pf = &alloc->font;
|
|
|
|
return pf;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline unsigned char *buffer_from_handle(int handle)
|
|
|
|
{
|
|
|
|
struct buflib_alloc_data *alloc = core_get_data(handle);
|
|
|
|
unsigned char* buffer = alloc->buffer;
|
|
|
|
return buffer;
|
|
|
|
}
|
2002-09-13 06:37:49 +00:00
|
|
|
|
2005-12-06 13:27:15 +00:00
|
|
|
/* Font cache structures */
|
2011-03-05 18:36:51 +00:00
|
|
|
static void cache_create(struct font* pf);
|
2011-11-19 23:34:26 +00:00
|
|
|
static void glyph_cache_load(const char *font_path, struct font *pf);
|
2005-12-06 13:27:15 +00:00
|
|
|
/* End Font cache structures */
|
|
|
|
|
2002-09-20 08:07:51 +00:00
|
|
|
void font_init(void)
|
2002-09-12 13:33:59 +00:00
|
|
|
{
|
2011-09-24 13:19:34 +00:00
|
|
|
int i = 0;
|
2011-10-16 13:18:46 +00:00
|
|
|
cache_fd = -1;
|
2010-02-14 06:26:16 +00:00
|
|
|
while (i<MAXFONTS)
|
2011-09-24 13:19:34 +00:00
|
|
|
buflib_allocations[i++] = -1;
|
2002-09-20 08:07:51 +00:00
|
|
|
}
|
2002-09-13 06:37:49 +00:00
|
|
|
|
2007-04-13 19:51:19 +00:00
|
|
|
/* Check if we have x bytes left in the file buffer */
|
2010-02-14 06:26:16 +00:00
|
|
|
#define HAVEBYTES(x) (pf->buffer_position + (x) <= pf->buffer_end)
|
2007-04-13 19:51:19 +00:00
|
|
|
|
|
|
|
/* Helper functions to read big-endian unaligned short or long from
|
|
|
|
the file buffer. Bounds-checking must be done in the calling
|
|
|
|
function.
|
|
|
|
*/
|
|
|
|
|
2010-02-14 06:26:16 +00:00
|
|
|
static short readshort(struct font *pf)
|
2002-09-20 08:07:51 +00:00
|
|
|
{
|
|
|
|
unsigned short s;
|
|
|
|
|
2010-02-14 06:26:16 +00:00
|
|
|
s = *pf->buffer_position++ & 0xff;
|
|
|
|
s |= (*pf->buffer_position++ << 8);
|
2007-04-13 19:51:19 +00:00
|
|
|
return s;
|
2002-09-12 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
2010-02-14 06:26:16 +00:00
|
|
|
static int32_t readlong(struct font *pf)
|
2002-09-12 13:33:59 +00:00
|
|
|
{
|
2007-06-30 17:54:02 +00:00
|
|
|
uint32_t l;
|
2002-09-12 13:33:59 +00:00
|
|
|
|
2010-02-14 06:26:16 +00:00
|
|
|
l = *pf->buffer_position++ & 0xff;
|
|
|
|
l |= *pf->buffer_position++ << 8;
|
|
|
|
l |= ((uint32_t)(*pf->buffer_position++)) << 16;
|
|
|
|
l |= ((uint32_t)(*pf->buffer_position++)) << 24;
|
2007-04-13 19:51:19 +00:00
|
|
|
return l;
|
2002-09-12 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
2011-03-05 18:36:51 +00:00
|
|
|
static int glyph_bytes( struct font *pf, int width )
|
|
|
|
{
|
2011-09-26 18:13:34 +00:00
|
|
|
int ret;
|
|
|
|
if (pf->depth)
|
|
|
|
ret = ( pf->height * width + 1 ) / 2;
|
|
|
|
else
|
|
|
|
ret = width * ((pf->height + 7) / 8);
|
|
|
|
return (ret + 1) & ~1;
|
2011-03-05 18:36:51 +00:00
|
|
|
}
|
|
|
|
|
2005-12-06 13:27:15 +00:00
|
|
|
/* Load memory font */
|
2011-11-19 23:34:26 +00:00
|
|
|
static struct font* font_load_in_memory(struct font* pf,
|
2011-11-20 17:49:47 +00:00
|
|
|
int32_t nwidth,
|
|
|
|
int32_t noffset )
|
2005-12-06 13:27:15 +00:00
|
|
|
{
|
2011-11-19 23:34:26 +00:00
|
|
|
int i;
|
2002-09-20 08:07:51 +00:00
|
|
|
/* variable font data*/
|
2011-11-19 23:34:26 +00:00
|
|
|
pf->buffer_position = pf->buffer_start + 36;
|
2010-02-14 06:26:16 +00:00
|
|
|
pf->bits = (unsigned char *)pf->buffer_position;
|
|
|
|
pf->buffer_position += pf->bits_size*sizeof(unsigned char);
|
2004-08-26 21:15:07 +00:00
|
|
|
|
2009-12-13 11:07:40 +00:00
|
|
|
if (pf->bits_size < MAX_FONTSIZE_FOR_16_BIT_OFFSETS)
|
2005-12-06 13:27:15 +00:00
|
|
|
{
|
|
|
|
/* pad to 16-bit boundary */
|
2010-02-14 06:26:16 +00:00
|
|
|
pf->buffer_position = (unsigned char *)(((intptr_t)pf->buffer_position + 1) & ~1);
|
2005-12-06 13:27:15 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* pad to 32-bit boundary*/
|
2010-02-14 06:26:16 +00:00
|
|
|
pf->buffer_position = (unsigned char *)(((intptr_t)pf->buffer_position + 3) & ~3);
|
2005-12-06 13:27:15 +00:00
|
|
|
}
|
2002-09-20 08:07:51 +00:00
|
|
|
|
2005-12-06 13:27:15 +00:00
|
|
|
if (noffset)
|
|
|
|
{
|
2009-12-13 11:07:40 +00:00
|
|
|
if (pf->bits_size < MAX_FONTSIZE_FOR_16_BIT_OFFSETS)
|
2005-12-06 13:27:15 +00:00
|
|
|
{
|
2010-02-14 06:26:16 +00:00
|
|
|
pf->long_offset = 0;
|
|
|
|
pf->offset = (uint16_t*)pf->buffer_position;
|
2007-04-13 19:51:19 +00:00
|
|
|
|
|
|
|
/* Check we have sufficient buffer */
|
2009-12-13 11:07:40 +00:00
|
|
|
if (!HAVEBYTES(noffset * sizeof(uint16_t)))
|
2007-04-13 19:51:19 +00:00
|
|
|
return NULL;
|
|
|
|
|
2005-12-06 13:27:15 +00:00
|
|
|
for (i=0; i<noffset; ++i)
|
|
|
|
{
|
2010-02-14 06:26:16 +00:00
|
|
|
((uint16_t*)(pf->offset))[i] = (uint16_t)readshort(pf);
|
2005-12-06 13:27:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2004-08-26 21:15:07 +00:00
|
|
|
{
|
2010-02-14 06:26:16 +00:00
|
|
|
pf->long_offset = 1;
|
|
|
|
pf->offset = (uint16_t*)pf->buffer_position;
|
2007-04-13 19:51:19 +00:00
|
|
|
|
|
|
|
/* Check we have sufficient buffer */
|
2007-06-30 17:54:02 +00:00
|
|
|
if (!HAVEBYTES(noffset * sizeof(int32_t)))
|
2007-04-13 19:51:19 +00:00
|
|
|
return NULL;
|
|
|
|
|
2005-12-06 13:27:15 +00:00
|
|
|
for (i=0; i<noffset; ++i)
|
|
|
|
{
|
2010-02-14 06:26:16 +00:00
|
|
|
((uint32_t*)(pf->offset))[i] = (uint32_t)readlong(pf);
|
2005-12-06 13:27:15 +00:00
|
|
|
}
|
2004-08-26 21:15:07 +00:00
|
|
|
}
|
2002-09-20 08:07:51 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
pf->offset = NULL;
|
|
|
|
|
|
|
|
if (nwidth) {
|
2010-02-14 06:26:16 +00:00
|
|
|
pf->width = (unsigned char *)pf->buffer_position;
|
|
|
|
pf->buffer_position += nwidth*sizeof(unsigned char);
|
2002-09-20 08:07:51 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
pf->width = NULL;
|
|
|
|
|
2010-02-14 06:26:16 +00:00
|
|
|
if (pf->buffer_position > pf->buffer_end)
|
2002-09-20 08:07:51 +00:00
|
|
|
return NULL;
|
|
|
|
|
2004-05-14 23:08:08 +00:00
|
|
|
return pf; /* success!*/
|
2002-09-12 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
2005-12-06 13:27:15 +00:00
|
|
|
/* Load cached font */
|
2011-11-19 23:34:26 +00:00
|
|
|
static struct font* font_load_cached(struct font* pf,
|
|
|
|
int32_t nwidth,
|
|
|
|
int32_t noffset)
|
2005-12-06 13:27:15 +00:00
|
|
|
{
|
|
|
|
/* We are now at the bitmap data, this is fixed at 36.. */
|
2014-01-15 12:37:40 +00:00
|
|
|
pf->width = NULL;
|
2005-12-06 13:27:15 +00:00
|
|
|
pf->bits = NULL;
|
|
|
|
|
|
|
|
/* Calculate offset to offset data */
|
2010-02-14 06:26:16 +00:00
|
|
|
pf->buffer_position += pf->bits_size * sizeof(unsigned char);
|
2005-12-06 13:27:15 +00:00
|
|
|
|
2009-12-13 11:07:40 +00:00
|
|
|
if (pf->bits_size < MAX_FONTSIZE_FOR_16_BIT_OFFSETS)
|
2005-12-06 13:27:15 +00:00
|
|
|
{
|
2010-02-14 06:26:16 +00:00
|
|
|
pf->long_offset = 0;
|
2005-12-06 13:27:15 +00:00
|
|
|
/* pad to 16-bit boundary */
|
2010-02-14 06:26:16 +00:00
|
|
|
pf->buffer_position = (unsigned char *)(((intptr_t)pf->buffer_position + 1) & ~1);
|
2005-12-06 13:27:15 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-02-14 06:26:16 +00:00
|
|
|
pf->long_offset = 1;
|
2005-12-06 13:27:15 +00:00
|
|
|
/* pad to 32-bit boundary*/
|
2010-02-14 06:26:16 +00:00
|
|
|
pf->buffer_position = (unsigned char *)(((intptr_t)pf->buffer_position + 3) & ~3);
|
2005-12-06 13:27:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (noffset)
|
2010-02-14 06:26:16 +00:00
|
|
|
pf->file_offset_offset = (uint32_t)(pf->buffer_position - pf->buffer_start);
|
2005-12-06 13:27:15 +00:00
|
|
|
else
|
2010-02-14 06:26:16 +00:00
|
|
|
pf->file_offset_offset = 0;
|
2005-12-06 13:27:15 +00:00
|
|
|
|
|
|
|
/* Calculate offset to widths data */
|
2009-12-13 11:07:40 +00:00
|
|
|
if (pf->bits_size < MAX_FONTSIZE_FOR_16_BIT_OFFSETS)
|
2010-02-14 06:26:16 +00:00
|
|
|
pf->buffer_position += noffset * sizeof(uint16_t);
|
2005-12-06 13:27:15 +00:00
|
|
|
else
|
2010-02-14 06:26:16 +00:00
|
|
|
pf->buffer_position += noffset * sizeof(uint32_t);
|
2005-12-06 13:27:15 +00:00
|
|
|
|
|
|
|
if (nwidth)
|
2010-02-14 06:26:16 +00:00
|
|
|
pf->file_width_offset = (uint32_t)(pf->buffer_position - pf->buffer_start);
|
2005-12-06 13:27:15 +00:00
|
|
|
else
|
2010-02-14 06:26:16 +00:00
|
|
|
pf->file_width_offset = 0;
|
2005-12-06 13:27:15 +00:00
|
|
|
|
|
|
|
/* Create the cache */
|
2011-03-05 18:36:51 +00:00
|
|
|
cache_create(pf);
|
2005-12-06 13:27:15 +00:00
|
|
|
|
|
|
|
return pf;
|
|
|
|
}
|
|
|
|
|
2010-02-26 07:16:32 +00:00
|
|
|
|
2011-09-24 13:19:34 +00:00
|
|
|
static int find_font_index(const char* path)
|
2010-02-14 06:26:16 +00:00
|
|
|
{
|
2011-09-24 13:19:34 +00:00
|
|
|
int index = 0, handle;
|
|
|
|
|
|
|
|
while (index < MAXFONTS)
|
2010-02-14 06:26:16 +00:00
|
|
|
{
|
2011-09-24 13:19:34 +00:00
|
|
|
handle = buflib_allocations[index];
|
|
|
|
if (handle > 0 && !strcmp(core_get_name(handle), path))
|
|
|
|
return index;
|
|
|
|
index++;
|
2010-02-14 06:26:16 +00:00
|
|
|
}
|
2011-09-24 13:19:34 +00:00
|
|
|
return FONT_SYSFIXED;
|
2010-02-14 06:26:16 +00:00
|
|
|
}
|
2005-12-06 13:27:15 +00:00
|
|
|
|
2011-09-24 13:19:34 +00:00
|
|
|
const char* font_filename(int font_id)
|
|
|
|
{
|
2011-11-27 15:36:03 +00:00
|
|
|
if ( font_id < 0 || font_id >= MAXFONTS )
|
|
|
|
return NULL;
|
2011-09-24 13:19:34 +00:00
|
|
|
int handle = buflib_allocations[font_id];
|
|
|
|
if (handle > 0)
|
|
|
|
return core_get_name(handle);
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-11-19 23:34:26 +00:00
|
|
|
|
2011-12-20 20:35:28 +00:00
|
|
|
static size_t font_glyphs_to_bufsize(struct font *pf, int glyphs)
|
2011-11-19 23:34:26 +00:00
|
|
|
{
|
|
|
|
size_t bufsize;
|
|
|
|
|
|
|
|
/* LRU bytes per glyph */
|
|
|
|
bufsize = LRU_SLOT_OVERHEAD + sizeof(struct font_cache_entry) +
|
|
|
|
sizeof( unsigned short);
|
|
|
|
/* Image bytes per glyph */
|
|
|
|
bufsize += glyph_bytes(pf, pf->maxwidth);
|
|
|
|
bufsize *= glyphs;
|
|
|
|
|
|
|
|
return bufsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct font* font_load_header(int fd, struct font *pheader,
|
|
|
|
struct font *pf,
|
|
|
|
uint32_t *nwidth, uint32_t *noffset)
|
|
|
|
{
|
|
|
|
/* Load the header. Readshort() and readlong() *
|
|
|
|
* update buffer_position address as they read */
|
|
|
|
pheader->buffer_start = pheader->buffer_position = (char *)pheader;
|
|
|
|
pheader->buffer_size = FONT_HEADER_SIZE;
|
|
|
|
pheader->buffer_end = pheader->buffer_start + pheader->buffer_size;
|
|
|
|
|
|
|
|
if (read(fd, pheader, FONT_HEADER_SIZE) != FONT_HEADER_SIZE)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* read magic and version #*/
|
|
|
|
if (memcmp(pheader->buffer_position, VERSION, 4) != 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
pheader->buffer_position += 4;
|
|
|
|
|
|
|
|
/* font info*/
|
|
|
|
pf->maxwidth = readshort(pheader);
|
|
|
|
pf->height = readshort(pheader);
|
|
|
|
pf->ascent = readshort(pheader);
|
|
|
|
pf->depth = readshort(pheader);
|
|
|
|
pf->firstchar = readlong(pheader);
|
|
|
|
pf->defaultchar = readlong(pheader);
|
|
|
|
pf->size = readlong(pheader);
|
|
|
|
|
|
|
|
/* get variable font data sizes*/
|
|
|
|
/* # words of bitmap_t*/
|
|
|
|
pf->bits_size = readlong(pheader);
|
|
|
|
*noffset = readlong(pheader);
|
|
|
|
*nwidth = readlong(pheader);
|
|
|
|
|
|
|
|
return pf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* load a font with room for glyphs, limited to bufsize if not zero */
|
|
|
|
int font_load_ex( const char *path, size_t buf_size, int glyphs )
|
2010-02-14 06:26:16 +00:00
|
|
|
{
|
2011-11-19 23:34:26 +00:00
|
|
|
//printf("\nfont_load_ex(%s, %d, %d)\n", path, buf_size, glyphs);
|
|
|
|
int fd = open(path, O_RDONLY|O_BINARY);
|
|
|
|
if ( fd < 0 )
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* load font struct f with file header */
|
|
|
|
int file_size = filesize( fd );
|
|
|
|
struct font header;
|
|
|
|
struct font f;
|
|
|
|
|
|
|
|
uint32_t nwidth, noffset;
|
2014-01-15 12:37:40 +00:00
|
|
|
if ( !font_load_header( fd, &header, &f, &nwidth, &noffset )
|
2011-11-19 23:34:26 +00:00
|
|
|
#if LCD_DEPTH < 16
|
|
|
|
|| f.depth
|
|
|
|
#endif
|
|
|
|
)
|
|
|
|
{
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* examine f and calc buffer size */
|
|
|
|
bool cached = false;
|
|
|
|
size_t bufsize = buf_size;
|
|
|
|
size_t glyph_buf_size = font_glyphs_to_bufsize( &f, glyphs );
|
|
|
|
|
|
|
|
if ( bufsize && glyphs && bufsize > glyph_buf_size)
|
|
|
|
bufsize = glyph_buf_size;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( glyphs )
|
|
|
|
bufsize = glyph_buf_size;
|
|
|
|
else
|
|
|
|
bufsize = MAX_FONT_SIZE;
|
|
|
|
}
|
|
|
|
#ifdef FONT_HARD_LIMIT
|
|
|
|
if ( bufsize > MAX_FONT_SIZE )
|
|
|
|
bufsize = MAX_FONT_SIZE;
|
|
|
|
#endif
|
|
|
|
if ( bufsize < (size_t) file_size )
|
|
|
|
cached = true;
|
|
|
|
else
|
|
|
|
bufsize = file_size;
|
|
|
|
|
|
|
|
/* check already loaded */
|
2011-09-24 13:19:34 +00:00
|
|
|
int font_id = find_font_index(path);
|
|
|
|
|
|
|
|
if (font_id > FONT_SYSFIXED)
|
2010-02-14 06:26:16 +00:00
|
|
|
{
|
2011-09-24 13:19:34 +00:00
|
|
|
/* already loaded, no need to reload */
|
|
|
|
struct buflib_alloc_data *pd = core_get_data(buflib_allocations[font_id]);
|
2011-11-19 23:34:26 +00:00
|
|
|
if (pd->font.buffer_size < bufsize)
|
2011-09-24 14:52:16 +00:00
|
|
|
{
|
|
|
|
int old_refcount, old_id;
|
2011-11-19 23:34:26 +00:00
|
|
|
size_t old_bufsize = pd->font.buffer_size;
|
|
|
|
bool failed = false;
|
2011-09-24 14:52:16 +00:00
|
|
|
/* reload the font:
|
|
|
|
* 1) save of refcont and id
|
|
|
|
* 2) force unload (set refcount to 1 to make sure it get unloaded)
|
|
|
|
* 3) reload with the larger buffer
|
|
|
|
* 4) restore the id and refcount
|
|
|
|
*/
|
|
|
|
old_id = font_id;
|
|
|
|
old_refcount = pd->refcount;
|
|
|
|
pd->refcount = 1;
|
|
|
|
font_unload(font_id);
|
2011-11-19 23:34:26 +00:00
|
|
|
font_id = font_load_ex(path, bufsize, glyphs);
|
2011-09-24 14:52:16 +00:00
|
|
|
if (font_id < 0)
|
|
|
|
{
|
2011-11-19 23:34:26 +00:00
|
|
|
failed = true;
|
|
|
|
font_id = font_load_ex(path, old_bufsize, 0);
|
|
|
|
/* we couldn't even get the old size, this shouldn't happen */
|
|
|
|
if ( font_id < 0 )
|
|
|
|
return -1;
|
2011-09-24 14:52:16 +00:00
|
|
|
}
|
|
|
|
if (old_id != font_id)
|
|
|
|
{
|
|
|
|
buflib_allocations[old_id] = buflib_allocations[font_id];
|
|
|
|
buflib_allocations[font_id] = -1;
|
|
|
|
font_id = old_id;
|
|
|
|
}
|
|
|
|
pd = core_get_data(buflib_allocations[font_id]);
|
|
|
|
pd->refcount = old_refcount;
|
2011-11-19 23:34:26 +00:00
|
|
|
if(failed)
|
|
|
|
/* return error because we didn't satisfy the new buffer size */
|
|
|
|
return -1;
|
2011-09-24 14:52:16 +00:00
|
|
|
}
|
2011-09-24 13:19:34 +00:00
|
|
|
pd->refcount++;
|
|
|
|
//printf("reusing handle %d for %s (count: %d)\n", font_id, path, pd->refcount);
|
2011-11-19 23:34:26 +00:00
|
|
|
close(fd);
|
2011-09-24 13:19:34 +00:00
|
|
|
return font_id;
|
2010-02-14 06:26:16 +00:00
|
|
|
}
|
2011-09-24 13:19:34 +00:00
|
|
|
|
2011-11-19 23:34:26 +00:00
|
|
|
int open_slot = -1;
|
|
|
|
|
2011-09-24 13:19:34 +00:00
|
|
|
for (font_id = FONT_FIRSTUSERFONT; font_id < MAXFONTS; font_id++)
|
2010-02-14 06:26:16 +00:00
|
|
|
{
|
2011-11-19 23:34:26 +00:00
|
|
|
if (buflib_allocations[ font_id ] < 0)
|
2010-02-14 06:26:16 +00:00
|
|
|
{
|
2011-11-19 23:34:26 +00:00
|
|
|
open_slot = font_id;
|
2011-09-24 13:19:34 +00:00
|
|
|
break;
|
2010-02-14 06:26:16 +00:00
|
|
|
}
|
|
|
|
}
|
2011-11-19 23:34:26 +00:00
|
|
|
if ( open_slot == -1 )
|
2011-09-24 13:19:34 +00:00
|
|
|
return -1;
|
2011-11-19 23:34:26 +00:00
|
|
|
font_id = open_slot;
|
2011-09-24 13:19:34 +00:00
|
|
|
|
2011-11-19 23:34:26 +00:00
|
|
|
/* allocate mem */
|
|
|
|
int handle = core_alloc_ex( path,
|
|
|
|
bufsize + sizeof( struct buflib_alloc_data ),
|
|
|
|
&buflibops );
|
2012-06-10 15:15:47 +00:00
|
|
|
if ( handle <= 0 )
|
2010-02-14 06:26:16 +00:00
|
|
|
{
|
|
|
|
return -1;
|
2011-09-24 13:19:34 +00:00
|
|
|
}
|
2011-11-19 23:34:26 +00:00
|
|
|
struct buflib_alloc_data *pdata;
|
|
|
|
pdata = core_get_data(handle);
|
|
|
|
pdata->handle_locks = 1;
|
|
|
|
pdata->refcount = 1;
|
|
|
|
|
|
|
|
/* load and init */
|
2014-01-16 09:31:15 +00:00
|
|
|
struct font *pf = &pdata->font;
|
2011-11-19 23:34:26 +00:00
|
|
|
memcpy(pf, &f, sizeof( struct font) );
|
|
|
|
|
|
|
|
pf->fd = fd;
|
|
|
|
pf->fd_width = pf->fd_offset = -1;
|
|
|
|
pf->handle = handle;
|
2014-01-26 12:56:53 +00:00
|
|
|
pf->disabled = false;
|
2011-11-19 23:34:26 +00:00
|
|
|
|
|
|
|
pf->buffer_start = buffer_from_handle( pf->handle );
|
|
|
|
pf->buffer_position = pf->buffer_start + FONT_HEADER_SIZE;
|
|
|
|
pf->buffer_size = bufsize;
|
|
|
|
pf->buffer_end = pf->buffer_start + bufsize;
|
|
|
|
|
|
|
|
if ( cached )
|
|
|
|
{
|
|
|
|
if ( ! font_load_cached( pf, nwidth, noffset ) )
|
|
|
|
{
|
|
|
|
core_free( handle );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* trick to get a small cache for each file section *
|
|
|
|
* during glyph_cache_load() */
|
|
|
|
pf->fd_width = open( path, O_RDONLY|O_BINARY );
|
|
|
|
pf->fd_offset = open( path, O_RDONLY|O_BINARY );
|
|
|
|
|
|
|
|
glyph_cache_load( path, pf );
|
|
|
|
|
|
|
|
/* cached font: pf->fd stays open until the font is unloaded */
|
|
|
|
close( pf->fd_width );
|
|
|
|
pf->fd_width = -1;
|
|
|
|
close( pf->fd_offset );
|
|
|
|
pf->fd_offset = -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lseek( fd, 0, SEEK_SET);
|
|
|
|
read(fd, pf->buffer_start, pf->buffer_size);
|
|
|
|
|
|
|
|
close( fd );
|
|
|
|
pf->fd = -1;
|
|
|
|
|
|
|
|
if ( ! font_load_in_memory( pf, nwidth, noffset ) )
|
|
|
|
{
|
|
|
|
core_free( handle );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2011-10-14 14:04:27 +00:00
|
|
|
buflib_allocations[font_id] = handle;
|
2011-11-08 10:09:33 +00:00
|
|
|
//printf("%s -> [%d] -> %d\n", path, font_id, *handle);
|
2011-11-19 23:34:26 +00:00
|
|
|
lock_font_handle( handle, false );
|
2010-02-14 06:26:16 +00:00
|
|
|
return font_id; /* success!*/
|
|
|
|
}
|
2011-11-19 23:34:26 +00:00
|
|
|
|
2011-09-24 14:52:16 +00:00
|
|
|
int font_load(const char *path)
|
|
|
|
{
|
2011-11-19 23:34:26 +00:00
|
|
|
return font_load_ex(path, MAX_FONT_SIZE, GLYPHS_TO_CACHE);
|
2011-09-24 14:52:16 +00:00
|
|
|
}
|
2005-12-06 13:27:15 +00:00
|
|
|
|
2010-02-14 06:26:16 +00:00
|
|
|
void font_unload(int font_id)
|
|
|
|
{
|
2011-11-27 15:36:03 +00:00
|
|
|
if ( font_id < 0 || font_id >= MAXFONTS )
|
2011-10-16 13:18:46 +00:00
|
|
|
return;
|
|
|
|
int handle = buflib_allocations[font_id];
|
|
|
|
if ( handle < 0 )
|
|
|
|
return;
|
|
|
|
struct buflib_alloc_data *pdata = core_get_data(handle);
|
2011-09-24 13:19:34 +00:00
|
|
|
struct font* pf = &pdata->font;
|
|
|
|
pdata->refcount--;
|
|
|
|
if (pdata->refcount < 1)
|
2010-02-14 06:26:16 +00:00
|
|
|
{
|
2011-09-24 13:19:34 +00:00
|
|
|
//printf("freeing id: %d %s\n", font_id, core_get_name(*handle));
|
|
|
|
if (pf && pf->fd >= 0)
|
2011-10-16 13:18:46 +00:00
|
|
|
{
|
|
|
|
glyph_cache_save(font_id);
|
2010-02-14 06:26:16 +00:00
|
|
|
close(pf->fd);
|
2011-10-16 13:18:46 +00:00
|
|
|
}
|
|
|
|
if (handle > 0)
|
|
|
|
core_free(handle);
|
|
|
|
buflib_allocations[font_id] = -1;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void font_unload_all(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i=0; i<MAXFONTS; i++)
|
|
|
|
{
|
|
|
|
if (buflib_allocations[i] > 0)
|
|
|
|
{
|
|
|
|
struct buflib_alloc_data *alloc = core_get_data(buflib_allocations[i]);
|
|
|
|
alloc->refcount = 1; /* force unload */
|
|
|
|
font_unload(i);
|
|
|
|
}
|
2010-02-14 06:26:16 +00:00
|
|
|
}
|
2005-12-06 13:27:15 +00:00
|
|
|
}
|
|
|
|
|
2014-01-15 12:37:40 +00:00
|
|
|
static void font_disable(int font_id)
|
|
|
|
{
|
|
|
|
if ( font_id < 0 || font_id >= MAXFONTS )
|
|
|
|
return;
|
|
|
|
int handle = buflib_allocations[font_id];
|
|
|
|
if ( handle < 0 )
|
|
|
|
return;
|
|
|
|
struct buflib_alloc_data *pdata = core_get_data(handle);
|
|
|
|
struct font *pf = &pdata->font;
|
|
|
|
|
|
|
|
if (pf->fd >= 0)
|
|
|
|
{
|
|
|
|
/* save the cache, but it keep it in-RAM so that cache lookups
|
|
|
|
* can still succeed on the same font */
|
|
|
|
glyph_cache_save(font_id);
|
|
|
|
close(pf->fd);
|
|
|
|
pf->fd = -1;
|
2014-01-26 12:56:53 +00:00
|
|
|
pf->disabled = true;
|
2014-01-15 12:37:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void font_disable_all(void)
|
|
|
|
{
|
|
|
|
for(int i = 0; i < MAXFONTS; i++)
|
|
|
|
font_disable(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void font_enable(int font_id)
|
|
|
|
{
|
|
|
|
if ( font_id < 0 || font_id >= MAXFONTS )
|
|
|
|
return;
|
|
|
|
int handle = buflib_allocations[font_id];
|
|
|
|
if ( handle < 0 )
|
|
|
|
return;
|
|
|
|
struct buflib_alloc_data *pdata = core_get_data(handle);
|
|
|
|
struct font *pf = &pdata->font;
|
|
|
|
|
2014-01-26 12:56:53 +00:00
|
|
|
if (pf->disabled && pf->fd < 0)
|
2014-01-15 12:37:40 +00:00
|
|
|
{
|
|
|
|
const char *filename = font_filename(font_id);
|
|
|
|
pf->fd = open(filename, O_RDONLY);
|
2014-01-26 12:56:53 +00:00
|
|
|
pf->disabled = false;
|
2014-01-15 12:37:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void font_enable_all(void)
|
|
|
|
{
|
|
|
|
for(int i = 0; i < MAXFONTS; i++)
|
|
|
|
font_enable(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-13 06:37:49 +00:00
|
|
|
/*
|
2002-09-20 08:07:51 +00:00
|
|
|
* Return a pointer to an incore font structure.
|
2011-11-08 10:09:33 +00:00
|
|
|
* If the requested font isn't loaded/compiled-in,
|
|
|
|
* decrement the font number and try again.
|
2002-09-13 06:37:49 +00:00
|
|
|
*/
|
2011-11-08 10:09:33 +00:00
|
|
|
struct font* font_get(int font)
|
2002-09-13 06:37:49 +00:00
|
|
|
{
|
2011-11-08 10:09:33 +00:00
|
|
|
struct font* pf;
|
|
|
|
if (font == FONT_UI)
|
|
|
|
font = MAXFONTS-1;
|
|
|
|
if (font <= FONT_SYSFIXED)
|
2011-10-22 17:13:33 +00:00
|
|
|
return &sysfont;
|
|
|
|
|
2011-11-08 10:09:33 +00:00
|
|
|
while (1) {
|
|
|
|
if (buflib_allocations[font] > 0)
|
|
|
|
{
|
|
|
|
struct buflib_alloc_data *alloc = core_get_data(buflib_allocations[font]);
|
|
|
|
pf = &alloc->font;
|
|
|
|
if (pf && pf->height)
|
|
|
|
return pf;
|
|
|
|
}
|
|
|
|
if (--font < 0)
|
|
|
|
return &sysfont;
|
|
|
|
}
|
2011-09-24 13:19:34 +00:00
|
|
|
}
|
|
|
|
|
2005-12-06 13:27:15 +00:00
|
|
|
/*
|
|
|
|
* Reads an entry into cache entry
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
load_cache_entry(struct font_cache_entry* p, void* callback_data)
|
|
|
|
{
|
|
|
|
struct font* pf = callback_data;
|
2011-11-19 23:34:26 +00:00
|
|
|
|
2005-12-06 13:27:15 +00:00
|
|
|
unsigned short char_code = p->_char_code;
|
|
|
|
unsigned char tmp[2];
|
2011-10-17 13:17:04 +00:00
|
|
|
int fd;
|
2010-02-26 07:16:32 +00:00
|
|
|
|
2011-11-19 23:34:26 +00:00
|
|
|
lock_font_handle(pf->handle, true);
|
2010-02-14 06:26:16 +00:00
|
|
|
if (pf->file_width_offset)
|
2005-12-06 13:27:15 +00:00
|
|
|
{
|
2010-02-14 06:26:16 +00:00
|
|
|
int width_offset = pf->file_width_offset + char_code;
|
2011-10-17 13:17:04 +00:00
|
|
|
/* load via different fd to get this file section cached */
|
|
|
|
if(pf->fd_width >=0 )
|
|
|
|
fd = pf->fd_width;
|
|
|
|
else
|
|
|
|
fd = pf->fd;
|
|
|
|
lseek(fd, width_offset, SEEK_SET);
|
|
|
|
read(fd, &(p->width), 1);
|
2005-12-06 13:27:15 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
p->width = pf->maxwidth;
|
|
|
|
}
|
2010-02-26 07:16:32 +00:00
|
|
|
|
2007-06-30 17:54:02 +00:00
|
|
|
int32_t bitmap_offset = 0;
|
2005-12-06 13:27:15 +00:00
|
|
|
|
2010-02-14 06:26:16 +00:00
|
|
|
if (pf->file_offset_offset)
|
2005-12-06 13:27:15 +00:00
|
|
|
{
|
2010-02-14 06:26:16 +00:00
|
|
|
int32_t offset = pf->file_offset_offset + char_code * (pf->long_offset ? sizeof(int32_t) : sizeof(int16_t));
|
2011-10-17 13:17:04 +00:00
|
|
|
/* load via different fd to get this file section cached */
|
|
|
|
if(pf->fd_offset >=0 )
|
|
|
|
fd = pf->fd_offset;
|
|
|
|
else
|
|
|
|
fd = pf->fd;
|
|
|
|
lseek(fd, offset, SEEK_SET);
|
|
|
|
read (fd, tmp, 2);
|
2005-12-06 13:27:15 +00:00
|
|
|
bitmap_offset = tmp[0] | (tmp[1] << 8);
|
2010-02-14 06:26:16 +00:00
|
|
|
if (pf->long_offset) {
|
2011-10-17 13:17:04 +00:00
|
|
|
read (fd, tmp, 2);
|
2005-12-06 13:27:15 +00:00
|
|
|
bitmap_offset |= (tmp[0] << 16) | (tmp[1] << 24);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-03-05 18:36:51 +00:00
|
|
|
bitmap_offset = char_code * glyph_bytes(pf, p->width);
|
2005-12-06 13:27:15 +00:00
|
|
|
}
|
|
|
|
|
2007-06-30 17:54:02 +00:00
|
|
|
int32_t file_offset = FONT_HEADER_SIZE + bitmap_offset;
|
2010-02-14 06:26:16 +00:00
|
|
|
lseek(pf->fd, file_offset, SEEK_SET);
|
2011-03-05 18:36:51 +00:00
|
|
|
int src_bytes = glyph_bytes(pf, p->width);
|
2010-02-14 06:26:16 +00:00
|
|
|
read(pf->fd, p->bitmap, src_bytes);
|
2011-09-24 13:19:34 +00:00
|
|
|
|
2011-11-19 23:34:26 +00:00
|
|
|
lock_font_handle(pf->handle, false);
|
2005-12-06 13:27:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Converts cbuf into a font cache
|
|
|
|
*/
|
2011-03-05 18:36:51 +00:00
|
|
|
static void cache_create(struct font* pf)
|
2005-12-06 13:27:15 +00:00
|
|
|
{
|
|
|
|
/* maximum size of rotated bitmap */
|
2014-01-15 12:37:40 +00:00
|
|
|
int bitmap_size = glyph_bytes(pf, pf->maxwidth);
|
|
|
|
/* reserve one blank glyph that is guaranteed to be available, even
|
|
|
|
* when the font file is closed during USB */
|
|
|
|
unsigned char *cache_buf = pf->buffer_start + bitmap_size;
|
2014-01-26 12:56:53 +00:00
|
|
|
size_t cache_size = pf->buffer_size - bitmap_size;
|
2014-01-15 12:37:40 +00:00
|
|
|
ALIGN_BUFFER(cache_buf, cache_size, 2);
|
|
|
|
memset(pf->buffer_start, 0, bitmap_size);
|
2005-12-06 13:27:15 +00:00
|
|
|
/* Initialise cache */
|
2014-01-15 12:37:40 +00:00
|
|
|
font_cache_create(&pf->cache, cache_buf, cache_size, bitmap_size);
|
2005-12-06 13:27:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns width of character
|
|
|
|
*/
|
|
|
|
int font_get_width(struct font* pf, unsigned short char_code)
|
|
|
|
{
|
2014-01-15 12:37:40 +00:00
|
|
|
int width;
|
|
|
|
struct font_cache_entry *e;
|
|
|
|
|
2006-03-22 09:53:27 +00:00
|
|
|
/* check input range*/
|
|
|
|
if (char_code < pf->firstchar || char_code >= pf->firstchar+pf->size)
|
|
|
|
char_code = pf->defaultchar;
|
|
|
|
char_code -= pf->firstchar;
|
|
|
|
|
2014-01-26 12:56:53 +00:00
|
|
|
if (pf->fd >= 0 && pf != &sysfont)
|
|
|
|
width = font_cache_get(&pf->cache,char_code,false,load_cache_entry,pf)->width;
|
|
|
|
else if (pf->disabled &&
|
|
|
|
(e = font_cache_get(&pf->cache,char_code,true,load_cache_entry,pf)))
|
|
|
|
width = e->width; /* falls back to pf->maxwidth if !e */
|
2014-01-15 12:37:40 +00:00
|
|
|
else if (pf->width)
|
|
|
|
width = pf->width[char_code];
|
|
|
|
else
|
|
|
|
width = pf->maxwidth;
|
|
|
|
|
|
|
|
return width;
|
2005-12-06 13:27:15 +00:00
|
|
|
}
|
2010-02-26 07:16:32 +00:00
|
|
|
|
2005-12-06 13:27:15 +00:00
|
|
|
const unsigned char* font_get_bits(struct font* pf, unsigned short char_code)
|
|
|
|
{
|
|
|
|
const unsigned char* bits;
|
2006-03-22 09:53:27 +00:00
|
|
|
|
|
|
|
/* check input range*/
|
|
|
|
if (char_code < pf->firstchar || char_code >= pf->firstchar+pf->size)
|
|
|
|
char_code = pf->defaultchar;
|
|
|
|
char_code -= pf->firstchar;
|
|
|
|
|
2010-02-14 06:26:16 +00:00
|
|
|
if (pf->fd >= 0 && pf != &sysfont)
|
2005-12-06 13:27:15 +00:00
|
|
|
{
|
2011-11-19 23:34:26 +00:00
|
|
|
bits =
|
2014-01-15 12:37:40 +00:00
|
|
|
(unsigned char*)font_cache_get(&pf->cache, char_code,
|
2014-01-26 12:56:53 +00:00
|
|
|
false, load_cache_entry, pf)->bitmap;
|
2014-01-15 12:37:40 +00:00
|
|
|
}
|
2014-01-26 12:56:53 +00:00
|
|
|
else if (pf->disabled)
|
2014-01-15 12:37:40 +00:00
|
|
|
{
|
|
|
|
/* the font handle is closed, but the cache is intact. Attempt
|
|
|
|
* a lookup, which is very likely to succeed. Return a placeholder
|
|
|
|
* glyph on miss (again, this is very unlikely */
|
|
|
|
struct font_cache_entry *e = font_cache_get(&pf->cache, char_code,
|
|
|
|
true, NULL, NULL);
|
|
|
|
if (LIKELY(e))
|
|
|
|
bits = (unsigned char *) e->bitmap;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Could attempt to find a suitable fallback glyph from the same
|
|
|
|
* font. For now just return blank space which is
|
|
|
|
* reserved by cache_create() at buffer_start */
|
|
|
|
bits = pf->buffer_start;
|
|
|
|
}
|
2005-12-06 13:27:15 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-01-15 12:37:40 +00:00
|
|
|
/* This font is entirely in RAM */
|
2009-12-13 11:07:40 +00:00
|
|
|
bits = pf->bits;
|
|
|
|
if (pf->offset)
|
|
|
|
{
|
|
|
|
if (pf->bits_size < MAX_FONTSIZE_FOR_16_BIT_OFFSETS)
|
|
|
|
bits += ((uint16_t*)(pf->offset))[char_code];
|
|
|
|
else
|
|
|
|
bits += ((uint32_t*)(pf->offset))[char_code];
|
|
|
|
}
|
|
|
|
else
|
2011-03-05 18:36:51 +00:00
|
|
|
bits += char_code * glyph_bytes(pf, pf->maxwidth);
|
2005-12-06 13:27:15 +00:00
|
|
|
}
|
2009-12-13 11:07:40 +00:00
|
|
|
|
2005-12-06 13:27:15 +00:00
|
|
|
return bits;
|
|
|
|
}
|
2011-10-16 13:18:46 +00:00
|
|
|
|
2011-11-03 23:32:49 +00:00
|
|
|
static void font_path_to_glyph_path( const char *font_path, char *glyph_path)
|
2011-10-16 13:18:46 +00:00
|
|
|
{
|
|
|
|
/* take full file name, cut extension, and add .glyphcache */
|
|
|
|
strlcpy(glyph_path, font_path, MAX_PATH);
|
|
|
|
glyph_path[strlen(glyph_path)-4] = '\0';
|
|
|
|
strcat(glyph_path, ".gc");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* call with NULL to flush */
|
2007-06-30 17:54:02 +00:00
|
|
|
static void glyph_file_write(void* data)
|
2005-12-06 13:27:15 +00:00
|
|
|
{
|
|
|
|
struct font_cache_entry* p = data;
|
2011-10-16 13:18:46 +00:00
|
|
|
struct font* pf = cache_pf;
|
2006-03-22 09:53:27 +00:00
|
|
|
unsigned short ch;
|
2011-10-16 13:18:46 +00:00
|
|
|
static int buffer_pos = 0;
|
|
|
|
#define WRITE_BUFFER 256
|
|
|
|
static unsigned char buffer[WRITE_BUFFER];
|
2005-12-06 13:27:15 +00:00
|
|
|
|
2011-10-16 13:18:46 +00:00
|
|
|
/* flush buffer & reset */
|
|
|
|
if ( data == NULL || buffer_pos >= WRITE_BUFFER)
|
|
|
|
{
|
|
|
|
write(cache_fd, buffer, buffer_pos);
|
|
|
|
buffer_pos = 0;
|
|
|
|
if ( data == NULL )
|
|
|
|
return;
|
|
|
|
}
|
2010-08-23 18:17:54 +00:00
|
|
|
if ( p->_char_code == 0xffff )
|
|
|
|
return;
|
|
|
|
|
2006-03-22 09:53:27 +00:00
|
|
|
ch = p->_char_code + pf->firstchar;
|
2011-10-16 13:18:46 +00:00
|
|
|
buffer[buffer_pos] = ch >> 8;
|
|
|
|
buffer[buffer_pos+1] = ch & 0xff;
|
|
|
|
buffer_pos += 2;
|
2005-12-06 13:27:15 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* save the char codes of the loaded glyphs to a file */
|
2011-11-27 15:28:16 +00:00
|
|
|
static void glyph_cache_save(int font_id)
|
2005-12-06 13:27:15 +00:00
|
|
|
{
|
2011-10-16 13:18:46 +00:00
|
|
|
int fd;
|
|
|
|
|
|
|
|
if( font_id < 0 )
|
2011-09-24 13:19:34 +00:00
|
|
|
return;
|
2011-10-16 13:18:46 +00:00
|
|
|
int handle = buflib_allocations[font_id];
|
|
|
|
if ( handle < 0 )
|
|
|
|
return;
|
|
|
|
|
|
|
|
struct font *pf = pf_from_handle(handle);
|
|
|
|
if(pf && pf->fd >= 0)
|
2010-02-14 06:26:16 +00:00
|
|
|
{
|
2011-10-16 13:18:46 +00:00
|
|
|
char filename[MAX_PATH];
|
|
|
|
font_path_to_glyph_path(font_filename(font_id), filename);
|
|
|
|
fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0666);
|
|
|
|
if (fd < 0)
|
2010-08-01 16:15:27 +00:00
|
|
|
return;
|
2005-12-06 13:27:15 +00:00
|
|
|
|
2011-10-16 13:18:46 +00:00
|
|
|
cache_pf = pf;
|
|
|
|
cache_fd = fd;
|
|
|
|
lru_traverse(&cache_pf->cache._lru, glyph_file_write);
|
|
|
|
glyph_file_write(NULL);
|
|
|
|
if (cache_fd >= 0)
|
2010-02-14 06:26:16 +00:00
|
|
|
{
|
|
|
|
close(cache_fd);
|
|
|
|
cache_fd = -1;
|
|
|
|
}
|
2005-12-06 13:27:15 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-08-25 14:11:38 +00:00
|
|
|
|
2010-08-23 18:17:54 +00:00
|
|
|
static int ushortcmp(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
return ((int)(*(unsigned short*)a - *(unsigned short*)b));
|
|
|
|
}
|
2011-11-19 23:34:26 +00:00
|
|
|
static void glyph_cache_load(const char *font_path, struct font *pf)
|
2005-12-06 13:27:15 +00:00
|
|
|
{
|
2010-08-23 18:17:54 +00:00
|
|
|
#define MAX_SORT 256
|
2011-10-16 13:18:46 +00:00
|
|
|
if (pf->fd >= 0) {
|
|
|
|
int i, size, fd;
|
2005-12-06 13:27:15 +00:00
|
|
|
unsigned char tmp[2];
|
|
|
|
unsigned short ch;
|
2010-08-23 18:17:54 +00:00
|
|
|
unsigned short glyphs[MAX_SORT];
|
|
|
|
unsigned short glyphs_lru_order[MAX_SORT];
|
|
|
|
int glyph_file_skip=0, glyph_file_size=0;
|
|
|
|
|
|
|
|
int sort_size = pf->cache._capacity;
|
|
|
|
if ( sort_size > MAX_SORT )
|
|
|
|
sort_size = MAX_SORT;
|
2005-12-06 13:27:15 +00:00
|
|
|
|
2011-10-16 13:18:46 +00:00
|
|
|
char filename[MAX_PATH];
|
2011-11-19 23:34:26 +00:00
|
|
|
font_path_to_glyph_path(font_path, filename);
|
2011-10-16 13:18:46 +00:00
|
|
|
|
|
|
|
fd = open(filename, O_RDONLY|O_BINARY);
|
|
|
|
#ifdef TRY_DEFAULT_GLYPHCACHE
|
|
|
|
/* if font specific file fails, try default */
|
|
|
|
if (fd < 0)
|
|
|
|
fd = open(GLYPH_CACHE_FILE, O_RDONLY|O_BINARY);
|
|
|
|
#endif
|
2005-12-06 13:27:15 +00:00
|
|
|
if (fd >= 0) {
|
2010-08-23 18:17:54 +00:00
|
|
|
/* only read what fits */
|
|
|
|
glyph_file_size = filesize( fd );
|
|
|
|
if ( glyph_file_size > 2*pf->cache._capacity ) {
|
|
|
|
glyph_file_skip = glyph_file_size - 2*pf->cache._capacity;
|
|
|
|
lseek( fd, glyph_file_skip, SEEK_SET );
|
|
|
|
}
|
2005-12-06 13:27:15 +00:00
|
|
|
|
2010-08-23 18:17:54 +00:00
|
|
|
while(1) {
|
|
|
|
|
|
|
|
for ( size = 0;
|
|
|
|
read( fd, tmp, 2 ) == 2 && size < sort_size;
|
|
|
|
size++ )
|
|
|
|
{
|
|
|
|
glyphs[size] = (tmp[0] << 8) | tmp[1];
|
|
|
|
glyphs_lru_order[size] = glyphs[size];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* sort glyphs array to make sector cache happy */
|
|
|
|
qsort((void *)glyphs, size, sizeof(unsigned short),
|
|
|
|
ushortcmp );
|
|
|
|
|
|
|
|
/* load font bitmaps */
|
2011-10-16 13:18:46 +00:00
|
|
|
for( i = 0; i < size ; i++ )
|
2010-08-23 18:17:54 +00:00
|
|
|
font_get_bits(pf, glyphs[i]);
|
|
|
|
|
|
|
|
/* redo to fix lru order */
|
|
|
|
for ( i = 0; i < size ; i++)
|
|
|
|
font_get_bits(pf, glyphs_lru_order[i]);
|
|
|
|
|
|
|
|
if ( size < sort_size )
|
|
|
|
break;
|
2005-12-06 13:27:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
} else {
|
|
|
|
/* load latin1 chars into cache */
|
2010-08-27 15:10:30 +00:00
|
|
|
for ( ch = 32 ; ch < 256 && ch < pf->cache._capacity + 32; ch++ )
|
2005-12-06 13:27:15 +00:00
|
|
|
font_get_bits(pf, ch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2008-09-23 21:35:54 +00:00
|
|
|
#else /* BOOTLOADER */
|
|
|
|
|
|
|
|
void font_init(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-10-21 20:15:26 +00:00
|
|
|
void font_lock(int font_id, bool lock)
|
|
|
|
{
|
|
|
|
(void)font_id;
|
|
|
|
(void)lock;
|
|
|
|
}
|
|
|
|
|
2008-09-23 21:35:54 +00:00
|
|
|
/*
|
|
|
|
* Bootloader only supports the built-in sysfont.
|
|
|
|
*/
|
|
|
|
struct font* font_get(int font)
|
|
|
|
{
|
|
|
|
(void)font;
|
2010-01-03 10:50:34 +00:00
|
|
|
return &sysfont;
|
2008-09-23 21:35:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns width of character
|
|
|
|
*/
|
|
|
|
int font_get_width(struct font* pf, unsigned short char_code)
|
|
|
|
{
|
|
|
|
/* check input range*/
|
|
|
|
if (char_code < pf->firstchar || char_code >= pf->firstchar+pf->size)
|
|
|
|
char_code = pf->defaultchar;
|
|
|
|
char_code -= pf->firstchar;
|
|
|
|
|
|
|
|
return pf->width? pf->width[char_code]: pf->maxwidth;
|
|
|
|
}
|
2010-02-26 07:16:32 +00:00
|
|
|
|
2008-09-23 21:35:54 +00:00
|
|
|
const unsigned char* font_get_bits(struct font* pf, unsigned short char_code)
|
|
|
|
{
|
|
|
|
const unsigned char* bits;
|
|
|
|
|
|
|
|
/* check input range*/
|
|
|
|
if (char_code < pf->firstchar || char_code >= pf->firstchar+pf->size)
|
|
|
|
char_code = pf->defaultchar;
|
|
|
|
char_code -= pf->firstchar;
|
|
|
|
|
2009-12-13 11:07:40 +00:00
|
|
|
/* assume small font with uint16_t offsets*/
|
2008-09-23 21:35:54 +00:00
|
|
|
bits = pf->bits + (pf->offset?
|
2009-12-13 11:19:06 +00:00
|
|
|
((uint16_t*)(pf->offset))[char_code]:
|
2008-09-23 21:35:54 +00:00
|
|
|
(((pf->height + 7) / 8) * pf->maxwidth * char_code));
|
2010-02-26 07:16:32 +00:00
|
|
|
|
2008-09-23 21:35:54 +00:00
|
|
|
return bits;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* BOOTLOADER */
|
2002-09-13 06:37:49 +00:00
|
|
|
|
2008-09-23 21:35:54 +00:00
|
|
|
/*
|
|
|
|
* Returns the stringsize of a given string.
|
|
|
|
*/
|
|
|
|
int font_getstringsize(const unsigned char *str, int *w, int *h, int fontnumber)
|
|
|
|
{
|
|
|
|
struct font* pf = font_get(fontnumber);
|
|
|
|
unsigned short ch;
|
|
|
|
int width = 0;
|
|
|
|
|
2011-10-21 18:05:52 +00:00
|
|
|
font_lock( fontnumber, true );
|
2008-09-23 21:35:54 +00:00
|
|
|
for (str = utf8decode(str, &ch); ch != 0 ; str = utf8decode(str, &ch))
|
|
|
|
{
|
2009-11-24 20:41:42 +00:00
|
|
|
if (is_diacritic(ch, NULL))
|
|
|
|
continue;
|
2008-09-23 21:35:54 +00:00
|
|
|
|
|
|
|
/* get proportional width and glyph bits*/
|
|
|
|
width += font_get_width(pf,ch);
|
|
|
|
}
|
|
|
|
if ( w )
|
|
|
|
*w = width;
|
|
|
|
if ( h )
|
|
|
|
*h = pf->height;
|
2011-10-21 18:05:52 +00:00
|
|
|
font_lock( fontnumber, false );
|
2008-09-23 21:35:54 +00:00
|
|
|
return width;
|
|
|
|
}
|
2002-09-12 13:33:59 +00:00
|
|
|
|
|
|
|
/* -----------------------------------------------------------------
|
Daniel,
The following patch makes loadable fonts actually work (finally!).
It took me quite a while, but I finally figured out why the sim
worked and the target didn't: the SH1 processor won't read
longwords from a shortword alignment... I had to rev the .fnt
file to version 1.1 (requires remaking *.fnt files) in order
to fix this. Please apply the following patch completely.
It's diffed against the latest CVS.
I've also attached rockbox-fonts-1.1.tar.gz which includes
known working *.fnt files, including a courB08 system.fnt,
for demonstration.
Now the real work can begin... Although the new
system.fnt will work fine, if you try going to a really
big font (try copying courB14.fnt to system.fnt), then
you will find that it comes up and works in tree mode,
but will crash the system when going into WPS
mode... I'm sure this is because of the low-level
lcd_bitmap not clipping properly when given a too-large
bitmap, which the characters become. I haven't yet
tried to debug the low-level driver. Of course, it all
works on the sim...
So the apps developers will now have to make sure that
all apps screen sizes may vary according to the loaded font.
The font height can be gotten through the lcd_getfontsize API.
Files patched in fonts-6.patch
1. apps/menu.c - LCD_PROPFONTS error (2nd resubmission on this, please checkin)
2. firmware/font.c - fixes and reformatting. Please check this in as is,
my vi editor requires more reformatting changes since I left tabs in the
file, these are removed now (2nd resubmission on this, please checkin)
3. firmware/fonts.h - doc change on .fnt file format, .fnt version
number incremented.
4. firmware/loadfont.c - fixes to load font properly, typedefs
removed.
5. firmware/system.c - lcd_setfont(FONT_SYSFIXED) before
issuing error, otherwise font may not exist.
6. tools/bdf2c - fixes for correct output when filename starts
with a number, as well as when no DEFAULT_CHAR in .bdf
file. (2nd resubmission on this, please checkin)
7. tools/writerbf.c - fixes for bugfixed fontfile format.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@2294 a1c6a512-1295-4272-9138-f99709370657
2002-09-16 03:18:49 +00:00
|
|
|
* vim: et sw=4 ts=8 sts=4 tw=78
|
2002-09-12 13:33:59 +00:00
|
|
|
*/
|