LibGfx+Font: Use OutputFileStream instead of BufferStream.

This commit is contained in:
asynts 2020-09-19 18:38:24 +02:00 committed by Andreas Kling
parent fa43bf92e4
commit 5056d8bb20
Notes: sideshowbarker 2024-07-19 02:18:47 +09:00

View file

@ -27,7 +27,6 @@
#include "Font.h"
#include "Bitmap.h"
#include "Emoji.h"
#include <AK/BufferStream.h>
#include <AK/MappedFile.h>
#include <AK/StdLibExtras.h>
#include <AK/StringBuilder.h>
@ -35,8 +34,7 @@
#include <AK/Utf8View.h>
#include <AK/Vector.h>
#include <AK/kmalloc.h>
#include <errno.h>
#include <fcntl.h>
#include <LibCore/FileStream.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -222,20 +220,6 @@ RefPtr<Font> Font::load_from_file(const StringView& path)
bool Font::write_to_file(const StringView& path)
{
int fd;
#ifdef __serenity__
fd = creat_with_path_length(path.characters_without_null_termination(), path.length(), 0644);
#else
{
String null_terminated_path = path;
fd = creat(null_terminated_path.characters(), 0644);
}
#endif
if (fd < 0) {
perror("open");
return false;
}
FontFileHeader header;
memset(&header, 0, sizeof(FontFileHeader));
memcpy(header.magic, "!Fnt", 4);
@ -250,18 +234,19 @@ bool Font::write_to_file(const StringView& path)
size_t bytes_per_glyph = sizeof(unsigned) * m_glyph_height;
size_t count = glyph_count_by_type(m_type);
auto buffer = ByteBuffer::create_uninitialized(sizeof(FontFileHeader) + (count * bytes_per_glyph) + count);
BufferStream stream(buffer);
auto stream_result = Core::OutputFileStream::open_buffered(path);
if (stream_result.is_error())
return false;
auto& stream = stream_result.value();
stream << ByteBuffer::wrap(&header, sizeof(FontFileHeader));
stream << ByteBuffer::wrap(m_rows, (count * bytes_per_glyph));
stream << ByteBuffer::wrap(m_glyph_widths, count);
stream << ReadonlyBytes { &header, sizeof(header) };
stream << ReadonlyBytes { m_rows, count * bytes_per_glyph };
stream << ReadonlyBytes { m_glyph_widths, count };
stream.flush();
if (stream.handle_any_error())
return false;
ASSERT(stream.at_end());
ssize_t nwritten = write(fd, buffer.data(), buffer.size());
ASSERT(nwritten == (ssize_t)buffer.size());
int rc = close(fd);
ASSERT(rc == 0);
return true;
}