TextureLayoutTexture.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "precompiled.h"
  28. #include "TextureLayoutTexture.h"
  29. #include <Rocket/Core/Core.h>
  30. #include "TextureDatabase.h"
  31. #include "TextureLayout.h"
  32. namespace Rocket {
  33. namespace Core {
  34. TextureLayoutTexture::TextureLayoutTexture() : dimensions(0, 0)
  35. {
  36. texture_data = NULL;
  37. }
  38. TextureLayoutTexture::~TextureLayoutTexture()
  39. {
  40. // Don't free texture data; freed in the texture loader.
  41. }
  42. // Returns the texture's dimensions. This is only valid after the texture has been generated.
  43. const Vector2i& TextureLayoutTexture::GetDimensions() const
  44. {
  45. return dimensions;
  46. }
  47. // Attempts to position unplaced rectangles from the layout into this texture.
  48. int TextureLayoutTexture::Generate(TextureLayout& layout, int maximum_dimensions)
  49. {
  50. // Come up with an estimate for how big a texture we need. Calculate the total square pixels
  51. // required by the remaining rectangles to place, square-root it to get the dimensions of the
  52. // smallest texture necessary (under optimal circumstances) and round it up to the nearest
  53. // power of two.
  54. int square_pixels = 0;
  55. int unplaced_rectangles = 0;
  56. for (int i = 0; i < layout.GetNumRectangles(); ++i)
  57. {
  58. const TextureLayoutRectangle& rectangle = layout.GetRectangle(i);
  59. if (!rectangle.IsPlaced())
  60. {
  61. square_pixels += (rectangle.GetDimensions().x + 1) * (rectangle.GetDimensions().y + 1);
  62. ++unplaced_rectangles;
  63. }
  64. }
  65. int texture_width = Math::RealToInteger(Math::SquareRoot((float) square_pixels));
  66. dimensions.y = Math::ToPowerOfTwo(texture_width);
  67. dimensions.x = dimensions.y >> 1;
  68. dimensions.x = Math::Min(dimensions.x, maximum_dimensions);
  69. dimensions.y = Math::Min(dimensions.y, maximum_dimensions);
  70. // Now we're layout out the rectangles in the texture. If we don't fit all the rectangles on
  71. // and have room to grow (ie, haven't hit the maximum texture size in both dimensions) then
  72. // we'll have another go with a bigger texture.
  73. int num_placed_rectangles = 0;
  74. for (;;)
  75. {
  76. bool success = true;
  77. int height = 1;
  78. while (num_placed_rectangles != unplaced_rectangles)
  79. {
  80. TextureLayoutRow row;
  81. int row_size = row.Generate(layout, dimensions.x, height);
  82. if (row_size == 0)
  83. {
  84. success = false;
  85. break;
  86. }
  87. height += row.GetHeight() + 1;
  88. if (height > dimensions.y)
  89. {
  90. // D'oh! We've exceeded our height boundaries. This row should be unplaced.
  91. row.Unplace();
  92. success = false;
  93. break;
  94. }
  95. rows.push_back(row);
  96. num_placed_rectangles += row_size;
  97. }
  98. // If the rectangles were successfully laid out within the texture limits, we're done.
  99. if (success)
  100. return num_placed_rectangles;
  101. // Couldn't do it! Increase the texture size, clear the rectangles and try again - unless
  102. // we've hit the maximum texture size, in which case return true if we've placed any
  103. // rectangles (ie, the layout isn't empty).
  104. if (dimensions.y > dimensions.x)
  105. dimensions.x = dimensions.y;
  106. else
  107. {
  108. if (dimensions.y << 1 > maximum_dimensions)
  109. return num_placed_rectangles;
  110. dimensions.y <<= 1;
  111. }
  112. // Unplace all of the glyphs we tried to place and have an other crack.
  113. for (size_t i = 0; i < rows.size(); i++)
  114. rows[i].Unplace();
  115. rows.clear();
  116. num_placed_rectangles = 0;
  117. }
  118. }
  119. // Allocates the texture.
  120. byte* TextureLayoutTexture::AllocateTexture()
  121. {
  122. // Note: this object does not free this texture data. It is freed in the font texture loader.
  123. if (dimensions.x > 0 &&
  124. dimensions.y > 0)
  125. {
  126. texture_data = new byte[dimensions.x * dimensions.y * 4];
  127. // Set the texture to transparent white.
  128. for (int i = 0; i < dimensions.x * dimensions.y; i++)
  129. ((unsigned int*)(texture_data))[i] = 0x00ffffff;
  130. for (size_t i = 0; i < rows.size(); ++i)
  131. rows[i].Allocate(texture_data, dimensions.x * 4);
  132. }
  133. return texture_data;
  134. }
  135. }
  136. }