summaryrefslogtreecommitdiff
path: root/src/text.cc
blob: cc06508506d727cfc83ae7fb61d98f989b471605 (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
#include "text.h"

#include "cglm/types.h"
#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_attr_pos(
    struct sg_vertex_attr_state* attr,
    struct sg_vertex_buffer_layout_state* layout) {
  wayc_notnull(attr);
  wayc_notnull(layout);

  attr->format = SG_VERTEXFORMAT_FLOAT2;
  attr->buffer_index = 0;
  layout->stride = sizeof(vec2);
}

static void font_context_layout_attr_uv(
    struct sg_vertex_attr_state* attr,
    struct sg_vertex_buffer_layout_state* layout) {
  wayc_notnull(attr);
  wayc_notnull(layout);

  attr->format = SG_VERTEXFORMAT_FLOAT2;
  attr->buffer_index = 0;
  layout->stride = sizeof(vec2);
}

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());
  sg_shader shader = sg_make_shader(shader_desc);

  sg_vertex_layout_state vertex_layout = {};
  font_context_layout_attr_pos(&vertex_layout.attrs[0],
                               &vertex_layout.buffers[0]);
  font_context_layout_attr_uv(&vertex_layout.attrs[1],
                              &vertex_layout.buffers[0]);

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

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);