FontTextureAtlas.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #include "../Common/TestsShell.h"
  2. #include <RmlUi/Core/Context.h>
  3. #include <RmlUi/Core/Core.h>
  4. #include <RmlUi/Core/Element.h>
  5. #include <RmlUi/Core/ElementDocument.h>
  6. #include <RmlUi/Core/Types.h>
  7. #include <doctest.h>
  8. #include <nanobench.h>
  9. using namespace ankerl;
  10. using namespace Rml;
  11. // Start of the "CJK unified ideographs" Unicode block.
  12. static constexpr int rml_font_texture_atlas_start_codepoint = 0x4E00;
  13. static const String rml_font_texture_atlas_document = R"(
  14. <rml>
  15. <head>
  16. <title>Font texture atlas benchmark</title>
  17. <link type="text/rcss" href="/../Tests/Data/style.rcss"/>
  18. <style>
  19. body {
  20. font-family: "Noto Sans JP";
  21. font-size: %dpx;
  22. }
  23. </style>
  24. </head>
  25. <body id="body">
  26. </body>
  27. </rml>
  28. )";
  29. TEST_CASE("font_texture_atlas")
  30. {
  31. Context* context = TestsShell::GetContext();
  32. REQUIRE(context);
  33. LoadFontFace("assets/NotoSansJP-Regular.ttf");
  34. nanobench::Bench bench;
  35. bool incremental = false;
  36. SUBCASE("all at once")
  37. {
  38. incremental = false;
  39. bench.title("Font texture atlas (all at once)");
  40. }
  41. SUBCASE("incremental")
  42. {
  43. incremental = true;
  44. bench.title("Font texture atlas (incremental)");
  45. }
  46. bench.relative(true);
  47. for (const int font_size : {12, 16, 24, 48, 96})
  48. {
  49. const String rml_document = CreateString(rml_font_texture_atlas_document.c_str(), font_size);
  50. ElementDocument* const document = context->LoadDocumentFromMemory(rml_document);
  51. REQUIRE(document);
  52. Element* const body = document->GetElementById("body");
  53. REQUIRE(body);
  54. document->Show();
  55. context->Update();
  56. context->Render();
  57. for (const int glyph_count : {10, 100, 1000})
  58. {
  59. const String benchmark_name = CreateString("Size %d with %d glyphs", font_size, glyph_count);
  60. if (incremental)
  61. {
  62. bench.run(benchmark_name, [&]() {
  63. ReleaseFontResources();
  64. std::string inner_rml;
  65. for (int i = 0; i < glyph_count; ++i)
  66. {
  67. inner_rml += StringUtilities::ToUTF8(static_cast<Character>(rml_font_texture_atlas_start_codepoint + i));
  68. body->SetInnerRML(inner_rml);
  69. }
  70. context->Update();
  71. context->Render();
  72. });
  73. }
  74. else
  75. {
  76. bench.run(benchmark_name, [&]() {
  77. ReleaseFontResources();
  78. for (int i = 0; i < glyph_count; ++i)
  79. {
  80. body->SetInnerRML(StringUtilities::ToUTF8(static_cast<Character>(rml_font_texture_atlas_start_codepoint + i)));
  81. context->Update();
  82. context->Render();
  83. }
  84. });
  85. }
  86. }
  87. document->Close();
  88. }
  89. TestsShell::ShutdownShell();
  90. }