[WS2_32_APITEST]
[reactos.git] / rostests / apitests / gdi32 / GetGlyphIndices.c
1 /*
2 * PROJECT: ReactOS api tests
3 * LICENSE: GPL - See COPYING in the top level directory
4 * PURPOSE: Test for GetGlyphIndices
5 * PROGRAMMERS: Ged Murphy
6 */
7
8 #include <apitest.h>
9 #include <wingdi.h>
10 #include <winuser.h>
11 #include <strsafe.h>
12
13
14 #define ok_lasterrornotchanged() \
15 ok_err(0x12345)
16
17 #define MAX_BMP_GLYPHS 0xFFFF
18
19 static LPVOID GetResource(LPCWSTR FontName, LPDWORD Size)
20 {
21 HRSRC hRsrc;
22 LPVOID Data;
23
24 hRsrc = FindResourceW(GetModuleHandleW(NULL), FontName, (LPCWSTR)RT_RCDATA);
25 if (!hRsrc) return NULL;
26
27 Data = LockResource(LoadResource(GetModuleHandleW(NULL), hRsrc));
28 if (!Data) return NULL;
29
30 *Size = SizeofResource(GetModuleHandleW(NULL), hRsrc);
31 if (*Size == 0) return NULL;
32
33 return Data;
34 }
35
36 static BOOL ExtractTTFFile(LPCWSTR FontName, LPWSTR TempFile)
37 {
38 WCHAR TempPath[MAX_PATH];
39 HANDLE hFile;
40 void *Data;
41 DWORD Size;
42 BOOL ret;
43
44 Data = GetResource(FontName, &Size);
45 if (!Data) return FALSE;
46
47 GetTempPathW(MAX_PATH, TempPath);
48 GetTempFileNameW(TempPath, L"ttf", 0, TempFile);
49
50 hFile = CreateFileW(TempFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
51 if (hFile == INVALID_HANDLE_VALUE) return FALSE;
52
53 ret = WriteFile(hFile, Data, Size, &Size, NULL);
54
55 CloseHandle(hFile);
56 return ret;
57 }
58
59 static BOOL InstallTempFont(LPWSTR TempFile)
60 {
61 if (ExtractTTFFile(L"ReactOSTestTahoma.ttf", TempFile))
62 {
63 if (AddFontResourceExW(TempFile, FR_PRIVATE, 0) > 0)
64 {
65 return TRUE;
66 }
67 }
68
69 return FALSE;
70 }
71
72 static VOID RemoveTempFont(LPWSTR TempFile)
73 {
74 BOOL Success;
75 Success = RemoveFontResourceExW(TempFile, FR_PRIVATE, 0);
76 ok(Success, "RemoveFontResourceEx() failed, we're leaving fonts installed : %lu\n", GetLastError());
77 DeleteFileW(TempFile);
78 }
79
80 static HFONT IntCreateFont(LPWSTR FontName)
81 {
82 LOGFONTW Font = { 0 };
83 Font.lfCharSet = DEFAULT_CHARSET;
84 wcsncpy(Font.lfFaceName, FontName, sizeof(Font.lfFaceName) / sizeof(Font.lfFaceName[0]));
85 return CreateFontIndirectW(&Font);
86 }
87
88 START_TEST(GetGlyphIndices)
89 {
90 WCHAR Glyphs[MAX_BMP_GLYPHS];
91 WORD Indices[MAX_BMP_GLYPHS];
92 WCHAR Single[2] = { L' ', UNICODE_NULL };
93 WCHAR TempTTFFile[MAX_PATH];
94 HFONT hFont;
95 HDC hdc;
96 int i;
97
98 if (!InstallTempFont(TempTTFFile))
99 {
100 skip("Failed to create ttf file for testing\n");
101 return;
102 }
103
104 hdc = CreateCompatibleDC(NULL);
105 ok(hdc != 0, "CreateCompatibleDC failed, skipping tests.\n");
106 if (!hdc) return;
107
108 hFont = IntCreateFont(L"ReactOSTestTahoma");
109 ok(hFont != NULL, "Failed to open the test font");
110 SelectObject(hdc, hFont);
111
112 SetLastError(0x12345);
113
114 /* Test NULL DC */
115 ok_int(GetGlyphIndicesW(NULL, Single, 1, Indices, 0), GDI_ERROR);
116 ok_lasterrornotchanged();
117
118 /* Test invalid DC */
119 ok_int(GetGlyphIndicesW((HDC)(ULONG_PTR)0x12345, Single, 1, Indices, 0), GDI_ERROR);
120 ok_lasterrornotchanged();
121
122 /* Test invalid params */
123 ok_int(GetGlyphIndicesW(hdc, NULL, 0, Indices, 0), GDI_ERROR);
124 ok_lasterrornotchanged();
125 ok_int(GetGlyphIndicesW(hdc, NULL, 1, Indices, 0), GDI_ERROR);
126 ok_lasterrornotchanged();
127 ok_int(GetGlyphIndicesW(hdc, Single, 1, NULL, 0), GDI_ERROR);
128 ok_lasterrornotchanged();
129
130 /* Test a single valid char */
131 Single[0] = L'a';
132 ok_int(GetGlyphIndicesW(hdc, Single, 1, Indices, 0), 1);
133 ok_lasterrornotchanged();
134 ok_int(Indices[0], 68);
135
136 /* Setup an array of all possible BMP glyphs */
137 for (i = 0; i < 4; i++)
138 Glyphs[i] = (WCHAR)i;
139
140 /* Test a string of valid chars */
141 StringCchCopyW(Glyphs, MAX_BMP_GLYPHS, L"0123");
142 ok_int(GetGlyphIndicesW(hdc, Glyphs, 4, Indices, 0), 4);
143 ok_lasterrornotchanged();
144 ok_int(Indices[0], 19);
145 ok_int(Indices[1], 20);
146 ok_int(Indices[2], 21);
147 ok_int(Indices[3], 22);
148
149 /* Setup an array of all possible BMP glyphs */
150 for (i = 0; i < MAX_BMP_GLYPHS; i++)
151 Glyphs[i] = (WCHAR)i;
152
153 /* Get all the glyphs */
154 ok_int(GetGlyphIndicesW(hdc,
155 Glyphs,
156 MAX_BMP_GLYPHS,
157 Indices,
158 GGI_MARK_NONEXISTING_GLYPHS), MAX_BMP_GLYPHS);
159
160 /* The first 32 are invalid and should contain 0xffff */
161 for (i = 0; i < 32; i++)
162 ok_int(Indices[i], 0xffff);
163
164 /* These are the first 2 valid chars */
165 ok(Indices[32] != 0xffff, "ascii char ' ' should be a valid char");
166 ok(Indices[33] != 0xffff, "ascii char '!' should be a valid char");
167
168 RemoveTempFont(TempTTFFile);
169 }
170