LayoutText.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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, StyleProperties&& style_properties)
  10. : LayoutInline(text, move(style_properties))
  11. {
  12. }
  13. LayoutText::~LayoutText()
  14. {
  15. }
  16. void LayoutText::load_font()
  17. {
  18. auto font_family = style_properties().string_or_fallback("font-family", "Katica");
  19. auto font_weight = style_properties().string_or_fallback("font-weight", "normal");
  20. String weight;
  21. if (font_weight == "lighter")
  22. weight = "Thin";
  23. else if (font_weight == "normal")
  24. weight = "";
  25. else if (font_weight == "bold")
  26. weight = "Bold";
  27. else
  28. ASSERT_NOT_REACHED();
  29. auto look_for_file = [](const StringView& expected_name) -> String {
  30. // TODO: handle font sizes properly?
  31. CDirIterator it { "/res/fonts/", CDirIterator::Flags::SkipDots };
  32. while (it.has_next()) {
  33. String name = it.next_path();
  34. ASSERT(name.ends_with(".font"));
  35. if (!name.starts_with(expected_name))
  36. continue;
  37. // Check that a numeric size immediately
  38. // follows the font name. This prevents,
  39. // for example, matching KaticaBold when
  40. // the regular Katica is requested.
  41. if (!isdigit(name[expected_name.length()]))
  42. continue;
  43. return name;
  44. }
  45. return {};
  46. };
  47. String file_name = look_for_file(String::format("%s%s", font_family.characters(), weight.characters()));
  48. if (file_name.is_null() && weight == "")
  49. file_name = look_for_file(String::format("%sRegular", font_family.characters()));
  50. if (file_name.is_null()) {
  51. dbg() << "Failed to find a font for family " << font_family << " weight " << font_weight;
  52. dbg() << "My text is " << node().data();
  53. ASSERT_NOT_REACHED();
  54. }
  55. #ifdef HTML_DEBUG
  56. dbg() << "Found font " << file_name << " for family " << font_family << " weight " << font_weight;
  57. #endif
  58. m_font = Font::load_from_file(String::format("/res/fonts/%s", file_name.characters()));
  59. }
  60. static bool is_all_whitespace(const String& string)
  61. {
  62. for (int i = 0; i < string.length(); ++i) {
  63. if (!isspace(string[i]))
  64. return false;
  65. }
  66. return true;
  67. }
  68. const String& LayoutText::text() const
  69. {
  70. static String one_space = " ";
  71. if (is_all_whitespace(node().data()))
  72. if (style_properties().string_or_fallback("white-space", "normal") == "normal")
  73. return one_space;
  74. return node().data();
  75. }
  76. void LayoutText::render_fragment(RenderingContext& context, const LineBoxFragment& fragment) const
  77. {
  78. auto& painter = context.painter();
  79. painter.set_font(*m_font);
  80. auto color = style_properties().color_or_fallback("color", Color::Black);
  81. auto text_decoration = style_properties().string_or_fallback("text-decoration", "none");
  82. bool is_underline = text_decoration == "underline";
  83. if (is_underline)
  84. painter.draw_line(fragment.rect().bottom_left().translated(0, -1), fragment.rect().bottom_right().translated(0, -1), color);
  85. painter.draw_text(fragment.rect(), node().data().substring_view(fragment.start(), fragment.length()), TextAlignment::TopLeft, color);
  86. }
  87. template<typename Callback>
  88. void LayoutText::for_each_word(Callback callback) const
  89. {
  90. Utf8View view(node().data());
  91. if (view.is_empty())
  92. return;
  93. auto start_of_word = view.begin();
  94. auto commit_word = [&](auto it) {
  95. int start = view.byte_offset_of(start_of_word);
  96. int length = view.byte_offset_of(it) - view.byte_offset_of(start_of_word);
  97. if (length > 0) {
  98. callback(view.substring_view(start, length), start, length);
  99. }
  100. start_of_word = it;
  101. };
  102. bool last_was_space = isspace(*view.begin());
  103. for (auto it = view.begin(); it != view.end();) {
  104. bool is_space = isspace(*it);
  105. if (is_space == last_was_space) {
  106. ++it;
  107. continue;
  108. }
  109. last_was_space = is_space;
  110. commit_word(it);
  111. ++it;
  112. }
  113. if (start_of_word != view.end())
  114. commit_word(view.end());
  115. }
  116. template<typename Callback>
  117. void LayoutText::for_each_source_line(Callback callback) const
  118. {
  119. Utf8View view(node().data());
  120. if (view.is_empty())
  121. return;
  122. auto start_of_line = view.begin();
  123. auto commit_line = [&](auto it) {
  124. int start = view.byte_offset_of(start_of_line);
  125. int length = view.byte_offset_of(it) - view.byte_offset_of(start_of_line);
  126. if (length > 0) {
  127. callback(view.substring_view(start, length), start, length);
  128. }
  129. };
  130. for (auto it = view.begin(); it != view.end();) {
  131. bool did_commit = false;
  132. if (*it == '\n') {
  133. commit_line(it);
  134. did_commit = true;
  135. }
  136. ++it;
  137. if (did_commit)
  138. start_of_line = it;
  139. }
  140. if (start_of_line != view.end())
  141. commit_line(view.end());
  142. }
  143. void LayoutText::split_into_lines(LayoutBlock& container)
  144. {
  145. if (!m_font)
  146. load_font();
  147. int space_width = m_font->glyph_width(' ') + m_font->glyph_spacing();
  148. // FIXME: Allow overriding the line-height. We currently default to 140% which seems to look nice.
  149. int line_height = (int)(m_font->glyph_height() * 1.4f);
  150. auto& line_boxes = container.line_boxes();
  151. if (line_boxes.is_empty())
  152. line_boxes.append(LineBox());
  153. int available_width = container.rect().width() - line_boxes.last().width();
  154. bool is_preformatted = style_properties().string_or_fallback("white-space", "normal") == "pre";
  155. if (is_preformatted) {
  156. for_each_source_line([&](const Utf8View& view, int start, int length) {
  157. line_boxes.last().add_fragment(*this, start, length, m_font->width(view), line_height);
  158. line_boxes.append(LineBox());
  159. });
  160. return;
  161. }
  162. struct Word {
  163. Utf8View view;
  164. int start;
  165. int length;
  166. };
  167. Vector<Word> words;
  168. for_each_word([&](const Utf8View& view, int start, int length) {
  169. words.append({ Utf8View(view), start, length });
  170. });
  171. for (int i = 0; i < words.size(); ++i) {
  172. auto& word = words[i];
  173. int word_width;
  174. bool is_whitespace = isspace(*word.view.begin());
  175. if (is_whitespace)
  176. word_width = space_width;
  177. else
  178. word_width = m_font->width(word.view) + m_font->glyph_spacing();
  179. if (word_width > available_width) {
  180. line_boxes.append(LineBox());
  181. available_width = container.rect().width();
  182. }
  183. if (is_whitespace && line_boxes.last().fragments().is_empty())
  184. continue;
  185. line_boxes.last().add_fragment(*this, word.start, word.length, word_width, line_height);
  186. available_width -= word_width;
  187. if (available_width < 0) {
  188. line_boxes.append(LineBox());
  189. available_width = container.rect().width();
  190. }
  191. }
  192. }