fix errors
[maemo-rb.git] / apps / gui / skin_engine / skin_fonts.c
blob56a87ca93c920c134e5d124188861c5057a64966
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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"
33 static struct skin_font_info {
34 struct font font;
35 int font_id;
36 char name[MAX_PATH];
37 char *buffer;
38 int ref_count; /* how many times has this font been loaded? */
39 } font_table[MAXUSERFONTS];
41 /* need this to know if we should be closing font fd's on the next init */
42 static bool first_load = true;
44 void skin_font_init(void)
46 int i;
47 for(i=0;i<MAXUSERFONTS;i++)
49 if (!first_load)
50 font_unload(font_table[i].font_id);
51 font_table[i].font_id = -1;
52 font_table[i].name[0] = '\0';
53 font_table[i].buffer = NULL;
54 font_table[i].ref_count = 0;
56 first_load = false;
59 /* load a font into the skin buffer. return the font id. */
60 int skin_font_load(char* font_name, int glyphs)
62 int i;
63 int skin_font_size = 0;
64 struct font *pf;
65 struct skin_font_info *font = NULL;
66 char filename[MAX_PATH];
67 char tmp[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 snprintf(tmp, MAX_PATH, FONT_DIR "/%s.fnt", font_name);
76 get_user_file_path(tmp, FORCE_BUFFER_COPY, filename, sizeof(filename));
78 for(i=0;i<MAXUSERFONTS;i++)
80 if (font_table[i].font_id >= 0 && !strcmp(font_table[i].name, filename))
82 font_table[i].ref_count++;
83 return font_table[i].font_id;
85 else if (!font && font_table[i].font_id == -1)
87 font = &font_table[i];
90 if (!font)
91 return -1; /* too many fonts loaded */
93 pf = &font->font;
94 if (!font->buffer)
96 if (!glyphs)
97 glyphs = GLYPHS_TO_CACHE;
98 #ifndef __PCTOOL__
99 skin_font_size = glyphs * get_glyph_size(filename);
100 #endif
101 if ( !skin_font_size )
103 skin_font_size = SKIN_FONT_SIZE;
105 pf->buffer_start = (char*)skin_buffer_alloc(skin_font_size);
106 if (!pf->buffer_start)
107 return -1;
108 font->buffer = pf->buffer_start;
109 pf->buffer_size = skin_font_size;
111 else
113 pf->buffer_start = font->buffer;
116 pf->fd = -1;
117 font->font_id = font_load(pf, filename);
119 if (font->font_id < 0)
120 return -1;
121 font->ref_count = 1;
123 return font->font_id;
125 /* unload a skin font. If a font has been loaded more than once it wont actually
126 * be unloaded untill all references have been unloaded */
127 void skin_font_unload(int font_id)
129 int i;
130 for(i=0;i<MAXUSERFONTS;i++)
132 if (font_table[i].font_id == font_id)
134 if (--font_table[i].ref_count == 0)
136 font_unload(font_id);
137 font_table[i].font_id = -1;
138 font_table[i].name[0] = '\0';
140 return;