TextureLayoutTexture.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 "../../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. int x = rectangle.GetDimensions().x + 1;
  62. int y = rectangle.GetDimensions().y + 1;
  63. square_pixels += x*y;
  64. ++unplaced_rectangles;
  65. }
  66. }
  67. int texture_width = Math::RealToInteger(Math::SquareRoot((float) square_pixels));
  68. dimensions.y = Math::ToPowerOfTwo(texture_width);
  69. dimensions.x = dimensions.y >> 1;
  70. dimensions.x = Math::Min(dimensions.x, maximum_dimensions);
  71. dimensions.y = Math::Min(dimensions.y, maximum_dimensions);
  72. // Now we're layout out the rectangles in the texture. If we don't fit all the rectangles on
  73. // and have room to grow (ie, haven't hit the maximum texture size in both dimensions) then
  74. // we'll have another go with a bigger texture.
  75. int num_placed_rectangles = 0;
  76. for (;;)
  77. {
  78. bool success = true;
  79. int height = 1;
  80. while (num_placed_rectangles != unplaced_rectangles)
  81. {
  82. TextureLayoutRow row;
  83. int row_size = row.Generate(layout, dimensions.x, height);
  84. if (row_size == 0)
  85. {
  86. success = false;
  87. break;
  88. }
  89. height += row.GetHeight() + 1;
  90. if (height > dimensions.y)
  91. {
  92. // D'oh! We've exceeded our height boundaries. This row should be unplaced.
  93. row.Unplace();
  94. success = false;
  95. break;
  96. }
  97. rows.push_back(row);
  98. num_placed_rectangles += row_size;
  99. }
  100. // If the rectangles were successfully laid out within the texture limits, we're done.
  101. if (success)
  102. return num_placed_rectangles;
  103. // Couldn't do it! Increase the texture size, clear the rectangles and try again - unless
  104. // we've hit the maximum texture size, in which case return true if we've placed any
  105. // rectangles (ie, the layout isn't empty).
  106. if (dimensions.y > dimensions.x)
  107. dimensions.x = dimensions.y;
  108. else
  109. {
  110. if (dimensions.y << 1 > maximum_dimensions)
  111. return num_placed_rectangles;
  112. dimensions.y <<= 1;
  113. }
  114. // Unplace all of the glyphs we tried to place and have an other crack.
  115. for (size_t i = 0; i < rows.size(); i++)
  116. rows[i].Unplace();
  117. rows.clear();
  118. num_placed_rectangles = 0;
  119. }
  120. }
  121. // Allocates the texture.
  122. byte* TextureLayoutTexture::AllocateTexture()
  123. {
  124. // Note: this object does not free this texture data. It is freed in the font texture loader.
  125. if (dimensions.x > 0 &&
  126. dimensions.y > 0)
  127. {
  128. texture_data = new byte[dimensions.x * dimensions.y * 4];
  129. // Set the texture to transparent white.
  130. for (int i = 0; i < dimensions.x * dimensions.y; i++)
  131. ((unsigned int*)(texture_data))[i] = 0x00ffffff;
  132. for (size_t i = 0; i < rows.size(); ++i)
  133. rows[i].Allocate(texture_data, dimensions.x * 4);
  134. }
  135. return texture_data;
  136. }
  137. }
  138. }