ImageRasterizer.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /**
  2. * Copyright (c) 2006-2017 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 "ImageRasterizer.h"
  22. #include "common/Exception.h"
  23. #include <string.h>
  24. namespace love
  25. {
  26. namespace font
  27. {
  28. static_assert(sizeof(Color) == 4, "sizeof(Color) must equal 4 bytes!");
  29. ImageRasterizer::ImageRasterizer(love::image::ImageData *data, uint32 *glyphs, int numglyphs, int extraspacing, float pixeldensity)
  30. : imageData(data)
  31. , glyphs(glyphs)
  32. , numglyphs(numglyphs)
  33. , extraSpacing(extraspacing)
  34. {
  35. this->pixelDensity = pixeldensity;
  36. if (data->getFormat() != PIXELFORMAT_RGBA8)
  37. throw love::Exception("Only 32-bit RGBA images are supported in Image Fonts!");
  38. load();
  39. }
  40. ImageRasterizer::~ImageRasterizer()
  41. {
  42. }
  43. int ImageRasterizer::getLineHeight() const
  44. {
  45. return getHeight();
  46. }
  47. GlyphData *ImageRasterizer::getGlyphData(uint32 glyph) const
  48. {
  49. GlyphMetrics gm = {};
  50. // Set relevant glyph metrics if the glyph is in this ImageFont
  51. std::map<uint32, ImageGlyphData>::const_iterator it = imageGlyphs.find(glyph);
  52. if (it != imageGlyphs.end())
  53. {
  54. gm.width = it->second.width;
  55. gm.advance = it->second.width + extraSpacing;
  56. }
  57. gm.height = metrics.height;
  58. GlyphData *g = new GlyphData(glyph, gm, PIXELFORMAT_RGBA8);
  59. if (gm.width == 0)
  60. return g;
  61. // We don't want another thread modifying our ImageData mid-copy.
  62. love::thread::Lock lock(imageData->getMutex());
  63. Color *gdpixels = (Color *) g->getData();
  64. const Color *imagepixels = (const Color *) imageData->getData();
  65. // copy glyph pixels from imagedata to glyphdata
  66. for (int i = 0; i < g->getWidth() * g->getHeight(); i++)
  67. {
  68. Color p = imagepixels[it->second.x + (i % gm.width) + (imageData->getWidth() * (i / gm.width))];
  69. // Use transparency instead of the spacer color
  70. if (p == spacer)
  71. gdpixels[i] = Color(0, 0, 0, 0);
  72. else
  73. gdpixels[i] = p;
  74. }
  75. return g;
  76. }
  77. void ImageRasterizer::load()
  78. {
  79. const Color *pixels = (const Color *) imageData->getData();
  80. int imgw = imageData->getWidth();
  81. int imgh = imageData->getHeight();
  82. // We don't want another thread modifying our ImageData mid-parse.
  83. love::thread::Lock lock(imageData->getMutex());
  84. // Set the only metric that matters
  85. metrics.height = imgh;
  86. // Reading texture data begins
  87. spacer = pixels[0];
  88. int start = 0;
  89. int end = 0;
  90. for (int i = 0; i < numglyphs; ++i)
  91. {
  92. start = end;
  93. // Finds out where the first character starts
  94. while (start < imgw && pixels[start] == spacer)
  95. ++start;
  96. end = start;
  97. // Find where glyph ends.
  98. while (end < imgw && pixels[end] != spacer)
  99. ++end;
  100. if (start >= end)
  101. break;
  102. ImageGlyphData imageGlyph;
  103. imageGlyph.x = start;
  104. imageGlyph.width = end - start;
  105. imageGlyphs[glyphs[i]] = imageGlyph;
  106. }
  107. }
  108. int ImageRasterizer::getGlyphCount() const
  109. {
  110. return numglyphs;
  111. }
  112. bool ImageRasterizer::hasGlyph(uint32 glyph) const
  113. {
  114. return imageGlyphs.find(glyph) != imageGlyphs.end();
  115. }
  116. Rasterizer::DataType ImageRasterizer::getDataType() const
  117. {
  118. return DATA_IMAGE;
  119. }
  120. } // font
  121. } // love