summaryrefslogtreecommitdiff
path: root/src/font.cpp
blob: 27204a7f3604520c0836f153b6e35bb5ac0eb3ad (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include "font.h"

#include <stdio.h>
#include <string.h>

#include <exception>

#define fh (( (FILE *)rawfh ))

Font::Font( const char *sSrcFile ) :
    iTableTop( 0 ),
    pTable( NULL )
{
    iTableTop = 128;
    pTable = new Glyph*[iTableTop];
    memset( pTable, 0, sizeof(Glyph*)*iTableTop );
    Parser p( sSrcFile, *this );
    p.parse();
}

Font::~Font()
{
}

Font::Parser::Parser( const char *sSrcFile, Font &rFont ) :
    rFont( rFont ),
    rawfh( NULL ),
    sLine( NULL ),
    bDone( false )
{
    sLine = new char[1024];
    rawfh = fopen( sSrcFile, "rt" );
}

Font::Parser::~Parser()
{
    delete[] sLine;
    fclose( fh );
}

void Font::Parser::parse()
{
    header();
    characters();
    if( strncasecmp("ENDFONT", sLine, 7 ) )
    {
        printf("End of file reached, but no end marker.\n");
        throw std::exception();
    }
}

void Font::Parser::getLine()
{
    for(;;)
    {
        if( fgets( sLine, 1024, fh ) == NULL )
        {
            sLine[0] = '\0';
            bDone = true;
            printf("Premature end of file.\n");
            throw std::exception();
            return;
        }

        if( strncasecmp("COMMENT ", sLine, 8 ) )
        {
            return;
        }
    }
}

void Font::Parser::header()
{
    getLine();
    if( strncasecmp("STARTFONT 2.1", sLine, 13 ) )
    {
        printf("Bad format: %s\n", sLine);
        throw std::exception();
    }

    for(;;)
    {
        getLine();
        if( strncasecmp("SIZE ", sLine, 5 ) == 0 )
        {
        }
        else if( strncasecmp("FONTBOUNDINGBOX ", sLine, 16 ) == 0 )
        {
        }
        else if( strncasecmp("STARTPROPERTIES ", sLine, 16 ) == 0 )
        {
            properties();
        }
        else if( strncasecmp("CHARS ", sLine, 6 ) == 0 )
        {
            return;
        }
    }
}

void Font::Parser::properties()
{
    for(;;)
    {
        getLine();
        if( strncasecmp("ENDPROPERTIES", sLine, 13 ) == 0 )
            return;
    }
}

void Font::Parser::characters()
{
    for(;;)
    {
        getLine();
        if( strncasecmp("STARTCHAR ", sLine, 10 ) == 0 )
            character();
        else
            return;
    }
}

uint8_t hex2byte( const char *str )
{
    uint8_t ret = 0;

    ret |= ((*str >= '0' && *str <= '9')?(*str-'0'):(
           (*str >= 'a' && *str <= 'f')?(*str-'a'+10):(
           (*str >= 'A' && *str <= 'F')?(*str-'A'+10):0
           )))<<4;
    str++;
    ret |= (*str >= '0' && *str <= '9')?(*str-'0'):(
           (*str >= 'a' && *str <= 'f')?(*str-'a'+10):(
           (*str >= 'A' && *str <= 'F')?(*str-'A'+10):0
           ));
    return ret;
}

void Font::Parser::character()
{
    char sName[1024];
    int32_t iIdx = -1;
    strcpy( sName, sLine+10 );
    Glyph *pChar = new Glyph();

    for(;;)
    {
        getLine();
        if( strncasecmp("ENCODING ", sLine, 9 ) == 0 )
        {
            sscanf( sLine+9, "%d", &iIdx );
        }
        else if( strncasecmp("DWIDTH ", sLine, 7 ) == 0 )
        {
            sscanf( sLine+7, "%d %d", &pChar->pDevOff.x, &pChar->pDevOff.y );
        }
        else if( strncasecmp("BBX ", sLine, 4 ) == 0 )
        {
            sscanf( sLine+4, "%d %d %d %d",
                &pChar->pBox.x, &pChar->pBox.y,
                &pChar->pOff.x, &pChar->pOff.y
                );
        }
        else if( strncasecmp("BITMAP", sLine, 6 ) == 0 )
        {
            pChar->iRowBytes = pChar->pBox.x/8 + ((pChar->pBox.x%8)==0?0:1);

            pChar->pData = new uint8_t[pChar->iRowBytes*pChar->pBox.y];
            // Read bitmap
            for( int y = 0; y < pChar->pBox.y; y++ )
            {
                getLine();
                for( int x = 0; x < pChar->iRowBytes; x++ )
                {
                    pChar->pData[x+y*pChar->iRowBytes] = hex2byte( sLine+(2*x) );
                }
            }
        }
        else if( strncasecmp("ENDCHAR", sLine, 7 ) == 0 )
        {
            if( iIdx >= 0 && iIdx < rFont.iTableTop )
            {
                rFont.pTable[iIdx] = pChar;
                //printf("Char %d: %s\n", iIdx, sName );
            }
            else
                delete pChar;
            return;
        }
    }
}