My favorites | Sign in
Project Home Downloads Source
Checkout   Browse   Changes    
 
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#include "Draw.h"

NAMESPACE_UPP

#ifdef PLATFORM_WIN32

#define LLOG(x) // LOG(x)
#define LTIMING(x) // TIMING(x)

void GetStdFontSys(String& name, int& height)
{
#ifdef PLATFORM_WINCE
name = "Arial";
height = 10;
#else
NONCLIENTMETRICS ncm;
ncm.cbSize = sizeof(ncm);
::SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(ncm), &ncm, 0);
name = FromSystemCharset(ncm.lfMenuFont.lfFaceName);
height = abs((int)ncm.lfMenuFont.lfHeight);
#endif
}

#define FONTCACHE 96

struct HFontEntry {
Font font;
HFONT hfont;
int angle;
};

HFONT GetWin32Font(Font fnt, int angle)
{
LTIMING("GetWin32Font");
static HFontEntry cache[FONTCACHE];
ONCELOCK {
for(int i = 0; i < FONTCACHE; i++)
cache[i].font.Height(-30000);
}
HFontEntry be;
be = cache[0];
for(int i = 0; i < FONTCACHE; i++) {
HFontEntry e = cache[i];
if(i)
cache[i] = be;
if(e.font == fnt && e.angle == angle) {
if(i)
cache[0] = e;
return e.hfont;
}
be = e;
}
LTIMING("GetWin32Font2");
if(be.hfont)
DeleteObject(be.hfont);

be.font = fnt;
be.angle = angle;
#ifdef PLATFORM_WINCE
LOGFONT lfnt;
Zero(lfnt);
lfnt.lfHeight = fnt.GetHeight() ? -abs(fnt.GetHeight()) : -12;
lfnt.lfWeight = fnt.IsBold() ? FW_BOLD : FW_NORMAL;
lfnt.lfItalic = fnt.IsItalic();
lfnt.lfUnderline = fnt.IsUnderline();
lfnt.lfStrikeOut = fnt.IsStrikeout();
wcscpy(lfnt.lfFaceName, ToSystemCharset(fnt.GetFaceName()));
be.hfont = CreateFontIndirect(&lfnt);
#else
be.hfont = CreateFont(
fnt.GetHeight() ? -abs(fnt.GetHeight()) : -12,
fnt.GetWidth(), angle, angle, fnt.IsBold() ? FW_BOLD : FW_NORMAL,
fnt.IsItalic(), fnt.IsUnderline(), fnt.IsStrikeout(),
fnt.GetFace() == Font::SYMBOL ? SYMBOL_CHARSET : DEFAULT_CHARSET,
fnt.IsTrueTypeOnly() ? OUT_TT_ONLY_PRECIS : OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
fnt.IsNonAntiAliased() ? NONANTIALIASED_QUALITY : DEFAULT_QUALITY,
DEFAULT_PITCH|FF_DONTCARE,
fnt.GetFaceName()
);
#endif
cache[0] = be;
return be.hfont;
}
/*
int sGetCW(HDC hdc, wchar *h, int n)
{
SIZE sz;
return GetTextExtentPoint32W(hdc, h, n, &sz) ? sz.cx : 0;
}
*/
static void Win32_GetGlyphIndices(HDC hdc, LPCWSTR s, int n, LPWORD r, DWORD flag)
{
typedef DWORD (WINAPI *GGIW)(HDC, LPCWSTR, int, LPWORD, DWORD);
static GGIW fn;
ONCELOCK
if(HMODULE hDLL = LoadLibrary("gdi32"))
fn = (GGIW) GetProcAddress(hDLL, "GetGlyphIndicesW");
if(fn)
fn(hdc, s, n, r, flag);
else
memset(r, 0, n * sizeof(WORD));
}

HDC Win32_IC()
{
static HDC hdc;
ONCELOCK {
hdc = CreateIC("DISPLAY", NULL, NULL, NULL);
}
return hdc;
}

