aboutsummaryrefslogtreecommitdiffstats
path: root/tools/mcufontencoder/src/bdf_import.cc
blob: 32deb057bb8d90cae787bc88b01a754d8ee54ad9 (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
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
155
156
#include "bdf_import.hh"
#include "importtools.hh"
#include <sstream>
#include <string>
#include <cctype>
#include <stdexcept>

namespace mcufont {

static std::string toupper(const std::string &input)
{
    std::string result;
    for (char c: input) result.push_back(::toupper(c));
    return result;
}

static int hextoint(char c)
{
    if (c >= '0' && c <= '9') return c - '0';
    if (c >= 'A' && c <= 'F') return c - 'A' + 10;
    throw std::domain_error("Hex digit not in range");
}

static void parse_fontinfo(std::istream &file, DataFile::fontinfo_t &fontinfo)
{
    std::string line;
    while (std::getline(file, line))
    {
        std::istringstream s(line);
        std::string tag;
        s >> tag;
        tag = toupper(tag);
        
        if (tag == "FONT")
        {
            while (isspace(s.peek())) s.get();
            std::getline(s, fontinfo.name);
        }
        else if (tag == "FONTBOUNDINGBOX")
        {
            int x, y;
            s >> fontinfo.max_width >> fontinfo.max_height;
            s >> x >> y;
            fontinfo.baseline_x = - x;
            fontinfo.baseline_y = fontinfo.max_height + y;
        }
        else if (tag == "STARTCHAR")
        {
            break;
        }
    }
}

static bool parse_glyph(std::istream &file, DataFile::glyphentry_t &glyph,
                        const DataFile::fontinfo_t &fontinfo)
{
    glyph.chars.clear();
    glyph.width = 0;
    
    // Initialize the character contents to all 0 with proper size.
    glyph.data.clear();
    glyph.data.resize(fontinfo.max_width * fontinfo.max_height, 0);
    
    int bbx_w = fontinfo.max_width;
    int bbx_h = fontinfo.max_height;
    int bbx_x = - fontinfo.baseline_x;
    int bbx_y = fontinfo.baseline_y - fontinfo.max_height;
    
    // Read glyph metadata
    std::string line;
    std::string tag;
    while (std::getline(file, line))
    {
        std::istringstream s(line);
        s >> tag;
        tag = toupper(tag);
        
        if (tag == "ENCODING")
        {
            int c;
            s >> c;
            glyph.chars.push_back(c);
        }
        else if (tag == "DWIDTH")
        {
            s >> glyph.width;
        }
        else if (tag == "BBX")
        {
            s >> bbx_w >> bbx_h >> bbx_x >> bbx_y;
        }
        else if (tag == "BITMAP")
        {
            break;
        }
    }
    
    if (tag != "BITMAP")
        return false;
    
    // Read glyph bits
    int x0 = fontinfo.baseline_x + bbx_x;
    int y = fontinfo.baseline_y - bbx_y - bbx_h;
    for (int i = 0; i < bbx_h; i++)
    {
        std::getline(file, line);
        line = toupper(line);
        
        for (int x = 0; x < bbx_w; x++)
        {
            int nibble = hextoint(line.at(x / 4));
            uint8_t pixel = 0;
            if (nibble & (8 >> (x % 4)))
                pixel = 15;
            
            glyph.data.at(y * fontinfo.max_width + x0 + x) = pixel;
        }
        
        y++;
    }
    
    std::getline(file, line);
    line = toupper(line);
    if (line.compare(0, 7, "ENDCHAR") == 0)
        return true;
    else
        return false;
}

std::unique_ptr<DataFile> LoadBDF(std::istream &file)
{
    DataFile::fontinfo_t fontinfo = {};
    std::vector<DataFile::glyphentry_t> glyphtable;
    std::vector<DataFile::dictentry_t> dictionary;
   
    parse_fontinfo(file, fontinfo);
    
    while (file)
    {
        DataFile::glyphentry_t glyph = {};
        if (parse_glyph(file, glyph, fontinfo))
            glyphtable.push_back(glyph);
    }
    
    eliminate_duplicates(glyphtable);
    crop_glyphs(glyphtable, fontinfo);
    detect_flags(glyphtable, fontinfo);
    
    fontinfo.line_height = fontinfo.max_height;
    
    std::unique_ptr<DataFile> result(new DataFile(
        dictionary, glyphtable, fontinfo));
    return result;
}

}