-
Notifications
You must be signed in to change notification settings - Fork 0
/
fnbfont.cpp
166 lines (138 loc) · 4.74 KB
/
fnbfont.cpp
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
#include "fnbfont.h"
#include <QFile>
#include <QDebug>
QFont dbgFont;
FnbFont::FnbFont()
{
// only for debugging
dbgFont = QFont();
dbgFont.setPointSize(12);
}
void FnbFont::Load(QString fnbFile, QString PNGFile)
{
texture.load(PNGFile);
QByteArray fnb;
QFile f(fnbFile);
f.open(QIODevice::ReadOnly);
fnb = f.readAll();
ParseFNB(fnb);
}
void FnbFont::SaveFNB(QString fnbFile)
{
QFile f(fnbFile);
f.open(QIODevice::WriteOnly);
QDataStream d(&f);
// Header 0x01, baseline[u16], 1/width[f32], 1/height[f32]
d.writeRawData("\x01", 1);
d.writeRawData((char*)&baseline, 2);
float w = 1.0f/texture.width();
float h = 1.0f/texture.height();
d.writeRawData((char*)&w, 4);
d.writeRawData((char*)&h, 4);
for(const auto& e: glyphs)
{
d.writeRawData("\x03", 1); // entry header 0x03
d.writeRawData( (char*)&e.second.charcode, 4);
d.writeRawData( (char*)&e.second.x, 2);
d.writeRawData( (char*)&e.second.y, 2);
d.writeRawData( (char*)&e.second.w, 2);
d.writeRawData( (char*)&e.second.h, 2);
d.writeRawData( (char*)&e.second.leftBearing, 2);
d.writeRawData( (char*)&e.second.descent, 2);
d.writeRawData( (char*)&e.second.horizontalAdvance, 2);
}
}
void FnbFont::ParseFNB(const QByteArray bytes)
{
glyphs.clear();
const int entrySize = 19;
const int firstEntryOffset = 11;
QDataStream d(bytes);
d.skipRawData(firstEntryOffset);
int c = 0;
while(entrySize *c + firstEntryOffset < bytes.size())
{
uint16_t temp;
uint8_t rd;
d.readRawData( (char*)&rd, 1); //?
if(rd==4) break;
FnbGlyphInfo e;
e.w = 16;
e.h = 16;
d.readRawData( (char*)&e.charcode, 4);
d.readRawData( (char*)&e.x, 2);
d.readRawData( (char*)&e.y, 2);
d.readRawData( (char*)&e.w, 2);
d.readRawData( (char*)&e.h, 2);
d.readRawData( (char*)&e.leftBearing, 2);
d.readRawData( (char*)&e.descent, 2);
d.readRawData( (char*)&e.horizontalAdvance, 2);
glyphs[e.charcode] = e;
qDebug() << "FnbFont::ParseFNB" << e.charcode << (QChar) e.charcode << ": w=" << e.w <<" h=" << e.h;
c++;
}
qDebug() << "Entries = " << c;
}
void FnbFont::DrawString(QPainter &p, QString text, QPoint pos)
{
p.setCompositionMode(QPainter::CompositionMode_Screen);
QPoint ps = pos;
for(const auto& c: text)
{
ps.rx() = DrawGlyph(p, c.unicode(), ps);
}
}
float FnbFont::DrawGlyph(QPainter &p, int codepoint, QPoint pos)
{
int space = 12;
if(codepoint==32) {pos.rx() += space;return pos.x();}
auto it = glyphs.find(codepoint);
if(it != glyphs.end() && it->second.w > 0)
{
const FnbGlyphInfo& g = it->second;
p.drawImage(pos.x()+g.leftBearing, pos.y()+g.descent, texture, g.x, g.y, g.w, g.h);
p.setPen(QPen(QColor(255,255,255,50), 5));
p.drawPoint(pos.x(), pos.y());
//p.setPen(Qt::green);
//QRect r (pos.x()+g.horizontalOffset, pos.y()+g.verticalOffset, g.w, g.h);
//p.drawRect(r);
pos.rx() += g.horizontalAdvance;// g.w+g.r1;
}
else if(it != glyphs.end()) pos.rx() += space;
return pos.x();
}
QRectF FnbFont::DebugGlyph(QPainter& p, const FnbGlyphInfo& g, QPoint offset, float f, bool selected)
{
QRectF rect(g.x*f + offset.x(), g.y*f + offset.y(), g.w*f, g.h*f);
QRectF rect2 = rect.adjusted(g.leftBearing*f, 0,0,0);
rect2.setWidth(g.horizontalAdvance*f);
QColor c = selected? Qt::yellow: Qt::green; c.setAlpha(60);
p.setPen(c);
p.drawRect(rect2);
// selection
p.setPen(QPen(selected? Qt::yellow: Qt::green, selected?3:1));
p.drawRect(rect);
//QRectF rect2(g.x + offset.x() + g.horizontalOffset, g.y + offset.y(), g.w - g.horizontalOffset, g.h);
//p.setPen(Qt::red);
//float y = g.y + offset.y();
//float x = g.x + offset.x() + g.horizontalOffset;
//p.drawLine(x, y, x, y+g.h);
//p.setPen(Qt::yellow);
//p.drawLine(x+g.charWidth, y, x+g.charWidth, y+g.h);
//p.setPen(Qt::yellow);
//float y = offset.y() +g.y - g.verticalOffset;
//float x = g.x+offset.x();
//p.drawLine(x, y, x+g.w, y);
p.setFont(dbgFont);
p.drawText(rect, QString(g.charcode), Qt::AlignCenter | Qt::AlignHCenter);
//p.setPen(QPen(Qt::yellow, 5));
//p.drawPoint( g.x + offset.x(), g.y + offset.y() + g.horizontalOffset);
return rect;
}
FnbGlyphInfo *FnbFont::operator[](uint32_t unicode)
{
for(auto& g: glyphs)
if(g.second.charcode == unicode)
return &g.second;
return nullptr;
}