mirror of
https://github.com/RGBCube/serenity
synced 2025-05-14 08:44:58 +00:00
LibGfx+Utilities: Add helpers to load vector fonts from Core::Resources
This commit is contained in:
parent
286dc6df7f
commit
86ce502ae2
8 changed files with 19 additions and 21 deletions
|
@ -118,9 +118,7 @@ void FontDatabase::load_all_fonts_from_uri(StringView uri)
|
||||||
auto root = root_or_error.release_value();
|
auto root = root_or_error.release_value();
|
||||||
|
|
||||||
root->for_each_descendant_file([this](Core::Resource const& resource) -> IterationDecision {
|
root->for_each_descendant_file([this](Core::Resource const& resource) -> IterationDecision {
|
||||||
// FIXME: Use Resources and their bytes/streams throughout so we don't have to use the path here
|
auto path = LexicalPath(resource.uri().bytes_as_string_view());
|
||||||
auto path_string = resource.filesystem_path().release_value();
|
|
||||||
auto path = LexicalPath(path_string.bytes_as_string_view());
|
|
||||||
if (path.has_extension(".font"sv)) {
|
if (path.has_extension(".font"sv)) {
|
||||||
if (auto font_or_error = Gfx::BitmapFont::try_load_from_resource(resource); !font_or_error.is_error()) {
|
if (auto font_or_error = Gfx::BitmapFont::try_load_from_resource(resource); !font_or_error.is_error()) {
|
||||||
auto font = font_or_error.release_value();
|
auto font = font_or_error.release_value();
|
||||||
|
@ -130,13 +128,13 @@ void FontDatabase::load_all_fonts_from_uri(StringView uri)
|
||||||
}
|
}
|
||||||
} else if (path.has_extension(".ttf"sv)) {
|
} else if (path.has_extension(".ttf"sv)) {
|
||||||
// FIXME: What about .otf
|
// FIXME: What about .otf
|
||||||
if (auto font_or_error = OpenType::Font::try_load_from_file(path.string()); !font_or_error.is_error()) {
|
if (auto font_or_error = OpenType::Font::try_load_from_resource(resource); !font_or_error.is_error()) {
|
||||||
auto font = font_or_error.release_value();
|
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(), font->variant());
|
||||||
typeface->set_vector_font(move(font));
|
typeface->set_vector_font(move(font));
|
||||||
}
|
}
|
||||||
} else if (path.has_extension(".woff"sv)) {
|
} else if (path.has_extension(".woff"sv)) {
|
||||||
if (auto font_or_error = WOFF::Font::try_load_from_file(path.string()); !font_or_error.is_error()) {
|
if (auto font_or_error = WOFF::Font::try_load_from_resource(resource); !font_or_error.is_error()) {
|
||||||
auto font = font_or_error.release_value();
|
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(), font->variant());
|
||||||
typeface->set_vector_font(move(font));
|
typeface->set_vector_font(move(font));
|
||||||
|
|
|
@ -13,6 +13,7 @@
|
||||||
#include <AK/MemoryStream.h>
|
#include <AK/MemoryStream.h>
|
||||||
#include <AK/Try.h>
|
#include <AK/Try.h>
|
||||||
#include <LibCore/MappedFile.h>
|
#include <LibCore/MappedFile.h>
|
||||||
|
#include <LibCore/Resource.h>
|
||||||
#include <LibGfx/Font/OpenType/Cmap.h>
|
#include <LibGfx/Font/OpenType/Cmap.h>
|
||||||
#include <LibGfx/Font/OpenType/Font.h>
|
#include <LibGfx/Font/OpenType/Font.h>
|
||||||
#include <LibGfx/Font/OpenType/Glyf.h>
|
#include <LibGfx/Font/OpenType/Glyf.h>
|
||||||
|
@ -350,11 +351,10 @@ GlyphHorizontalMetrics Hmtx::get_glyph_horizontal_metrics(u32 glyph_id) const
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
ErrorOr<NonnullRefPtr<Font>> Font::try_load_from_file(DeprecatedString path, unsigned index)
|
ErrorOr<NonnullRefPtr<Font>> Font::try_load_from_resource(Core::Resource const& resource, unsigned index)
|
||||||
{
|
{
|
||||||
auto file = TRY(Core::MappedFile::map(path));
|
auto font = TRY(try_load_from_externally_owned_memory(resource.data(), index));
|
||||||
auto font = TRY(try_load_from_externally_owned_memory(file->bytes(), index));
|
font->m_resource = resource;
|
||||||
font->m_mapped_file = move(file);
|
|
||||||
return font;
|
return font;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -24,7 +24,7 @@ class Font : public Gfx::VectorFont {
|
||||||
AK_MAKE_NONCOPYABLE(Font);
|
AK_MAKE_NONCOPYABLE(Font);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static ErrorOr<NonnullRefPtr<Font>> try_load_from_file(DeprecatedString path, unsigned index = 0);
|
static ErrorOr<NonnullRefPtr<Font>> try_load_from_resource(Core::Resource const&, unsigned index = 0);
|
||||||
static ErrorOr<NonnullRefPtr<Font>> try_load_from_externally_owned_memory(ReadonlyBytes bytes, unsigned index = 0);
|
static ErrorOr<NonnullRefPtr<Font>> try_load_from_externally_owned_memory(ReadonlyBytes bytes, unsigned index = 0);
|
||||||
|
|
||||||
virtual Gfx::ScaledFontMetrics metrics(float x_scale, float y_scale) const override;
|
virtual Gfx::ScaledFontMetrics metrics(float x_scale, float y_scale) const override;
|
||||||
|
@ -107,7 +107,7 @@ private:
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
OwnPtr<Core::MappedFile> m_mapped_file;
|
RefPtr<Core::Resource> m_resource;
|
||||||
|
|
||||||
ReadonlyBytes m_buffer;
|
ReadonlyBytes m_buffer;
|
||||||
|
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
#include <AK/IntegralMath.h>
|
#include <AK/IntegralMath.h>
|
||||||
#include <AK/MemoryStream.h>
|
#include <AK/MemoryStream.h>
|
||||||
#include <LibCompress/Zlib.h>
|
#include <LibCompress/Zlib.h>
|
||||||
|
#include <LibCore/Resource.h>
|
||||||
#include <LibGfx/Font/OpenType/Font.h>
|
#include <LibGfx/Font/OpenType/Font.h>
|
||||||
#include <LibGfx/Font/WOFF/Font.h>
|
#include <LibGfx/Font/WOFF/Font.h>
|
||||||
|
|
||||||
|
@ -68,10 +69,9 @@ static u16 pow_2_less_than_or_equal(u16 x)
|
||||||
return 1 << (sizeof(u16) * 8 - count_leading_zeroes_safe<u16>(x - 1));
|
return 1 << (sizeof(u16) * 8 - count_leading_zeroes_safe<u16>(x - 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
ErrorOr<NonnullRefPtr<Font>> Font::try_load_from_file(DeprecatedString path, unsigned int index)
|
ErrorOr<NonnullRefPtr<Font>> Font::try_load_from_resource(Core::Resource const& resource, unsigned index)
|
||||||
{
|
{
|
||||||
auto file = TRY(Core::MappedFile::map(path));
|
return try_load_from_externally_owned_memory(resource.data(), index);
|
||||||
return try_load_from_externally_owned_memory(file->bytes(), index);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ErrorOr<NonnullRefPtr<Font>> Font::try_load_from_externally_owned_memory(ReadonlyBytes buffer, unsigned int index)
|
ErrorOr<NonnullRefPtr<Font>> Font::try_load_from_externally_owned_memory(ReadonlyBytes buffer, unsigned int index)
|
||||||
|
|
|
@ -21,7 +21,7 @@ class Font : public Gfx::VectorFont {
|
||||||
AK_MAKE_NONCOPYABLE(Font);
|
AK_MAKE_NONCOPYABLE(Font);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static ErrorOr<NonnullRefPtr<Font>> try_load_from_file(DeprecatedString path, unsigned index = 0);
|
static ErrorOr<NonnullRefPtr<Font>> try_load_from_resource(Core::Resource const&, unsigned index = 0);
|
||||||
static ErrorOr<NonnullRefPtr<Font>> try_load_from_externally_owned_memory(ReadonlyBytes bytes, unsigned index = 0);
|
static ErrorOr<NonnullRefPtr<Font>> try_load_from_externally_owned_memory(ReadonlyBytes bytes, unsigned index = 0);
|
||||||
|
|
||||||
virtual Gfx::ScaledFontMetrics metrics(float x_scale, float y_scale) const override { return m_input_font->metrics(x_scale, y_scale); }
|
virtual Gfx::ScaledFontMetrics metrics(float x_scale, float y_scale) const override { return m_input_font->metrics(x_scale, y_scale); }
|
||||||
|
|
|
@ -9,7 +9,7 @@
|
||||||
#include <AK/BitStream.h>
|
#include <AK/BitStream.h>
|
||||||
#include <AK/MemoryStream.h>
|
#include <AK/MemoryStream.h>
|
||||||
#include <LibCompress/Brotli.h>
|
#include <LibCompress/Brotli.h>
|
||||||
#include <LibCore/File.h>
|
#include <LibCore/Resource.h>
|
||||||
#include <LibGfx/Font/Font.h>
|
#include <LibGfx/Font/Font.h>
|
||||||
#include <LibGfx/Font/OpenType/Font.h>
|
#include <LibGfx/Font/OpenType/Font.h>
|
||||||
#include <LibGfx/Font/WOFF2/Font.h>
|
#include <LibGfx/Font/WOFF2/Font.h>
|
||||||
|
@ -831,10 +831,9 @@ static ErrorOr<GlyfAndLocaTableBuffers> create_glyf_and_loca_tables_from_transfo
|
||||||
return GlyfAndLocaTableBuffers { .glyf_table = move(reconstructed_glyf_table), .loca_table = move(loca_table_buffer) };
|
return GlyfAndLocaTableBuffers { .glyf_table = move(reconstructed_glyf_table), .loca_table = move(loca_table_buffer) };
|
||||||
}
|
}
|
||||||
|
|
||||||
ErrorOr<NonnullRefPtr<Font>> Font::try_load_from_file(StringView path)
|
ErrorOr<NonnullRefPtr<Font>> Font::try_load_from_resource(Core::Resource const& resource)
|
||||||
{
|
{
|
||||||
auto woff2_file_stream = TRY(Core::File::open(path, Core::File::OpenMode::Read));
|
return try_load_from_externally_owned_memory(resource.data());
|
||||||
return try_load_from_externally_owned_memory(*woff2_file_stream);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ErrorOr<NonnullRefPtr<Font>> Font::try_load_from_externally_owned_memory(ReadonlyBytes bytes)
|
ErrorOr<NonnullRefPtr<Font>> Font::try_load_from_externally_owned_memory(ReadonlyBytes bytes)
|
||||||
|
|
|
@ -21,7 +21,7 @@ class Font : public Gfx::VectorFont {
|
||||||
AK_MAKE_NONCOPYABLE(Font);
|
AK_MAKE_NONCOPYABLE(Font);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static ErrorOr<NonnullRefPtr<Font>> try_load_from_file(StringView path);
|
static ErrorOr<NonnullRefPtr<Font>> try_load_from_resource(Core::Resource const&);
|
||||||
static ErrorOr<NonnullRefPtr<Font>> try_load_from_externally_owned_memory(SeekableStream&);
|
static ErrorOr<NonnullRefPtr<Font>> try_load_from_externally_owned_memory(SeekableStream&);
|
||||||
static ErrorOr<NonnullRefPtr<Font>> try_load_from_externally_owned_memory(ReadonlyBytes);
|
static ErrorOr<NonnullRefPtr<Font>> try_load_from_externally_owned_memory(ReadonlyBytes);
|
||||||
|
|
||||||
|
|
|
@ -168,7 +168,8 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
args_parser.add_option(no_color, "Disable syntax highlighting", "no-color", 'n');
|
args_parser.add_option(no_color, "Disable syntax highlighting", "no-color", 'n');
|
||||||
args_parser.parse(arguments);
|
args_parser.parse(arguments);
|
||||||
|
|
||||||
auto font = TRY(OpenType::Font::try_load_from_file(font_path));
|
auto resource = TRY(Core::Resource::load_from_filesystem(font_path));
|
||||||
|
auto font = TRY(OpenType::Font::try_load_from_resource(resource));
|
||||||
|
|
||||||
if (dump_font_program)
|
if (dump_font_program)
|
||||||
print_disassembly("Font program"sv, font->font_program(), !no_color);
|
print_disassembly("Font program"sv, font->font_program(), !no_color);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue