mirror of
https://github.com/RGBCube/serenity
synced 2025-06-01 09:18:11 +00:00
LibGUI: Categorize font families by variant instead of weight
FontPickerWeightModel is no longer necessary as variants contain weight as part of a complete typeface description. This fixes fonts not inventorying correctly in picker when they contained more than bold and regular typefaces. Weight mapping has been moved into LibGfx/FontStyleMapping.h
This commit is contained in:
parent
84ce923850
commit
4d2f349710
4 changed files with 31 additions and 105 deletions
|
@ -8,7 +8,7 @@
|
|||
#include <LibGUI/Button.h>
|
||||
#include <LibGUI/FontPicker.h>
|
||||
#include <LibGUI/FontPickerDialogGML.h>
|
||||
#include <LibGUI/FontPickerWeightModel.h>
|
||||
#include <LibGUI/ItemListModel.h>
|
||||
#include <LibGUI/Label.h>
|
||||
#include <LibGUI/ListView.h>
|
||||
#include <LibGUI/SpinBox.h>
|
||||
|
@ -33,9 +33,9 @@ FontPicker::FontPicker(Window* parent_window, const Gfx::Font* current_font, boo
|
|||
m_family_list_view->set_model(ItemListModel<String>::create(m_families));
|
||||
m_family_list_view->horizontal_scrollbar().set_visible(false);
|
||||
|
||||
m_weight_list_view = *widget.find_descendant_of_type_named<ListView>("weight_list_view");
|
||||
m_weight_list_view->set_model(adopt_ref(*new FontWeightListModel(m_weights)));
|
||||
m_weight_list_view->horizontal_scrollbar().set_visible(false);
|
||||
m_variant_list_view = *widget.find_descendant_of_type_named<ListView>("variant_list_view");
|
||||
m_variant_list_view->set_model(ItemListModel<String>::create(m_variants));
|
||||
m_variant_list_view->horizontal_scrollbar().set_visible(false);
|
||||
|
||||
m_size_spin_box = *widget.find_descendant_of_type_named<SpinBox>("size_spin_box");
|
||||
m_size_spin_box->set_range(1, 255);
|
||||
|
@ -58,34 +58,32 @@ FontPicker::FontPicker(Window* parent_window, const Gfx::Font* current_font, boo
|
|||
m_family_list_view->on_selection_change = [this] {
|
||||
const auto& index = m_family_list_view->selection().first();
|
||||
m_family = index.data().to_string();
|
||||
m_weights.clear();
|
||||
m_variants.clear();
|
||||
Gfx::FontDatabase::the().for_each_typeface([&](auto& typeface) {
|
||||
if (m_fixed_width_only && !typeface.is_fixed_width())
|
||||
return;
|
||||
if (typeface.family() == m_family.value() && !m_weights.contains_slow(typeface.weight())) {
|
||||
m_weights.append(typeface.weight());
|
||||
}
|
||||
if (typeface.family() == m_family.value() && !m_variants.contains_slow(typeface.variant()))
|
||||
m_variants.append(typeface.variant());
|
||||
});
|
||||
quick_sort(m_weights);
|
||||
Optional<size_t> index_of_old_weight_in_new_list;
|
||||
if (m_weight.has_value())
|
||||
index_of_old_weight_in_new_list = m_weights.find_first_index(m_weight.value());
|
||||
quick_sort(m_variants);
|
||||
Optional<size_t> index_of_old_variant_in_new_list;
|
||||
if (m_variant.has_value())
|
||||
index_of_old_variant_in_new_list = m_variants.find_first_index(m_variant.value());
|
||||
|
||||
m_weight_list_view->model()->invalidate();
|
||||
m_weight_list_view->set_cursor(m_weight_list_view->model()->index(index_of_old_weight_in_new_list.value_or(0)), GUI::AbstractView::SelectionUpdate::Set);
|
||||
m_variant_list_view->model()->invalidate();
|
||||
m_variant_list_view->set_cursor(m_variant_list_view->model()->index(index_of_old_variant_in_new_list.value_or(0)), GUI::AbstractView::SelectionUpdate::Set);
|
||||
update_font();
|
||||
};
|
||||
|
||||
m_weight_list_view->on_selection_change = [this] {
|
||||
const auto& index = m_weight_list_view->selection().first();
|
||||
m_variant_list_view->on_selection_change = [this] {
|
||||
const auto& index = m_variant_list_view->selection().first();
|
||||
bool font_is_fixed_size = false;
|
||||
m_weight = index.data(ModelRole::Custom).to_i32();
|
||||
m_variant = index.data().to_string();
|
||||
m_sizes.clear();
|
||||
dbgln("Selected weight: {}", m_weight.value());
|
||||
Gfx::FontDatabase::the().for_each_typeface([&](auto& typeface) {
|
||||
if (m_fixed_width_only && !typeface.is_fixed_width())
|
||||
return;
|
||||
if (typeface.family() == m_family.value() && (int)typeface.weight() == m_weight.value()) {
|
||||
if (typeface.family() == m_family.value() && typeface.variant() == m_variant.value()) {
|
||||
font_is_fixed_size = typeface.is_fixed_size();
|
||||
if (font_is_fixed_size) {
|
||||
m_size_spin_box->set_visible(false);
|
||||
|
@ -184,27 +182,26 @@ void FontPicker::set_font(const Gfx::Font* font)
|
|||
|
||||
if (!m_font) {
|
||||
m_family = {};
|
||||
m_weight = {};
|
||||
m_variant = {};
|
||||
m_size = {};
|
||||
m_weights.clear();
|
||||
m_variants.clear();
|
||||
m_sizes.clear();
|
||||
m_weight_list_view->model()->invalidate();
|
||||
m_variant_list_view->model()->invalidate();
|
||||
m_size_list_view->model()->invalidate();
|
||||
return;
|
||||
}
|
||||
|
||||
m_family = font->family();
|
||||
m_weight = font->weight();
|
||||
m_variant = font->variant();
|
||||
m_size = font->presentation_size();
|
||||
|
||||
auto family_index = m_families.find_first_index(m_font->family());
|
||||
if (family_index.has_value())
|
||||
m_family_list_view->set_cursor(m_family_list_view->model()->index(family_index.value()), GUI::AbstractView::SelectionUpdate::Set);
|
||||
|
||||
auto weight_index = m_weights.find_first_index(m_font->weight());
|
||||
if (weight_index.has_value()) {
|
||||
m_weight_list_view->set_cursor(m_weight_list_view->model()->index(weight_index.value()), GUI::AbstractView::SelectionUpdate::Set);
|
||||
}
|
||||
auto variant_index = m_variants.find_first_index(m_font->variant());
|
||||
if (variant_index.has_value())
|
||||
m_variant_list_view->set_cursor(m_variant_list_view->model()->index(variant_index.value()), GUI::AbstractView::SelectionUpdate::Set);
|
||||
|
||||
auto size_index = m_sizes.find_first_index(m_font->presentation_size());
|
||||
if (size_index.has_value())
|
||||
|
@ -213,8 +210,8 @@ void FontPicker::set_font(const Gfx::Font* font)
|
|||
|
||||
void FontPicker::update_font()
|
||||
{
|
||||
if (m_family.has_value() && m_size.has_value() && m_weight.has_value()) {
|
||||
m_font = Gfx::FontDatabase::the().get(m_family.value(), m_size.value(), m_weight.value());
|
||||
if (m_family.has_value() && m_size.has_value() && m_variant.has_value()) {
|
||||
m_font = Gfx::FontDatabase::the().get(m_family.value(), m_variant.value(), m_size.value());
|
||||
m_sample_text_label->set_font(m_font);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,17 +31,17 @@ private:
|
|||
RefPtr<Gfx::Font> m_font;
|
||||
|
||||
RefPtr<ListView> m_family_list_view;
|
||||
RefPtr<ListView> m_weight_list_view;
|
||||
RefPtr<ListView> m_variant_list_view;
|
||||
RefPtr<ListView> m_size_list_view;
|
||||
RefPtr<SpinBox> m_size_spin_box;
|
||||
RefPtr<Label> m_sample_text_label;
|
||||
|
||||
Vector<String> m_families;
|
||||
Vector<int> m_weights;
|
||||
Vector<String> m_variants;
|
||||
Vector<int> m_sizes;
|
||||
|
||||
Optional<String> m_family;
|
||||
Optional<int> m_weight;
|
||||
Optional<String> m_variant;
|
||||
Optional<int> m_size;
|
||||
};
|
||||
|
||||
|
|
|
@ -31,13 +31,13 @@
|
|||
}
|
||||
|
||||
@GUI::Label {
|
||||
text: "Weight:"
|
||||
text: "Style:"
|
||||
text_alignment: "CenterLeft"
|
||||
fixed_height: 16
|
||||
}
|
||||
|
||||
@GUI::ListView {
|
||||
name: "weight_list_view"
|
||||
name: "variant_list_view"
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,71 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <LibGUI/ItemListModel.h>
|
||||
|
||||
namespace GUI {
|
||||
|
||||
struct FontWeightNameMapping {
|
||||
constexpr FontWeightNameMapping(int w, const char* n)
|
||||
: weight(w)
|
||||
, name(n)
|
||||
{
|
||||
}
|
||||
int weight { 0 };
|
||||
StringView name;
|
||||
};
|
||||
|
||||
static constexpr FontWeightNameMapping font_weight_names[] = {
|
||||
{ 100, "Thin" },
|
||||
{ 200, "Extra Light" },
|
||||
{ 300, "Light" },
|
||||
{ 400, "Regular" },
|
||||
{ 500, "Medium" },
|
||||
{ 600, "Semi Bold" },
|
||||
{ 700, "Bold" },
|
||||
{ 800, "Extra Bold" },
|
||||
{ 900, "Black" },
|
||||
{ 950, "Extra Black" },
|
||||
};
|
||||
|
||||
static constexpr StringView weight_to_name(int weight)
|
||||
{
|
||||
for (auto& it : font_weight_names) {
|
||||
if (it.weight == weight)
|
||||
return it.name;
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
static constexpr int name_to_weight(StringView name)
|
||||
{
|
||||
for (auto& it : font_weight_names) {
|
||||
if (it.name == name)
|
||||
return it.weight;
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
class FontWeightListModel : public ItemListModel<int> {
|
||||
public:
|
||||
FontWeightListModel(const Vector<int>& weights)
|
||||
: ItemListModel(weights)
|
||||
{
|
||||
}
|
||||
|
||||
virtual Variant data(const ModelIndex& index, ModelRole role) const override
|
||||
{
|
||||
if (role == ModelRole::Custom)
|
||||
return m_data.at(index.row());
|
||||
if (role == ModelRole::Display)
|
||||
return String(weight_to_name(m_data.at(index.row())));
|
||||
return ItemListModel::data(index, role);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue