Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FLTK doesn't use system UI font #466

Closed
CendioOssman opened this issue Jul 14, 2022 · 6 comments
Closed

FLTK doesn't use system UI font #466

CendioOssman opened this issue Jul 14, 2022 · 6 comments
Assignees
Labels
fixed The issue or PR was fixed.

Comments

@CendioOssman
Copy link
Contributor

Pretty much all platforms have a font dedicated to UI elements, that is extra tailored for that use case. Unfortunately FLTK defaults to something Helvetica compatible, which will give a worse appearance and also make FLTK applications look (even more) different¹ than ones using the native toolkit.

Right now applications can try to work around this. E.g. on Windows you can do:

  NONCLIENTMETRICS metrics;
  metrics.cbSize = sizeof(metrics);
  if (SystemParametersInfo(SPI_GETNONCLIENTMETRICS,
                           sizeof(metrics), &metrics, 0))
    Fl::set_font(FL_HELVETICA, metrics.lfMessageFont.lfFaceName);

On Linux it should also be possible, but it's a bit more complex. You need to decide on a toolkit to follow, e.g. GTK vs QT, and then dig around in their settings.

On macOS, it looks like it is impossible given FLTK's API. They seem to refuse to let you access the UI font using the name of the font, and must get a font object via special APIs.

¹ Fortunately the UI font is almost always a sans serif font, so they are at least very similar

@ManoloFLTK
Copy link
Contributor

What is the macOS special API mentionned above ? Is it CTFontCreateUIFontForLanguage() ? Does it produce a CTFontRef value ? That is the value the FLTK macOS code uses to set what font is used when drawing text. I could try to add a public API allowing to set the FLTK font from a CTFontRef.

@ManoloFLTK
Copy link
Contributor

ManoloFLTK commented Jul 28, 2022

I believe no new API is necessary, as shown by this code snippet :

#include <ApplicationServices/ApplicationServices.h>
  
  CTFontRef ft = CTFontCreateUIFontForLanguage(kCTFontUIFontSystem, 0, NULL);
  CFStringRef str = CTFontCopyFullName(ft);
  char fname[60];
  CFStringGetCString(str, fname, sizeof(fname), kCFStringEncodingUTF8);
  Fl_Fontsize size = CTFontGetSize(ft);
  CFRelease(ft);
// printf("%s [%d]\n", fname, size);
  Fl::set_font(FL_HELVETICA, fname);
// at this point, the system font is obtained by
  fl_font(FL_HELVETICA, size);

This requires macOS 10.5 or above.

@ManoloFLTK ManoloFLTK added the enhancement New feature or request label Jul 28, 2022
@CendioOssman
Copy link
Contributor Author

Many thanks. That actually worked. Tested on macOS 12.

I'm not sure what method I used, but I couldn't get it to accept any of the strings I tried to use. I also found posts (even from Apple people), stating that the system would reject even the correct string to force people to use the newer APIs that had constants for the system fonts. That evidently isn't true.

So, it seems everything can be done from the application, and FLTK doesn't need to change. :)

It might still be nice if FLTK defaults to the system UI font, though, if you feel it is worth the effort.

(minor note is that kCTFontUIFontSystem requires 10.8)

@Albrecht-S
Copy link
Member

It might still be nice if FLTK defaults to the system UI font, though, if you feel it is worth the effort.

@CendioOssman I don't think that this is appropriate, keeping backwards and cross-platform compatibility in mind. There would be at least eight fonts affected (FL_HELVETICA (proportional): normal, bold, italic, and bold italic) and the same for FL_COURIER (fixed font). These are the fonts that are most likely used by all applications. Loading other fonts than we have now by default would likely cause trouble because applications would look different, maybe font sizes and spacing would be different, etc..

That said, an additional API to overload specific fonts with all variants from alternate system UI fonts might make sense. A global flag set by a function call early in the program, i.e. before "the display is opened", might also be useful. However, such an API would need to be well defined and usable cross platform etc. etc.

@CendioOssman
Copy link
Contributor Author

(minor note is that kCTFontUIFontSystem requires 10.8)

Apparently, it just got renamed in 10.8 from kCTFontSystemFontType.

@ManoloFLTK
Copy link
Contributor

Closing since the required solution under macOS is provided here.

@ManoloFLTK ManoloFLTK self-assigned this Nov 1, 2023
@ManoloFLTK ManoloFLTK added fixed The issue or PR was fixed. and removed enhancement New feature or request labels Nov 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixed The issue or PR was fixed.
Projects
None yet
Development

No branches or pull requests

3 participants