font_resource.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) 2012-2014 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #include "font_resource.h"
  6. #include "json_parser.h"
  7. #include "allocator.h"
  8. #include "filesystem.h"
  9. #include "string_utils.h"
  10. #include "compile_options.h"
  11. namespace crown
  12. {
  13. namespace font_resource
  14. {
  15. void parse_glyph(JSONElement e, FontGlyphData& glyph)
  16. {
  17. glyph.id = e.key("id").to_int();
  18. glyph.x = e.key("x").to_int();
  19. glyph.y = e.key("y").to_int();
  20. glyph.width = e.key("width").to_int();
  21. glyph.height = e.key("height").to_int();
  22. glyph.x_offset = e.key("x_offset").to_float();
  23. glyph.y_offset = e.key("y_offset").to_float();
  24. glyph.x_advance = e.key("x_advance").to_float();
  25. }
  26. void compile(const char* path, CompileOptions& opts)
  27. {
  28. static const uint32_t VERSION = 1;
  29. Buffer buf = opts.read(path);
  30. JSONParser json(array::begin(buf));
  31. JSONElement root = json.root();
  32. Array<FontGlyphData> m_glyphs(default_allocator());
  33. JSONElement count = root.key("count");
  34. JSONElement size = root.key("size");
  35. JSONElement font_size = root.key("font_size");
  36. JSONElement glyphs = root.key("glyphs");
  37. uint32_t num_glyphs = count.to_int();
  38. for (uint32_t i = 0; i < num_glyphs; i++)
  39. {
  40. FontGlyphData data;
  41. parse_glyph(glyphs[i], data);
  42. array::push_back(m_glyphs, data);
  43. }
  44. FontResource fr;
  45. fr.version = VERSION;
  46. fr.num_glyphs = array::size(m_glyphs);
  47. fr.texture_size = size.to_int();
  48. fr.font_size = font_size.to_int();
  49. opts.write(fr.version);
  50. opts.write(fr.num_glyphs);
  51. opts.write(fr.texture_size);
  52. opts.write(fr.font_size);
  53. for (uint32_t i = 0; i < array::size(m_glyphs); i++)
  54. {
  55. opts.write(m_glyphs[i].id);
  56. opts.write(m_glyphs[i].x);
  57. opts.write(m_glyphs[i].y);
  58. opts.write(m_glyphs[i].width);
  59. opts.write(m_glyphs[i].height);
  60. opts.write(m_glyphs[i].x_offset);
  61. opts.write(m_glyphs[i].y_offset);
  62. opts.write(m_glyphs[i].x_advance);
  63. }
  64. }
  65. void* load(File& file, Allocator& a)
  66. {
  67. const size_t file_size = file.size();
  68. void* res = a.allocate(file_size);
  69. file.read(res, file_size);
  70. return res;
  71. }
  72. void online(StringId64 /*id*/, ResourceManager& /*rm*/)
  73. {
  74. }
  75. void offline(StringId64 /*id*/, ResourceManager& /*rm*/)
  76. {
  77. }
  78. void unload(Allocator& allocator, void* resource)
  79. {
  80. allocator.deallocate(resource);
  81. }
  82. uint32_t num_glyphs(const FontResource* fr)
  83. {
  84. return fr->num_glyphs;
  85. }
  86. uint32_t texture_size(const FontResource* fr)
  87. {
  88. return fr->texture_size;
  89. }
  90. uint32_t font_size(const FontResource* fr)
  91. {
  92. return fr->font_size;
  93. }
  94. const FontGlyphData* get_glyph(const FontResource* fr, uint32_t i)
  95. {
  96. CE_ASSERT(i < num_glyphs(fr), "Index out of bounds");
  97. FontGlyphData* begin = (FontGlyphData*)((char*)fr + sizeof(FontResource));
  98. for (uint32_t i = 0; i < num_glyphs(fr); i++)
  99. {
  100. if (begin[i].id == i)
  101. return &begin[i];
  102. }
  103. CE_FATAL("Glyph not found");
  104. return NULL;
  105. }
  106. } // namespace font_resource
  107. } // namespace crown