LayoutText.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #include <AK/StringBuilder.h>
  2. #include <AK/Utf8View.h>
  3. #include <LibCore/CDirIterator.h>
  4. #include <LibDraw/Font.h>
  5. #include <LibGUI/GPainter.h>
  6. #include <LibHTML/Layout/LayoutBlock.h>
  7. #include <LibHTML/Layout/LayoutText.h>
  8. #include <ctype.h>
  9. LayoutText::LayoutText(const Text& text)
  10. : LayoutNode(&text)
  11. {
  12. set_inline(true);
  13. }
  14. LayoutText::~LayoutText()
  15. {
  16. }
  17. static bool is_all_whitespace(const String& string)
  18. {
  19. for (int i = 0; i < string.length(); ++i) {
  20. if (!isspace(string[i]))
  21. return false;
  22. }
  23. return true;
  24. }
  25. const String& LayoutText::text_for_style(const StyleProperties& style) const
  26. {
  27. static String one_space = " ";
  28. if (is_all_whitespace(node().data())) {
  29. if (style.string_or_fallback(CSS::PropertyID::WhiteSpace, "normal") == "normal")
  30. return one_space;
  31. }
  32. return node().data();
  33. }
  34. void LayoutText::render_fragment(RenderingContext& context, const LineBoxFragment& fragment) const
  35. {
  36. auto& painter = context.painter();
  37. painter.set_font(style().font());
  38. auto color = style().color_or_fallback(CSS::PropertyID::Color, document(), Color::Black);
  39. auto text_decoration = style().string_or_fallback(CSS::PropertyID::TextDecoration, "none");
  40. bool is_underline = text_decoration == "underline";
  41. if (is_underline)
  42. painter.draw_line(fragment.rect().bottom_left().translated(0, -1), fragment.rect().bottom_right().translated(0, -1), color);
  43. painter.draw_text(fragment.rect(), m_text_for_rendering.substring_view(fragment.start(), fragment.length()), TextAlignment::TopLeft, color);
  44. }
  45. template<typename Callback>
  46. void LayoutText::for_each_word(Callback callback) const
  47. {
  48. Utf8View view(m_text_for_rendering);
  49. if (view.is_empty())
  50. return;
  51. auto start_of_word = view.begin();
  52. auto commit_word = [&](auto it) {
  53. int start = view.byte_offset_of(start_of_word);
  54. int length = view.byte_offset_of(it) - view.byte_offset_of(start_of_word);
  55. if (length > 0) {
  56. callback(view.substring_view(start, length), start, length);
  57. }
  58. start_of_word = it;
  59. };
  60. bool last_was_space = isspace(*view.begin());
  61. for (auto it = view.begin(); it != view.end();) {
  62. bool is_space = isspace(*it);
  63. if (is_space == last_was_space) {
  64. ++it;
  65. continue;
  66. }
  67. last_was_space = is_space;
  68. commit_word(it);
  69. ++it;
  70. }
  71. if (start_of_word != view.end())
  72. commit_word(view.end());
  73. }
  74. template<typename Callback>
  75. void LayoutText::for_each_source_line(Callback callback) const
  76. {
  77. Utf8View view(m_text_for_rendering);
  78. if (view.is_empty())
  79. return;
  80. auto start_of_line = view.begin();
  81. auto commit_line = [&](auto it) {
  82. int start = view.byte_offset_of(start_of_line);
  83. int length = view.byte_offset_of(it) - view.byte_offset_of(start_of_line);
  84. if (length > 0) {
  85. callback(view.substring_view(start, length), start, length);
  86. }
  87. };
  88. for (auto it = view.begin(); it != view.end();) {
  89. bool did_commit = false;
  90. if (*it == '\n') {
  91. commit_line(it);
  92. did_commit = true;
  93. }
  94. ++it;
  95. if (did_commit)
  96. start_of_line = it;
  97. }
  98. if (start_of_line != view.end())
  99. commit_line(view.end());
  100. }
  101. void LayoutText::split_into_lines(LayoutBlock& container)
  102. {
  103. auto& font = style().font();
  104. int space_width = font.glyph_width(' ') + font.glyph_spacing();
  105. int line_height = style().line_height();
  106. auto& line_boxes = container.line_boxes();
  107. if (line_boxes.is_empty())
  108. line_boxes.append(LineBox());
  109. int available_width = container.width() - line_boxes.last().width();
  110. bool is_preformatted = style().string_or_fallback(CSS::PropertyID::WhiteSpace, "normal") == "pre";
  111. if (is_preformatted) {
  112. m_text_for_rendering = node().data();
  113. for_each_source_line([&](const Utf8View& view, int start, int length) {
  114. line_boxes.last().add_fragment(*this, start, length, font.width(view), line_height);
  115. line_boxes.append(LineBox());
  116. });
  117. return;
  118. }
  119. // Collapse whitespace into single spaces
  120. auto utf8_view = Utf8View(node().data());
  121. StringBuilder builder(node().data().length());
  122. for (auto it = utf8_view.begin(); it != utf8_view.end(); ++it) {
  123. if (!isspace(*it)) {
  124. builder.append(utf8_view.as_string().characters_without_null_termination() + utf8_view.byte_offset_of(it), it.codepoint_length_in_bytes());
  125. } else {
  126. builder.append(' ');
  127. auto prev = it;
  128. while (it != utf8_view.end() && isspace(*it)) {
  129. prev = it;
  130. ++it;
  131. }
  132. it = prev;
  133. }
  134. }
  135. m_text_for_rendering = builder.to_string();
  136. struct Word {
  137. Utf8View view;
  138. int start;
  139. int length;
  140. };
  141. Vector<Word> words;
  142. for_each_word([&](const Utf8View& view, int start, int length) {
  143. words.append({ Utf8View(view), start, length });
  144. });
  145. for (int i = 0; i < words.size(); ++i) {
  146. auto& word = words[i];
  147. int word_width;
  148. bool is_whitespace = isspace(*word.view.begin());
  149. if (is_whitespace)
  150. word_width = space_width;
  151. else
  152. word_width = font.width(word.view) + font.glyph_spacing();
  153. if (word_width > available_width) {
  154. line_boxes.append(LineBox());
  155. available_width = container.width();
  156. }
  157. if (is_whitespace && line_boxes.last().fragments().is_empty())
  158. continue;
  159. line_boxes.last().add_fragment(*this, word.start, is_whitespace ? 1 : word.length, word_width, line_height);
  160. available_width -= word_width;
  161. if (available_width < 0) {
  162. line_boxes.append(LineBox());
  163. available_width = container.width();
  164. }
  165. }
  166. }