MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ rasterize()

bool MayaFlux::Portal::Text::GlyphAtlas::rasterize ( FT_UInt  glyph_index)
private

Definition at line 58 of file GlyphAtlas.cpp.

59{
60 FT_Face face = m_face.get_face();
61
62 if (const FT_Error err = FT_Set_Pixel_Sizes(face, 0, m_pixel_size); err != 0) {
64 "FT_Set_Pixel_Sizes({}) failed: {}", m_pixel_size, static_cast<int>(err));
65 return false;
66 }
67
68 if (const FT_Error err = FT_Load_Glyph(face, glyph_index, FT_LOAD_RENDER); err != 0) {
70 "FT_Load_Glyph({}) failed: {}", glyph_index, static_cast<int>(err));
71 return false;
72 }
73
74 const FT_Bitmap& bmp = face->glyph->bitmap;
75 const uint32_t gw = bmp.width;
76 const uint32_t gh = bmp.rows;
77
78 constexpr uint32_t k_pad = 1;
79
80 if (m_cursor_x + gw + k_pad > m_atlas_size) {
81 m_cursor_x = 0;
82 m_cursor_y += m_shelf_height + k_pad;
84 }
85
86 if (m_cursor_y + gh + k_pad > m_atlas_size) {
88 "GlyphAtlas full at pixel_size={} atlas_size={}. "
89 "Construct with a larger atlas_size.",
91 return false;
92 }
93
94 const std::span<uint8_t> pixels = m_texture->pixel_bytes(0);
95 if (pixels.empty()) {
97 "GlyphAtlas: TextureContainer pixel_bytes returned empty span");
98 return false;
99 }
100
101 const uint32_t stride = m_atlas_size;
102
103 for (uint32_t row = 0; row < gh; ++row) {
104 const uint8_t* src = bmp.buffer + static_cast<size_t>(row * static_cast<uint32_t>(std::abs(bmp.pitch)));
105 uint8_t* dst = pixels.data() + static_cast<size_t>((m_cursor_y + row) * stride) + m_cursor_x;
106 std::memcpy(dst, src, gw);
107 }
108
109 const float inv = 1.F / static_cast<float>(m_atlas_size);
110
111 GlyphMetrics m;
112 m.uv_x0 = static_cast<float>(m_cursor_x) * inv;
113 m.uv_y0 = static_cast<float>(m_cursor_y) * inv;
114 m.uv_x1 = static_cast<float>(m_cursor_x + gw) * inv;
115 m.uv_y1 = static_cast<float>(m_cursor_y + gh) * inv;
116 m.bearing_x = face->glyph->bitmap_left;
117 m.bearing_y = face->glyph->bitmap_top;
118 m.width = gw;
119 m.height = gh;
120 m.advance_x = static_cast<int32_t>(face->glyph->advance.x >> 6);
121
122 m_cache.emplace(glyph_index, m);
123
124 m_cursor_x += gw + k_pad;
125 if (gh > m_shelf_height) {
126 m_shelf_height = gh;
127 }
128
129 m_dirty = true;
130 return true;
131}
#define MF_ERROR(comp, ctx,...)
const std::vector< float > * pixels
FT_Face get_face() const
Raw FT_Face handle for use by GlyphAtlas.
Definition FontFace.hpp:59
std::unique_ptr< Kakshya::TextureContainer > m_texture
std::unordered_map< FT_UInt, GlyphMetrics > m_cache
@ API
API calls from external code.
@ Portal
High-level user-facing API layer.

References MayaFlux::Portal::Text::GlyphMetrics::advance_x, MayaFlux::Journal::API, MayaFlux::Portal::Text::GlyphMetrics::bearing_x, MayaFlux::Portal::Text::GlyphMetrics::bearing_y, MayaFlux::Portal::Text::FontFace::get_face(), MayaFlux::Portal::Text::GlyphMetrics::height, m_atlas_size, m_cache, m_cursor_x, m_cursor_y, m_dirty, m_face, m_pixel_size, m_shelf_height, m_texture, MF_ERROR, pixels, MayaFlux::Journal::Portal, MayaFlux::Portal::Text::GlyphMetrics::uv_x0, MayaFlux::Portal::Text::GlyphMetrics::uv_x1, MayaFlux::Portal::Text::GlyphMetrics::uv_y0, MayaFlux::Portal::Text::GlyphMetrics::uv_y1, and MayaFlux::Portal::Text::GlyphMetrics::width.

Referenced by get_or_rasterize().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: