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
|
|
|
|
|
|
|
#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;
|
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 */
|
2004-08-03 05:58:46 +00:00
|
|
|
static struct font* const sysfonts[MAXFONTS] = { &sysfont, &font_ui };
|
2002-09-12 13:33:59 +00:00
|
|
|
|
2002-09-20 08:07:51 +00:00
|
|
|
/* static buffer allocation structures */
|
|
|
|
static unsigned char mbuf[MAX_FONT_SIZE];
|
|
|
|
static unsigned char *freeptr = mbuf;
|
|
|
|
static unsigned char *fileptr;
|
|
|
|
static unsigned char *eofptr;
|
2002-09-13 06:37:49 +00:00
|
|
|
|
2005-12-06 13:27:15 +00:00
|
|
|
/* Font cache structures */
|
|
|
|
static struct font_cache font_cache_ui;
|
|
|
|
static int fnt_file = -1; /* >=0 if font is cached */
|
2008-04-20 10:24:15 +00:00
|
|
|
static uint32_t file_width_offset; /* offset to file width data */
|
|
|
|
static uint32_t file_offset_offset; /* offset to file offset data */
|
2005-12-06 13:27:15 +00:00
|
|
|
static void cache_create(int maxwidth, int height);
|
|
|
|
static int long_offset = 0;
|
|
|
|
static int glyph_file;
|
|
|
|
/* End Font cache structures */
|
|
|
|
|
2007-06-30 17:54:02 +00:00
|
|
|
static void glyph_cache_load(void);
|
|
|
|
|
2002-09-20 08:07:51 +00:00
|
|
|
void font_init(void)
|
2002-09-12 13:33:59 +00:00
|
|
|
{
|
2002-09-20 08:07:51 +00:00
|
|
|
memset(&font_ui, 0, sizeof(struct font));
|
|
|
|
}
|
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 */
|
|
|
|
#define HAVEBYTES(x) (fileptr + (x) <= eofptr)
|
|
|
|
|
|
|
|
/* Helper functions to read big-endian unaligned short or long from
|
|
|
|
the file buffer. Bounds-checking must be done in the calling
|
|
|
|
function.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static short readshort(void)
|
2002-09-20 08:07:51 +00:00
|
|
|
{
|
|
|
|
unsigned short s;
|
|
|
|
|
|
|
|
s = *fileptr++ & 0xff;
|
2007-04-13 19:51:19 +00:00
|
|
|
s |= (*fileptr++ << 8);
|
|
|
|
return s;
|
2002-09-12 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
2007-06-30 17:54:02 +00:00
|
|
|
static int32_t readlong(void)
|
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
|
|
|
|
2002-09-20 08:07:51 +00:00
|
|
|
l = *fileptr++ & 0xff;
|
|
|
|
l |= *fileptr++ << 8;
|
2007-06-30 17:54:02 +00:00
|
|
|
l |= ((uint32_t)(*fileptr++)) << 16;
|
|
|
|
l |= ((uint32_t)(*fileptr++)) << 24;
|
2007-04-13 19:51:19 +00:00
|
|
|
return l;
|
2002-09-12 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
2002-09-20 08:07:51 +00:00
|
|
|
/* read count bytes*/
|
2007-04-13 19:51:19 +00:00
|
|
|
static void readstr(char *buf, int count)
|
2002-09-12 13:33:59 +00:00
|
|
|
{
|
2007-04-13 19:51:19 +00:00
|
|
|
while (count--)
|
2002-09-20 08:07:51 +00:00
|
|
|
*buf++ = *fileptr++;
|
2002-09-12 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
2002-10-09 23:13:25 +00:00
|
|
|
void font_reset(void)
|
|
|
|
{
|
|
|
|
memset(&font_ui, 0, sizeof(struct font));
|
|
|
|
}
|
|
|
|
|
2005-12-06 13:27:15 +00:00
|
|
|
static struct font* font_load_header(struct font *pf)
|
2002-09-20 08:07:51 +00:00
|
|
|
{
|
2005-12-06 13:27:15 +00:00
|
|
|
char version[4+1];
|
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 #*/
|
|
|
|
memset(version, 0, sizeof(version));
|
2007-04-13 19:51:19 +00:00
|
|
|
readstr(version, 4);
|
|
|
|
|
2002-09-20 08:07:51 +00:00
|
|
|
if (strcmp(version, VERSION) != 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* font info*/
|
2007-04-13 19:51:19 +00:00
|
|
|
pf->maxwidth = readshort();
|
|
|
|
pf->height = readshort();
|
|
|
|
pf->ascent = readshort();
|
|
|
|
fileptr += 2; /* Skip padding */
|
|
|
|
pf->firstchar = readlong();
|
|
|
|
pf->defaultchar = readlong();
|
|
|
|
pf->size = readlong();
|
2002-09-20 08:07:51 +00:00
|
|
|
|
|
|
|
/* get variable font data sizes*/
|
|
|
|
/* # words of bitmap_t*/
|
2007-04-13 19:51:19 +00:00
|
|
|
pf->bits_size = readlong();
|
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*/
|
|
|
|
noffset = readlong();
|
|
|
|
|
2002-09-20 08:07:51 +00:00
|
|
|
/* # bytes of width*/
|
2007-04-13 19:51:19 +00:00
|
|
|
nwidth = readlong();
|
2002-09-20 08:07:51 +00:00
|
|
|
|
|
|
|
/* variable font data*/
|
2004-08-26 21:15:07 +00:00
|
|
|
pf->bits = (unsigned char *)fileptr;
|
2005-12-06 13:27:15 +00:00
|
|
|
fileptr += pf->bits_size*sizeof(unsigned char);
|
2004-08-26 21:15:07 +00:00
|
|
|
|
2005-12-06 13:27:15 +00:00
|
|
|
if ( pf->bits_size < 0xFFDB )
|
|
|
|
{
|
|
|
|
/* pad to 16-bit boundary */
|
2007-06-30 18:09:46 +00:00
|
|
|
fileptr = (unsigned char *)(((intptr_t)fileptr + 1) & ~1);
|
2005-12-06 13:27:15 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* pad to 32-bit boundary*/
|
2007-06-30 18:09:46 +00:00
|
|
|
fileptr = (unsigned char *)(((intptr_t)fileptr + 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)
|
|
|
|
{
|
|
|
|
if ( pf->bits_size < 0xFFDB )
|
|
|
|
{
|
|
|
|
long_offset = 0;
|
|
|
|
pf->offset = (unsigned short *)fileptr;
|
2007-04-13 19:51:19 +00:00
|
|
|
|
|
|
|
/* Check we have sufficient buffer */
|
|
|
|
if (!HAVEBYTES(noffset * sizeof(short)))
|
|
|
|
return NULL;
|
|
|
|
|
2005-12-06 13:27:15 +00:00
|
|
|
for (i=0; i<noffset; ++i)
|
|
|
|
{
|
2007-04-13 19:51:19 +00:00
|
|
|
((unsigned short*)(pf->offset))[i] = (unsigned short)readshort();
|
2005-12-06 13:27:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2004-08-26 21:15:07 +00:00
|
|
|
{
|
2005-12-06 13:27:15 +00:00
|
|
|
long_offset = 1;
|
|
|
|
pf->offset = (unsigned short *)fileptr;
|
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)
|
|
|
|
{
|
2007-06-30 17:54:02 +00:00
|
|
|
((uint32_t*)(pf->offset))[i] = (uint32_t)readlong();
|
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) {
|
|
|
|
pf->width = (unsigned char *)fileptr;
|
|
|
|
fileptr += nwidth*sizeof(unsigned char);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
pf->width = NULL;
|
|
|
|
|
|
|
|
if (fileptr > eofptr)
|
|
|
|
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;
|
2005-12-06 13:27:15 +00:00
|
|
|
unsigned char* oldfileptr = fileptr;
|
|
|
|
|
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*/
|
|
|
|
noffset = readlong();
|
|
|
|
|
2005-12-06 13:27:15 +00:00
|
|
|
/* # bytes of width*/
|
2007-04-13 19:51:19 +00:00
|
|
|
nwidth = readlong();
|
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 */
|
|
|
|
fileptr += pf->bits_size * sizeof(unsigned char);
|
|
|
|
|
|
|
|
if ( pf->bits_size < 0xFFDB )
|
|
|
|
{
|
|
|
|
long_offset = 0;
|
|
|
|
/* pad to 16-bit boundary */
|
2007-06-30 18:09:46 +00:00
|
|
|
fileptr = (unsigned char *)(((intptr_t)fileptr + 1) & ~1);
|
2005-12-06 13:27:15 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
long_offset = 1;
|
|
|
|
/* pad to 32-bit boundary*/
|
2007-06-30 18:09:46 +00:00
|
|
|
fileptr = (unsigned char *)(((intptr_t)fileptr + 3) & ~3);
|
2005-12-06 13:27:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (noffset)
|
2007-06-30 17:54:02 +00:00
|
|
|
file_offset_offset = (uint32_t)(fileptr - freeptr);
|
2005-12-06 13:27:15 +00:00
|
|
|
else
|
|
|
|
file_offset_offset = 0;
|
|
|
|
|
|
|
|
/* Calculate offset to widths data */
|
|
|
|
if ( pf->bits_size < 0xFFDB )
|
|
|
|
fileptr += noffset * sizeof(unsigned short);
|
|
|
|
else
|
2007-06-30 17:54:02 +00:00
|
|
|
fileptr += noffset * sizeof(uint32_t);
|
2005-12-06 13:27:15 +00:00
|
|
|
|
|
|
|
if (nwidth)
|
2007-06-30 17:54:02 +00:00
|
|
|
file_width_offset = (uint32_t)(fileptr - freeptr);
|
2005-12-06 13:27:15 +00:00
|
|
|
else
|
|
|
|
file_width_offset = 0;
|
|
|
|
|
|
|
|
fileptr = oldfileptr;
|
|
|
|
|
|
|
|
/* Create the cache */
|
|
|
|
cache_create(pf->maxwidth, pf->height);
|
|
|
|
|
|
|
|
return pf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* read and load font into incore font structure*/
|
|
|
|
struct font* font_load(const char *path)
|
|
|
|
{
|
2006-04-24 16:02:44 +00:00
|
|
|
int size;
|
2005-12-06 13:27:15 +00:00
|
|
|
struct font* pf = &font_ui;
|
|
|
|
|
|
|
|
/* save loaded glyphs */
|
|
|
|
glyph_cache_save();
|
|
|
|
|
|
|
|
/* Close font file handle */
|
|
|
|
if (fnt_file >= 0)
|
|
|
|
close(fnt_file);
|
|
|
|
|
|
|
|
/* open and read entire font file*/
|
|
|
|
fnt_file = open(path, O_RDONLY|O_BINARY);
|
|
|
|
|
|
|
|
if (fnt_file < 0) {
|
|
|
|
DEBUGF("Can't open font: %s\n", path);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check file size */
|
2006-04-24 16:02:44 +00:00
|
|
|
size = filesize(fnt_file);
|
2005-12-06 13:27:15 +00:00
|
|
|
|
|
|
|
font_reset();
|
|
|
|
|
|
|
|
/* currently, font loading replaces earlier font allocation*/
|
2007-10-02 17:04:56 +00:00
|
|
|
freeptr = (unsigned char *)(((intptr_t)mbuf + 3) & ~3);
|
2005-12-06 13:27:15 +00:00
|
|
|
fileptr = freeptr;
|
|
|
|
|
|
|
|
|
2006-04-24 16:02:44 +00:00
|
|
|
if (size > MAX_FONT_SIZE)
|
2005-12-06 13:27:15 +00:00
|
|
|
{
|
|
|
|
read(fnt_file, fileptr, FONT_HEADER_SIZE);
|
|
|
|
eofptr = fileptr + FONT_HEADER_SIZE;
|
|
|
|
|
|
|
|
if (!font_load_header(pf))
|
|
|
|
{
|
|
|
|
DEBUGF("Failed font header load");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!font_load_cached(pf))
|
|
|
|
{
|
|
|
|
DEBUGF("Failed font cache load");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
glyph_cache_load();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
read(fnt_file, fileptr, MAX_FONT_SIZE);
|
2006-04-24 16:02:44 +00:00
|
|
|
eofptr = fileptr + size;
|
2005-12-06 13:27:15 +00:00
|
|
|
close(fnt_file);
|
|
|
|
fnt_file = -1;
|
|
|
|
|
|
|
|
if (!font_load_header(pf))
|
|
|
|
{
|
|
|
|
DEBUGF("Failed font header load");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!font_load_in_memory(pf))
|
|
|
|
{
|
|
|
|
DEBUGF("Failed mem load");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* no need for multiple font loads currently*/
|
|
|
|
/*freeptr += filesize;*/
|
|
|
|
/*freeptr = (unsigned char *)(freeptr + 3) & ~3;*/ /* pad freeptr*/
|
|
|
|
|
|
|
|
return pf; /* success!*/
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (font >= MAXFONTS)
|
|
|
|
font = 0;
|
|
|
|
|
|
|
|
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];
|
|
|
|
|
|
|
|
if (file_width_offset)
|
|
|
|
{
|
|
|
|
int width_offset = file_width_offset + char_code;
|
|
|
|
lseek(fnt_file, width_offset, SEEK_SET);
|
|
|
|
read(fnt_file, &(p->width), 1);
|
|
|
|
}
|
|
|
|
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
|
|
|
|
|
|
|
if (file_offset_offset)
|
|
|
|
{
|
2007-06-30 17:54:02 +00:00
|
|
|
int32_t offset = file_offset_offset + char_code * (long_offset ? sizeof(int32_t) : sizeof(short));
|
2005-12-06 13:27:15 +00:00
|
|
|
lseek(fnt_file, offset, SEEK_SET);
|
|
|
|
read (fnt_file, tmp, 2);
|
|
|
|
bitmap_offset = tmp[0] | (tmp[1] << 8);
|
|
|
|
if (long_offset) {
|
|
|
|
read (fnt_file, tmp, 2);
|
|
|
|
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;
|
2005-12-06 13:27:15 +00:00
|
|
|
lseek(fnt_file, file_offset, SEEK_SET);
|
|
|
|
|
|
|
|
int src_bytes = p->width * ((pf->height + 7) / 8);
|
|
|
|
read(fnt_file, p->bitmap, src_bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Converts cbuf into a font cache
|
|
|
|
*/
|
|
|
|
static void cache_create(int maxwidth, int height)
|
|
|
|
{
|
|
|
|
/* maximum size of rotated bitmap */
|
|
|
|
int bitmap_size = maxwidth * ((height + 7) / 8);
|
|
|
|
|
|
|
|
/* Initialise cache */
|
|
|
|
font_cache_create(&font_cache_ui, mbuf, MAX_FONT_SIZE, bitmap_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
2005-12-06 13:27:15 +00:00
|
|
|
return (fnt_file >= 0 && pf != &sysfont)?
|
|
|
|
font_cache_get(&font_cache_ui,char_code,load_cache_entry,pf)->width:
|
|
|
|
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;
|
|
|
|
|
2005-12-06 13:27:15 +00:00
|
|
|
if (fnt_file >= 0 && pf != &sysfont)
|
|
|
|
{
|
|
|
|
bits =
|
|
|
|
(unsigned char*)font_cache_get(&font_cache_ui,char_code,load_cache_entry,pf)->bitmap;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bits = pf->bits + (pf->offset?
|
|
|
|
pf->offset[char_code]:
|
|
|
|
(((pf->height + 7) / 8) * pf->maxwidth * char_code));
|
|
|
|
}
|
|
|
|
|
|
|
|
return bits;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (ch != 0xffff && glyph_file >= 0) {
|
|
|
|
tmp[0] = ch >> 8;
|
|
|
|
tmp[1] = ch & 0xff;
|
2005-12-06 13:27:15 +00:00
|
|
|
if (write(glyph_file, tmp, 2) != 2) {
|
|
|
|
close(glyph_file);
|
|
|
|
glyph_file = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* save the char codes of the loaded glyphs to a file */
|
|
|
|
void glyph_cache_save(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (fnt_file >= 0) {
|
2008-08-29 21:08:38 +00:00
|
|
|
#ifdef WPSEDITOR
|
|
|
|
glyph_file = open(GLYPH_CACHE_FILE, O_WRONLY|O_CREAT|O_TRUNC);
|
|
|
|
#else
|
2007-02-01 23:08:15 +00:00
|
|
|
glyph_file = creat(GLYPH_CACHE_FILE);
|
2008-08-29 21:08:38 +00:00
|
|
|
#endif
|
2005-12-06 13:27:15 +00:00
|
|
|
if (glyph_file < 0) return;
|
|
|
|
|
|
|
|
lru_traverse(&font_cache_ui._lru, glyph_file_write);
|
|
|
|
|
|
|
|
if (glyph_file >= 0)
|
|
|
|
close(glyph_file);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-06-30 17:54:02 +00:00
|
|
|
static void glyph_cache_load(void)
|
2005-12-06 13:27:15 +00:00
|
|
|
{
|
|
|
|
if (fnt_file >= 0) {
|
|
|
|
|
|
|
|
int fd;
|
|
|
|
unsigned char tmp[2];
|
|
|
|
unsigned short ch;
|
|
|
|
struct font* pf = &font_ui;
|
|
|
|
|
|
|
|
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;
|
|
|
|
return &sysfont;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
bits = pf->bits + (pf->offset?
|
|
|
|
pf->offset[char_code]:
|
|
|
|
(((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
|
|
|
*/
|