home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21fs.zip / octave / kpathsea / magstep.c < prev    next >
C/C++ Source or Header  |  2000-01-15  |  3KB  |  101 lines

  1. /* magstep.c: fix up fixed-point vs. floating-point.
  2.  
  3. Copyright (C) 1994, 95 Karl Berry.
  4.  
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public
  7. License as published by the Free Software Foundation; either
  8. version 2 of the License, or (at your option) any later version.
  9.  
  10. This library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. Library General Public License for more details.
  14.  
  15. You should have received a copy of the GNU Library General Public
  16. License along with this library; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  18.  
  19. #include <kpathsea/config.h>
  20.  
  21. #include <kpathsea/magstep.h>
  22.  
  23.  
  24. /* Return true magstep N, where the lsb of N means ``half'' (see
  25.    magstep.h) for resolution BDPI.  From Tom Rokicki's dvips.  */
  26.  
  27. static int
  28. magstep P2C(int, n,  int, bdpi)
  29. {
  30.    double t;
  31.    int step;
  32.    int neg = 0;
  33.  
  34.    if (n < 0)
  35.      {
  36.        neg = 1;
  37.        n = -n;
  38.      }
  39.  
  40.    if (n & 1)
  41.      {
  42.        n &= ~1;
  43.        t = 1.095445115;
  44.      }
  45.     else
  46.       t = 1.0;
  47.  
  48.    while (n > 8)
  49.      {
  50.        n -= 8;
  51.        t = t * 2.0736;
  52.      }
  53.  
  54.    while (n > 0)
  55.      {
  56.        n -= 2;
  57.        t = t * 1.2;
  58.      }
  59.  
  60.    step = 0.5 + (neg ? bdpi / t : bdpi * t);
  61.    return step;
  62. }
  63.  
  64. /* This is adapted from code written by Tom Rokicki for dvips.  It's
  65.    part of Kpathsea now so all the drivers can use it.  The idea is to
  66.    return the true dpi corresponding to DPI with a base resolution of
  67.    BDPI.  If M_RET is non-null, we also set that to the mag value.  */
  68.    
  69. /* Don't bother trying to use fabs or some other ``standard'' routine
  70.    which can only cause trouble; just roll our own simple-minded
  71.    absolute-value function that is all we need.  */
  72. #undef ABS /* be safe */
  73. #define ABS(expr) ((expr) < 0 ? -(expr) : (expr))
  74.  
  75. #define MAGSTEP_MAX 40
  76.  
  77. unsigned
  78. kpse_magstep_fix P3C(unsigned, dpi,  unsigned, bdpi,  int *, m_ret)
  79. {
  80.   int m;
  81.   int mdpi = -1;
  82.   unsigned real_dpi = 0;
  83.   int sign = dpi < bdpi ? -1 : 1; /* negative or positive magsteps? */
  84.   
  85.   for (m = 0; !real_dpi && m < MAGSTEP_MAX; m++) /* don't go forever */
  86.     {
  87.       mdpi = magstep (m * sign, bdpi);
  88.       if (ABS (mdpi - (int) dpi) <= 1) /* if this magstep matches, quit */
  89.         real_dpi = mdpi;
  90.       else if ((mdpi - (int) dpi) * sign > 0) /* if gone too far, quit */
  91.         real_dpi = dpi;
  92.     }
  93.   
  94.   /* If requested, return the encoded magstep (the loop went one too far).  */
  95.   if (m_ret)
  96.     *m_ret = real_dpi == mdpi ? (m - 1) * sign : 0;
  97.  
  98.   /* Always return the true dpi found.  */
  99.   return real_dpi ? real_dpi : dpi;
  100. }
  101.