fontstash.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //
  2. // Copyright (c) 2011 Andreas Krinke [email protected]
  3. // Copyright (c) 2009 Mikko Mononen [email protected]
  4. //
  5. // This software is provided 'as-is', without any express or implied
  6. // warranty. In no event will the authors be held liable for any damages
  7. // arising from the use of this software.
  8. // Permission is granted to anyone to use this software for any purpose,
  9. // including commercial applications, and to alter it and redistribute it
  10. // freely, subject to the following restrictions:
  11. // 1. The origin of this software must not be misrepresented; you must not
  12. // claim that you wrote the original software. If you use this software
  13. // in a product, an acknowledgment in the product documentation would be
  14. // appreciated but is not required.
  15. // 2. Altered source versions must be plainly marked as such, and must not be
  16. // misrepresented as being the original software.
  17. // 3. This notice may not be removed or altered from any source distribution.
  18. //
  19. #ifndef FONTSTASH_H
  20. #define FONTSTASH_H
  21. #define MAX_ROWS 128
  22. #define VERT_COUNT (6*128)
  23. #define INDEX_COUNT (VERT_COUNT*2)
  24. struct vec2
  25. {
  26. vec2(float x, float y)
  27. {
  28. p[0] = x;
  29. p[1] = y;
  30. }
  31. float p[2];
  32. };
  33. struct vec4
  34. {
  35. vec4(float x,float y, float z, float w)
  36. {
  37. p[0] = x;
  38. p[1] = y;
  39. p[2] = z;
  40. p[3] = w;
  41. }
  42. float p[4];
  43. };
  44. typedef struct
  45. {
  46. vec4 position;
  47. vec4 colour;
  48. vec2 uv;
  49. } Vertex;
  50. struct sth_quad
  51. {
  52. float x0,y0,s0,t0;
  53. float x1,y1,s1,t1;
  54. };
  55. struct sth_row
  56. {
  57. short x,y,h;
  58. };
  59. struct sth_glyph
  60. {
  61. unsigned int codepoint;
  62. short size;
  63. struct sth_texture* texture;
  64. int x0_,y0,x1,y1;
  65. float xadv,xoff,yoff;
  66. int next;
  67. };
  68. struct sth_texture
  69. {
  70. union
  71. {
  72. void* m_userData;
  73. int m_userId;
  74. };
  75. unsigned char* m_texels;
  76. // TODO: replace rows with pointer
  77. struct sth_row rows[MAX_ROWS];
  78. int nrows;
  79. int nverts;
  80. Vertex newverts[VERT_COUNT];
  81. struct sth_texture* next;
  82. };
  83. struct RenderCallbacks
  84. {
  85. virtual ~RenderCallbacks() {}
  86. virtual void updateTexture(sth_texture* texture, sth_glyph* glyph, int textureWidth, int textureHeight)=0;
  87. virtual void render(sth_texture* texture)=0;
  88. };
  89. struct sth_stash* sth_create(int cachew, int cacheh, RenderCallbacks* callbacks);
  90. int sth_add_font(struct sth_stash* stash, const char* path);
  91. int sth_add_font_from_memory(struct sth_stash* stash, unsigned char* buffer);
  92. int sth_add_bitmap_font(struct sth_stash* stash, int ascent, int descent, int line_gap);
  93. /*void sth_add_glyph(struct sth_stash* stash, int idx, unsigned int uid, const char* s,
  94. short size, short base, int x, int y, int w, int h,
  95. float xoffset, float yoffset, float xadvance);
  96. */
  97. void sth_begin_draw(struct sth_stash* stash);
  98. void sth_end_draw(struct sth_stash* stash);
  99. void sth_draw_texture(struct sth_stash* stash,
  100. int idx, float size,
  101. float x, float y,
  102. int screenwidth, int screenheight,
  103. const char* s, float* dx);
  104. void sth_flush_draw(struct sth_stash* stash);
  105. void sth_draw_text(struct sth_stash* stash,
  106. int idx, float size,
  107. float x, float y, const char* string, float* dx, int screenwidth, int screenheight, int measureOnly=0, float retinaScale=1);
  108. void sth_dim_text(struct sth_stash* stash, int idx, float size, const char* string,
  109. float* minx, float* miny, float* maxx, float* maxy);
  110. void sth_vmetrics(struct sth_stash* stash,
  111. int idx, float size,
  112. float* ascender, float* descender, float * lineh);
  113. void sth_delete(struct sth_stash* stash);
  114. #endif // FONTSTASH_H