jdfbfont.cpp
6.59 KB
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/***************************************************************************
* Copyright (C) 2005 by Jeff Ferr *
* root@sat *
* *
* 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 program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "Stdafx.h"
#include "jfont.h"
#include "jgfxhandler.h"
namespace jgui {
Font *Font::_default_font = NULL;
Font::Font(std::string name, jfont_attributes_t attributes, int height, int scale_width, int scale_height):
jcommon::Object()
{
jcommon::Object::SetClassName("jgui::Font");
#ifdef DIRECTFB_UI
_font = NULL;
#endif
_attributes = attributes;
_name = name;
_height = 0;
_ascender = 0;
_descender = 0;
_virtual_height = height;
_screen.width = GFXHandler::GetInstance()->GetScreenWidth();
_screen.height = GFXHandler::GetInstance()->GetScreenHeight();
_scale.width = scale_width;
_scale.height = scale_height;
if (_scale.width <= 0) {
_scale.width = DEFAULT_SCALE_WIDTH;
}
if (_scale.height <= 0) {
_scale.height = DEFAULT_SCALE_HEIGHT;
}
#ifdef DIRECTFB_UI
((GFXHandler *)GFXHandler::GetInstance())->CreateFont(name, height, &_font, _scale.width, _scale.height);
if (_font != NULL) {
_font->GetHeight(_font, &_height);
_font->GetAscender(_font, &_ascender);
_font->GetDescender(_font, &_descender);
}
#endif
GFXHandler::GetInstance()->Add(this);
}
Font::~Font()
{
GFXHandler::GetInstance()->Add(this);
#ifdef DIRECTFB_UI
if (_font != NULL) {
_font->Release(_font);
}
#endif
}
Font * Font::GetDefaultFont()
{
if (_default_font == NULL) {
_default_font = new Font(_DATA_PREFIX"/fonts/font.ttf", JFA_NONE, DEFAULT_FONT_SIZE, DEFAULT_SCALE_WIDTH, DEFAULT_SCALE_HEIGHT);
}
return _default_font;
}
Font * Font::CreateFont(std::string name, jfont_attributes_t attributes, int height, int scale_width, int scale_height)
{
return new Font(name, attributes, height, scale_width, scale_height);
}
void Font::SetWorkingScreenSize(int width, int height)
{
_scale.width = width;
_scale.height = height;
}
jsize_t Font::GetWorkingScreenSize()
{
return _scale;
}
jfont_attributes_t Font::GetFontAttributes()
{
return _attributes;
}
void * Font::GetFont()
{
#ifdef DIRECTFB_UI
return _font;
#endif
return NULL;
}
bool Font::SetEncoding(std::string code)
{
#ifdef DIRECTFB_UI
DFBTextEncodingID enc_id;
if (_font == NULL) {
return false;
}
if (_font->FindEncoding(_font, code.c_str(), &enc_id) != DFB_OK) {
return false;
}
if (_font->SetEncoding(_font, enc_id) != DFB_OK) {
return false;
}
return true;
#endif
return false;
}
std::string Font::GetName()
{
return _name;
}
int Font::GetVirtualHeight()
{
return _virtual_height;
}
int Font::GetHeight()
{
return SCREEN_TO_SCALE(_height, _screen.width, _scale.width);
}
int Font::GetAscender()
{
return SCREEN_TO_SCALE(_ascender, _screen.width, _scale.width);
}
int Font::GetDescender()
{
return SCREEN_TO_SCALE(abs(_descender), _screen.width, _scale.width);
}
int Font::GetMaxAdvanced()
{
return SCREEN_TO_SCALE(_max_advance, _screen.width, _scale.width);
}
int Font::GetLeading()
{
return SCREEN_TO_SCALE(_height/2.0, _screen.width, _scale.width);
}
int Font::GetStringWidth(std::string text)
{
int size = 0;
#ifdef DIRECTFB_UI
if (_font == NULL) {
return 0;
}
_font->GetStringWidth(_font, text.c_str(), -1, &size);
#endif
return SCREEN_TO_SCALE(size, _screen.width, _scale.width);
}
jregion_t Font::GetStringExtends(std::string text)
{
jregion_t region;
region.x = 0;
region.y = 0;
region.width = 0;
region.height = 0;
#ifdef DIRECTFB_UI
if (_font == NULL) {
return region;
}
DFBRectangle lrect;
// irect;
_font->GetStringExtents(_font, text.c_str(), -1, &lrect, NULL); // &irect);
region.x = SCREEN_TO_SCALE(lrect.x, _screen.width, _scale.width);
region.y = SCREEN_TO_SCALE(lrect.y, _screen.width, _scale.width);
region.width = SCREEN_TO_SCALE(lrect.w, _screen.width, _scale.width);
region.height = SCREEN_TO_SCALE(lrect.h, _screen.width, _scale.width);
#endif
return region;
}
jregion_t Font::GetGlyphExtends(int symbol)
{
jregion_t region;
region.x = 0;
region.y = 0;
region.width = 0;
region.height = 0;
#ifdef DIRECTFB_UI
if (_font == NULL) {
return region;
}
DFBRectangle lrect;
int advance;
_font->GetGlyphExtents(_font, symbol, &lrect, &advance);
region.x = SCREEN_TO_SCALE(lrect.x, _screen.width, _scale.width);
region.y = SCREEN_TO_SCALE(lrect.y, _screen.width, _scale.width);
region.width = SCREEN_TO_SCALE(lrect.w, _screen.width, _scale.width);
region.height = SCREEN_TO_SCALE(lrect.h, _screen.width, _scale.width);
#endif
return region;
}
std::string Font::TruncateString(std::string text, std::string extension, int width)
{
if (text.size() <= 1 || width <= 0) {
return text;
}
if (GetStringWidth(text) < width) {
return text;
}
bool flag = false;
while (GetStringWidth(text + extension) > width) {
flag = true;
text = text.substr(0, text.size()-1);
if (text.size() <= 1) {
break;
}
}
if (flag == true) {
return text + extension;
}
return text;
}
void Font::Release()
{
#ifdef DIRECTFB_UI
if (_font != NULL) {
_font->Dispose(_font);
_font->Release(_font);
_font = NULL;
}
#endif
}
void Font::Restore()
{
_screen.width = GFXHandler::GetInstance()->GetScreenWidth();
_screen.height = GFXHandler::GetInstance()->GetScreenHeight();
#ifdef DIRECTFB_UI
if (_font == NULL) {
((GFXHandler *)GFXHandler::GetInstance())->CreateFont(_name, _virtual_height, &_font, _scale.width, _scale.height);
}
#endif
}
}