~ruther/CTU-FEE-B0B35APO-Semestral-project

ref: 120c16db845c6be7d839d128f9dcab24c97bd7bf CTU-FEE-B0B35APO-Semestral-project/lib-gui/src/font.c -rw-r--r-- 3.8 KiB
120c16db — František Boháček feat: add support for larger fonts 3 years ago
                                                                                
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
#include "font.h"
#include <string.h>
#include <stdio.h>
static bool font_descriptor_contains_character(font_descriptor_t **descriptor,
                                               uint32_t c);

uint32_t font_get_real_char(char *text, uint16_t *bytes) {
  *bytes = 1;
  uint8_t first_byte_offset = 0;

  if ((*text & 0xC0) == 0xC0 && (*text & 0x20) == 0) {
    first_byte_offset = 3;
    *bytes = 2;
  }

  else if ((*text & 0xE0) == 0xE0 && (*text & 0x10) == 0) {
    first_byte_offset = 4;
    *bytes = 3;
  }

  else if ((*text & 0xF0) == 0xF0 && (*text & 0x8) == 0) {
    first_byte_offset = 5;
    *bytes = 4;
  }

  else {
    *bytes = 1;
    return (uint8_t)*(uint8_t*)text;
  }

  uint32_t result = 0;
  result |= *(uint8_t*)text;
  uint32_t mask = 0;

  for (int i = 0; i < 7 - first_byte_offset; i++) {
    mask |= 1 << i;
  }
  result &= mask;

  for (int i = 1; i < *bytes; i++) {
    result <<= 6;
    uint8_t current = *(text + i);

    if ((current & 0x80) != 0x80 || (current & 0x40)) {
      // malformed or no unicode, abort
      *bytes = 1;
      return (uint8_t)*(uint8_t*)text;
    }

    result |= current & 0x3F;
  }

  return result;  
}

font_t font_create(font_descriptor_t descriptor) {
  font_t font = {
      .font = descriptor,
      .size = descriptor.height,
      .char_spacing = 0,
      .line_spacing = 0,
  };

  return font;
}

size2d_t font_measure_text(font_t *font, char *text) {
  size2d_t size = {.x = 0, .y = font->size};

  double scale = (double)font->size / font->font.height;
  size_t len = strlen(text);
  for (int i = 0; i < len && *text != '\0'; i++) {
    uint16_t bytes;
    uint32_t c = font_get_real_char(text, &bytes);
    text += bytes;

    font_character_t character = font_get_character(font, c);
    size.x += character.width * scale;
  }

  return size;
}

font_character_t font_get_character(font_t *font, uint32_t c) {
  font_descriptor_t *descriptor = &font->font;

  if (!font_descriptor_contains_character(&descriptor, c)) {
    return font_get_character(font, font->font.default_char);
  }

  uint32_t index = c - descriptor->first_char;
  uint16_t width = descriptor->max_width;
  if (descriptor->widths != NULL) {
    width = descriptor->widths[index];
  }

  uint32_t one_char_width =
      (width + sizeof(font_bits_t) * 8 - 1) / (sizeof(font_bits_t) * 8);
  uint32_t offset = (one_char_width * descriptor->height) * (index);
  if (descriptor->offsets != NULL) {
    offset = descriptor->offsets[index];
  }

  font_character_t character = {
    .width = width, .bits = descriptor->bits + offset};
  return character;
}

static bool font_descriptor_contains_character(font_descriptor_t **descriptor,
                                               uint32_t c) {
  if (*descriptor == NULL) {
    return false;
  }

  bool contains = c >= (*descriptor)->first_char &&
                  c - (*descriptor)->first_char < (*descriptor)->chars_count;

  if (!contains) {
    *descriptor = (*descriptor)->font_next_part;
    return font_descriptor_contains_character(descriptor, c);
  }

  return true;
}

bool font_contains_character(font_t *font, uint32_t c){
  font_descriptor_t *descriptor = &font->font;
  return font_descriptor_contains_character(&descriptor, c);
}

uint16_t
    font_fit_ellipsis(font_t *font, size2d_t size, char *text, char *ellipsis) {
  uint16_t ellipsis_width = font_measure_text(font, ellipsis).x;
  size.x -= ellipsis_width;

  return font_fit_cut(font, size, text);
}

uint16_t font_fit_cut(font_t *font, size2d_t size, char *text) {
  size_t len = strlen(text);

  uint16_t x_size = 0;
  double scale = (double)font->size / font->font.height;
  for (int i = 0; i < len; i++) {
    font_character_t character = font_get_character(font, text[i]);
    x_size += character.width * scale;

    if (x_size > size.x) {
      return i;
    }
  }

  return 0;
}
Do not follow this link