ufont.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. /* Alpha blend one glyph. The mask is positioned at
  44. * (x + g->xoff, baseline + g->yoff). Returns the number of touched pixels. */
  45. int ufont_blit_rgb565(unsigned short *px, int w, int h,
  46. int x, int baseline, const struct ufont_glyph *g,
  47. unsigned short color,
  48. int cx, int cy, int cw, int ch);
  49. int ufont_blit_rgb(void *px, int w, int h, int pitch, int bpp,
  50. int x, int baseline, const struct ufont_glyph *g,
  51. unsigned int rgb,
  52. int cx, int cy, int cw, int ch);
  53. #endif