-
Notifications
You must be signed in to change notification settings - Fork 4
/
sixtwelve_helpers.c
59 lines (45 loc) · 1.28 KB
/
sixtwelve_helpers.c
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
#include "sixtwelve_helpers.h"
const sixtwelve_character_info *sixtwelve_get_character_info(unsigned char iso_8859_code) {
return sixtwelve_characters + iso_8859_code;
}
const unsigned char ISO_8859_SPACE = 0x20;
const unsigned char ISO_8859_NEWLINE = 0x0A;
const unsigned char ISO_8859_HYPHEN = 0x2D;
unsigned int sixtwelve_calculate_string_crass_width(const unsigned char* str) {
if (!str) {
return 0;
}
int resultingWidth = 0;
int i = 0;
while (str[i] != '\0') {
const sixtwelve_character_info* characterInfo = sixtwelve_get_character_info(str[i]);
resultingWidth += (characterInfo->width + characterInfo->x_advance);
i++;
}
return resultingWidth;
}
unsigned int sixtwelve_calculate_string_width(const unsigned char* str) {
if (!str) {
return 0;
}
int resultingWidth = 0;
int i = 0;
while (str[i] != '\0') {
// Stop measuring if we hit a space
if (str[i] == ISO_8859_SPACE) {
break;
}
// Stop measuring if we hit a newline
if (str[i] == ISO_8859_NEWLINE) {
break;
}
// Stop measuring if we hit a hypen
if (str[i] == ISO_8859_HYPHEN) {
break;
}
const sixtwelve_character_info* characterInfo = sixtwelve_get_character_info(str[i]);
resultingWidth += (characterInfo->width + characterInfo->x_advance);
i++;
}
return resultingWidth;
}