home *** CD-ROM | disk | FTP | other *** search
/ Mega Top 1 / os2_top1.zip / os2_top1 / APPS / TEKST / PSUTILS / PSRESIZE.C < prev    next >
C/C++ Source or Header  |  1993-11-29  |  5KB  |  184 lines

  1. /* psresize.c
  2.  * AJCD 29/11/93
  3.  * alter pagesize of document
  4.  *
  5.  * Usage:
  6.  *      psresize [-q] [-w<dim>] [-h<dim>] [-ppaper] [-W<dim>] [-H<dim>]
  7.  *            [-Ppaper] [in [out]]
  8.  *              -w<dim> sets the output paper width
  9.  *              -h<dim> sets the output paper height
  10.  *              -ppaper sets the output paper size (width and height) by name
  11.  *              -W<dim> sets the input paper width
  12.  *              -H<dim> sets the input paper height
  13.  *              -Ppaper sets the input paper size (width and height) by name
  14.  */
  15.  
  16. #include "psutil.h"
  17. #include "psspec.h"
  18. #include "patchlev.h"
  19.  
  20. void usage()
  21. {
  22.    fprintf(stderr, "%s release %d patchlevel %d\n", prog, RELEASE, PATCHLEVEL);
  23.    fprintf(stderr, "Usage: %s [-q] [-wwidth] [-hheight] [-ppaper] [-Wwidth] [-Hheight] [-Ppaper] [infile [outfile]]\n",
  24.        prog);
  25.    fflush(stderr);
  26.    exit(1);
  27. }
  28.  
  29. void argerror()
  30. {
  31.    fprintf(stderr, "%s: bad dimension\n", prog);
  32.    fflush(stderr);
  33.    exit(1);
  34. }
  35.  
  36. #define min(x,y) ((x) > (y) ? (y) : (x))
  37. #define max(x,y) ((x) > (y) ? (x) : (y))
  38.  
  39. main(argc, argv)
  40.      int argc;
  41.      char *argv[];
  42. {
  43.    double scale, rscale;            /* page scale */
  44.    double waste, rwaste;            /* amount wasted */
  45.    double vshift, hshift;            /* page centring shifts */
  46.    int rotate;
  47.    double inwidth = -1;
  48.    double inheight = -1;
  49.    struct papersize *paper;
  50.    struct pagespec *specs;
  51.  
  52. #ifdef PAPER
  53.    if (paper = findpaper(PAPER)) {
  54.       inwidth = width = (double)paper->width;
  55.       inheight = height = (double)paper->height;
  56.    }
  57. #endif
  58.  
  59.    vshift = hshift = 0;
  60.    rotate = 0;
  61.  
  62.    infile = stdin;
  63.    outfile = stdout;
  64.    verbose = 1;
  65.    for (prog = *argv++; --argc; argv++) {
  66.       if (argv[0][0] == '-') {
  67.      switch (argv[0][1]) {
  68.      case 'q':    /* quiet */
  69.         verbose = 0;
  70.         break;
  71.      case 'w':    /* page width */
  72.         width = singledimen(*argv+2);
  73.         break;
  74.      case 'h':    /* page height */
  75.         height = singledimen(*argv+2);
  76.         break;
  77.      case 'p':    /* paper type */
  78.         if (paper = findpaper(*argv+2)) {
  79.            width = (double)paper->width;
  80.            height = (double)paper->height;
  81.         } else {
  82.            fprintf(stderr, "%s: paper size '%s' not recognised\n",
  83.                prog, *argv+2);
  84.            fflush(stderr);
  85.            exit(1);
  86.         }
  87.         break;
  88.      case 'W':    /* input page width */
  89.         inwidth = singledimen(*argv+2);
  90.         break;
  91.      case 'H':    /* input page height */
  92.         inheight = singledimen(*argv+2);
  93.         break;
  94.      case 'P':    /* input paper type */
  95.         if (paper = findpaper(*argv+2)) {
  96.            inwidth = (double)paper->width;
  97.            inheight = (double)paper->height;
  98.         } else {
  99.            fprintf(stderr, "%s: paper size '%s' not recognised\n",
  100.                prog, *argv+2);
  101.            fflush(stderr);
  102.            exit(1);
  103.         }
  104.         break;
  105.      case 'v':    /* version */
  106.      default:
  107.         usage();
  108.      }
  109.       } else if (infile == stdin) {
  110.      if ((infile = fopen(*argv, "r")) == NULL) {
  111.         fprintf(stderr, "%s: can't open input file %s\n", prog, *argv);
  112.         fflush(stderr);
  113.         exit(1);
  114.      }
  115.       } else if (outfile == stdout) {
  116.      if ((outfile = fopen(*argv, "w")) == NULL) {
  117.         fprintf(stderr, "%s: can't open output file %s\n", prog, *argv);
  118.         fflush(stderr);
  119.         exit(1);
  120.      }
  121.       } else usage();
  122.    }
  123.    if ((infile=seekable(infile))==NULL) {
  124.       fprintf(stderr, "%s: can't seek input\n", prog);
  125.       fflush(stderr);
  126.       exit(1);
  127.    }
  128.  
  129.    if (width <= 0 || height <= 0) {
  130.       fprintf(stderr, "%s: output page width and height must be set\n", prog);
  131.       fflush(stderr);
  132.       exit(1);
  133.    }
  134.  
  135.    if (inwidth <= 0 || inheight <= 0) {
  136.       fprintf(stderr, "%s: input page width and height must be set\n", prog);
  137.       fflush(stderr);
  138.       exit(1);
  139.    }
  140.  
  141.    /* try normal orientation first */
  142.    scale = min(width/inwidth, height/inheight);
  143.    waste = (width-scale*inwidth)*(width-scale*inwidth) +
  144.       (height-scale*inheight)*(height-scale*inheight);
  145.    hshift = (width - inwidth*scale)/2;
  146.    vshift = (height - inheight*scale)/2;
  147.  
  148.    /* try rotated orientation */
  149.    rscale = min(height/inwidth, width/inheight);
  150.    rwaste = (height-scale*inwidth)*(height-scale*inwidth) +
  151.       (width-scale*inheight)*(width-scale*inheight);
  152.    if (rwaste < waste) {
  153.       double tmp = width;
  154.       scale = rscale;
  155.       hshift = (width + inheight*scale)/2;
  156.       vshift = (height - inwidth*scale)/2;
  157.       rotate = 1;
  158.       width = height;
  159.       height = tmp;
  160.    }
  161.  
  162.    width /= scale;
  163.    height /= scale;
  164.  
  165.    /* now construct specification list and run page rearrangement procedure */
  166.    specs = newspec();
  167.  
  168.    if (rotate) {
  169.       specs->rotate = 90;
  170.       specs->flags |= ROTATE;
  171.    }
  172.    specs->pageno = 0;
  173.    specs->scale = scale;
  174.    specs->flags |= SCALE;
  175.    specs->xoff = hshift;
  176.    specs->yoff = vshift;
  177.    specs->flags |= OFFSET;
  178.       
  179.    pstops(1, 1, 0, specs, 0.0);        /* do page rearrangement */
  180.  
  181.    exit(0);
  182. }
  183.  
  184.