Lablgtk: trouble loading fonts with Gdk.Font.load/loadset

Hello,

I am writing a GUI for a board game that is being implemented in OCaml. I’m using LablGtk. I tried following the tutorial on https://ocaml.org/learn/tutorials/introduction_to_gtk.html#Graph-widget, but when I run the graph/test example, I got an error indicating that the font being used in the strings being drawn on the main Drawable is not found. Upon closer look it appears that Gdk.Font.load returns Gpointer.null rather than the desired result, when provided with “--helvetica-medium-r-normal--120-*”. I’m not sure what direction to take from here. It’s somewhat critical for my GUI to contain text in an x,y plane versus the structure that using GLabels would entail, and the GUI should be implemented in OCaml . I can’t get Gdk.Font.load or Gdk.Font.load_fontset to provide anything other than GPointer.Null. Is there something wrong with how I have my fonts installed, or is it possible LablGtk is out of date with something I have installed? Does anyone have any ideas on how to fix this, or some alternative way I should be doing this? I’ve tried this tutorial code on OSX 10.11.6 as well as Ubuntu 17.10 with the same result. I installed gtk2 on mac using brew, and apt-get for ubuntu, and then I used OPAM to install lablgtk on both.

Thanks.

Try "Helvetica 12", instead. GTK has moved from X-server side fonts, which are defined the lke this --helvetica-medium-r-normal--120-*, to client side fonts where you specify the font name and size.

Ah, just tried, no luck. Is it deprecated? I see there is this other module Pango, which has a function that gives you a Pango.font from a string such as “Helvetica 12”. But I want to make use of LablGtk’s GMisc.drawable, which has a function that draws a string, and requires a Gdk.Font.

That’s strange. Out of curiosity I’ve downloaded the graph.ml and test.ml file and after changing the font_name to "Helvetica 12" it works.


Do you have Helvetika installed on your box?

I appear to have it if I look in the OSX font book. I also tried running fc-list and I see the word “Helvetica” around a few places. Does LablGtk use some specific place to look for fonts? I also tried Arial and Verdana to no avail.

I’ve never used OSX but my understanding is that it can use either the native renderer or X-Server running on top of it.

If you happen to use second option, it can be that the fonts available for X-Server are different from one available natively.
Probably you’re best option is to use a GTK based editor and open the font selection editor.

Where would I find a GDK-based editor? Also interestingly, I just tried changing to “Helvetica 12” on my Ubuntu box and got the same error.

Try opam install ocaml-top then from the open editor press F4. It may be the easiest on OSX. If it installs.

Just FYI, it seems unlikely to me that you have actual Helvetica available on your Ubuntu machine. The font is, after all, owned by Linotype, and is not free.

Similarly, as has been noted, if you’re using X rather than the native UI under OS X, you are almost certainly going to only have free fonts available.

So I installed ocaml-top and ran it, and I see it has some fonts when I press F4. I noticed there was FreeMono, so in the tutorial I tried “FreeMono 12” but I have the same problem. Tried a couple others, like Garuda.

Which free fonts would be available? I’ll take anything. I’ll take comic sans.

UPDATE: tried this both on OSX and Ubuntu but no luck.

Should I be using something other than Gdk.Font.load?

I’m afraid that the only advice, that I can offer now is the equivalent of fresh install. With opam this can be done as

opam switch gtk-bless-thy-fonts --alias-of=4.04.1

and then install labgtk in the new env

I am afraid I really don’t know enough about lablgtk myself to be of much more use either. Very sorry about that. :frowning:

Ah, thanks for the help anyways you guys. I was wondering if it would be worth checking out OCaml’s Graphics module, although it sounds like a pain to keep track of the event loop and stuff. Have you ever heard of anyone using LablGtk in conjunction with Graphics?

Try this in Ubuntu

let font_name = "-*-*-medium-r-normal-*-120-*"

In my case it displays something which chinese (?) characters. The first 2 * means any foundry, any type family.
You can see all fonts in Ubuntu running

xlsfonts

Picking one from those displayed and doing

let font_name = "-adobe-helvetica-medium-r-normal--0-0-0-0-p-0-iso8859-1"

Should work too.

Now a little summary as I understand it.

Gdk.Font.load works with X font names and relies on X-Server font rendering. Pango works with simple name like Helvetica 12.

Sorry for the confusion. Ocaml-top uses Pango for font rendering and displays font names in the new format.

PS. On Windows Gdk.Font.load "Helvetica 12" works which may be confusing

Dear god, it worked. Thank you so much. Been tearing my hair out trying to get this to work. Worked on Ubuntu, will try getting it up and running on OSX.

Weirdly, some of the fonts returned by xlsfonts didn’t work, gave me same error - but I tried a few and finally one worked (in my case it was some misc bold font).

I guess my question now is, it seems like bad practice to hardcode in some random font I find in xlsfonts. Is there some better way for me to do this?

Try

let font_name = "fixed"

It should work pretty much everywhere

That worked on Ubuntu for me, but not OSX. There must be something wrong with my OSX Lablgtk… Well, for my purposes just Ubuntu works. I really can’t thank you enough.

There are almost certainly calls you can use to enumerate the available fonts…

Hi, I decided that it may be worth trying to use the Pango fonts int graph example and here is the result; Updated labltgk example

I tested it only on Ubuntu, though

Also, I have experienced first-hand the joys of missing/scarce documentation. Looking for a way to create a Pango.context left some bruises.