ImageRasterizer.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /**
  2. * Copyright (c) 2006-2014 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. inline bool equal(const love::image::pixel &a, const love::image::pixel &b)
  29. {
  30. return (a.r == b.r && a.g == b.g && a.b == b.b && a.a == b.a);
  31. }
  32. ImageRasterizer::ImageRasterizer(love::image::ImageData *data, uint32 *glyphs, int numglyphs)
  33. : imageData(data)
  34. , glyphs(glyphs)
  35. , numglyphs(numglyphs)
  36. {
  37. load();
  38. }
  39. ImageRasterizer::~ImageRasterizer()
  40. {
  41. }
  42. int ImageRasterizer::getLineHeight() const
  43. {
  44. return getHeight();
  45. }
  46. GlyphData *ImageRasterizer::getGlyphData(uint32 glyph) const
  47. {
  48. GlyphMetrics gm = {};
  49. // Set relevant glyph metrics if the glyph is in this ImageFont
  50. std::map<uint32, ImageGlyphData>::const_iterator it = imageGlyphs.find(glyph);
  51. if (it != imageGlyphs.end())
  52. {
  53. gm.width = it->second.width;
  54. gm.advance = it->second.width + it->second.spacing;
  55. }
  56. gm.height = metrics.height;
  57. GlyphData *g = new GlyphData(glyph, gm, GlyphData::FORMAT_RGBA);
  58. if (gm.width == 0)
  59. return g;
  60. // We don't want another thread modifying our ImageData mid-copy.
  61. love::thread::Lock lock(imageData->getMutex());
  62. love::image::pixel *gdpixels = (love::image::pixel *) g->getData();
  63. love::image::pixel *imagepixels = (love::image::pixel *) imageData->getData();
  64. // copy glyph pixels from imagedata to glyphdata
  65. for (int i = 0; i < g->getWidth() * g->getHeight(); i++)
  66. {
  67. love::image::pixel p = imagepixels[it->second.x + (i % gm.width) + (imageData->getWidth() * (i / gm.width))];
  68. // Use transparency instead of the spacer color
  69. if (equal(p, spacer))
  70. gdpixels[i].r = gdpixels[i].g = gdpixels[i].b = gdpixels[i].a = 0;
  71. else
  72. gdpixels[i] = p;
  73. }
  74. return g;
  75. }
  76. void ImageRasterizer::load()
  77. {
  78. love::image::pixel *pixels = (love::image::pixel *) imageData->getData();
  79. int imgw = imageData->getWidth();
  80. int imgh = imageData->getHeight();
  81. // We don't want another thread modifying our ImageData mid-parse.
  82. love::thread::Lock lock(imageData->getMutex());
  83. // Set the only metric that matters
  84. metrics.height = imgh;
  85. // Reading texture data begins
  86. spacer = pixels[0];
  87. int start = 0;
  88. int end = 0;
  89. for (int i = 0; i < numglyphs; ++i)
  90. {
  91. start = end;
  92. // Finds out where the first character starts
  93. while (start < imgw && equal(pixels[start], spacer))
  94. ++start;
  95. // set previous glyph's spacing
  96. if (i > 0 && imageGlyphs.size() > 0)
  97. imageGlyphs[glyphs[i - 1]].spacing = (start > end) ? (start - end) : 0;
  98. end = start;
  99. // Find where glyph ends.
  100. while (end < imgw && !equal(pixels[end], spacer))
  101. ++end;
  102. if (start >= end)
  103. break;
  104. ImageGlyphData imageGlyph;
  105. imageGlyph.x = start;
  106. imageGlyph.width = end - start;
  107. imageGlyphs[glyphs[i]] = imageGlyph;
  108. }
  109. // Find spacing of last glyph
  110. if (numglyphs > 0)
  111. {
  112. start = end;
  113. while (start < imgw && equal(pixels[start], spacer))
  114. ++start;
  115. imageGlyphs[glyphs[numglyphs - 1]].spacing = (start > end) ? (start - end) : 0;
  116. }
  117. }
  118. int ImageRasterizer::getGlyphCount() const
  119. {
  120. return numglyphs;
  121. }
  122. bool ImageRasterizer::hasGlyph(uint32 glyph) const
  123. {
  124. return imageGlyphs.find(glyph) != imageGlyphs.end();
  125. }
  126. } // font
  127. } // love