diff --git a/Userland/Libraries/LibGfx/Font/BitmapFont.cpp b/Userland/Libraries/LibGfx/Font/BitmapFont.cpp index ebaa4f26ad..1ef9234ff6 100644 --- a/Userland/Libraries/LibGfx/Font/BitmapFont.cpp +++ b/Userland/Libraries/LibGfx/Font/BitmapFont.cpp @@ -188,9 +188,9 @@ BitmapFont::~BitmapFont() } } -ErrorOr> BitmapFont::try_load_from_mapped_file(NonnullOwnPtr mapped_file) +ErrorOr> BitmapFont::try_load_from_stream(FixedMemoryStream& stream) { - auto& header = *TRY(mapped_file->read_in_place()); + auto& header = *TRY(stream.read_in_place()); if (memcmp(header.magic, "!Fnt", 4)) return Error::from_string_literal("Gfx::BitmapFont::load_from_memory: Incompatible header"); if (header.name[sizeof(header.name) - 1] != '\0') @@ -204,23 +204,29 @@ ErrorOr> BitmapFont::try_load_from_mapped_file(Nonnull // FIXME: These ReadonlyFoo -> Foo casts are awkward, and only needed because BitmapFont is // sometimes editable and sometimes not. Splitting it into editable/non-editable classes // would make this a lot cleaner. - ReadonlyBytes readonly_range_mask = TRY(mapped_file->read_in_place(header.range_mask_size)); + ReadonlyBytes readonly_range_mask = TRY(stream.read_in_place(header.range_mask_size)); Bytes range_mask { const_cast(readonly_range_mask.data()), readonly_range_mask.size() }; for (size_t i = 0; i < header.range_mask_size; ++i) glyph_count += 256 * popcount(range_mask[i]); - ReadonlyBytes readonly_rows = TRY(mapped_file->read_in_place(glyph_count * bytes_per_glyph)); + ReadonlyBytes readonly_rows = TRY(stream.read_in_place(glyph_count * bytes_per_glyph)); Bytes rows { const_cast(readonly_rows.data()), readonly_rows.size() }; - ReadonlySpan readonly_widths = TRY(mapped_file->read_in_place(glyph_count)); + ReadonlySpan readonly_widths = TRY(stream.read_in_place(glyph_count)); Span widths { const_cast(readonly_widths.data()), readonly_widths.size() }; - if (!mapped_file->is_eof()) + if (!stream.is_eof()) return Error::from_string_literal("Gfx::BitmapFont::load_from_memory: Trailing data in file"); auto name = TRY(String::from_utf8(ReadonlyBytes { header.name, strlen(header.name) })); auto family = TRY(String::from_utf8(ReadonlyBytes { header.family, strlen(header.family) })); auto font = TRY(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))); + return font; +} + +ErrorOr> BitmapFont::try_load_from_mapped_file(NonnullOwnPtr mapped_file) +{ + auto font = TRY(try_load_from_stream(*mapped_file)); font->m_mapped_file = move(mapped_file); return font; } diff --git a/Userland/Libraries/LibGfx/Font/BitmapFont.h b/Userland/Libraries/LibGfx/Font/BitmapFont.h index 2915417d55..e3963f3882 100644 --- a/Userland/Libraries/LibGfx/Font/BitmapFont.h +++ b/Userland/Libraries/LibGfx/Font/BitmapFont.h @@ -32,6 +32,7 @@ public: static RefPtr load_from_file(DeprecatedString const& path); static ErrorOr> try_load_from_file(DeprecatedString const& path); static ErrorOr> try_load_from_mapped_file(NonnullOwnPtr); + static ErrorOr> try_load_from_stream(FixedMemoryStream&); ErrorOr write_to_file(DeprecatedString const& path); ErrorOr write_to_file(NonnullOwnPtr file);