summaryrefslogtreecommitdiff
path: root/utils/themeeditor/graphics/rbfont.cpp
blob: 1c2739d11832a06fd30cda877266247a58dae594 (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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2010 Robert Bieber
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include "rbfont.h"

#include <QFont>
#include <QBrush>
#include <QFile>
#include <QPainter>
#include <QBitmap>
#include <QImage>
#include <QSettings>

quint16 RBFont::maxFontSizeFor16BitOffsets = 0xFFDB;

RBFont::RBFont(QString file)
    : valid(false), imageData(0), offsetData(0), widthData(0)
{

    /* Attempting to locate the correct file name */
    if(!QFile::exists(file))
    {
        /* Checking in the fonts repository */
        QSettings settings;
        settings.beginGroup("RBFont");

        file = file.split("/").last();
        file = settings.value("fontDir", "").toString() + "/" + file;

        settings.endGroup();

        if(!QFile::exists(file))
            file = ":/fonts/08-Schumacher-Clean.fnt";
    }
    header.insert("filename", file);

    /* Opening the file */
    QFile fin(file);
    fin.open(QFile::ReadOnly);

    /* Loading the header info */
    quint8 byte;
    quint16 word;
    quint32 dword;

    QDataStream data(&fin);
    data.setByteOrder(QDataStream::LittleEndian);

    /* Grabbing the magic number and version */
    data >> dword;
    header.insert("version", dword);

    /* Max font width */
    data >> word;
    header.insert("maxwidth", word);

    /* Font height */
    data >> word;
    header.insert("height", word);

    /* Ascent */
    data >> word;
    header.insert("ascent", word);

    /* Padding */
    data >> word;

    /* First character code */
    data >> dword;
    header.insert("firstchar", dword);

    /* Default character code */
    data >> dword;
    header.insert("defaultchar", dword);

    /* Number of characters */
   data >> dword;
   header.insert("size", dword);

   /* Bytes of imagebits in file */
   data >> dword;
   header.insert("nbits", dword);

   /* Longs (dword) of offset data in file */
   data >> dword;
   header.insert("noffset", dword);

   /* Bytes of width data in file */
   data >> dword;
   header.insert("nwidth", dword);

   /* Loading the image data */
   imageData = new quint8[header.value("nbits").toInt()];
   for(int i = 0; i < header.value("nbits").toInt(); i++)
   {
       data >> byte;
       imageData[i] = byte;
   }

   /* Aligning on 16-bit boundary */
   if(header.value("nbits").toInt() % 2 == 1)
       data >> byte;

   /* Loading the offset table if necessary */
   if(header.value("noffset").toInt() > 0)
   {
       offsetData = new quint16[header.value("noffset").toInt()];
       for(int i = 0; i < header.value("noffset").toInt(); i++)
       {
           data >> word;
           offsetData[i] = word;
       }
   }

   /* Loading the width table if necessary */
   if(header.value("nwidth").toInt() > 0)
   {
       widthData = new quint8[header.value("nwidth").toInt()];
       for(int i = 0; i < header.value("nwidth").toInt(); i++)
       {
           data >> byte;
           widthData[i] = byte;
       }
   }

   fin.close();

}

RBFont::~RBFont()
{
    if(imageData)
        delete[] imageData;
    if(offsetData)
        delete[] offsetData;
    if(widthData)
        delete[] widthData;
}

RBText* RBFont::renderText(QString text, QColor color, QGraphicsItem *parent)
{
    int firstChar = header.value("firstchar").toInt();
    int height = header.value("height").toInt();
    int maxWidth = header.value("maxwidth").toInt();

    /* First we determine the width of the combined text */
    QList<int> widths;
    for(int i = 0; i < text.length(); i++)
    {
        if(widthData)
            widths.append(widthData[text[i].unicode() - firstChar]);
        else
            widths.append(maxWidth);
    }

    int totalWidth = 0;
    for(int i = 0; i < widths.count(); i++)
        totalWidth += widths[i];

    QImage image(totalWidth, height, QImage::Format_Indexed8);

    image.setColor(0, qRgba(0,0,0,0));
    image.setColor(1, color.rgb());

    /* Drawing the text */
    int startX = 0;
    for(int i = 0; i < text.length(); i++)
    {
        unsigned int offset;
        if(offsetData)
            offset = offsetData[text[i].unicode() - firstChar];
        else
            offset = (text[i].unicode() - firstChar) * maxWidth;

        int bytesHigh = height / 8;
        if(height % 8 > 0)
            bytesHigh++;

        int bytes = bytesHigh * widths[i];

        for(int byte = 0; byte < bytes; byte++)
        {
            int x = startX + byte % widths[i];
            int y = byte / widths[i] * 8;
            quint8 data = imageData[offset];
            quint8 mask = 0x1;
            for(int bit = 0; bit < 8; bit++)
            {
                if(mask & data)
                    image.setPixel(x, y, 1);
                else
                    image.setPixel(x, y, 0);

                y++;
                mask <<= 1;
                if(y >= height)
                    break;
            }

            offset++;
        }

        startX += widths[i];
    }

    return new RBText(image, parent);

}