TextureLayoutTexture.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #include "TextureLayoutTexture.h"
  2. #include "TextureDatabase.h"
  3. #include "TextureLayout.h"
  4. namespace Rml {
  5. TextureLayoutTexture::TextureLayoutTexture() : dimensions(0, 0) {}
  6. TextureLayoutTexture::~TextureLayoutTexture()
  7. {
  8. // Don't free texture data; freed in the texture loader.
  9. }
  10. Vector2i TextureLayoutTexture::GetDimensions() const
  11. {
  12. return dimensions;
  13. }
  14. int TextureLayoutTexture::Generate(TextureLayout& layout, int maximum_dimensions)
  15. {
  16. // Come up with an estimate for how big a texture we need. Calculate the total square pixels
  17. // required by the remaining rectangles to place, square-root it to get the dimensions of the
  18. // smallest texture necessary (under optimal circumstances) and round it up to the nearest
  19. // power of two.
  20. int square_pixels = 0;
  21. int unplaced_rectangles = 0;
  22. for (int i = 0; i < layout.GetNumRectangles(); ++i)
  23. {
  24. const TextureLayoutRectangle& rectangle = layout.GetRectangle(i);
  25. if (!rectangle.IsPlaced())
  26. {
  27. int x = rectangle.GetDimensions().x + 1;
  28. int y = rectangle.GetDimensions().y + 1;
  29. square_pixels += x * y;
  30. ++unplaced_rectangles;
  31. }
  32. }
  33. int texture_width = int(Math::SquareRoot((float)square_pixels));
  34. dimensions.y = Math::ToPowerOfTwo(texture_width);
  35. dimensions.x = dimensions.y >> 1;
  36. dimensions.x = Math::Min(dimensions.x, maximum_dimensions);
  37. dimensions.y = Math::Min(dimensions.y, maximum_dimensions);
  38. // Now we're layout out the rectangles in the texture. If we don't fit all the rectangles on
  39. // and have room to grow (ie, haven't hit the maximum texture size in both dimensions) then
  40. // we'll have another go with a bigger texture.
  41. int num_placed_rectangles = 0;
  42. for (;;)
  43. {
  44. bool success = true;
  45. int height = 1;
  46. while (num_placed_rectangles != unplaced_rectangles)
  47. {
  48. TextureLayoutRow row;
  49. int row_size = row.Generate(layout, dimensions.x, height);
  50. if (row_size == 0)
  51. {
  52. success = false;
  53. break;
  54. }
  55. height += row.GetHeight() + 1;
  56. if (height > dimensions.y)
  57. {
  58. // D'oh! We've exceeded our height boundaries. This row should be unplaced.
  59. row.Unplace();
  60. success = false;
  61. break;
  62. }
  63. rows.push_back(row);
  64. num_placed_rectangles += row_size;
  65. }
  66. // If the rectangles were successfully laid out within the texture limits, we're done.
  67. if (success)
  68. return num_placed_rectangles;
  69. // Couldn't do it! Increase the texture size, clear the rectangles and try again - unless
  70. // we've hit the maximum texture size, in which case return true if we've placed any
  71. // rectangles (ie, the layout isn't empty).
  72. if (dimensions.y > dimensions.x)
  73. dimensions.x = dimensions.y;
  74. else
  75. {
  76. if (dimensions.y << 1 > maximum_dimensions)
  77. return num_placed_rectangles;
  78. dimensions.y <<= 1;
  79. }
  80. // Unplace all of the glyphs we tried to place and have an other crack.
  81. for (size_t i = 0; i < rows.size(); i++)
  82. rows[i].Unplace();
  83. rows.clear();
  84. num_placed_rectangles = 0;
  85. }
  86. }
  87. Vector<byte> TextureLayoutTexture::AllocateTexture()
  88. {
  89. Vector<byte> texture_data;
  90. if (dimensions.x > 0 && dimensions.y > 0)
  91. {
  92. // Set the texture to transparent black.
  93. texture_data.resize(dimensions.x * dimensions.y * 4, 0);
  94. for (size_t i = 0; i < rows.size(); ++i)
  95. rows[i].Allocate(texture_data.data(), dimensions.x * 4);
  96. }
  97. return texture_data;
  98. }
  99. } // namespace Rml