DecoratorText.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #include "DecoratorText.h"
  2. #include "../../Include/RmlUi/Core/ComputedValues.h"
  3. #include "../../Include/RmlUi/Core/Context.h"
  4. #include "../../Include/RmlUi/Core/Element.h"
  5. #include "../../Include/RmlUi/Core/FontEngineInterface.h"
  6. #include "../../Include/RmlUi/Core/Geometry.h"
  7. #include "../../Include/RmlUi/Core/PropertyDefinition.h"
  8. #include "../../Include/RmlUi/Core/RenderManager.h"
  9. #include "../../Include/RmlUi/Core/TextShapingContext.h"
  10. namespace Rml {
  11. DecoratorText::DecoratorText() {}
  12. DecoratorText::~DecoratorText() {}
  13. void DecoratorText::Initialise(String in_text, bool in_inherit_color, Colourb in_color, Vector2Numeric in_align)
  14. {
  15. text = std::move(in_text);
  16. inherit_color = in_inherit_color;
  17. color = in_color;
  18. align = in_align;
  19. }
  20. DecoratorDataHandle DecoratorText::GenerateElementData(Element* element, BoxArea paint_area) const
  21. {
  22. ElementData* data = new ElementData{paint_area, {}, -1};
  23. if (!GenerateGeometry(element, *data))
  24. {
  25. Log::Message(Log::LT_WARNING, "Could not construct text decorator with text %s on element %s", text.c_str(), element->GetAddress().c_str());
  26. return {};
  27. }
  28. return reinterpret_cast<DecoratorDataHandle>(data);
  29. }
  30. void DecoratorText::ReleaseElementData(DecoratorDataHandle element_data) const
  31. {
  32. delete reinterpret_cast<ElementData*>(element_data);
  33. }
  34. void DecoratorText::RenderElement(Element* element, DecoratorDataHandle element_data) const
  35. {
  36. ElementData* data = reinterpret_cast<ElementData*>(element_data);
  37. if (!GenerateGeometry(element, *data))
  38. return;
  39. const Vector2f translation = element->GetAbsoluteOffset(BoxArea::Border);
  40. for (size_t i = 0; i < data->textured_geometry.size(); ++i)
  41. data->textured_geometry[i].geometry.Render(translation, data->textured_geometry[i].texture);
  42. }
  43. bool DecoratorText::GenerateGeometry(Element* element, ElementData& element_data) const
  44. {
  45. FontFaceHandle font_face_handle = element->GetFontFaceHandle();
  46. if (font_face_handle == 0)
  47. return false;
  48. FontEngineInterface* font_engine_interface = GetFontEngineInterface();
  49. const int new_version = font_engine_interface->GetVersion(font_face_handle);
  50. if (new_version == element_data.font_handle_version)
  51. return true;
  52. const auto& computed = element->GetComputedValues();
  53. const TextShapingContext text_shaping_context{computed.language(), computed.direction(), computed.font_kerning(), computed.letter_spacing()};
  54. const int string_width = font_engine_interface->GetStringWidth(font_face_handle, text, text_shaping_context);
  55. const FontMetrics& metrics = font_engine_interface->GetFontMetrics(font_face_handle);
  56. const RenderBox render_box = element->GetRenderBox(element_data.paint_area);
  57. const Vector2f text_size = {float(string_width), metrics.ascent + metrics.descent};
  58. const Vector2f offset_to_align_area = render_box.GetFillOffset() + Vector2f{0, metrics.ascent};
  59. const Vector2f size_of_align_area = render_box.GetFillSize() - text_size;
  60. const Vector2f offset_within_align_area =
  61. Vector2f{element->ResolveNumericValue(align.x, size_of_align_area.x), element->ResolveNumericValue(align.y, size_of_align_area.y)};
  62. const Vector2f offset = offset_to_align_area + offset_within_align_area;
  63. const float opacity = computed.opacity();
  64. const ColourbPremultiplied text_color = (inherit_color ? computed.color() : color).ToPremultiplied(opacity);
  65. RenderManager& render_manager = element->GetContext()->GetRenderManager();
  66. TexturedMeshList mesh_list;
  67. font_engine_interface->GenerateString(render_manager, font_face_handle, {}, text, offset, text_color, opacity, text_shaping_context, mesh_list);
  68. if (mesh_list.empty())
  69. return false;
  70. Vector<TexturedGeometry> textured_geometry(mesh_list.size());
  71. for (size_t i = 0; i < textured_geometry.size(); i++)
  72. {
  73. textured_geometry[i].geometry = render_manager.MakeGeometry(std::move(mesh_list[i].mesh));
  74. textured_geometry[i].texture = mesh_list[i].texture;
  75. }
  76. element_data = ElementData{
  77. element_data.paint_area,
  78. std::move(textured_geometry),
  79. font_engine_interface->GetVersion(font_face_handle),
  80. };
  81. return true;
  82. }
  83. DecoratorTextInstancer::DecoratorTextInstancer()
  84. {
  85. ids = {};
  86. ids.text = RegisterProperty("text", "").AddParser("string").GetId();
  87. ids.color = RegisterProperty("color", "inherit-color").AddParser("keyword", "inherit-color").AddParser("color").GetId();
  88. ids.align_x = RegisterProperty("align-x", "center").AddParser("keyword", "left, center, right").AddParser("length_percent").GetId();
  89. ids.align_y = RegisterProperty("align-y", "center").AddParser("keyword", "top, center, bottom").AddParser("length_percent").GetId();
  90. RegisterShorthand("decorator", "text, color, align-x, align-y, align-x", ShorthandType::FallThrough);
  91. }
  92. DecoratorTextInstancer::~DecoratorTextInstancer() {}
  93. SharedPtr<Decorator> DecoratorTextInstancer::InstanceDecorator(const String& /*name*/, const PropertyDictionary& properties,
  94. const DecoratorInstancerInterface& /*instancer_interface*/)
  95. {
  96. const Property* p_text = properties.GetProperty(ids.text);
  97. const Property* p_color = properties.GetProperty(ids.color);
  98. Array<const Property*, 2> p_align = {properties.GetProperty(ids.align_x), properties.GetProperty(ids.align_y)};
  99. if (!p_text || !p_color || !p_align[0] || !p_align[1])
  100. return nullptr;
  101. String text = StringUtilities::DecodeRml(p_text->Get<String>());
  102. if (text.empty())
  103. return nullptr;
  104. const bool inherit_color = (p_color->unit == Unit::KEYWORD);
  105. const Colourb color = (p_color->unit == Unit::COLOUR ? p_color->Get<Colourb>() : Colourb{});
  106. const Vector2Numeric align = ComputePosition(p_align);
  107. auto decorator = MakeShared<DecoratorText>();
  108. decorator->Initialise(std::move(text), inherit_color, color, align);
  109. return decorator;
  110. }
  111. } // namespace Rml