aboutsummaryrefslogtreecommitdiff
path: root/src/renderer.c
blob: 6abb159755249919cddf7fb184110af56f15a44d (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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#include <stdio.h>
#include <stdbool.h>
#include <assert.h>
#include <math.h>
#include "renderer.h"
#include "font_renderer.h"

#define MAX_GLYPHSET 256

struct RenImage {
  RenColor *pixels;
  int width, height;
};

// Important: when a subpixel scale is used the width below will be the width in logical pixel.
// As each logical pixel contains 3 subpixels it means that the 'pixels' pointer
// will hold enough space for '3 * width' uint8_t values.
typedef struct {
  uint8_t *pixels;
  int width, height;
} RenCoverageImage;

struct GlyphSet {
  RenCoverageImage *coverage;
  GlyphBitmapInfo glyphs[256];
};
typedef struct GlyphSet GlyphSet;

struct RenFont {
  void *data;
  GlyphSet *sets[MAX_GLYPHSET];
  float size;
  int height;
  FontRenderer *renderer;
};


static SDL_Window *window;
static struct { int left, top, right, bottom; } clip;


static void* check_alloc(void *ptr) {
  if (!ptr) {
    fprintf(stderr, "Fatal error: memory allocation failed\n");
    exit(EXIT_FAILURE);
  }
  return ptr;
}


static const char* utf8_to_codepoint(const char *p, unsigned *dst) {
  unsigned res, n;
  switch (*p & 0xf0) {
    case 0xf0 :  res = *p & 0x07;  n = 3;  break;
    case 0xe0 :  res = *p & 0x0f;  n = 2;  break;
    case 0xd0 :
    case 0xc0 :  res = *p & 0x1f;  n = 1;  break;
    default   :  res = *p;         n = 0;  break;
  }
  while (n--) {
    res = (res << 6) | (*(++p) & 0x3f);
  }
  *dst = res;
  return p + 1;
}


void ren_init(SDL_Window *win) {
  assert(win);
  window = win;
  SDL_Surface *surf = SDL_GetWindowSurface(window);
  ren_set_clip_rect( (RenRect) { 0, 0, surf->w, surf->h } );
}


void ren_update_rects(RenRect *rects, int count) {
  SDL_UpdateWindowSurfaceRects(window, (SDL_Rect*) rects, count);
  static bool initial_frame = true;
  if (initial_frame) {
    SDL_ShowWindow(window);
    initial_frame = false;
  }
}


void ren_set_clip_rect(RenRect rect) {
  clip.left   = rect.x;
  clip.top    = rect.y;
  clip.right  = rect.x + rect.width;
  clip.bottom = rect.y + rect.height;
}


void ren_get_size(int *x, int *y) {
  SDL_Surface *surf = SDL_GetWindowSurface(window);
  *x = surf->w;
  *y = surf->h;
}


RenImage* ren_new_image(int width, int height) {
  assert(width > 0 && height > 0);
  RenImage *image = malloc(sizeof(RenImage) + width * height * sizeof(RenColor));
  check_alloc(image);
  image->pixels = (void*) (image + 1);
  image->width = width;
  image->height = height;
  return image;
}

RenCoverageImage* ren_new_coverage(int width, int height, int subpixel_scale) {
  assert(width > 0 && height > 0);
  RenCoverageImage *image = malloc(sizeof(RenCoverageImage) + width * height * subpixel_scale * sizeof(uint8_t));
  check_alloc(image);
  image->pixels = (void*) (image + 1);
  image->width = width;
  image->height = height;
  return image;
}

void ren_free_image(RenImage *image) {
  free(image);
}

void ren_free_coverage(RenCoverageImage *coverage) {
  free(coverage);
}

#ifdef FONT_RENDERER_DEBUG
void debug_bitmap_draw_rect(RenImage *image, GlyphBitmapInfo *glyph) {
  const int subpixel = 3;
  const int x0 = glyph->x0, x1 = glyph->x1;
  const int y0 = glyph->y0, y1 = glyph->y1;
  const int w = image->width;
  RenColor *c = image->pixels + y0 * w;
  for (int x = x0 * subpixel; x < x1 * subpixel; x++) {
    c[x].g = c[x].g >> 1;
    c[x].b = c[x].b >> 1;
  }
  c = image->pixels + x1 * subpixel;
  for (int y = y0; y < y1; y++) {
    c[y * w].g = c[y * w].g >> 1;
    c[y * w].b = c[y * w].b >> 1;
  }
  c = image->pixels + y1 * w;
  for (int x = x0 * subpixel; x < x1 * subpixel; x++) {
    c[x].g = c[x].g >> 1;
    c[x].b = c[x].b >> 1;
  }
  c = image->pixels + x0 * subpixel;
  for (int y = y0 + 1; y < y1; y++) {
    c[y * w].g = c[y * w].g >> 1;
    c[y * w].b = c[y * w].b >> 1;
  }
  c = image->pixels + glyph->ybaseline * w;
  for (int x = x0 * subpixel; x < x1 * subpixel; x++) {
    c[x].g = c[x].g >> 1;
    c[x].r = c[x].r >> 1;
  }
  const int x_or = x0 - glyph->xoff, y_or = y0 - glyph->yoff;
  c = image->pixels + y_or * w;
  for (int x = x_or * subpixel; x < x1 * subpixel; x++) {
    c[x].b = c[x].b >> 1;
    c[x].r = c[x].r >> 1;
  }
  c = image->pixels + x_or * subpixel;
  for (int y = y_or; y < y1; y++) {
    c[y * w].b = c[y * w].b >> 1;
    c[y * w].r = c[y * w].r >> 1;
  }
}
#endif

static GlyphSet* load_glyphset(RenFont *font, int idx) {
  GlyphSet *set = check_alloc(calloc(1, sizeof(GlyphSet)));

  const int subpixel_scale = 3;

  /* init image */
  int width = 128;
  int height = 128;
retry:
  set->coverage = ren_new_coverage(width, height, subpixel_scale);

  int res = FontRendererBakeFontBitmap(font->renderer, font->height,
    (void *) set->coverage->pixels, width, height,
    idx << 8, 256, set->glyphs, subpixel_scale);

  /* retry with a larger image buffer if the buffer wasn't large enough */
  if (res < 0) {
    width *= 2;
    height *= 2;
    ren_free_coverage(set->coverage);
    goto retry;
  }

#ifdef FONT_RENDERER_DEBUG
  static int debug_image_index = 1;
  if (idx == 0) {
    RenImage *debug_cov_image = ren_new_image(width * subpixel_scale, height);
    for (int h = 0; h < height; h++) {
      RenColor *d = debug_cov_image->pixels + h * width * subpixel_scale;
      uint8_t *s = set->coverage->pixels + h * subpixel_scale * width;
      for (int w = 0; w < width * subpixel_scale; w++) {
        uint8_t cover = s[w];
        d[w] = (RenColor) {.b = 0xff - cover, .g = 0xff - cover, .r = 0xff - cover, .a = 0xff};
      }
    }
    for (int i = 0; i < 256; i++) {
      debug_bitmap_draw_rect(debug_cov_image, &set->glyphs[i]);
    }
    SDL_Surface *surface = SDL_CreateRGBSurfaceWithFormatFrom(
        debug_cov_image->pixels,
        width * subpixel_scale, height, 32, width * subpixel_scale * 4,
        SDL_PIXELFORMAT_RGBA32);
    char img_filename[64];
    sprintf(img_filename, "agg-glyphset-%03d.bmp", debug_image_index);
    SDL_SaveBMP(surface, img_filename);
    SDL_FreeSurface(surface);
    ren_free_image(debug_cov_image);
    debug_image_index++;
  }
#endif

  /* adjust glyph's xadvance */
  for (int i = 0; i < 256; i++) {
    set->glyphs[i].xadvance = floor(set->glyphs[i].xadvance + 0.5);
  }

  return set;
}


static GlyphSet* get_glyphset(RenFont *font, int codepoint) {
  int idx = (codepoint >> 8) % MAX_GLYPHSET;
  if (!font->sets[idx]) {
    font->sets[idx] = load_glyphset(font, idx);
  }
  return font->sets[idx];
}


RenFont* ren_load_font(const char *filename, float size) {
  RenFont *font = NULL;

  /* init font */
  font = check_alloc(calloc(1, sizeof(RenFont)));
  font->size = size;

  const float gamma = 1.5;
  font->renderer = FontRendererNew(FONT_RENDERER_HINTING|FONT_RENDERER_SUBPIXEL, gamma);
  if (FontRendererLoadFont(font->renderer, filename)) {
    free(font);
    return NULL;
  }
  font->height = FontRendererGetFontHeight(font->renderer, size);

  /* make tab and newline glyphs invisible */
  GlyphBitmapInfo *g = get_glyphset(font, '\n')->glyphs;
  g['\t'].x1 = g['\t'].x0;
  g['\n'].x1 = g['\n'].x0;

  return font;
}


void ren_free_font(RenFont *font) {
  for (int i = 0; i < MAX_GLYPHSET; i++) {
    GlyphSet *set = font->sets[i];
    if (set) {
      ren_free_coverage(set->coverage);
      free(set);
    }
  }
  FontRendererFree(font->renderer);
  free(font);
}


void ren_set_font_tab_width(RenFont *font, int n) {
  GlyphSet *set = get_glyphset(font, '\t');
  set->glyphs['\t'].xadvance = n;
}


int ren_get_font_width(RenFont *font, const char *text) {
  int x = 0;
  const char *p = text;
  unsigned codepoint;
  while (*p) {
    p = utf8_to_codepoint(p, &codepoint);
    GlyphSet *set = get_glyphset(font, codepoint);
    GlyphBitmapInfo *g = &set->glyphs[codepoint & 0xff];
    x += g->xadvance;
  }
  return x;
}


int ren_get_font_height(RenFont *font) {
  return font->height;
}


static inline RenColor blend_pixel(RenColor dst, RenColor src) {
  int ia = 0xff - src.a;
  dst.r = ((src.r * src.a) + (dst.r * ia)) >> 8;
  dst.g = ((src.g * src.a) + (dst.g * ia)) >> 8;
  dst.b = ((src.b * src.a) + (dst.b * ia)) >> 8;
  return dst;
}


static inline RenColor blend_pixel2(RenColor dst, RenColor src, RenColor color) {
  src.a = (src.a * color.a) >> 8;
  int ia = 0xff - src.a;
  dst.r = ((src.r * color.r * src.a) >> 16) + ((dst.r * ia) >> 8);
  dst.g = ((src.g * color.g * src.a) >> 16) + ((dst.g * ia) >> 8);
  dst.b = ((src.b * color.b * src.a) >> 16) + ((dst.b * ia) >> 8);
  return dst;
}


#define rect_draw_loop(expr)        \
  for (int j = y1; j < y2; j++) {   \
    for (int i = x1; i < x2; i++) { \
      *d = expr;                    \
      d++;                          \
    }                               \
    d += dr;                        \
  }

void ren_draw_rect(RenRect rect, RenColor color) {
  if (color.a == 0) { return; }

  int x1 = rect.x < clip.left ? clip.left : rect.x;
  int y1 = rect.y < clip.top  ? clip.top  : rect.y;
  int x2 = rect.x + rect.width;
  int y2 = rect.y + rect.height;
  x2 = x2 > clip.right  ? clip.right  : x2;
  y2 = y2 > clip.bottom ? clip.bottom : y2;

  SDL_Surface *surf = SDL_GetWindowSurface(window);
  RenColor *d = (RenColor*) surf->pixels;
  d += x1 + y1 * surf->w;
  int dr = surf->w - (x2 - x1);

  if (color.a == 0xff) {
    rect_draw_loop(color);
  } else {
    rect_draw_loop(blend_pixel(*d, color));
  }
}

static void clip_point_inside_rect(int *x, int *y, RenRect *sub) {
  /* clip */
  int n;
  if ((n = clip.left - (*x)) > 0) { sub->width  -= n; sub->x += n; (*x) += n; }
  if ((n = clip.top  - (*y)) > 0) { sub->height -= n; sub->y += n; (*y) += n; }
  if ((n = (*x) + sub->width  - clip.right ) > 0) { sub->width  -= n; }
  if ((n = (*y) + sub->height - clip.bottom) > 0) { sub->height -= n; }
}

void ren_draw_image(RenImage *image, RenRect *sub, int x, int y, RenColor color) {
  if (color.a == 0) { return; }

  clip_point_inside_rect(&x, &y, sub);

  if (sub->width <= 0 || sub->height <= 0) {
    return;
  }

  /* draw */
  SDL_Surface *surf = SDL_GetWindowSurface(window);
  RenColor *s = image->pixels;
  RenColor *d = (RenColor*) surf->pixels;
  s += sub->x + sub->y * image->width;
  d += x + y * surf->w;
  int sr = image->width - sub->width;
  int dr = surf->w - sub->width;

  for (int j = 0; j < sub->height; j++) {
    for (int i = 0; i < sub->width; i++) {
      *d = blend_pixel2(*d, *s, color);
      d++;
      s++;
    }
    d += dr;
    s += sr;
  }
}

static void ren_draw_coverage_with_color(FontRenderer *renderer, RenCoverageImage *image, RenRect *sub, int x, int y, RenColor color) {
  if (color.a == 0) { return; }

  clip_point_inside_rect(&x, &y, sub);

  if (sub->width <= 0 || sub->height <= 0) {
    return;
  }

  /* draw */
  SDL_Surface *surf = SDL_GetWindowSurface(window);
  const int subpixel_scale = 3;
  uint8_t *s = image->pixels;
  RenColor *d = (RenColor*) surf->pixels;
  s += (sub->x + sub->y * image->width) * subpixel_scale;
  d += x + y * surf->w;
  const int surf_pixel_size = 4;
  FontRendererBlendGammaSubpixel(
    renderer,
    (uint8_t *) d, surf->w * surf_pixel_size,
    s, image->width * subpixel_scale,
    sub->width, sub->height,
    (FontRendererColor) { .r = color.r, .g = color.g, .b = color.b });
}

int ren_draw_text(RenFont *font, const char *text, int x, int y, RenColor color) {
  RenRect rect;
  const char *p = text;
  unsigned codepoint;
  while (*p) {
    p = utf8_to_codepoint(p, &codepoint);
    GlyphSet *set = get_glyphset(font, codepoint);
    GlyphBitmapInfo *g = &set->glyphs[codepoint & 0xff];
    rect.x = g->x0;
    rect.y = g->y0;
    rect.width = g->x1 - g->x0;
    rect.height = g->y1 - g->y0;
    ren_draw_coverage_with_color(font->renderer, set->coverage, &rect, x + g->xoff, y + g->yoff, color);
    x += g->xadvance;
  }
  return x;
}