TextureLayoutTexture.cpp 5.0 KB

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