font_resource.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (c) 2012-2015 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. Buffer buf = opts.read(path);
  29. JSONParser json(buf);
  30. JSONElement root = json.root();
  31. Array<FontGlyphData> m_glyphs(default_allocator());
  32. JSONElement count = root.key("count");
  33. JSONElement size = root.key("size");
  34. JSONElement font_size = root.key("font_size");
  35. JSONElement glyphs = root.key("glyphs");
  36. uint32_t num_glyphs = count.to_int();
  37. for (uint32_t i = 0; i < num_glyphs; i++)
  38. {
  39. FontGlyphData data;
  40. parse_glyph(glyphs[i], data);
  41. array::push_back(m_glyphs, data);
  42. }
  43. FontResource fr;
  44. fr.version = FONT_VERSION;
  45. fr.num_glyphs = array::size(m_glyphs);
  46. fr.texture_size = size.to_int();
  47. fr.font_size = font_size.to_int();
  48. opts.write(fr.version);
  49. opts.write(fr.num_glyphs);
  50. opts.write(fr.texture_size);
  51. opts.write(fr.font_size);
  52. for (uint32_t i = 0; i < array::size(m_glyphs); i++)
  53. {
  54. opts.write(m_glyphs[i].id);
  55. opts.write(m_glyphs[i].x);
  56. opts.write(m_glyphs[i].y);
  57. opts.write(m_glyphs[i].width);
  58. opts.write(m_glyphs[i].height);
  59. opts.write(m_glyphs[i].x_offset);
  60. opts.write(m_glyphs[i].y_offset);
  61. opts.write(m_glyphs[i].x_advance);
  62. }
  63. }
  64. void* load(File& file, Allocator& a)
  65. {
  66. const size_t file_size = file.size();
  67. void* res = a.allocate(file_size);
  68. file.read(res, file_size);
  69. return res;
  70. }
  71. void online(StringId64 /*id*/, ResourceManager& /*rm*/)
  72. {
  73. }
  74. void offline(StringId64 /*id*/, ResourceManager& /*rm*/)
  75. {
  76. }
  77. void unload(Allocator& allocator, void* resource)
  78. {
  79. allocator.deallocate(resource);
  80. }
  81. uint32_t num_glyphs(const FontResource* fr)
  82. {
  83. return fr->num_glyphs;
  84. }
  85. uint32_t texture_size(const FontResource* fr)
  86. {
  87. return fr->texture_size;
  88. }
  89. uint32_t font_size(const FontResource* fr)
  90. {
  91. return fr->font_size;
  92. }
  93. const FontGlyphData* get_glyph(const FontResource* fr, uint32_t i)
  94. {
  95. CE_ASSERT(i < num_glyphs(fr), "Index out of bounds");
  96. FontGlyphData* begin = (FontGlyphData*)((char*)fr + sizeof(FontResource));
  97. for (uint32_t i = 0; i < num_glyphs(fr); i++)
  98. {
  99. if (begin[i].id == i)
  100. return &begin[i];
  101. }
  102. CE_FATAL("Glyph not found");
  103. return NULL;
  104. }
  105. } // namespace font_resource
  106. } // namespace crown