Length.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/CSS/Length.h>
  7. #include <LibWeb/DOM/Document.h>
  8. #include <LibWeb/HTML/HTMLHtmlElement.h>
  9. #include <LibWeb/Page/BrowsingContext.h>
  10. namespace Web::CSS {
  11. float Length::relative_length_to_px(const Layout::Node& layout_node) const
  12. {
  13. switch (m_type) {
  14. case Type::Ex:
  15. return m_value * layout_node.font().x_height();
  16. case Type::Em:
  17. return m_value * layout_node.font_size();
  18. case Type::Rem:
  19. return m_value * layout_node.document().document_element()->layout_node()->font_size();
  20. case Type::Vw:
  21. return layout_node.document().browsing_context()->viewport_rect().width() * (m_value / 100);
  22. case Type::Vh:
  23. return layout_node.document().browsing_context()->viewport_rect().height() * (m_value / 100);
  24. case Type::Vmin: {
  25. auto viewport = layout_node.document().browsing_context()->viewport_rect();
  26. return min(viewport.width(), viewport.height()) * (m_value / 100);
  27. }
  28. case Type::Vmax: {
  29. auto viewport = layout_node.document().browsing_context()->viewport_rect();
  30. return max(viewport.width(), viewport.height()) * (m_value / 100);
  31. }
  32. default:
  33. VERIFY_NOT_REACHED();
  34. }
  35. }
  36. const char* Length::unit_name() const
  37. {
  38. switch (m_type) {
  39. case Type::Cm:
  40. return "cm";
  41. case Type::In:
  42. return "in";
  43. case Type::Px:
  44. return "px";
  45. case Type::Pt:
  46. return "pt";
  47. case Type::Mm:
  48. return "mm";
  49. case Type::Q:
  50. return "Q";
  51. case Type::Pc:
  52. return "pc";
  53. case Type::Ex:
  54. return "ex";
  55. case Type::Em:
  56. return "em";
  57. case Type::Rem:
  58. return "rem";
  59. case Type::Auto:
  60. return "auto";
  61. case Type::Percentage:
  62. return "%";
  63. case Type::Undefined:
  64. return "undefined";
  65. case Type::Vh:
  66. return "vh";
  67. case Type::Vw:
  68. return "vw";
  69. case Type::Vmax:
  70. return "vmax";
  71. case Type::Vmin:
  72. return "vmin";
  73. }
  74. VERIFY_NOT_REACHED();
  75. }
  76. }