📄 sec-gdkfont.html
字号:
</h2> <p> To use a font, you typically need detailed information about its <i class="FIRSTTERM">metrics</i>. Font metrics are used to position characters with respect to one another, and to determine the size of strings rendered in the font. The most fundamental metrics are the <i class= "FIRSTTERM">ascent</i> and <i class="FIRSTTERM"> descent</i> of the font. Text is placed on a <i class= "FIRSTTERM">baseline</i>; the baseline is like a rule on a sheet of notebook paper. The bottom of each character touches the baseline. Some characters (such as lowercase "p" and "y") extend below the baseline. A font's <i class="FIRSTTERM">descent</i> is the maximum distance below the baseline reached by its characters. Its <i class="FIRSTTERM">ascent</i> is the maximum distance above the baseline. The <i class="FIRSTTERM">height</i> of a font is the sum of the ascent and the descent. When rendering multiple lines of text, you should leave at least the font's height between each baseline. </p> <p> Ascent and descent are available as members of the <span class="STRUCTNAME">GdkFont</span> struct: </p> <table border="0" bgcolor="#E0E0E0" width="100%"> <tr> <td><pre class="PROGRAMLISTING"> typedef struct _GdkFont GdkFont;struct _GdkFont{ GdkFontType type; gint ascent; gint descent;}; </pre> </td> </tr> </table> <p> The <span class="STRUCTNAME">type</span> field distinguishes fonts from fontsets; fontsets are used to display non-European languages. </p> <p> Individual characters in a font have their own ascent and descent; a character's ascent and descent will always be less than or equal to the font's ascent and descent. GDK can calculate the sum of the maximum ascent and descent for a particular string, rather than an entire font; this height will be less than or equal to the font's height. The relevant functions are <tt class="FUNCTION"> gdk_string_height()</tt>, <tt class="FUNCTION"> gdk_text_height()</tt>, and <tt class="FUNCTION"> gdk_char_height()</tt>. <tt class="FUNCTION"> gdk_text_height()</tt> is different from <tt class= "FUNCTION">gdk_string_height()</tt> because it accepts the length of the string as an argument; <tt class= "FUNCTION">gdk_string_height()</tt> calls <tt class= "FUNCTION">strlen()</tt> for you. Thus <tt class= "FUNCTION">gdk_text_height()</tt> is preferred if you already know a string's length. </p> <p> In addition to its vertical metrics, each character in a font has three metrics describing its horizontal dimensions. The <i class="FIRSTTERM">width</i> of a character is the distance from the left origin of the character to the start of the next character. Note: the width is <i class="EMPHASIS">not</i> the distance to the furthest-right pixel in a character; in some fonts, especially italic fonts, characters may lean over past the start of the next character. The <i class= "FIRSTTERM">left-side-bearing</i> or <i class= "FIRSTTERM">lbearing</i> is the distance from the left origin to the leftmost pixel in the character; the <i class="FIRSTTERM">right-side-bearing</i> or <i class= "FIRSTTERM">rbearing</i> is the distance from the origin to the rightmost pixel in the character. Thus the <i class="FIRSTTERM">rbearing</i> can be larger than the width, in the case of italic fonts that lean over past the start of the next character. </p> <p> GDK functions that return a character or string width return the width of the character, or the sum of the character widths in the string. If the rbearing of the rightmost character is greater than its width, a string may require more space than <tt class="FUNCTION"> gdk_string_width()</tt>, <tt class="FUNCTION"> gdk_text_width()</tt>, or <tt class="FUNCTION"> gdk_char_width()</tt> will return. Like the height-measurement functions, the <span class= "STRUCTNAME">_string_</span> variant computes the length of the string for you, and the <span class="STRUCTNAME"> _text_</span> variant accepts a precomputed length as an argument. </p> <p> The functions ending in <span class="STRUCTNAME"> _measure</span> are more often what you want. For a string with <i class="EMPHASIS">N</i> characters, these return the sum of the widths of the first <i class= "EMPHASIS">N-1</i> characters, plus the rbearing of the final character. That is, they take into account the fact that rbearing may be greater than width. If you're trying to decide how much space to leave for rendering a string, you probably want the <tt class="FUNCTION"> gdk_string_measure()</tt>, <tt class="FUNCTION"> gdk_text_measure()</tt>, or <tt class="FUNCTION"> gdk_char_measure()</tt> functions. Sometimes you don't want to consider the rbearing, however; for example, if you're centering a string, it may look nicer to use the width (because a small italic flourish extending past the width won't "fill" the space and the string will look slightly left of center). </p> <p> <tt class="FUNCTION">gdk_text_extents()</tt> and <tt class="FUNCTION">gdk_string_extents()</tt> return <i class="EMPHASIS">all</i> the metrics for a string, including both bearings, the width, ascent, and descent. The returned left-side-bearing is the leftmost pixel in the string; the right-side-bearing is the rightmost pixel as returned by <tt class="FUNCTION"> gdk_text_measure()</tt>. The width is the sum of character widths, as returned by <tt class="FUNCTION"> gdk_text_width()</tt>. </p> <p> <a href="sec-gdkfont.html#FIG-FONTMETRICS">Figure 11</a> depicts the various font metrics. <a href= "sec-gdkfont.html#FL-FONTMETRICS">Figure 12</a> summarizes the API for querying font metrics. All font metrics are computed on the client side, so these functions are not expensive compared to most of the drawing API. </p> <div class="FIGURE"> <a name="FIG-FONTMETRICS"></a> <p> <img src="TO_BE_D.png"> </p> <p> <b>Figure 11. Font Metrics</b> </p> </div> <div class="FIGURE"> <a name="FL-FONTMETRICS"></a> <div class="FUNCSYNOPSIS"> <a name="FL-FONTMETRICS.SYNOPSIS"></a> <table border="0" bgcolor="#E0E0E0" width="100%"> <tr> <td><pre class="FUNCSYNOPSISINFO">#include <gdk/gdk.h></pre> </td> </tr> </table> <p> <code><code class="FUNCDEF">gint <tt class= "FUNCTION">gdk_string_width</tt></code>(GdkFont* <tt class="PARAMETER"><i>font</i></tt>, const gchar* <tt class="PARAMETER"><i>string</i></tt>);</code> </p> <p> <code><code class="FUNCDEF">gint <tt class= "FUNCTION">gdk_text_width</tt></code>(GdkFont* <tt class="PARAMETER"><i>font</i></tt>, const gchar* <tt class="PARAMETER"><i>string</i></tt>, gint <tt class= "PARAMETER"><i>string_length</i></tt>);</code> </p> <p> <code><code class="FUNCDEF">gint <tt class= "FUNCTION">gdk_char_width</tt></code>(GdkFont* <tt class="PARAMETER"><i>font</i></tt>, gchar <tt class= "PARAMETER"><i>character</i></tt>);</code> </p> <p> <code><code class="FUNCDEF">gint <tt class= "FUNCTION">gdk_string_measure</tt></code>(GdkFont* <tt class="PARAMETER"><i>font</i></tt>, const gchar* <tt class="PARAMETER"><i>string</i></tt>);</code> </p> <p> <code><code class="FUNCDEF">gint <tt class= "FUNCTION">gdk_text_measure</tt></code>(GdkFont* <tt class="PARAMETER"><i>font</i></tt>, const gchar* <tt class="PARAMETER"><i>string</i></tt>, gint <tt class= "PARAMETER"><i>string_length</i></tt>);</code> </p> <p> <code><code class="FUNCDEF">gint <tt class= "FUNCTION">gdk_char_measure</tt></code>(GdkFont* <tt class="PARAMETER"><i>font</i></tt>, gchar <tt class= "PARAMETER"><i>character</i></tt>);</code> </p> <p> <code><code class="FUNCDEF">gint <tt class= "FUNCTION">gdk_string_height</tt></code>(GdkFont* <tt class="PARAMETER"><i>font</i></tt>, const gchar* <tt class="PARAMETER"><i>string</i></tt>);</code> </p> <p> <code><code class="FUNCDEF">gint <tt class= "FUNCTION">gdk_text_height</tt></code>(GdkFont* <tt class="PARAMETER"><i>font</i></tt>, const gchar* <tt class="PARAMETER"><i>string</i></tt>, gint <tt class= "PARAMETER"><i>string_length</i></tt>);</code> </p> <p> <code><code class="FUNCDEF">gint <tt class= "FUNCTION">gdk_char_height</tt></code>(GdkFont* <tt class="PARAMETER"><i>font</i></tt>, gchar <tt class= "PARAMETER"><i>character</i></tt>);</code> </p> <p> <code><code class="FUNCDEF">void <tt class= "FUNCTION">gdk_string_extents</tt></code>(GdkFont* <tt class="PARAMETER"><i>font</i></tt>, const gchar* <tt class="PARAMETER"><i>string</i></tt>, gint* <tt class="PARAMETER"><i>lbearing</i></tt>, gint* <tt class="PARAMETER"><i>rbearing</i></tt>, gint* <tt class="PARAMETER"><i>width</i></tt>, gint* <tt class= "PARAMETER"><i>ascent</i></tt>, gint* <tt class= "PARAMETER"><i>descent</i></tt>);</code> </p> <p> <code><code class="FUNCDEF">void <tt class= "FUNCTION">gdk_text_extents</tt></code>(GdkFont* <tt class="PARAMETER"><i>font</i></tt>, const gchar* <tt class="PARAMETER"><i>string</i></tt>, gint <tt class= "PARAMETER"><i>string_length</i></tt>, gint* <tt class="PARAMETER"><i>lbearing</i></tt>, gint* <tt class="PARAMETER"><i>rbearing</i></tt>, gint* <tt class="PARAMETER"><i>width</i></tt>, gint* <tt class= "PARAMETER"><i>ascent</i></tt>, gint* <tt class= "PARAMETER"><i>descent</i></tt>);</code> </p> </div> <p> <b>Figure 12. Font Metrics</b> </p> </div> </div> </div> <div class="NAVFOOTER"> <br> <br> <table width="100%" border="0" bgcolor="#ffffff" cellpadding= "1" cellspacing="0"> <tr> <td width="25%" bgcolor="#ffffff" align="left"> <a href="sec-gdkcursor.html"><font color="#0000ff" size="2"><b><<< Previous</b></font></a> </td> <td width="25%" colspan="2" bgcolor="#ffffff" align= "center"> <font color="#0000ff" size="2"><b><a href="ggad.html"> <font color="#0000ff" size="2"><b> Home</b></font></a></b></font> </td> <td width="25%" bgcolor="#ffffff" align="right"> <a href="sec-gc.html"><font color="#0000ff" size="2"> <b>Next >>></b></font></a> </td> </tr> <tr> <td colspan="2" align="left"> <font color="#000000" size="2"><b>The Mouse Pointer</b></font> </td> <td colspan="2" align="right"> <font color="#000000" size="2"><b>Graphics Contexts</b></font> </td> </tr> </table> </div> </body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -