summaryrefslogtreecommitdiff
path: root/vendor/text.c
blob: 818597d455a75b3ecfc35277b2f345d87473714d (plain) (blame)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* sketchbook's text (text.h): HarfBuzz shapes, pardes's font.c rasterizes
   and gives the line box. The ten bundled faces are embedded with #embed;
   each face is opened on first use, by FreeType (font.c) and HarfBuzz's
   own OpenType functions over the same bytes, so glyph ids agree. */
#include "text.h"
#include <hb.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>

/* font.h's allocator, for FreeType and HarfBuzz (built with
   hb_malloc_impl=ui_malloc): C's heap. */
void *ui_malloc(size_t size) { return malloc(size); }
void *ui_calloc(size_t count, size_t size) { return calloc(count, size); }
void *ui_realloc(void *block, size_t size) { return realloc(block, size); }
void ui_free(void *block) { free(block); }

static const unsigned char go_mono[] = {
#embed "fonts/Go-Mono.ttf"
};
static const unsigned char go_mono_bold[] = {
#embed "fonts/Go-Mono-Bold.ttf"
};
static const unsigned char go_mono_italic[] = {
#embed "fonts/Go-Mono-Italic.ttf"
};
static const unsigned char go_regular[] = {
#embed "fonts/Go-Regular.ttf"
};
static const unsigned char go_medium[] = {
#embed "fonts/Go-Medium.ttf"
};
static const unsigned char go_bold[] = {
#embed "fonts/Go-Bold.ttf"
};
static const unsigned char jb_mono[] = {
#embed "fonts/JetBrainsMonoNL-Regular.ttf"
};
static const unsigned char jb_mono_bold[] = {
#embed "fonts/JetBrainsMonoNL-Bold.ttf"
};
static const unsigned char jb_mono_italic[] = {
#embed "fonts/JetBrainsMonoNL-Italic.ttf"
};
static const unsigned char jb_mono_medium[] = {
#embed "fonts/JetBrainsMonoNL-Medium.ttf"
};

/* ponytail: one thread. The host shapes on its main thread, a test binary
   on the runner's; a second thread would need a lock here. */
static struct {
  const uint8_t *bytes;
  size_t len;
  UIFont *ui;
  hb_font_t *hb;
} faces[SK_TEXT_MAX_FACES] = {
    {go_mono, sizeof go_mono},           {go_mono_bold, sizeof go_mono_bold},
    {go_mono_italic, sizeof go_mono_italic}, {go_regular, sizeof go_regular},
    {go_medium, sizeof go_medium},       {go_bold, sizeof go_bold},
    {jb_mono, sizeof jb_mono},           {jb_mono_bold, sizeof jb_mono_bold},
    {jb_mono_italic, sizeof jb_mono_italic}, {jb_mono_medium, sizeof jb_mono_medium},
};
static int count = SK_TEXT_FACES;
static hb_buffer_t *buffer;

/* `face` opened, or the mono for an unknown one. */
static int open_face(int face) {
  if (face < 0 || face >= count) face = SK_TEXT_MONO;
  if (!faces[face].ui) {
    faces[face].ui = ui_font_new(faces[face].bytes, (int32_t)faces[face].len);
    hb_blob_t *blob = hb_blob_create((const char *)faces[face].bytes, (unsigned)faces[face].len,
                                     HB_MEMORY_MODE_READONLY, NULL, NULL);
    hb_face_t *hb_face = hb_face_create(blob, 0);
    faces[face].hb = hb_font_create(hb_face);
    hb_face_destroy(hb_face);
    hb_blob_destroy(blob);
  }
  return face;
}

UIFont *sk_text_ui(int face) {
  if (face < 0 || face >= count) return NULL;
  return faces[open_face(face)].ui;
}

int sk_text_font(const uint8_t *bytes, size_t len) {
  if (!bytes || len == 0 || len > INT32_MAX) return -1;
  for (int i = SK_TEXT_FACES; i < count; i++)
    if (faces[i].len == len && memcmp(faces[i].bytes, bytes, len) == 0) return i;
  if (count == SK_TEXT_MAX_FACES) return -1;
  uint8_t *copy = malloc(len);
  if (!copy) return -1;
  memcpy(copy, bytes, len);
  UIFont *ui = ui_font_new(copy, (int32_t)len);
  if (!ui) {
    free(copy);
    return -1;
  }
  faces[count].bytes = copy;
  faces[count].len = len;
  faces[count].ui = ui;
  open_face(count); /* its HarfBuzz font */
  return count++;
}

