home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / x / xibm.zip / ibm8514 / brcFont.c < prev    next >
C/C++ Source or Header  |  1991-09-20  |  12KB  |  422 lines

  1. /*
  2.  * $Id: brcFont.c,v 1.1 1991/09/20 19:10:24 mtranle Exp $
  3.  *
  4.  * Copyright IBM Corporation 1987,1990
  5.  *
  6.  * All Rights Reserved
  7.  *
  8.  * Permission to use, copy, modify, and distribute this software and its
  9.  * documentation for any purpose and without fee is hereby granted,
  10.  * provided that the above copyright notice appear in all copies and that 
  11.  * both that copyright notice and this permission notice appear in
  12.  * supporting documentation, and that the name of IBM not be
  13.  * used in advertising or publicity pertaining to distribution of the
  14.  * software without specific, written prior permission.
  15.  *
  16.  * IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  17.  * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
  18.  * IBM BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  19.  * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20.  * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  21.  * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  22.  * SOFTWARE.
  23.  *
  24. */
  25. /*
  26.  * PRPQ 5799-PFF (C) COPYRIGHT IBM CORPORATION 1987,1990
  27.  * LICENSED MATERIALS - PROPERTY OF IBM
  28.  * REFER TO COPYRIGHT INSTRUCTIONS FORM NUMBER G120-2083
  29.  */
  30. /*
  31.  *  Hardware interface routines for IBM 8514/A adapter for
  32.  *  X.11 server(s) on IBM equipment.
  33.  *
  34.  */
  35.  
  36. /* Cached Font Manager
  37.  *
  38.  * Try to stuff a font off screen and remember where it is
  39.  * Always draw characters aligned to make life easy on the hardware
  40.  * If no room, leave for Image Glyph Blit
  41.  * When releasing, free up the space
  42.  *
  43.  */
  44. #include "X.h"
  45. #include "Xproto.h"
  46. #include "scrnintstr.h"
  47. #include "dixfontstr.h"
  48.  
  49. #include "mfb.h"
  50.  
  51. #include "OScompiler.h"
  52.  
  53. #include "x8514.h"
  54.  
  55. #include "brcFonts.h"
  56.  
  57. #include "ibmTrace.h"
  58.  
  59. ibm8514FontCacheRec ibm8514FontCacheEntry[NUMFONTPLANES] ;
  60.  
  61. void
  62. ibm8514InitFontCache()
  63. {
  64.     int i ;
  65.  
  66.     TRACE( ( "ibm8514InitFontCache()\n" ) ) ;
  67.  
  68.     ibm8514FontCacheEntry[0].RPlaneMask = RPLANE2 ;
  69.     ibm8514FontCacheEntry[0].WPlaneMask = WPLANE2 ;
  70.     ibm8514FontCacheEntry[1].RPlaneMask = RPLANE3 ;
  71.     ibm8514FontCacheEntry[1].WPlaneMask = WPLANE3 ;
  72.     ibm8514FontCacheEntry[2].RPlaneMask = RPLANE4 ;
  73.     ibm8514FontCacheEntry[2].WPlaneMask = WPLANE4 ;
  74.     ibm8514FontCacheEntry[3].RPlaneMask = RPLANE5 ;
  75.     ibm8514FontCacheEntry[3].WPlaneMask = WPLANE5 ;
  76.     ibm8514FontCacheEntry[4].RPlaneMask = RPLANE6 ;
  77.     ibm8514FontCacheEntry[4].WPlaneMask = WPLANE6 ;
  78.     ibm8514FontCacheEntry[5].RPlaneMask = RPLANE7 ;
  79.     ibm8514FontCacheEntry[5].WPlaneMask = WPLANE7 ;
  80.  
  81.     for ( i = 0 ; i < NUMFONTPLANES ; i++ )
  82.     {
  83.     ibm8514FontCacheEntry[i].numInstalled         = 0 ;
  84.     ibm8514FontCacheEntry[i].FirstAvailableLine    = FONTCACHESTARTLINE ;
  85.     ibm8514FontCacheEntry[i].NumLinesAvailable    = FONTCACHELENGTH ;
  86.     ibm8514FontCacheEntry[i].FontList        = NULL ;
  87.     }
  88.     return ;
  89. }
  90.  
  91. extern int ibm8514NumberOfPlanes ;
  92.  
  93. void
  94. ibm8514FreeFontChars( pChars, numChars )
  95.      InstalledCharPtr *pChars ;
  96.      int numChars ;
  97. {
  98.     InstalledCharPtr *pNextChar = pChars ;
  99.  
  100.     while ( numChars-- )
  101.     Xfree( *pNextChar++ ) ;
  102.     Xfree( pChars ) ;
  103.  
  104.     return ;
  105. }
  106.  
  107. Bool
  108. ibm8514RealizeFont( pscr, pFont )
  109.     ScreenPtr    pscr ;
  110.     FontPtr    pFont ;
  111. {
  112.     FontEncoding    encoding;
  113.     FontInfoPtr        pfi = &pFont->info ;
  114.     int            lines, linesneeded, width, height, i,
  115.             numchars, firstchar, lastchar, found,
  116.             CacheEntries, destX, destY, glw, glh,
  117.             relativeY, wplane, padwidth ;
  118.     InstalledFontPtr    ptr, pIFont ;
  119.     unsigned char    *glyphs ;
  120.     CharInfoPtr        pCinfo ;
  121.     unsigned char    ch[2];
  122.     unsigned long    n;
  123.     char         *pglyphBase;
  124.  
  125.  
  126.     TRACE( ( "ibm8514RealizeFont: realizing 0x%x\n", pFont ) ) ;
  127.  
  128.     firstchar = FONTFIRSTCOL(pFont);
  129.  
  130.     if ( FONTLASTROW(pFont) == 0 )
  131.     {
  132.     numchars = N1dChars(pFont);
  133.     lastchar = FONTLASTCOL(pFont);
  134.     encoding = Linear16Bit;
  135.     }
  136.     else  /* XXX  This code needs to be looked at more  XXX */
  137.     {
  138.     numchars = N2dChars(pFont);
  139.     if ( ( i + numchars ) > 0xFFFF )
  140.     {
  141.         ErrorF("ibm8514RealizeFont: bad 16bit realize") ;
  142.         return mfbRealizeFont( pscr, pFont ) ;
  143.     }
  144.     lastchar = firstchar + numchars - 1 ;
  145.     encoding = TwoD16Bit;
  146.     }
  147.  
  148.     lines = 1 ;
  149.     width = 0 ;
  150.     height = FONT_MAX_HEIGHT(pfi);
  151.     if ( FONT_MAX_WIDTH(pfi) > FONTCACHEWIDTH )
  152.     {
  153.     ErrorF( "Xibm: 8514: Not enough cache space (wide glyph)...") ;
  154.     ErrorF( "Font was 0x%x\n", pFont ) ;
  155.     return mfbRealizeFont( pscr, pFont ) ;
  156.     }
  157.  
  158.     for ( i = 0 ; i < numchars ; i++ )
  159.     {
  160.     ch[0] = (firstchar + i) >> 8;
  161.     ch[1] = (firstchar + i);
  162.  
  163.     GetGlyphs(pFont, 1, ch, encoding, &n, &pCinfo);
  164.  
  165.     width += GLYPHWIDTHBYTES( pCinfo ) ;
  166.     if ( width > FONTCACHEBYTEWIDTH )
  167.     {
  168.         width = GLYPHWIDTHBYTES( pCinfo ) ;
  169.         lines++ ;
  170.     }
  171.     }
  172.  
  173.     linesneeded = lines * height ;
  174.     CacheEntries = ibm8514NumberOfPlanes - 2 ; /* 1 for stage, 1 for fill,
  175.                         * rest are for fonts
  176.                         */
  177.  
  178.     for ( i = 0, found = -1 ; ( i < CacheEntries ) && ( found == -1 ) ; i++ )
  179.     if ( linesneeded <= ibm8514FontCacheEntry[i].NumLinesAvailable )
  180.         found = i ;
  181.     if ( found == -1 ) /* Can't find enough space !! */
  182.     return mfbRealizeFont( pscr, pFont ) ;
  183.  
  184. /* OK, we've space, we're going to go off-screen */
  185.     pIFont = (InstalledFontRec *) Xalloc( sizeof (InstalledFontRec) ) ;
  186.     if ( !pIFont )
  187.     {
  188.     ErrorF( "ibm8514RealizeFont: Cannot Xalloc for 0x%x\n", pFont ) ;
  189.     return mfbRealizeFont( pscr, pFont ) ;
  190.     }
  191.     pIFont->ppCharacter =
  192.     (InstalledCharPtr *) Xalloc( numchars * sizeof (InstalledCharPtr) ) ;
  193.     if ( !pIFont->ppCharacter )
  194.     {
  195.     ErrorF( "ibm8514RealizeFont: Cannot Xalloc for 0x%x\n", pFont ) ;
  196.     Xfree( pIFont ) ;
  197.     return mfbRealizeFont( pscr, pFont ) ;
  198.     }
  199.  
  200.     pIFont->firstchar = firstchar ;
  201.     pIFont->lastchar = lastchar ;
  202.     pIFont->numchars = numchars ;
  203.     pIFont->CacheIndex = found ;
  204.     pIFont->cacheheight = linesneeded ;
  205.  
  206.     destX = 0 ;
  207.     relativeY = 0 ;
  208.     destY = pIFont->startline =
  209.     ibm8514FontCacheEntry[found].FirstAvailableLine ;
  210.  
  211.     /* allocate, install, fill chars */
  212.     wplane = ibm8514FontCacheEntry[found].WPlaneMask ;
  213.  
  214.     pglyphBase = FONTGLYPHS(pFont);
  215.  
  216.     for ( i = 0 ; i < numchars ; i++ )
  217.     {
  218.     pIFont->ppCharacter[i] =
  219.         (InstalledCharPtr) Xalloc( sizeof (InstalledCharRec) ) ;
  220.         if ( !(pIFont->ppCharacter[i]) )
  221.     {
  222.         ErrorF("ibm8514RealizeFont: Cannot Xalloc for 0x%x\n", pFont ) ;
  223.         ibm8514FreeFontChars( pIFont->ppCharacter, i ) ;
  224.         Xfree( pIFont ) ;
  225.         return mfbRealizeFont( pscr, pFont ) ;
  226.     }
  227.  
  228.     ch[0] = (firstchar + i) >> 8;
  229.     ch[1] = (firstchar + i);
  230.  
  231.     GetGlyphs(pFont, 1, ch, encoding, &n, &pCinfo);
  232.     pIFont->ppCharacter[i]->pInfo = pCinfo;
  233.  
  234.     if ( n )
  235.     {
  236.         padwidth = 8 * GLYPHWIDTHBYTES( pCinfo ) ;
  237.         if ( destX + padwidth > FONTCACHEWIDTH )
  238.         {
  239.         destX = 0 ;
  240.         destY += height ; /* NEW LINE */
  241.         relativeY += height ;
  242.         }
  243.         pIFont->ppCharacter[i]->x = destX ;
  244.         pIFont->ppCharacter[i]->y = relativeY ;
  245.         pIFont->ppCharacter[i]->w = glw = GLYPHWIDTHPIXELS( pCinfo ) ;
  246.         pIFont->ppCharacter[i]->h = glh = GLYPHHEIGHTPIXELS( pCinfo ) ;
  247.         glyphs = FONTGLYPHBITS(pglyphBase, pCinfo);
  248.         TRACE( (
  249.             "Font Realize align:pl=%d,destX=%d,destY=%d,glw=%d,glh=%d,glyphs=x%x,char=%d\n",
  250.             wplane, destX, destY, glw, glh, glyphs, i ) ) ;
  251.  
  252.         /* dont do zero width or height glyphs */
  253.         if ( glw && glh )
  254.         ibm8514ByteAlignMonoImage( wplane, GXcopy, destX, destY,
  255.                       glw, glh, glyphs ) ;
  256.         destX += padwidth ;
  257.     }
  258.     }
  259.  
  260.     /* tack the Installed Font into the beginning of the fontlist */
  261.     ptr = ibm8514FontCacheEntry[found].FontList ;
  262.     ibm8514FontCacheEntry[found].FontList = pIFont ;
  263.     pIFont->NextFont = ptr ;
  264.  
  265.     ibm8514FontCacheEntry[found].numInstalled++ ;
  266.     ibm8514FontCacheEntry[found].FirstAvailableLine += linesneeded ;
  267.     ibm8514FontCacheEntry[found].NumLinesAvailable -= linesneeded ;
  268.  
  269.     /* ok, so tell the world that it's been off-screened */
  270.     FontSetPrivate(pFont, pscr->myNum, (pointer)pIFont);
  271.     pIFont->pFont = pFont ;
  272.  
  273.     return TRUE ;
  274. }
  275.  
  276. Bool
  277. ibm8514UnrealizeFont( pscr, pFont )
  278.     ScreenPtr    pscr ;
  279.     FontPtr    pFont ;
  280. {
  281.     int            freeheight, blitheight, blitstart, blitdest ;
  282.     InstalledFontPtr    currentfont, *lastfont, pIFont ;
  283.     ibm8514FontCachePtr    pFC ;
  284.  
  285.     pIFont = (InstalledFontPtr) FontGetPrivate(pFont, pscr->myNum) ;
  286.  
  287.     /* this code depends on knowing what is going on in the mfb.  I am assuming
  288.      * that the mfbrealizefont routine will only store a 0,1,2 in the devPriv
  289.      * field, whereas I store a pointer.  In case the mfb grows, I will check
  290.      * the field to see if it is a mfb number else assuming a pointer
  291.      */
  292.  
  293.     if ((long)pIFont < 20)
  294.     return mfbUnrealizeFont( pscr, pFont );
  295.  
  296.     TRACE( ( "ibm8514UnrealizeFont: unrealizing 0x%x\n", pFont ) ) ;
  297.  
  298.     /* Free the font's character's data in core */
  299.     ibm8514FreeFontChars( pIFont->ppCharacter, pIFont->numchars ) ;
  300.  
  301.     pFC = (ibm8514FontCachePtr) &(ibm8514FontCacheEntry[pIFont->CacheIndex]) ;
  302.  
  303.     freeheight = pIFont->cacheheight ;
  304.     blitstart = pIFont->startline+freeheight ;
  305.     blitheight = 0 ;
  306.     blitdest = pIFont->startline ;
  307.  
  308.     currentfont = pFC->FontList ;
  309.     lastfont = (InstalledFontPtr *) &(pFC->FontList) ;
  310.     /* notify all lower (earlier in list) fonts that they are moved up */
  311.     while ( currentfont != pIFont )
  312.     {
  313.     currentfont->startline -= freeheight ;
  314.     blitheight += currentfont->cacheheight ;
  315.     lastfont = (InstalledFontPtr *) &(currentfont->NextFont) ;
  316.     currentfont = currentfont->NextFont ;
  317.     }
  318.     /* move them up */
  319.     if ( blitheight )
  320.     ibm8514Bitblt( GXcopy, pFC->RPlaneMask, pFC->WPlaneMask,
  321.               0, blitstart, 0, blitdest,
  322.               FONTCACHEWIDTH, blitheight) ;
  323.  
  324.     pFC->FirstAvailableLine -= freeheight ;
  325.     pFC->NumLinesAvailable += freeheight ;
  326.     pFC->numInstalled-- ;
  327.     *lastfont = pIFont->NextFont ;
  328.     Xfree( pIFont ) ;
  329.  
  330.     return TRUE ;
  331. }
  332.  
  333. #ifdef FONTDUMP
  334. FontDump( pscr, pFont )
  335.     ScreenPtr   pscr ;
  336.     FontPtr    pFont ;
  337. {
  338. InstalledFontPtr     pIF ;
  339. ibm8514FontCachePtr    pFC ;
  340.  
  341. pIF = (InstalledFontPtr) pFont->devPriv[pscr->myNum] ;
  342. pFC = (ibm8514FontCachePtr) &(ibm8514FontCacheEntry[pIF->CacheIndex]) ;
  343.  
  344. ErrorF( "FontDump: 0x%x\n", pFont ) ;
  345. ErrorF( "InstalledFont:\n" ) ;
  346. ErrorF( "\tCacheIndex = %d\n", pIF->CacheIndex ) ;
  347. ErrorF( "\tstartline = %d\n", pIF->startline ) ;
  348. ErrorF( "\tcacheheight = %d\n", pIF->cacheheight ) ;
  349. ErrorF( "\tfirstchar = %d\n", pIF->firstchar ) ;
  350. ErrorF( "\tlastchar = %d\n", pIF->lastchar ) ;
  351. ErrorF( "\tnumchars = %d\n", pIF->numchars ) ;
  352. ErrorF( "\tNextFont = %x\n", pIF->NextFont ) ;
  353.  
  354. ErrorF( "CacheIndex:\n" ) ;
  355. ErrorF( "\tRplanemask, Wplanemask = %x, %x\n",
  356.     pFC->RPlaneMask,pFC->WPlaneMask ) ;
  357. ErrorF( "\tnumInstalled = %d\n", pFC->numInstalled ) ;
  358. ErrorF( "\tLinesAvail, First line = %d, %d\n",
  359.     pFC->FirstAvailableLine, pFC->NumLinesAvailable  ) ;
  360. ErrorF( "\n" ) ;
  361. }
  362. #endif
  363.  
  364. /* The X font from which it came */
  365. static FontPtr *ibm8514SavedFontList = (FontPtr *) 0 ;
  366. static int ibm8514SavedfontCount = 0 ;
  367.  
  368. void
  369. ibm8514CreateSavedFontList()
  370. {
  371. InstalledFontPtr brcFonts ;
  372. InstalledFontPtr nextInstalled ;
  373. int i ;
  374. int fontCount ;
  375. int tPcnt ;
  376.  
  377. fontCount = 0 ;
  378. for ( i = 0 ; i < NUMFONTPLANES ; i++ ) {
  379.     fontCount += ibm8514FontCacheEntry[i].numInstalled ;
  380.     ibm8514FontCacheEntry[i].FirstAvailableLine    = FONTCACHESTARTLINE ;
  381.     ibm8514FontCacheEntry[i].NumLinesAvailable    = FONTCACHELENGTH ;
  382. }
  383. if ( !( ibm8514SavedFontList =
  384.         (FontPtr *) Xalloc( fontCount * sizeof (FontPtr) ) ) )
  385.     return ; /* Blew It !! */
  386.  
  387. ibm8514SavedfontCount = 0 ;
  388. for ( i = 0 ; i < NUMFONTPLANES ; i++ ) {
  389.     brcFonts = ibm8514FontCacheEntry[i].FontList ;
  390.     for ( tPcnt = ibm8514FontCacheEntry[i].numInstalled ;
  391.           tPcnt-- ; ) {
  392.         ibm8514SavedFontList[ibm8514SavedfontCount++] =
  393.             brcFonts->pFont ;
  394.         ibm8514FreeFontChars( brcFonts->ppCharacter,
  395.                       brcFonts->numchars ) ;
  396.         nextInstalled = brcFonts->NextFont ;
  397.         Xfree( brcFonts ) ;
  398.         brcFonts = nextInstalled ;
  399.     }
  400.     ibm8514FontCacheEntry[i].numInstalled         = 0 ;
  401.     ibm8514FontCacheEntry[i].FontList        = NULL ;
  402. }
  403.  
  404. return ;
  405. }
  406.  
  407. void
  408. ibm8514RestoreOffScreenFonts( pScr )
  409. ScreenPtr pScr ;
  410. {
  411. FontPtr *fontList = ibm8514SavedFontList ;
  412.  
  413. ibm8514InitFontCache() ;
  414. while ( ibm8514SavedfontCount-- )
  415.     ibm8514RealizeFont( pScr, *fontList++ ) ;
  416.  
  417. Xfree( ibm8514SavedFontList ) ;
  418. ibm8514SavedFontList = (FontPtr *) 0 ;
  419.  
  420. return ;
  421. }
  422.