TextureLayoutTexture.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 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 "precompiled.h"
  29. #include "TextureLayoutTexture.h"
  30. #include "../../Include/RmlUi/Core/Core.h"
  31. #include "TextureDatabase.h"
  32. #include "TextureLayout.h"
  33. namespace Rml {
  34. namespace Core {
  35. TextureLayoutTexture::TextureLayoutTexture() : dimensions(0, 0)
  36. {}
  37. TextureLayoutTexture::~TextureLayoutTexture()
  38. {
  39. // Don't free texture data; freed in the texture loader.
  40. }
  41. // Returns the texture's dimensions. This is only valid after the texture has been generated.
  42. const Vector2i& TextureLayoutTexture::GetDimensions() const
  43. {
  44. return dimensions;
  45. }
  46. // Attempts to position unplaced rectangles from the layout into this texture.
  47. int TextureLayoutTexture::Generate(TextureLayout& layout, int maximum_dimensions)
  48. {
  49. // Come up with an estimate for how big a texture we need. Calculate the total square pixels
  50. // required by the remaining rectangles to place, square-root it to get the dimensions of the
  51. // smallest texture necessary (under optimal circumstances) and round it up to the nearest
  52. // power of two.
  53. int square_pixels = 0;
  54. int unplaced_rectangles = 0;
  55. for (int i = 0; i < layout.GetNumRectangles(); ++i)
  56. {
  57. const TextureLayoutRectangle& rectangle = layout.GetRectangle(i);
  58. if (!rectangle.IsPlaced())
  59. {
  60. int x = rectangle.GetDimensions().x + 1;
  61. int y = rectangle.GetDimensions().y + 1;
  62. square_pixels += x*y;
  63. ++unplaced_rectangles;
  64. }
  65. }
  66. int texture_width = Math::RealToInteger(Math::SquareRoot((float) square_pixels));
  67. dimensions.y = Math::ToPowerOfTwo(texture_width);
  68. dimensions.x = dimensions.y >> 1;
  69. dimensions.x = Math::Min(dimensions.x, maximum_dimensions);
  70. dimensions.y = Math::Min(dimensions.y, maximum_dimensions);
  71. // Now we're layout out the rectangles in the texture. If we don't fit all the rectangles on
  72. // and have room to grow (ie, haven't hit the maximum texture size in both dimensions) then
  73. // we'll have another go with a bigger texture.
  74. int num_placed_rectangles = 0;
  75. for (;;)
  76. {
  77. bool success = true;
  78. int height = 1;
  79. while (num_placed_rectangles != unplaced_rectangles)
  80. {
  81. TextureLayoutRow row;
  82. int row_size = row.Generate(layout, dimensions.x, height);
  83. if (row_size == 0)
  84. {
  85. success = false;
  86. break;
  87. }
  88. height += row.GetHeight() + 1;
  89. if (height > dimensions.y)
  90. {
  91. // D'oh! We've exceeded our height boundaries. This row should be unplaced.
  92. row.Unplace();
  93. success = false;
  94. break;
  95. }
  96. rows.push_back(row);
  97. num_placed_rectangles += row_size;
  98. }
  99. // If the rectangles were successfully laid out within the texture limits, we're done.
  100. if (success)
  101. return num_placed_rectangles;
  102. // Couldn't do it! Increase the texture size, clear the rectangles and try again - unless
  103. // we've hit the maximum texture size, in which case return true if we've placed any
  104. // rectangles (ie, the layout isn't empty).
  105. if (dimensions.y > dimensions.x)
  106. dimensions.x = dimensions.y;
  107. else
  108. {
  109. if (dimensions.y << 1 > maximum_dimensions)
  110. return num_placed_rectangles;
  111. dimensions.y <<= 1;
  112. }
  113. // Unplace all of the glyphs we tried to place and have an other crack.
  114. for (size_t i = 0; i < rows.size(); i++)
  115. rows[i].Unplace();
  116. rows.clear();
  117. num_placed_rectangles = 0;
  118. }
  119. }
  120. // Allocates the texture.
  121. UniquePtr<byte[]> TextureLayoutTexture::AllocateTexture()
  122. {
  123. // Note: this object does not free this texture data. It is freed in the font texture loader.
  124. UniquePtr<byte[]> texture_data;
  125. if (dimensions.x > 0 &&
  126. dimensions.y > 0)
  127. {
  128. texture_data.reset(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.get()))[i] = 0x00ffffff;
  132. for (size_t i = 0; i < rows.size(); ++i)
  133. rows[i].Allocate(texture_data.get(), dimensions.x * 4);
  134. }
  135. return texture_data;
  136. }
  137. }
  138. }