Is font attributes available (original) (raw)
January 14, 2024, 4:16am 1
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,
gwillems January 22, 2024, 10:30pm 2
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…
oneeyeman1 (Igor K) January 22, 2024, 10:51pm 3
Let me rephrase my issue.
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.
cpb January 23, 2024, 1:11pm 4
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()
oneeyeman1 (Igor K) January 23, 2024, 2:00pm 5
@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.
cpb January 23, 2024, 2:41pm 6
@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.
gwillems January 23, 2024, 4:27pm 7
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…)
oneeyeman1 (Igor K) January 23, 2024, 5:09pm 8
Hi,
You don’t read the question carefully…
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.
Thank you.
chrisaw (Chris Williams) January 23, 2024, 6:23pm 9
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 bysizes
and 0 at the location pointed to byn_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.
oneeyeman1 (Igor K) January 24, 2024, 2:15am 10
@chrisaw ,
I’m now curios how font panel does it?
Thank you.
chrisaw (Chris Williams) January 24, 2024, 2:46am 11
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
};
oneeyeman1 (Igor K) January 24, 2024, 2:53am 12
@chrisaw ,
OK, so I can pretty much use the same technique.
Thank you…
oneeyeman1 (Igor K) January 24, 2024, 5:57am 13
@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…
chrisaw (Chris Williams) January 24, 2024, 6:54am 14
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.
oneeyeman1 (Igor K) January 25, 2024, 1:49am 15
@chrisaw ,
I have a FaceName, not FontFamily…
Thank you.
chrisaw (Chris Williams) January 25, 2024, 3:21am 16
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?
oneeyeman1 (Igor K) January 25, 2024, 4:16am 17
@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.
chrisaw (Chris Williams) January 25, 2024, 6:20am 18
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 GtkTextTag
s. There are properties for each of those options.
If you’re instead using GtkLabel, you’d have to drop down to using PangoAttrList
s 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?
oneeyeman1 (Igor K) January 25, 2024, 7:11am 19
@chrisaw ,
Im painting the text myself, so low leve things.
Thank you.
oneeyeman1 (Igor K) February 2, 2024, 6:33am 20
@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.