2005-12-06 13:27:15 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
*
|
|
|
|
* Copyright (C) 2003 Tat Tang
|
|
|
|
*
|
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.
|
2005-12-06 13:27:15 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include "font_cache.h"
|
|
|
|
#include "debug.h"
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
* font_cache_lru_init
|
|
|
|
******************************************************************************/
|
2008-04-12 17:08:35 +00:00
|
|
|
static void font_cache_lru_init(void* data)
|
2005-12-06 13:27:15 +00:00
|
|
|
{
|
|
|
|
struct font_cache_entry* p = data;
|
|
|
|
p->_char_code = 0xffff; /* assume invalid char */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
* font_cache_create
|
|
|
|
******************************************************************************/
|
|
|
|
void font_cache_create(
|
|
|
|
struct font_cache* fcache,
|
|
|
|
void *buf,
|
|
|
|
int buf_size,
|
|
|
|
int bitmap_bytes_size)
|
|
|
|
{
|
|
|
|
int font_cache_entry_size =
|
|
|
|
sizeof(struct font_cache_entry) + bitmap_bytes_size;
|
|
|
|
|
|
|
|
/* make sure font cache entries are a multiple of 16 bits */
|
|
|
|
if (font_cache_entry_size % 2 != 0)
|
|
|
|
font_cache_entry_size++;
|
|
|
|
|
|
|
|
int cache_size = buf_size /
|
|
|
|
(font_cache_entry_size + LRU_SLOT_OVERHEAD + sizeof(short));
|
|
|
|
|
|
|
|
fcache->_size = 1;
|
|
|
|
fcache->_capacity = cache_size;
|
2011-10-18 17:02:24 +00:00
|
|
|
fcache->_prev_result = 0;
|
|
|
|
fcache->_prev_char_code = 0;
|
2005-12-06 13:27:15 +00:00
|
|
|
|
|
|
|
/* set up index */
|
|
|
|
fcache->_index = buf;
|
|
|
|
|
|
|
|
/* set up lru list */
|
|
|
|
unsigned char* lru_buf = buf;
|
|
|
|
lru_buf += sizeof(short) * cache_size;
|
|
|
|
lru_create(&fcache->_lru, lru_buf, cache_size, font_cache_entry_size);
|
|
|
|
|
|
|
|
/* initialise cache */
|
|
|
|
lru_traverse(&fcache->_lru, font_cache_lru_init);
|
|
|
|
short i;
|
|
|
|
for (i = 0; i < cache_size; i++)
|
|
|
|
fcache->_index[i] = i; /* small cheat here */
|
|
|
|
}
|
|
|
|
|
2011-10-16 20:17:44 +00:00
|
|
|
/*************************************************************************
|
|
|
|
* Binary search that attempts a primary lucky guess that succeeds
|
|
|
|
* when there are consecutive codes in the cache between previous
|
|
|
|
* search and new search. Returns a negative of insertion point if
|
|
|
|
* not found.
|
|
|
|
************************************************************************/
|
2011-11-03 23:32:49 +00:00
|
|
|
static int search(struct font_cache* fcache,
|
2011-10-16 20:17:44 +00:00
|
|
|
unsigned short char_code,
|
2011-11-25 16:15:16 +00:00
|
|
|
int size,
|
2011-10-16 20:17:44 +00:00
|
|
|
int *p_insertion_point )
|
2005-12-06 13:27:15 +00:00
|
|
|
{
|
2011-10-16 20:17:44 +00:00
|
|
|
struct font_cache_entry *p;
|
|
|
|
int left, right, mid=-1, c;
|
2005-12-06 13:27:15 +00:00
|
|
|
left = 0;
|
2011-11-25 16:15:16 +00:00
|
|
|
right = size;
|
|
|
|
|
2011-10-16 20:17:44 +00:00
|
|
|
/* go for a lucky guess */
|
2011-10-18 17:02:24 +00:00
|
|
|
mid = char_code +
|
|
|
|
fcache->_prev_result - fcache->_prev_char_code;
|
2011-10-16 20:17:44 +00:00
|
|
|
|
2011-10-18 17:02:24 +00:00
|
|
|
/* check bounds */
|
2011-10-16 20:17:44 +00:00
|
|
|
if ( mid < 0 || mid > right )
|
|
|
|
mid = ( left + right ) / 2;
|
|
|
|
|
2005-12-06 13:27:15 +00:00
|
|
|
do
|
|
|
|
{
|
|
|
|
p = lru_data(&fcache->_lru, fcache->_index[mid]);
|
|
|
|
c = p->_char_code - char_code;
|
|
|
|
|
|
|
|
if (c == 0)
|
2011-10-16 20:17:44 +00:00
|
|
|
{
|
|
|
|
fcache->_prev_result = mid;
|
|
|
|
fcache->_prev_char_code = char_code;
|
|
|
|
*p_insertion_point = mid;
|
|
|
|
return 1;
|
|
|
|
}
|
2005-12-06 13:27:15 +00:00
|
|
|
if (c < 0)
|
|
|
|
left = mid + 1;
|
|
|
|
else
|
|
|
|
right = mid - 1;
|
|
|
|
|
|
|
|
mid = (left + right) / 2;
|
|
|
|
}
|
|
|
|
while (left <= right);
|
2011-10-16 20:17:44 +00:00
|
|
|
|
2005-12-06 13:27:15 +00:00
|
|
|
/* not found */
|
2011-11-25 16:15:16 +00:00
|
|
|
p = lru_data(&fcache->_lru, fcache->_index[mid]);
|
2011-10-16 20:17:44 +00:00
|
|
|
*p_insertion_point = mid;
|
|
|
|
return 0;
|
2005-12-06 13:27:15 +00:00
|
|
|
}
|
|
|
|
/*******************************************************************************
|
|
|
|
* font_cache_get
|
|
|
|
******************************************************************************/
|
|
|
|
struct font_cache_entry* font_cache_get(
|
|
|
|
struct font_cache* fcache,
|
|
|
|
unsigned short char_code,
|
|
|
|
void (*callback) (struct font_cache_entry* p, void *callback_data),
|
|
|
|
void *callback_data)
|
|
|
|
{
|
2011-10-16 20:17:44 +00:00
|
|
|
struct font_cache_entry* p;
|
|
|
|
int insertion_point;
|
|
|
|
int index_to_replace;
|
|
|
|
|
2011-11-25 16:15:16 +00:00
|
|
|
/* check bounds */
|
|
|
|
p = lru_data(&fcache->_lru, fcache->_index[0]);
|
|
|
|
if( char_code < p->_char_code )
|
|
|
|
insertion_point = -1;
|
|
|
|
else
|
2005-12-06 13:27:15 +00:00
|
|
|
{
|
2011-11-25 16:15:16 +00:00
|
|
|
p = lru_data(&fcache->_lru, fcache->_index[fcache->_capacity - 1]);
|
|
|
|
if( char_code > p->_char_code )
|
2005-12-06 13:27:15 +00:00
|
|
|
{
|
2011-11-25 16:15:16 +00:00
|
|
|
insertion_point = fcache->_capacity - 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( search(fcache, char_code, fcache->_size - 1, &insertion_point))
|
|
|
|
{
|
|
|
|
short lru_handle = fcache->_index[insertion_point];
|
|
|
|
p = lru_data(&fcache->_lru, lru_handle);
|
|
|
|
if (p->_char_code == char_code)
|
|
|
|
{
|
|
|
|
lru_touch(&fcache->_lru, lru_handle);
|
|
|
|
return lru_data(&fcache->_lru, lru_handle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
p = lru_data(&fcache->_lru,
|
|
|
|
fcache->_index[insertion_point+1]);
|
|
|
|
if ( char_code > p->_char_code )
|
|
|
|
insertion_point++;
|
|
|
|
}
|
2005-12-06 13:27:15 +00:00
|
|
|
}
|
|
|
|
}
|
2011-11-25 16:15:16 +00:00
|
|
|
|
|
|
|
/* not found */
|
2005-12-06 13:27:15 +00:00
|
|
|
|
2011-10-16 20:17:44 +00:00
|
|
|
/* find index to replace */
|
2005-12-06 13:27:15 +00:00
|
|
|
short lru_handle_to_replace = fcache->_lru._head;
|
2011-10-16 20:17:44 +00:00
|
|
|
p = lru_data(&fcache->_lru, lru_handle_to_replace);
|
2011-11-25 16:15:16 +00:00
|
|
|
search(fcache, p->_char_code, fcache->_size - 1, &index_to_replace);
|
2005-12-06 13:27:15 +00:00
|
|
|
if (insertion_point < index_to_replace)
|
|
|
|
{
|
2006-02-06 17:24:47 +00:00
|
|
|
/* shift memory up */
|
|
|
|
memmove(fcache->_index + insertion_point + 2,
|
|
|
|
fcache->_index + insertion_point + 1,
|
|
|
|
(index_to_replace - insertion_point - 1) * sizeof(short));
|
2005-12-06 13:27:15 +00:00
|
|
|
|
|
|
|
/* add to index */
|
|
|
|
fcache->_index[insertion_point + 1] = lru_handle_to_replace;
|
|
|
|
}
|
|
|
|
else if (insertion_point > index_to_replace)
|
|
|
|
{
|
2006-02-06 17:24:47 +00:00
|
|
|
/* shift memory down */
|
|
|
|
memmove(fcache->_index + index_to_replace,
|
|
|
|
fcache->_index + index_to_replace + 1,
|
|
|
|
(insertion_point - index_to_replace) * sizeof(short));
|
2005-12-06 13:27:15 +00:00
|
|
|
|
|
|
|
/* add to index */
|
|
|
|
fcache->_index[insertion_point] = lru_handle_to_replace;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* load new entry into cache */
|
|
|
|
lru_touch(&fcache->_lru, lru_handle_to_replace);
|
|
|
|
|
|
|
|
if (fcache->_size < fcache->_capacity)
|
|
|
|
fcache->_size++;
|
|
|
|
|
|
|
|
p->_char_code = char_code;
|
2011-10-16 20:17:44 +00:00
|
|
|
/* fill bitmap */
|
2005-12-06 13:27:15 +00:00
|
|
|
callback(p, callback_data);
|
|
|
|
return p;
|
|
|
|
}
|