StyleSheetParser.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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-2024 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 "../Common/TestsShell.h"
  29. #include <RmlUi/Core/Core.h>
  30. #include <RmlUi/Core/Spritesheet.h>
  31. #include <RmlUi/Core/StreamMemory.h>
  32. #include <RmlUi/Core/StyleSheet.h>
  33. #include <RmlUi/Core/StyleSheetContainer.h>
  34. #include <doctest.h>
  35. static const char spritesheet[] = R"(
  36. @spritesheet test_sheet {
  37. src: /assets/high_scores_alien_3.tga;
  38. test00: 0px 0px 64px 64px;
  39. test01: 64px 0px 64px 64px;
  40. test10: 0px 64px 64px 64px;
  41. test11: 64px 64px 64px 64px;
  42. resolution: 1x;
  43. }
  44. )";
  45. static const char spritesheet_with_path_string_encoding[] = R"(
  46. @spritesheet test_sheet_with_path_string_encoding {
  47. src: "/assets/test.tga";
  48. test00: 0px 0px 128px 128px;
  49. test01: 128px 0px 128px 128px;
  50. test10: 0px 128px 128px 128px;
  51. test11: 128px 128px 128px 128px;
  52. resolution: 2x;
  53. }
  54. )";
  55. using namespace Rml;
  56. TEST_CASE("style_sheet_parser.spritesheet")
  57. {
  58. Context* context = TestsShell::GetContext();
  59. {
  60. StyleSheetContainer style_sheet_container;
  61. StreamMemory spritesheet_stream{reinterpret_cast<const byte*>(spritesheet), sizeof(spritesheet) - 1};
  62. style_sheet_container.LoadStyleSheetContainer(&spritesheet_stream, 0);
  63. style_sheet_container.UpdateCompiledStyleSheet(context);
  64. const auto* style_sheet = style_sheet_container.GetCompiledStyleSheet();
  65. CHECK(style_sheet != nullptr);
  66. const auto* sprite00 = style_sheet->GetSprite("test00");
  67. CHECK(sprite00 != nullptr);
  68. CHECK(sprite00->sprite_sheet->name == "test_sheet");
  69. CHECK(sprite00->sprite_sheet->texture_source.GetSource() == "/assets/high_scores_alien_3.tga");
  70. CHECK(sprite00->rectangle.TopLeft() == Vector2f(0.f, 0.f));
  71. CHECK(sprite00->rectangle.BottomRight() == Vector2f(64.f, 64.f));
  72. CHECK(sprite00->sprite_sheet->display_scale == 1.f);
  73. const auto* sprite01 = style_sheet->GetSprite("test01");
  74. CHECK(sprite01 != nullptr);
  75. CHECK(sprite01->sprite_sheet->name == "test_sheet");
  76. CHECK(sprite01->sprite_sheet->texture_source.GetSource() == "/assets/high_scores_alien_3.tga");
  77. CHECK(sprite01->rectangle.TopLeft() == Vector2f(64.f, 0.f));
  78. CHECK(sprite01->rectangle.BottomRight() == Vector2f(128.f, 64.f));
  79. CHECK(sprite01->sprite_sheet->display_scale == 1.f);
  80. const auto* sprite10 = style_sheet->GetSprite("test10");
  81. CHECK(sprite10 != nullptr);
  82. CHECK(sprite10->sprite_sheet->name == "test_sheet");
  83. CHECK(sprite10->sprite_sheet->texture_source.GetSource() == "/assets/high_scores_alien_3.tga");
  84. CHECK(sprite10->rectangle.TopLeft() == Vector2f(0.f, 64.f));
  85. CHECK(sprite10->rectangle.BottomRight() == Vector2f(64.f, 128.f));
  86. CHECK(sprite10->sprite_sheet->display_scale == 1.f);
  87. const auto* sprite11 = style_sheet->GetSprite("test11");
  88. CHECK(sprite11 != nullptr);
  89. CHECK(sprite11->sprite_sheet->name == "test_sheet");
  90. CHECK(sprite11->sprite_sheet->texture_source.GetSource() == "/assets/high_scores_alien_3.tga");
  91. CHECK(sprite11->rectangle.TopLeft() == Vector2f(64.f, 64.f));
  92. CHECK(sprite11->rectangle.BottomRight() == Vector2f(128.f, 128.f));
  93. CHECK(sprite11->sprite_sheet->display_scale == 1.f);
  94. }
  95. TestsShell::ShutdownShell();
  96. }
  97. TEST_CASE("style_sheet_parser.spritesheet_with_path_string_encoding")
  98. {
  99. Context* context = TestsShell::GetContext();
  100. {
  101. StyleSheetContainer style_sheet_container;
  102. StreamMemory spritesheet_stream{reinterpret_cast<const byte*>(spritesheet_with_path_string_encoding),
  103. sizeof(spritesheet_with_path_string_encoding) - 1};
  104. style_sheet_container.LoadStyleSheetContainer(&spritesheet_stream, 0);
  105. style_sheet_container.UpdateCompiledStyleSheet(context);
  106. const auto* style_sheet = style_sheet_container.GetCompiledStyleSheet();
  107. CHECK(style_sheet != nullptr);
  108. const auto* sprite00 = style_sheet->GetSprite("test00");
  109. CHECK(sprite00 != nullptr);
  110. CHECK(sprite00->sprite_sheet->name == "test_sheet_with_path_string_encoding");
  111. CHECK(sprite00->sprite_sheet->texture_source.GetSource() == "/assets/test.tga");
  112. CHECK(sprite00->rectangle.TopLeft() == Vector2f(0.f, 0.f));
  113. CHECK(sprite00->rectangle.BottomRight() == Vector2f(128.f, 128.f));
  114. CHECK(sprite00->sprite_sheet->display_scale == 0.5f);
  115. const auto* sprite01 = style_sheet->GetSprite("test01");
  116. CHECK(sprite01 != nullptr);
  117. CHECK(sprite01->sprite_sheet->name == "test_sheet_with_path_string_encoding");
  118. CHECK(sprite01->sprite_sheet->texture_source.GetSource() == "/assets/test.tga");
  119. CHECK(sprite01->rectangle.TopLeft() == Vector2f(128.f, 0.f));
  120. CHECK(sprite01->rectangle.BottomRight() == Vector2f(256.f, 128.f));
  121. CHECK(sprite01->sprite_sheet->display_scale == 0.5f);
  122. const auto* sprite10 = style_sheet->GetSprite("test10");
  123. CHECK(sprite10 != nullptr);
  124. CHECK(sprite10->sprite_sheet->name == "test_sheet_with_path_string_encoding");
  125. CHECK(sprite10->sprite_sheet->texture_source.GetSource() == "/assets/test.tga");
  126. CHECK(sprite10->rectangle.TopLeft() == Vector2f(0.f, 128.f));
  127. CHECK(sprite10->rectangle.BottomRight() == Vector2f(128.f, 256.f));
  128. CHECK(sprite10->sprite_sheet->display_scale == 0.5f);
  129. const auto* sprite11 = style_sheet->GetSprite("test11");
  130. CHECK(sprite11 != nullptr);
  131. CHECK(sprite11->sprite_sheet->name == "test_sheet_with_path_string_encoding");
  132. CHECK(sprite11->sprite_sheet->texture_source.GetSource() == "/assets/test.tga");
  133. CHECK(sprite11->rectangle.TopLeft() == Vector2f(128.f, 128.f));
  134. CHECK(sprite11->rectangle.BottomRight() == Vector2f(256.f, 256.f));
  135. CHECK(sprite11->sprite_sheet->display_scale == 0.5f);
  136. }
  137. TestsShell::ShutdownShell();
  138. }