mirror of
https://github.com/LadybirdBrowser/ladybird
synced 2026-04-28 02:27:19 +02:00
This change lays the groundwork for variable font support in LibWeb. FontVariationSettings enables customization of existing font axes such as weight (wght) and width (wdth).
69 lines
1.5 KiB
C++
69 lines
1.5 KiB
C++
/*
|
|
* Copyright (c) 2025, Norbiros <me@norbiros.dev>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#include <AK/HashMap.h>
|
|
#include <AK/QuickSort.h>
|
|
#include <AK/Vector.h>
|
|
#include <LibGfx/FourCC.h>
|
|
|
|
#pragma once
|
|
|
|
namespace Gfx {
|
|
|
|
struct FontVariationAxis {
|
|
FourCC tag;
|
|
float value;
|
|
|
|
FontVariationAxis(FourCC t, float v)
|
|
: tag(t)
|
|
, value(v)
|
|
{
|
|
}
|
|
|
|
bool operator==(FontVariationAxis const& other) const
|
|
{
|
|
return tag == other.tag && value == other.value;
|
|
}
|
|
};
|
|
|
|
// FIXME: Support other named axes like 'slnt', 'ital', 'opsz', 'GRAD', etc.
|
|
struct FontVariationSettings {
|
|
HashMap<FourCC, float> axes;
|
|
|
|
FontVariationSettings() = default;
|
|
|
|
// https://learn.microsoft.com/en-us/typography/opentype/spec/dvaraxistag_wght
|
|
void set_weight(float value)
|
|
{
|
|
axes.set(FourCC("wght"), value);
|
|
}
|
|
|
|
// https://learn.microsoft.com/en-us/typography/opentype/spec/dvaraxistag_wdth
|
|
void set_width(float value)
|
|
{
|
|
axes.set(FourCC("wdth"), value);
|
|
}
|
|
|
|
bool is_empty() const { return axes.is_empty(); }
|
|
|
|
Vector<FontVariationAxis> to_sorted_list() const
|
|
{
|
|
Vector<FontVariationAxis> list;
|
|
list.ensure_capacity(axes.size());
|
|
|
|
for (auto const& entry : axes)
|
|
list.unchecked_append(FontVariationAxis(entry.key, entry.value));
|
|
|
|
quick_sort(list, [](auto const& a, auto const& b) {
|
|
return a.tag < b.tag;
|
|
});
|
|
|
|
return list;
|
|
}
|
|
};
|
|
|
|
}
|