Is font attributes available

Hi, ALL,
I now there is a font selection dialog in GTK which will let you choose a font.
I didn’t look at its code but I presume there is a code to get an individual characteristics for a given font.

Lets say my application displays such a dialog and the dialog knows about the specific sizes of the font chosen, whether the font is bold, italic or underline.

What I wonder is whether this info is available to the application code?

What Id like to do is to display 2 combo boxes and 3 buttons inside a toolbar to show the font selected for the object user selected.

I can probably show one combo box displaying font and then a second one displaying the sizes, but I will still need to know what sizes are supported for this specific font.

So what is the recommendation here?

Displaying 5 controls or just 2?

And how to find out which font characteristics are there and the sizes that are supported.

Thank you,

Hi,

I’m not sure I understand what you try to do, but in general there are many many parameters that can affect how the font is rendered.

The font are typically set by CSS themes, also it’s possible to change the font properties of each glyph individually, for example using PangoAttributes. GtkLabel has a gtk_label_get_attributes API to retrieve them.

Without more context, it will be hard to help you…

@gwillems ,

Let me rephrase my issue. :wink:

I have a custom control which contains text as well.

By default the text is using the default font.

The program is also contains a toolbar which ideally would contain a combo box with the font names, combo box with the possible sizes that is available for this font and 3 buttons “Bold”, “Italic” and “Underlined”.

So when the user clicks “Bold” button the font on the text in my custom control will become bold.

Now I can have all 5 controls in my toolbar, just like I explained here or just try to mimic the font selection dialog/panel in GTK.

I hope I didn’t confuse you further…

Thank you.

Hey Oneeymotron, try changing via add_css_class()
eg:

string mycsses = """
    .bold { font-weight: 9000; }
    .italian { font-style: goddamnitalians; }
    .underline { font-decoration: underline; }
""";
Gtk.CssProvider mycsp = new Gtk.CssProvider();
mysp.load_from_string(mycsses);
Gdk.Display thisdisplay = Gdk.Display.get_default();
Gtk.StyleContext.add_provider_for_display(thisdisplay, mycsp, Gtk.STYLE_PROVIDER_PRIORITY_USER);

… then add/remove classes conditonaly as required via add_css_class() & set_css_classes()

@cpb ,
I know how to change the font.
My problem is to display sizes for a specific font…
In other words - if i choose font named X how do i know what size this font have?

Thank you.

@oneeymandus
Gotcha. Only way I’ve been doing this is in Cairo via extents… and monospace… and assumptions. No experience doing this in Gtk sorry. I suspect the answer lies here.

Do you mean the size in pixels?

You can do something like this (in Python):

layout = Pango.Layout.new(widget.get_pango_context())
layout.set_text("M")
print(layout.get_size())

that will give you the size of the character “M” in Pango units (1024 unit for 1 pixel, if I remember well. Not sure when scaling is used…)

Hi,
You don’t read the question carefully… :wink:

Let’s say I have selected a font X. This font X has available sizes of 8, 10, 14, 24, 36 and 72
Now there is another font - font Y, that have the available sizes as 8, 9, 10, 12, 14, 16, 20, 24, 36, 72, 104 and 228

So, when I select a font X I want to know the list of available sizes for the font X. And when I choose font Y - list of available sizes for font Y.

I don’t want to now the current default size for the font. I want to know all possible sizes I can choose.

If you are familiar with Windows, think about font selection dialog there. It is split for font name and all available sizes for that font.

Now as said - I can probably use different layout when working with GTK/Linux (display just a combo box and a button), but I’d like to explore the possibility of not doing conditional compilation. :wink:

Thank you.

I think you’re looking for Pango.FontMap. You can get that from PangoCairo.FontMap.get_default() or Pango.Context.get_font_map(). From there, you can get font families and individual font faces. Pango.FontFace.list_sizes() exists, but it says:

This is only applicable to bitmap fonts. For scalable fonts, stores NULL at the location pointed to by sizes and 0 at the location pointed to by n_sizes.

If you want to provide typical word processor-style font size choices, I think you’ll have to create that list yourself in most cases.

1 Like

@chrisaw ,
I’m now curios how font panel does it?

Thank you.

If you mean the marks on the slider in the font chooser dialog, those use a hardcoded fallback when not specified by the font (which only happens with bitmap fonts, apparently):

      static const int fallback_sizes[] = {
        6, 8, 9, 10, 11, 12, 13, 14, 16, 20, 24, 36, 48, 72
      };

@chrisaw ,
OK, so I can pretty much use the same technique.

Thank you…

@chrisaw ,
When I said “font X” or “font Y”, I meant I have a string representation of face name, i.e. “MS Sans Seriif” or “Segoe Print”.

How do I get FontMap or FontContext out of it?
Can I get some code please?

Thank you…

I’m not totally clear on your use case. Where are you getting this string from?

You would start with a FontMap; that’s the full set of available fonts. I don’t know if it matters whether you get it with PangoCairo.FontMap.get_default() or widget.get_pango_context().get_font_map(). In practice, those appear to yield the same object.

If you have a string representation of a font family, you can use fontmap.get_family() to get a Pango.FontFamily.

If the string you have contains more detail than that, you could use Pango.FontDescription.from_string() to try to parse it.

@chrisaw ,
I have a FaceName, not FontFamily…

Thank you.

What does that mean exactly? You’ve written ‘FaceName’ like it’s a type, but it isn’t. (PangoFontFace is though).

It would be less effort to use a GtkFontChooserButton, but doing it yourself shouldn’t be too difficult, and it would probably make a better UI for something like a word processor. I’m not clear on what you’re making though. How are you ultimately going to use the selected font?

@chrisaw ,

What does that mean exactly? You’ve written ‘FaceName’ like it’s a type, but it isn’t. (PangoFontFace is though).

I’m mostly develop on Windows, so I’m using Windows terminology.

Are you familiar with MS ACCESS?
Imagine I have a toolbar with the combo box for face name, combo box for font sizes and 3 buttons for bold, italic and underline.

When I change the selection in one of the combo boxes or click the button the field in the view will reflect the font change.

This is why I’ve been asking what you’re going to do with the font information.

If you’re making something based on GtkTextView, then you can use GtkTextTags. There are properties for each of those options.

If you’re instead using GtkLabel, you’d have to drop down to using PangoAttrLists with gtk_label_set_attributes()

If you go lower than that, you’d be using PangoLayout directly.

Or are you not intending to use GTK to render the text?

@chrisaw ,
Im painting the text myself, so low leve things.

Thank you.

@chrisaw ,
As said - I do know how to change the font.
All I’m looking for is a way to find and display the possible sizes inside the “sizecombobox” for a specific PangoFontFace.

Can I get some code, please?

Thank you.