DecoratorText.cpp 6.9 KB

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