mirror of
https://github.com/RGBCube/serenity
synced 2025-05-25 22:15:06 +00:00
LibHTML: Move font loading from LayoutText to StyleProperties
Since LayoutText inherits all of its style information from its parent Element anyway, it makes more sense to load the font at a higher level. And since the font depends only on the style and nothing else, this patch moves font loading (and caching) into StyleProperties. This could be made a lot smarter to avoid loading the same font many times, etc.
This commit is contained in:
parent
c8e5039418
commit
b9557bf876
4 changed files with 72 additions and 66 deletions
|
@ -1,4 +1,6 @@
|
|||
#include <LibCore/CDirIterator.h>
|
||||
#include <LibHTML/CSS/StyleProperties.h>
|
||||
#include <ctype.h>
|
||||
|
||||
void StyleProperties::set_property(const String& name, NonnullRefPtr<StyleValue> value)
|
||||
{
|
||||
|
@ -36,3 +38,55 @@ Color StyleProperties::color_or_fallback(const StringView& property_name, const
|
|||
return fallback;
|
||||
return value.value()->to_color(document);
|
||||
}
|
||||
|
||||
void StyleProperties::load_font() const
|
||||
{
|
||||
auto font_family = string_or_fallback("font-family", "Katica");
|
||||
auto font_weight = string_or_fallback("font-weight", "normal");
|
||||
|
||||
String weight;
|
||||
if (font_weight == "lighter")
|
||||
weight = "Thin";
|
||||
else if (font_weight == "normal")
|
||||
weight = "";
|
||||
else if (font_weight == "bold")
|
||||
weight = "Bold";
|
||||
else
|
||||
ASSERT_NOT_REACHED();
|
||||
|
||||
auto look_for_file = [](const StringView& expected_name) -> String {
|
||||
// TODO: handle font sizes properly?
|
||||
CDirIterator it { "/res/fonts/", CDirIterator::Flags::SkipDots };
|
||||
while (it.has_next()) {
|
||||
String name = it.next_path();
|
||||
ASSERT(name.ends_with(".font"));
|
||||
if (!name.starts_with(expected_name))
|
||||
continue;
|
||||
|
||||
// Check that a numeric size immediately
|
||||
// follows the font name. This prevents,
|
||||
// for example, matching KaticaBold when
|
||||
// the regular Katica is requested.
|
||||
if (!isdigit(name[expected_name.length()]))
|
||||
continue;
|
||||
|
||||
return name;
|
||||
}
|
||||
return {};
|
||||
};
|
||||
|
||||
String file_name = look_for_file(String::format("%s%s", font_family.characters(), weight.characters()));
|
||||
if (file_name.is_null() && weight == "")
|
||||
file_name = look_for_file(String::format("%sRegular", font_family.characters()));
|
||||
|
||||
if (file_name.is_null()) {
|
||||
dbg() << "Failed to find a font for family " << font_family << " weight " << font_weight;
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
|
||||
#ifdef HTML_DEBUG
|
||||
dbg() << "Found font " << file_name << " for family " << font_family << " weight " << font_weight;
|
||||
#endif
|
||||
|
||||
m_font = Font::load_from_file(String::format("/res/fonts/%s", file_name.characters()));
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue