font.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * File : font.h
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2009-10-16 Bernard first version
  13. */
  14. #ifndef __RTGUI_FONT_H__
  15. #define __RTGUI_FONT_H__
  16. #include <rtgui/rtgui.h>
  17. #include <rtgui/list.h>
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. struct rtgui_font;
  22. struct rtgui_dc;
  23. struct rtgui_rect;
  24. struct rtgui_font_engine
  25. {
  26. /* font engine function */
  27. void (*font_init)(struct rtgui_font *font);
  28. void (*font_load)(struct rtgui_font *font);
  29. void (*font_draw_text)(struct rtgui_font *font, struct rtgui_dc *dc, const char *text,
  30. rt_ubase_t len, struct rtgui_rect *rect);
  31. void (*font_get_metrics)(struct rtgui_font *font, const char *text, struct rtgui_rect *rect);
  32. };
  33. /*
  34. * bitmap font engine
  35. */
  36. struct rtgui_font_bitmap
  37. {
  38. const rt_uint8_t *bmp; /* bitmap font data */
  39. const rt_uint8_t *char_width; /* each character width, NULL for fixed font */
  40. const rt_uint32_t *offset; /* offset for each character */
  41. rt_uint16_t width; /* font width */
  42. rt_uint16_t height; /* font height */
  43. rt_uint8_t first_char;
  44. rt_uint8_t last_char;
  45. };
  46. extern const struct rtgui_font_engine bmp_font_engine;
  47. #include <rtgui/tree.h>
  48. SPLAY_HEAD(cache_tree, hz_cache);
  49. struct hz_cache
  50. {
  51. SPLAY_ENTRY(hz_cache) hz_node;
  52. rt_uint16_t hz_id;
  53. };
  54. struct rtgui_hz_file_font
  55. {
  56. struct cache_tree cache_root;
  57. rt_uint16_t cache_size;
  58. /* font size */
  59. rt_uint16_t font_size;
  60. rt_uint16_t font_data_size;
  61. /* file descriptor */
  62. int fd;
  63. /* font file name */
  64. const char *font_fn;
  65. };
  66. extern const struct rtgui_font_engine rtgui_hz_file_font_engine;
  67. struct rtgui_font
  68. {
  69. /* font name */
  70. char *family;
  71. /* font height */
  72. rt_uint16_t height;
  73. /* refer count */
  74. rt_uint32_t refer_count;
  75. /* font engine */
  76. const struct rtgui_font_engine *engine;
  77. /* font private data */
  78. void *data;
  79. /* the font list */
  80. rtgui_list_t list;
  81. };
  82. typedef struct rtgui_font rtgui_font_t;
  83. void rtgui_font_system_init(void);
  84. void rtgui_font_system_add_font(struct rtgui_font *font);
  85. void rtgui_font_system_remove_font(struct rtgui_font *font);
  86. struct rtgui_font *rtgui_font_default(void);
  87. void rtgui_font_set_defaut(struct rtgui_font *font);
  88. struct rtgui_font *rtgui_font_refer(const char *family, rt_uint16_t height);
  89. void rtgui_font_derefer(struct rtgui_font *font);
  90. /* draw a text */
  91. void rtgui_font_draw(struct rtgui_font *font, struct rtgui_dc *dc, const char *text, rt_ubase_t len, struct rtgui_rect *rect);
  92. int rtgui_font_get_string_width(struct rtgui_font *font, const char *text);
  93. void rtgui_font_get_metrics(struct rtgui_font *font, const char *text, struct rtgui_rect *rect);
  94. /* used by stract font */
  95. #define FONT_BMP_DATA_BEGIN
  96. #define FONT_BMP_DATA_END
  97. struct rtgui_char_position
  98. {
  99. /* Keep the size of this struct within 4 bytes so it can be passed by
  100. * value. */
  101. /* How long this char is. */
  102. rt_uint16_t char_width;
  103. /* How many bytes remaining from current pointer. At least, it will be 1. */
  104. rt_uint16_t remain;
  105. };
  106. /*
  107. * @len the length of @str.
  108. * @offset the char offset on the string to check with.
  109. */
  110. struct rtgui_char_position _string_char_width(char *str, rt_size_t len, rt_size_t offset);
  111. #ifdef __cplusplus
  112. }
  113. #endif
  114. #endif