StyleProperties.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include <LibCore/CDirIterator.h>
  2. #include <LibHTML/CSS/StyleProperties.h>
  3. #include <ctype.h>
  4. void StyleProperties::set_property(const String& name, NonnullRefPtr<StyleValue> value)
  5. {
  6. m_property_values.set(name, move(value));
  7. }
  8. Optional<NonnullRefPtr<StyleValue>> StyleProperties::property(const String& name) const
  9. {
  10. auto it = m_property_values.find(name);
  11. if (it == m_property_values.end())
  12. return {};
  13. return it->value;
  14. }
  15. Length StyleProperties::length_or_fallback(const StringView& property_name, const Length& fallback) const
  16. {
  17. auto value = property(property_name);
  18. if (!value.has_value())
  19. return fallback;
  20. return value.value()->to_length();
  21. }
  22. String StyleProperties::string_or_fallback(const StringView& property_name, const StringView& fallback) const
  23. {
  24. auto value = property(property_name);
  25. if (!value.has_value())
  26. return fallback;
  27. return value.value()->to_string();
  28. }
  29. Color StyleProperties::color_or_fallback(const StringView& property_name, const Document& document, Color fallback) const
  30. {
  31. auto value = property(property_name);
  32. if (!value.has_value())
  33. return fallback;
  34. return value.value()->to_color(document);
  35. }
  36. void StyleProperties::load_font() const
  37. {
  38. auto font_family = string_or_fallback("font-family", "Katica");
  39. auto font_weight = string_or_fallback("font-weight", "normal");
  40. String weight;
  41. if (font_weight == "lighter")
  42. weight = "Thin";
  43. else if (font_weight == "normal")
  44. weight = "";
  45. else if (font_weight == "bold")
  46. weight = "Bold";
  47. else
  48. ASSERT_NOT_REACHED();
  49. auto look_for_file = [](const StringView& expected_name) -> String {
  50. // TODO: handle font sizes properly?
  51. CDirIterator it { "/res/fonts/", CDirIterator::Flags::SkipDots };
  52. while (it.has_next()) {
  53. String name = it.next_path();
  54. ASSERT(name.ends_with(".font"));
  55. if (!name.starts_with(expected_name))
  56. continue;
  57. // Check that a numeric size immediately
  58. // follows the font name. This prevents,
  59. // for example, matching KaticaBold when
  60. // the regular Katica is requested.
  61. if (!isdigit(name[expected_name.length()]))
  62. continue;
  63. return name;
  64. }
  65. return {};
  66. };
  67. String file_name = look_for_file(String::format("%s%s", font_family.characters(), weight.characters()));
  68. if (file_name.is_null() && weight == "")
  69. file_name = look_for_file(String::format("%sRegular", font_family.characters()));
  70. if (file_name.is_null()) {
  71. dbg() << "Failed to find a font for family " << font_family << " weight " << font_weight;
  72. m_font = Font::default_font();
  73. return;
  74. }
  75. #ifdef HTML_DEBUG
  76. dbg() << "Found font " << file_name << " for family " << font_family << " weight " << font_weight;
  77. #endif
  78. m_font = Font::load_from_file(String::format("/res/fonts/%s", file_name.characters()));
  79. }