29 file_buffer{std::move(file_buffer)},
34 FT_ULong c = FT_Get_First_Char(face, &index);
37 this->
charset.insert(
static_cast<char32_t
>(c));
38 c = FT_Get_Next_Char(face, c, &index);
45 FT_Done_FreeType(library);
50 return FT_HAS_KERNING(face);
56 set_face_pixel_size(height);
59 metrics.
size = height;
60 metrics.
ascent = face->size->metrics.ascender / 64.0f;
61 metrics.
descent = face->size->metrics.descender / 64.0f;
62 metrics.
linespace = face->size->metrics.height / 64.0f;
64 metrics.
underline_position = FT_MulFix(face->underline_position, face->size->metrics.y_scale) / 64.0f;
65 metrics.
underline_thickness = FT_MulFix(face->underline_thickness, face->size->metrics.y_scale) / 64.0f;
67 metrics.
max_vertical_advance = FT_MulFix(face->max_advance_height, face->size->metrics.y_scale) / 64.0f;
75 set_face_pixel_size(height);
78 FT_UInt glyph_index = FT_Get_Char_Index(face,
static_cast<FT_ULong
>(code));
81 if (FT_Error error = FT_Load_Glyph(face, glyph_index, FT_LOAD_DEFAULT))
83 throw std::runtime_error(
"FreeType failed to load glyph (error code " + std::to_string(error) +
")");
87 metrics.
width = face->glyph->metrics.width / 64.0f;
88 metrics.
height = face->glyph->metrics.height / 64.0f;
99 bool ft_typeface::get_bitmap(
float height, char32_t code, std::vector<std::byte>& bitmap, std::uint32_t& bitmap_width, std::uint32_t& bitmap_height)
const
102 set_face_pixel_size(height);
105 FT_UInt glyph_index = FT_Get_Char_Index(face,
static_cast<FT_ULong
>(code));
108 if (FT_Error error = FT_Load_Glyph(face, glyph_index, FT_LOAD_RENDER | FT_LOAD_TARGET_(FT_RENDER_MODE_NORMAL)))
110 throw std::runtime_error(
"FreeType failed to load glyph (error code " + std::to_string(error) +
")");
114 bitmap.resize(face->glyph->bitmap.width * face->glyph->bitmap.rows);
115 std::memcpy(bitmap.data(), face->glyph->bitmap.buffer, bitmap.size());
117 bitmap_width =
static_cast<std::uint32_t
>(face->glyph->bitmap.width);
118 bitmap_height =
static_cast<std::uint32_t
>(face->glyph->bitmap.rows);
132 set_face_pixel_size(height);
135 const FT_UInt first_index = FT_Get_Char_Index(face,
static_cast<FT_ULong
>(first));
136 const FT_UInt second_index = FT_Get_Char_Index(face,
static_cast<FT_ULong
>(second));
140 if (FT_Error error = FT_Get_Kerning(face, first_index, second_index, FT_KERNING_DEFAULT, &kerning))
142 throw std::runtime_error(
"FreeType failed to get kerning vector (error code " + std::to_string(error) +
")");
145 offset =
math::fvec2{
static_cast<float>(kerning.x),
static_cast<float>(kerning.y)} / 64.0f;
150 void ft_typeface::set_face_pixel_size(
float height)
const
152 if (this->height == height)
157 if (FT_Error error = FT_Set_Pixel_Sizes(face, 0,
static_cast<FT_UInt
>(height)))
159 throw std::runtime_error(
"FreeType failed to set face size (error code " + std::to_string(error) +
")");
162 this->height = height;
virtual ~ft_typeface()
Destroys a FreeType typeface.
ft_typeface(FT_Library library, FT_Face face, std::unique_ptr< std::vector< FT_Byte >> file_buffer)
Creates a FreeType typeface.
virtual bool get_metrics(float height, font_metrics &metrics) const
Gets metrics for a font of the specified size.
virtual bool get_kerning(float height, char32_t first, char32_t second, math::fvec2 &offset) const
Gets the kerning offset for a pair of glyphs.
virtual bool has_kerning() const
Returns true if the typeface contains kerning information, false otherwise.
virtual bool get_bitmap(float height, char32_t code, std::vector< std::byte > &bitmap, std::uint32_t &bitmap_width, std::uint32_t &bitmap_height) const
Gets a bitmap of a glyph in a font of the specified size.
std::unordered_set< char32_t > charset
T offset(T longitude)
Calculates the UTC offset at a given longitude.
constexpr element_type & x() noexcept
Returns a reference to the first element.
constexpr element_type & y() noexcept
Returns a reference to the second element.
Metrics describing properties of a font.
float max_vertical_advance
Maximum advance height for all glyphs, for vertical layouts.
float underline_position
Vertical position of an underline.
float linespace
Baseline-to-baseline distance, computed as ascent - descent + linegap.
float linegap
Distance that must be placed between two lines of text.
float underline_thickness
Thickness of an underline.
float descent
Negative distance from the baseline to the lowest grid coordinate.
float size
Vertical size of the font.
float ascent
Positive distance from the baseline to the highest or upper grid coordinate.
float max_horizontal_advance
Maximum advance with for all glyphs, for horizontal layouts.
Metrics describing properties of a glyph.
float height
Vertical extent of the glyph.
float vertical_advance
Distance to move the pen position after the glyph has been rendered, in vertical layouts.
math::fvec2 vertical_bearing
Offset from the pen position to the glph's top-left edge, in vertical layouts.
float width
Horizontal extent of the glyph.
math::fvec2 horizontal_bearing
Offset from the pen position to the glyph's top-left edge, in horizontal layouts.
float horizontal_advance
Distance to move the pen position after the glyph has been rendered, in horizontal layouts.