Length.cpp 13 KB

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