FormattingContext.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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-2023 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 "FormattingContext.h"
  29. #include "../../../Include/RmlUi/Core/ComputedValues.h"
  30. #include "../../../Include/RmlUi/Core/Element.h"
  31. #include "../../../Include/RmlUi/Core/Profiling.h"
  32. #include "BlockFormattingContext.h"
  33. #include "ContainerBox.h"
  34. #include "FlexFormattingContext.h"
  35. #include "FormattingContextDebug.h"
  36. #include "LayoutBox.h"
  37. #include "LayoutDetails.h"
  38. #include "ReplacedFormattingContext.h"
  39. #include "TableFormattingContext.h"
  40. namespace Rml {
  41. FormattingContextType FormattingContext::GetFormattingContextType(Element* element)
  42. {
  43. if (element->IsReplaced())
  44. return FormattingContextType::Replaced;
  45. using namespace Style;
  46. auto& computed = element->GetComputedValues();
  47. const Display display = computed.display();
  48. if (display == Display::Flex || display == Display::InlineFlex)
  49. return FormattingContextType::Flex;
  50. if (display == Display::Table || display == Display::InlineTable)
  51. return FormattingContextType::Table;
  52. if (display == Display::InlineBlock || display == Display::FlowRoot || display == Display::TableCell || computed.float_() != Float::None ||
  53. computed.position() == Position::Absolute || computed.position() == Position::Fixed || computed.overflow_x() != Overflow::Visible ||
  54. computed.overflow_y() != Overflow::Visible || !element->GetParentNode() || element->GetParentNode()->GetDisplay() == Display::Flex)
  55. {
  56. return FormattingContextType::Block;
  57. }
  58. return FormattingContextType::None;
  59. }
  60. UniquePtr<LayoutBox> FormattingContext::FormatIndependent(ContainerBox* parent_container, Element* element, const Box* override_initial_box,
  61. FormattingContextType default_context)
  62. {
  63. RMLUI_ASSERT(parent_container && element);
  64. RMLUI_ZoneScopedC(0xAFAFAF);
  65. FormattingContextType type = GetFormattingContextType(element);
  66. if (type == FormattingContextType::None)
  67. type = default_context;
  68. #ifdef RMLUI_DEBUG
  69. auto* debug_tracker = FormatIndependentDebugTracker::GetIf();
  70. FormatIndependentDebugTracker::Entry* tracker_entry = nullptr;
  71. if (debug_tracker && type != FormattingContextType::None)
  72. {
  73. tracker_entry = &debug_tracker->entries.emplace_back(FormatIndependentDebugTracker::Entry{
  74. debug_tracker->current_stack_level,
  75. parent_container->GetElement() ? parent_container->GetElement()->GetAddress() : "",
  76. parent_container->GetAbsolutePositioningContainingBlockElementForDebug()
  77. ? parent_container->GetAbsolutePositioningContainingBlockElementForDebug()->GetAddress()
  78. : "",
  79. parent_container->GetContainingBlockSize(Style::Position::Static),
  80. parent_container->GetContainingBlockSize(Style::Position::Absolute),
  81. element->GetAddress(),
  82. override_initial_box ? Optional<Box>{*override_initial_box} : std::nullopt,
  83. type,
  84. });
  85. debug_tracker->current_stack_level += 1;
  86. }
  87. #endif
  88. UniquePtr<LayoutBox> layout_box;
  89. switch (type)
  90. {
  91. case FormattingContextType::Block: layout_box = BlockFormattingContext::Format(parent_container, element, override_initial_box); break;
  92. case FormattingContextType::Table: layout_box = TableFormattingContext::Format(parent_container, element, override_initial_box); break;
  93. case FormattingContextType::Flex: layout_box = FlexFormattingContext::Format(parent_container, element, override_initial_box); break;
  94. case FormattingContextType::Replaced: layout_box = ReplacedFormattingContext::Format(parent_container, element, override_initial_box); break;
  95. case FormattingContextType::None: break;
  96. }
  97. #ifdef RMLUI_DEBUG
  98. if (tracker_entry)
  99. {
  100. debug_tracker->current_stack_level -= 1;
  101. if (layout_box)
  102. {
  103. tracker_entry->layout = Optional<FormatIndependentDebugTracker::Entry::LayoutResults>({
  104. layout_box->GetVisibleOverflowSize(),
  105. layout_box->GetIfBox() ? Optional<Box>{*layout_box->GetIfBox()} : std::nullopt,
  106. });
  107. }
  108. }
  109. #endif
  110. return layout_box;
  111. }
  112. } // namespace Rml