From a3b91378df25b90cc2f7c4146dce538c7b9c3455 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Fri, 6 Oct 2023 16:41:40 +0100 Subject: [PATCH] LibGfx: Store BitmapFont rows data as Bytes --- Userland/Libraries/LibGfx/Font/BitmapFont.cpp | 27 +++++++++---------- Userland/Libraries/LibGfx/Font/BitmapFont.h | 6 ++--- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/Userland/Libraries/LibGfx/Font/BitmapFont.cpp b/Userland/Libraries/LibGfx/Font/BitmapFont.cpp index faac666638..d9befa1a67 100644 --- a/Userland/Libraries/LibGfx/Font/BitmapFont.cpp +++ b/Userland/Libraries/LibGfx/Font/BitmapFont.cpp @@ -51,10 +51,10 @@ ErrorOr> BitmapFont::try_clone() const 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); + m_rows.copy_to(new_rows); 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, new_range_mask, 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, 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, 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.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, new_range_mask, 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, 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 @@ -120,10 +120,10 @@ ErrorOr> BitmapFont::masked_character_set() const } // 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)); + return adopt_nonnull_ref_or_enomem(new (nothrow) BitmapFont(m_name, m_family, new_rows, 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, Bytes range_mask, u8 baseline, u8 mean_line, u8 presentation_size, u16 weight, u8 slope, bool owns_arrays) +BitmapFont::BitmapFont(String name, String family, Bytes 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(range_mask) @@ -142,7 +142,6 @@ 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_rows); VERIFY(m_glyph_widths); update_x_height(); @@ -174,7 +173,7 @@ BitmapFont::~BitmapFont() { if (m_owns_arrays) { free(m_glyph_widths); - free(m_rows); + free(m_rows.data()); free(m_range_mask.data()); } } @@ -195,8 +194,9 @@ ErrorOr> BitmapFont::load_from_memory(u8 const* data) 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_start + header.range_mask_size; - u8* widths = (u8*)(rows) + glyph_count * bytes_per_glyph; + u8* rows_start = range_mask_start + header.range_mask_size; + Bytes rows { rows_start, glyph_count * bytes_per_glyph }; + u8* widths = rows_start + 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, range_mask, header.baseline, header.mean_line, header.presentation_size, header.weight, header.slope)); @@ -246,10 +246,9 @@ ErrorOr BitmapFont::write_to_file(NonnullOwnPtr file) 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) })); 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_rows)); TRY(file->write_until_depleted({ m_glyph_widths, m_glyph_count })); return {}; @@ -262,7 +261,7 @@ Glyph BitmapFont::glyph(u32 code_point) const auto index = glyph_index(code_point).value_or('?'); auto width = m_glyph_widths[index]; return Glyph( - GlyphBitmap(m_rows, index * m_glyph_height, { width, m_glyph_height }), + GlyphBitmap(m_rows.data(), index * m_glyph_height, { width, m_glyph_height }), 0, width, m_glyph_height); @@ -272,7 +271,7 @@ Glyph BitmapFont::raw_glyph(u32 code_point) const { auto width = m_glyph_widths[code_point]; return Glyph( - GlyphBitmap(m_rows, code_point * m_glyph_height, { width, m_glyph_height }), + GlyphBitmap(m_rows.data(), code_point * m_glyph_height, { width, m_glyph_height }), 0, width, m_glyph_height); diff --git a/Userland/Libraries/LibGfx/Font/BitmapFont.h b/Userland/Libraries/LibGfx/Font/BitmapFont.h index 1018e97275..fe970492b1 100644 --- a/Userland/Libraries/LibGfx/Font/BitmapFont.h +++ b/Userland/Libraries/LibGfx/Font/BitmapFont.h @@ -38,7 +38,7 @@ public: ~BitmapFont(); - u8* rows() { return m_rows; } + u8* rows() { return m_rows.data(); } u8* widths() { return m_glyph_widths; } virtual float point_size() const override { return m_presentation_size; } @@ -131,7 +131,7 @@ public: virtual RefPtr with_size(float point_size) const override; private: - BitmapFont(String name, String family, u8* rows, u8* widths, bool is_fixed_width, + BitmapFont(String name, String family, Bytes 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 = false); @@ -151,7 +151,7 @@ private: Bytes m_range_mask; Vector> m_range_indices; - u8* m_rows { nullptr }; + Bytes m_rows; u8* m_glyph_widths { nullptr }; OwnPtr m_mapped_file;