mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-01 13:49:16 +00:00
LibTTF: Address code-style comments, gracefully handle load failures.
This commit is contained in:
parent
ec08e9e780
commit
3b31f069f0
Notes:
sideshowbarker
2024-07-19 00:20:37 +09:00
Author: https://github.com/SrimantaBarua
Commit: 3b31f069f0
Pull-request: https://github.com/SerenityOS/serenity/pull/4631
Issue: https://github.com/SerenityOS/serenity/issues/743
Reviewed-by: https://github.com/alimpfard
Reviewed-by: https://github.com/sunverwerth
7 changed files with 336 additions and 248 deletions
|
@ -24,117 +24,152 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "Cmap.h"
|
||||
#include "Font.h"
|
||||
#include "Glyf.h"
|
||||
#include "Tables.h"
|
||||
#include "AK/ByteBuffer.h"
|
||||
#include <AK/LogStream.h>
|
||||
#include <AK/Utf8View.h>
|
||||
#include <AK/Utf32View.h>
|
||||
#include <bits/stdint.h>
|
||||
#include <AK/Utf8View.h>
|
||||
#include <LibCore/File.h>
|
||||
#include <LibTTF/Cmap.h>
|
||||
#include <LibTTF/Font.h>
|
||||
#include <LibTTF/Glyf.h>
|
||||
#include <LibTTF/Tables.h>
|
||||
#include <bits/stdint.h>
|
||||
#include <math.h>
|
||||
|
||||
namespace TTF {
|
||||
|
||||
u16 be_u16(const u8* ptr)
|
||||
{
|
||||
return (((u16) ptr[0]) << 8) | ((u16) ptr[1]);
|
||||
return (((u16)ptr[0]) << 8) | ((u16)ptr[1]);
|
||||
}
|
||||
|
||||
u32 be_u32(const u8* ptr)
|
||||
{
|
||||
return (((u32) ptr[0]) << 24) | (((u32) ptr[1]) << 16) | (((u32) ptr[2]) << 8) | ((u32) ptr[3]);
|
||||
return (((u32)ptr[0]) << 24) | (((u32)ptr[1]) << 16) | (((u32)ptr[2]) << 8) | ((u32)ptr[3]);
|
||||
}
|
||||
|
||||
i16 be_i16(const u8* ptr)
|
||||
{
|
||||
return (((i16) ptr[0]) << 8) | ((i16) ptr[1]);
|
||||
return (((i16)ptr[0]) << 8) | ((i16)ptr[1]);
|
||||
}
|
||||
|
||||
float be_fword(const u8* ptr)
|
||||
{
|
||||
return (float) be_i16(ptr) / (float) (1 << 14);
|
||||
return (float)be_i16(ptr) / (float)(1 << 14);
|
||||
}
|
||||
|
||||
u32 tag_from_str(const char *str)
|
||||
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)
|
||||
{
|
||||
if (slice.size() < (size_t)Sizes::Table) {
|
||||
return {};
|
||||
}
|
||||
return Head(slice);
|
||||
}
|
||||
|
||||
u16 Head::units_per_em() const
|
||||
{
|
||||
return be_u16(m_slice.offset_pointer((u32) Offsets::UnitsPerEM));
|
||||
return be_u16(m_slice.offset_pointer((u32)Offsets::UnitsPerEM));
|
||||
}
|
||||
|
||||
i16 Head::xmin() const
|
||||
{
|
||||
return be_i16(m_slice.offset_pointer((u32) Offsets::XMin));
|
||||
return be_i16(m_slice.offset_pointer((u32)Offsets::XMin));
|
||||
}
|
||||
|
||||
i16 Head::ymin() const
|
||||
{
|
||||
return be_i16(m_slice.offset_pointer((u32) Offsets::YMin));
|
||||
return be_i16(m_slice.offset_pointer((u32)Offsets::YMin));
|
||||
}
|
||||
|
||||
i16 Head::xmax() const
|
||||
{
|
||||
return be_i16(m_slice.offset_pointer((u32) Offsets::XMax));
|
||||
return be_i16(m_slice.offset_pointer((u32)Offsets::XMax));
|
||||
}
|
||||
|
||||
i16 Head::ymax() const
|
||||
{
|
||||
return be_i16(m_slice.offset_pointer((u32) Offsets::YMax));
|
||||
return be_i16(m_slice.offset_pointer((u32)Offsets::YMax));
|
||||
}
|
||||
|
||||
u16 Head::lowest_recommended_ppem() const
|
||||
{
|
||||
return be_u16(m_slice.offset_pointer((u32) Offsets::LowestRecPPEM));
|
||||
return be_u16(m_slice.offset_pointer((u32)Offsets::LowestRecPPEM));
|
||||
}
|
||||
|
||||
IndexToLocFormat Head::index_to_loc_format() const
|
||||
{
|
||||
i16 raw = be_i16(m_slice.offset_pointer((u32) Offsets::IndexToLocFormat));
|
||||
i16 raw = be_i16(m_slice.offset_pointer((u32)Offsets::IndexToLocFormat));
|
||||
switch (raw) {
|
||||
case 0:
|
||||
return IndexToLocFormat::Offset16;
|
||||
case 1:
|
||||
return IndexToLocFormat::Offset32;
|
||||
default:
|
||||
ASSERT_NOT_REACHED();
|
||||
case 0: return IndexToLocFormat::Offset16;
|
||||
case 1: return IndexToLocFormat::Offset32;
|
||||
default: ASSERT_NOT_REACHED();
|
||||
}
|
||||
}
|
||||
|
||||
Optional<Hhea> Hhea::from_slice(const ByteBuffer& slice)
|
||||
{
|
||||
if (slice.size() < (size_t)Sizes::Table) {
|
||||
return {};
|
||||
}
|
||||
return Hhea(slice);
|
||||
}
|
||||
|
||||
i16 Hhea::ascender() const
|
||||
{
|
||||
return be_i16(m_slice.offset_pointer((u32) Offsets::Ascender));
|
||||
return be_i16(m_slice.offset_pointer((u32)Offsets::Ascender));
|
||||
}
|
||||
|
||||
i16 Hhea::descender() const
|
||||
{
|
||||
return be_i16(m_slice.offset_pointer((u32) Offsets::Descender));
|
||||
return be_i16(m_slice.offset_pointer((u32)Offsets::Descender));
|
||||
}
|
||||
|
||||
i16 Hhea::line_gap() const
|
||||
{
|
||||
return be_i16(m_slice.offset_pointer((u32) Offsets::LineGap));
|
||||
return be_i16(m_slice.offset_pointer((u32)Offsets::LineGap));
|
||||
}
|
||||
|
||||
u16 Hhea::advance_width_max() const
|
||||
{
|
||||
return be_u16(m_slice.offset_pointer((u32) Offsets::AdvanceWidthMax));
|
||||
return be_u16(m_slice.offset_pointer((u32)Offsets::AdvanceWidthMax));
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
if (slice.size() < (size_t)Sizes::TableV0p5) {
|
||||
return {};
|
||||
}
|
||||
return Maxp(slice);
|
||||
}
|
||||
|
||||
u16 Maxp::num_glyphs() const
|
||||
{
|
||||
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)
|
||||
{
|
||||
if (slice.size() < number_of_h_metrics * (u32)Sizes::LongHorMetric + (num_glyphs - number_of_h_metrics) * (u32)Sizes::LeftSideBearing) {
|
||||
return {};
|
||||
}
|
||||
return Hmtx(slice, num_glyphs, number_of_h_metrics);
|
||||
}
|
||||
|
||||
GlyphHorizontalMetrics Hmtx::get_glyph_horizontal_metrics(u32 glyph_id) const
|
||||
{
|
||||
ASSERT(glyph_id < m_num_glyphs);
|
||||
if (glyph_id < m_number_of_h_metrics) {
|
||||
auto offset = glyph_id * (u32) Sizes::LongHorMetric;
|
||||
auto offset = glyph_id * (u32)Sizes::LongHorMetric;
|
||||
u16 advance_width = be_u16(m_slice.offset_pointer(offset));
|
||||
i16 left_side_bearing = be_i16(m_slice.offset_pointer(offset + 2));
|
||||
return GlyphHorizontalMetrics {
|
||||
|
@ -142,8 +177,8 @@ GlyphHorizontalMetrics Hmtx::get_glyph_horizontal_metrics(u32 glyph_id) const
|
|||
.left_side_bearing = left_side_bearing,
|
||||
};
|
||||
}
|
||||
auto offset = m_number_of_h_metrics * (u32) Sizes::LongHorMetric + (glyph_id - m_number_of_h_metrics) * (u32) Sizes::LeftSideBearing;
|
||||
u16 advance_width = be_u16(m_slice.offset_pointer((m_number_of_h_metrics - 1) * (u32) Sizes::LongHorMetric));
|
||||
auto offset = m_number_of_h_metrics * (u32)Sizes::LongHorMetric + (glyph_id - m_number_of_h_metrics) * (u32)Sizes::LeftSideBearing;
|
||||
u16 advance_width = be_u16(m_slice.offset_pointer((m_number_of_h_metrics - 1) * (u32)Sizes::LongHorMetric));
|
||||
i16 left_side_bearing = be_i16(m_slice.offset_pointer(offset));
|
||||
return GlyphHorizontalMetrics {
|
||||
.advance_width = advance_width,
|
||||
|
@ -151,11 +186,6 @@ GlyphHorizontalMetrics Hmtx::get_glyph_horizontal_metrics(u32 glyph_id) const
|
|||
};
|
||||
}
|
||||
|
||||
u16 Maxp::num_glyphs() const
|
||||
{
|
||||
return be_u16(m_slice.offset_pointer((u32) Offsets::NumGlyphs));
|
||||
}
|
||||
|
||||
RefPtr<Font> Font::load_from_file(const StringView& path, unsigned index)
|
||||
{
|
||||
auto file_or_error = Core::File::open(String(path), Core::IODevice::ReadOnly);
|
||||
|
@ -176,12 +206,12 @@ RefPtr<Font> Font::load_from_file(const StringView& path, unsigned index)
|
|||
u32 tag = be_u32(buffer.data());
|
||||
if (tag == tag_from_str("ttcf")) {
|
||||
// It's a font collection
|
||||
if (buffer.size() < (u32) Sizes::TTCHeaderV1 + sizeof(u32) * (index + 1)) {
|
||||
if (buffer.size() < (u32)Sizes::TTCHeaderV1 + sizeof(u32) * (index + 1)) {
|
||||
dbg() << "Font file too small";
|
||||
return nullptr;
|
||||
}
|
||||
u32 offset = be_u32(buffer.offset_pointer((u32) Sizes::TTCHeaderV1 + sizeof(u32) * index));
|
||||
return adopt(*new Font(move(buffer), offset));
|
||||
u32 offset = be_u32(buffer.offset_pointer((u32)Sizes::TTCHeaderV1 + sizeof(u32) * index));
|
||||
return load_from_offset(move(buffer), offset);
|
||||
}
|
||||
if (tag == tag_from_str("OTTO")) {
|
||||
dbg() << "CFF fonts not supported yet";
|
||||
|
@ -191,163 +221,190 @@ RefPtr<Font> Font::load_from_file(const StringView& path, unsigned index)
|
|||
dbg() << "Not a valid font";
|
||||
return nullptr;
|
||||
}
|
||||
return adopt(*new Font(move(buffer), 0));
|
||||
return load_from_offset(move(buffer), 0);
|
||||
}
|
||||
|
||||
// FIXME: "loca" and "glyf" are not available for CFF fonts.
|
||||
Font::Font(ByteBuffer&& buffer, u32 offset)
|
||||
: m_buffer(move(buffer))
|
||||
RefPtr<Font> Font::load_from_offset(ByteBuffer&& buffer, u32 offset)
|
||||
{
|
||||
ASSERT(m_buffer.size() >= offset + (u32) Sizes::OffsetTable);
|
||||
Optional<ByteBuffer> head_slice = {};
|
||||
Optional<ByteBuffer> hhea_slice = {};
|
||||
Optional<ByteBuffer> maxp_slice = {};
|
||||
Optional<ByteBuffer> hmtx_slice = {};
|
||||
Optional<ByteBuffer> cmap_slice = {};
|
||||
Optional<ByteBuffer> loca_slice = {};
|
||||
Optional<ByteBuffer> glyf_slice = {};
|
||||
if (buffer.size() < offset + (u32)Sizes::OffsetTable) {
|
||||
dbg() << "Font file too small";
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
//auto sfnt_version = be_u32(data + offset);
|
||||
auto num_tables = be_u16(m_buffer.offset_pointer(offset + (u32) Offsets::NumTables));
|
||||
ASSERT(m_buffer.size() >= offset + (u32) Sizes::OffsetTable + num_tables * (u32) Sizes::TableRecord);
|
||||
Optional<ByteBuffer> opt_head_slice = {};
|
||||
Optional<ByteBuffer> opt_hhea_slice = {};
|
||||
Optional<ByteBuffer> opt_maxp_slice = {};
|
||||
Optional<ByteBuffer> opt_hmtx_slice = {};
|
||||
Optional<ByteBuffer> opt_cmap_slice = {};
|
||||
Optional<ByteBuffer> opt_loca_slice = {};
|
||||
Optional<ByteBuffer> opt_glyf_slice = {};
|
||||
|
||||
Optional<Head> opt_head = {};
|
||||
Optional<Hhea> opt_hhea = {};
|
||||
Optional<Maxp> opt_maxp = {};
|
||||
Optional<Hmtx> opt_hmtx = {};
|
||||
Optional<Cmap> opt_cmap = {};
|
||||
Optional<Loca> opt_loca = {};
|
||||
|
||||
auto num_tables = be_u16(buffer.offset_pointer(offset + (u32)Offsets::NumTables));
|
||||
if (buffer.size() < offset + (u32)Sizes::OffsetTable + num_tables * (u32)Sizes::TableRecord) {
|
||||
dbg() << "Font file too small";
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
for (auto i = 0; i < num_tables; i++) {
|
||||
u32 record_offset = offset + (u32) Sizes::OffsetTable + i * (u32) Sizes::TableRecord;
|
||||
u32 tag = be_u32(m_buffer.offset_pointer(record_offset));
|
||||
u32 table_offset = be_u32(m_buffer.offset_pointer(record_offset + (u32) Offsets::TableRecord_Offset));
|
||||
u32 table_length = be_u32(m_buffer.offset_pointer(record_offset + (u32) Offsets::TableRecord_Length));
|
||||
ASSERT(m_buffer.size() >= table_offset + table_length);
|
||||
auto buffer = ByteBuffer::wrap(m_buffer.offset_pointer(table_offset), table_length);
|
||||
u32 record_offset = offset + (u32)Sizes::OffsetTable + i * (u32)Sizes::TableRecord;
|
||||
u32 tag = be_u32(buffer.offset_pointer(record_offset));
|
||||
u32 table_offset = be_u32(buffer.offset_pointer(record_offset + (u32)Offsets::TableRecord_Offset));
|
||||
u32 table_length = be_u32(buffer.offset_pointer(record_offset + (u32)Offsets::TableRecord_Length));
|
||||
if (buffer.size() < table_offset + table_length) {
|
||||
dbg() << "Font file too small";
|
||||
return nullptr;
|
||||
}
|
||||
auto buffer_here = ByteBuffer::wrap(buffer.offset_pointer(table_offset), table_length);
|
||||
|
||||
// Get the table offsets we need.
|
||||
if (tag == tag_from_str("head")) {
|
||||
head_slice = buffer;
|
||||
opt_head_slice = buffer_here;
|
||||
} else if (tag == tag_from_str("hhea")) {
|
||||
hhea_slice = buffer;
|
||||
opt_hhea_slice = buffer_here;
|
||||
} else if (tag == tag_from_str("maxp")) {
|
||||
maxp_slice = buffer;
|
||||
opt_maxp_slice = buffer_here;
|
||||
} else if (tag == tag_from_str("hmtx")) {
|
||||
hmtx_slice = buffer;
|
||||
opt_hmtx_slice = buffer_here;
|
||||
} else if (tag == tag_from_str("cmap")) {
|
||||
cmap_slice = buffer;
|
||||
opt_cmap_slice = buffer_here;
|
||||
} else if (tag == tag_from_str("loca")) {
|
||||
loca_slice = buffer;
|
||||
opt_loca_slice = buffer_here;
|
||||
} else if (tag == tag_from_str("glyf")) {
|
||||
glyf_slice = buffer;
|
||||
opt_glyf_slice = buffer_here;
|
||||
}
|
||||
}
|
||||
|
||||
// Check that we've got everything we need.
|
||||
ASSERT(head_slice.has_value());
|
||||
ASSERT(hhea_slice.has_value());
|
||||
ASSERT(maxp_slice.has_value());
|
||||
ASSERT(hmtx_slice.has_value());
|
||||
ASSERT(cmap_slice.has_value());
|
||||
ASSERT(loca_slice.has_value());
|
||||
ASSERT(glyf_slice.has_value());
|
||||
if (!opt_head_slice.has_value() || !(opt_head = Head::from_slice(opt_head_slice.value())).has_value()) {
|
||||
dbg() << "Could not load Head";
|
||||
return nullptr;
|
||||
}
|
||||
auto head = opt_head.value();
|
||||
|
||||
// Load the tables.
|
||||
m_head_slice = head_slice.value();
|
||||
m_hhea_slice = hhea_slice.value();
|
||||
m_maxp_slice = maxp_slice.value();
|
||||
m_hmtx_slice = hmtx_slice.value();
|
||||
m_loca_slice = loca_slice.value();
|
||||
m_glyf_slice = glyf_slice.value();
|
||||
if (!opt_hhea_slice.has_value() || !(opt_hhea = Hhea::from_slice(opt_hhea_slice.value())).has_value()) {
|
||||
dbg() << "Could not load Hhea";
|
||||
return nullptr;
|
||||
}
|
||||
auto hhea = opt_hhea.value();
|
||||
|
||||
m_cmap = Cmap(cmap_slice.value());
|
||||
if (!opt_maxp_slice.has_value() || !(opt_maxp = Maxp::from_slice(opt_maxp_slice.value())).has_value()) {
|
||||
dbg() << "Could not load Maxp";
|
||||
return nullptr;
|
||||
}
|
||||
auto maxp = opt_maxp.value();
|
||||
|
||||
if (!opt_hmtx_slice.has_value() || !(opt_hmtx = Hmtx::from_slice(opt_hmtx_slice.value(), maxp.num_glyphs(), hhea.number_of_h_metrics())).has_value()) {
|
||||
dbg() << "Could not load Hmtx";
|
||||
return nullptr;
|
||||
}
|
||||
auto hmtx = opt_hmtx.value();
|
||||
|
||||
if (!opt_cmap_slice.has_value() || !(opt_cmap = Cmap::from_slice(opt_cmap_slice.value())).has_value()) {
|
||||
dbg() << "Could not load Cmap";
|
||||
return nullptr;
|
||||
}
|
||||
auto cmap = opt_cmap.value();
|
||||
|
||||
if (!opt_loca_slice.has_value() || !(opt_loca = Loca::from_slice(opt_loca_slice.value(), maxp.num_glyphs(), head.index_to_loc_format())).has_value()) {
|
||||
dbg() << "Could not load Loca";
|
||||
return nullptr;
|
||||
}
|
||||
auto loca = opt_loca.value();
|
||||
|
||||
if (!opt_glyf_slice.has_value()) {
|
||||
dbg() << "Could not load Glyf";
|
||||
return nullptr;
|
||||
}
|
||||
auto glyf = Glyf(opt_glyf_slice.value());
|
||||
|
||||
// Select cmap table. FIXME: Do this better. Right now, just looks for platform "Windows"
|
||||
// and corresponding encoding "Unicode full repertoire", or failing that, "Unicode BMP"
|
||||
for (u32 i = 0; i < m_cmap.num_subtables(); i++) {
|
||||
auto opt_subtable = m_cmap.subtable(i);
|
||||
for (u32 i = 0; i < cmap.num_subtables(); i++) {
|
||||
auto opt_subtable = cmap.subtable(i);
|
||||
if (!opt_subtable.has_value()) {
|
||||
continue;
|
||||
}
|
||||
auto subtable = opt_subtable.value();
|
||||
if (subtable.platform_id() == Cmap::Subtable::Platform::Windows) {
|
||||
if (subtable.encoding_id() == (u16) Cmap::Subtable::WindowsEncoding::UnicodeFullRepertoire) {
|
||||
m_cmap.set_active_index(i);
|
||||
if (subtable.encoding_id() == (u16)Cmap::Subtable::WindowsEncoding::UnicodeFullRepertoire) {
|
||||
cmap.set_active_index(i);
|
||||
break;
|
||||
}
|
||||
if (subtable.encoding_id() == (u16) Cmap::Subtable::WindowsEncoding::UnicodeBMP) {
|
||||
m_cmap.set_active_index(i);
|
||||
if (subtable.encoding_id() == (u16)Cmap::Subtable::WindowsEncoding::UnicodeBMP) {
|
||||
cmap.set_active_index(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return adopt(*new Font(move(buffer), move(head), move(hhea), move(maxp), move(hmtx), move(cmap), move(loca), move(glyf)));
|
||||
}
|
||||
|
||||
ScaledFontMetrics Font::metrics(float x_scale, float y_scale) const
|
||||
{
|
||||
Hhea hhea(m_hhea_slice);
|
||||
auto ascender = hhea.ascender() * y_scale;
|
||||
auto descender = hhea.descender() * y_scale;
|
||||
auto line_gap = hhea.line_gap() * y_scale;
|
||||
auto advance_width_max = hhea.advance_width_max() * x_scale;
|
||||
auto ascender = m_hhea.ascender() * y_scale;
|
||||
auto descender = m_hhea.descender() * y_scale;
|
||||
auto line_gap = m_hhea.line_gap() * y_scale;
|
||||
auto advance_width_max = m_hhea.advance_width_max() * x_scale;
|
||||
return ScaledFontMetrics {
|
||||
.ascender = (int) roundf(ascender),
|
||||
.descender = (int) roundf(descender),
|
||||
.line_gap = (int) roundf(line_gap),
|
||||
.advance_width_max = (int) roundf(advance_width_max),
|
||||
.ascender = (int)roundf(ascender),
|
||||
.descender = (int)roundf(descender),
|
||||
.line_gap = (int)roundf(line_gap),
|
||||
.advance_width_max = (int)roundf(advance_width_max),
|
||||
};
|
||||
}
|
||||
|
||||
// FIXME: "loca" and "glyf" are not available for CFF fonts.
|
||||
ScaledGlyphMetrics Font::glyph_metrics(u32 glyph_id, float x_scale, float y_scale) const
|
||||
{
|
||||
u32 num_glyphs = glyph_count();
|
||||
u16 number_of_h_metrics = Hhea(m_hhea_slice).number_of_h_metrics();
|
||||
auto index_to_loc_format = Head(m_head_slice).index_to_loc_format();
|
||||
Hmtx hmtx(m_hmtx_slice, num_glyphs, number_of_h_metrics);
|
||||
Loca loca(m_loca_slice, num_glyphs, index_to_loc_format);
|
||||
Glyf glyf(m_glyf_slice);
|
||||
|
||||
if (glyph_id >= num_glyphs) {
|
||||
if (glyph_id >= glyph_count()) {
|
||||
glyph_id = 0;
|
||||
}
|
||||
auto horizontal_metrics = hmtx.get_glyph_horizontal_metrics(glyph_id);
|
||||
auto glyph_offset = loca.get_glyph_offset(glyph_id);
|
||||
auto glyph = glyf.glyph(glyph_offset);
|
||||
auto horizontal_metrics = m_hmtx.get_glyph_horizontal_metrics(glyph_id);
|
||||
auto glyph_offset = m_loca.get_glyph_offset(glyph_id);
|
||||
auto glyph = m_glyf.glyph(glyph_offset);
|
||||
int ascender = glyph.ascender();
|
||||
int descender = glyph.descender();
|
||||
return ScaledGlyphMetrics {
|
||||
.ascender = (int) roundf(ascender * y_scale),
|
||||
.descender = (int) roundf(descender * y_scale),
|
||||
.advance_width = (int) roundf(horizontal_metrics.advance_width * x_scale),
|
||||
.left_side_bearing = (int) roundf(horizontal_metrics.left_side_bearing * x_scale),
|
||||
.ascender = (int)roundf(ascender * y_scale),
|
||||
.descender = (int)roundf(descender * y_scale),
|
||||
.advance_width = (int)roundf(horizontal_metrics.advance_width * x_scale),
|
||||
.left_side_bearing = (int)roundf(horizontal_metrics.left_side_bearing * x_scale),
|
||||
};
|
||||
}
|
||||
|
||||
// FIXME: "loca" and "glyf" are not available for CFF fonts.
|
||||
RefPtr<Gfx::Bitmap> Font::raster_glyph(u32 glyph_id, float x_scale, float y_scale) const
|
||||
{
|
||||
u32 num_glyphs = glyph_count();
|
||||
auto index_to_loc_format = Head(m_head_slice).index_to_loc_format();
|
||||
Loca loca(m_loca_slice, num_glyphs, index_to_loc_format);
|
||||
Glyf glyf(m_glyf_slice);
|
||||
|
||||
if (glyph_id >= num_glyphs) {
|
||||
if (glyph_id >= glyph_count()) {
|
||||
glyph_id = 0;
|
||||
}
|
||||
auto glyph_offset = loca.get_glyph_offset(glyph_id);
|
||||
auto glyph = glyf.glyph(glyph_offset);
|
||||
auto glyph_offset = m_loca.get_glyph_offset(glyph_id);
|
||||
auto glyph = m_glyf.glyph(glyph_offset);
|
||||
return glyph.raster(x_scale, y_scale, [&](u16 glyph_id) {
|
||||
if (glyph_id >= num_glyphs) {
|
||||
if (glyph_id >= glyph_count()) {
|
||||
glyph_id = 0;
|
||||
}
|
||||
auto glyph_offset = loca.get_glyph_offset(glyph_id);
|
||||
return glyf.glyph(glyph_offset);
|
||||
auto glyph_offset = m_loca.get_glyph_offset(glyph_id);
|
||||
return m_glyf.glyph(glyph_offset);
|
||||
});
|
||||
}
|
||||
|
||||
u32 Font::glyph_count() const
|
||||
{
|
||||
return Maxp(m_maxp_slice).num_glyphs();
|
||||
return m_maxp.num_glyphs();
|
||||
}
|
||||
|
||||
u16 Font::units_per_em() const
|
||||
{
|
||||
return Head(m_head_slice).units_per_em();
|
||||
return m_head.units_per_em();
|
||||
}
|
||||
|
||||
int ScaledFont::width(const StringView& string) const
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue