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 "config.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2007-06-30 17:54:02 +00:00
|
|
|
#include "inttypes.h"
|
2002-09-12 13:33:59 +00:00
|
|
|
#include "lcd.h"
|
|
|
|
#include "font.h"
|
2002-09-20 08:07:51 +00:00
|
|
|
#include "file.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"
|
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
|
|
|
|
#if MEM > 2
|
|
|
|
#define MAX_FONT_SIZE 60000
|
|
|
|
#else
|
|
|
|
#define MAX_FONT_SIZE 10000
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
#define MAX_FONT_SIZE 4000
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef FONT_HEADER_SIZE
|
|
|
|
#define FONT_HEADER_SIZE 36
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define GLYPH_CACHE_FILE ROCKBOX_DIR"/.glyphcache"
|
|
|
|
|
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
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
|
2002-09-20 08:07:51 +00:00
|
|
|
/* structure filled in by font_load */
|
|
|
|
static struct font font_ui;
|
2010-02-14 06:26:16 +00:00
|
|
|
/* static buffer allocation structures */
|
|
|
|
static unsigned char main_buf[MAX_FONT_SIZE];
|
|
|
|
#ifdef HAVE_REMOTE_LCD
|
|
|
|
#define REMOTE_FONT_SIZE 10000
|
|
|
|
static struct font remote_font_ui;
|
|
|
|
static unsigned char remote_buf[REMOTE_FONT_SIZE];
|
|
|
|
#endif
|
2002-09-12 13:33:59 +00:00
|
|
|
|
2002-09-20 08:07:51 +00:00
|
|
|
/* system font table, in order of FONT_xxx definition */
|
2010-02-14 06:26:16 +00:00
|
|
|
static struct font* sysfonts[MAXFONTS] = { &sysfont, &font_ui, NULL};
|
2002-09-12 13:33:59 +00:00
|
|
|
|
2002-09-13 06:37:49 +00:00
|
|
|
|
2005-12-06 13:27:15 +00:00
|
|
|
/* Font cache structures */
|
2010-02-14 06:26:16 +00:00
|
|
|
static void cache_create(struct font* pf, int maxwidth, int height);
|
|
|
|
static void glyph_cache_load(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
|
|
|
{
|
2010-02-14 06:26:16 +00:00
|
|
|
int i = SYSTEMFONTCOUNT;
|
|
|
|
while (i<MAXFONTS)
|
|
|
|
sysfonts[i++] = NULL;
|
|
|
|
font_reset(NULL);
|
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
|
|
|
}
|
|
|
|
|
2010-02-14 06:26:16 +00:00
|
|
|
void font_reset(struct font *pf)
|
2002-10-09 23:13:25 +00:00
|
|
|
{
|
2010-02-14 06:26:16 +00:00
|
|
|
unsigned char* buffer = NULL;
|
|
|
|
size_t buf_size = 0;
|
|
|
|
if (pf == NULL)
|
|
|
|
pf = &font_ui;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buffer = pf->buffer_start;
|
|
|
|
buf_size = pf->buffer_size;
|
|
|
|
}
|
|
|
|
memset(pf, 0, sizeof(struct font));
|
|
|
|
pf->fd = -1;
|
|
|
|
if (buffer)
|
|
|
|
{
|
|
|
|
pf->buffer_start = buffer;
|
|
|
|
pf->buffer_size = buf_size;
|
|
|
|
}
|
2002-10-09 23:13:25 +00:00
|
|
|
}
|
|
|
|
|
2009-12-13 11:07:40 +00:00
|
|
|
static struct font* font_load_header(struct font *pf)
|
2002-09-20 08:07:51 +00:00
|
|
|
{
|
2007-04-13 19:51:19 +00:00
|
|
|
/* Check we have enough data */
|
|
|
|
if (!HAVEBYTES(28))
|
|
|
|
return NULL;
|
2002-09-12 13:33:59 +00:00
|
|
|
|
2002-09-20 08:07:51 +00:00
|
|
|
/* read magic and version #*/
|
2010-02-14 06:26:16 +00:00
|
|
|
if (memcmp(pf->buffer_position, VERSION, 4) != 0)
|
2002-09-20 08:07:51 +00:00
|
|
|
return NULL;
|
|
|
|
|
2010-02-14 06:26:16 +00:00
|
|
|
pf->buffer_position += 4;
|
2009-12-13 11:07:40 +00:00
|
|
|
|
2002-09-20 08:07:51 +00:00
|
|
|
/* font info*/
|
2010-02-14 06:26:16 +00:00
|
|
|
pf->maxwidth = readshort(pf);
|
|
|
|
pf->height = readshort(pf);
|
|
|
|
pf->ascent = readshort(pf);
|
|
|
|
pf->buffer_position += 2; /* Skip padding */
|
|
|
|
pf->firstchar = readlong(pf);
|
|
|
|
pf->defaultchar = readlong(pf);
|
|
|
|
pf->size = readlong(pf);
|
2002-09-20 08:07:51 +00:00
|
|
|
|
|
|
|
/* get variable font data sizes*/
|
|
|
|
/* # words of bitmap_t*/
|
2010-02-14 06:26:16 +00:00
|
|
|
pf->bits_size = readlong(pf);
|
2005-12-06 13:27:15 +00:00
|
|
|
|
|
|
|
return pf;
|
|
|
|
}
|
|
|
|
/* Load memory font */
|
2007-06-30 17:54:02 +00:00
|
|
|
static struct font* font_load_in_memory(struct font* pf)
|
2005-12-06 13:27:15 +00:00
|
|
|
{
|
2007-06-30 17:54:02 +00:00
|
|
|
int32_t i, noffset, nwidth;
|
2002-09-20 08:07:51 +00:00
|
|
|
|
2007-04-13 19:51:19 +00:00
|
|
|
if (!HAVEBYTES(4))
|
2002-09-20 08:07:51 +00:00
|
|
|
return NULL;
|
|
|
|
|
2007-04-13 19:51:19 +00:00
|
|
|
/* # longs of offset*/
|
2010-02-14 06:26:16 +00:00
|
|
|
noffset = readlong(pf);
|
2007-04-13 19:51:19 +00:00
|
|
|
|
2002-09-20 08:07:51 +00:00
|
|
|
/* # bytes of width*/
|
2010-02-14 06:26:16 +00:00
|
|
|
nwidth = readlong(pf);
|
2002-09-20 08:07:51 +00:00
|
|
|
|
|
|
|
/* variable font data*/
|
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 */
|
2007-06-30 17:54:02 +00:00
|
|
|
static struct font* font_load_cached(struct font* pf)
|
2005-12-06 13:27:15 +00:00
|
|
|
{
|
2007-06-30 17:54:02 +00:00
|
|
|
uint32_t noffset, nwidth;
|
2010-02-14 06:26:16 +00:00
|
|
|
unsigned char* oldfileptr = pf->buffer_position;
|
2005-12-06 13:27:15 +00:00
|
|
|
|
2007-06-30 17:54:02 +00:00
|
|
|
if (!HAVEBYTES(2 * sizeof(int32_t)))
|
2005-12-06 13:27:15 +00:00
|
|
|
return NULL;
|
|
|
|
|
2007-04-13 19:51:19 +00:00
|
|
|
/* # longs of offset*/
|
2010-02-14 06:26:16 +00:00
|
|
|
noffset = readlong(pf);
|
2007-04-13 19:51:19 +00:00
|
|
|
|
2005-12-06 13:27:15 +00:00
|
|
|
/* # bytes of width*/
|
2010-02-14 06:26:16 +00:00
|
|
|
nwidth = readlong(pf);
|
2005-12-06 13:27:15 +00:00
|
|
|
|
|
|
|
/* We are now at the bitmap data, this is fixed at 36.. */
|
|
|
|
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
|
|
|
|
2010-02-14 06:26:16 +00:00
|
|
|
pf->buffer_position = oldfileptr;
|
2005-12-06 13:27:15 +00:00
|
|
|
|
|
|
|
/* Create the cache */
|
2010-02-14 06:26:16 +00:00
|
|
|
cache_create(pf, pf->maxwidth, pf->height);
|
2005-12-06 13:27:15 +00:00
|
|
|
|
|
|
|
return pf;
|
|
|
|
}
|
|
|
|
|
2010-02-14 06:26:16 +00:00
|
|
|
static bool internal_load_font(struct font* pf, const char *path,
|
|
|
|
char *buf, size_t buf_size)
|
2005-12-06 13:27:15 +00:00
|
|
|
{
|
2006-04-24 16:02:44 +00:00
|
|
|
int size;
|
2010-02-14 06:26:16 +00:00
|
|
|
|
2005-12-06 13:27:15 +00:00
|
|
|
/* save loaded glyphs */
|
2010-02-14 06:26:16 +00:00
|
|
|
glyph_cache_save(pf);
|
2005-12-06 13:27:15 +00:00
|
|
|
/* Close font file handle */
|
2010-02-14 06:26:16 +00:00
|
|
|
if (pf->fd >= 0)
|
|
|
|
close(pf->fd);
|
|
|
|
|
|
|
|
font_reset(pf);
|
2005-12-06 13:27:15 +00:00
|
|
|
|
|
|
|
/* open and read entire font file*/
|
2010-02-14 06:26:16 +00:00
|
|
|
pf->fd = open(path, O_RDONLY|O_BINARY);
|
2005-12-06 13:27:15 +00:00
|
|
|
|
2010-02-14 06:26:16 +00:00
|
|
|
if (pf->fd < 0) {
|
2005-12-06 13:27:15 +00:00
|
|
|
DEBUGF("Can't open font: %s\n", path);
|
2010-02-14 06:26:16 +00:00
|
|
|
return false;
|
2005-12-06 13:27:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check file size */
|
2010-02-14 06:26:16 +00:00
|
|
|
size = filesize(pf->fd);
|
|
|
|
pf->buffer_start = buf;
|
|
|
|
pf->buffer_size = buf_size;
|
|
|
|
|
|
|
|
pf->buffer_position = buf;
|
|
|
|
|
|
|
|
if (size > pf->buffer_size)
|
2005-12-06 13:27:15 +00:00
|
|
|
{
|
2010-02-14 06:26:16 +00:00
|
|
|
read(pf->fd, pf->buffer_position, FONT_HEADER_SIZE);
|
|
|
|
pf->buffer_end = pf->buffer_position + FONT_HEADER_SIZE;
|
2005-12-06 13:27:15 +00:00
|
|
|
|
|
|
|
if (!font_load_header(pf))
|
|
|
|
{
|
|
|
|
DEBUGF("Failed font header load");
|
2010-02-14 06:26:16 +00:00
|
|
|
return false;
|
2005-12-06 13:27:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!font_load_cached(pf))
|
|
|
|
{
|
|
|
|
DEBUGF("Failed font cache load");
|
2010-02-14 06:26:16 +00:00
|
|
|
return false;
|
2005-12-06 13:27:15 +00:00
|
|
|
}
|
|
|
|
|
2010-02-14 06:26:16 +00:00
|
|
|
glyph_cache_load(pf);
|
2005-12-06 13:27:15 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-02-14 06:26:16 +00:00
|
|
|
read(pf->fd, pf->buffer_position, pf->buffer_size);
|
|
|
|
pf->buffer_end = pf->buffer_position + size;
|
|
|
|
close(pf->fd);
|
|
|
|
pf->fd = -1;
|
2005-12-06 13:27:15 +00:00
|
|
|
|
|
|
|
if (!font_load_header(pf))
|
|
|
|
{
|
|
|
|
DEBUGF("Failed font header load");
|
2010-02-14 06:26:16 +00:00
|
|
|
return false;
|
2005-12-06 13:27:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!font_load_in_memory(pf))
|
|
|
|
{
|
|
|
|
DEBUGF("Failed mem load");
|
2010-02-14 06:26:16 +00:00
|
|
|
return false;
|
2005-12-06 13:27:15 +00:00
|
|
|
}
|
|
|
|
}
|
2010-02-14 06:26:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_REMOTE_LCD
|
|
|
|
/* Load a font into the special remote ui font slot */
|
|
|
|
int font_load_remoteui(const char* path)
|
|
|
|
{
|
|
|
|
struct font* pf = &remote_font_ui;
|
|
|
|
if (!path)
|
|
|
|
{
|
|
|
|
if (sysfonts[FONT_UI_REMOTE] && sysfonts[FONT_UI_REMOTE] != sysfonts[FONT_UI])
|
|
|
|
font_unload(FONT_UI_REMOTE);
|
|
|
|
sysfonts[FONT_UI_REMOTE] = NULL;
|
|
|
|
return FONT_UI;
|
|
|
|
}
|
|
|
|
if (!internal_load_font(pf, path, remote_buf, REMOTE_FONT_SIZE))
|
|
|
|
{
|
|
|
|
sysfonts[FONT_UI_REMOTE] = NULL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sysfonts[FONT_UI_REMOTE] = pf;
|
|
|
|
return FONT_UI_REMOTE;
|
|
|
|
}
|
|
|
|
#endif
|
2005-12-06 13:27:15 +00:00
|
|
|
|
2010-02-14 06:26:16 +00:00
|
|
|
/* read and load font into incore font structure,
|
|
|
|
* returns the font number on success, -1 on failure */
|
|
|
|
int font_load(struct font* pf, const char *path)
|
|
|
|
{
|
|
|
|
int font_id = -1;
|
|
|
|
char *buffer;
|
|
|
|
size_t buffer_size;
|
|
|
|
if (pf == NULL)
|
|
|
|
{
|
|
|
|
pf = &font_ui;
|
|
|
|
font_id = FONT_UI;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (font_id = SYSTEMFONTCOUNT; font_id < MAXFONTS; font_id++)
|
|
|
|
{
|
|
|
|
if (sysfonts[font_id] == NULL)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (font_id == MAXFONTS)
|
|
|
|
return -1; /* too many fonts */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (font_id == FONT_UI)
|
|
|
|
{
|
|
|
|
/* currently, font loading replaces earlier font allocation*/
|
|
|
|
buffer = (unsigned char *)(((intptr_t)main_buf + 3) & ~3);
|
|
|
|
buffer_size = MAX_FONT_SIZE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buffer = pf->buffer_start;
|
|
|
|
buffer_size = pf->buffer_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!internal_load_font(pf, path, buffer, buffer_size))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
sysfonts[font_id] = pf;
|
|
|
|
return font_id; /* success!*/
|
|
|
|
}
|
2005-12-06 13:27:15 +00:00
|
|
|
|
2010-02-14 06:26:16 +00:00
|
|
|
void font_unload(int font_id)
|
|
|
|
{
|
|
|
|
struct font* pf = sysfonts[font_id];
|
|
|
|
if (font_id >= SYSTEMFONTCOUNT && pf)
|
|
|
|
{
|
|
|
|
if (pf->fd >= 0)
|
|
|
|
close(pf->fd);
|
|
|
|
sysfonts[font_id] = NULL;
|
|
|
|
}
|
2005-12-06 13:27:15 +00:00
|
|
|
}
|
|
|
|
|
2002-09-13 06:37:49 +00:00
|
|
|
/*
|
2002-09-20 08:07:51 +00:00
|
|
|
* Return a pointer to an incore font structure.
|
|
|
|
* If the requested font isn't loaded/compiled-in,
|
|
|
|
* decrement the font number and try again.
|
2002-09-13 06:37:49 +00:00
|
|
|
*/
|
2002-09-20 08:07:51 +00:00
|
|
|
struct font* font_get(int font)
|
2002-09-13 06:37:49 +00:00
|
|
|
{
|
2002-09-20 08:07:51 +00:00
|
|
|
struct font* pf;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
pf = sysfonts[font];
|
|
|
|
if (pf && pf->height)
|
|
|
|
return pf;
|
|
|
|
if (--font < 0)
|
|
|
|
panicf("No font!");
|
2002-09-13 06:37:49 +00:00
|
|
|
}
|
|
|
|
}
|
2005-04-19 12:47:16 +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;
|
|
|
|
unsigned short char_code = p->_char_code;
|
|
|
|
unsigned char tmp[2];
|
|
|
|
|
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;
|
|
|
|
lseek(pf->fd, width_offset, SEEK_SET);
|
|
|
|
read(pf->fd, &(p->width), 1);
|
2005-12-06 13:27:15 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
p->width = pf->maxwidth;
|
|
|
|
}
|
|
|
|
|
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));
|
|
|
|
lseek(pf->fd, offset, SEEK_SET);
|
|
|
|
read (pf->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) {
|
|
|
|
read (pf->fd, tmp, 2);
|
2005-12-06 13:27:15 +00:00
|
|
|
bitmap_offset |= (tmp[0] << 16) | (tmp[1] << 24);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bitmap_offset = ((pf->height + 7) / 8) * p->width * char_code;
|
|
|
|
}
|
|
|
|
|
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);
|
2005-12-06 13:27:15 +00:00
|
|
|
|
|
|
|
int src_bytes = p->width * ((pf->height + 7) / 8);
|
2010-02-14 06:26:16 +00:00
|
|
|
read(pf->fd, p->bitmap, src_bytes);
|
2005-12-06 13:27:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Converts cbuf into a font cache
|
|
|
|
*/
|
2010-02-14 06:26:16 +00:00
|
|
|
static void cache_create(struct font* pf, int maxwidth, int height)
|
2005-12-06 13:27:15 +00:00
|
|
|
{
|
|
|
|
/* maximum size of rotated bitmap */
|
|
|
|
int bitmap_size = maxwidth * ((height + 7) / 8);
|
|
|
|
|
|
|
|
/* Initialise cache */
|
2010-02-14 06:26:16 +00:00
|
|
|
font_cache_create(&pf->cache, pf->buffer_start, pf->buffer_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)
|
|
|
|
{
|
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
|
|
|
return (pf->fd >= 0 && pf != &sysfont)?
|
|
|
|
font_cache_get(&pf->cache,char_code,load_cache_entry,pf)->width:
|
2005-12-06 13:27:15 +00:00
|
|
|
pf->width? pf->width[char_code]: pf->maxwidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
bits =
|
2010-02-14 06:26:16 +00:00
|
|
|
(unsigned char*)font_cache_get(&pf->cache,char_code,load_cache_entry,pf)->bitmap;
|
2005-12-06 13:27:15 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
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
|
|
|
|
bits += ((pf->height + 7) / 8) * pf->maxwidth * char_code;
|
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;
|
|
|
|
}
|
2010-02-14 06:26:16 +00:00
|
|
|
static int cache_fd;
|
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;
|
2006-03-22 09:53:27 +00:00
|
|
|
struct font* pf = &font_ui;
|
|
|
|
unsigned short ch;
|
2005-12-06 13:27:15 +00:00
|
|
|
unsigned char tmp[2];
|
|
|
|
|
2006-03-22 09:53:27 +00:00
|
|
|
ch = p->_char_code + pf->firstchar;
|
|
|
|
|
2010-02-14 06:26:16 +00:00
|
|
|
if (ch != 0xffff && cache_fd >= 0) {
|
2006-03-22 09:53:27 +00:00
|
|
|
tmp[0] = ch >> 8;
|
|
|
|
tmp[1] = ch & 0xff;
|
2010-02-14 06:26:16 +00:00
|
|
|
if (write(cache_fd, tmp, 2) != 2) {
|
|
|
|
close(cache_fd);
|
|
|
|
cache_fd = -1;
|
2005-12-06 13:27:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* save the char codes of the loaded glyphs to a file */
|
2010-02-14 06:26:16 +00:00
|
|
|
void glyph_cache_save(struct font* pf)
|
2005-12-06 13:27:15 +00:00
|
|
|
{
|
2010-02-14 06:26:16 +00:00
|
|
|
if (!pf)
|
|
|
|
pf = &font_ui;
|
|
|
|
if (pf->fd >= 0 && pf == &font_ui)
|
|
|
|
{
|
2008-08-29 21:08:38 +00:00
|
|
|
#ifdef WPSEDITOR
|
2010-02-14 06:26:16 +00:00
|
|
|
cache_fd = open(GLYPH_CACHE_FILE, O_WRONLY|O_CREAT|O_TRUNC);
|
2008-08-29 21:08:38 +00:00
|
|
|
#else
|
2010-02-14 06:26:16 +00:00
|
|
|
cache_fd = creat(GLYPH_CACHE_FILE);
|
2008-08-29 21:08:38 +00:00
|
|
|
#endif
|
2010-02-14 06:26:16 +00:00
|
|
|
if (cache_fd < 0) return;
|
2005-12-06 13:27:15 +00:00
|
|
|
|
2010-02-14 06:26:16 +00:00
|
|
|
lru_traverse(&pf->cache._lru, glyph_file_write);
|
|
|
|
|
|
|
|
if (cache_fd < 0)
|
|
|
|
{
|
|
|
|
close(cache_fd);
|
|
|
|
cache_fd = -1;
|
|
|
|
}
|
2005-12-06 13:27:15 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-02-14 06:26:16 +00:00
|
|
|
static void glyph_cache_load(struct font* pf)
|
2005-12-06 13:27:15 +00:00
|
|
|
{
|
2010-02-14 06:26:16 +00:00
|
|
|
if (pf->fd >= 0) {
|
2005-12-06 13:27:15 +00:00
|
|
|
int fd;
|
|
|
|
unsigned char tmp[2];
|
|
|
|
unsigned short ch;
|
|
|
|
|
|
|
|
fd = open(GLYPH_CACHE_FILE, O_RDONLY|O_BINARY);
|
|
|
|
|
|
|
|
if (fd >= 0) {
|
|
|
|
|
|
|
|
while (read(fd, tmp, 2) == 2) {
|
|
|
|
ch = (tmp[0] << 8) | tmp[1];
|
|
|
|
font_get_bits(pf, ch);
|
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
} else {
|
|
|
|
/* load latin1 chars into cache */
|
2006-03-22 09:53:27 +00:00
|
|
|
ch = 256;
|
|
|
|
while (ch-- > 32)
|
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)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
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;
|
|
|
|
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
|
|
|
*/
|