1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2010 Robert Bieber
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
31 quint16
RBFont::maxFontSizeFor16BitOffsets
= 0xFFDB;
33 RBFont::RBFont(QString file
)
34 : valid(false), imageData(0), offsetData(0), widthData(0)
37 /* Attempting to locate the correct file name */
38 if(!QFile::exists(file
))
39 file
= ":/fonts/08-Schumacher-Clean.fnt";
40 header
.insert("filename", file
);
42 /* Opening the file */
44 fin
.open(QFile::ReadOnly
);
46 /* Loading the header info */
51 QDataStream
data(&fin
);
52 data
.setByteOrder(QDataStream::LittleEndian
);
54 /* Grabbing the magic number and version */
56 header
.insert("version", dword
);
60 header
.insert("maxwidth", word
);
64 header
.insert("height", word
);
68 header
.insert("ascent", word
);
73 /* First character code */
75 header
.insert("firstchar", dword
);
77 /* Default character code */
79 header
.insert("defaultchar", dword
);
81 /* Number of characters */
83 header
.insert("size", dword
);
85 /* Bytes of imagebits in file */
87 header
.insert("nbits", dword
);
89 /* Longs (dword) of offset data in file */
91 header
.insert("noffset", dword
);
93 /* Bytes of width data in file */
95 header
.insert("nwidth", dword
);
97 /* Loading the image data */
98 imageData
= new quint8
[header
.value("nbits").toInt()];
99 for(int i
= 0; i
< header
.value("nbits").toInt(); i
++)
105 /* Aligning on 16-bit boundary */
106 if(header
.value("nbits").toInt() % 2 == 1)
109 /* Loading the offset table if necessary */
110 if(header
.value("noffset").toInt() > 0)
112 offsetData
= new quint16
[header
.value("noffset").toInt()];
113 for(int i
= 0; i
< header
.value("noffset").toInt(); i
++)
116 offsetData
[i
] = word
;
120 /* Loading the width table if necessary */
121 if(header
.value("nwidth").toInt() > 0)
123 widthData
= new quint8
[header
.value("nwidth").toInt()];
124 for(int i
= 0; i
< header
.value("nwidth").toInt(); i
++)
145 RBText
* RBFont::renderText(QString text
, QColor color
, QGraphicsItem
*parent
)
147 int firstChar
= header
.value("firstchar").toInt();
148 int height
= header
.value("height").toInt();
149 int maxWidth
= header
.value("maxwidth").toInt();
151 /* First we determine the width of the combined text */
153 for(int i
= 0; i
< text
.length(); i
++)
156 widths
.append(widthData
[text
[i
].unicode() - firstChar
]);
158 widths
.append(maxWidth
);
162 for(int i
= 0; i
< widths
.count(); i
++)
163 totalWidth
+= widths
[i
];
165 QImage
image(totalWidth
, height
, QImage::Format_Indexed8
);
167 image
.setColor(0, qRgba(0,0,0,0));
168 image
.setColor(1, color
.rgb());
170 /* Drawing the text */
172 for(int i
= 0; i
< text
.length(); i
++)
176 offset
= offsetData
[text
[i
].unicode() - firstChar
];
178 offset
= (text
[i
].unicode() - firstChar
) * maxWidth
;
180 int bytesHigh
= height
/ 8;
184 int bytes
= bytesHigh
* widths
[i
];
186 for(int byte
= 0; byte
< bytes
; byte
++)
188 int x
= startX
+ byte
% widths
[i
];
189 int y
= byte
/ widths
[i
] * 8;
190 quint8 data
= imageData
[offset
];
192 for(int bit
= 0; bit
< 8; bit
++)
195 image
.setPixel(x
, y
, 1);
197 image
.setPixel(x
, y
, 0);
211 return new RBText(image
, parent
);