summaryrefslogtreecommitdiff
path: root/src/text.cc
blob: ab2155f1765ee6535301f363436f0fef7ae9a3f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "text.h"

#include <cstddef>

#include "atlas.h"
#include "freetype/freetype.h"
#include "shaders.h"
#include "utils.h"

static void wayc_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 wayc_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 wayc_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,
                                       struct sg_shader shader) {
  wayc_notnull(desc);

  struct sg_vertex_layout_state vertex_layout = {};
  wayc_font_context_layout(&vertex_layout);

  struct sg_color_target_state color = {};
  wayc_font_context_color(&color);

  desc->layout = vertex_layout;
  desc->shader = shader;

  desc->colors[0] = color;
  desc->color_count = 1;
}

enum font_context_error_e wayc_font_context_init(
    struct font_context_s* context) {
  wayc_notnull(context);

  bool success = false;

  FT_Library ft;
  FT_Error fterr = FT_Init_FreeType(&ft);
  if (fterr) return FONT_CONTEXT_ERROR_LOAD_LIBRARY;
  wayc_defer_cond(FT_Done_FreeType(ft), success, true);

  struct sg_shader shader =
      sg_make_shader(text_shader_desc(sg_query_backend()));
  if (sg_query_shader_state(shader) != SG_RESOURCESTATE_VALID)
    return FONT_CONTEXT_ERROR_CREATE_SHADER;
  wayc_defer_cond(sg_destroy_shader(shader), success, true);

  struct sg_sampler_desc sampler_desc = {};
  wayc_font_context_sampler_desc(&sampler_desc);

  struct sg_pipeline_desc pipeline_desc = {};
  font_context_pipeline_desc(&pipeline_desc, shader);

  struct sg_sampler sampler = sg_make_sampler(&sampler_desc);
  if (sg_query_sampler_state(sampler) != SG_RESOURCESTATE_VALID)
    return FONT_CONTEXT_ERROR_CREATE_SAMPLER;

  wayc_defer_cond(sg_destroy_sampler(sampler), success, true);

  struct sg_pipeline pipeline = sg_make_pipeline(&pipeline_desc);
  if (sg_query_pipeline_state(pipeline) != SG_RESOURCESTATE_VALID)
    return FONT_CONTEXT_ERROR_CREATE_PIPELINE;

  context->ft = ft;
  context->sampler = sampler;
  context->pipeline = pipeline;

  success = true;

  return FONT_CONTEXT_ERROR_NONE;
}

void wayc_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);
}