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

LibTTF: Use ReadonlyBytes where possible to avoid copies

This commit is contained in:
Stephan Unverwerth 2020-12-29 16:48:30 +01:00 committed by Andreas Kling
parent 187acd8f21
commit 0f6cf9caa1
6 changed files with 48 additions and 48 deletions

View file

@ -90,7 +90,7 @@ Optional<Cmap::Subtable> Cmap::subtable(u32 index) const
u16 encoding_id = be_u16(m_slice.offset_pointer(record_offset + (u32)Offsets::EncodingRecord_EncodingID)); u16 encoding_id = be_u16(m_slice.offset_pointer(record_offset + (u32)Offsets::EncodingRecord_EncodingID));
u32 subtable_offset = be_u32(m_slice.offset_pointer(record_offset + (u32)Offsets::EncodingRecord_Offset)); u32 subtable_offset = be_u32(m_slice.offset_pointer(record_offset + (u32)Offsets::EncodingRecord_Offset));
ASSERT(subtable_offset < m_slice.size()); ASSERT(subtable_offset < m_slice.size());
auto subtable_slice = ByteBuffer::copy(m_slice.offset_pointer(subtable_offset), m_slice.size() - subtable_offset); auto subtable_slice = ReadonlyBytes(m_slice.offset_pointer(subtable_offset), m_slice.size() - subtable_offset);
return Subtable(subtable_slice, platform_id, encoding_id); return Subtable(subtable_slice, platform_id, encoding_id);
} }
@ -163,7 +163,7 @@ u32 Cmap::glyph_id_for_codepoint(u32 codepoint) const
return subtable.glyph_id_for_codepoint(codepoint); return subtable.glyph_id_for_codepoint(codepoint);
} }
Optional<Cmap> Cmap::from_slice(const ByteBuffer& slice) Optional<Cmap> Cmap::from_slice(const ReadonlyBytes& slice)
{ {
if (slice.size() < (size_t)Sizes::TableHeader) { if (slice.size() < (size_t)Sizes::TableHeader) {
return {}; return {};

View file

@ -26,7 +26,7 @@
#pragma once #pragma once
#include <AK/ByteBuffer.h> #include <AK/Span.h>
namespace TTF { namespace TTF {
@ -56,7 +56,7 @@ public:
UnicodeFullRepertoire = 10, UnicodeFullRepertoire = 10,
}; };
Subtable(const ByteBuffer& slice, u16 platform_id, u16 encoding_id) Subtable(const ReadonlyBytes& slice, u16 platform_id, u16 encoding_id)
: m_slice(slice) : m_slice(slice)
, m_raw_platform_id(platform_id) , m_raw_platform_id(platform_id)
, m_encoding_id(encoding_id) , m_encoding_id(encoding_id)
@ -95,12 +95,12 @@ public:
u32 glyph_id_for_codepoint_table_4(u32 codepoint) const; u32 glyph_id_for_codepoint_table_4(u32 codepoint) const;
u32 glyph_id_for_codepoint_table_12(u32 codepoint) const; u32 glyph_id_for_codepoint_table_12(u32 codepoint) const;
ByteBuffer m_slice; ReadonlyBytes m_slice;
u16 m_raw_platform_id { 0 }; u16 m_raw_platform_id { 0 };
u16 m_encoding_id { 0 }; u16 m_encoding_id { 0 };
}; };
static Optional<Cmap> from_slice(const ByteBuffer&); static Optional<Cmap> from_slice(const ReadonlyBytes&);
u32 num_subtables() const; u32 num_subtables() const;
Optional<Subtable> subtable(u32 index) const; Optional<Subtable> subtable(u32 index) const;
void set_active_index(u32 index) { m_active_index = index; } void set_active_index(u32 index) { m_active_index = index; }
@ -118,12 +118,12 @@ private:
EncodingRecord = 8, EncodingRecord = 8,
}; };
Cmap(const ByteBuffer& slice) Cmap(const ReadonlyBytes& slice)
: m_slice(slice) : m_slice(slice)
{ {
} }
ByteBuffer m_slice; ReadonlyBytes m_slice;
u32 m_active_index { UINT32_MAX }; u32 m_active_index { UINT32_MAX };
}; };

View file

