summaryrefslogtreecommitdiff
path: root/components/drivers/serial/device/virtual/psf.c
blob: c839c7c02582125cffbbf603b2df27768acdbf3b (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
/*
 * Copyright (c) 2006-2023, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2023-02-25     GuEe-GUI     the first version
 */

#include "psf.h"

static void psf_read_header(struct psf_font *psf)
{
    if (psf->header1->magic[0] == PSF1_MAGIC0 &&
        psf->header1->magic[1] == PSF1_MAGIC1)
    {
        psf->font_map = (void *)&psf->header1[1];
        psf->type = PSF_TYPE_1;

        if (psf->header1->mode & PSF1_MODE512)
        {
            psf->count = 512;
        }
        else
        {
            psf->count = 256;
        }

        psf->size = psf->header1->charsize;
        psf->height = psf->header1->charsize;
        psf->width = 8;
    }
    else if (psf->header2->magic[0] == PSF2_MAGIC0 &&
            psf->header2->magic[1] == PSF2_MAGIC1 &&
            psf->header2->magic[2] == PSF2_MAGIC2 &&
            psf->header2->magic[3] == PSF2_MAGIC3)
    {
        psf->font_map = (void *)&psf->header2[1];
        psf->type = PSF_TYPE_2;
        psf->count = psf->header2->length;
        psf->size = psf->header2->charsize;
        psf->height = psf->header2->height;
        psf->width = psf->header2->width;
    }
    else
    {
        psf->type = PSF_TYPE_UNKNOW;
    }

    psf->glyph = psf->height * psf->width;
}

rt_err_t psf_initialize(const void *psf_data, struct psf_font *out_psf)
{
    struct psf_font *psf = out_psf;

    psf->raw_data = psf_data;
    psf_read_header(psf);

    if (psf->type == PSF_TYPE_UNKNOW)
    {
        return -RT_ENOSYS;
    }

    return RT_EOK;
}

rt_err_t psf_parse(struct psf_font *psf, const rt_uint8_t *font_data,
        rt_uint8_t *tmpglyph, rt_uint32_t color_size,
        rt_uint32_t foreground, rt_uint32_t background)
{
    rt_uint8_t *font = (void *)font_data, *map = (void *)psf->font_map;

    psf->font_data = font_data;

    for (int n = 0; n < psf->count; ++n, map += psf->size)
    {
        rt_memcpy(tmpglyph, map, psf->size);

        for (int i = 0; i < psf->size; ++i)
        {
            for (int j = 0; j < 8; ++j)
            {
                if (i % (psf->size / psf->height) * 8 + j < psf->width)
                {
                    if (tmpglyph[i] & 0x80)
                    {
                        rt_memcpy(font, &foreground, color_size);
                    }
                    else
                    {
                        rt_memcpy(font, &background, color_size);
                    }

                    font += color_size;
                }

                tmpglyph[i] = tmpglyph[i] << 1;
            }
        }
    }

    return RT_EOK;
}