summaryrefslogtreecommitdiff
path: root/src/text.h
blob: c93f968304a1a570dee3b08d1363dbb51183aa4a (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
#pragma once

#include "atlas.h"
#include "cglm/types.h"
#include "freetype/freetype.h"
#include "hash.h"
#include "sokol_gfx.h"
#include "utils.h"

struct text_vertex_s {
  vec2 pos;
  vec2 uv;
};

typedef u32 codepoint_t;

enum font_context_error_e : u8 {
  FONT_CONTEXT_ERROR_NONE = 0,
  FONT_CONTEXT_ERROR_LOAD_LIBRARY,
  FONT_CONTEXT_ERROR_CREATE_SAMPLER,
  FONT_CONTEXT_ERROR_CREATE_SHADER,
  FONT_CONTEXT_ERROR_CREATE_PIPELINE,
};

struct font_context_s {
  FT_Library ft;
  struct sg_pipeline pipeline;
  struct sg_sampler sampler;
};

enum font_context_error_e wayc_font_context_init(
    struct font_context_s* context);
void wayc_font_context_deinit(struct font_context_s* context);

struct glyph_s {
  vec2 uv0, uv1;
  vec2 bearing;
  f32 advance;
};

static inline void glyph_print(struct glyph_s* glyph) {
  u32 uv0x = WAYC_X(glyph->uv0);
  u32 uv0y = WAYC_Y(glyph->uv0);
  u32 uv1x = WAYC_X(glyph->uv1);
  u32 uv1y = WAYC_Y(glyph->uv1);
  fprintf(stderr,
          "uv0: (%u, %u), uv1: (%u, %u), bearing: (%f, %f), advance: %f\n",
          uv0x, uv0y, uv1x, uv1y, WAYC_X(glyph->bearing),
          WAYC_Y(glyph->bearing), glyph->advance);
}

enum font_error_e : u8 {
  FONT_ERROR_NONE = 0,
  FONT_ERROR_LOAD_FACE,
  FONT_ERROR_ATLAS_FAILED,
  FONT_ERROR_LOAD_GLYPH,
};

struct font_s {
  FT_Face face;
  u32 font_size;

  struct atlas_s atlas;
  struct hashmap_s<codepoint_t, struct glyph_s> cached;
};

enum font_error_e wayc_font_init(struct font_s* font,
                                 struct font_context_s* context,
                                 const char* path, u32 font_size,
                                 u32 atlas_width, u32 atlas_height);
enum font_error_e wayc_font_render(struct font_s* font, codepoint_t codepoint,
                                   struct glyph_s* glyph);
static inline enum font_error_e wayc_font_flush(struct font_s* font) {
  enum atlas_error_e atlas_err = wayc_atlas_flush(&font->atlas);
  return atlas_err == ATLAS_ERROR_NONE ? FONT_ERROR_NONE
                                       : FONT_ERROR_ATLAS_FAILED;
}
void wayc_font_deinit(struct font_s* font);