@ -69,7 +69,7 @@ u32 tag_from_str(const char* str)
return be_u32((const u8*)str); return be_u32((const u8*)str);
} }
Optional<Head> Head::from_slice(const ByteBuffer& slice) Optional<Head> Head::from_slice(const ReadonlyBytes& slice)
{ {
if (slice.size() < (size_t)Sizes::Table) { if (slice.size() < (size_t)Sizes::Table) {
return {}; return {};
@ -120,7 +120,7 @@ IndexToLocFormat Head::index_to_loc_format() const
} }
} }
Optional<Hhea> Hhea::from_slice(const ByteBuffer& slice) Optional<Hhea> Hhea::from_slice(const ReadonlyBytes& slice)
{ {
if (slice.size() < (size_t)Sizes::Table) { if (slice.size() < (size_t)Sizes::Table) {
return {}; return {};
@ -153,7 +153,7 @@ u16 Hhea::number_of_h_metrics() const
return be_u16(m_slice.offset_pointer((u32)Offsets::NumberOfHMetrics)); return be_u16(m_slice.offset_pointer((u32)Offsets::NumberOfHMetrics));
} }
Optional<Maxp> Maxp::from_slice(const ByteBuffer& slice) Optional<Maxp> Maxp::from_slice(const ReadonlyBytes& slice)
{ {
if (slice.size() < (size_t)Sizes::TableV0p5) { if (slice.size() < (size_t)Sizes::TableV0p5) {
return {}; return {};
@ -166,7 +166,7 @@ u16 Maxp::num_glyphs() const
return be_u16(m_slice.offset_pointer((u32)Offsets::NumGlyphs)); return be_u16(m_slice.offset_pointer((u32)Offsets::NumGlyphs));
} }
Optional<Hmtx> Hmtx::from_slice(const ByteBuffer& slice, u32 num_glyphs, u32 number_of_h_metrics) Optional<Hmtx> Hmtx::from_slice(const ReadonlyBytes& slice, u32 num_glyphs, u32 number_of_h_metrics)
{ {
if (slice.size() < number_of_h_metrics * (u32)Sizes::LongHorMetric + (num_glyphs - number_of_h_metrics) * (u32)Sizes::LeftSideBearing) { if (slice.size() < number_of_h_metrics * (u32)Sizes::LongHorMetric + (num_glyphs - number_of_h_metrics) * (u32)Sizes::LeftSideBearing) {
return {}; return {};
@ -241,13 +241,13 @@ RefPtr<Font> Font::load_from_offset(ByteBuffer&& buffer, u32 offset)
return nullptr; return nullptr;
} }
Optional<ByteBuffer> opt_head_slice = {}; Optional<ReadonlyBytes> opt_head_slice = {};
Optional<ByteBuffer> opt_hhea_slice = {}; Optional<ReadonlyBytes> opt_hhea_slice = {};
Optional<ByteBuffer> opt_maxp_slice = {}; Optional<ReadonlyBytes> opt_maxp_slice = {};
Optional<ByteBuffer> opt_hmtx_slice = {}; Optional<ReadonlyBytes> opt_hmtx_slice = {};
Optional<ByteBuffer> opt_cmap_slice = {}; Optional<ReadonlyBytes> opt_cmap_slice = {};
Optional<ByteBuffer> opt_loca_slice = {}; Optional<ReadonlyBytes> opt_loca_slice = {};
Optional<ByteBuffer> opt_glyf_slice = {}; Optional<ReadonlyBytes> opt_glyf_slice = {};
Optional<Head> opt_head = {}; Optional<Head> opt_head = {};
Optional<Hhea> opt_hhea = {}; Optional<Hhea> opt_hhea = {};
@ -271,7 +271,7 @@ RefPtr<Font> Font::load_from_offset(ByteBuffer&& buffer, u32 offset)
dbg() << "Font file too small"; dbg() << "Font file too small";
return nullptr; return nullptr;
} }
auto buffer_here = ByteBuffer::copy(buffer.offset_pointer(table_offset), table_length); auto buffer_here = ReadonlyBytes(buffer.offset_pointer(table_offset), table_length);
// Get the table offsets we need. // Get the table offsets we need.
if (tag == tag_from_str("head")) { if (tag == tag_from_str("head")) {

View file

@ -76,7 +76,7 @@ public:
Gfx::FloatPoint point; Gfx::FloatPoint point;
}; };
PointIterator(const ByteBuffer& slice, u16 num_points, u32 flags_offset, u32 x_offset, u32 y_offset, Gfx::AffineTransform affine) PointIterator(const ReadonlyBytes& slice, u16 num_points, u32 flags_offset, u32 x_offset, u32 y_offset, Gfx::AffineTransform affine)
: m_slice(slice) : m_slice(slice)
, m_points_remaining(num_points) , m_points_remaining(num_points)
, m_flags_offset(flags_offset) , m_flags_offset(flags_offset)
@ -136,7 +136,7 @@ public:
} }
private: private:
ByteBuffer m_slice; ReadonlyBytes m_slice;
u16 m_points_remaining; u16 m_points_remaining;
u8 m_flag { 0 }; u8 m_flag { 0 };
Gfx::FloatPoint m_last_point = { 0.0f, 0.0f }; Gfx::FloatPoint m_last_point = { 0.0f, 0.0f };
@ -337,7 +337,7 @@ void Rasterizer::draw_line(Gfx::FloatPoint p0, Gfx::FloatPoint p1)
} }
} }
Optional<Loca> Loca::from_slice(const ByteBuffer& slice, u32 num_glyphs, IndexToLocFormat index_to_loc_format) Optional<Loca> Loca::from_slice(const ReadonlyBytes& slice, u32 num_glyphs, IndexToLocFormat index_to_loc_format)
{ {
switch (index_to_loc_format) { switch (index_to_loc_format) {
case IndexToLocFormat::Offset16: case IndexToLocFormat::Offset16:
@ -367,7 +367,7 @@ u32 Loca::get_glyph_offset(u32 glyph_id) const
} }
} }
static void get_ttglyph_offsets(const ByteBuffer& slice, u32 num_points, u32 flags_offset, u32* x_offset, u32* y_offset) static void get_ttglyph_offsets(const ReadonlyBytes& slice, u32 num_points, u32 flags_offset, u32* x_offset, u32* y_offset)
{ {
u32 flags_size = 0; u32 flags_size = 0;
u32 x_size = 0; u32 x_size = 0;
@ -512,7 +512,7 @@ Glyf::Glyph Glyf::glyph(u32 offset) const
i16 ymin = be_i16(m_slice.offset_pointer(offset + (u32)Offsets::YMin)); i16 ymin = be_i16(m_slice.offset_pointer(offset + (u32)Offsets::YMin));
i16 xmax = be_i16(m_slice.offset_pointer(offset + (u32)Offsets::XMax)); i16 xmax = be_i16(m_slice.offset_pointer(offset + (u32)Offsets::XMax));
i16 ymax = be_i16(m_slice.offset_pointer(offset + (u32)Offsets::YMax)); i16 ymax = be_i16(m_slice.offset_pointer(offset + (u32)Offsets::YMax));
auto slice = ByteBuffer::copy(m_slice.offset_pointer(offset + (u32)Sizes::GlyphHeader), m_slice.size() - offset - (u32)Sizes::GlyphHeader); auto slice = ReadonlyBytes(m_slice.offset_pointer(offset + (u32)Sizes::GlyphHeader), m_slice.size() - offset - (u32)Sizes::GlyphHeader);
return Glyph(slice, xmin, ymin, xmax, ymax, num_contours); return Glyph(slice, xmin, ymin, xmax, ymax, num_contours);
} }

