Font.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /**
  2. * Copyright (c) 2006-2016 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  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. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. // LOVE
  21. #include "Font.h"
  22. #include "BMFontRasterizer.h"
  23. #include "ImageRasterizer.h"
  24. #include "libraries/utf8/utf8.h"
  25. namespace love
  26. {
  27. namespace font
  28. {
  29. // Default TrueType font.
  30. #include "Vera.ttf.h"
  31. class DefaultFontData : public love::Data
  32. {
  33. public:
  34. void *getData() const override { return Vera_ttf; }
  35. size_t getSize() const override { return sizeof(Vera_ttf); }
  36. };
  37. Rasterizer *Font::newTrueTypeRasterizer(int size, TrueTypeRasterizer::Hinting hinting)
  38. {
  39. StrongRef<DefaultFontData> data(new DefaultFontData, Acquire::NORETAIN);
  40. return newTrueTypeRasterizer(data.get(), size, hinting);
  41. }
  42. Rasterizer *Font::newTrueTypeRasterizer(int size, float pixeldensity, TrueTypeRasterizer::Hinting hinting)
  43. {
  44. StrongRef<DefaultFontData> data(new DefaultFontData, Acquire::NORETAIN);
  45. return newTrueTypeRasterizer(data.get(), size, pixeldensity, hinting);
  46. }
  47. Rasterizer *Font::newBMFontRasterizer(love::filesystem::FileData *fontdef, const std::vector<image::ImageData *> &images, float pixeldensity)
  48. {
  49. return new BMFontRasterizer(fontdef, images, pixeldensity);
  50. }
  51. Rasterizer *Font::newImageRasterizer(love::image::ImageData *data, const std::string &text, int extraspacing, float pixeldensity)
  52. {
  53. std::vector<uint32> glyphs;
  54. glyphs.reserve(text.size());
  55. try
  56. {
  57. utf8::iterator<std::string::const_iterator> i(text.begin(), text.begin(), text.end());
  58. utf8::iterator<std::string::const_iterator> end(text.end(), text.begin(), text.end());
  59. while (i != end)
  60. glyphs.push_back(*i++);
  61. }
  62. catch (utf8::exception &e)
  63. {
  64. throw love::Exception("UTF-8 decoding error: %s", e.what());
  65. }
  66. return newImageRasterizer(data, &glyphs[0], (int) glyphs.size(), extraspacing, pixeldensity);
  67. }
  68. Rasterizer *Font::newImageRasterizer(love::image::ImageData *data, uint32 *glyphs, int numglyphs, int extraspacing, float pixeldensity)
  69. {
  70. return new ImageRasterizer(data, glyphs, numglyphs, extraspacing, pixeldensity);
  71. }
  72. GlyphData *Font::newGlyphData(Rasterizer *r, const std::string &text)
  73. {
  74. uint32 codepoint = 0;
  75. try
  76. {
  77. codepoint = utf8::peek_next(text.begin(), text.end());
  78. }
  79. catch (utf8::exception &e)
  80. {
  81. throw love::Exception("UTF-8 decoding error: %s", e.what());
  82. }
  83. return r->getGlyphData(codepoint);
  84. }
  85. GlyphData *Font::newGlyphData(Rasterizer *r, uint32 glyph)
  86. {
  87. return r->getGlyphData(glyph);
  88. }
  89. } // font
  90. } // love