Pango::FontDescription::to_filename return value has no connection to any physical file (original) (raw)
September 18, 2023, 7:13am 1
Hi all,
I’m using the library from freetype.org to load a font into OpenGL. For now, the filename passed to FT_New_Face is given manually, but these filenames tend to change during Fedora upgrades.
Looking at pango_font_description_to_filename (accessed through gtk_widget_get_pango_context and pango_context_get_font_description), I’d hoped to get a filename located in /usr/share/fonts, such that I can share the font specified in CSS and used across my GtkWindows with OpenGL, but instead I get a string that has no connection to any physical file at all.
Please advise,
Best regards,
Mischa Baars.
lb90 September 18, 2023, 12:41pm 2
Hi @MischaBaars! Pango.FontDescription.to_filename doesn’t return the path to the font file, it’s just a way to serialize the PangoFontDescription structure to a string. I suggest that:
- Get a
PangoCairoFontMap
for FreeType fonts: call pango_cairo_font_map_new_for_font_type with theCAIRO_FONT_TYPE_FT
argument - Call pango_font_map_load_font to get a
PangoFont
from yourPangoFontDescription
- Get the underlying
hb_font_t
using pango_font_get_hb_font - Get the
FT_Face
using eitherhb_ft_font_get_face
or hb_ft_font_lock_face
lb90 September 18, 2023, 12:52pm 3
Note that unless you want to use advanced rendering features of FreeType, you can use Pango directly to render into a Cairo image surface
MischaBaars (Mischa Baars) September 19, 2023, 11:01am 4
Hi,
Migrating to HarfBuzz would only make things complicated. I don’t use any GSK or Cairo related functions either. I use OpenGL and the GtkGLArea to do some font related rendering. I would like to keep it that way for now.
All I’d like to know from GTK is the name of the font file it uses, when I tell the application to use a particular font, i.e.
window *
{
font-family: …;
}
Because the during a Fedora upgrade, the CSS syntax stays the same, but most likely the names of the font files in /usr/share/fonts/ will have changed.
Is it possible, and how do I do it?
lb90 September 19, 2023, 12:37pm 5
PangoFontDescription Is a logical font description, only a PangoFontMap can map that description to a physical font. If you want the same PangoFontMap used by GTK, just call pango_cairo_font_map_get_default on the UI thread. Anyway you don’t have to use harfbuzz, it’s just an intermediate step to get the FT_Face
back.
Note that a font is not always backed by a file; it could be created from in-memory data, a resource bundle or a remote service. In addition, some font files provide multiple font faces.
That said, perhaps there are FontConfig or FreeType API that give you back a file path, but I don’t know any.
MischaBaars (Mischa Baars) September 20, 2023, 9:53am 6
Thank you for this information. Let me try.
MischaBaars (Mischa Baars) September 22, 2023, 10:07am 7
Ok. These are my findings:
The font family name can be retrieved using gtk_widget_get_pango_context() → pango_context_get_font_description() → pango_font_description_get_family(), but the output relates only to input from GtkSettings::gtk-font-name and does not relate to CSS input.
I fear it’s not only the /usr/share/fonts file that can not be retrieved, but when specified from CSS, it’s the font-family as well.
MischaBaars (Mischa Baars) September 24, 2023, 12:29pm 8
Also I was unable to find a function that makes changes to pango_font_description permanent. Calling gtk_widget_queue_draw() is insufficient.
I think my only option is to inline all CSS. That way I have the font-family available at all times.
system (system) Closed October 24, 2023, 12:29pm 9
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.