Serenity Operating System
at hosted 201 lines 6.7 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include <LibCore/DirIterator.h> 28#include <LibWeb/CSS/StyleProperties.h> 29#include <LibWeb/FontCache.h> 30#include <ctype.h> 31 32namespace Web { 33 34StyleProperties::StyleProperties() 35{ 36} 37 38StyleProperties::StyleProperties(const StyleProperties& other) 39 : m_property_values(other.m_property_values) 40{ 41 if (other.m_font) { 42 m_font = other.m_font->clone(); 43 } else { 44 m_font = nullptr; 45 } 46} 47 48NonnullRefPtr<StyleProperties> StyleProperties::clone() const 49{ 50 return adopt(*new StyleProperties(*this)); 51} 52 53void StyleProperties::set_property(CSS::PropertyID id, NonnullRefPtr<StyleValue> value) 54{ 55 m_property_values.set((unsigned)id, move(value)); 56} 57 58Optional<NonnullRefPtr<StyleValue>> StyleProperties::property(CSS::PropertyID id) const 59{ 60 auto it = m_property_values.find((unsigned)id); 61 if (it == m_property_values.end()) 62 return {}; 63 return it->value; 64} 65 66Length StyleProperties::length_or_fallback(CSS::PropertyID id, const Length& fallback) const 67{ 68 auto value = property(id); 69 if (!value.has_value()) 70 return fallback; 71 return value.value()->to_length(); 72} 73 74String StyleProperties::string_or_fallback(CSS::PropertyID id, const StringView& fallback) const 75{ 76 auto value = property(id); 77 if (!value.has_value()) 78 return fallback; 79 return value.value()->to_string(); 80} 81 82Color StyleProperties::color_or_fallback(CSS::PropertyID id, const Document& document, Color fallback) const 83{ 84 auto value = property(id); 85 if (!value.has_value()) 86 return fallback; 87 return value.value()->to_color(document); 88} 89 90void StyleProperties::load_font() const 91{ 92 auto font_family = string_or_fallback(CSS::PropertyID::FontFamily, "Katica"); 93 auto font_weight = string_or_fallback(CSS::PropertyID::FontWeight, "normal"); 94 95 if (auto cached_font = FontCache::the().get({ font_family, font_weight })) { 96 m_font = cached_font; 97 return; 98 } 99 100 String weight; 101 if (font_weight == "lighter") 102 weight = "Thin"; 103 else if (font_weight == "normal") 104 weight = ""; 105 else if (font_weight == "bold") 106 weight = "Bold"; 107 else { 108 dbg() << "Unknown font-weight: " << font_weight; 109 weight = ""; 110 } 111 112 auto look_for_file = [](const StringView& expected_name) -> String { 113 // TODO: handle font sizes properly? 114 Core::DirIterator it { "/res/fonts/", Core::DirIterator::Flags::SkipDots }; 115 while (it.has_next()) { 116 String name = it.next_path(); 117 118 if (!name.ends_with(".font")) 119 continue; 120 if (!name.starts_with(expected_name)) 121 continue; 122 123 // Check that a numeric size immediately 124 // follows the font name. This prevents, 125 // for example, matching KaticaBold when 126 // the regular Katica is requested. 127 if (!isdigit(name[expected_name.length()])) 128 continue; 129 130 return name; 131 } 132 return {}; 133 }; 134 135 String file_name = look_for_file(String::format("%s%s", font_family.characters(), weight.characters())); 136 if (file_name.is_null() && weight == "") 137 file_name = look_for_file(String::format("%sRegular", font_family.characters())); 138 139 if (file_name.is_null()) { 140 dbg() << "Failed to find a font for family " << font_family << " weight " << font_weight; 141 142 if (font_weight == "bold") 143 m_font = Gfx::Font::default_bold_font(); 144 else 145 m_font = Gfx::Font::default_font(); 146 return; 147 } 148 149#ifdef HTML_DEBUG 150 dbg() << "Found font " << file_name << " for family " << font_family << " weight " << font_weight; 151#endif 152 153 m_font = Gfx::Font::load_from_file(String::format("/res/fonts/%s", file_name.characters())); 154 FontCache::the().set({ font_family, font_weight }, *m_font); 155} 156 157float StyleProperties::line_height() const 158{ 159 auto line_height_length = length_or_fallback(CSS::PropertyID::LineHeight, {}); 160 if (line_height_length.is_absolute()) 161 return (float)font().glyph_height() * line_height_length.to_px(); 162 return (float)font().glyph_height() * 1.4f; 163} 164 165CSS::Position StyleProperties::position() const 166{ 167 if (property(CSS::PropertyID::Position).has_value()) { 168 String position_string = string_or_fallback(CSS::PropertyID::Position, "static"); 169 if (position_string == "relative") 170 return CSS::Position::Relative; 171 if (position_string == "absolute") 172 return CSS::Position::Absolute; 173 if (position_string == "sticky") 174 return CSS::Position::Sticky; 175 if (position_string == "fixed") 176 return CSS::Position::Fixed; 177 } 178 return CSS::Position::Static; 179} 180 181bool StyleProperties::operator==(const StyleProperties& other) const 182{ 183 if (m_property_values.size() != other.m_property_values.size()) 184 return false; 185 186 for (auto& it : m_property_values) { 187 auto jt = other.m_property_values.find(it.key); 188 if (jt == other.m_property_values.end()) 189 return false; 190 auto& my_value = *it.value; 191 auto& other_value = *jt->value; 192 if (my_value.type() != other_value.type()) 193 return false; 194 if (my_value.to_string() != other_value.to_string()) 195 return false; 196 } 197 198 return true; 199} 200 201}