View file

@ -26,7 +26,7 @@
#pragma once #pragma once
#include <AK/ByteBuffer.h> #include <AK/Span.h>
#include <AK/Vector.h> #include <AK/Vector.h>
#include <LibGfx/AffineTransform.h> #include <LibGfx/AffineTransform.h>
#include <LibGfx/Bitmap.h> #include <LibGfx/Bitmap.h>
@ -50,18 +50,18 @@ private:
class Loca { class Loca {
public: public:
static Optional<Loca> from_slice(const ByteBuffer&, u32 num_glyphs, IndexToLocFormat); static Optional<Loca> from_slice(const ReadonlyBytes&, u32 num_glyphs, IndexToLocFormat);
u32 get_glyph_offset(u32 glyph_id) const; u32 get_glyph_offset(u32 glyph_id) const;
private: private:
Loca(const ByteBuffer& slice, u32 num_glyphs, IndexToLocFormat index_to_loc_format) Loca(const ReadonlyBytes& slice, u32 num_glyphs, IndexToLocFormat index_to_loc_format)
: m_slice(slice) : m_slice(slice)
, m_num_glyphs(num_glyphs) , m_num_glyphs(num_glyphs)
, m_index_to_loc_format(index_to_loc_format) , m_index_to_loc_format(index_to_loc_format)
{ {
} }
ByteBuffer m_slice; ReadonlyBytes m_slice;
u32 m_num_glyphs { 0 }; u32 m_num_glyphs { 0 };
IndexToLocFormat m_index_to_loc_format; IndexToLocFormat m_index_to_loc_format;
}; };
@ -70,7 +70,7 @@ class Glyf {
public: public:
class Glyph { class Glyph {
public: public:
Glyph(const ByteBuffer& slice, i16 xmin, i16 ymin, i16 xmax, i16 ymax, i16 num_contours = -1) Glyph(const ReadonlyBytes& slice, i16 xmin, i16 ymin, i16 xmax, i16 ymax, i16 num_contours = -1)
: m_xmin(xmin) : m_xmin(xmin)
, m_ymin(ymin) , m_ymin(ymin)
, m_xmax(xmax) , m_xmax(xmax)
@ -109,14 +109,14 @@ public:
Gfx::AffineTransform affine; Gfx::AffineTransform affine;
}; };
ComponentIterator(const ByteBuffer& slice) ComponentIterator(const ReadonlyBytes& slice)
: m_slice(slice) : m_slice(slice)
{ {
} }
Optional<Item> next(); Optional<Item> next();
private: private:
ByteBuffer m_slice; ReadonlyBytes m_slice;
bool m_has_more { true }; bool m_has_more { true };
u32 m_offset { 0 }; u32 m_offset { 0 };
}; };
@ -150,10 +150,10 @@ public:
i16 m_xmax { 0 }; i16 m_xmax { 0 };
i16 m_ymax { 0 }; i16 m_ymax { 0 };
i16 m_num_contours { -1 }; i16 m_num_contours { -1 };
ByteBuffer m_slice; ReadonlyBytes m_slice;
}; };
Glyf(const ByteBuffer& slice) Glyf(const ReadonlyBytes& slice)
: m_slice(slice) : m_slice(slice)
{ {
} }
@ -170,7 +170,7 @@ private:
GlyphHeader = 10, GlyphHeader = 10,
}; };
ByteBuffer m_slice; ReadonlyBytes m_slice;
}; };
} }

