/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. * History:
  27. * ================================================================
  28. * 2011-03-20 Li XianJing <xianjimli@hotmail.com> created
  29. *
  30. */
  31. #include "ftk_util.h"
  32. #include "ftk_font_desc.h"
  33. struct _FtkFontDesc
  34. {
  35. int ref;
  36. int size;
  37. int is_bold;
  38. int is_italic;
  39. char *fontname;
  40. };
  41. static Ret ftk_font_desc_parse(FtkFontDesc* thiz, const char* font_desc)
  42. {
  43. const char* p = NULL;
  44. if(font_desc == NULL)
  45. {
  46. #ifdef FTK_FONT_SIZE
  47. thiz->size = FTK_FONT_SIZE;
  48. thiz->is_italic = 0;
  49. thiz->is_bold = 0;
  50. thiz->fontname = ftk_strdup(FTK_FONT);
  51. return RET_OK;
  52. #else
  53. font_desc = font_desc != NULL ? font_desc : FTK_DEFAULT_FONT;
  54. #endif
  55. }
  56. p = strstr(font_desc, "size:");
  57. if(p != NULL)
  58. {
  59. thiz->size = ftk_atoi(p + 5);
  60. }
  61. p = strstr(font_desc, "bold:");
  62. if(p != NULL)
  63. {
  64. thiz->is_bold = ftk_atoi(p + 5);
  65. }
  66. p = strstr(font_desc, "italic:");
  67. if(p != NULL)
  68. {
  69. thiz->is_italic = ftk_atoi(p + 7);
  70. }
  71. p = strstr(font_desc, "fontname:");
  72. if(p != NULL)
  73. {
  74. thiz->fontname = ftk_strdup(p + 9);
  75. }
  76. else
  77. {
  78. thiz->fontname = ftk_strdup(FTK_FONT);
  79. }
  80. return RET_OK;
  81. }
  82. FtkFontDesc* ftk_font_desc_create(const char* font_desc)
  83. {
  84. FtkFontDesc* thiz = FTK_NEW(FtkFontDesc);
  85. if(thiz != NULL)
  86. {
  87. thiz->ref = 1;
  88. ftk_font_desc_parse(thiz, font_desc);
  89. }
  90. return thiz;
  91. }
  92. int ftk_font_desc_is_equal(FtkFontDesc* thiz, FtkFontDesc* other)
  93. {
  94. return_val_if_fail(thiz != NULL && other != NULL, 0);
  95. return (thiz->size == other->size &&
  96. thiz->is_bold == other->is_bold &&
  97. thiz->is_italic == other->is_italic &&
  98. strcmp(thiz->fontname, other->fontname) == 0);
  99. }
  100. int ftk_font_desc_is_bold(FtkFontDesc* thiz)
  101. {
  102. return_val_if_fail(thiz != NULL, 0);
  103. return thiz->is_bold;
  104. }
  105. int ftk_font_desc_is_italic(FtkFontDesc* thiz)
  106. {
  107. return_val_if_fail(thiz != NULL, 0);
  108. return thiz->is_italic;
  109. }
  110. int ftk_font_desc_get_size(FtkFontDesc* thiz)
  111. {
  112. return_val_if_fail(thiz != NULL, 0);
  113. return thiz->size;
  114. }
  115. Ret ftk_font_desc_set_bold(FtkFontDesc* thiz, int bold)
  116. {
  117. return_val_if_fail(thiz != NULL, RET_FAIL);
  118. thiz->is_bold = bold;
  119. return RET_OK;
  120. }
  121. Ret ftk_font_desc_set_italic(FtkFontDesc* thiz, int italic)
  122. {
  123. return_val_if_fail(thiz != NULL, RET_FAIL);
  124. thiz->is_italic = italic;
  125. return RET_OK;
  126. }
  127. Ret ftk_font_desc_set_size(FtkFontDesc* thiz, int size)
  128. {
  129. return_val_if_fail(thiz != NULL, RET_FAIL);
  130. thiz->size = size;
  131. return RET_OK;
  132. }
  133. const char *ftk_font_desc_get_fontname(FtkFontDesc* thiz)
  134. {
  135. return_val_if_fail(thiz != NULL && thiz->fontname != NULL, NULL);
  136. return thiz->fontname;
  137. }
  138. Ret ftk_font_desc_get_string(FtkFontDesc* thiz, char* desc, size_t len)
  139. {
  140. return_val_if_fail(thiz != NULL && desc != NULL, RET_FAIL);
  141. ftk_snprintf(desc, len, FONT_DESC_FMT,
  142. thiz->size, thiz->is_bold, thiz->is_italic, thiz->fontname);
  143. return RET_OK;
  144. }
  145. static void ftk_font_desc_destroy(FtkFontDesc* thiz)
  146. {
  147. if(thiz != NULL)
  148. {
  149. if (thiz->fontname != NULL) {
  150. FTK_FREE(thiz->fontname);
  151. }
  152. FTK_FREE(thiz);
  153. }
  154. return;
  155. }
  156. int ftk_font_desc_ref(FtkFontDesc* thiz)
  157. {
  158. return_val_if_fail(thiz != NULL, 0);
  159. thiz->ref++;
  160. return thiz->ref;
  161. }
  162. int ftk_font_desc_unref(FtkFontDesc* thiz)
  163. {
  164. int ret = 0;
  165. assert(thiz != NULL && thiz->ref > 0);
  166. return_val_if_fail(thiz != NULL, 0);
  167. ret = --thiz->ref;
  168. if(ret == 0)
  169. {
  170. ftk_font_desc_destroy(thiz);
  171. }
  172. return ret;
  173. }
  174. static FtkFontDesc* s_default_font_desc;
  175. FtkFontDesc* ftk_default_font()
  176. {
  177. if(s_default_font_desc == NULL)
  178. {
  179. s_default_font_desc = ftk_font_desc_create(FTK_DEFAULT_FONT);
  180. }
  181. ftk_font_desc_ref(s_default_font_desc);
  182. return s_default_font_desc;
  183. }