summaryrefslogtreecommitdiff
path: root/fw/fe310/eos/eve/eve_font.c
blob: 8f272bc5c50c073fd5feac90bb43f62bea7bab7a (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
#include <stdlib.h>

#include "eve.h"
#include "eve_font.h"

void eve_font_init(EVEFont *font, uint8_t font_id) {
    uint32_t p;

    p = eve_read32(EVE_ROM_FONT_ADDR);
    p += (148 * (font_id - 16));
    font->id = font_id;
    font->w = eve_read32(p + 136);
    font->h = eve_read32(p + 140);
    eve_readb(p, font->w_ch, 128);
}

int eve_font_ch_w(EVEFont *font, ucp_t ch) {
    if (ch < 128) return font->w_ch[ch];
    return EVE_ERR;
}

static int font_str_w(EVEFont *font, utf8_t *str, int *_str_w, size_t *_str_len) {
    uint16_t r = 0;
    size_t len = 0;
    ucp_t ch;
    int ch_w;
    int ch_l;

    while (str[len]) {
        ch_l = utf8_dec(str + len, &ch);
        ch_w = eve_font_ch_w(font, ch);
        if (ch_w < 0) {
            if (_str_w) *_str_w = r;
            if (_str_len) *_str_len = len;
            return EVE_ERR;
        }
        r += ch_w;
        len += ch_l;
    }

    if (_str_w) *_str_w = r;
    if (_str_len) *_str_len = len;
    return EVE_OK;
}

static int font_buf_w(EVEFont *font, utf8_t *buf, size_t buf_len, int *_buf_w, size_t *_buf_len) {
    uint16_t r = 0;
    size_t len = 0;
    ucp_t ch;
    int ch_w;
    int ch_l;

    while (len < buf_len) {
        ch_l = utf8_dec(buf + len, &ch);
        ch_w = eve_font_ch_w(font, ch);
        if (ch_w < 0) {
            if (_buf_w) *_buf_w = r;
            if (_buf_len) *_buf_len = len;
            return EVE_ERR;
        }
        r += ch_w;
        len += ch_l;
    }

    if (_buf_w) *_buf_w = r;
    if (_buf_len) *_buf_len = len;
    return EVE_OK;
}

int eve_font_verify(EVEFont *font, utf8_t *str, int *str_w, size_t *str_len) {
    int rv;

    rv = font_str_w(font, str, str_w, str_len);
    return rv;
}

int eve_font_str_w(EVEFont *font, utf8_t *str) {
    int str_w, rv;

    rv = font_str_w(font, str, &str_w, NULL);
    if (rv < 0) return rv;
    return str_w;
}

int eve_font_buf_w(EVEFont *font, utf8_t *buf, size_t buf_len) {
    int buf_w, rv;

    rv = font_buf_w(font, buf, buf_len, &buf_w, NULL);
    if (rv < 0) return rv;
    return buf_w;
}

uint8_t eve_font_h(EVEFont *font) {
    return font->h;
}