After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 748771 - Add API to attach custom PangoFontMap to a font dialog instance
Add API to attach custom PangoFontMap to a font dialog instance
Status: RESOLVED FIXED
Product: gtk+
Classification: Platform
Component: .General
unspecified
Other Linux
: Normal enhancement
: ---
Assigned To: gtk-bugs
gtk-bugs
Depends on:
Blocks:
 
 
Reported: 2015-05-01 20:37 UTC by Behdad Esfahbod
Modified: 2015-05-12 19:25 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
GtkFontChooser: Allow a custom font map (9.52 KB, patch)
2015-05-05 03:22 UTC, Matthias Clasen
none Details | Review
GtkFontChooser: Allow a custom font map (13.11 KB, patch)
2015-05-05 10:58 UTC, Matthias Clasen
none Details | Review
GtkFontChooser: Allow a custom font map (19.95 KB, patch)
2015-05-07 03:40 UTC, Matthias Clasen
committed Details | Review
Fix precondition in gtk_font_chooser_get_font_map () (873 bytes, patch)
2015-05-08 09:00 UTC, Víctor Manuel Jáquez Leal
none Details | Review

Description Behdad Esfahbod 2015-05-01 20:37:42 UTC
Needed for implementing private application fonts...
Comment 1 Matthias Clasen 2015-05-04 11:56:31 UTC
gtk_font_chooser_set_font_map ?
Comment 2 Behdad Esfahbod 2015-05-04 18:08:12 UTC
Yep.  With getter please.
Comment 3 Matthias Clasen 2015-05-05 03:22:23 UTC
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.
Comment 4 Matthias Clasen 2015-05-05 03:24:12 UTC
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).
Comment 5 Matthias Clasen 2015-05-05 03:25:48 UTC
Note that I had to define PANGO_ENABLE_BACKEND and include pangofc-fontmap.h in testfontchooser.c - is that intentional ?
Comment 6 Behdad Esfahbod 2015-05-05 06:50:37 UTC
Definitely not intentional.  I'll check as soon as I can build gtk+.  Thanks!
Comment 7 Behdad Esfahbod 2015-05-05 06:56:18 UTC
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.
Comment 8 Matthias Clasen 2015-05-05 10:58:57 UTC
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.
Comment 9 Matthias Clasen 2015-05-05 11:06:04 UTC
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.
Comment 10 Matthias Clasen 2015-05-05 11:07:10 UTC
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...
Comment 11 Behdad Esfahbod 2015-05-05 17:41:48 UTC
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.
Comment 12 Behdad Esfahbod 2015-05-05 21:33:03 UTC
This builds, but the test doesn't work for me.  Didn't get to debug.  Does it work for you?
Comment 13 Matthias Clasen 2015-05-06 03:32:58 UTC
It worked to the extent described in comment 4: I got a font chooser which had just Hind, sans, serif, and monospace in it.
Comment 14 Behdad Esfahbod 2015-05-06 06:13:47 UTC
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!
Comment 15 Matthias Clasen 2015-05-07 03:40:14 UTC
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.
Comment 16 Behdad Esfahbod 2015-05-07 06:49:23 UTC
lgtm.  Tested and it works.  Thanks!

One issue: instead of FcConfigCreate(), use FcInitLoadConfig().  This way synthetic bold and italic also work.
Comment 17 Behdad Esfahbod 2015-05-07 06:51:01 UTC
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.
Comment 18 Matthias Clasen 2015-05-07 10:49:47 UTC
I think I'll add a commented 'custom font chooser' example in the 'pickers' example in gtk3-demo.
Comment 19 Matthias Clasen 2015-05-07 11:34:54 UTC
Attachment 303004 [details] pushed as 049cbc8 - GtkFontChooser: Allow a custom font map
Comment 20 Víctor Manuel Jáquez Leal 2015-05-08 09:00:28 UTC
Created attachment 303062 [details] [review]
Fix precondition in gtk_font_chooser_get_font_map ()

We need to return a value here.
Comment 21 Behdad Esfahbod 2015-05-12 19:25:24 UTC
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