LayoutText.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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("white-space", "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("color", document(), Color::Black);
  39. auto text_decoration = style().string_or_fallback("text-decoration", "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(), node().data().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(node().data());
  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(node().data());
  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("white-space", "normal") == "pre";
  112. if (is_preformatted) {
  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. struct Word {
  120. Utf8View view;
  121. int start;
  122. int length;
  123. };
  124. Vector<Word> words;
  125. for_each_word([&](const Utf8View& view, int start, int length) {
  126. words.append({ Utf8View(view), start, length });
  127. });
  128. for (int i = 0; i < words.size(); ++i) {
  129. auto& word = words[i];
  130. int word_width;
  131. bool is_whitespace = isspace(*word.view.begin());
  132. if (is_whitespace)
  133. word_width = space_width;
  134. else
  135. word_width = font.width(word.view) + font.glyph_spacing();
  136. if (word_width > available_width) {
  137. line_boxes.append(LineBox());
  138. available_width = container.rect().width();
  139. }
  140. if (is_whitespace && line_boxes.last().fragments().is_empty())
  141. continue;
  142. line_boxes.last().add_fragment(*this, word.start, word.length, word_width, line_height);
  143. available_width -= word_width;
  144. if (available_width < 0) {
  145. line_boxes.append(LineBox());
  146. available_width = container.rect().width();
  147. }
  148. }
  149. }