1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 11:37:44 +00:00

LibTTF: Address code-style comments, gracefully handle load failures.

This commit is contained in:
Srimanta Barua 2020-06-14 18:32:39 +05:30 committed by Andreas Kling
parent ec08e9e780
commit 3b31f069f0
7 changed files with 336 additions and 248 deletions

View file

@ -26,13 +26,15 @@
#pragma once
#include "Cmap.h"
#include <AK/ByteBuffer.h>
#include <AK/Noncopyable.h>
#include <AK/RefCounted.h>
#include <AK/StringView.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/Size.h>
#include <LibTTF/Cmap.h>
#include <LibTTF/Glyf.h>
#include <LibTTF/Tables.h>
#define POINTS_PER_INCH 72.0f
#define DEFAULT_DPI 96
@ -66,6 +68,13 @@ class Font : public RefCounted<Font> {
public:
static RefPtr<Font> load_from_file(const StringView& path, unsigned index = 0);
ScaledFontMetrics metrics(float x_scale, float y_scale) const;
ScaledGlyphMetrics glyph_metrics(u32 glyph_id, float x_scale, float y_scale) const;
RefPtr<Gfx::Bitmap> raster_glyph(u32 glyph_id, float x_scale, float y_scale) const;
u32 glyph_count() const;
u16 units_per_em() const;
u32 glyph_id_for_codepoint(u32 codepoint) const { return m_cmap.glyph_id_for_codepoint(codepoint); }
private:
enum class Offsets {
NumTables = 4,
@ -78,26 +87,29 @@ private:
TableRecord = 16,
};
Font(ByteBuffer&& buffer, u32 offset);
ScaledFontMetrics metrics(float x_scale, float y_scale) const;
ScaledGlyphMetrics glyph_metrics(u32 glyph_id, float x_scale, float y_scale) const;
RefPtr<Gfx::Bitmap> raster_glyph(u32 glyph_id, float x_scale, float y_scale) const;
u32 glyph_count() const;
u16 units_per_em() const;
static RefPtr<Font> load_from_offset(ByteBuffer&&, unsigned index = 0);
Font(ByteBuffer&& buffer, Head&& head, Hhea&& hhea, Maxp&& maxp, Hmtx&& hmtx, Cmap&& cmap, Loca&& loca, Glyf&& glyf)
: m_buffer(move(buffer))
, m_head(move(head))
, m_hhea(move(hhea))
, m_maxp(move(maxp))
, m_hmtx(move(hmtx))
, m_loca(move(loca))
, m_glyf(move(glyf))
, m_cmap(move(cmap))
{
}
// This owns the font data
ByteBuffer m_buffer;
// These are all non-owning slices
ByteBuffer m_head_slice;
ByteBuffer m_hhea_slice;
ByteBuffer m_maxp_slice;
ByteBuffer m_hmtx_slice;
ByteBuffer m_loca_slice;
ByteBuffer m_glyf_slice;
// These are stateful wrappers around tables
// These are stateful wrappers around non-owning slices
Head m_head;
Hhea m_hhea;
Maxp m_maxp;
Hmtx m_hmtx;
Loca m_loca;
Glyf m_glyf;
Cmap m_cmap;
friend ScaledFont;
};
class ScaledFont {
@ -109,7 +121,7 @@ public:
m_x_scale = (point_width * dpi_x) / (POINTS_PER_INCH * units_per_em);
m_y_scale = (point_height * dpi_y) / (POINTS_PER_INCH * units_per_em);
}
u32 glyph_id_for_codepoint(u32 codepoint) const { return m_font->m_cmap.glyph_id_for_codepoint(codepoint); }
u32 glyph_id_for_codepoint(u32 codepoint) const { return m_font->glyph_id_for_codepoint(codepoint); }
ScaledFontMetrics metrics() const { return m_font->metrics(m_x_scale, m_y_scale); }
ScaledGlyphMetrics glyph_metrics(u32 glyph_id) const { return m_font->glyph_metrics(glyph_id, m_x_scale, m_y_scale); }
RefPtr<Gfx::Bitmap> raster_glyph(u32 glyph_id) const { return m_font->raster_glyph(glyph_id, m_x_scale, m_y_scale); }
@ -120,10 +132,8 @@ public:
private:
RefPtr<Font> m_font;
float m_x_scale;
float m_y_scale;
friend Font;
float m_x_scale { 0.0 };
float m_y_scale { 0.0 };
};
}