home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / fonts / lib / font / bitmap / bitmaputils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-31  |  6.9 KB  |  226 lines

  1. /*
  2.  * $XConsortium: bitmaputils.c,v 1.3 91/05/31 14:23:06 rws Exp $
  3.  *
  4.  * Copyright 1990 Massachusetts Institute of Technology
  5.  *
  6.  * Permission to use, copy, modify, distribute, and sell this software and its
  7.  * documentation for any purpose is hereby granted without fee, provided that
  8.  * the above copyright notice appear in all copies and that both that
  9.  * copyright notice and this permission notice appear in supporting
  10.  * documentation, and that the name of M.I.T. not be used in advertising or
  11.  * publicity pertaining to distribution of the software without specific,
  12.  * written prior permission.  M.I.T. makes no representations about the
  13.  * suitability of this software for any purpose.  It is provided "as is"
  14.  * without express or implied warranty.
  15.  *
  16.  * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  17.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
  18.  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  20.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  21.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  *
  23.  */
  24.  
  25. #include    "fontfilest.h"
  26. #include    "bitmap.h"
  27.  
  28. #ifndef MAXSHORT
  29. #define MAXSHORT    32767
  30. #endif
  31.  
  32. #ifndef MINSHORT
  33. #define MINSHORT    -32768
  34. #endif
  35.  
  36. static xCharInfo initMinMetrics = {
  37. MAXSHORT, MAXSHORT, MAXSHORT, MAXSHORT, MAXSHORT, 0xFFFF};
  38. static xCharInfo initMaxMetrics = {
  39. MINSHORT, MINSHORT, MINSHORT, MINSHORT, MINSHORT, 0x0000};
  40.  
  41. #define MINMAX(field,ci) \
  42.     if (minbounds->field > (ci)->field) \
  43.          minbounds->field = (ci)->field; \
  44.     if (maxbounds->field < (ci)->field) \
  45.          maxbounds->field = (ci)->field;
  46.  
  47. void
  48. bitmapComputeFontBounds(pFont)
  49.     FontPtr     pFont;
  50. {
  51.     BitmapFontPtr  bitmapFont = (BitmapFontPtr) pFont->fontPrivate;
  52.     int         nchars;
  53.     int         r,
  54.                 c;
  55.     CharInfoPtr ci,
  56.                *pci;
  57.     int         maxOverlap;
  58.     int         overlap;
  59.     xCharInfo  *minbounds,
  60.                *maxbounds;
  61.     int         i;
  62.     int        numneg = 0, numpos = 0;
  63.  
  64.     if (bitmapFont->bitmapExtra) {
  65.     minbounds = &bitmapFont->bitmapExtra->info.minbounds;
  66.     maxbounds = &bitmapFont->bitmapExtra->info.maxbounds;
  67.     } else {
  68.     minbounds = &pFont->info.minbounds;
  69.     maxbounds = &pFont->info.maxbounds;
  70.     }
  71.     *minbounds = initMinMetrics;
  72.     *maxbounds = initMaxMetrics;
  73.     maxOverlap = MINSHORT;
  74.     nchars = bitmapFont->num_chars;
  75.     for (i = 0, ci = bitmapFont->metrics; i < nchars; i++, ci++) {
  76.     MINMAX(ascent, &ci->metrics);
  77.     MINMAX(descent, &ci->metrics);
  78.     MINMAX(leftSideBearing, &ci->metrics);
  79.     MINMAX(rightSideBearing, &ci->metrics);
  80.     MINMAX(characterWidth, &ci->metrics);
  81.     if (ci->metrics.characterWidth < 0)
  82.         numneg++;
  83.     else
  84.         numpos++;
  85.     minbounds->attributes &= ci->metrics.attributes;
  86.     maxbounds->attributes |= ci->metrics.attributes;
  87.     overlap = ci->metrics.rightSideBearing - ci->metrics.characterWidth;
  88.     if (maxOverlap < overlap)
  89.         maxOverlap = overlap;
  90.     }
  91.     if (bitmapFont->bitmapExtra) {
  92.     if (numneg > numpos)
  93.         bitmapFont->bitmapExtra->info.drawDirection = RightToLeft;
  94.     else
  95.         bitmapFont->bitmapExtra->info.drawDirection = LeftToRight;
  96.     bitmapFont->bitmapExtra->info.maxOverlap = maxOverlap;
  97.     minbounds = &pFont->info.minbounds;
  98.     maxbounds = &pFont->info.maxbounds;
  99.     *minbounds = initMinMetrics;
  100.     *maxbounds = initMaxMetrics;
  101.     pci = bitmapFont->encoding;
  102.     maxOverlap = MINSHORT;
  103.     for (r = pFont->info.firstRow; r <= pFont->info.lastRow; r++) {
  104.         for (c = pFont->info.firstCol; c <= pFont->info.lastCol; c++) {
  105.         ci = *pci++;
  106.         if (ci) {
  107.             MINMAX(ascent, &ci->metrics);
  108.             MINMAX(descent, &ci->metrics);
  109.             MINMAX(leftSideBearing, &ci->metrics);
  110.             MINMAX(rightSideBearing, &ci->metrics);
  111.             MINMAX(characterWidth, &ci->metrics);
  112.             if (ci->metrics.characterWidth < 0)
  113.             numneg++;
  114.             else
  115.             numpos++;
  116.             minbounds->attributes &= ci->metrics.attributes;
  117.             maxbounds->attributes |= ci->metrics.attributes;
  118.             overlap = ci->metrics.rightSideBearing -
  119.             ci->metrics.characterWidth;
  120.             if (maxOverlap < overlap)
  121.             maxOverlap = overlap;
  122.         }
  123.         }
  124.     }
  125.     }
  126.     if (numneg > numpos)
  127.     pFont->info.drawDirection = RightToLeft;
  128.     else
  129.     pFont->info.drawDirection = LeftToRight;
  130.     pFont->info.maxOverlap = maxOverlap;
  131. }
  132.  
  133. void
  134. bitmapComputeFontInkBounds(pFont)
  135.     FontPtr     pFont;
  136. {
  137.     BitmapFontPtr  bitmapFont = (BitmapFontPtr) pFont->fontPrivate;
  138.     int         nchars;
  139.     int         r,
  140.                 c;
  141.     CharInfoPtr *pci,
  142.                 cit;
  143.     xCharInfo  *ci;
  144.     int         offset;
  145.     xCharInfo  *minbounds,
  146.                *maxbounds;
  147.     int         i;
  148.  
  149.     if (!bitmapFont->ink_metrics) {
  150.     if (bitmapFont->bitmapExtra) {
  151.         bitmapFont->bitmapExtra->info.ink_minbounds = bitmapFont->bitmapExtra->info.minbounds;
  152.         bitmapFont->bitmapExtra->info.ink_maxbounds = bitmapFont->bitmapExtra->info.maxbounds;
  153.     }
  154.     pFont->info.ink_minbounds = pFont->info.minbounds;
  155.     pFont->info.ink_maxbounds = pFont->info.maxbounds;
  156.     } else {
  157.     if (bitmapFont->bitmapExtra) {
  158.         minbounds = &bitmapFont->bitmapExtra->info.ink_minbounds;
  159.         maxbounds = &bitmapFont->bitmapExtra->info.ink_maxbounds;
  160.     } else {
  161.         minbounds = &pFont->info.ink_minbounds;
  162.         maxbounds = &pFont->info.ink_maxbounds;
  163.     }
  164.     *minbounds = initMinMetrics;
  165.     *maxbounds = initMaxMetrics;
  166.     nchars = bitmapFont->num_chars;
  167.     for (i = 0, ci = bitmapFont->ink_metrics; i < nchars; i++, ci++) {
  168.         MINMAX(ascent, ci);
  169.         MINMAX(descent, ci);
  170.         MINMAX(leftSideBearing, ci);
  171.         MINMAX(rightSideBearing, ci);
  172.         MINMAX(characterWidth, ci);
  173.         minbounds->attributes &= ci->attributes;
  174.         maxbounds->attributes |= ci->attributes;
  175.     }
  176.     if (bitmapFont->bitmapExtra) {
  177.         minbounds = &pFont->info.ink_minbounds;
  178.         maxbounds = &pFont->info.ink_maxbounds;
  179.         *minbounds = initMinMetrics;
  180.         *maxbounds = initMaxMetrics;
  181.         pci = bitmapFont->encoding;
  182.         for (r = pFont->info.firstRow; r <= pFont->info.lastRow; r++) {
  183.         for (c = pFont->info.firstCol; c <= pFont->info.lastCol; c++) {
  184.             cit = *pci++;
  185.             if (cit) {
  186.             offset = cit - bitmapFont->metrics;
  187.             ci = &bitmapFont->ink_metrics[offset];
  188.             MINMAX(ascent, ci);
  189.             MINMAX(descent, ci);
  190.             MINMAX(leftSideBearing, ci);
  191.             MINMAX(rightSideBearing, ci);
  192.             MINMAX(characterWidth, ci);
  193.             minbounds->attributes &= ci->attributes;
  194.             maxbounds->attributes |= ci->attributes;
  195.             }
  196.         }
  197.         }
  198.     }
  199.     }
  200. }
  201.  
  202. Bool
  203. bitmapAddInkMetrics(pFont)
  204.     FontPtr     pFont;
  205. {
  206.     BitmapFontPtr  bitmapFont;
  207.     int         i;
  208.  
  209.     bitmapFont = (BitmapFontPtr) pFont->fontPrivate;
  210.     bitmapFont->ink_metrics = (xCharInfo *) xalloc(bitmapFont->num_chars * sizeof(xCharInfo));
  211.     if (!bitmapFont->ink_metrics)
  212.     return FALSE;
  213.     for (i = 0; i < bitmapFont->num_chars; i++)
  214.     FontCharInkMetrics(pFont, &bitmapFont->metrics[i], &bitmapFont->ink_metrics[i]);
  215.     pFont->info.inkMetrics = TRUE;
  216.     return TRUE;
  217. }
  218.  
  219. /* ARGSUSED */
  220. int
  221. bitmapComputeWeight(pFont)
  222.     FontPtr     pFont;
  223. {
  224.     return 10;
  225. }
  226.