1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 19:27:35 +00:00

LibGfx: Convert Font APIs to return String instead of DeprecatedString

This commit is contained in:
Andreas Kling 2023-09-05 15:32:52 +02:00 committed by Tim Flynn
parent 4e9dc127ae
commit 545d8336b8
29 changed files with 106 additions and 104 deletions

View file

@ -85,7 +85,7 @@ ErrorOr<NonnullRefPtr<BitmapFont>> BitmapFont::try_create(u8 glyph_height, u8 gl
auto* new_widths = static_cast<u8*>(calloc(glyph_count, 1));
if (!new_widths)
return Error::from_errno(errno);
return adopt_nonnull_ref_or_enomem(new (nothrow) BitmapFont("Untitled", "Untitled", new_rows, new_widths, fixed, glyph_width, glyph_height, 1, range_mask_size, new_range_mask, 0, 0, 0, 400, 0, true));
return adopt_nonnull_ref_or_enomem(new (nothrow) BitmapFont("Untitled"_string, "Untitled"_string, new_rows, new_widths, fixed, glyph_width, glyph_height, 1, range_mask_size, new_range_mask, 0, 0, 0, 400, 0, true));
}
ErrorOr<NonnullRefPtr<BitmapFont>> BitmapFont::unmasked_character_set() const
@ -148,7 +148,7 @@ ErrorOr<NonnullRefPtr<BitmapFont>> BitmapFont::masked_character_set() const
return adopt_nonnull_ref_or_enomem(new (nothrow) BitmapFont(m_name, m_family, new_rows, new_widths, m_fixed_width, m_glyph_width, m_glyph_height, m_glyph_spacing, new_range_mask_size, new_range_mask, m_baseline, m_mean_line, m_presentation_size, m_weight, m_slope, true));
}
BitmapFont::BitmapFont(DeprecatedString name, DeprecatedString family, u8* rows, u8* widths, bool is_fixed_width, u8 glyph_width, u8 glyph_height, u8 glyph_spacing, u16 range_mask_size, u8* range_mask, u8 baseline, u8 mean_line, u8 presentation_size, u16 weight, u8 slope, bool owns_arrays)
BitmapFont::BitmapFont(String name, String family, u8* rows, u8* widths, bool is_fixed_width, u8 glyph_width, u8 glyph_height, u8 glyph_spacing, u16 range_mask_size, u8* range_mask, u8 baseline, u8 mean_line, u8 presentation_size, u16 weight, u8 slope, bool owns_arrays)
: m_name(move(name))
, m_family(move(family))
, m_range_mask_size(range_mask_size)
@ -223,7 +223,9 @@ ErrorOr<NonnullRefPtr<BitmapFont>> BitmapFont::load_from_memory(u8 const* data)
glyph_count += 256 * popcount(range_mask[i]);
u8* rows = range_mask + header.range_mask_size;
u8* widths = (u8*)(rows) + glyph_count * bytes_per_glyph;
return adopt_nonnull_ref_or_enomem(new (nothrow) BitmapFont(DeprecatedString(header.name), DeprecatedString(header.family), rows, widths, !header.is_variable_width, header.glyph_width, header.glyph_height, header.glyph_spacing, header.range_mask_size, range_mask, header.baseline, header.mean_line, header.presentation_size, header.weight, header.slope));
auto name = TRY(String::from_utf8(ReadonlyBytes { header.name, strlen(header.name) }));
auto family = TRY(String::from_utf8(ReadonlyBytes { header.family, strlen(header.family) }));
return adopt_nonnull_ref_or_enomem(new (nothrow) BitmapFont(move(name), move(family), rows, widths, !header.is_variable_width, header.glyph_width, header.glyph_height, header.glyph_spacing, header.range_mask_size, range_mask, header.baseline, header.mean_line, header.presentation_size, header.weight, header.slope));
}
RefPtr<BitmapFont> BitmapFont::load_from_file(DeprecatedString const& path)
@ -267,8 +269,8 @@ ErrorOr<void> BitmapFont::write_to_file(NonnullOwnPtr<Core::File> file)
header.presentation_size = m_presentation_size;
header.weight = m_weight;
header.slope = m_slope;
memcpy(header.name, m_name.characters(), min(m_name.length(), sizeof(header.name) - 1));
memcpy(header.family, m_family.characters(), min(m_family.length(), sizeof(header.family) - 1));
memcpy(header.name, m_name.bytes().data(), min(m_name.bytes().size(), sizeof(header.name) - 1));
memcpy(header.family, m_family.bytes().data(), min(m_family.bytes().size(), sizeof(header.family) - 1));
size_t bytes_per_glyph = sizeof(u32) * m_glyph_height;
TRY(file->write_until_depleted({ &header, sizeof(header) }));
@ -386,12 +388,12 @@ ALWAYS_INLINE int BitmapFont::unicode_view_width(T const& view) const
return longest_width;
}
DeprecatedString BitmapFont::qualified_name() const
String BitmapFont::qualified_name() const
{
return DeprecatedString::formatted("{} {} {} {}", family(), presentation_size(), weight(), slope());
return MUST(String::formatted("{} {} {} {}", family(), presentation_size(), weight(), slope()));
}
DeprecatedString BitmapFont::variant() const
String BitmapFont::variant() const
{
StringBuilder builder;
builder.append(weight_to_name(weight()));
@ -402,19 +404,19 @@ DeprecatedString BitmapFont::variant() const
builder.append(' ');
builder.append(slope_to_name(slope()));
}
return builder.to_deprecated_string();
return MUST(builder.to_string());
}
RefPtr<Font> BitmapFont::with_size(float point_size) const
{
return Gfx::FontDatabase::the().get(family(), point_size, weight(), width(), slope());
return Gfx::FontDatabase::the().get(family().to_deprecated_string(), point_size, weight(), width(), slope());
}
Font const& Font::bold_variant() const
{
if (m_bold_variant)
return *m_bold_variant;
m_bold_variant = Gfx::FontDatabase::the().get(family(), presentation_size(), 700, Gfx::FontWidth::Normal, 0);
m_bold_variant = Gfx::FontDatabase::the().get(family().to_deprecated_string(), presentation_size(), 700, Gfx::FontWidth::Normal, 0);
if (!m_bold_variant)
m_bold_variant = this;
return *m_bold_variant;

View file

@ -102,8 +102,8 @@ public:
virtual int width_rounded_up(StringView) const override;
DeprecatedString name() const override { return m_name; }
void set_name(DeprecatedString name) { m_name = move(name); }
virtual String name() const override { return m_name; }
void set_name(String name) { m_name = move(name); }
bool is_fixed_width() const override { return m_fixed_width; }
void set_fixed_width(bool b) { m_fixed_width = b; }
@ -123,17 +123,17 @@ public:
u16 range_size() const { return m_range_mask_size; }
bool is_range_empty(u32 code_point) const { return !(m_range_mask[code_point / 256 / 8] & 1 << (code_point / 256 % 8)); }
DeprecatedString family() const override { return m_family; }
void set_family(DeprecatedString family) { m_family = move(family); }
DeprecatedString variant() const override;
virtual String family() const override { return m_family; }
void set_family(String family) { m_family = move(family); }
virtual String variant() const override;
DeprecatedString qualified_name() const override;
DeprecatedString human_readable_name() const override { return DeprecatedString::formatted("{} {} {}", family(), variant(), presentation_size()); }
virtual String qualified_name() const override;
virtual String human_readable_name() const override { return MUST(String::formatted("{} {} {}", family(), variant(), presentation_size())); }
virtual RefPtr<Font> with_size(float point_size) const override;
private:
BitmapFont(DeprecatedString name, DeprecatedString family, u8* rows, u8* widths, bool is_fixed_width,
BitmapFont(String name, String family, u8* rows, u8* widths, bool is_fixed_width,
u8 glyph_width, u8 glyph_height, u8 glyph_spacing, u16 range_mask_size, u8* range_mask,
u8 baseline, u8 mean_line, u8 presentation_size, u16 weight, u8 slope, bool owns_arrays = false);
@ -146,8 +146,8 @@ private:
virtual bool has_color_bitmaps() const override { return false; }
DeprecatedString m_name;
DeprecatedString m_family;
String m_name;
String m_family;
size_t m_glyph_count { 0 };
u16 m_range_mask_size { 0 };

View file

@ -9,9 +9,9 @@
#include <AK/Bitmap.h>
#include <AK/ByteReader.h>
#include <AK/DeprecatedString.h>
#include <AK/RefCounted.h>
#include <AK/RefPtr.h>
#include <AK/String.h>
#include <AK/Types.h>
#include <LibCore/MappedFile.h>
#include <LibGfx/Bitmap.h>
@ -198,7 +198,7 @@ public:
virtual int width_rounded_up(StringView) const = 0;
virtual DeprecatedString name() const = 0;
virtual String name() const = 0;
virtual bool is_fixed_width() const = 0;
@ -206,11 +206,11 @@ public:
virtual size_t glyph_count() const = 0;
virtual DeprecatedString family() const = 0;
virtual DeprecatedString variant() const = 0;
virtual String family() const = 0;
virtual String variant() const = 0;
virtual DeprecatedString qualified_name() const = 0;
virtual DeprecatedString human_readable_name() const = 0;
virtual String qualified_name() const = 0;
virtual String human_readable_name() const = 0;
virtual RefPtr<Font> with_size(float point_size) const = 0;

View file

@ -143,21 +143,21 @@ void FontDatabase::load_all_fonts_from_path(DeprecatedString const& root)
if (path.ends_with(".font"sv)) {
if (auto font_or_error = Gfx::BitmapFont::try_load_from_file(path); !font_or_error.is_error()) {
auto font = font_or_error.release_value();
m_private->full_name_to_font_map.set(font->qualified_name(), *font);
auto typeface = get_or_create_typeface(font->family(), font->variant());
m_private->full_name_to_font_map.set(font->qualified_name().to_deprecated_string(), *font);
auto typeface = get_or_create_typeface(font->family().to_deprecated_string(), font->variant().to_deprecated_string());
typeface->add_bitmap_font(font);
}
} else if (path.ends_with(".ttf"sv)) {
// FIXME: What about .otf
if (auto font_or_error = OpenType::Font::try_load_from_file(path); !font_or_error.is_error()) {
auto font = font_or_error.release_value();
auto typeface = get_or_create_typeface(font->family(), font->variant());
auto typeface = get_or_create_typeface(font->family().to_deprecated_string(), font->variant().to_deprecated_string());
typeface->set_vector_font(move(font));
}
} else if (path.ends_with(".woff"sv)) {
if (auto font_or_error = WOFF::Font::try_load_from_file(path); !font_or_error.is_error()) {
auto font = font_or_error.release_value();
auto typeface = get_or_create_typeface(font->family(), font->variant());
auto typeface = get_or_create_typeface(font->family().to_deprecated_string(), font->variant().to_deprecated_string());
typeface->set_vector_font(move(font));
}
}

View file

@ -292,7 +292,7 @@ Optional<i16> Kern::read_glyph_kerning_format0(ReadonlyBytes slice, u16 left_gly
return pair->value;
}
DeprecatedString Name::string_for_id(NameId id) const
String Name::string_for_id(NameId id) const
{
auto const count = header().count;
auto const storage_offset = header().storage_offset;
@ -306,7 +306,7 @@ DeprecatedString Name::string_for_id(NameId id) const
}
if (valid_ids.is_empty())
return DeprecatedString::empty();
return String {};
auto it = valid_ids.find_if([this](auto const& i) {
// check if font has naming table for en-US language id
@ -326,10 +326,10 @@ DeprecatedString Name::string_for_id(NameId id) const
if (platform_id == to_underlying(Platform::Windows)) {
static auto& decoder = *TextCodec::decoder_for("utf-16be"sv);
return decoder.to_utf8(StringView { (char const*)m_slice.offset_pointer(storage_offset + offset), length }).release_value_but_fixme_should_propagate_errors().to_deprecated_string();
return decoder.to_utf8(StringView { (char const*)m_slice.offset_pointer(storage_offset + offset), length }).release_value_but_fixme_should_propagate_errors();
}
return DeprecatedString((char const*)m_slice.offset_pointer(storage_offset + offset), length);
return String::from_utf8(m_slice.slice(storage_offset + offset, length)).release_value_but_fixme_should_propagate_errors();
}
GlyphHorizontalMetrics Hmtx::get_glyph_horizontal_metrics(u32 glyph_id) const
@ -763,7 +763,7 @@ u16 Font::units_per_em() const
return m_head.units_per_em();
}
DeprecatedString Font::family() const
String Font::family() const
{
auto string = m_name.typographic_family_name();
if (!string.is_empty())
@ -771,7 +771,7 @@ DeprecatedString Font::family() const
return m_name.family_name();
}
DeprecatedString Font::variant() const
String Font::variant() const
{
auto string = m_name.typographic_subfamily_name();
if (!string.is_empty())

View file

@ -34,8 +34,8 @@ public:
virtual u32 glyph_count() const override;
virtual u16 units_per_em() const override;
virtual u32 glyph_id_for_code_point(u32 code_point) const override;
virtual DeprecatedString family() const override;
virtual DeprecatedString variant() const override;
virtual String family() const override;
virtual String variant() const override;
virtual u16 weight() const override;
virtual u16 width() const override;
virtual u8 slope() const override;

View file

@ -326,10 +326,10 @@ public:
};
static Optional<Name> from_slice(ReadonlyBytes);
DeprecatedString family_name() const { return string_for_id(NameId::FamilyName); }
DeprecatedString subfamily_name() const { return string_for_id(NameId::SubfamilyName); }
DeprecatedString typographic_family_name() const { return string_for_id(NameId::TypographicFamilyName); }
DeprecatedString typographic_subfamily_name() const { return string_for_id(NameId::TypographicSubfamilyName); }
String family_name() const { return string_for_id(NameId::FamilyName); }
String subfamily_name() const { return string_for_id(NameId::SubfamilyName); }
String typographic_family_name() const { return string_for_id(NameId::TypographicFamilyName); }
String typographic_subfamily_name() const { return string_for_id(NameId::TypographicSubfamilyName); }
private:
// https://learn.microsoft.com/en-us/typography/opentype/spec/name#name-records
@ -373,7 +373,7 @@ private:
{
}
DeprecatedString string_for_id(NameId) const;
[[nodiscard]] String string_for_id(NameId) const;
ReadonlyBytes m_slice;
};

View file

@ -62,14 +62,14 @@ public:
virtual float width(Utf8View const&) const override;
virtual float width(Utf32View const&) const override;
virtual int width_rounded_up(StringView) const override;
virtual DeprecatedString name() const override { return DeprecatedString::formatted("{} {}", family(), variant()); }
virtual String name() const override { return MUST(String::formatted("{} {}", family(), variant())); }
virtual bool is_fixed_width() const override { return m_font->is_fixed_width(); }
virtual u8 glyph_spacing() const override { return 0; }
virtual size_t glyph_count() const override { return m_font->glyph_count(); }
virtual DeprecatedString family() const override { return m_font->family(); }
virtual DeprecatedString variant() const override { return m_font->variant(); }
virtual DeprecatedString qualified_name() const override { return DeprecatedString::formatted("{} {} {} {}", family(), presentation_size(), weight(), slope()); }
virtual DeprecatedString human_readable_name() const override { return DeprecatedString::formatted("{} {} {}", family(), variant(), presentation_size()); }
virtual String family() const override { return m_font->family(); }
virtual String variant() const override { return m_font->variant(); }
virtual String qualified_name() const override { return MUST(String::formatted("{} {} {} {}", family(), presentation_size(), weight(), slope())); }
virtual String human_readable_name() const override { return MUST(String::formatted("{} {} {}", family(), variant(), presentation_size())); }
virtual RefPtr<Font> with_size(float point_size) const override;

View file

@ -42,8 +42,8 @@ public:
virtual u32 glyph_count() const = 0;
virtual u16 units_per_em() const = 0;
virtual u32 glyph_id_for_code_point(u32 code_point) const = 0;
virtual DeprecatedString family() const = 0;
virtual DeprecatedString variant() const = 0;
virtual String family() const = 0;
virtual String variant() const = 0;
virtual u16 weight() const = 0;
virtual u16 width() const = 0;
virtual u8 slope() const = 0;

View file

@ -31,8 +31,8 @@ public:
virtual u32 glyph_count() const override { return m_input_font->glyph_count(); }
virtual u16 units_per_em() const override { return m_input_font->units_per_em(); }
virtual u32 glyph_id_for_code_point(u32 code_point) const override { return m_input_font->glyph_id_for_code_point(code_point); }
virtual DeprecatedString family() const override { return m_input_font->family(); }
virtual DeprecatedString variant() const override { return m_input_font->variant(); }
virtual String family() const override { return m_input_font->family(); }
virtual String variant() const override { return m_input_font->variant(); }
virtual u16 weight() const override { return m_input_font->weight(); }
virtual u16 width() const override { return m_input_font->width(); }
virtual u8 slope() const override { return m_input_font->slope(); }

View file

@ -35,8 +35,8 @@ public:
virtual u32 glyph_count() const override { return m_input_font->glyph_count(); }
virtual u16 units_per_em() const override { return m_input_font->units_per_em(); }
virtual u32 glyph_id_for_code_point(u32 code_point) const override { return m_input_font->glyph_id_for_code_point(code_point); }
virtual DeprecatedString family() const override { return m_input_font->family(); }
virtual DeprecatedString variant() const override { return m_input_font->variant(); }
virtual String family() const override { return m_input_font->family(); }
virtual String variant() const override { return m_input_font->variant(); }
virtual u16 weight() const override { return m_input_font->weight(); }
virtual u16 width() const override { return m_input_font->width(); }
virtual u8 slope() const override { return m_input_font->slope(); }