LayoutText.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. // FIXME: Allow overriding the line-height. We currently default to 140% which seems to look nice.
  106. int line_height = (int)(font.glyph_height() * 1.4f);
  107. auto& line_boxes = container.line_boxes();
  108. if (line_boxes.is_empty())
  109. line_boxes.append(LineBox());
  110. int available_width = container.rect().width() - line_boxes.last().width();
  111. bool is_preformatted = style().string_or_fallback(CSS::PropertyID::WhiteSpace, "normal") == "pre";
  112. if (is_preformatted) {
  113. m_text_for_rendering = node().data();
  114. for_each_source_line([&](const Utf8View& view, int start, int length) {
  115. line_boxes.last().add_fragment(*this, start, length, font.width(view), line_height);
  116. line_boxes.append(LineBox());
  117. });
  118. return;
  119. }
  120. // Collapse whitespace into single spaces
  121. auto& raw_text = node().data();
  122. StringBuilder builder(raw_text.length());
  123. for (int i = 0; i < raw_text.length(); ++i) {
  124. if (!isspace(raw_text[i])) {
  125. builder.append(raw_text[i]);
  126. } else {
  127. builder.append(' ');
  128. while (i < raw_text.length() && isspace(raw_text[i]))
  129. ++i;
  130. --i;
  131. }
  132. }
  133. m_text_for_rendering = builder.to_string();
  134. struct Word {
  135. Utf8View view;
  136. int start;
  137. int length;
  138. };
  139. Vector<Word> words;
  140. for_each_word([&](const Utf8View& view, int start, int length) {
  141. words.append({ Utf8View(view), start, length });
  142. });
  143. for (int i = 0; i < words.size(); ++i) {
  144. auto& word = words[i];
  145. int word_width;
  146. bool is_whitespace = isspace(*word.view.begin());
  147. if (is_whitespace)
  148. word_width = space_width;
  149. else
  150. word_width = font.width(word.view) + font.glyph_spacing();
  151. if (word_width > available_width) {
  152. line_boxes.append(LineBox());
  153. available_width = container.rect().width();
  154. }
  155. if (is_whitespace && line_boxes.last().fragments().is_empty())
  156. continue;
  157. line_boxes.last().add_fragment(*this, word.start, is_whitespace ? 1 : word.length, word_width, line_height);
  158. available_width -= word_width;
  159. if (available_width < 0) {
  160. line_boxes.append(LineBox());
  161. available_width = container.rect().width();
  162. }
  163. }
  164. }