1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 22:37:35 +00:00

LibGfx: Simplify writing to WOFF2 reconstructed glyf table

This commit is contained in:
Sam Atkins 2023-10-21 14:23:41 +01:00 committed by Andreas Kling
parent ad717af63d
commit 885665b3a6

View file

@ -123,12 +123,6 @@ static i16 be_i16(u8 const* ptr)
return (((i16)ptr[0]) << 8) | ((i16)ptr[1]); return (((i16)ptr[0]) << 8) | ((i16)ptr[1]);
} }
static void be_i16(u8* ptr, i16 value)
{
ptr[0] = (value >> 8) & 0xff;
ptr[1] = value & 0xff;
}
static u16 pow_2_less_than_or_equal(u16 x) static u16 pow_2_less_than_or_equal(u16 x)
{ {
u16 result = 1; u16 result = 1;
@ -580,33 +574,16 @@ static ErrorOr<GlyfAndLocaTableBuffers> create_glyf_and_loca_tables_from_transfo
ByteBuffer reconstructed_glyf_table; ByteBuffer reconstructed_glyf_table;
Vector<u32> loca_indexes; Vector<u32> loca_indexes;
auto append_u16 = [&](u16 value) -> ErrorOr<void> { auto append_u16 = [&](BigEndian<u16> value) -> ErrorOr<void> {
auto end = reconstructed_glyf_table.size(); return reconstructed_glyf_table.try_append(&value, sizeof(value));
TRY(reconstructed_glyf_table.try_resize(reconstructed_glyf_table.size() + sizeof(value)));
auto* slot = reconstructed_glyf_table.offset_pointer(end);
be_u16(slot, value);
return {};
}; };
auto append_i16 = [&](i16 value) -> ErrorOr<void> { auto append_i16 = [&](BigEndian<i16> value) -> ErrorOr<void> {
auto end = reconstructed_glyf_table.size(); return reconstructed_glyf_table.try_append(&value, sizeof(value));
TRY(reconstructed_glyf_table.try_resize(reconstructed_glyf_table.size() + sizeof(value)));
auto* slot = reconstructed_glyf_table.offset_pointer(end);
be_i16(slot, value);
return {};
}; };
auto append_bytes = [&](ReadonlyBytes bytes) -> ErrorOr<void> { auto append_bytes = [&](ReadonlyBytes bytes) -> ErrorOr<void> {
TRY(reconstructed_glyf_table.try_append(bytes)); return reconstructed_glyf_table.try_append(bytes);
return {};
};
auto transfer_bytes = [&](ByteBuffer& dst, FixedMemoryStream& src, size_t count) -> ErrorOr<void> {
auto end = dst.size();
TRY(dst.try_resize(dst.size() + count));
auto* slot = dst.offset_pointer(end);
TRY(src.read_until_filled(Bytes { slot, count }));
return {};
}; };
for (size_t glyph_index = 0; glyph_index < header.num_glyphs; ++glyph_index) { for (size_t glyph_index = 0; glyph_index < header.num_glyphs; ++glyph_index) {
@ -682,18 +659,15 @@ static ErrorOr<GlyfAndLocaTableBuffers> create_glyf_and_loca_tables_from_transfo
} }
TRY(append_u16(flags)); TRY(append_u16(flags));
TRY(transfer_bytes(reconstructed_glyf_table, composite_stream, argument_byte_count)); TRY(reconstructed_glyf_table.try_append(TRY(composite_stream.read_in_place<u8>(argument_byte_count))));
} }
if (have_instructions) { if (have_instructions) {
auto number_of_instructions = TRY(read_255_u_short(glyph_stream)); auto number_of_instructions = TRY(read_255_u_short(glyph_stream));
TRY(append_u16(number_of_instructions)); TRY(append_u16(number_of_instructions));
if (number_of_instructions) { if (number_of_instructions)
auto instructions = TRY(ByteBuffer::create_zeroed(number_of_instructions)); TRY(reconstructed_glyf_table.try_append(TRY(instruction_stream.read_in_place<u8>(number_of_instructions))));
TRY(instruction_stream.read_until_filled(instructions));
TRY(reconstructed_glyf_table.try_append(instructions));
}
} }
} else if (number_of_contours > 0) { } else if (number_of_contours > 0) {
// Decoding of Simple Glyphs // Decoding of Simple Glyphs