summaryrefslogtreecommitdiff
path: root/font.c
blob: 10d9499752a4b276f0769ec57e838a00a64ecce3 (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
/*
 * Copyright (C) 2021 Mikhail Burakov. This file is part of Pui.
 *
 * Pui is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Pui is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Pui.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "font.h"

extern uint32_t kCp00[256 * 3];
extern uint32_t kCp04[256 * 3];

static int GetUnicode(const char** str) {
  const char* character = *str;
  if ((character[0] & 0xf8) == 0xf0 && (character[1] & 0xc0) == 0x80 &&
      (character[2] & 0xc0) == 0x80 && (character[3] & 0xc0) == 0x80) {
    (*str) += 4;
    return ((character[0] & 0x07) << 18) | ((character[1] & 0x3f) << 12) |
           ((character[2] & 0x3f) << 6) | (character[3] & 0x3f);
  }
  if ((character[0] & 0xf0) == 0xe0 && (character[1] & 0xc0) == 0x80 &&
      (character[2] & 0xc0) == 0x80) {
    (*str) += 3;
    return ((character[0] & 0x0f) << 12) | ((character[1] & 0x3f) << 6) |
           (character[2] & 0x3f);
  }
  if ((character[0] & 0xe0) == 0xc0 && (character[1] & 0xc0) == 0x80) {
    (*str) += 2;
    return ((character[0] & 0x1f) << 6) | (character[1] & 0x3f);
  }
  if ((character[0] & 0x80) == 0x00) {
    (*str) += 1;
    return character[0];
  }
  return -1;
}

static size_t GlyphWidth(int point) {
  const uint32_t* page;
  switch (point >> 8) {
    case 0:
      if (point == 0x20) return 4;
      page = kCp00;
      break;
    case 4:
      page = kCp04;
      point &= 0xff;
      break;
    default:
      return 0;
  }
  uint32_t glyph = page[point * 3];
  return (glyph >> 24) + 1;
}

static void BlockRender(uint32_t block, uint32_t* buffer, size_t stride,
                        uint32_t color) {
  for (size_t counter = 31; block; block >>= 1, counter--) {
    if (!(block & 1)) continue;
    size_t x = counter & 0x7u;
    size_t y = counter >> 3u;
    buffer[x + y * stride] = color;
  }
}

static void GlyphRender(int point, uint32_t* buffer, size_t stride,
                        uint32_t color) {
  const uint32_t* page;
  switch (point >> 8) {
    case 0:
      if (point == 0x20) return;
      page = kCp00;
      break;
    case 4:
      page = kCp04;
      point &= 0xff;
      break;
    default:
      return;
  }
  const uint32_t* glyph = page + point * 3;
  BlockRender(glyph[2], buffer + stride * 7, stride, color);
  BlockRender(glyph[1], buffer + stride * 3, stride, color);
  BlockRender(glyph[0] & 0xffffff, buffer - stride, stride, color);
}

size_t PuiStringWidth(const char* str) {
  size_t result = 0;
  for (;;) {
    int cp = GetUnicode(&str);
    if (cp < 1) break;
    result += GlyphWidth(cp);
  }
  return result;
}

size_t PuiStringCut(const char* str, size_t width) {
  size_t result = 0;
  for (size_t current = 0;;) {
    const char* before = str;
    int cp = GetUnicode(&str);
    if (cp < 1) break;
    current += GlyphWidth(cp);
    if (current >= width) break;
    result += (size_t)(str - before);
  }
  return result;
}

void PuiStringRender(const char* str, void* buffer, size_t stride,
                     uint32_t color) {
  for (uint32_t* ptr = buffer;;) {
    int cp = GetUnicode(&str);
    if (cp < 1) break;
    GlyphRender(cp, ptr, stride, color);
    ptr += GlyphWidth(cp);
  }
}