mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 23:17:46 +00:00
Font: Clean up AK::MappedFile and use it for mapping font files.
This commit is contained in:
parent
ab6bd3872b
commit
3dc3754cde
5 changed files with 48 additions and 37 deletions
|
@ -2,7 +2,7 @@
|
|||
#include <AK/kmalloc.h>
|
||||
#include <AK/BufferStream.h>
|
||||
#include <AK/StdLibExtras.h>
|
||||
|
||||
#include <AK/MappedFile.h>
|
||||
#include <LibC/unistd.h>
|
||||
#include <LibC/stdio.h>
|
||||
#include <LibC/fcntl.h>
|
||||
|
@ -90,10 +90,6 @@ Font::Font(const String& name, unsigned* rows, byte* widths, bool is_fixed_width
|
|||
|
||||
Font::~Font()
|
||||
{
|
||||
if (m_mmap_ptr) {
|
||||
int rc = munmap(m_mmap_ptr, 4096 * 3);
|
||||
ASSERT(rc == 0);
|
||||
}
|
||||
}
|
||||
|
||||
RetainPtr<Font> Font::load_from_memory(const byte* data)
|
||||
|
@ -119,25 +115,12 @@ RetainPtr<Font> Font::load_from_memory(const byte* data)
|
|||
|
||||
RetainPtr<Font> Font::load_from_file(const String& path)
|
||||
{
|
||||
int fd = open(path.characters(), O_RDONLY, 0644);
|
||||
if (fd < 0) {
|
||||
dbgprintf("open(%s) got fd=%d, failed: %s\n", path.characters(), fd, strerror(errno));
|
||||
perror("open");
|
||||
MappedFile mapped_file(path);
|
||||
if (!mapped_file.is_valid())
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto* mapped_file = (byte*)mmap(nullptr, 4096 * 3, PROT_READ, MAP_SHARED, fd, 0);
|
||||
if (mapped_file == MAP_FAILED) {
|
||||
int rc = close(fd);
|
||||
ASSERT(rc == 0);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto font = load_from_memory(mapped_file);
|
||||
font->m_mmap_ptr = mapped_file;
|
||||
|
||||
int rc = close(fd);
|
||||
ASSERT(rc == 0);
|
||||
auto font = load_from_memory((const byte*)mapped_file.pointer());
|
||||
font->m_mapped_file = move(mapped_file);
|
||||
return font;
|
||||
}
|
||||
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
#include <AK/Retainable.h>
|
||||
#include <AK/RetainPtr.h>
|
||||
#include <AK/AKString.h>
|
||||
#include <AK/MappedFile.h>
|
||||
#include <AK/Types.h>
|
||||
|
||||
// FIXME: Make a MutableGlyphBitmap buddy class for FontEditor instead?
|
||||
|
@ -47,8 +48,6 @@ public:
|
|||
|
||||
RetainPtr<Font> clone() const;
|
||||
|
||||
static RetainPtr<Font> load_from_memory(const byte*);
|
||||
|
||||
static RetainPtr<Font> load_from_file(const String& path);
|
||||
bool write_to_file(const String& path);
|
||||
|
||||
|
@ -78,11 +77,13 @@ public:
|
|||
private:
|
||||
Font(const String& name, unsigned* rows, byte* widths, bool is_fixed_width, byte glyph_width, byte glyph_height);
|
||||
|
||||
static RetainPtr<Font> load_from_memory(const byte*);
|
||||
|
||||
String m_name;
|
||||
|
||||
unsigned* m_rows { nullptr };
|
||||
byte* m_glyph_widths { nullptr };
|
||||
void* m_mmap_ptr { nullptr };
|
||||
MappedFile m_mapped_file;
|
||||
|
||||
byte m_glyph_width { 0 };
|
||||
byte m_glyph_height { 0 };
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue