TextureLayoutTexture.cpp 5.0 KB

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