View file

@ -26,7 +26,7 @@
#pragma once #pragma once
#include <AK/ByteBuffer.h> #include <AK/Span.h>
namespace TTF { namespace TTF {
@ -37,7 +37,7 @@ enum class IndexToLocFormat {
class Head { class Head {
public: public:
static Optional<Head> from_slice(const ByteBuffer&); static Optional<Head> from_slice(const ReadonlyBytes&);
u16 units_per_em() const; u16 units_per_em() const;
i16 xmin() const; i16 xmin() const;
i16 ymin() const; i16 ymin() const;
@ -60,17 +60,17 @@ private:
Table = 54, Table = 54,
}; };
Head(const ByteBuffer& slice) Head(const ReadonlyBytes& slice)
: m_slice(slice) : m_slice(slice)
{ {
} }
ByteBuffer m_slice; ReadonlyBytes m_slice;
}; };
class Hhea { class Hhea {
public: public:
static Optional<Hhea> from_slice(const ByteBuffer&); static Optional<Hhea> from_slice(const ReadonlyBytes&);
i16 ascender() const; i16 ascender() const;
i16 descender() const; i16 descender() const;
i16 line_gap() const; i16 line_gap() const;
@ -89,17 +89,17 @@ private:
Table = 36, Table = 36,
}; };
Hhea(const ByteBuffer& slice) Hhea(const ReadonlyBytes& slice)
: m_slice(slice) : m_slice(slice)
{ {
} }
ByteBuffer m_slice; ReadonlyBytes m_slice;
}; };
class Maxp { class Maxp {
public: public:
static Optional<Maxp> from_slice(const ByteBuffer&); static Optional<Maxp> from_slice(const ReadonlyBytes&);
u16 num_glyphs() const; u16 num_glyphs() const;
private: private:
@ -110,12 +110,12 @@ private:
TableV0p5 = 6, TableV0p5 = 6,
}; };
Maxp(const ByteBuffer& slice) Maxp(const ReadonlyBytes& slice)
: m_slice(slice) : m_slice(slice)
{ {
} }
ByteBuffer m_slice; ReadonlyBytes m_slice;
}; };
struct GlyphHorizontalMetrics { struct GlyphHorizontalMetrics {
@ -125,7 +125,7 @@ struct GlyphHorizontalMetrics {
class Hmtx { class Hmtx {
public: public:
static Optional<Hmtx> from_slice(const ByteBuffer&, u32 num_glyphs, u32 number_of_h_metrics); static Optional<Hmtx> from_slice(const ReadonlyBytes&, u32 num_glyphs, u32 number_of_h_metrics);
GlyphHorizontalMetrics get_glyph_horizontal_metrics(u32 glyph_id) const; GlyphHorizontalMetrics get_glyph_horizontal_metrics(u32 glyph_id) const;
private: private:
@ -134,14 +134,14 @@ private:
LeftSideBearing = 2 LeftSideBearing = 2
}; };
Hmtx(const ByteBuffer& slice, u32 num_glyphs, u32 number_of_h_metrics) Hmtx(const ReadonlyBytes& slice, u32 num_glyphs, u32 number_of_h_metrics)
: m_slice(slice) : m_slice(slice)
, m_num_glyphs(num_glyphs) , m_num_glyphs(num_glyphs)
, m_number_of_h_metrics(number_of_h_metrics) , m_number_of_h_metrics(number_of_h_metrics)
{ {
} }
ByteBuffer m_slice; ReadonlyBytes m_slice;
u32 m_num_glyphs { 0 }; u32 m_num_glyphs { 0 };
u32 m_number_of_h_metrics { 0 }; u32 m_number_of_h_metrics { 0 };
}; };