home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / s / stex2-18.zip / SeeTeX / libtex / conv.h < prev    next >
C/C++ Source or Header  |  1990-07-10  |  3KB  |  90 lines

  1. /*
  2.  * Copyright (c) 1987, 1989 University of Maryland
  3.  * Department of Computer Science.  All rights reserved.
  4.  * Permission to copy for any purpose is hereby granted
  5.  * so long as this copyright notice remains intact.
  6.  */
  7.  
  8. /*
  9.  * Conversions.  Conversion factors convert between values in scaled
  10.  * points and values in device-dependent units.  The results of all
  11.  * conversions are rounded to the nearest integral value, of type (i32).
  12.  */
  13.  
  14. /*
  15.  * This is now done using `double' values, but may be changed to
  16.  * fixed-point or some other `fast' method, as long as the results
  17.  * are consistent and reasonably accurate.  The structure `conversion'
  18.  * holds the conversion-method-dependent quantities; the macros
  19.  * fromSP and toSP apply the conversion to a value.  (Note that
  20.  * fromSP and toSP need not be macros, but should be fast.)
  21.  *
  22.  * SetConversion sets the (single, global) conversion factor.
  23.  * If a driver needs special conversions, there is another routine,
  24.  * CSetConversion that sets a specific conversion, and cfromSP and
  25.  * ctoSP to apply these.
  26.  *
  27.  * IS USING DOTS PER INCH SUFFICIENT?  (Pixels per point might be better.)
  28.  *
  29.  * Note that it is necessary to set the global conversion factor before
  30.  * using any fonts.
  31.  */
  32. typedef struct conversion {
  33.     double    c_fromsp;    /* multiplier to convert from scaled points */
  34.     double    c_tosp;        /* multiplier to convert to scaled points:
  35.                    could divide by c_fromsp, but this should
  36.                    be faster and more accurate */
  37.     double    c_mag;        /* the magnification this conversion
  38.                    represents; mainly for GetFont() */
  39.     double    c_dpi;        /* dpi (should be pixels per point?) */
  40. } Conv;
  41.  
  42. extern Conv Conversion;        /* the global conversion factor */
  43.  
  44. /*
  45.  * In order to do this, we need to round properly.  The compilers I
  46.  * have tend to generate very poor code for this.  The following is
  47.  * intended to help them out.  Smarter compilers can do better, but
  48.  * if they are smart enough, they will realise that the variables
  49.  * here are not used anywhere else, and discard them.  (For a compiler
  50.  * to do this given separate compliation, `static' is a must.)
  51.  */
  52. #ifdef lint            /* or a smart compiler */
  53.  
  54. #define    ROUND(f) ((i32) ((f) < 0.0 ? (f) - 0.5 : (f) + 0.5))
  55. #define    CEIL(f)    ((double) (i32) (f) < (f) ? (i32) (f) + 1 : (i32) (f))
  56.  
  57. #else /* lint */
  58.  
  59. static double _half = 0.5;
  60. static double _d;
  61. #define    ROUND(f) ((i32) (_d = (f), _d < 0.0 ? _d - _half : _d + _half))
  62.  
  63. #ifdef NEGATIVE_FLOAT_ROUNDS_TO_NEGATIVE_INFINITY
  64. #define    CEIL(f)  (-(i32) -(f))
  65. #else /* we will assume that floating to integer truncates */
  66. static i32 _i;
  67. #define    CEIL(f)     (_i = _d = (f), _i < _d ? _i + 1 : _i)
  68. #endif /* round towards negative infinity */
  69.  
  70. #endif /* lint */
  71.  
  72. #define    SetConversion(dpi, usermag, num, denom, dvimag)    \
  73.     CSetConversion(&Conversion, dpi, usermag, num, denom, dvimag)
  74.  
  75. #define    cfromSP(c, v)    ROUND((c)->c_fromsp * (v))
  76. #define    ctoSP(c, v)    ROUND((c)->c_tosp * (v))
  77.  
  78. #define    fromSP(v)    cfromSP(&Conversion, v)
  79. #define    toSP(v)        ctoSP(&Conversion, v)
  80.  
  81. /*
  82.  * Conversions for rules are a bit different: we must round up, rather
  83.  * than off.  ConvRule applies the global conversion value for a rule
  84.  * value (height or width); CConvRule applies a specific conversion.
  85.  */
  86. #define    CConvRule(c, v)    CEIL((c)->c_fromsp * (v))
  87. #define    ConvRule(v)    CConvRule(&Conversion, v)
  88.  
  89. void    CSetConversion();
  90.