home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / s / stex2-18.zip / SeeTeX / Xtex / X11-Font-infor < prev    next >
Internet Message Format  |  1991-02-19  |  9KB

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