#include "text.h" #include #include "freetype/freetype.h" #include "shaders.h" #include "utils.h" static void font_context_sampler_desc(struct sg_sampler_desc* desc) { wayc_notnull(desc); desc->min_filter = SG_FILTER_LINEAR; desc->mag_filter = SG_FILTER_LINEAR; desc->wrap_u = SG_WRAP_CLAMP_TO_EDGE; desc->wrap_v = SG_WRAP_CLAMP_TO_EDGE; } static void font_context_layout(struct sg_vertex_layout_state* layout) { wayc_notnull(layout); layout->buffers[0].stride = sizeof(text_vertex_s); layout->attrs[ATTR_text_in_position].format = SG_VERTEXFORMAT_FLOAT2; layout->attrs[ATTR_text_in_position].buffer_index = 0; layout->attrs[ATTR_text_in_position].offset = offsetof(text_vertex_s, pos); layout->attrs[ATTR_text_in_uv].format = SG_VERTEXFORMAT_FLOAT2; layout->attrs[ATTR_text_in_uv].buffer_index = 0; layout->attrs[ATTR_text_in_uv].offset = offsetof(text_vertex_s, uv); } static void font_context_color(struct sg_color_target_state* color) { wayc_notnull(color); struct sg_blend_state blend = {}; blend.enabled = true; blend.src_factor_rgb = SG_BLENDFACTOR_SRC_ALPHA; blend.dst_factor_rgb = SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA; blend.src_factor_alpha = SG_BLENDFACTOR_SRC_ALPHA; blend.dst_factor_alpha = SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA; color->blend = blend; } static void font_context_pipeline_desc(struct sg_pipeline_desc* desc) { wayc_notnull(desc); const struct sg_shader_desc* shader_desc = text_shader_desc(sg_query_backend()); struct sg_shader shader = sg_make_shader(shader_desc); struct sg_vertex_layout_state vertex_layout = {}; font_context_layout(&vertex_layout); struct sg_color_target_state color = {}; font_context_color(&color); desc->layout = vertex_layout; desc->shader = shader; desc->colors[0] = color; desc->color_count = 1; } enum font_context_error_e font_context_init(struct font_context_s* context) { wayc_notnull(context); FT_Library ft; FT_Error fterr = FT_Init_FreeType(&ft); if (fterr) return FONT_CONTEXT_ERROR_LOAD_LIBRARY; struct sg_sampler_desc sampler_desc = {}; font_context_sampler_desc(&sampler_desc); struct sg_pipeline_desc pipeline_desc = {}; font_context_pipeline_desc(&pipeline_desc); context->ft = ft; context->sampler = sg_make_sampler(&sampler_desc); context->pipeline = sg_make_pipeline(&pipeline_desc); return FONT_CONTEXT_ERROR_NONE; } void font_context_deinit(struct font_context_s* context) { wayc_notnull(context); sg_destroy_pipeline(context->pipeline); sg_destroy_sampler(context->sampler); FT_Done_FreeType(context->ft); }