CommonFontInfo GetFontInfoSys(Font font)
{
CommonFontInfo fi;
memset(&fi, 0, sizeof(fi));
HFONT hfont = GetWin32Font(font, 0);
if(hfont) {
HDC hdc = Win32_IC();
HFONT ohfont = (HFONT) ::SelectObject(hdc, hfont);
TEXTMETRIC tm;
::GetTextMetrics(hdc, &tm);
fi.ascent = tm.tmAscent;
fi.descent = tm.tmDescent;
fi.external = tm.tmExternalLeading;
fi.internal = tm.tmInternalLeading;
fi.height = fi.ascent + fi.descent;
fi.lineheight = fi.height + fi.external;
fi.overhang = tm.tmOverhang;
fi.avewidth = tm.tmAveCharWidth;
fi.maxwidth = tm.tmMaxCharWidth;
fi.firstchar = tm.tmFirstChar;
fi.charcount = tm.tmLastChar - tm.tmFirstChar + 1;
fi.default_char = tm.tmDefaultChar;
fi.fixedpitch = (tm.tmPitchAndFamily & TMPF_FIXED_PITCH) == 0;
fi.scaleable = tm.tmPitchAndFamily & TMPF_TRUETYPE;
if(fi.scaleable) {
ABC abc;
GetCharABCWidths(hdc, 'o', 'o', &abc);
fi.spacebefore = abc.abcA;
fi.spaceafter = abc.abcC;
}
else
fi.spacebefore = fi.spaceafter = 0;
::SelectObject(hdc, ohfont);
}
return fi;
}

static VectorMap<String, FaceInfo> *sList;

static int CALLBACK Win32_AddFace(const LOGFONT *logfont, const TEXTMETRIC *, dword type, LPARAM param)
{
#ifdef PLATFORM_WINCE
const wchar *facename = (const wchar *)param;
if(facename && _wcsicmp(logfont->lfFaceName, facename))
return 1;
#else
const char *facename = (const char *)param;
if(facename && stricmp(logfont->lfFaceName, facename))
return 1;
#endif
if(logfont->lfFaceName[0] == '@')
return 1;

#ifdef PLATFORM_WINCE
FontFaceInfo& f = sFontFace().GetAdd(WString(logfont->lfFaceName).ToString());
f.name = FromSystemCharset(logfont->lfFaceName);
#else
String name = FromSystemCharset(logfont->lfFaceName);

if(FindIndex(Split("Courier New CE;Courier New CYR;Courier New Greek;"
"Courier New TUR;Courier New Baltic;Arial CE;Arial CYR;"
"Arial Greek;Arial TUR;Arial Baltic;Arial CE;Times New Roman CE;"
"Times New Roman CYR;Times New Roman Greek;Times New Roman TUR;"
"Times New Roman Baltic;Times New Roman CE", ';'), name) >= 0)
return 1;

int q = sList->Find(name);
FaceInfo& f = q < 0 ? sList->Add(logfont->lfFaceName) : (*sList)[q];
f.name = FromSystemCharset(logfont->lfFaceName);
#endif
if(q < 0)
f.info = Font::SCALEABLE;
if((logfont->lfPitchAndFamily & 3) == FIXED_PITCH)
f.info |= Font::FIXEDPITCH;
if(!(type & TRUETYPE_FONTTYPE))
f.info &= ~Font::SCALEABLE;
if(logfont->lfCharSet == SYMBOL_CHARSET || logfont->lfCharSet == OEM_CHARSET)
f.info |= Font::SPECIAL;
return facename ? 0 : 1;
}

static int Win32_EnumFace(HDC hdc, const char *face)
{
#ifdef PLATFORM_WINCE
return EnumFontFamilies(hdc, ToSystemCharset(face), Win32_AddFace, (LPARAM)~ToSystemCharset(face));
#else
return EnumFontFamilies(hdc, face, Win32_AddFace, (LPARAM)face);
#endif
}

static void Win32_ForceFace(HDC hdc, const char *face, const char *aface)
{
if(!aface)
aface = "Arial";
if(Win32_EnumFace(hdc, face) && Win32_EnumFace(hdc, aface))
Panic("Missing font " + String(face));
}

Vector<FaceInfo> GetAllFacesSys()
{
VectorMap<String, FaceInfo> list;
sList = &list;
list.Add("STDFONT").name = "STDFONT";
list.Top().info = 0;
#ifdef PLATFORM_WINCE
HDC hdc = CreateDC(NULL, NULL, NULL, NULL);
Win32_ForceFace(hdc, "Tahoma", NULL);
Win32_ForceFace(hdc, "Tahoma", "Tahoma");
Win32_ForceFace(hdc, "Courier New", "Tahoma");
Win32_ForceFace(hdc, "Tahoma", "Tahoma");
Win32_ForceFace(hdc, "Tahoma", "Tahoma");
Win32_ForceFace(hdc, "Tahoma", "Tahoma");
#else
HDC hdc = CreateIC("DISPLAY", NULL, NULL, NULL);
Win32_ForceFace(hdc, "Times New Roman", "Arial");
Win32_ForceFace(hdc, "Arial", "Arial");
Win32_ForceFace(hdc, "Courier New", "Arial");
Win32_ForceFace(hdc, "Symbol", "Arial");
Win32_ForceFace(hdc, "WingDings", "Arial");
Win32_ForceFace(hdc, "Tahoma", "Arial");
#endif
Win32_EnumFace(hdc, NULL);
DeleteDC(hdc);
return list.PickValues();
}

