home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / modules / libfont / src / wfSzList.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  4.7 KB  |  241 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18. /* 
  19.  * wfSzList.cpp (FontObject.cpp) 
  20.  *
  21.  * Object local to libfont. This is used to track the association between
  22.  * rc, sizes[] and rf[] for a particular fh in a Font Object.
  23.  *
  24.  * dp Suresh <dp@netscape.com>
  25.  */
  26.  
  27.  
  28. #include "wfSzList.h"
  29.  
  30. wfSizesList::wfSizesList() : sizesLen(-1), sizes(NULL), rfs(NULL), rfcount(0),
  31.   maxrfcount(0), rfAllocStep(4)
  32. {
  33. }
  34.  
  35. wfSizesList::~wfSizesList()
  36. {
  37.     freeSizes();
  38.  
  39.     if (rfs)
  40.     {
  41.         WF_FREE(rfs);
  42.     }
  43. }
  44.  
  45. int
  46. wfSizesList::initialized()
  47. {
  48.     return(sizesLen >= 0 );
  49. }
  50.  
  51.  
  52. jdouble *
  53. wfSizesList::getSizes()
  54. {
  55.     jdouble *ret = NULL;
  56.     if (sizesLen)
  57.       {
  58.         ret = sizes;
  59.       }
  60.     return (ret);
  61. }
  62.  
  63. int
  64. wfSizesList::addSizes(jdouble *s)
  65. {
  66.     if (sizesLen)
  67.       {
  68.         // A list of sizes already exists for this. We will just free
  69.         // it and use the current sizes list.
  70.         freeSizes();
  71.       }
  72.     sizes = s;
  73.     for (sizesLen=0; sizes && sizes[sizesLen]>=0; sizesLen++);
  74.     return 0;
  75. }
  76.  
  77. int
  78. wfSizesList::removeSize(jdouble size)
  79. {
  80.     int ret = -1;
  81.  
  82.     for(int i=0; i<sizesLen; i++)
  83.       {
  84.         if (sizes[i] == size)
  85.           {
  86.             // Remove the size from the array
  87.             // by moving the last element to this spot
  88.             sizes[i] = sizes[sizesLen-1];
  89.             sizesLen--;
  90.             sizes[sizesLen] = -1;
  91.             ret = 0;
  92.           }
  93.       }
  94.     return (ret);
  95. }
  96.  
  97. int wfSizesList::supportsSize(jdouble size)
  98. {
  99. #if defined(XP_WIN) && !defined(WIN32)
  100.     /* for win16, don't use the size list, */
  101.     return(1);
  102. #else
  103.     int found = 0;
  104.  
  105.     if (sizes)
  106.       {
  107.         for (int i=0; i<sizesLen; i++)
  108.           {
  109.             if (sizes[i] == size || sizes[i] == 0)
  110.               {
  111.                 found = 1;
  112.                 break;
  113.               }
  114.           }
  115.       }
  116.     return (found);
  117. #endif
  118.     
  119. }
  120.  
  121. int
  122. /*ARGSUSED*/
  123. wfSizesList::addRf(struct nfrf *rf)
  124. {
  125.     // We are changing logic here. All we are going to do is to
  126.     // add the rf in the list of rf's that were created here.
  127.     // size is not used.
  128.     if (rfcount + 1 > maxrfcount)
  129.     {
  130.         // need more memory
  131.         if (maxrfcount == 0)
  132.         {
  133.             rfs = (struct nfrf **)WF_ALLOC(rfAllocStep * sizeof (*rfs));
  134.         }
  135.         else
  136.         {
  137.             rfs = (struct nfrf **) WF_REALLOC(rfs,
  138.                 (maxrfcount+rfAllocStep) * sizeof(*rfs));
  139.         }
  140.         if (!rfs)
  141.         {
  142.             // XXX no more memory. things are about to go horribly
  143.             // wrong.  The garbage collection code is banking on this
  144.             // to be able to keep track of the rfs that were created.
  145.             return -1;
  146.         }
  147.         maxrfcount += rfAllocStep;
  148.     }
  149.     rfs[rfcount] = rf;
  150.     // NOTE: we should not increment the reference count here
  151.     //            This was done so that the rfs will release normally.
  152.     //            If we did increment the refcount, then this rf will
  153.     //            never be released. That is why we have nffbp::RfDone()
  154.     //            to notify us that this rf is going away.
  155.     // nfrf_addRef((struct nfrf *)rf, NULL);
  156.     rfcount++;
  157.  
  158.     return (0);
  159. }
  160.  
  161.  
  162. int
  163. wfSizesList::removeRf(struct nfrf *rf)
  164. {
  165.     int ret = 0;
  166.     int i = 0;
  167.     while (i < rfcount)
  168.       {
  169.         if (rfs[i] == rf)
  170.           {
  171.             // NOTE: we dont have to decrement the reference count here
  172.             //            as we are not incrementing the reference count for
  173.             //            the rf's stored int he rfs[]. This was done so that
  174.             //            the rfs will release normally. If we did increment
  175.             //            the refcount, then this rf will never be released.
  176.             //            That is why we have nffbp::RfDone() to notify us
  177.             //            that this rf is going away.
  178.             rfs[i] = rfs[rfcount-1];
  179.             rfcount--;
  180.             ret ++;
  181.           }
  182.         else
  183.         {
  184.             i++;
  185.         }
  186.       }
  187.     return (ret);
  188. }
  189.  
  190.  
  191. int
  192. wfSizesList::isRfExist(struct nfrf *rf)
  193. {
  194.     int ret = 0;
  195.     for (int i = 0; i < rfcount; i++)
  196.     {
  197.         if (rfs[i] == rf)
  198.         {
  199.             ret++;
  200.         }
  201.       }
  202.     return (ret);
  203. }
  204.  
  205. int
  206. wfSizesList::getRfCount()
  207. {
  208.     return (rfcount);
  209. }
  210.  
  211.  
  212. struct nfrf *
  213. wfSizesList::getRf(jdouble pointsize)
  214. {
  215.     struct nfrf *rf = NULL;
  216.     for (int i = 0; i < rfcount; i++)
  217.       {
  218.         if (nfrf_GetPointSize(rfs[i], NULL) == pointsize)
  219.           {
  220.             rf = rfs[i];
  221.             break;
  222.           }
  223.       }
  224.     return (rf);
  225. }
  226.  
  227. //
  228. // Private method implementations
  229. //
  230.  
  231. void
  232. wfSizesList::freeSizes()
  233. {
  234.     if (sizes)
  235.       {
  236.         // XXX Possible alloc/free mismatch
  237.         WF_FREE(sizes);
  238.         sizes = NULL;
  239.       }
  240. }
  241.