Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to use new fonts without installing ?

166 views
Skip to first unread message

Aldo Buratti

unread,
Mar 26, 2017, 1:18:04 PM3/26/17
to
I need to use some special fonts for my app.

On Windows I can use a small C extension as explained in http://wiki.tcl.tk/22262

For Linux I only found a partial solution, that is, copy my *.ttf fonts under some predefined directories ( $HOME/.fonts )
This solution works, but it's unsatisfactory because I'd prefer to use my *.ttf without installing it even under a local, temporary directory.

Surely the full solution will require a C extension. Any suggestions ?

Aldo

Matthew Hiles

unread,
Mar 27, 2017, 9:14:55 AM3/27/17
to
I have faced this same exact problem in a game client.

My solution is as you say. Check if the desired font is present on the system, and if it is not, copy to $HOME/.fonts and restart the program.

From what I understand of the font server on X11 there isn't really a better solution.

On windows I used twapi.

You could access an in-memory directory like /dev/shm; make a new directory and temporarily add it to the font directory:

xset +fp /dev/shm/fonts

But if anyone knows a way to just force a font to be available to Tk without having to copy the font file to a directory, I'd love to know as well.

Aldo Buratti

unread,
Jul 17, 2017, 6:38:14 AM7/17/17
to
Now you can try with *extrafont* package.
It works on Windows/Linux/Mac.

See http://wiki.tcl.tk/48903 for details.

Matthew Hiles

unread,
Jul 17, 2017, 8:30:54 AM7/17/17
to
This looks fantastic! Thank you so much!

Hey! This adds some serious functionality that was missing from Tk, any chance it can be added as part of the standard Tk distribution?

Aldo Buratti

unread,
Jul 17, 2017, 9:39:46 AM7/17/17
to
Well, extrafont need more in-field testing for the different Linux platforms,
but I think, YES, it could be included in the Tk core, extending the 'font' command.

The strange thing about this development is that it took just few lines of code; if you skip the standard boilerplate code used for writing any C extension, you could find that it took just 1 line of C-code for Linux, 5 lines of C-code for Mac, and 5 lines of C-code for Windows.

I'm not an expert of these font-management APIs for those platforms; I just did a copy&paste . Maybe a code review could be useful... and more testing, of course.

AB

Ralf Fassel

unread,
Jul 17, 2017, 12:26:14 PM7/17/17
to
* Aldo Buratti <aldo.w....@gmail.com>
| I'm not an expert of these font-management APIs for those platforms; I
| just did a copy&paste . Maybe a code review could be useful... and
| more testing, of course.

On Windows, you do

unicode = Tcl_GetEncoding(_interp, "unicode");
Tcl_UtfToExternalDString(unicode, path, len, &ds);
res = AddFontResourceExW((LPCWSTR)Tcl_DStringValue(&ds), FR_PRIVATE, NULL);

i.e. basically passing an 'unicode' encoded string to
AddFontResourceExW() which expects a 'wide' string. I'm not sure
whether this is 100% correct in all cases of all unicode strings out
there.

We usually do
- convert Tcl-to-utf8
- MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, utf-8-char*, utf-8-len, 0, 0);
to obtain the required length, allocate the wbuffer and perform the conversion:
- MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, utf-8-char*, utf-8-len, wbuffer, reqlen);

I had the impression that 'unicode' and Microsoft's wide strings are two
different beasts which very much look alike, but not 100% (I might be
wrong here).

My EUR 0.02.
R'
0 new messages