Serenity Operating System
at portability 179 lines 6.1 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 <LibHTML/CSS/StyleProperties.h> 29#include <LibHTML/FontCache.h> 30#include <ctype.h> 31 32StyleProperties::StyleProperties() 33{ 34} 35 36StyleProperties::StyleProperties(const StyleProperties& other) 37 : m_property_values(*new HashMap(other.m_property_values)) 38{ 39 if (other.m_font) { 40 m_font = other.m_font->clone(); 41 } else { 42 m_font = nullptr; 43 } 44} 45 46NonnullRefPtr<StyleProperties> StyleProperties::clone() const 47{ 48 return adopt(*new StyleProperties(*this)); 49} 50 51void StyleProperties::set_property(CSS::PropertyID id, NonnullRefPtr<StyleValue> value) 52{ 53 m_property_values.set((unsigned)id, move(value)); 54} 55 56Optional<NonnullRefPtr<StyleValue>> StyleProperties::property(CSS::PropertyID id) const 57{ 58 auto it = m_property_values.find((unsigned)id); 59 if (it == m_property_values.end()) 60 return {}; 61 return it->value; 62} 63 64Length StyleProperties::length_or_fallback(CSS::PropertyID id, const Length& fallback) const 65{ 66 auto value = property(id); 67 if (!value.has_value()) 68 return fallback; 69 return value.value()->to_length(); 70} 71 72String StyleProperties::string_or_fallback(CSS::PropertyID id, const StringView& fallback) const 73{ 74 auto value = property(id); 75 if (!value.has_value()) 76 return fallback; 77 return value.value()->to_string(); 78} 79 80Color StyleProperties::color_or_fallback(CSS::PropertyID id, const Document& document, Color fallback) const 81{ 82 auto value = property(id); 83 if (!value.has_value()) 84 return fallback; 85 return value.value()->to_color(document); 86} 87 88void StyleProperties::load_font() const 89{ 90 auto font_family = string_or_fallback(CSS::PropertyID::FontFamily, "Katica"); 91 auto font_weight = string_or_fallback(CSS::PropertyID::FontWeight, "normal"); 92 93 if (auto cached_font = FontCache::the().get({ font_family, font_weight })) { 94 m_font = cached_font; 95 return; 96 } 97 98 String weight; 99 if (font_weight == "lighter") 100 weight = "Thin"; 101 else if (font_weight == "normal") 102 weight = ""; 103 else if (font_weight == "bold") 104 weight = "Bold"; 105 else { 106 dbg() << "Unknown font-weight: " << font_weight; 107 weight = ""; 108 } 109 110 auto look_for_file = [](const StringView& expected_name) -> String { 111 // TODO: handle font sizes properly? 112 Core::DirIterator it { "/res/fonts/", Core::DirIterator::Flags::SkipDots }; 113 while (it.has_next()) { 114 String name = it.next_path(); 115 ASSERT(name.ends_with(".font")); 116 if (!name.starts_with(expected_name)) 117 continue; 118 119 // Check that a numeric size immediately 120 // follows the font name. This prevents, 121 // for example, matching KaticaBold when 122 // the regular Katica is requested. 123 if (!isdigit(name[expected_name.length()])) 124 continue; 125 126 return name; 127 } 128 return {}; 129 }; 130 131 String file_name = look_for_file(String::format("%s%s", font_family.characters(), weight.characters())); 132 if (file_name.is_null() && weight == "") 133 file_name = look_for_file(String::format("%sRegular", font_family.characters())); 134 135 if (file_name.is_null()) { 136 dbg() << "Failed to find a font for family " << font_family << " weight " << font_weight; 137 138 if (font_weight == "bold") 139 m_font = Gfx::Font::default_bold_font(); 140 else 141 m_font = Gfx::Font::default_font(); 142 return; 143 } 144 145#ifdef HTML_DEBUG 146 dbg() << "Found font " << file_name << " for family " << font_family << " weight " << font_weight; 147#endif 148 149 m_font = Gfx::Font::load_from_file(String::format("/res/fonts/%s", file_name.characters())); 150 FontCache::the().set({ font_family, font_weight }, *m_font); 151} 152 153float StyleProperties::line_height() const 154{ 155 auto line_height_length = length_or_fallback(CSS::PropertyID::LineHeight, {}); 156 if (line_height_length.is_absolute()) 157 return (float)font().glyph_height() * line_height_length.to_px(); 158 return (float)font().glyph_height() * 1.4f; 159} 160 161bool StyleProperties::operator==(const StyleProperties& other) const 162{ 163 if (m_property_values.size() != other.m_property_values.size()) 164 return false; 165 166 for (auto& it : m_property_values) { 167 auto jt = other.m_property_values.find(it.key); 168 if (jt == other.m_property_values.end()) 169 return false; 170 auto& my_value = *it.value; 171 auto& other_value = *jt->value; 172 if (my_value.type() != other_value.type()) 173 return false; 174 if (my_value.to_string() != other_value.to_string()) 175 return false; 176 } 177 178 return true; 179}