Pango Programming Guide

zhaozj2021-02-16  88

If you're using any kind of non-default fonts, you'll want to convert your code to use the new PangoFontDescription rather than GdkFont PangoFontDescription is much, much easier to use;. To load a GdkFont you had to specify an awkward X font description string, and the resulting GdkFont was specific to a certain locale;. it would not work for all languages ​​moreover, there were no fonts you could choose that everyone using your application was guaranteed to have, so a call to gdk_font_load () Could Always Fail.

PangoFontDescription is much higher-level The exact font technology used by GTK varies according to GDK target and how GTK was compiled;. Underneath Pango, the real implementation could be Win32 fonts, XRender fonts, traditional X fonts, TrueType fonts, or even a mixture . Pango can even assemble a collection of locale-specific fonts and use those to emulate a single Unicode font Programmers do not have to worry about this;. they specify high-level font details such as "Sans Italic 12".

A PangoFontDescription has a stringified form, described in the Pango documentation. "Sans Italic 12" is an example of such a string. The easiest way to obtain a PangoFontDescription is to call pango_font_description_from_string (). The font families "Sans", "Monospace" And "Serif" Are Guaranteed to EXIST ON All GTK Installations; these Will Never Fail To Load.

If you were using the font field in GtkStyle to change the font for a widget, you'll need to use the new font_desc field instead. However, when converting your code from GdkFont, ask yourself if setting style-> font remains the easiest thing to do; gtk_label_new_from_markup () is a handy alternative provided for GtkLabel You can pass in a string marked up with HTML-like tags affecting text attributes such as color, text size, font family, and so on The Pango documentation describes all the.. tags you can use. If you do still want to modify the style, probably you can use the new gtk_widget_modify_font () function instead of fooling with styles manually. gtk_widget_modify_font () works properly when the theme changes, and is easier to use.If you were using gdk_draw_string (), gdk_draw_text (), gtk_paint_string (), or variants thereof, you'll want to rework your code to use PangoLayout. A PangoLayout represents a paragraph of text to be rendered to the screen. With English or European text, TO . Render a paragraph, you just break on spaces and draw each line one font height below the previous line When you start to handle languages ​​such as Arabic, Hindi, Japanese, Hebrew, Thai, or Korean, however, this is wrong; it simply will not work properly. Pango handles the details with PangoLayout. to render a paragraph of text, create a PangoLayout for that text, then call gdk_draw_layout or gtk_paint_layout (). to create your layout, call gtk_widget_create_pango_layout () on the widget you intend to Draw on.

While you'll usually just create a layout and draw it, for more complex cases PangoLayout has a lot of useful features; it has text attributes, and you can use the HTML-like Pango markup language mentioned earlier as a convenient way to set those attributes. (The less-convenient way uses PangoAttrList directly.) PangoLayout can also handle cursors, conversion from pixel locations to text positions, and that kind of thing.When asking for text metrics, such as the size of a PangoLayout, you'll notice that all Pango objects have a "logical size" and an "ink size." The logical size corresponds to the font metrics (ascent and descent), and should be used to position text. The logical size for a line of text will change if you add a new character that's larger than the existing characters. The ink size of some text describes the actual pixels covered by the font glyphs, so for example capital letters have a larger ink size than lowercase, and letters with "descenders" such as LowerCase "Y", "g", "p" have a larger ink size. USE. Logical Size Should Be Used in Widget Size Requests, and in Any Other Case Where You're Positioning Text.

Pango Sizes Are Specified in "Pango Units," Which Are Converted to and from Device Units (E.G. Pixels) USING The PANGO_SCALE CONSTANT. There Are Pango_scale Units Per Pixel.

IF Your Application Does Anything Complicated with Text, Such As Text Editing, You'll Want To Investigate Pango in More Depth. Some Considerations and Definitions To Keep In Mind:

. Logical and visual direction do not correspond Logical direction is the sequence of characters in a string; iterating over a string moves through it logically Visual direction is the sequence of font glyphs on the user's monitor Overall text direction can be right-to-.. left instead of left-to-right; also, individual characters can be "reordered" on a smaller scale, so that glyphs representing characters are not in the same sequence as the characters themselves Glyphs do not correspond to characters that is, multiple.. unicode characters can be displayed as a single font glyph on the screen. A cluster is a set of characters represented by a single glyph. (Glyphs are the graphical characters making up a font.) The process of converting a string of characters into a string Of glyphs is caled shaping. The low-level pango function pango_shape () Performs this Operation - Pangolayout Don't recomd for you, sopically you don't need to think about it. Word, Line, Sentence, and Paragr aph breaks do not necessarily follow English rules. Some languages ​​are written with no spaces, for example, so you can not use spaces to split up words. Line break rules depend on the language. Paragraphs can be broken by the newline / linefeed ( '/ n') character, by carriage return ( '/ r'), by a sequence of carriage return and line feed, or by a special Unicode paragraph separator character. You can obtain text boundary information for a paragraph of text with the functions PANGO_BREAK (), PANGO_GET_LOG_ATTRS (), and PANGO_FIND_PARAGRAPH_BOUNDARY (). It's not possible to put the cursor (Caret, Insertion Mark) at all character positions. Some Characters "Combine"

into a single logical unit, and the cursor can not go between the combined characters. A group of combined characters is called a grapheme, that is, a logical unit of writing. pango_break () and friends report valid cursor positions along with other text boundary information.The PangoLayout structure represents and entire paragraph of text. It is initialized with a PangoContext, UTF-8 string and set of attributes for that string. Once that is done, the set of formatted lines can be extracted from the object, the .

There are also a number of parameters to adjust the formatting of a PangoLayout, which are illustrated in Figure? 1. It is possible, as well, to ignore the 2-D setup, and simply treat the results of a PangoLayout as a list of Lines.

Figure? 1. Adjustable Parameters for a pangolayout

Basicly, in gtk program, we can call gtk_widget_get_pango_context () to get a PangoContext pointer associated with this GtkWidget. With this context, we could get / set the font description, get / set language, list font families, get font metrics, even break A string of unicode text. must call pango_layout_context_changed () after changing the context attributes.

For PangoLayout, we could create a pango layout from one pango context, or use gtk_widget_create_pango_layout, ro deep-copy from another layout. From gtklabel / entry ... widgets, they provide the gtk_xxx_get_layout method to retrive the layout assoicated with the widget. Must Call Pango_Layout_Context_changed () After changing the layout attributes.

With pangolayout, we can set the attributes, get / set text, get / set width, set (no get) font description, Get / set wrap, get pixel / pango_unit size. # Include

Gint Main (Gint Argc, Gchar * Argv [])

{

Gtkwidget * window;

Gtkwidget * label;

PangofonTDescription * font;

GTK_INIT (& Argc, & Argv);

WINDOW = gtk_window_new (gtk_window_toplevel);

g_signal_connect (window, "delete_event", g_callback (gtk_widget_destroy), null;

G_Signal_Connect (Window, "Destroy", G_Callback (gtk_main_quit), null;

Label = gtk_label_new ("Hello World!");

Font = PANGO_FONT_DESCRIPTION_FROM_STRING ("Monospace 30");

GTK_WIDGET_MODIFY_FONT (Label, Font);

GTK_CONTAINER_ADD (GTK_CONTAINER (Window), Label;

GTK_WIDGET_SHOW_ALL (Window);

gtk_main ();

Return 0;

}

转载请注明原文地址:https://www.9cbs.com/read-10493.html

New Post(0)