home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / fonts / server / MacFS / MacFontRender.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-29  |  7.6 KB  |  238 lines

  1. /***********************************************************************
  2. Copyright 1991 by Apple Computer, Inc, Cupertino, California
  3.             All Rights Reserved
  4.  
  5. Permission to use, copy, modify, and distribute this software
  6. for any purpose and without fee is hereby granted, provided
  7. that the above copyright notice appear in all copies.
  8.  
  9. APPLE MAKES NO WARRANTY OR REPRESENTATION, EITHER EXPRESS,
  10. OR IMPLIED, WITH RESPECT TO THIS SOFTWARE, ITS QUALITY,
  11. PERFORMANCE, MERCHANABILITY, OR FITNESS FOR A PARTICULAR
  12. PURPOSE. AS A RESULT, THIS SOFTWARE IS PROVIDED "AS IS,"
  13. AND YOU THE USER ARE ASSUMING THE ENTIRE RISK AS TO ITS
  14. QUALITY AND PERFORMANCE. IN NO EVENT WILL APPLE BE LIABLE 
  15. FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
  16. DAMAGES RESULTING FROM ANY DEFECT IN THE SOFTWARE.
  17.  
  18. THE WARRANTY AND REMEDIES SET FORTH ABOVE ARE EXCLUSIVE
  19. AND IN LIEU OF ALL OTHERS, ORAL OR WRITTEN, EXPRESS OR
  20. IMPLIED.
  21.  
  22. ***********************************************************************/
  23. /************************************************************************
  24. Copyright 1989 by Digital Equipment Corporation, Maynard, Massachusetts,
  25. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  26.  
  27.                         All Rights Reserved
  28.  
  29. Permission to use, copy, modify, and distribute this software and its
  30. documentation for any purpose and without fee is hereby granted,
  31. provided that the above copyright notice appear in all copies and that
  32. both that copyright notice and this permission notice appear in
  33. supporting documentation, and that the names of Digital or MIT not be
  34. used in advertising or publicity pertaining to distribution of the
  35. software without specific, written prior permission.
  36.  
  37. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  38. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  39. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  40. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  41. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  42. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  43. SOFTWARE.
  44.  
  45. ************************************************************************/
  46.  
  47. #include "MacFont.h"
  48. #include <ctype.h>
  49. #include "fontfilest.h"
  50. /* use bitmap structure */
  51. #include "bitmap.h"
  52.  
  53. extern int  bitmapGetGlyphs(), bitmapGetMetrics();
  54. extern int  bitmapGetBitmaps(), bitmapGetExtents();
  55. void        MacFontUnloadFont();
  56.  
  57. int
  58. MacFontRenderFont(pFont, entry, vals, bit, byte, glyph, scan)
  59.     FontPtr     pFont;
  60.     FontEntryPtr entry;
  61.     FontScalablePtr vals;
  62.     int         bit, byte, glyph, scan;
  63. {
  64.     xCharInfo  *min,
  65.                *max;
  66.     BitmapFontPtr  bitmapFont;
  67.     MacBitmapFontRecPtr pMacPriv;
  68.     char        fullName[MAXFONTNAMELEN];
  69.     int            ret;
  70.     int            starttime = GetTimeInMillis();
  71.  
  72.     strcpy(fullName, entry->name.name);
  73.     FontParseXLFDName(fullName, vals, FONT_XLFD_REPLACE_VALUE);
  74.  
  75.     pFont->fontPrivate = 0;
  76.     pFont->info.defaultCh = 0;
  77.     pFont->bit = bit;
  78.     pFont->byte = byte;
  79.     pFont->glyph = glyph = ((glyph == 1) ? 2 : glyph); /* Must be even for QD */
  80.     pFont->scan = scan;
  81.     pFont->info.anamorphic = FALSE;
  82.  
  83.     bitmapFont = (BitmapFontPtr) 
  84.                  xalloc(sizeof(BitmapFontRec) + sizeof(MacBitmapFontRec));
  85.     if (!bitmapFont)
  86.         return AllocError;
  87.  
  88.     pMacPriv = (MacBitmapFontRecPtr)(bitmapFont + 1);
  89.  
  90.     if ((ret = OpenMacFont(pFont, pMacPriv, fullName, vals->pixel, vals->x, vals->y))
  91.                      != Successful) {
  92.         xfree(bitmapFont); /* which also frees the tag along pMacPriv */
  93.         return ret;
  94.     }
  95.  
  96.     pFont->fontPrivate = (pointer) bitmapFont;
  97.     bitmapFont->metrics = 0;
  98.     bitmapFont->ink_metrics = 0;
  99.     bitmapFont->bitmaps = 0;
  100.     bitmapFont->encoding = 0;
  101.     bitmapFont->pDefault = NULL;
  102.     bitmapFont->bitmapExtra = 0;
  103.  
  104.     
  105.     if (ConvertMacFont(pFont, pMacPriv, bit, byte, glyph, scan) != Successful)
  106.         goto BAILOUT; /* Only error return from ConvertMacFont is AllocError */
  107.     
  108.     if (pFont -> info.defaultCh) {
  109.     int         r,
  110.                 c,
  111.                 cols;
  112.  
  113.     r = pFont->info.defaultCh >> 8;
  114.     c = pFont->info.defaultCh & 0xFF;
  115.     if (pFont->info.firstRow <= r && r <= pFont->info.lastRow &&
  116.         pFont->info.firstCol <= c && c <= pFont->info.lastCol) {
  117.         cols = pFont->info.lastCol - pFont->info.firstCol + 1;
  118.             r = r - pFont->info.firstRow;
  119.             c = c - pFont->info.firstCol;
  120.         bitmapFont->pDefault = bitmapFont->encoding[r * cols + c];
  121.     }
  122.     }
  123.     bitmapComputeFontBounds(pFont);
  124.     if (FontCouldBeTerminal(&pFont->info)) {
  125.     MacFontPadToTerminal(pFont);
  126.     bitmapComputeFontBounds(pFont);
  127.     }
  128.     FontComputeInfoAccelerators(&pFont->info);
  129.     if (bitmapFont->bitmapExtra)
  130.     FontComputeInfoAccelerators(&bitmapFont->bitmapExtra->info);
  131.     if (pFont->info.constantMetrics)
  132.     bitmapAddInkMetrics(pFont);
  133.     if (bitmapFont->bitmapExtra)
  134.     bitmapFont->bitmapExtra->info.inkMetrics = pFont->info.inkMetrics;
  135.  
  136.     bitmapComputeFontInkBounds(pFont);
  137.  
  138.     /* generate properties */
  139.     min = &pFont->info.ink_minbounds;
  140.     max = &pFont->info.ink_maxbounds;
  141.  
  142.     if (pMacPriv->xHeight == 0)
  143.         pMacPriv->xHeight = min->ascent;
  144.  
  145.     if (pMacPriv->quadWidth == 0)
  146.         pMacPriv->quadWidth = (min->characterWidth + max->characterWidth) / 2;
  147.  
  148.     pMacPriv->weight = bitmapComputeWeight(pFont);
  149.  
  150.     MacFontComputedProps(fullName, vals, &(pFont->info), pMacPriv);
  151.  
  152.     pFont->get_bitmaps = bitmapGetBitmaps;
  153.     pFont->get_extents = bitmapGetExtents;
  154.     pFont->get_glyphs = bitmapGetGlyphs;
  155.     pFont->get_metrics = bitmapGetMetrics;
  156.     pFont->unload_font = MacFontUnloadFont;
  157.     (void) NoticeF("%s in %d ms.", fullName, GetTimeInMillis() - starttime);
  158.     return Successful;
  159.  
  160. BAILOUT:
  161.     if (pFont->fontPrivate)
  162.         MacFontFreeFontBits(pFont); /* *pFont itself freed by caller. */
  163.     return AllocError;
  164. }
  165.  
  166. MacFontFreeFontBits(pFont)
  167.     FontPtr     pFont;
  168. {
  169.     BitmapFontPtr  bitmapFont;
  170.     MacBitmapFontRecPtr pMacPriv;
  171.     int         i;
  172.  
  173.     bitmapFont = (BitmapFontPtr) pFont->fontPrivate;
  174.     xfree(bitmapFont->ink_metrics);
  175.     xfree(bitmapFont->encoding);
  176.     for (i = 0; i < bitmapFont->num_chars; i++)
  177.     xfree(bitmapFont->metrics[i].bits);
  178.     xfree(bitmapFont->metrics);
  179.     xfree(pFont->info.props);
  180.     xfree(pFont->info.isStringProp); /* XXX missing in bdf version ?! */
  181.  
  182.     pMacPriv = (MacBitmapFontRecPtr)(bitmapFont + 1);
  183.     if (pMacPriv->pgp) {
  184.         ClosePort(pMacPriv->pgp);
  185.         DisposPtr(pMacPriv->pgp);
  186.     }
  187.     if (pMacPriv->pfm)
  188.         DisposPtr(pMacPriv->pfm);
  189.  
  190.     xfree(bitmapFont); /* which also frees the MacBitmapFontRec tag-along */
  191. }
  192.  
  193. void
  194. MacFontUnloadFont(pFont)
  195.     FontPtr     pFont;
  196. {
  197.     MacFontFreeFontBits (pFont);
  198.     xfree(pFont);
  199. }
  200.  
  201. static Bool
  202. MacFontPadToTerminal(pFont)
  203.     FontPtr     pFont;
  204. {
  205.     BitmapFontPtr  bitmapFont;
  206.     BitmapExtraPtr bitmapExtra;
  207.     int         i;
  208.     int         new_size;
  209.     CharInfoRec new;
  210.     int         w,
  211.                 h;
  212.  
  213.     bitmapFont = (BitmapFontPtr) pFont->fontPrivate;
  214.     new.metrics.ascent = pFont->info.fontAscent;
  215.     new.metrics.descent = pFont->info.fontDescent;
  216.     new.metrics.leftSideBearing = 0;
  217.     new.metrics.rightSideBearing = pFont->info.minbounds.characterWidth;
  218.     new.metrics.characterWidth = new.metrics.rightSideBearing;
  219.     new_size = BYTES_FOR_GLYPH(&new, pFont->glyph);
  220.     for (i = 0; i < bitmapFont->num_chars; i++) {
  221.     new.bits = (char *) xalloc(new_size);
  222.     if (!new.bits)
  223.         return FALSE;
  224.     FontCharReshape(pFont, &bitmapFont->metrics[i], &new);
  225.     xfree(bitmapFont->metrics[i].bits);
  226.     bitmapFont->metrics[i] = new;
  227.     }
  228.     bitmapExtra = bitmapFont->bitmapExtra;
  229.     if (bitmapExtra) {
  230.     w = GLYPHWIDTHPIXELS(&new);
  231.     h = GLYPHHEIGHTPIXELS(&new);
  232.     for (i = 0; i < GLYPHPADOPTIONS; i++)
  233.         bitmapExtra->bitmapsSizes[i] = bitmapFont->num_chars *
  234.         (BYTES_PER_ROW(w, 1 << i) * h);
  235.     }
  236.     return TRUE;
  237. }
  238.