-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutf8.h
More file actions
46 lines (35 loc) · 1.05 KB
/
utf8.h
File metadata and controls
46 lines (35 loc) · 1.05 KB
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
#ifndef _UTF8_UTILS_
#define _UTF8_UTILS_
// Copyright Patrick Louis
// https: // venam.net/blog/unix/2018/09/02/fonts-xcb.html
// https: // github.com/venam/fonts-for-xcb
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fontconfig/fontconfig.h>
struct utf_holder {
FcChar32 *str;
unsigned int length;
};
struct utf_holder char_to_uint32(char *str);
void utf_holder_destroy(struct utf_holder holder);
struct utf_holder char_to_uint32(char *str) {
struct utf_holder holder;
FcChar32 *output = NULL;
int length = 0, shift = 0;
// there should be less than or same as the strlen of str
output = (FcChar32 *)malloc(sizeof(FcChar32) * strlen(str));
if (!output) {
puts("couldn't allocate mem for char_to_uint32");
}
while (*(str + shift)) {
shift += FcUtf8ToUcs4((FcChar8 *)(str + shift), output + length,
strlen(str) - shift);
length++;
}
holder.length = length;
holder.str = output;
return holder;
}
void utf_holder_destroy(struct utf_holder holder) { free(holder.str); }
#endif