diff options
Diffstat (limited to 'utils/themeeditor/graphics/rbfont.cpp')
-rw-r--r-- | utils/themeeditor/graphics/rbfont.cpp | 126 |
1 files changed, 114 insertions, 12 deletions
diff --git a/utils/themeeditor/graphics/rbfont.cpp b/utils/themeeditor/graphics/rbfont.cpp index 3988fbc64f..e59c79a970 100644 --- a/utils/themeeditor/graphics/rbfont.cpp +++ b/utils/themeeditor/graphics/rbfont.cpp | |||
@@ -24,10 +24,14 @@ | |||
24 | #include <QFont> | 24 | #include <QFont> |
25 | #include <QBrush> | 25 | #include <QBrush> |
26 | #include <QFile> | 26 | #include <QFile> |
27 | #include <QPainter> | ||
28 | #include <QBitmap> | ||
29 | #include <QImage> | ||
27 | 30 | ||
28 | #include <QDebug> | 31 | quint16 RBFont::maxFontSizeFor16BitOffsets = 0xFFDB; |
29 | 32 | ||
30 | RBFont::RBFont(QString file) | 33 | RBFont::RBFont(QString file) |
34 | : valid(false), imageData(0), offsetData(0), widthData(0) | ||
31 | { | 35 | { |
32 | 36 | ||
33 | /* Attempting to locate the correct file name */ | 37 | /* Attempting to locate the correct file name */ |
@@ -40,6 +44,7 @@ RBFont::RBFont(QString file) | |||
40 | fin.open(QFile::ReadOnly); | 44 | fin.open(QFile::ReadOnly); |
41 | 45 | ||
42 | /* Loading the header info */ | 46 | /* Loading the header info */ |
47 | quint8 byte; | ||
43 | quint16 word; | 48 | quint16 word; |
44 | quint32 dword; | 49 | quint32 dword; |
45 | 50 | ||
@@ -89,23 +94,120 @@ RBFont::RBFont(QString file) | |||
89 | data >> dword; | 94 | data >> dword; |
90 | header.insert("nwidth", dword); | 95 | header.insert("nwidth", dword); |
91 | 96 | ||
92 | fin.close(); | 97 | /* Loading the image data */ |
98 | imageData = new quint8[header.value("nbits").toInt()]; | ||
99 | for(int i = 0; i < header.value("nbits").toInt(); i++) | ||
100 | { | ||
101 | data >> byte; | ||
102 | imageData[i] = byte; | ||
103 | } | ||
104 | |||
105 | /* Aligning on 16-bit boundary */ | ||
106 | if(header.value("nbits").toInt() % 2 == 1) | ||
107 | data >> byte; | ||
108 | |||
109 | /* Loading the offset table if necessary */ | ||
110 | if(header.value("noffset").toInt() > 0) | ||
111 | { | ||
112 | offsetData = new quint16[header.value("noffset").toInt()]; | ||
113 | for(int i = 0; i < header.value("noffset").toInt(); i++) | ||
114 | { | ||
115 | data >> word; | ||
116 | offsetData[i] = word; | ||
117 | } | ||
118 | } | ||
119 | |||
120 | /* Loading the width table if necessary */ | ||
121 | if(header.value("nwidth").toInt() > 0) | ||
122 | { | ||
123 | widthData = new quint8[header.value("nwidth").toInt()]; | ||
124 | for(int i = 0; i < header.value("nwidth").toInt(); i++) | ||
125 | { | ||
126 | data >> byte; | ||
127 | widthData[i] = byte; | ||
128 | } | ||
129 | } | ||
130 | |||
131 | fin.close(); | ||
93 | 132 | ||
94 | qDebug() << header ; | ||
95 | } | 133 | } |
96 | 134 | ||
97 | RBFont::~RBFont() | 135 | RBFont::~RBFont() |
98 | { | 136 | { |
137 | if(imageData) | ||
138 | delete[] imageData; | ||
139 | if(offsetData) | ||
140 | delete[] offsetData; | ||
141 | if(widthData) | ||
142 | delete[] widthData; | ||
99 | } | 143 | } |
100 | 144 | ||
101 | QGraphicsSimpleTextItem* RBFont::renderText(QString text, QColor color, | 145 | RBText* RBFont::renderText(QString text, QColor color, QGraphicsItem *parent) |
102 | QGraphicsItem *parent) | ||
103 | { | 146 | { |
104 | QGraphicsSimpleTextItem* retval = new QGraphicsSimpleTextItem(text, parent); | 147 | int firstChar = header.value("firstchar").toInt(); |
105 | QFont font; | 148 | int height = header.value("height").toInt(); |
106 | font.setFixedPitch(true); | 149 | int maxWidth = header.value("maxwidth").toInt(); |
107 | font.setPixelSize(8); | 150 | |
108 | retval->setFont(font); | 151 | /* First we determine the width of the combined text */ |
109 | retval->setBrush(QBrush(color)); | 152 | QList<int> widths; |
110 | return retval; | 153 | for(int i = 0; i < text.length(); i++) |
154 | { | ||
155 | if(widthData) | ||
156 | widths.append(widthData[text[i].unicode() - firstChar]); | ||
157 | else | ||
158 | widths.append(maxWidth); | ||
159 | } | ||
160 | |||
161 | int totalWidth = 0; | ||
162 | for(int i = 0; i < widths.count(); i++) | ||
163 | totalWidth += widths[i]; | ||
164 | |||
165 | QImage image(totalWidth, height, QImage::Format_Indexed8); | ||
166 | |||
167 | image.setColor(0, qRgba(0,0,0,0)); | ||
168 | image.setColor(1, color.rgb()); | ||
169 | |||
170 | /* Drawing the text */ | ||
171 | int startX = 0; | ||
172 | for(int i = 0; i < text.length(); i++) | ||
173 | { | ||
174 | unsigned int offset; | ||
175 | if(offsetData) | ||
176 | offset = offsetData[text[i].unicode() - firstChar]; | ||
177 | else | ||
178 | offset = (text[i].unicode() - firstChar) * maxWidth; | ||
179 | |||
180 | int bytesHigh = height / 8; | ||
181 | if(height % 8 > 0) | ||
182 | bytesHigh++; | ||
183 | |||
184 | int bytes = bytesHigh * widths[i]; | ||
185 | |||
186 | for(int byte = 0; byte < bytes; byte++) | ||
187 | { | ||
188 | int x = startX + byte % widths[i]; | ||
189 | int y = byte / widths[i] * 8; | ||
190 | quint8 data = imageData[offset]; | ||
191 | quint8 mask = 0x1; | ||
192 | for(int bit = 0; bit < 8; bit++) | ||
193 | { | ||
194 | if(mask & data) | ||
195 | image.setPixel(x, y, 1); | ||
196 | else | ||
197 | image.setPixel(x, y, 0); | ||
198 | |||
199 | y++; | ||
200 | mask <<= 1; | ||
201 | if(y >= height) | ||
202 | break; | ||
203 | } | ||
204 | |||
205 | offset++; | ||
206 | } | ||
207 | |||
208 | startX += widths[i]; | ||
209 | } | ||
210 | |||
211 | return new RBText(image, parent); | ||
212 | |||
111 | } | 213 | } |