TextureLayoutTexture.cpp 4.6 KB

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