/* ponytail: one fallback face, no fontconfig. JetBrains Mono NL for the Go
   faces and a sketch's own, Go Mono for JetBrains Mono's. */
static int fallback(int face) {
  return face >= SK_TEXT_CODE && face < SK_TEXT_FACES ? SK_TEXT_MONO : SK_TEXT_CODE;
}

/* The codepoint at `s` and its length; an invalid byte is U+FFFD, one byte. */
static size_t decode(const unsigned char *s, size_t n, uint32_t *cp) {
  const unsigned lead = s[0];
  const size_t len = lead < 0x80 ? 1 : (lead >> 5) == 6 ? 2 : (lead >> 4) == 14 ? 3 : (lead >> 3) == 30 ? 4 : 0;
  if (len == 0 || len > n) return *cp = 0xfffd, 1;
  uint32_t v = len == 1 ? lead : lead & (0x7f >> len);
  for (size_t i = 1; i < len; i++) {
    if ((s[i] & 0xc0) != 0x80) return *cp = 0xfffd, 1;
    v = v << 6 | (s[i] & 0x3f);
  }
  *cp = v;
  return len;
}

static int32_t whole(hb_position_t v) { return (int32_t)lroundf((float)v / 64.0f); }

static int scale_of(float px) { return (int)lroundf(fmaxf(px, 1.0f) * 64.0f); }

int32_t sk_text_advance(int face, float px, uint32_t glyph) {
  hb_font_t *hb = faces[open_face(face)].hb;
  hb_font_set_scale(hb, scale_of(px), scale_of(px));
  return whole(hb_font_get_glyph_h_advance(hb, glyph));
}

int sk_text_shape(int face, float px, const char *utf8, size_t len, SkGlyph *out, int max,
                  int32_t *advance, int32_t *ascent, int32_t *height) {
  face = open_face(face);
  const int other = open_face(fallback(face));
  int32_t cell_w, cell_h, asc;
  ui_font_cell_metrics(faces[face].ui, px, &cell_w, &cell_h, &asc);
  *ascent = asc;
  *height = cell_h;
  *advance = 0;
  if (!utf8 || len == 0 || len > INT32_MAX) return 0;
  if (!buffer) buffer = hb_buffer_create();
  const int scale = scale_of(px);
  int n = 0;
  int32_t pen = 0;
  size_t i = 0;
  while (i < len) {
    /* A segment: the codepoints one face draws. A codepoint goes to the
       fallback only when the face lacks it and the fallback has it. */
    const size_t start = i;
    int which = -1;
    while (i < len) {
      uint32_t cp;
      const size_t step = decode((const unsigned char *)utf8 + i, len - i, &cp);
      const int own = ui_font_glyph(faces[face].ui, (int32_t)cp) != 0 ||
                      ui_font_glyph(faces[other].ui, (int32_t)cp) == 0;
      const int here = own ? face : other;
      if (which < 0) which = here;
      else if (here != which) break;
      i += step;
    }
    hb_font_t *hb = faces[which].hb;
    hb_font_set_scale(hb, scale, scale);
    hb_buffer_clear_contents(buffer);
    /* The whole text as context, the segment as the item: clusters are
       byte offsets into `utf8`. */
    hb_buffer_add_utf8(buffer, utf8, (int)len, (unsigned)start, (int)(i - start));
    hb_buffer_set_direction(buffer, HB_DIRECTION_LTR); /* ponytail: no bidi */
    hb_buffer_guess_segment_properties(buffer);
    hb_shape(hb, buffer, NULL, 0);
    unsigned glyphs = 0;
    const hb_glyph_info_t *info = hb_buffer_get_glyph_infos(buffer, &glyphs);
    const hb_glyph_position_t *pos = hb_buffer_get_glyph_positions(buffer, &glyphs);
    for (unsigned k = 0; k < glyphs; k++, n++) {
      if (out && n < max)
        out[n] = (SkGlyph){info[k].codepoint, info[k].cluster, pen + whole(pos[k].x_offset),
                           -whole(pos[k].y_offset), (uint8_t)which};
      pen += whole(pos[k].x_advance);
    }
  }
  *advance = pen;
  return n;
}