ufont.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 (256u * 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 (256 KB). Returns 0 on success. */
  28. int ufont_arena_init(void);
  29. void ufont_reset(void);
  30. /* Open a face from memory or from a VFS path. The path form lives in
  31. * ufont_file.c and is not linked into the host tests. */
  32. struct ufont_face *ufont_face_open_mem(const void *data, unsigned int len);
  33. struct ufont_face *ufont_face_open_path(const char *path);
  34. void ufont_metrics(const struct ufont_face *face, int px,
  35. struct ufont_metrics *out);
  36. /* Width in whole pixels. n < 0 means NUL-terminated; utf8 selects the
  37. * decoder (0 = Latin-1 / one byte per codepoint). */
  38. int ufont_text_width(const struct ufont_face *face, int px,
  39. const char *s, int n, int utf8);
  40. /* Cached alpha bitmap for one codepoint. */
  41. const struct ufont_glyph *ufont_glyph(const struct ufont_face *face, int px,
  42. unsigned int cp);
  43. /* Whether the face has a real glyph for the codepoint, and its advance.
  44. * Used by the ngui fallback stack before opening/rasterizing a mask. */
  45. int ufont_has_glyph(const struct ufont_face *face, unsigned int cp);
  46. int ufont_advance_cp(const struct ufont_face *face, int px,
  47. unsigned int cp);
  48. /* Decode one UTF-8 codepoint. Advances *i; returns U+FFFD for invalid
  49. * sequences. n < 0 means NUL-terminated. */
  50. unsigned int ufont_utf8_next(const char *s, int *i, int n);
  51. /* Alpha blend one glyph. The mask is positioned at
  52. * (x + g->xoff, baseline + g->yoff). Returns the number of touched pixels. */
  53. int ufont_blit_rgb565(unsigned short *px, int w, int h,
  54. int x, int baseline, const struct ufont_glyph *g,
  55. unsigned short color,
  56. int cx, int cy, int cw, int ch);
  57. int ufont_blit_rgb(void *px, int w, int h, int pitch, int bpp,
  58. int x, int baseline, const struct ufont_glyph *g,
  59. unsigned int rgb,
  60. int cx, int cy, int cw, int ch);
  61. #endif