#define GLYPHINFOCACHE 31

#ifdef flagWINGL
#include <WinGl/FontGl.h>
#include <WinGl/ResGl.h>
GlyphInfo GetGlyphInfoSys(Font font, int chr)
{
static GlyphInfo gi;
const OpenGLFont& fi = resources.GetFont(font);
gi.width = chr < fi.chars.GetCount()
? int(fi.chars[chr].xadvance * fi.scale + 0.5f)
: 0;
gi.lspc = 0;
gi.rspc = 0;
return gi;
}
#else
GlyphInfo GetGlyphInfoSys(Font font, int chr)
{
static Font fnt[GLYPHINFOCACHE];
static int pg[GLYPHINFOCACHE];
static GlyphInfo li[GLYPHINFOCACHE][256];

ONCELOCK {
for(int i = 0; i < GLYPHINFOCACHE; i++)
pg[i] = -1;
}

int page = chr >> 8;
int q = CombineHash(font, page) % GLYPHINFOCACHE;

if(fnt[q] != font || pg[q] != page) {
fnt[q] = font;
pg[q] = page;
HFONT hfont = GetWin32Font(font, 0);
if(!hfont) {
GlyphInfo n;
memset(&n, 0, sizeof(GlyphInfo));
return n;
}
HDC hdc = Win32_IC();
HFONT ohfont = (HFONT) ::SelectObject(hdc, hfont);
int from = page << 8;
GlyphInfo *t = li[q];
/* if(page >= 32) {
wchar h[3];
h[0] = 'x';
h[1] = 'x';
h[2] = 'x';
int w0 = sGetCW(hdc, h, 2);
for(int i = 0; i < 256; i++) {
h[1] = from + i;
t[i].width = sGetCW(hdc, h, 3) - w0;
t[i].lspc = t[i].rspc = 0;
}
}
else */{
bool abca = false, abcw = false;
Buffer<ABC> abc(256);
abcw = ::GetCharABCWidthsW(hdc, from, from + 256 - 1, abc);
#ifndef PLATFORM_WINCE
if(!abcw)
abca = ::GetCharABCWidthsA(hdc, from, from + 256 - 1, abc);
#endif
if(abcw)
{
for(ABC *s = abc, *lim = abc + 256; s < lim; s++, t++)
{
t->width = s->abcA + s->abcB + s->abcC;
t->lspc = s->abcA;
t->rspc = s->abcC;
}
}
else
{
Buffer<int> wb(256);
#ifdef PLATFORM_WINCE
::GetCharWidth32(hdc, from, from + 256 - 1, wb);
#else
::GetCharWidthW(hdc, from, from + 256 - 1, wb);
#endif
for(int *s = wb, *lim = wb + 256; s < lim; s++, t++)
{
t->width = *s - GetFontInfoSys(font).overhang;
if(abca)
{
ABC aa = abc[(byte)ToAscii(from++)];
t->lspc = aa.abcA;
t->rspc = aa.abcC;
}
else
t->lspc = t->rspc = 0;
}
}
}
#ifndef PLATFORM_WINCE
WORD pos[256];
WCHAR wch[256];
for(int i = 0; i < 256; i++)
wch[i] = from + i;
Win32_GetGlyphIndices(hdc, wch, 256, pos, 1);
for(int i = 0; i < 256; i++)
if(pos[i] == 0xffff) {
li[q][i].width = (int16)0x8000;
li[q][i].lspc = li[q][i].rspc = 0;
}
#endif
::SelectObject(hdc, ohfont);
}
return li[q][chr & 255];
}
#endif

#endif

END_UPP_NAMESPACE

Change log

r4188 by unodgs on Nov 22, 2011   Diff
WinGL: Added automatic atlas textures,
fixed some bugs
Go to: 
Project members, sign in to write a code review

Older revisions

r3924 by unodgs on Sep 28, 2011   Diff
Rainbow: WinGL..
r3765 by unodgs on Aug 15, 2011   Diff
Rainbow: WinGL..
r3711 by unodgs on Jul 24, 2011   Diff
Rainbow: WinGL..
All revisions of this file

File info

Size: 9438 bytes, 355 lines
Powered by Google Project Hosting