Length.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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.value(), 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;
  68. case Type::Rem:
  69. return m_value * root_font_metrics.font_size;
  70. case Type::Ex:
  71. return m_value * font_metrics.x_height;
  72. case Type::Rex:
  73. return m_value * root_font_metrics.x_height;
  74. case Type::Cap:
  75. return m_value * font_metrics.cap_height;
  76. case Type::Rcap:
  77. return m_value * root_font_metrics.cap_height;
  78. case Type::Ch:
  79. return m_value * font_metrics.zero_advance;
  80. case Type::Rch:
  81. return m_value * root_font_metrics.zero_advance;
  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;
  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;
  88. case Type::Lh:
  89. return m_value * font_metrics.line_height;
  90. case Type::Rlh:
  91. return m_value * root_font_metrics.line_height;
  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. CSSPixels Length::to_px(Layout::Node const& layout_node) const
  136. {
  137. if (is_auto()) {
  138. // FIXME: We really, really shouldn't end up here, but we do, and so frequently that
  139. // adding a dbgln() here outputs a couple hundred lines loading `welcome.html`.
  140. return 0;
  141. }
  142. if (is_absolute())
  143. return absolute_length_to_px();
  144. if (!layout_node.document().browsing_context())
  145. return 0;
  146. if (is_font_relative()) {
  147. auto* root_element = layout_node.document().document_element();
  148. if (!root_element || !root_element->layout_node())
  149. return 0;
  150. FontMetrics font_metrics {
  151. layout_node.computed_values().font_size(),
  152. layout_node.font().pixel_metrics(),
  153. layout_node.line_height()
  154. };
  155. FontMetrics root_font_metrics {
  156. root_element->layout_node()->computed_values().font_size(),
  157. root_element->layout_node()->font().pixel_metrics(),
  158. root_element->layout_node()->line_height()
  159. };
  160. return font_relative_length_to_px(font_metrics, root_font_metrics);
  161. }
  162. VERIFY(is_viewport_relative());
  163. auto const& viewport_rect = layout_node.document().browsing_context()->viewport_rect();
  164. return viewport_relative_length_to_px(viewport_rect);
  165. }
  166. ErrorOr<String> Length::to_string() const
  167. {
  168. if (is_auto())
  169. return "auto"_string;
  170. return String::formatted("{}{}", m_value, unit_name());
  171. }
  172. char const* Length::unit_name() const
  173. {
  174. switch (m_type) {
  175. case Type::Em:
  176. return "em";
  177. case Type::Rem:
  178. return "rem";
  179. case Type::Ex:
  180. return "ex";
  181. case Type::Rex:
  182. return "rex";
  183. case Type::Cap:
  184. return "cap";
  185. case Type::Rcap:
  186. return "rcap";
  187. case Type::Ch:
  188. return "ch";
  189. case Type::Rch:
  190. return "rch";
  191. case Type::Ic:
  192. return "ic";
  193. case Type::Ric:
  194. return "ric";
  195. case Type::Lh:
  196. return "lh";
  197. case Type::Rlh:
  198. return "rlh";
  199. case Type::Vw:
  200. return "vw";
  201. case Type::Svw:
  202. return "svw";
  203. case Type::Lvw:
  204. return "lvw";
  205. case Type::Dvw:
  206. return "dvw";
  207. case Type::Vh:
  208. return "vh";
  209. case Type::Svh:
  210. return "svh";
  211. case Type::Lvh:
  212. return "lvh";
  213. case Type::Dvh:
  214. return "dvh";
  215. case Type::Vi:
  216. return "vi";
  217. case Type::Svi:
  218. return "svi";
  219. case Type::Lvi:
  220. return "lvi";
  221. case Type::Dvi:
  222. return "dvi";
  223. case Type::Vb:
  224. return "vb";
  225. case Type::Svb:
  226. return "svb";
  227. case Type::Lvb:
  228. return "lvb";
  229. case Type::Dvb:
  230. return "dvb";
  231. case Type::Vmin:
  232. return "vmin";
  233. case Type::Svmin:
  234. return "svmin";
  235. case Type::Lvmin:
  236. return "lvmin";
  237. case Type::Dvmin:
  238. return "dvmin";
  239. case Type::Vmax:
  240. return "vmax";
  241. case Type::Svmax:
  242. return "svmax";
  243. case Type::Lvmax:
  244. return "lvmax";
  245. case Type::Dvmax:
  246. return "dvmax";
  247. case Type::Cm:
  248. return "cm";
  249. case Type::Mm:
  250. return "mm";
  251. case Type::Q:
  252. return "Q";
  253. case Type::In:
  254. return "in";
  255. case Type::Pt:
  256. return "pt";
  257. case Type::Pc:
  258. return "pc";
  259. case Type::Px:
  260. return "px";
  261. case Type::Auto:
  262. return "auto";
  263. }
  264. VERIFY_NOT_REACHED();
  265. }
  266. Optional<Length::Type> Length::unit_from_name(StringView name)
  267. {
  268. if (name.equals_ignoring_ascii_case("em"sv)) {
  269. return Length::Type::Em;
  270. } else if (name.equals_ignoring_ascii_case("rem"sv)) {
  271. return Length::Type::Rem;
  272. } else if (name.equals_ignoring_ascii_case("ex"sv)) {
  273. return Length::Type::Ex;
  274. } else if (name.equals_ignoring_ascii_case("rex"sv)) {
  275. return Length::Type::Rex;
  276. } else if (name.equals_ignoring_ascii_case("cap"sv)) {
  277. return Length::Type::Cap;
  278. } else if (name.equals_ignoring_ascii_case("rcap"sv)) {
  279. return Length::Type::Rcap;
  280. } else if (name.equals_ignoring_ascii_case("ch"sv)) {
  281. return Length::Type::Ch;
  282. } else if (name.equals_ignoring_ascii_case("rch"sv)) {
  283. return Length::Type::Rch;
  284. } else if (name.equals_ignoring_ascii_case("ic"sv)) {
  285. return Length::Type::Ic;
  286. } else if (name.equals_ignoring_ascii_case("ric"sv)) {
  287. return Length::Type::Ric;
  288. } else if (name.equals_ignoring_ascii_case("lh"sv)) {
  289. return Length::Type::Lh;
  290. } else if (name.equals_ignoring_ascii_case("rlh"sv)) {
  291. return Length::Type::Rlh;
  292. } else if (name.equals_ignoring_ascii_case("vw"sv)) {
  293. return Length::Type::Vw;
  294. } else if (name.equals_ignoring_ascii_case("svw"sv)) {
  295. return Length::Type::Svw;
  296. } else if (name.equals_ignoring_ascii_case("lvw"sv)) {
  297. return Length::Type::Lvw;
  298. } else if (name.equals_ignoring_ascii_case("dvw"sv)) {
  299. return Length::Type::Dvw;
  300. } else if (name.equals_ignoring_ascii_case("vh"sv)) {
  301. return Length::Type::Vh;
  302. } else if (name.equals_ignoring_ascii_case("svh"sv)) {
  303. return Length::Type::Svh;
  304. } else if (name.equals_ignoring_ascii_case("lvh"sv)) {
  305. return Length::Type::Lvh;
  306. } else if (name.equals_ignoring_ascii_case("dvh"sv)) {
  307. return Length::Type::Dvh;
  308. } else if (name.equals_ignoring_ascii_case("vi"sv)) {
  309. return Length::Type::Vi;
  310. } else if (name.equals_ignoring_ascii_case("svi"sv)) {
  311. return Length::Type::Svi;
  312. } else if (name.equals_ignoring_ascii_case("lvi"sv)) {
  313. return Length::Type::Lvi;
  314. } else if (name.equals_ignoring_ascii_case("dvi"sv)) {
  315. return Length::Type::Dvi;
  316. } else if (name.equals_ignoring_ascii_case("vb"sv)) {
  317. return Length::Type::Vb;
  318. } else if (name.equals_ignoring_ascii_case("svb"sv)) {
  319. return Length::Type::Svb;
  320. } else if (name.equals_ignoring_ascii_case("lvb"sv)) {
  321. return Length::Type::Lvb;
  322. } else if (name.equals_ignoring_ascii_case("dvb"sv)) {
  323. return Length::Type::Dvb;
  324. } else if (name.equals_ignoring_ascii_case("vmin"sv)) {
  325. return Length::Type::Vmin;
  326. } else if (name.equals_ignoring_ascii_case("svmin"sv)) {
  327. return Length::Type::Svmin;
  328. } else if (name.equals_ignoring_ascii_case("lvmin"sv)) {
  329. return Length::Type::Lvmin;
  330. } else if (name.equals_ignoring_ascii_case("dvmin"sv)) {
  331. return Length::Type::Dvmin;
  332. } else if (name.equals_ignoring_ascii_case("vmax"sv)) {
  333. return Length::Type::Vmax;
  334. } else if (name.equals_ignoring_ascii_case("svmax"sv)) {
  335. return Length::Type::Svmax;
  336. } else if (name.equals_ignoring_ascii_case("lvmax"sv)) {
  337. return Length::Type::Lvmax;
  338. } else if (name.equals_ignoring_ascii_case("dvmax"sv)) {
  339. return Length::Type::Dvmax;
  340. } else if (name.equals_ignoring_ascii_case("cm"sv)) {
  341. return Length::Type::Cm;
  342. } else if (name.equals_ignoring_ascii_case("mm"sv)) {
  343. return Length::Type::Mm;
  344. } else if (name.equals_ignoring_ascii_case("Q"sv)) {
  345. return Length::Type::Q;
  346. } else if (name.equals_ignoring_ascii_case("in"sv)) {
  347. return Length::Type::In;
  348. } else if (name.equals_ignoring_ascii_case("pt"sv)) {
  349. return Length::Type::Pt;
  350. } else if (name.equals_ignoring_ascii_case("pc"sv)) {
  351. return Length::Type::Pc;
  352. } else if (name.equals_ignoring_ascii_case("px"sv)) {
  353. return Length::Type::Px;
  354. }
  355. return {};
  356. }
  357. Optional<Length> Length::absolutize(CSSPixelRect const& viewport_rect, FontMetrics const& font_metrics, FontMetrics const& root_font_metrics) const
  358. {
  359. if (is_px())
  360. return {};
  361. if (is_absolute() || is_relative()) {
  362. auto px = to_px(viewport_rect, font_metrics, root_font_metrics);
  363. return CSS::Length::make_px(px);
  364. }
  365. return {};
  366. }
  367. Length Length::absolutized(CSSPixelRect const& viewport_rect, FontMetrics const& font_metrics, FontMetrics const& root_font_metrics) const
  368. {
  369. return absolutize(viewport_rect, font_metrics, root_font_metrics).value_or(*this);
  370. }
  371. }