ufont.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /* Userland text engine: fixed-point TrueType faces, Latin-1/UTF-8 text
  2. * measurement and antialiased glyph bitmaps.
  3. *
  4. * Faces and glyph masks live in one heap arena owned by this module. The
  5. * arena is deliberately reset by ngui_font_detach()/ufont_reset() because the
  6. * browser calls heap_reset() on navigation; no pointer from this module may
  7. * survive that call. */
  8. #ifndef UFONT_H
  9. #define UFONT_H
  10. #include <stddef.h>
  11. #define UFONT_ARENA_BYTES (512u * 1024u)
  12. #define UFONT_MAX_FILE (1024u * 1024u)
  13. #define UFONT_CACHE_SLOTS 512
  14. struct ufont_face;
  15. struct ufont_glyph {
  16. const unsigned char *mask; /* alpha, w*h bytes, row-major */
  17. unsigned short w, h;
  18. short xoff, yoff; /* bitmap origin relative to pen/baseline */
  19. int advance; /* 26.6 pixels */
  20. };
  21. struct ufont_metrics {
  22. int ascent; /* pixels above the baseline */
  23. int descent; /* pixels below the baseline */
  24. int line_gap;
  25. int line_height;
  26. };
  27. /* Allocate the shared arena (UFONT_ARENA_BYTES unless
  28. * ufont_set_arena_bytes() asked for more). Returns 0 on success. */
  29. int ufont_arena_init(void);
  30. void ufont_reset(void);
  31. /* Arena size for the next ufont_arena_init(); an app that loads web fonts
  32. * (the browser) raises it before its first face is opened. 0 restores the
  33. * default. It does not resize an arena that already exists. */
  34. void ufont_set_arena_bytes(unsigned int bytes);
  35. /* Open a face from memory or from a VFS path. The path form lives in
  36. * ufont_file.c and is not linked into the host tests. */
  37. struct ufont_face *ufont_face_open_mem(const void *data, unsigned int len);
  38. struct ufont_face *ufont_face_open_path(const char *path);
  39. /* WOFF 1.0 support (ufont_woff.c, needs inflate): rebuild the sfnt from the
  40. * per-table zlib streams. Returns a malloc'ed TrueType/OpenType image (the
  41. * caller frees it after ufont_face_open_mem()) or 0. WOFF2 is detected but
  42. * not decoded. */
  43. int ufont_is_woff(const void *data, unsigned int len);
  44. int ufont_is_woff2(const void *data, unsigned int len);
  45. unsigned char *ufont_woff_decode(const void *data, unsigned int len,
  46. unsigned int *out_len);
  47. void ufont_metrics(const struct ufont_face *face, int px,
  48. struct ufont_metrics *out);
  49. /* Width in whole pixels. n < 0 means NUL-terminated; utf8 selects the
  50. * decoder (0 = Latin-1 / one byte per codepoint). */
  51. int ufont_text_width(const struct ufont_face *face, int px,
  52. const char *s, int n, int utf8);
  53. /* Cached alpha bitmap for one codepoint. */
  54. const struct ufont_glyph *ufont_glyph(const struct ufont_face *face, int px,
  55. unsigned int cp);
  56. /* Whether the face has a real glyph for the codepoint, and its advance.
  57. * Used by the ngui fallback stack before opening/rasterizing a mask. */
  58. int ufont_has_glyph(const struct ufont_face *face, unsigned int cp);
  59. int ufont_advance_cp(const struct ufont_face *face, int px,
  60. unsigned int cp);
  61. /* Decode one UTF-8 codepoint. Advances *i; returns U+FFFD for invalid
  62. * sequences. n < 0 means NUL-terminated. */
  63. unsigned int ufont_utf8_next(const char *s, int *i, int n);
  64. /* Alpha blend one glyph. The mask is positioned at
  65. * (x + g->xoff, baseline + g->yoff). Returns the number of touched pixels. */
  66. int ufont_blit_rgb565(unsigned short *px, int w, int h,
  67. int x, int baseline, const struct ufont_glyph *g,
  68. unsigned short color,
  69. int cx, int cy, int cw, int ch);
  70. int ufont_blit_rgb(void *px, int w, int h, int pitch, int bpp,
  71. int x, int baseline, const struct ufont_glyph *g,
  72. unsigned int rgb,
  73. int cx, int cy, int cw, int ch);
  74. #endif