Length.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
  4. * Copyright (c) 2022-2023, Sam Atkins <atkinssj@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <AK/NonnullOwnPtr.h>
  9. #include <AK/Variant.h>
  10. #include <LibGfx/Font/Font.h>
  11. #include <LibGfx/Rect.h>
  12. #include <LibWeb/CSS/Length.h>
  13. #include <LibWeb/CSS/Percentage.h>
  14. #include <LibWeb/DOM/Document.h>
  15. #include <LibWeb/HTML/BrowsingContext.h>
  16. #include <LibWeb/HTML/HTMLHtmlElement.h>
  17. #include <LibWeb/Layout/Node.h>
  18. namespace Web::CSS {
  19. Length::FontMetrics::FontMetrics(CSSPixels font_size, Gfx::FontPixelMetrics const& pixel_metrics)
  20. : font_size(font_size)
  21. , x_height(pixel_metrics.x_height)
  22. // FIXME: This is only approximately the cap height. The spec suggests measuring the "O" glyph:
  23. // https://www.w3.org/TR/css-values-4/#cap
  24. , cap_height(pixel_metrics.ascent)
  25. , zero_advance(pixel_metrics.advance_of_ascii_zero + pixel_metrics.glyph_spacing)
  26. , line_height(round(pixel_metrics.line_spacing()))
  27. {
  28. }
  29. Length::Length(double value, Type type)
  30. : m_type(type)
  31. , m_value(value)
  32. {
  33. }
  34. Length::~Length() = default;
  35. Length Length::make_auto()
  36. {
  37. return Length(0, Type::Auto);
  38. }
  39. Length Length::make_px(CSSPixels value)
  40. {
  41. return Length(value.to_double(), Type::Px);
  42. }
  43. Length Length::percentage_of(Percentage const& percentage) const
  44. {
  45. if (is_auto()) {
  46. dbgln("Attempting to get percentage of an auto length, this seems wrong? But for now we just return the original length.");
  47. return *this;
  48. }
  49. return Length { percentage.as_fraction() * raw_value(), m_type };
  50. }
  51. CSSPixels Length::font_relative_length_to_px(Length::FontMetrics const& font_metrics, Length::FontMetrics const& root_font_metrics) const
  52. {
  53. switch (m_type) {
  54. case Type::Em:
  55. return CSSPixels::nearest_value_for(m_value * font_metrics.font_size.to_double());
  56. case Type::Rem:
  57. return CSSPixels::nearest_value_for(m_value * root_font_metrics.font_size.to_double());
  58. case Type::Ex:
  59. return CSSPixels::nearest_value_for(m_value * font_metrics.x_height.to_double());
  60. case Type::Rex:
  61. return CSSPixels::nearest_value_for(m_value * root_font_metrics.x_height.to_double());
  62. case Type::Cap:
  63. return CSSPixels::nearest_value_for(m_value * font_metrics.cap_height.to_double());
  64. case Type::Rcap:
  65. return CSSPixels::nearest_value_for(m_value * root_font_metrics.cap_height.to_double());
  66. case Type::Ch:
  67. return CSSPixels::nearest_value_for(m_value * font_metrics.zero_advance.to_double());
  68. case Type::Rch:
  69. return CSSPixels::nearest_value_for(m_value * root_font_metrics.zero_advance.to_double());
  70. case Type::Ic:
  71. // FIXME: Use the "advance measure of the “水” (CJK water ideograph, U+6C34) glyph"
  72. return CSSPixels::nearest_value_for(m_value * font_metrics.font_size.to_double());
  73. case Type::Ric:
  74. // FIXME: Use the "advance measure of the “水” (CJK water ideograph, U+6C34) glyph"
  75. return CSSPixels::nearest_value_for(m_value * root_font_metrics.font_size.to_double());
  76. case Type::Lh:
  77. return CSSPixels::nearest_value_for(m_value * font_metrics.line_height.to_double());
  78. case Type::Rlh:
  79. return CSSPixels::nearest_value_for(m_value * root_font_metrics.line_height.to_double());
  80. default:
  81. VERIFY_NOT_REACHED();
  82. }
  83. }
  84. CSSPixels Length::viewport_relative_length_to_px(CSSPixelRect const& viewport_rect) const
  85. {
  86. switch (m_type) {
  87. case Type::Vw:
  88. case Type::Svw:
  89. case Type::Lvw:
  90. case Type::Dvw:
  91. return viewport_rect.width() * (CSSPixels::nearest_value_for(m_value) / 100);
  92. case Type::Vh:
  93. case Type::Svh:
  94. case Type::Lvh:
  95. case Type::Dvh:
  96. return viewport_rect.height() * (CSSPixels::nearest_value_for(m_value) / 100);
  97. case Type::Vi:
  98. case Type::Svi:
  99. case Type::Lvi:
  100. case Type::Dvi:
  101. // FIXME: Select the width or height based on which is the inline axis.
  102. return viewport_rect.width() * (CSSPixels::nearest_value_for(m_value) / 100);
  103. case Type::Vb:
  104. case Type::Svb:
  105. case Type::Lvb:
  106. case Type::Dvb:
  107. // FIXME: Select the width or height based on which is the block axis.
  108. return viewport_rect.height() * (CSSPixels::nearest_value_for(m_value) / 100);
  109. case Type::Vmin:
  110. case Type::Svmin:
  111. case Type::Lvmin:
  112. case Type::Dvmin:
  113. return min(viewport_rect.width(), viewport_rect.height()) * (CSSPixels::nearest_value_for(m_value) / 100);
  114. case Type::Vmax:
  115. case Type::Svmax:
  116. case Type::Lvmax:
  117. case Type::Dvmax:
  118. return max(viewport_rect.width(), viewport_rect.height()) * (CSSPixels::nearest_value_for(m_value) / 100);
  119. default:
  120. VERIFY_NOT_REACHED();
  121. }
  122. }
  123. Length::ResolutionContext Length::ResolutionContext::for_layout_node(Layout::Node const& node)
  124. {
  125. auto const* root_element = node.document().document_element();
  126. VERIFY(root_element);
  127. VERIFY(root_element->layout_node());
  128. return Length::ResolutionContext {
  129. .viewport_rect = node.navigable()->viewport_rect(),
  130. .font_metrics = { node.computed_values().font_size(), node.first_available_font().pixel_metrics() },
  131. .root_font_metrics = { root_element->layout_node()->computed_values().font_size(), root_element->layout_node()->first_available_font().pixel_metrics() },
  132. };
  133. }
  134. CSSPixels Length::to_px(ResolutionContext const& context) const
  135. {
  136. return to_px(context.viewport_rect, context.font_metrics, context.root_font_metrics);
  137. }
  138. CSSPixels Length::to_px_slow_case(Layout::Node const& layout_node) const
  139. {
  140. if (is_auto()) {
  141. // FIXME: We really, really shouldn't end up here, but we do, and so frequently that
  142. // adding a dbgln() here outputs a couple hundred lines loading `welcome.html`.
  143. return 0;
  144. }
  145. if (!layout_node.document().browsing_context())
  146. return 0;
  147. if (is_font_relative()) {
  148. auto* root_element = layout_node.document().document_element();
  149. if (!root_element || !root_element->layout_node())
  150. return 0;
  151. FontMetrics font_metrics {
  152. layout_node.computed_values().font_size(),
  153. layout_node.first_available_font().pixel_metrics(),
  154. };
  155. FontMetrics root_font_metrics {
  156. root_element->layout_node()->computed_values().font_size(),
  157. root_element->layout_node()->first_available_font().pixel_metrics(),
  158. };
  159. return font_relative_length_to_px(font_metrics, root_font_metrics);
  160. }
  161. VERIFY(is_viewport_relative());
  162. auto const& viewport_rect = layout_node.document().viewport_rect();
  163. return viewport_relative_length_to_px(viewport_rect);
  164. }
  165. String Length::to_string() const
  166. {
  167. if (is_auto())
  168. return "auto"_string;
  169. return MUST(String::formatted("{:.5}{}", m_value, unit_name()));
  170. }
  171. char const* Length::unit_name() const
  172. {
  173. switch (m_type) {
  174. case Type::Em:
  175. return "em";
  176. case Type::Rem:
  177. return "rem";
  178. case Type::Ex:
  179. return "ex";
  180. case Type::Rex:
  181. return "rex";
  182. case Type::Cap:
  183. return "cap";
  184. case Type::Rcap:
  185. return "rcap";
  186. case Type::Ch:
  187. return "ch";
  188. case Type::Rch:
  189. return "rch";
  190. case Type::Ic:
  191. return "ic";
  192. case Type::Ric:
  193. return "ric";
  194. case Type::Lh:
  195. return "lh";
  196. case Type::Rlh:
  197. return "rlh";
  198. case Type::Vw:
  199. return "vw";
  200. case Type::Svw:
  201. return "svw";
  202. case Type::Lvw:
  203. return "lvw";
  204. case Type::Dvw:
  205. return "dvw";
  206. case Type::Vh:
  207. return "vh";
  208. case Type::Svh:
  209. return "svh";
  210. case Type::Lvh:
  211. return "lvh";
  212. case Type::Dvh:
  213. return "dvh";
  214. case Type::Vi:
  215. return "vi";
  216. case Type::Svi:
  217. return "svi";
  218. case Type::Lvi:
  219. return "lvi";
  220. case Type::Dvi:
  221. return "dvi";
  222. case Type::Vb:
  223. return "vb";
  224. case Type::Svb:
  225. return "svb";
  226. case Type::Lvb:
  227. return "lvb";
  228. case Type::Dvb:
  229. return "dvb";
  230. case Type::Vmin:
  231. return "vmin";
  232. case Type::Svmin:
  233. return "svmin";
  234. case Type::Lvmin:
  235. return "lvmin";
  236. case Type::Dvmin:
  237. return "dvmin";
  238. case Type::Vmax:
  239. return "vmax";
  240. case Type::Svmax:
  241. return "svmax";
  242. case Type::Lvmax:
  243. return "lvmax";
  244. case Type::Dvmax:
  245. return "dvmax";
  246. case Type::Cm:
  247. return "cm";
  248. case Type::Mm:
  249. return "mm";
  250. case Type::Q:
  251. return "Q";
  252. case Type::In:
  253. return "in";
  254. case Type::Pt:
  255. return "pt";
  256. case Type::Pc:
  257. return "pc";
  258. case Type::Px:
  259. return "px";
  260. case Type::Auto:
  261. return "auto";
  262. }
  263. VERIFY_NOT_REACHED();
  264. }
  265. Optional<Length::Type> Length::unit_from_name(StringView name)
  266. {
  267. if (name.equals_ignoring_ascii_case("em"sv)) {
  268. return Length::Type::Em;
  269. } else if (name.equals_ignoring_ascii_case("rem"sv)) {
  270. return Length::Type::Rem;
  271. } else if (name.equals_ignoring_ascii_case("ex"sv)) {
  272. return Length::Type::Ex;
  273. } else if (name.equals_ignoring_ascii_case("rex"sv)) {
  274. return Length::Type::Rex;
  275. } else if (name.equals_ignoring_ascii_case("cap"sv)) {
  276. return Length::Type::Cap;
  277. } else if (name.equals_ignoring_ascii_case("rcap"sv)) {
  278. return Length::Type::Rcap;
  279. } else if (name.equals_ignoring_ascii_case("ch"sv)) {
  280. return Length::Type::Ch;
  281. } else if (name.equals_ignoring_ascii_case("rch"sv)) {
  282. return Length::Type::Rch;
  283. } else if (name.equals_ignoring_ascii_case("ic"sv)) {
  284. return Length::Type::Ic;
  285. } else if (name.equals_ignoring_ascii_case("ric"sv)) {
  286. return Length::Type::Ric;
  287. } else if (name.equals_ignoring_ascii_case("lh"sv)) {
  288. return Length::Type::Lh;
  289. } else if (name.equals_ignoring_ascii_case("rlh"sv)) {
  290. return Length::Type::Rlh;
  291. } else if (name.equals_ignoring_ascii_case("vw"sv)) {
  292. return Length::Type::Vw;
  293. } else if (name.equals_ignoring_ascii_case("svw"sv)) {
  294. return Length::Type::Svw;
  295. } else if (name.equals_ignoring_ascii_case("lvw"sv)) {
  296. return Length::Type::Lvw;
  297. } else if (name.equals_ignoring_ascii_case("dvw"sv)) {
  298. return Length::Type::Dvw;
  299. } else if (name.equals_ignoring_ascii_case("vh"sv)) {
  300. return Length::Type::Vh;
  301. } else if (name.equals_ignoring_ascii_case("svh"sv)) {
  302. return Length::Type::Svh;
  303. } else if (name.equals_ignoring_ascii_case("lvh"sv)) {
  304. return Length::Type::Lvh;
  305. } else if (name.equals_ignoring_ascii_case("dvh"sv)) {
  306. return Length::Type::Dvh;
  307. } else if (name.equals_ignoring_ascii_case("vi"sv)) {
  308. return Length::Type::Vi;
  309. } else if (name.equals_ignoring_ascii_case("svi"sv)) {
  310. return Length::Type::Svi;
  311. } else if (name.equals_ignoring_ascii_case("lvi"sv)) {
  312. return Length::Type::Lvi;
  313. } else if (name.equals_ignoring_ascii_case("dvi"sv)) {
  314. return Length::Type::Dvi;
  315. } else if (name.equals_ignoring_ascii_case("vb"sv)) {
  316. return Length::Type::Vb;
  317. } else if (name.equals_ignoring_ascii_case("svb"sv)) {
  318. return Length::Type::Svb;
  319. } else if (name.equals_ignoring_ascii_case("lvb"sv)) {
  320. return Length::Type::Lvb;
  321. } else if (name.equals_ignoring_ascii_case("dvb"sv)) {
  322. return Length::Type::Dvb;
  323. } else if (name.equals_ignoring_ascii_case("vmin"sv)) {
  324. return Length::Type::Vmin;
  325. } else if (name.equals_ignoring_ascii_case("svmin"sv)) {
  326. return Length::Type::Svmin;
  327. } else if (name.equals_ignoring_ascii_case("lvmin"sv)) {
  328. return Length::Type::Lvmin;
  329. } else if (name.equals_ignoring_ascii_case("dvmin"sv)) {
  330. return Length::Type::Dvmin;
  331. } else if (name.equals_ignoring_ascii_case("vmax"sv)) {
  332. return Length::Type::Vmax;
  333. } else if (name.equals_ignoring_ascii_case("svmax"sv)) {
  334. return Length::Type::Svmax;
  335. } else if (name.equals_ignoring_ascii_case("lvmax"sv)) {
  336. return Length::Type::Lvmax;
  337. } else if (name.equals_ignoring_ascii_case("dvmax"sv)) {
  338. return Length::Type::Dvmax;
  339. } else if (name.equals_ignoring_ascii_case("cm"sv)) {
  340. return Length::Type::Cm;
  341. } else if (name.equals_ignoring_ascii_case("mm"sv)) {
  342. return Length::Type::Mm;
  343. } else if (name.equals_ignoring_ascii_case("Q"sv)) {
  344. return Length::Type::Q;
  345. } else if (name.equals_ignoring_ascii_case("in"sv)) {
  346. return Length::Type::In;
  347. } else if (name.equals_ignoring_ascii_case("pt"sv)) {
  348. return Length::Type::Pt;
  349. } else if (name.equals_ignoring_ascii_case("pc"sv)) {
  350. return Length::Type::Pc;
  351. } else if (name.equals_ignoring_ascii_case("px"sv)) {
  352. return Length::Type::Px;
  353. }
  354. return {};
  355. }
  356. Optional<Length> Length::absolutize(CSSPixelRect const& viewport_rect, FontMetrics const& font_metrics, FontMetrics const& root_font_metrics) const
  357. {
  358. if (is_px())
  359. return {};
  360. if (is_absolute() || is_relative()) {
  361. auto px = to_px(viewport_rect, font_metrics, root_font_metrics);
  362. return CSS::Length::make_px(px);
  363. }
  364. return {};
  365. }
  366. Length Length::absolutized(CSSPixelRect const& viewport_rect, FontMetrics const& font_metrics, FontMetrics const& root_font_metrics) const
  367. {
  368. return absolutize(viewport_rect, font_metrics, root_font_metrics).value_or(*this);
  369. }
  370. }