PageRenderTime 425ms CodeModel.GetById 241ms app.highlight 14ms RepoModel.GetById 167ms app.codeStats 1ms

/src/ftk_font_desc.c

http://ftk.googlecode.com/
C | 232 lines | 153 code | 49 blank | 30 comment | 44 complexity | 2d0ed8b12f7f1d09b61fea5529b91bd3 MD5 | raw file
  1/*
  2 * 
  3 * File: ftk_font_desc.c
  4 * Author:  Li XianJing <xianjimli@hotmail.com>
  5 * Brief:   font description.
  6 *
  7 * Copyright (c) 2009 - 2011  Li XianJing <xianjimli@hotmail.com>
  8 *
  9 * Licensed under the Academic Free License version 2.1
 10 *
 11 * This program is free software; you can redistribute it and/or modify
 12 * it under the terms of the GNU General Public License as published by
 13 * the Free Software Foundation; either version 2 of the License, or
 14 * (at your option) any later version.
 15 *
 16 * This program is distributed in the hope that it will be useful,
 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 19 * GNU General Public License for more details.
 20 *
 21 * You should have received a copy of the GNU General Public License
 22 * along with this program; if not, write to the Free Software
 23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 24 */
 25
 26/*
 27 * History:
 28 * ================================================================
 29 * 2011-03-20 Li XianJing <xianjimli@hotmail.com> created
 30 *
 31 */
 32
 33#include "ftk_util.h"
 34#include "ftk_font_desc.h"
 35
 36struct _FtkFontDesc
 37{
 38	int ref;
 39	int size;
 40	int is_bold;
 41	int is_italic;
 42	char *fontname;
 43};
 44
 45static Ret ftk_font_desc_parse(FtkFontDesc* thiz, const char* font_desc)
 46{
 47	const char* p = NULL;
 48
 49	if(font_desc == NULL)
 50	{
 51#ifdef FTK_FONT_SIZE
 52		thiz->size = FTK_FONT_SIZE;
 53		thiz->is_italic = 0;
 54		thiz->is_bold = 0;
 55        thiz->fontname = ftk_strdup(FTK_FONT);
 56		return RET_OK;
 57#else
 58		font_desc = font_desc != NULL ? font_desc : FTK_DEFAULT_FONT;
 59#endif		
 60	}
 61
 62	p = strstr(font_desc, "size:");
 63	if(p != NULL)
 64	{
 65		thiz->size = ftk_atoi(p + 5);
 66	}
 67
 68	p = strstr(font_desc, "bold:");
 69	if(p != NULL)
 70	{
 71		thiz->is_bold = ftk_atoi(p + 5);
 72	}
 73
 74	p = strstr(font_desc, "italic:");
 75	if(p != NULL)
 76	{
 77		thiz->is_italic = ftk_atoi(p + 7);
 78	}
 79
 80    p = strstr(font_desc, "fontname:");
 81    if(p != NULL)
 82    {
 83        thiz->fontname = ftk_strdup(p + 9);
 84    }
 85    else
 86    {
 87        thiz->fontname = ftk_strdup(FTK_FONT);
 88    }
 89
 90	return RET_OK;
 91}
 92
 93FtkFontDesc* ftk_font_desc_create(const char* font_desc)
 94{
 95	FtkFontDesc* thiz = FTK_NEW(FtkFontDesc);
 96
 97	if(thiz != NULL)
 98	{
 99		thiz->ref = 1;
100		ftk_font_desc_parse(thiz, font_desc);
101	}
102
103	return thiz;
104}
105
106int ftk_font_desc_is_equal(FtkFontDesc* thiz, FtkFontDesc* other)
107{
108	return_val_if_fail(thiz != NULL && other != NULL, 0);
109
110	return (thiz->size == other->size &&
111			thiz->is_bold == other->is_bold &&
112			thiz->is_italic == other->is_italic &&
113			strcmp(thiz->fontname, other->fontname) == 0);
114
115}
116
117int  ftk_font_desc_is_bold(FtkFontDesc* thiz)
118{
119	return_val_if_fail(thiz != NULL, 0);
120
121	return thiz->is_bold;
122}
123
124int  ftk_font_desc_is_italic(FtkFontDesc* thiz)
125{
126	return_val_if_fail(thiz != NULL, 0);
127
128	return thiz->is_italic;
129}
130
131int  ftk_font_desc_get_size(FtkFontDesc* thiz)
132{
133	return_val_if_fail(thiz != NULL, 0);
134
135	return thiz->size;
136}
137
138Ret  ftk_font_desc_set_bold(FtkFontDesc* thiz, int bold)
139{
140	return_val_if_fail(thiz != NULL, RET_FAIL);
141
142	thiz->is_bold = bold;
143	
144	return RET_OK;
145}
146
147Ret  ftk_font_desc_set_italic(FtkFontDesc* thiz, int italic)
148{
149	return_val_if_fail(thiz != NULL, RET_FAIL);
150
151	thiz->is_italic = italic;
152
153	return RET_OK;
154}
155
156Ret  ftk_font_desc_set_size(FtkFontDesc* thiz, int size)
157{
158	return_val_if_fail(thiz != NULL, RET_FAIL);
159
160	thiz->size = size;
161
162	return RET_OK;
163}
164
165const char *ftk_font_desc_get_fontname(FtkFontDesc* thiz)
166{
167    return_val_if_fail(thiz != NULL && thiz->fontname != NULL, NULL);
168
169    return thiz->fontname;
170}
171
172Ret ftk_font_desc_get_string(FtkFontDesc* thiz, char* desc, size_t len)
173{
174	return_val_if_fail(thiz != NULL && desc != NULL, RET_FAIL);
175
176	ftk_snprintf(desc, len, FONT_DESC_FMT, 
177		thiz->size, thiz->is_bold, thiz->is_italic, thiz->fontname);
178
179	return RET_OK;
180}
181
182static void ftk_font_desc_destroy(FtkFontDesc* thiz)
183{
184	if(thiz != NULL)
185	{
186	    if (thiz->fontname != NULL) {
187	        FTK_FREE(thiz->fontname);
188	    }
189		FTK_FREE(thiz);
190	}
191
192	return;
193}
194
195int ftk_font_desc_ref(FtkFontDesc* thiz)
196{
197	return_val_if_fail(thiz != NULL, 0);
198
199	thiz->ref++;
200
201	return thiz->ref;
202}
203
204int ftk_font_desc_unref(FtkFontDesc* thiz)
205{
206	int ret = 0;
207	assert(thiz != NULL && thiz->ref > 0);
208	return_val_if_fail(thiz != NULL, 0);
209
210	ret = --thiz->ref;
211
212	if(ret == 0)
213	{
214		ftk_font_desc_destroy(thiz);
215	}
216
217	return ret;
218}
219
220static FtkFontDesc* s_default_font_desc;
221
222FtkFontDesc* ftk_default_font()
223{
224	if(s_default_font_desc == NULL)
225	{
226		s_default_font_desc = ftk_font_desc_create(FTK_DEFAULT_FONT);
227	}
228
229	ftk_font_desc_ref(s_default_font_desc);
230
231	return s_default_font_desc;
232}