-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpdfontloader.c
More file actions
47 lines (43 loc) · 1.39 KB
/
pdfontloader.c
File metadata and controls
47 lines (43 loc) · 1.39 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
// pdfontloader.c: a minimal Tcl C extension to load a font on Windows
// into the app's Private Font collection
// Dan Wilcox <danomatika@gmail.com> 2017
#include <tcl.h>
#include <windows.h>
#include <wingdi.h>
#if TCL_MAJOR_VERSION < 9
typedef int Tcl_Size;
#endif
static int
Pdfontloader_load(ClientData cdata, Tcl_Interp *interp,
int objc, Tcl_Obj *const objv[]) {
if(objc != 2) {
Tcl_WrongNumArgs(interp, 1, objv, "filename");
return TCL_ERROR;
}
Tcl_DString ds;
Tcl_Encoding unicode;
Tcl_Size pathlen = 0;
int ret = TCL_OK;
const char *path = Tcl_GetStringFromObj(objv[1], &pathlen);
Tcl_DStringInit(&ds);
unicode = Tcl_GetEncoding(interp, "unicode");
Tcl_UtfToExternalDString(unicode, path, pathlen, &ds);
if(!AddFontResourceExW((LPCWSTR)Tcl_DStringValue(&ds), FR_PRIVATE, NULL)) {
Tcl_SetObjResult(interp,
Tcl_ObjPrintf("couldn't load font: %s\n", path));
ret = TCL_ERROR;
}
Tcl_DStringFree(&ds);
Tcl_FreeEncoding(unicode);
return ret;
}
int DLLEXPORT Pdfontloader_Init(Tcl_Interp *interp) {
if(Tcl_InitStubs(interp, TCL_VERSION, 0) == NULL) {
return TCL_ERROR;
}
if(!Tcl_CreateObjCommand(interp, "pdfontloader::load",
Pdfontloader_load, NULL, NULL)) {
return TCL_ERROR;
}
return TCL_OK;
}