-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathphpwb_font.c
More file actions
291 lines (237 loc) · 9.02 KB
/
phpwb_font.c
File metadata and controls
291 lines (237 loc) · 9.02 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
/*******************************************************************************
WINBINDER - The native Windows binding for PHP for PHP
Copyright Hypervisual - see LICENSE.TXT for details
Author: Rubem Pechansky (https://github.com/crispy-computing-machine/Winbinder)
ZEND wrapper for font functions
*******************************************************************************/
//----------------------------------------------------------------- DEPENDENCIES
#include "phpwb.h"
//------------------------------------------------------------------ TTF STRUCTS
typedef struct _tagTT_OFFSET_TABLE {
USHORT uMajorVersion;
USHORT uMinorVersion;
USHORT uNumOfTables;
USHORT uSearchRange;
USHORT uEntrySelector;
USHORT uRangeShift;
} TT_OFFSET_TABLE;
typedef struct _tagTT_TABLE_DIRECTORY {
char szTag[4];
ULONG uCheckSum;
ULONG uOffset;
ULONG uLength;
} TT_TABLE_DIRECTORY;
typedef struct _tagTT_NAME_TABLE_HEADER {
USHORT uFSelector;
USHORT uNRCount;
USHORT uStorageOffset;
} TT_NAME_TABLE_HEADER;
typedef struct _tagTT_NAME_RECORD {
USHORT uPlatformID;
USHORT uEncodingID;
USHORT uLanguageID;
USHORT uNameID;
USHORT uStringLength;
USHORT uStringOffset;
} TT_NAME_RECORD;
//----------------------------------------------------------- EXPORTED FUNCTIONS
static int wbFontHeightToPoints(int logicalHeight)
{
HDC hdc;
int absLogicalHeight;
int points;
absLogicalHeight = logicalHeight < 0 ? -logicalHeight : logicalHeight;
hdc = GetDC(NULL);
if (!hdc)
return absLogicalHeight;
points = MulDiv(absLogicalHeight, 72, GetDeviceCaps(hdc, LOGPIXELSY));
ReleaseDC(NULL, hdc);
return points;
}
// Creates a font and stores it in the font cache
ZEND_FUNCTION(wb_create_font)
{
zend_long height = 10, color = 0x000000, flags = 0;
char *name;
size_t name_len;
TCHAR *wcs = 0;
zend_bool color_isnull;
zend_bool flags_isnull;
// if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl|ll", &name, &name_len, &height, &color, &flags) == FAILURE)
// ZEND_PARSE_PARAMETERS_START() takes two arguments minimal and maximal parameters count.
ZEND_PARSE_PARAMETERS_START(2, 4)
Z_PARAM_STRING(name,name_len)
Z_PARAM_LONG(height)
Z_PARAM_OPTIONAL
Z_PARAM_LONG_OR_NULL(color, color_isnull)
Z_PARAM_LONG_OR_NULL(flags, flags_isnull)
ZEND_PARSE_PARAMETERS_END();
wcs = Utf82WideChar(name, name_len);
RETURN_LONG(wbCreateFont(wcs, height, color, flags));
}
// Destroys a font or all created fonts
ZEND_FUNCTION(wb_destroy_font)
{
zend_long nfont = 0;
zend_bool nfont_isnull = 0;
// if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &nfont) == FAILURE)
// ZEND_PARSE_PARAMETERS_START() takes two arguments minimal and maximal parameters count.
ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_LONG_OR_NULL(nfont, nfont_isnull)
ZEND_PARSE_PARAMETERS_END();
RETURN_BOOL(wbDestroyFont(nfont));
}
// Applies a font to a control
ZEND_FUNCTION(wb_set_font)
{
zend_long pwbo, nfont = 0, redraw = 1;
zend_bool nfont_isnull, redraw_isnull;
// if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|ll", &pwbo, &nfont, &redraw) == FAILURE)
// ZEND_PARSE_PARAMETERS_START() takes two arguments minimal and maximal parameters count.
ZEND_PARSE_PARAMETERS_START(1, 3)
Z_PARAM_LONG(pwbo)
Z_PARAM_OPTIONAL
Z_PARAM_LONG_OR_NULL(nfont, nfont_isnull)
Z_PARAM_LONG_OR_NULL(redraw, redraw_isnull)
ZEND_PARSE_PARAMETERS_END();
RETURN_BOOL(wbSetControlFont((PWBOBJ)pwbo, nfont, redraw));
}
// Gets font details by font id or control/window object
ZEND_FUNCTION(wb_get_font)
{
zend_long source;
PFONT pfont = NULL;
LOGFONT lf;
HFONT hFont;
char *name;
int name_len;
ZeroMemory(&lf, sizeof(LOGFONT));
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_LONG(source)
ZEND_PARSE_PARAMETERS_END();
if (wbIsValidFontId((int)source))
{
pfont = wbGetFont((int)source);
if (!pfont)
RETURN_FALSE;
array_init(return_value);
name = WideChar2Utf8(pfont->pszName ? pfont->pszName : TEXT(""), &name_len);
add_assoc_stringl(return_value, "name", name, name_len);
add_assoc_long(return_value, "height", wbFontHeightToPoints(pfont->nHeight));
add_assoc_long(return_value, "color", pfont->color);
add_assoc_long(return_value, "flags", pfont->dwFlags);
add_assoc_long(return_value, "handle", (LONG_PTR)pfont->hFont);
add_assoc_long(return_value, "id", source);
return;
}
if (wbIsWBObj((void *)source, FALSE))
{
PWBOBJ pwbo = (PWBOBJ)source;
if (!IsWindow(pwbo->hwnd))
RETURN_FALSE;
hFont = (HFONT)SendMessage(pwbo->hwnd, WM_GETFONT, 0, 0);
if (!hFont || !GetObject(hFont, sizeof(LOGFONT), &lf))
RETURN_FALSE;
pfont = wbGetFontFromHandle(hFont);
array_init(return_value);
name = WideChar2Utf8(lf.lfFaceName, &name_len);
add_assoc_stringl(return_value, "name", name, name_len);
add_assoc_long(return_value, "height", wbFontHeightToPoints(lf.lfHeight));
add_assoc_long(return_value, "color", pfont ? pfont->color : NOCOLOR);
add_assoc_long(return_value, "flags",
(lf.lfWeight >= FW_BOLD ? FTA_BOLD : 0) |
(lf.lfItalic ? FTA_ITALIC : 0) |
(lf.lfUnderline ? FTA_UNDERLINE : 0));
add_assoc_long(return_value, "handle", (LONG_PTR)hFont);
return;
}
RETURN_FALSE;
}
ZEND_FUNCTION(wb_get_ttf_info)
{
char *file;
size_t file_size;
php_stream *fhnd = NULL;
char real_path[MAXPATHLEN];
TT_OFFSET_TABLE ttOffsetTable;
TT_TABLE_DIRECTORY tblDir;
TT_NAME_TABLE_HEADER ttNTHeader;
TT_NAME_RECORD ttRecord[20];
BOOL bFound = FALSE;
char csTemp[4096];
int i,j,k=0;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STRING_OR_NULL(file, file_size)
ZEND_PARSE_PARAMETERS_END();
if(!realpath(file, real_path)) RETURN_BOOL(FALSE);
fhnd = php_stream_fopen(real_path, "rb", NULL);
if(!fhnd) RETURN_BOOL(FALSE);
if(!php_stream_read(fhnd, (char *)&ttOffsetTable, sizeof(TT_OFFSET_TABLE))) {
php_stream_close(fhnd);
RETURN_BOOL(FALSE);
}
ttOffsetTable.uNumOfTables = SWAPWORD(ttOffsetTable.uNumOfTables);
ttOffsetTable.uMajorVersion = SWAPWORD(ttOffsetTable.uMajorVersion);
ttOffsetTable.uMinorVersion = SWAPWORD(ttOffsetTable.uMinorVersion);
// Check is this is a true type font and the version is 1.0
if(ttOffsetTable.uMajorVersion != 1 || ttOffsetTable.uMinorVersion != 0)
RETURN_BOOL(FALSE);
for(i = 0; i< ttOffsetTable.uNumOfTables; i++) {
php_stream_read(fhnd, (char *)&tblDir, sizeof(TT_TABLE_DIRECTORY));
ZeroMemory(csTemp, sizeof(csTemp));
strncpy(csTemp, tblDir.szTag, 4);
if(!strcasecmp(csTemp,"name")){
tblDir.uLength = SWAPLONG(tblDir.uLength);
tblDir.uOffset = SWAPLONG(tblDir.uOffset);
bFound = TRUE;
break;
}
}
if(bFound){
php_stream_seek(fhnd, tblDir.uOffset, SEEK_SET);
php_stream_read(fhnd, (char *)&ttNTHeader, sizeof(TT_NAME_TABLE_HEADER));
ttNTHeader.uNRCount = SWAPWORD(ttNTHeader.uNRCount);
ttNTHeader.uStorageOffset = SWAPWORD(ttNTHeader.uStorageOffset);
for(i=0; i<ttNTHeader.uNRCount && i < 20; i++){
php_stream_read(fhnd, (char *)&ttRecord[i], sizeof(TT_NAME_RECORD));
ttRecord[i].uNameID = SWAPWORD(ttRecord[i].uNameID);
ttRecord[i].uStringLength = SWAPWORD(ttRecord[i].uStringLength);
ttRecord[i].uStringOffset = SWAPWORD(ttRecord[i].uStringOffset);
}
array_init(return_value);
for(j = 0; j < i; j++) {
if(ttRecord[j].uNameID < k) continue;
if(ttRecord[j].uStringLength > 4095) ttRecord[j].uStringLength = 4095;
php_stream_seek(fhnd, tblDir.uOffset + ttNTHeader.uStorageOffset + ttRecord[j].uStringOffset, SEEK_SET);
ZeroMemory(csTemp, sizeof(csTemp));
php_stream_read(fhnd, csTemp, ttRecord[j].uStringLength);
if(!strlen(csTemp)) continue;
k = ttRecord[j].uNameID;
switch(ttRecord[j].uNameID){
case 0: add_assoc_string(return_value, "copyright", csTemp); break;
case 1: add_assoc_string(return_value, "name", csTemp); break;
case 2: add_assoc_string(return_value, "subfamily", csTemp); break;
case 3: add_assoc_string(return_value, "subfamily_id", csTemp); break;
case 4: add_assoc_string(return_value, "full_name", csTemp); break;
case 5: add_assoc_string(return_value, "version", csTemp); break;
case 6: add_assoc_string(return_value, "postscript_name", csTemp); break;
case 7: add_assoc_string(return_value, "trademark", csTemp); break;
case 8: add_assoc_string(return_value, "manufacturer", csTemp); break;
case 9: add_assoc_string(return_value, "designer", csTemp); break;
case 10: add_assoc_string(return_value, "description", csTemp); break;
case 11: add_assoc_string(return_value, "vendor_url", csTemp); break;
case 12: add_assoc_string(return_value, "designer_url", csTemp); break;
case 13: add_assoc_string(return_value, "license", csTemp); break;
case 14: add_assoc_string(return_value, "license_url", csTemp); break;
case 16: add_assoc_string(return_value, "preferre_family", csTemp); break;
case 17: add_assoc_string(return_value, "preferre_subfamily", csTemp); break;
case 18: add_assoc_string(return_value, "compat_full_name", csTemp); break;
case 19: add_assoc_string(return_value, "sample_text", csTemp); break;
}
}
}
php_stream_close(fhnd);
if(!bFound) RETURN_BOOL(FALSE);
}
//------------------------------------------------------------------ END OF FILE