FontTextureAtlas.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. bench.title("Font texture atlas");
  36. bench.relative(true);
  37. for (const int font_size : {12, 16, 24, 48, 96})
  38. {
  39. const String rml_document = CreateString(rml_font_texture_atlas_document.c_str(), font_size);
  40. ElementDocument *const document = context->LoadDocumentFromMemory(rml_document);
  41. REQUIRE(document);
  42. Element *const body = document->GetElementById("body");
  43. REQUIRE(body);
  44. document->Show();
  45. context->Update();
  46. context->Render();
  47. for (const int glyph_count : {10, 100, 1000})
  48. {
  49. String benchmark_name;
  50. FormatString(benchmark_name, "Size %d with %d glyphs", font_size, glyph_count);
  51. bench.run(benchmark_name.c_str(), [&]() {
  52. ReleaseFontResources();
  53. for (int i = 0; i < glyph_count; ++i)
  54. {
  55. body->SetInnerRML(StringUtilities::ToUTF8(static_cast<Character>(
  56. rml_font_texture_atlas_start_codepoint + i
  57. )));
  58. context->Update();
  59. context->Render();
  60. }
  61. });
  62. }
  63. document->Close();
  64. }
  65. TestsShell::ShutdownShell();
  66. }