-
Notifications
You must be signed in to change notification settings - Fork 74
/
ttf-otf-windows-loader.cpp
354 lines (284 loc) · 12.7 KB
/
ttf-otf-windows-loader.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
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
/////////////////////////////////////////////////////////////////////////
//
// Author: Mateusz Jurczyk ([email protected])
//
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include <Windows.h>
#include <Windowsx.h>
#include <usp10.h>
#include <cstdio>
#include "config.h"
#pragma comment(lib, "Usp10.lib")
#ifndef ARRAY_SIZE
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
#endif
//
// Undocumented definitions required to use the gdi32!GetFontResourceInfoW function.
//
typedef BOOL(WINAPI *PGFRI)(LPCWSTR, LPDWORD, LPVOID, DWORD);
#define QFR_LOGFONT (2)
static BOOL GetLogfonts(LPCWSTR szFontPath, LPLOGFONTW *lpLogfonts, LPDWORD lpdwFonts) {
// Unload all instances of fonts with this path, in case there are any leftovers in the system.
while (RemoveFontResourceW(szFontPath)) {}
// Load the font file into the system temporarily.
DWORD dwFontsLoaded = AddFontResourceW(szFontPath);
if (dwFontsLoaded == 0) {
wprintf(L"[-] AddFontResourceW() failed.\n");
return FALSE;
}
*lpdwFonts = dwFontsLoaded;
*lpLogfonts = (LPLOGFONTW)HeapAlloc(GetProcessHeap(), 0, dwFontsLoaded * sizeof(LOGFONTW));
if (*lpLogfonts == NULL) {
wprintf(L"[-] HeapAlloc(%u) failed.\n", dwFontsLoaded * sizeof(LOGFONTW));
RemoveFontResourceW(szFontPath);
return FALSE;
}
// Resolve the GDI32!GetFontResourceInfoW symbol.
HINSTANCE hGdi32 = GetModuleHandleA("gdi32.dll");
PGFRI GetFontResourceInfo = (PGFRI)GetProcAddress(hGdi32, "GetFontResourceInfoW");
DWORD cbBuffer = dwFontsLoaded * sizeof(LOGFONTW);
if (!GetFontResourceInfo(szFontPath, &cbBuffer, *lpLogfonts, QFR_LOGFONT)) {
wprintf(L"[-] GetFontResourceInfoW() failed.\n");
HeapFree(GetProcessHeap(), 0, *lpLogfonts);
RemoveFontResourceW(szFontPath);
return FALSE;
}
// Unload the font.
RemoveFontResourceW(szFontPath);
return TRUE;
}
static VOID TestUniscribe(HDC hdc, LPGLYPHSET lpGlyphset) {
SCRIPT_CACHE sc = NULL;
// Get font height.
long tmHeight;
ScriptCacheGetHeight(hdc, &sc, &tmHeight);
// Get font properties.
SCRIPT_FONTPROPERTIES fp;
ScriptGetFontProperties(hdc, &sc, &fp);
// Perform some operations (mostly in batches) over each supported glyph.
WCHAR szTextBuffer[DISPLAYED_GLYPHS_COUNT];
DWORD dwTextCount = 0;
WORD *wOutGlyphs = (WORD *)HeapAlloc(GetProcessHeap(), 0, DISPLAYED_GLYPHS_COUNT * sizeof(WORD));
for (DWORD i = 0; i < lpGlyphset->cRanges; i++) {
for (LONG j = lpGlyphset->ranges[i].wcLow; j < lpGlyphset->ranges[i].wcLow + lpGlyphset->ranges[i].cGlyphs; j++) {
szTextBuffer[dwTextCount++] = (WCHAR)j;
// Test particular characters.
ABC abc;
ScriptGetGlyphABCWidth(hdc, &sc, (WORD)j, &abc);
// Test characters in batches where possible.
if (dwTextCount >= DISPLAYED_GLYPHS_COUNT) {
// Test the usp10!ScriptGetCMap function.
ScriptGetCMap(hdc, &sc, szTextBuffer, dwTextCount, 0, wOutGlyphs);
dwTextCount = 0;
}
}
}
if (dwTextCount > 0) {
// Test the usp10!ScriptGetCMap function.
ScriptGetCMap(hdc, &sc, szTextBuffer, dwTextCount, 0, wOutGlyphs);
}
// Call some script/lang/feature-related APIs over each glyph.
OPENTYPE_TAG *ScriptTags = (OPENTYPE_TAG *)HeapAlloc(GetProcessHeap(), 0, UNISCRIBE_MAX_TAGS * sizeof(OPENTYPE_TAG));
OPENTYPE_TAG *LangTags = (OPENTYPE_TAG *)HeapAlloc(GetProcessHeap(), 0, UNISCRIBE_MAX_TAGS * sizeof(OPENTYPE_TAG));
OPENTYPE_TAG *FeatureTags = (OPENTYPE_TAG *)HeapAlloc(GetProcessHeap(), 0, UNISCRIBE_MAX_TAGS * sizeof(OPENTYPE_TAG));
int cScriptTags = 0, cLangTags = 0, cFeatureTags = 0;
WORD *AlternateGlyphs = (WORD *)HeapAlloc(GetProcessHeap(), 0, MAX_ALTERNATE_GLYPHS * sizeof(WORD));
int cAlternates;
// Retrieve a list of available scripts in the font.
if (ScriptGetFontScriptTags(hdc, &sc, NULL, UNISCRIBE_MAX_TAGS, ScriptTags, &cScriptTags) == 0) {
for (int ScriptTag = 0; ScriptTag < cScriptTags; ScriptTag++) {
// Retrieve a list of language tags for the specified script tag.
if (ScriptGetFontLanguageTags(hdc, &sc, NULL, ScriptTags[ScriptTag], UNISCRIBE_MAX_TAGS, LangTags, &cLangTags) == 0) {
for (int LangTag = 0; LangTag < cLangTags; LangTag++) {
// Retrieve a list of typographic features for the defined writing system.
if (ScriptGetFontFeatureTags(hdc, &sc, NULL, ScriptTags[ScriptTag], LangTags[LangTag], UNISCRIBE_MAX_TAGS, FeatureTags, &cFeatureTags) == 0) {
for (int FeatureTag = 0; FeatureTag < cFeatureTags; FeatureTag++) {
// Iterate through all glyphs in the font.
for (DWORD i = 0; i < lpGlyphset->cRanges; i++) {
for (LONG j = lpGlyphset->ranges[i].wcLow; j < lpGlyphset->ranges[i].wcLow + lpGlyphset->ranges[i].cGlyphs; j++) {
// Test usp10!ScriptGetFontAlternateGlyphs.
if (ScriptGetFontAlternateGlyphs(hdc, &sc, NULL, ScriptTags[ScriptTag], LangTags[LangTag], FeatureTags[FeatureTag],
(WORD)j, MAX_ALTERNATE_GLYPHS, AlternateGlyphs, &cAlternates) == 0) {
for (int alt_glyph_id = 1; alt_glyph_id < cAlternates; alt_glyph_id++) {
WORD wOutGlyphId;
// Test usp10!ScriptSubstituteSingleGlyph.
ScriptSubstituteSingleGlyph(hdc, &sc, NULL, ScriptTags[ScriptTag], LangTags[LangTag], FeatureTags[FeatureTag],
alt_glyph_id, (WORD)j, &wOutGlyphId);
}
}
}
}
}
}
}
}
}
}
ScriptFreeCache(&sc);
HeapFree(GetProcessHeap(), 0, wOutGlyphs);
HeapFree(GetProcessHeap(), 0, ScriptTags);
HeapFree(GetProcessHeap(), 0, LangTags);
HeapFree(GetProcessHeap(), 0, FeatureTags);
HeapFree(GetProcessHeap(), 0, AlternateGlyphs);
}
int wmain(int argc, wchar_t *argv[], wchar_t *envp[]) {
if (argc != 2) {
wprintf(L"Usage: %s <font path>\n", argv[0]);
return EXIT_FAILURE;
}
LPCWSTR szFontPath = argv[1];
// Get screen coordinates.
RECT screen_rect = { 0 };
screen_rect.left = 0;
screen_rect.top = 0;
screen_rect.right = GetSystemMetrics(SM_CXFULLSCREEN);
screen_rect.bottom = GetSystemMetrics(SM_CYFULLSCREEN);
// Reset the PRNG state.
srand(0);
do {
// Get the logfont structures.
LPLOGFONTW lpLogfonts = NULL;
DWORD dwFonts = 0;
if (GetLogfonts(szFontPath, &lpLogfonts, &dwFonts)) {
wprintf(L"[+] Extracted %u logfonts.\n", dwFonts);
} else {
break;
}
// Load the font in the system from memory.
int cFonts = AddFontResourceW(szFontPath);
if (cFonts > 0) {
wprintf(L"[+] Installed %d fonts.\n", cFonts);
} else {
wprintf(L"[-] AddFontResourceW() failed.\n");
break;
}
HDC hDC = GetDC(NULL);
SetGraphicsMode(hDC, GM_ADVANCED);
SetMapMode(hDC, MM_TEXT);
// Display all fonts from the input file.
BOOL success = TRUE;
for (DWORD font_it = 0; success && font_it < dwFonts; font_it++) {
// Display the font in several different point sizes.
CONST LONG point_sizes[] = { 8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72 };
for (unsigned int variation_it = 0; success && variation_it < ARRAY_SIZE(point_sizes) + 1; variation_it++) {
wprintf(L"[+] Starting to test font %u / %d, variation %u / %d\n",
font_it + 1, dwFonts, variation_it + 1, ARRAY_SIZE(point_sizes) + 1);
HFONT hFont = NULL;
if (variation_it == 0) {
hFont = CreateFontIndirectW(&lpLogfonts[font_it]);
} else {
LOGFONTW lf;
RtlCopyMemory(&lf, &lpLogfonts[font_it], sizeof(LOGFONTW));
lf.lfHeight = -MulDiv(point_sizes[variation_it - 1], GetDeviceCaps(hDC, LOGPIXELSY), 72);
lf.lfWeight = 0;
lf.lfItalic = (rand() & 1);
lf.lfUnderline = (rand() & 1);
lf.lfStrikeOut = (rand() & 1);
lf.lfQuality = (rand() % 6);
hFont = CreateFontIndirectW(&lf);
}
if (hFont == NULL) {
wprintf(L"[!] CreateFontIndirectW() failed.\n");
success = FALSE;
DeleteFont(hFont);
break;
}
// Select the font for the Device Context.
SelectFont(hDC, hFont);
#if HARNESS_TEST_KERNING_PAIRS
wprintf(L"[+] Getting kerning pairs.\n");
// Get the font's kerning pairs.
DWORD nNumPairs = GetKerningPairs(hDC, 0, NULL);
if (nNumPairs != 0) {
LPKERNINGPAIR lpkrnpairs = (LPKERNINGPAIR)HeapAlloc(GetProcessHeap(), 0, nNumPairs * sizeof(KERNINGPAIR));
if (GetKerningPairs(hDC, nNumPairs, lpkrnpairs) == 0) {
wprintf(L"[!] GetKerningPairs() failed.\n");
}
HeapFree(GetProcessHeap(), 0, lpkrnpairs);
}
#endif // HARNESS_TEST_KERNING_PAIRS
wprintf(L"[+] Getting unicode ranges.\n");
// Get Unicode ranges available in the font.
DWORD dwGlyphsetSize = GetFontUnicodeRanges(hDC, NULL);
if (dwGlyphsetSize == 0) {
wprintf(L"[!] GetFontUnicodeRanges() failed.\n");
success = FALSE;
DeleteFont(hFont);
break;
}
LPGLYPHSET lpGlyphset = (LPGLYPHSET)HeapAlloc(GetProcessHeap(), 0, dwGlyphsetSize);
if (GetFontUnicodeRanges(hDC, lpGlyphset) == 0) {
wprintf(L"[!] GetFontUnicodeRanges() failed.\n");
success = FALSE;
HeapFree(GetProcessHeap(), 0, lpGlyphset);
DeleteFont(hFont);
break;
}
#if HARNESS_TEST_DRAWTEXT
WCHAR szTextBuffer[DISPLAYED_GLYPHS_COUNT + 1];
DWORD dwTextCount = 0;
#endif // HARNESS_TEST_DRAWTEXT
wprintf(L"[+] Getting glyph outlines and drawing them on screen.\n");
for (DWORD i = 0; i < lpGlyphset->cRanges; i++) {
for (LONG j = lpGlyphset->ranges[i].wcLow; j < lpGlyphset->ranges[i].wcLow + lpGlyphset->ranges[i].cGlyphs; j++) {
#if HARNESS_TEST_GLYPH_OUTLINE
// Get the glyph outline in all available formats.
CONST UINT glyph_formats[] = { GGO_BEZIER, GGO_BITMAP, GGO_GRAY2_BITMAP, GGO_GRAY4_BITMAP, GGO_GRAY8_BITMAP, GGO_NATIVE };
for (UINT format : glyph_formats) {
GLYPHMETRICS gm;
MAT2 mat2 = { 0, 1, 0, 0, 0, 0, 0, 1 };
DWORD cbBuffer = GetGlyphOutline(hDC, j, format, &gm, 0, NULL, &mat2);
if (cbBuffer != GDI_ERROR) {
LPVOID lpvBuffer = HeapAlloc(GetProcessHeap(), 0, cbBuffer);
if (GetGlyphOutline(hDC, j, format, &gm, cbBuffer, lpvBuffer, &mat2) == GDI_ERROR) {
wprintf(L"[!] GetGlyphOutline() failed for glyph %u.\n", j);
}
HeapFree(GetProcessHeap(), 0, lpvBuffer);
}
}
#endif // HARNESS_TEST_GLYPH_OUTLINE
#if HARNESS_TEST_DRAWTEXT
// Insert the glyph into current string to be displayed.
szTextBuffer[dwTextCount++] = (WCHAR)j;
if (dwTextCount >= DISPLAYED_GLYPHS_COUNT) {
szTextBuffer[DISPLAYED_GLYPHS_COUNT] = L'\0';
DrawTextW(hDC, szTextBuffer, -1, &screen_rect, DT_WORDBREAK | DT_NOCLIP);
dwTextCount = 0;
}
#endif // HARNESS_TEST_DRAWTEXT
}
}
#if HARNESS_TEST_DRAWTEXT
if (dwTextCount > 0) {
szTextBuffer[dwTextCount] = L'\0';
DrawTextW(hDC, szTextBuffer, -1, &screen_rect, DT_WORDBREAK | DT_NOCLIP);
}
#endif // HARNESS_TEST_DRAWTEXT
#if HARNESS_TEST_UNISCRIBE
wprintf(L"[+] Testing the Uniscribe user-mode library.\n");
// Test some user-mode Windows Uniscribe functionality.
TestUniscribe(hDC, lpGlyphset);
#endif // HARNESS_TEST_UNISCRIBE
HeapFree(GetProcessHeap(), 0, lpGlyphset);
DeleteFont(hFont);
}
}
// Remove the font from the system.
ReleaseDC(NULL, hDC);
RemoveFontResourceW(szFontPath);
} while (0);
return 0;
}