home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / doc / tutorials / fmetrics.txt < prev    next >
Encoding:
Text File  |  1989-06-13  |  8.5 KB  |  230 lines

  1.                  Font Metrics
  2.  
  3.  
  4.                   Jim Fulton
  5.                    MIT X Consortium
  6.  
  7.  
  8.  
  9. In X11, text is printed by specifying an "origin" in (x,y) relative to which
  10. characters should be displayed.  The "baseline" of the font (the top-most row
  11. of pixels in any descenders) is laid on top of the y-coordinate and characters
  12. are drawn relative to this line.  Thus, characters that do not have descenders
  13. but which touch the baseline will illuminate pixels in row (y-1), but not in
  14. row y.
  15.  
  16. For each character in the font, the following information is provided:
  17.  
  18.     logical width        This is the horizontal distance from the
  19.                 origin of this character to next character.
  20.                 If the string "ab" where printed with its
  21.                 origin at (x,y) and "a" had a logical width of
  22.                 10 pixels, "b" would be printed at (x+10,y).
  23.                 Well-designed fonts include a certain amount
  24.                 of spacing in the logical width so that 
  25.                 characters don't bunch up.  This value will
  26.                 usually be positive for characters that are
  27.                 read from left-to-right, and negative for
  28.                 characters that are read right-to-left.
  29.  
  30.     left bearing        This is the directed, horizontal distance from
  31.                 the origin (x,y) at which a character is
  32.                 printed to the left edge of the glyph raster
  33.                 (which is the where the left-most pixel is
  34.                 drawn).  Counting from zero at the origin, this
  35.                 is the number of pixels to the left-most pixel.
  36.                 Depending on whether this pixel appears to the
  37.                 left, on, or to the right of the origin, this
  38.                 value will be negative, zero, or positive,
  39.                 respectively.
  40.  
  41.     right bearing        This is the directed, horizontal distance from
  42.                 the origin (x,y) at which a character is
  43.                 printed to the right edge of the glyph raster
  44.                 (which is one to the right of where the right-
  45.                 most pixel is drawn).  Counting from zero at
  46.                 the origin, this is the number of pixels to
  47.                 just beyond the right-most illuminated pixel.
  48.                 Depending on whether this pixel appears to the
  49.                 left, on, or to the right of the origin, this
  50.                 value will be negative, zero, or positive,
  51.                 respectively.
  52.  
  53.     ascent            This is the directed vertical distance from the
  54.                 origin (x,y) at which a character is printed to
  55.                 the top edge of the top-most pixel that is
  56.                 illuminated when this character is drawn.
  57.                 Counting from zero at the origin, this is the 
  58.                 number of pixels to the top-most pixel.
  59.                 Depending on whether the top-most pixel appears
  60.                 above, on, or below the origin, this value will
  61.                 be positive, zero, or negative, respectively
  62.                 (note the assymmetry between this and the other
  63.                 metrics).
  64.  
  65.     descent            This is the directed, vertical distance from
  66.                 the origin (x,y) at which a character is
  67.                 printed to the bottom edge of the glyph raster
  68.                 (which is one below where the bottom-most pixel
  69.                 is drawn).  Counting from zero at the origin,
  70.                 this is the number of pixels to just below the
  71.                 bottom-most pixel.  Depending on whether this
  72.                 pixel appears above, on, or below the baseline,
  73.                 this value will be negative, zero, or positive.
  74.  
  75.  
  76. Fonts also have a font-ascent and a font-descent, defined the same as the
  77. character ascents and descents, that is used for determining inter-line
  78. spacing.  These two values are choosen by the font designer so that lines of
  79. text that are vertically spaced by (font-ascent + font-descent) look
  80. appropriate for the font.  
  81.  
  82. When text is drawn at a point (x,y), the server paints the first glyph (call
  83. its metrics g, for short) such that:
  84.  
  85.     left-most pixel        is in column (x + g.lbearing)
  86.  
  87.     right-most pixel        is in column (x + g.rbearing - 1)
  88.  
  89.     top-most pixel        is in row (y - g.ascent)
  90.  
  91.     bottom-most pixel        is in row (y + g.descent - 1)
  92.  
  93.  
  94. The origin is then advanced to by the logical width of the first character
  95. (i.e. (x,y) becomes (x + g.width, y)), and the process is repeated for the
  96. next character in the string being printed.
  97.  
  98. Normally, the only pixels which are touched are those that are set in the
  99. character glyph.  However, applications (such as terminal emulators) that would
  100. like a rectangle of "background color" painted behind each character can use a
  101. special type of text printing, called ImageText.  In this case, the server
  102. fills a rectangle before printing the character such that:
  103.  
  104.     left side            is in column x
  105.  
  106.     right side            is in column (x + logical_width - 1)
  107.  
  108.     top side            is in row (y - font_ascent)
  109.  
  110.     bottom side            is in row (y + font_descent - 1)
  111.  
  112.  
  113. Since the size of the rectangle (logical_width by font_ascent+font_descent)
  114. is determined by the font ascent and descent and not by the character font and
  115. descent, a character printed with ImageText may "stick out" beyond the
  116. rectangle as follows:
  117.  
  118.     lbearing < 0        some pixels on left will have no background
  119.  
  120.     rbearing >= width        some pixels on right will have no background
  121.  
  122.     ascent > font_ascent    some pixels on top will have no background
  123.  
  124.     descent >= font_descent    some pixels on bottom will have no background
  125.  
  126. This is particularly common for characters with accents.  Fonts that have 
  127. been specially designed so that all of the characters have the same logical
  128. width and illuminate only those pixels that would appear within the ImageText
  129. background rectangle described above are called Character Cell fonts.  These
  130. are the only fonts that are truly suitable for use with terminal emulators
  131. and other programs that use ImageText.  Fonts in which all of the characters
  132. are of the same width, but which stick out beyond the background rectangle are
  133. called Monospaced fonts.  All other fonts are called Proportional fonts.  For
  134. more information, see the X Logical Font Description Conventions (XLFD) 
  135. document.
  136.  
  137. The following example will show how the string "T-p" would be printed in the
  138. -adobe-helvetica-bold-r-normal--12-120-75-75-p-70-iso8859-1.  The glyphs
  139. "T", "-", and "p" have the images and character metrics shown below
  140.  
  141.     ########
  142.     ...##...    width 8
  143.     ...##...    lbearing 0, rbearing 8
  144.     ...##...    ascent 9, descent 0
  145.     ...##...
  146.     ...##...
  147.     ...##...
  148.     ...##...
  149.     ...##...
  150.  
  151.  
  152.     #####        width 8
  153.             lbearing 1, rbearing 6
  154.             ascent 4, descent -3
  155.  
  156.  
  157.     ##.##.
  158.     ###.##        width 7
  159.     ##..##        lbearing 0, rbearing 6
  160.     ##..##        ascent 7, descent 3
  161.     ##..##
  162.     ###.##
  163.     ##.##.
  164.     ##....
  165.     ##....
  166.     ##....
  167.  
  168.  
  169. Note that the width of "-" is 8 even though it only has 5 illuminated pixels.
  170. In this case, one blank pixel to the left and two pixels to the right are 
  171. considered to be part of the glyph.  This can seen clearly by padding the
  172. images out to their logical widths and their proper heights (the font-ascent
  173. and font-descent for this font are 11 and 3, respectively):
  174.  
  175.  
  176.                +---     ........    ........    .......
  177.                |        ........    ........    .......
  178.                |        ########    ........    .......
  179.                |        ...##...    ........    .......
  180.                |        ...##...    ........    ##.##..
  181.   font ascent  |        ...##...    ........    ###.##.
  182.       (11)     |        ...##...    ........    ##..##.
  183.                |        ...##...    .#####..    ##..##.
  184.                |        ...##...    ........    ##..##.
  185.                |        ...##...    ........    ###.##.
  186.  baseline ____ |        ...##...    ........    ##.##..
  187.                +===     ........    ........    ##.....
  188.  font descent  |        ........    ........    ##.....
  189.      (3)       |        ........    ........    ##.....
  190.                +---
  191.                         |           |           |
  192.                         origin      origin      origin
  193.  
  194.  
  195. Now, removing the illustration spaces between the images, we get:
  196.  
  197.  
  198.                +---     .......................
  199.                |        .......................
  200.                |        ########...............
  201.                |        ...##..................
  202.                |        ...##...........##.##..
  203.   font ascent  |        ...##...........###.##.
  204.       (11)     |        ...##...........##..##.
  205.                |        ...##....#####..##..##.
  206.                |        ...##...........##..##.
  207.                |        ...##...........###.##.
  208.  baseline ____ |        ...##...........##.##..
  209.                +===     ................##.....
  210.  font descent  |        ................##.....
  211.      (3)       |        ................##.....
  212.                +---
  213.                         |       |       |
  214.                         origin  origin  origin
  215.  
  216.  
  217. From this picture, we can compute the extents of the string as would be
  218. returned by XTextExtents:
  219.  
  220.     width (total lit and unlit across)                23
  221.  
  222.     lbearing (number crossed to left lit pixel)            0
  223.  
  224.     rbearing (number crossed to beyond right lit pixel)        22
  225.  
  226.     ascent (number crossed to top lit pixel)            9
  227.  
  228.     descent (number crossed to below bottom lit pixel)        3
  229.  
  230.