GNOME Bugzilla – Bug 748771
Add API to attach custom PangoFontMap to a font dialog instance
Last modified: 2015-05-12 19:25:24 UTC
Needed for implementing private application fonts...
gtk_font_chooser_set_font_map ?
Yep. With getter please.
Created attachment 302908 [details] [review] GtkFontChooser: Allow a custom font map This lets applications show their own fonts instead of or in addition to system fonts.
Quick implementation. To test, use e.g ./testfontchooser ~/.fonts/Hind-Medium.ttf ~/.fonts/Hind-Bold.ttf to get a font chooser which has just Hind Medium, Hind Bold, and the standard aliases (monospace, sans, serif).
Note that I had to define PANGO_ENABLE_BACKEND and include pangofc-fontmap.h in testfontchooser.c - is that intentional ?
Definitely not intentional. I'll check as soon as I can build gtk+. Thanks!
Review of attachment 302908 [details] [review]: ::: tests/testfontchooser.c @@ -68,0 +71,11 @@ + if (argc > 1) + { + FcConfig *config; ... 8 more ... Use pango_cairo_font_map_new_for_font_type(CAIRO_FONT_TYPE_FT). If cairo-ft is not available, skip this part of the test.
Created attachment 302917 [details] [review] GtkFontChooser: Allow a custom font map This lets applications show their own fonts instead of or in addition to system fonts.
Better patch - use that cairo_font_map api - no longer need the backend define now, yay. Also made GtkFontButton work, and added the same test code to testfontchooserdialog. I was idly wondering if apps can actually use this already, do they have to do anything to the font maps of their widgets to use fonts returned by such a font chooser ? I guess since we mostely deal with fonts in an 'unresolved' (textual) form, we do need a way to tell other widgets which font map to use to resolve them. That opens up an interesting question about inheritance of pango contexts / font maps.
To point that out, the example I gave with Hind happens to work because the font I'm 'adding' to the font map is in the reguar fontconfig paths anyway. If I had used an entirely custom font, things probably won't work...
Humm. Yes, I think it's desirable to be able to attach a fontmap to a widget too. Right now, what I had in mind, was that apps like GIMP or Inkscape use this with direct cairo / pango calls.
This builds, but the test doesn't work for me. Didn't get to debug. Does it work for you?
It worked to the extent described in comment 4: I got a font chooser which had just Hind, sans, serif, and monospace in it.
I suppose it worked before ebassi's changes. Does after, since fonts are loaded once, then fontmap set, and not loaded again. Currently the widget loads fonts in _init(). If I remove that then it "works". Ideally I want to avoid loading in _init() in case user wants to set the config. I suggest removing it from _init(). It's already called from style_updated. And then add a variant that forces loading, perhaps by clearing last_fontconfig_timestamp first. Also, the problem with saving the font_map in the widget's PangoContext is that it's lost if the screen changes. So we either have to save it somewhere else, or change gtkwidget to preserve it. I wonder if saving this in the gtkwidget is even a good idea. Apparently doing so does not change the fontmap use for rendering the dialog itself. I suppose because it's not inherited into the components that draw the dialog. Right? I think asking users to call pango_context_set_font_map() on the context returned by gtk_widget_get_pango_context() is legit, if we make gtkwidget preserve that forward across screen change. Thanks!
Created attachment 303004 [details] [review] GtkFontChooser: Allow a custom font map This lets applications show their own fonts instead of or in addition to system fonts.
lgtm. Tested and it works. Thanks! One issue: instead of FcConfigCreate(), use FcInitLoadConfig(). This way synthetic bold and italic also work.
And since this probably will end up being a demo of how to do this, perhaps add a comment with FcInitLoadConfigAndFonts() as well. That version will keep the system fonts. Or just push this, and I'll add add comments and push.
I think I'll add a commented 'custom font chooser' example in the 'pickers' example in gtk3-demo.
Attachment 303004 [details] pushed as 049cbc8 - GtkFontChooser: Allow a custom font map
Created attachment 303062 [details] [review] Fix precondition in gtk_font_chooser_get_font_map () We need to return a value here.
For reference, I discussed using custom fonts with pango+fontconfig in detail here: http://mces.blogspot.ca/2015/05/how-to-use-custom-application-fonts.html