summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/text.cc57
1 files changed, 55 insertions, 2 deletions
diff --git a/src/text.cc b/src/text.cc
index ab812c2..603e6fb 100644
--- a/src/text.cc
+++ b/src/text.cc
@@ -114,6 +114,11 @@ enum font_error_e wayc_font_lookup(struct font_s* font, struct atlas_s* atlas,
glyph.bearing_y = slot->bitmap_top;
glyph.advance = (f32)slot->advance.x / WAYC_SCALE;
+ glyph.uv0[0] = uv0[0];
+ glyph.uv0[1] = uv0[1];
+ glyph.uv1[0] = uv0[0] + size[0];
+ glyph.uv1[1] = uv0[1] + size[1];
+
wayc_font_cache_insert(font, codepoint, glyph);
*out = glyph;
@@ -128,6 +133,41 @@ void wayc_font_deinit(struct font_s* font) {
mi_free(font->data);
}
+static void wayc_text_assemble_one(const struct glyph_s* glyph, vec2* pen,
+ f32 atlas_width, f32 atlas_height,
+ struct text_vertex_s* out_verts,
+ text_index_t* out_indices) {
+ wayc_notnull(glyph);
+ wayc_notnull(pen);
+ wayc_notnull(out_verts);
+ wayc_notnull(out_indices);
+
+ const f32 left = WAYC_X(*pen) + glyph->bearing_x;
+ const f32 top = WAYC_Y(*pen) - glyph->bearing_y;
+ const f32 right = left + (WAYC_X(glyph->uv1) - WAYC_X(glyph->uv0));
+ const f32 bottom = top + (WAYC_Y(glyph->uv1) - WAYC_Y(glyph->uv0));
+
+ const f32 uv_left = (f32)WAYC_X(glyph->uv0) / atlas_width;
+ const f32 uv_top = (f32)WAYC_Y(glyph->uv0) / atlas_height;
+ const f32 uv_right = (f32)WAYC_X(glyph->uv1) / atlas_width;
+ const f32 uv_bottom = (f32)WAYC_Y(glyph->uv1) / atlas_height;
+
+ struct quad_s quad;
+ wayc_quad_init(&quad, left, top, right, bottom);
+
+ out_verts[0] = text_vertex_s{{left, top}, {uv_left, uv_top}};
+ out_verts[1] = text_vertex_s{{right, top}, {uv_right, uv_top}};
+ out_verts[2] = text_vertex_s{{right, bottom}, {uv_right, uv_bottom}};
+ out_verts[3] = text_vertex_s{{left, bottom}, {uv_left, uv_bottom}};
+
+ out_indices[0] = 0;
+ out_indices[1] = 1;
+ out_indices[2] = 2;
+ out_indices[3] = 2;
+ out_indices[4] = 3;
+ out_indices[5] = 0;
+}
+
enum text_error_e wayc_text_assemble(struct text_s* text, usize* out_size,
text_index_t** out_indices,
struct text_vertex_s** out_verts) {
@@ -154,6 +194,7 @@ enum text_error_e wayc_text_assemble(struct text_s* text, usize* out_size,
text_index_t* indices = (text_index_t*)mi_malloc(index_bytes);
wayc_defer_cond(mi_free(indices), success, true);
+ vec2 pen = {0, 0};
for (usize i = 0; i < glyph_count; i++) {
codepoint_t codepoint = text->string.data[i];
struct glyph_s glyph;
@@ -161,10 +202,22 @@ enum text_error_e wayc_text_assemble(struct text_s* text, usize* out_size,
text->packer, codepoint, &glyph);
if (err != FONT_ERROR_NONE) return TEXT_ERROR_ATLAS;
- wayc_panic("unimplemented");
+ f32 atlas_width = (f32)text->atlas->width;
+ f32 atlas_height = (f32)text->atlas->height;
+
+ usize vertex_offset = i * WAYC_LETTER_NVERTICES;
+ usize index_offset = i * WAYC_LETTER_NTRIANGLES * WAYC_TRIANGLE_NINDICES;
+
+ struct text_vertex_s* vertices_writer = &vertices[vertex_offset];
+ text_index_t* indices_writer = &indices[index_offset];
+
+ wayc_text_assemble_one(&glyph, &pen, atlas_width, atlas_height,
+ vertices_writer, indices_writer);
+
+ pen[0] += glyph.advance;
}
- *out_size = index_bytes;
+ *out_size = index_count;
*out_indices = indices;
*out_verts = vertices;