FontResource.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "Types.h"
  25. #include "Resource.h"
  26. #include "Bundle.h"
  27. #include "Allocator.h"
  28. #include "File.h"
  29. #include "Assert.h"
  30. #include "Log.h"
  31. namespace crown
  32. {
  33. // Glyph metrics:
  34. // --------------
  35. //
  36. // x_min|<-------- width -------->|x_max
  37. // | |
  38. // | +-------------------------+--------------- y_max
  39. // | | ggggggggg ggggg | ^ ^
  40. // | | g:::::::::ggg::::g | | |
  41. // | | g:::::::::::::::::g | | |
  42. // | | g::::::ggggg::::::gg | | |
  43. // | | g:::::g g:::::g | | |
  44. // x_offset -|-------->| g:::::g g:::::g | y_offset |
  45. // | | g:::::g g:::::g | | |
  46. // | | g::::::g g:::::g | | |
  47. // | | g:::::::ggggg:::::g | | |
  48. // | | g::::::::::::::::g | | height
  49. // | | gg::::::::::::::g | | |
  50. // baseline ---*---------|---- gggggggg::::::g-----*-------- |
  51. // / | | g:::::g | |
  52. // origin | | gggggg g:::::g | |
  53. // | | g:::::gg gg:::::g | |
  54. // | | g::::::ggg:::::::g | |
  55. // | | gg:::::::::::::g | |
  56. // | | ggg::::::ggg | |
  57. // | | gggggg | v
  58. // | +-------------------------+--------------- y_min
  59. // | |
  60. // |------------- x_advance ---------->|
  61. //-----------------------------------------------------------------------------
  62. struct FontHeader
  63. {
  64. ResourceId material;
  65. uint32_t num_glyphs;
  66. uint32_t texture_size; // Font texture size -- pow of 2
  67. uint32_t font_size;
  68. };
  69. //-----------------------------------------------------------------------------
  70. struct FontGlyphData
  71. {
  72. uint32_t id;
  73. uint32_t x;
  74. uint32_t y;
  75. uint32_t width;
  76. uint32_t height;
  77. float x_offset;
  78. float y_offset;
  79. float x_advance;
  80. };
  81. //-----------------------------------------------------------------------------
  82. class FontResource
  83. {
  84. public:
  85. //-----------------------------------------------------------------------------
  86. static void* load(Allocator& allocator, Bundle& bundle, ResourceId id)
  87. {
  88. File* file = bundle.open(id);
  89. const size_t file_size = file->size();
  90. void* res = allocator.allocate(file_size);
  91. file->read(res, file_size);
  92. bundle.close(file);
  93. return res;
  94. }
  95. //-----------------------------------------------------------------------------
  96. static void online(void* resource)
  97. {
  98. (void)resource;
  99. // TODO
  100. }
  101. //-----------------------------------------------------------------------------
  102. static void unload(Allocator& allocator, void* resource)
  103. {
  104. CE_ASSERT_NOT_NULL(resource);
  105. allocator.deallocate(resource);
  106. }
  107. //-----------------------------------------------------------------------------
  108. static void offline(void* /*resource*/)
  109. {
  110. // TODO
  111. }
  112. //-----------------------------------------------------------------------------
  113. ResourceId material() const
  114. {
  115. return ((FontHeader*) this)->material;
  116. }
  117. //-----------------------------------------------------------------------------
  118. uint32_t num_glyphs() const
  119. {
  120. return ((FontHeader*)this)->num_glyphs;
  121. }
  122. //-----------------------------------------------------------------------------
  123. uint32_t texture_size() const
  124. {
  125. return ((FontHeader*)this)->texture_size;
  126. }
  127. //-----------------------------------------------------------------------------
  128. uint32_t font_size() const
  129. {
  130. return ((FontHeader*)this)->font_size;
  131. }
  132. //-----------------------------------------------------------------------------
  133. FontGlyphData get_glyph(const uint32_t index) const
  134. {
  135. CE_ASSERT(index < num_glyphs(), "Index out of bounds");
  136. FontGlyphData* begin = (FontGlyphData*) (((char*) this) + sizeof(FontHeader));
  137. for (uint32_t i = 0; i < num_glyphs(); i++)
  138. {
  139. if (begin[i].id == index)
  140. {
  141. return begin[i];
  142. }
  143. }
  144. CE_FATAL("Glyph not found");
  145. }
  146. private:
  147. // Disable construction
  148. FontResource();
  149. };
  150. } // namespace crown