PageRenderTime 43ms CodeModel.GetById 20ms app.highlight 9ms RepoModel.GetById 12ms app.codeStats 0ms

/src/canvas/default/ftk_font_manager.c

http://ftk.googlecode.com/
C | 145 lines | 91 code | 25 blank | 29 comment | 16 complexity | 4db74ddc2b90b6e1310632042d52498b MD5 | raw file
  1/*
  2 * File: ftk_font_manager.c
  3 * Author:  Li XianJing <xianjimli@hotmail.com>
  4 * Brief:   font manager.
  5 *
  6 * Copyright (c) 2009 - 2011  Li XianJing <xianjimli@hotmail.com>
  7 *
  8 * Licensed under the Academic Free License version 2.1
  9 *
 10 * This program is free software; you can redistribute it and/or modify
 11 * it under the terms of the GNU General Public License as published by
 12 * the Free Software Foundation; either version 2 of the License, or
 13 * (at your option) any later version.
 14 *
 15 * This program is distributed in the hope that it will be useful,
 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 18 * GNU General Public License for more details.
 19 *
 20 * You should have received a copy of the GNU General Public License
 21 * along with this program; if not, write to the Free Software
 22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 23 */
 24
 25/*
 26 * History:
 27 * ================================================================
 28 * 2011-03-20 Li XianJing <xianjimli@hotmail.com> created
 29 *
 30 */
 31
 32#include "ftk_util.h"
 33#include "ftk_font.h"
 34#include "ftk_globals.h"
 35#include "ftk_font_manager.h"
 36
 37typedef struct _FtkFontEntry
 38{
 39	FtkFont* font;
 40	FtkFontDesc* font_desc;	
 41}FtkFontEntry;
 42
 43struct _FtkFontManager
 44{
 45	int nr;
 46	int used_nr;
 47	FtkFontDesc* default_font_desc;
 48	FtkFontEntry fonts[ZERO_LEN_ARRAY];
 49};
 50
 51FtkFontManager* ftk_font_manager_create(int max_font_nr)
 52{
 53	FtkFontManager* thiz = (FtkFontManager*)FTK_ZALLOC(sizeof(FtkFontManager) 
 54		+ sizeof(FtkFontEntry) * max_font_nr);
 55
 56	if(thiz != NULL)
 57	{
 58		thiz->nr = max_font_nr;
 59		thiz->default_font_desc = ftk_font_desc_create(NULL);
 60	}
 61
 62	return thiz;
 63}
 64
 65FtkFont* ftk_font_manager_get_default_font(FtkFontManager* thiz)
 66{
 67	FtkFont* font = NULL;
 68	return_val_if_fail(thiz != NULL, NULL);
 69
 70	font = ftk_font_manager_load(thiz, thiz->default_font_desc);
 71	ftk_font_ref(font);
 72
 73	return font;
 74}
 75
 76FtkFont* ftk_font_manager_load(FtkFontManager* thiz, FtkFontDesc* font_desc)
 77{
 78	int i = 0;
 79	FtkFont* font = NULL;
 80	char filename[FTK_MAX_PATH + 1] = {0};
 81
 82	return_val_if_fail(thiz != NULL && font_desc != NULL, NULL);
 83
 84#ifndef USE_FREETYPE
 85	font_desc = thiz->default_font_desc;
 86#endif
 87
 88	for(i = 0; i < thiz->used_nr; i++)
 89	{
 90		if(ftk_font_desc_is_equal(thiz->fonts[i].font_desc, font_desc))
 91		{
 92			ftk_font_ref(thiz->fonts[i].font);
 93			
 94			return thiz->fonts[i].font;
 95		}
 96	}
 97
 98	return_val_if_fail((thiz->used_nr+1) < thiz->nr, NULL);
 99
100#if defined(USE_FREETYPE) && defined(ANDROID) && defined(ANDROID_NDK)
101	ftk_strcpy(filename, ftk_font_desc_get_fontname(font_desc));
102#else
103	ftk_strs_cat(filename, FTK_MAX_PATH, 
104		ftk_config_get_data_dir(ftk_default_config()), "/data/", ftk_font_desc_get_fontname(font_desc), NULL);
105	ftk_normalize_path(filename);
106#endif
107	font = ftk_font_create(filename, font_desc);
108	
109	if(font != NULL)
110	{
111#if defined(USE_FREETYPE) || defined(USE_DEFAULT_FONT) 	
112		FtkFont* cached_font = ftk_font_cache_create(font, 512);
113		ftk_font_unref(font);
114		font = cached_font;
115#endif		
116		ftk_font_ref(font);
117		ftk_font_desc_ref(font_desc);
118		thiz->fonts[thiz->used_nr].font = font;
119		thiz->fonts[thiz->used_nr].font_desc = font_desc;
120
121		thiz->used_nr++;
122	}
123
124	return font;
125}
126
127void ftk_font_manager_destroy(FtkFontManager* thiz)
128{
129	int i = 0;
130
131	if(thiz != NULL)
132	{
133		for(i = 0; i < thiz->used_nr; i++)
134		{
135			ftk_font_destroy(thiz->fonts[i].font);
136			ftk_font_desc_unref(thiz->fonts[i].font_desc);
137		}
138
139		ftk_font_desc_unref(thiz->default_font_desc);
140		FTK_FREE(thiz);
141	}
142
143	return;
144}
145