From 415a024bc8f060395dc5091d0cc7c151a046d71d Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Mon, 2 Oct 2023 15:45:17 +0100 Subject: [PATCH] LibGfx: Store BitmapFont range mask as Bytes instead of pointer and size --- Userland/Libraries/LibGfx/Font/BitmapFont.cpp | 33 ++++++++++--------- Userland/Libraries/LibGfx/Font/BitmapFont.h | 6 ++-- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/Userland/Libraries/LibGfx/Font/BitmapFont.cpp b/Userland/Libraries/LibGfx/Font/BitmapFont.cpp index f3b1c465c62..faac666638b 100644 --- a/Userland/Libraries/LibGfx/Font/BitmapFont.cpp +++ b/Userland/Libraries/LibGfx/Font/BitmapFont.cpp @@ -47,14 +47,14 @@ NonnullRefPtr BitmapFont::clone() const ErrorOr> BitmapFont::try_clone() const { - auto new_range_mask = TRY(Core::System::allocate(m_range_mask_size, 1)); - memcpy(new_range_mask.data(), m_range_mask, m_range_mask_size); + auto new_range_mask = TRY(Core::System::allocate(m_range_mask.size(), 1)); + m_range_mask.copy_to(new_range_mask); size_t bytes_per_glyph = sizeof(u32) * glyph_height(); auto new_rows = TRY(Core::System::allocate(m_glyph_count, bytes_per_glyph)); memcpy(new_rows.data(), m_rows, bytes_per_glyph * m_glyph_count); auto new_widths = TRY(Core::System::allocate(m_glyph_count, 1)); memcpy(new_widths.data(), m_glyph_widths, m_glyph_count); - return TRY(adopt_nonnull_ref_or_enomem(new (nothrow) BitmapFont(m_name, m_family, new_rows.data(), new_widths.data(), m_fixed_width, m_glyph_width, m_glyph_height, m_glyph_spacing, m_range_mask_size, new_range_mask.data(), m_baseline, m_mean_line, m_presentation_size, m_weight, m_slope, true))); + return TRY(adopt_nonnull_ref_or_enomem(new (nothrow) BitmapFont(m_name, m_family, new_rows.data(), new_widths.data(), m_fixed_width, m_glyph_width, m_glyph_height, m_glyph_spacing, new_range_mask, m_baseline, m_mean_line, m_presentation_size, m_weight, m_slope, true))); } ErrorOr> BitmapFont::create(u8 glyph_height, u8 glyph_width, bool fixed, size_t glyph_count) @@ -70,7 +70,7 @@ ErrorOr> BitmapFont::create(u8 glyph_height, u8 glyph_ size_t bytes_per_glyph = sizeof(u32) * glyph_height; auto new_rows = TRY(Core::System::allocate(glyph_count, bytes_per_glyph)); auto new_widths = TRY(Core::System::allocate(glyph_count, 1)); - return adopt_nonnull_ref_or_enomem(new (nothrow) BitmapFont("Untitled"_string, "Untitled"_string, new_rows.data(), new_widths.data(), fixed, glyph_width, glyph_height, 1, range_mask_size, new_range_mask.data(), 0, 0, 0, 400, 0, true)); + return adopt_nonnull_ref_or_enomem(new (nothrow) BitmapFont("Untitled"_string, "Untitled"_string, new_rows.data(), new_widths.data(), fixed, glyph_width, glyph_height, 1, new_range_mask, 0, 0, 0, 400, 0, true)); } ErrorOr> BitmapFont::unmasked_character_set() const @@ -88,7 +88,7 @@ ErrorOr> BitmapFont::unmasked_character_set() const memcpy(&new_rows[code_point * bytes_per_glyph], &m_rows[index.value() * bytes_per_glyph], bytes_per_glyph); } } - return adopt_nonnull_ref_or_enomem(new (nothrow) BitmapFont(m_name, m_family, new_rows.data(), new_widths.data(), m_fixed_width, m_glyph_width, m_glyph_height, m_glyph_spacing, s_max_range_mask_size, new_range_mask.data(), m_baseline, m_mean_line, m_presentation_size, m_weight, m_slope, true)); + return adopt_nonnull_ref_or_enomem(new (nothrow) BitmapFont(m_name, m_family, new_rows.data(), new_widths.data(), m_fixed_width, m_glyph_width, m_glyph_height, m_glyph_spacing, new_range_mask, m_baseline, m_mean_line, m_presentation_size, m_weight, m_slope, true)); } ErrorOr> BitmapFont::masked_character_set() const @@ -118,13 +118,14 @@ ErrorOr> BitmapFont::masked_character_set() const memcpy(&new_widths[i - j * 256], &m_glyph_widths[i], 1); memcpy(&new_rows[(i - j * 256) * bytes_per_glyph], &m_rows[i * bytes_per_glyph], bytes_per_glyph); } - return adopt_nonnull_ref_or_enomem(new (nothrow) BitmapFont(m_name, m_family, new_rows.data(), new_widths.data(), m_fixed_width, m_glyph_width, m_glyph_height, m_glyph_spacing, new_range_mask_size, new_range_mask.data(), m_baseline, m_mean_line, m_presentation_size, m_weight, m_slope, true)); + // Now that we're done working with the range-mask memory, reduce its reported size down to what it should be. + new_range_mask = { new_range_mask.data(), new_range_mask_size }; + return adopt_nonnull_ref_or_enomem(new (nothrow) BitmapFont(m_name, m_family, new_rows.data(), new_widths.data(), m_fixed_width, m_glyph_width, m_glyph_height, m_glyph_spacing, new_range_mask, m_baseline, m_mean_line, m_presentation_size, m_weight, m_slope, true)); } -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) +BitmapFont::BitmapFont(String name, String family, u8* rows, u8* widths, bool is_fixed_width, u8 glyph_width, u8 glyph_height, u8 glyph_spacing, Bytes 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) , m_range_mask(range_mask) , m_rows(rows) , m_glyph_widths(widths) @@ -141,13 +142,12 @@ BitmapFont::BitmapFont(String name, String family, u8* rows, u8* widths, bool is , m_fixed_width(is_fixed_width) , m_owns_arrays(owns_arrays) { - VERIFY(m_range_mask); VERIFY(m_rows); VERIFY(m_glyph_widths); update_x_height(); - for (size_t i = 0, index = 0; i < m_range_mask_size; ++i) { + for (size_t i = 0, index = 0; i < m_range_mask.size(); ++i) { for (size_t j = 0; j < 8; ++j) { if (m_range_mask[i] & (1 << j)) { m_glyph_count += 256; @@ -175,7 +175,7 @@ BitmapFont::~BitmapFont() if (m_owns_arrays) { free(m_glyph_widths); free(m_rows); - free(m_range_mask); + free(m_range_mask.data()); } } @@ -191,14 +191,15 @@ ErrorOr> BitmapFont::load_from_memory(u8 const* data) size_t bytes_per_glyph = sizeof(u32) * header.glyph_height; size_t glyph_count { 0 }; - u8* range_mask = const_cast(data + sizeof(FontFileHeader)); + u8* range_mask_start = const_cast(data + sizeof(FontFileHeader)); + Bytes range_mask { range_mask_start, header.range_mask_size }; for (size_t i = 0; i < header.range_mask_size; ++i) glyph_count += 256 * popcount(range_mask[i]); - u8* rows = range_mask + header.range_mask_size; + u8* rows = range_mask_start + header.range_mask_size; u8* widths = (u8*)(rows) + glyph_count * bytes_per_glyph; 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)); + 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, range_mask, header.baseline, header.mean_line, header.presentation_size, header.weight, header.slope)); } RefPtr BitmapFont::load_from_file(DeprecatedString const& path) @@ -234,7 +235,7 @@ ErrorOr BitmapFont::write_to_file(NonnullOwnPtr file) memcpy(header.magic, "!Fnt", 4); header.glyph_width = m_glyph_width; header.glyph_height = m_glyph_height; - header.range_mask_size = m_range_mask_size; + header.range_mask_size = m_range_mask.size(); header.baseline = m_baseline; header.mean_line = m_mean_line; header.is_variable_width = !m_fixed_width; @@ -247,7 +248,7 @@ ErrorOr BitmapFont::write_to_file(NonnullOwnPtr file) size_t bytes_per_glyph = sizeof(u32) * m_glyph_height; TRY(file->write_until_depleted({ &header, sizeof(header) })); - TRY(file->write_until_depleted({ m_range_mask, m_range_mask_size })); + TRY(file->write_until_depleted(m_range_mask)); TRY(file->write_until_depleted({ m_rows, m_glyph_count * bytes_per_glyph })); TRY(file->write_until_depleted({ m_glyph_widths, m_glyph_count })); diff --git a/Userland/Libraries/LibGfx/Font/BitmapFont.h b/Userland/Libraries/LibGfx/Font/BitmapFont.h index 438843a4dcb..1018e972759 100644 --- a/Userland/Libraries/LibGfx/Font/BitmapFont.h +++ b/Userland/Libraries/LibGfx/Font/BitmapFont.h @@ -119,7 +119,6 @@ public: size_t glyph_count() const override { return m_glyph_count; } Optional glyph_index(u32 code_point) const; - 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)); } virtual String family() const override { return m_family; } @@ -133,7 +132,7 @@ public: private: 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 glyph_width, u8 glyph_height, u8 glyph_spacing, Bytes range_mask, u8 baseline, u8 mean_line, u8 presentation_size, u16 weight, u8 slope, bool owns_arrays = false); static ErrorOr> load_from_memory(u8 const*); @@ -149,8 +148,7 @@ private: String m_family; size_t m_glyph_count { 0 }; - u16 m_range_mask_size { 0 }; - u8* m_range_mask { nullptr }; + Bytes m_range_mask; Vector> m_range_indices; u8* m_rows { nullptr };