Store the list of fonts in a linked list instead of in a static array (together with...
[kugel-rb.git] / apps / gui / skin_engine / skin_fonts.c
blob882c04038326396047c4af5b69659fd29c677d9a
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id: skin_tokens.c 24526 2010-02-05 23:58:53Z jdgordon $
10 * Copyright (C) 2010 Jonathan Gordon
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include "config.h"
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdlib.h>
27 #include "file.h"
28 #include "settings.h"
29 #include "font.h"
30 #include "skin_buffer.h"
31 #include "skin_fonts.h"
32 #define FONT_SIZE 10000
35 static struct skin_font_info {
36 struct font font;
37 int font_id;
38 char name[MAX_PATH];
39 char *buffer;
40 int ref_count; /* how many times has this font been loaded? */
41 } font_table[MAXUSERFONTS];
43 /* need this to know if we should be closing font fd's on the next init */
44 static bool first_load = true;
46 void skin_font_init(void)
48 int i;
49 for(i=0;i<MAXUSERFONTS;i++)
51 if (!first_load)
52 font_unload(font_table[i].font_id);
53 font_table[i].font_id = -1;
54 font_table[i].name[0] = '\0';
55 font_table[i].buffer = NULL;
56 font_table[i].ref_count = 0;
58 first_load = false;
61 /* load a font into the skin buffer. return the font id. */
62 int skin_font_load(char* font_name)
64 int i;
65 struct font *pf;
66 struct skin_font_info *font = NULL;
67 char filename[MAX_PATH];
69 if (!strcmp(font_name, global_settings.font_file))
70 return FONT_UI;
71 #ifdef HAVE_REMOTE_LCD
72 if (!strcmp(font_name, global_settings.remote_font_file))
73 return FONT_UI_REMOTE;
74 #endif
75 for(i=0;i<MAXUSERFONTS;i++)
77 if (font_table[i].font_id >= 0 && !strcmp(font_table[i].name, font_name))
79 font_table[i].ref_count++;
80 return font_table[i].font_id;
82 else if (!font && font_table[i].font_id == -1)
84 font = &font_table[i];
87 if (!font)
88 return -1; /* too many fonts loaded */
90 pf = &font->font;
91 if (!font->buffer)
93 pf->buffer_start = skin_buffer_alloc(FONT_SIZE);
94 if (!pf->buffer_start)
95 return -1;
96 font->buffer = pf->buffer_start;
98 else
100 pf->buffer_start = font->buffer;
102 pf->buffer_size = FONT_SIZE;
104 snprintf(filename, MAX_PATH, FONT_DIR "/%s.fnt", font_name);
105 strcpy(font->name, font_name);
107 pf->fd = -1;
108 font->font_id = font_load(pf, filename);
110 if (font->font_id < 0)
111 return -1;
112 font->ref_count = 1;
114 return font->font_id;
117 /* unload a skin font. If a font has been loaded more than once it wont actually
118 * be unloaded untill all references have been unloaded */
119 void skin_font_unload(int font_id)
121 int i;
122 for(i=0;i<MAXUSERFONTS;i++)
124 if (font_table[i].font_id == font_id)
126 if (--font_table[i].ref_count == 0)
128 font_unload(font_id);
129 font_table[i].font_id = -1;
130 font_table[i].name[0] = '\0';
132 return;