Query about OSX font laoding (probably I need Manolo's help!) [General Use]

14 views
Skip to first unread message

MacArthur, Ian (Leonardo, UK)

unread,
Sep 21, 2016, 5:42:02 AM9/21/16
to fltkg...@googlegroups.com
For a little project, I'd quite like to load some custom fonts.

Now, on OSX I usually do this be setting up some hooks in the bundles info.plist, viz:

<key>ATSApplicationFontsPath</key>
<string>Fonts</string>

And then stick the desired fonts into the "Fonts" folder in the bundle.

However, to make the code "more like" the X11 and WIN32 versions, it would be convenient if I could load the font for files "by name" at runtime, rather than via the bundle.

Now, I had a vague recollection I'd looked at this before (and not got it working...) and it looks to me as if what I *want* is probably CTFontManagerRegisterFontsForURL(...) but I have *no idea* how to use that to make it work.

I guess I'd want to make a small .m (or .mm ?) wrapper that exposed a C-callable API that takes a font path/name and "loads" the font such that a subsequent call to Fl::set_font(x, "fontname"); will find the face available.

Suggestions?

TIA,
--
Ian



Leonardo MW Ltd
Registered Office: Sigma House, Christopher Martin Road, Basildon, Essex SS14 3EL
A company registered in England & Wales. Company no. 02426132
********************************************************************
This email and any attachments are confidential to the intended
recipient and may also be privileged. If you are not the intended
recipient please delete it from your system and notify the sender.
You should not copy it or use it for any purpose nor disclose or
distribute its contents to any other person.
********************************************************************

Manolo

unread,
Sep 21, 2016, 7:34:55 AM9/21/16
to fltk.general, ian.ma...@leonardocompany.com


On Wednesday, 21 September 2016 11:42:02 UTC+2, MacArthur, Ian (Leonardo, UK) wrote:
For a little project, I'd quite like to load some custom fonts.

Now, on OSX I usually do this be setting up some hooks in the bundles info.plist, viz:

    <key>ATSApplicationFontsPath</key>
    <string>Fonts</string>

And then stick the desired fonts into the "Fonts" folder in the bundle.

However, to make the code "more like" the X11 and WIN32 versions, it would be convenient if I could load the font for files "by name" at runtime, rather than via the bundle.

Now, I had a vague recollection I'd looked at this before (and not got it working...) and it looks to me as if what I *want* is probably CTFontManagerRegisterFontsForURL(...) but I have *no idea* how to use that to make it work.

I guess I'd want to make a small .m (or .mm ?) wrapper that exposed a C-callable API that takes a font path/name and "loads" the font such that a subsequent call to Fl::set_font(x, "fontname"); will find the face available.

Suggestions?

TIA,
--
Ian

The approach illustrated below should work. No need to use objective-C.
Replace therein the font pathname and PostScript name by those relevant for you.

#include <FL/Fl_Window.H>
#include <FL/Fl_Box.H>
#include <FL/Fl.H>

#include <ApplicationServices/ApplicationServices.h>


int main(int argc, char **argv)
{
  CFErrorRef err;
  const char *fontfile = "/Users/XXXXX/Desktop/myZapfino.ttf"; // use here your font's pathname
  CFURLRef fontURL = CFURLCreateFromFileSystemRepresentation(NULL, (const UInt8*)fontfile, strlen(fontfile), false);
  bool b = CTFontManagerRegisterFontsForURL(fontURL, kCTFontManagerScopeProcess, &err);
  if (!b) {
    printf("fontURL is not recognized\n");
    exit(1);
  }
  Fl::set_font(FL_SYMBOL, "Zapfino"); // use here the font's PostScript name
  Fl_Window *window = new Fl_Window(360,180);
  Fl_Box *box = new Fl_Box(20,40,320,100,"Hello, World!");
  box->box(FL_UP_BOX);
  box->labelfont(FL_SYMBOL);
  box->labelsize(30);
  window->end();
  window->show();
  return Fl::run();
}
 

MacArthur, Ian (Leonardo, UK)

unread,
Sep 21, 2016, 8:54:49 AM9/21/16
to fltkg...@googlegroups.com

> The approach illustrated below should work.
> No need to use objective-C.

Thanks Manolo, that looks ideal - I hadn't realised it could be C-callable and had disappeared down some confusing maze that was leading me nowhere...
Your way is so much simpler and more sensible that where I was going.

Cheers,

MacArthur, Ian (Leonardo, UK)

unread,
Sep 22, 2016, 8:57:54 AM9/22/16
to fltkg...@googlegroups.com

Took Manolo's proposed approach to font loading on OSX and it works pretty well.

If anyone else is interested in loading application private fonts at runtime, I've added it to my dummy font-loading demo (should be attached if I remember!) to illustrate programmatic loading of fonts at runtime for OSX, WIN32 and X11/XFT platforms.

The "demo" also contains a (smallish) dummy test font, based on (and very much cut down from) George Durous "Musica" font, which has a pretty generous license, so it is not beyond the bounds of possibility that we could clean this up as a working sample to add to the "examples" folder.
(Or even build the loading of private fonts into fltk as a helper function - "fl_load_private_font()" etc. - is that the sort of thing people would use? I would, but then...)

Have fun.

Builds for me with "fltk-config --compile load-app-font.cxx && ./load-app-font" on all tested hosts.
(The WIN32 test was done in an Msys shell with mingw, but the code should be VS compatible too!)
lf-demo.zip
Reply all
Reply to author
Forward
0 new messages