font.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * File : font.h
  3. * This file is part of RT-Thread GUI Engine
  4. * COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2009-10-16 Bernard first version
  23. */
  24. #ifndef __RTGUI_FONT_H__
  25. #define __RTGUI_FONT_H__
  26. #include <rtgui/rtgui.h>
  27. #include <rtgui/list.h>
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. struct rtgui_font;
  32. struct rtgui_dc;
  33. struct rtgui_rect;
  34. struct rtgui_font_engine
  35. {
  36. /* font engine function */
  37. void (*font_init)(struct rtgui_font *font);
  38. void (*font_load)(struct rtgui_font *font);
  39. void (*font_draw_text)(struct rtgui_font *font, struct rtgui_dc *dc, const char *text,
  40. rt_ubase_t len, struct rtgui_rect *rect);
  41. void (*font_get_metrics)(struct rtgui_font *font, const char *text, struct rtgui_rect *rect);
  42. };
  43. /*
  44. * bitmap font engine
  45. */
  46. struct rtgui_font_bitmap
  47. {
  48. const rt_uint8_t *bmp; /* bitmap font data */
  49. const rt_uint8_t *char_width; /* each character width, NULL for fixed font */
  50. const rt_uint32_t *offset; /* offset for each character */
  51. rt_uint16_t width; /* font width */
  52. rt_uint16_t height; /* font height */
  53. rt_uint8_t first_char;
  54. rt_uint8_t last_char;
  55. };
  56. extern const struct rtgui_font_engine bmp_font_engine;
  57. #include <rtgui/tree.h>
  58. SPLAY_HEAD(cache_tree, hz_cache);
  59. struct hz_cache
  60. {
  61. SPLAY_ENTRY(hz_cache) hz_node;
  62. rt_uint16_t hz_id;
  63. };
  64. struct rtgui_hz_file_font
  65. {
  66. struct cache_tree cache_root;
  67. rt_uint16_t cache_size;
  68. /* font size */
  69. rt_uint16_t font_size;
  70. rt_uint16_t font_data_size;
  71. /* file descriptor */
  72. int fd;
  73. /* font file name */
  74. const char *font_fn;
  75. };
  76. extern const struct rtgui_font_engine rtgui_hz_file_font_engine;
  77. struct rtgui_font
  78. {
  79. /* font name */
  80. char *family;
  81. /* font height */
  82. rt_uint16_t height;
  83. /* refer count */
  84. rt_uint32_t refer_count;
  85. /* font engine */
  86. const struct rtgui_font_engine *engine;
  87. /* font private data */
  88. void *data;
  89. /* the font list */
  90. rtgui_list_t list;
  91. };
  92. typedef struct rtgui_font rtgui_font_t;
  93. void rtgui_font_system_init(void);
  94. void rtgui_font_system_add_font(struct rtgui_font *font);
  95. void rtgui_font_system_remove_font(struct rtgui_font *font);
  96. struct rtgui_font *rtgui_font_default(void);
  97. void rtgui_font_set_defaut(struct rtgui_font *font);
  98. struct rtgui_font *rtgui_font_refer(const char *family, rt_uint16_t height);
  99. void rtgui_font_derefer(struct rtgui_font *font);
  100. /* draw a text */
  101. void rtgui_font_draw(struct rtgui_font *font, struct rtgui_dc *dc, const char *text, rt_ubase_t len, struct rtgui_rect *rect);
  102. int rtgui_font_get_string_width(struct rtgui_font *font, const char *text);
  103. void rtgui_font_get_metrics(struct rtgui_font *font, const char *text, struct rtgui_rect *rect);
  104. /* used by stract font */
  105. #define FONT_BMP_DATA_BEGIN
  106. #define FONT_BMP_DATA_END
  107. struct rtgui_char_position
  108. {
  109. /* Keep the size of this struct within 4 bytes so it can be passed by
  110. * value. */
  111. /* How long this char is. */
  112. rt_uint16_t char_width;
  113. /* How many bytes remaining from current pointer. At least, it will be 1. */
  114. rt_uint16_t remain;
  115. };
  116. /*
  117. * @len the length of @str.
  118. * @offset the char offset on the string to check with.
  119. */
  120. struct rtgui_char_position _string_char_width(char *str, rt_size_t len, rt_size_t offset);
  121. #ifdef __cplusplus
  122. }
  123. #endif
  124. #endif