ttf_parser.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #ifndef TTF_PARSER_H
  2. #define TTF_PARSER_H
  3. #include <stdint.h>
  4. #include <stddef.h>
  5. #define TTF_OK 0
  6. #define TTF_ERR_IO -1
  7. #define TTF_ERR_FORMAT -2
  8. #define TTF_ERR_UNSUPPORTED -3
  9. #define TTF_ERR_RANGE -4
  10. #define TTF_ERR_OOM -5
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. typedef struct {
  15. uint8_t *data;
  16. size_t size;
  17. uint16_t unitsPerEm;
  18. uint16_t numGlyphs;
  19. uint16_t indexToLocFormat;
  20. uint32_t cmapOffset;
  21. uint32_t cmapLength;
  22. uint32_t glyfOffset;
  23. uint32_t locaOffset;
  24. uint32_t headOffset;
  25. /* 1.0 内存优化新增:流式读取支持与小表缓存 */
  26. void *file; /* VFS 文件句柄 (luat_fs_fopen 返回的 FILE*),无数据整读时有效 */
  27. size_t fileSize; /* 字体文件大小 */
  28. uint8_t streaming; /* 1 表示未整读,仅按需读取 */
  29. uint8_t ownsData; /* 1 表示 data 由解析器分配并在 unload 释放;0 表示外部内存(不可释放) */
  30. uint8_t *cmapBuf; /* 常驻内存的 cmap 子表数据(按需加载) */
  31. uint32_t cmapBufLen; /* cmapBuf 长度 */
  32. uint16_t cmapFormat; /* 4 或 12,标记当前常驻的 cmap 子表格式 */
  33. uint32_t cmapBufOffset; /* 缓存子表的绝对偏移,便于快速判断是否命中 */
  34. } TtfFont;
  35. typedef struct {
  36. int16_t x;
  37. int16_t y;
  38. uint8_t onCurve;
  39. } TtfPoint;
  40. typedef struct {
  41. int16_t minX;
  42. int16_t minY;
  43. int16_t maxX;
  44. int16_t maxY;
  45. uint16_t contourCount;
  46. uint16_t *contourEnds;
  47. uint16_t pointCount;
  48. TtfPoint *points;
  49. } TtfGlyph;
  50. typedef struct {
  51. uint32_t width;
  52. uint32_t height;
  53. int32_t originX;
  54. int32_t originY;
  55. float scale;
  56. uint8_t *pixels;
  57. } TtfBitmap;
  58. int ttf_load_from_file(const char *path, TtfFont *font);
  59. int ttf_load_from_memory(const uint8_t *data, size_t size, TtfFont *font);
  60. void ttf_unload(TtfFont *font);
  61. /* 调试开关:1 开启详细日志,0 关闭 */
  62. int ttf_set_debug(int enable);
  63. int ttf_get_debug(void);
  64. int ttf_get_supersample_rate(void);
  65. /* 运行时设置超采样率:仅允许 1(无AA)、2(2x2)、4(4x4),非法值将被修正到最近的允许值 */
  66. int ttf_set_supersample_rate(int rate);
  67. int ttf_lookup_glyph_index(const TtfFont *font, uint32_t codepoint, uint16_t *glyphIndex);
  68. int ttf_load_glyph(const TtfFont *font, uint16_t glyphIndex, TtfGlyph *glyph);
  69. void ttf_free_glyph(TtfGlyph *glyph);
  70. int ttf_rasterize_glyph(const TtfFont *font, const TtfGlyph *glyph, int ppem, TtfBitmap *bitmap);
  71. void ttf_free_bitmap(TtfBitmap *bitmap);
  72. #ifdef __cplusplus
  73. }
  74. #endif
  75. #endif /* TTF_PARSER_H */