summaryrefslogtreecommitdiffstats
path: root/apps/gui/skin_engine/skin_fonts.c
blob: 128bffcfbad6135373874f18db9ae51595cf6cef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2010 Jonathan Gordon
 *
 * 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.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include "config.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "file.h"
#include "settings.h"
#include "font.h"
#include "skin_buffer.h"
#include "skin_fonts.h"

static struct skin_font_info {
    struct font font;
    int font_id;
    char name[MAX_PATH];
    char *buffer;
    int ref_count; /* how many times has this font been loaded? */
} font_table[MAXUSERFONTS];

/* need this to know if we should be closing font fd's on the next init */
static bool first_load = true;

void skin_font_init(void)
{
    int i;
    for(i=0;i<MAXUSERFONTS;i++)
    {
        if (!first_load)
            font_unload(font_table[i].font_id);
        font_table[i].font_id = -1;
        font_table[i].name[0] = '\0';
        font_table[i].buffer = NULL;
        font_table[i].ref_count = 0;
    }
    first_load = false;
}

/* load a font into the skin buffer. return the font id. */
int skin_font_load(char* font_name, int glyphs)
{
    int i;
    int skin_font_size = 0;
    struct font *pf;
    struct skin_font_info *font = NULL;
    char filename[MAX_PATH];
    char tmp[MAX_PATH];
    
    if (!strcmp(font_name, global_settings.font_file))
        return FONT_UI;
#ifdef HAVE_REMOTE_LCD
    if (!strcmp(font_name, global_settings.remote_font_file))
        return FONT_UI_REMOTE;
#endif
    snprintf(tmp, MAX_PATH, FONT_DIR "/%s.fnt", font_name);
    get_user_file_path(tmp, FORCE_BUFFER_COPY, filename, sizeof(filename));
    
    for(i=0;i<MAXUSERFONTS;i++)
    {
        if (font_table[i].font_id >= 0 && !strcmp(font_table[i].name, filename))
        {
            font_table[i].ref_count++;
            return font_table[i].font_id;
        }
        else if (!font && font_table[i].font_id == -1)
        {
            font = &font_table[i];
        }
    }
    if (!font)
        return -1; /* too many fonts loaded */
    
    pf = &font->font;
    if (!font->buffer)
    {
        if (!glyphs) 
            glyphs = GLYPHS_TO_CACHE;
#ifndef __PCTOOL__
        skin_font_size = font_glyphs_to_bufsize(filename, glyphs);
#endif
        if ( !skin_font_size )
        {
            skin_font_size = SKIN_FONT_SIZE;
        }
        pf->buffer_start = (char*)skin_buffer_alloc(skin_font_size);
        if (!pf->buffer_start)
            return -1;
        font->buffer = pf->buffer_start;
        pf->buffer_size = skin_font_size;
    }
    else
    {
        pf->buffer_start = font->buffer;
    }
    
    pf->fd = -1;
    font->font_id = font_load(pf, filename);
    
    if (font->font_id < 0)
        return -1;
    font->ref_count = 1;
    
    return font->font_id;
}
/* unload a skin font. If a font has been loaded more than once it wont actually
 * be unloaded untill all references have been unloaded */
void skin_font_unload(int font_id)
{
    int i;
    for(i=0;i<MAXUSERFONTS;i++)
    {
        if (font_table[i].font_id == font_id)
        {
            if (--font_table[i].ref_count == 0)
            {
                font_unload(font_id);
                font_table[i].font_id = -1;
                font_table[i].name[0] = '\0';
            }
            return;
        }
    }
}