home *** CD-ROM | disk | FTP | other *** search
/ swCHIP 1991 January / swCHIP_95-1.bin / utility / gsview13 / src / gvcprn.c < prev    next >
C/C++ Source or Header  |  1995-12-09  |  11KB  |  424 lines

  1. /* Copyright (C) 1993, 1994, Russell Lang.  All rights reserved.
  2.   
  3.   This file is part of GSview.
  4.   
  5.   This program is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the GSview Free Public Licence 
  9.   (the "Licence") for full details.
  10.   
  11.   Every copy of GSview must include a copy of the Licence, normally in a 
  12.   plain ASCII text file named LICENCE.  The Licence grants you the right 
  13.   to copy, modify and redistribute GSview, but only under certain conditions 
  14.   described in the Licence.  Among other things, the Licence requires that 
  15.   the copyright notice and this notice be preserved on all copies.
  16. */
  17.  
  18. /* gvcprn.c */
  19. /* Printer module of PM and Windows GSview */
  20.  
  21. #ifdef _Windows
  22. #include "gvwin.h"
  23. #else
  24. #include "gvpm.h"
  25. #endif
  26.  
  27. struct prop_item_s *
  28. get_properties(char *device)
  29. {
  30. char *entries, *p, *q;
  31. int i, numentry;
  32. struct prop_item_s *proplist;
  33. PROFILE *prf;
  34.     entries = malloc(PROFILE_SIZE);
  35.     if (entries == (char *)NULL)
  36.        return NULL;
  37.     if ( (prf = profile_open(szIniFile)) == (PROFILE *)NULL) {
  38.         free(entries);
  39.         return NULL;
  40.     }
  41.     profile_read_string(prf, device, NULL, "", entries, PROFILE_SIZE);
  42.     if (strlen(entries) == 0) {
  43.         profile_close(prf);
  44.         free(entries);
  45.         return NULL;
  46.     }
  47.     p = entries;
  48.     for (numentry=0; p!=(char *)NULL && strlen(p)!=0; numentry++)
  49.         p += strlen(p) + 1;
  50.     proplist = (struct prop_item_s *)malloc((numentry+1) * sizeof(struct prop_item_s));
  51.     if (proplist == (struct prop_item_s *)NULL) {
  52.         profile_close(prf);
  53.         free(entries);
  54.         return NULL;
  55.     }
  56.     p = entries;
  57.     for (i=0; i<numentry; i++) {
  58.         strcpy(proplist[i].name, p);
  59.         profile_read_string(prf, device, p, "", proplist[i].value, sizeof(proplist->value));
  60.         q = proplist[i].value;
  61.         while ((*q) && (q[strlen(q)-1]==' '))
  62.         q[strlen(q)-1] = '\0';    /* remove trailing spaces */
  63.         p += strlen(p) + 1;
  64.     }
  65.     proplist[numentry].name[0] = '\0';
  66.     proplist[numentry].value[0] = '\0';
  67.     profile_close(prf);
  68.     free(entries);
  69.     return proplist;
  70. }
  71.  
  72. char *
  73. get_devices()
  74. {
  75. char *p;
  76. PROFILE *prf;
  77.     if ( (prf = profile_open(szIniFile)) == (PROFILE *)NULL)
  78.         return (char *)NULL;
  79.  
  80.     if ( (p = malloc(PROFILE_SIZE)) == (char *)NULL) {
  81.         profile_close(prf);
  82.         return (char *)NULL;
  83.     }
  84.  
  85.     profile_read_string(prf, DEVSECTION, NULL, "", p, PROFILE_SIZE);
  86.     if (strlen(p) == 0) {
  87.         /* [Devices] section doesn't exist.  Initialise from resources */
  88.         profile_create_section(prf, DEVSECTION, IDR_DEVICES);
  89.     }
  90.     profile_read_string(prf, DEVSECTION, NULL, "", p, PROFILE_SIZE);
  91.     profile_close(prf);
  92.     return p;
  93. }
  94.  
  95.  
  96. /* get a filename and spool it for printing */
  97. void
  98. gsview_spool(char *fname, char *port)
  99. {
  100.     static char filename[MAXSTR];
  101.  
  102.     if (fname == (char *)NULL) {
  103.         if (!get_filename(filename, FALSE, FILTER_ALL, IDS_PRINTFILE, IDS_TOPICPRINT))
  104.         return;
  105.     }
  106.     else {
  107.         while (*fname && *fname==' ')
  108.             fname++;
  109.         strncpy(filename, fname, MAXSTR);
  110.     }
  111.  
  112.     if (!gp_printfile(filename, port)) {
  113.         play_sound(SOUND_ERROR);
  114.         return;
  115.     }
  116. }
  117.  
  118.  
  119. /* save entire file */
  120. /* added to save files when GSview used as a WWW viewer */
  121. void
  122. gsview_saveas()
  123. {
  124. FILE *f;
  125. char output[MAXSTR];
  126. FILE *infile;
  127. UINT count;
  128. char *buffer;
  129.  
  130.     output[0] = '\0';
  131.     if (psfile.name[0] == '\0') {
  132.         gserror(IDS_NOTOPEN, NULL, MB_ICONEXCLAMATION, SOUND_NOTOPEN);
  133.         return;
  134.     }
  135.  
  136.     load_string(IDS_TOPICOPEN, szHelpTopic, sizeof(szHelpTopic));
  137.     if (!get_filename(output, TRUE, FILTER_PS, 0, IDS_TOPICOPEN))
  138.         return;
  139.  
  140.     if ((f = fopen(output, "wb")) == (FILE *)NULL) {
  141.         return;
  142.     }
  143.  
  144.     /* create buffer for PS file copy */
  145.     buffer = malloc(COPY_BUF_SIZE);
  146.     if (buffer == (char *)NULL) {
  147.         play_sound(SOUND_ERROR);
  148.         fclose(f);
  149.         unlink(output);
  150.         return;
  151.     }
  152.  
  153.     infile = fopen(psfile.name, "rb");
  154.     if (infile == (FILE *)NULL) {
  155.         play_sound(SOUND_ERROR);
  156.         free(buffer);
  157.         fclose(f);
  158.         unlink(output);
  159.         return;
  160.     }
  161.  
  162.     load_string(IDS_WAITWRITE, szWait, sizeof(szWait));
  163.     info_wait(TRUE);
  164.  
  165.         while ( (count = fread(buffer, 1, COPY_BUF_SIZE, infile)) != 0 ) {
  166.         fwrite(buffer, 1, count, f);
  167.     }
  168.     free(buffer);
  169.     fclose(infile);
  170.     fclose(f);
  171.  
  172.     info_wait(FALSE);
  173.     return;
  174. }
  175.  
  176. /* extract a range of pages for later printing */
  177. void
  178. gsview_extract()
  179. {
  180.     FILE *f;
  181.     static char output[MAXSTR];
  182.     int thispage = psfile.pagenum;
  183.  
  184.     if (psfile.name[0] == '\0') {
  185.         gserror(IDS_NOTOPEN, NULL, MB_ICONEXCLAMATION, SOUND_NOTOPEN);
  186.         return;
  187.     }
  188.  
  189.     if (doc == (PSDOC *)NULL) {
  190.         gserror(IDS_NOPAGE, NULL, MB_ICONEXCLAMATION, SOUND_NONUMBER);
  191.         return;
  192.     }
  193.     
  194.     load_string(IDS_TOPICOPEN, szHelpTopic, sizeof(szHelpTopic));
  195.     if (doc->numpages != 0)
  196.         if (!get_page(&thispage, TRUE))
  197.             return;
  198.  
  199.     if (!get_filename(output, TRUE, FILTER_PS, 0, IDS_TOPICOPEN))
  200.         return;
  201.  
  202.     if ((f = fopen(output, "wb")) == (FILE *)NULL) {
  203.         return;
  204.     }
  205.  
  206.     load_string(IDS_WAITWRITE, szWait, sizeof(szWait));
  207.     info_wait(TRUE);
  208.     if (doc->numpages != 0)
  209.         psfile_extract(f);
  210.     else {
  211.         dfreopen();
  212.         pscopyuntil(psfile.file, f, doc->beginheader, doc->endtrailer, NULL);
  213.         dfclose();
  214.     }
  215.  
  216.     fclose(f);
  217.  
  218.     info_wait(FALSE);
  219.     return;
  220. }
  221.  
  222.  
  223. /* Copy the headers, marked pages, and trailer to f */
  224. void
  225. psfile_extract(FILE *f)
  226. {
  227.     char text[PSLINELENGTH];
  228.     char *comment;
  229.     BOOL pages_written = FALSE;
  230.     BOOL pages_atend = FALSE;
  231.     int pages = 0;
  232.     int page = 1;
  233.     int i;
  234.     long position;
  235.  
  236.     for (i=0; i< doc->numpages; i++) {
  237.         if (page_list.select[i]) pages++;
  238.     }
  239.  
  240.     position = doc->beginheader;
  241.     while ( (comment = pscopyuntil(psfile.file, f, position,
  242.                doc->endheader, "%%Pages:")) != (char *)NULL ) {
  243.     position = ftell(psfile.file);
  244.     if (pages_written || pages_atend) {
  245.         free(comment);
  246.         continue;
  247.     }
  248.     sscanf(comment+8, "%s", text);
  249.     if (strcmp(text, "(atend)") == 0) {
  250.         fputs(comment, f);
  251.         pages_atend = TRUE;
  252.     } else {
  253.         switch (sscanf(comment+8, "%*d %d", &i)) {
  254.         case 1:
  255.             fprintf(f, "%%%%Pages: %d %d\r\n", pages, i);
  256.             break;
  257.         default:
  258.             fprintf(f, "%%%%Pages: %d\r\n", pages);
  259.             break;
  260.         }
  261.         pages_written = TRUE;
  262.     }
  263.     free(comment);
  264.     }
  265.     pscopyuntil(psfile.file, f, doc->beginpreview, doc->endpreview, NULL);
  266.     pscopyuntil(psfile.file, f, doc->begindefaults, doc->enddefaults, NULL);
  267.     pscopyuntil(psfile.file, f, doc->beginprolog, doc->endprolog, NULL);
  268.     pscopyuntil(psfile.file, f, doc->beginsetup, doc->endsetup, NULL);
  269.  
  270.     page = 1;
  271.     for (i = 0; i < doc->numpages; i++) {
  272.     if (page_list.select[map_page(i)])  {
  273.         comment = pscopyuntil(psfile.file, f, doc->pages[i].begin,
  274.                   doc->pages[i].end, "%%Page:");
  275.         fprintf(f, "%%%%Page: %s %d\r\n",
  276.             doc->pages[i].label, page++);
  277.         free(comment);
  278.         pscopyuntil(psfile.file, f, -1, doc->pages[i].end, NULL);
  279.     }
  280.     }
  281.  
  282.     position = doc->begintrailer;
  283.     while ( (comment = pscopyuntil(psfile.file, f, position,
  284.                doc->endtrailer, "%%Pages:")) != (char *)NULL ) {
  285.     position = ftell(psfile.file);
  286.     if (pages_written) {
  287.         free(comment);
  288.         continue;
  289.     }
  290.     switch (sscanf(comment+8, "%*d %d", &i)) {
  291.         case 1:
  292.         fprintf(f, "%%%%Pages: %d %d\r\n", pages, i);
  293.         break;
  294.         default:
  295.         fprintf(f, "%%%%Pages: %d\r\n", pages);
  296.         break;
  297.     }
  298.     pages_written = TRUE;
  299.     free(comment);
  300.     }
  301. }
  302.  
  303. /* common printer code */
  304. BOOL
  305. gsview_cprint(BOOL to_file, char *cfname, char *optfname)
  306. {
  307. char buf[MAXSTR];
  308. int i;
  309. float print_xdpi, print_ydpi;
  310. int width, height;
  311. struct prop_item_s *proplist;
  312. int pages;
  313. int thispage = psfile.pagenum;
  314. FILE *optfile;
  315. FILE *pcfile;
  316. char *fname;
  317. char *p;
  318. static char output[MAXSTR]; /* output filename for printing */
  319.  
  320.     fname = (char *)NULL;
  321.     if (doc == (PSDOC *)NULL) {
  322.         play_sound(SOUND_NONUMBER);
  323.             load_string(IDS_PRINTINGALL, buf, sizeof(buf));
  324.         if (message_box(buf, MB_ICONASTERISK) == IDCANCEL)
  325.             return FALSE;
  326.         fname = psfile.name;
  327.         pages = 1;
  328.     }
  329.     else {
  330.         pages = 1;
  331.         if (doc->numpages != 0) {
  332.         if (!get_page(&thispage, TRUE))
  333.             return FALSE;
  334.             pages = 0;
  335.             for (i=0; i< doc->numpages; i++) {
  336.                 if (page_list.select[i]) pages++;
  337.             }
  338.         }
  339.  
  340.         if ((cfname[0] != '\0') && !debug)
  341.         unlink(cfname);
  342.         cfname[0] = '\0';
  343.         if ( (pcfile = gp_open_scratch_file(szScratch, cfname, "wb")) == (FILE *)NULL) {
  344.         play_sound(SOUND_ERROR);
  345.         return FALSE;
  346.         }
  347.         if (doc->numpages != 0)
  348.             psfile_extract(pcfile);
  349.         else {
  350.             dfreopen();
  351.             pscopyuntil(psfile.file, pcfile, doc->beginheader, doc->endtrailer, NULL);
  352.             dfclose();
  353.         }
  354.         fclose(pcfile);
  355.         fname = cfname;
  356.     }
  357.     
  358.     if (to_file) {
  359.         if (!get_filename(output, TRUE, FILTER_ALL, IDS_OUTPUTFILE, IDS_TOPICPRINT))
  360.         return FALSE;
  361.     }
  362.  
  363.     /* calculate image size */
  364.     switch (sscanf(option.device_resolution,"%fx%f", &print_xdpi, &print_ydpi)) {
  365.         case EOF:
  366.         case 0:
  367.             print_xdpi = print_ydpi = DEFAULT_RESOLUTION;
  368.             break;
  369.         case 1:
  370.             print_ydpi = print_xdpi;
  371.     }
  372.     i = get_paper_size_index();
  373.     if (i < 0) {
  374.         width = option.user_width;
  375.         height = option.user_height;
  376.     }
  377.     else {
  378.         width = papersizes[i].width;
  379.         height = papersizes[i].height;
  380.     }
  381.     width  = (unsigned int)(width  / 72.0 * print_xdpi);
  382.     height = (unsigned int)(height / 72.0 * print_ydpi);
  383.  
  384.     if ((optfname[0] != '\0') && !debug)
  385.         unlink(optfname);
  386.     optfname[0] = '\0';
  387.     if ( (optfile = gp_open_scratch_file(szScratch, optfname, "w")) == (FILE *)NULL) {
  388.         play_sound(SOUND_ERROR);
  389.         return FALSE;
  390.     }
  391.     fprintf(optfile, "-dNOPAUSE\n");
  392.     if (option.safer)
  393.         fprintf(optfile, "-dSAFER\n");
  394.     fprintf(optfile, "-sDEVICE=%s\n",option.device_name);
  395.     fprintf(optfile, "-r%gx%g\n", (double)print_xdpi, (double)print_ydpi);
  396.     fprintf(optfile, "-g%ux%u\n",width,height);
  397.     if (to_file) {
  398.         fprintf(optfile, "-sOutputFile=");
  399.         for (p=output; *p != '\0'; p++)
  400.             if (*p == '\\')
  401.                 fputc('/',optfile);
  402.             else
  403.                 fputc(*p,optfile);
  404.         fputc('\n',optfile);
  405.     }
  406.     if ((proplist = get_properties(option.device_name)) != (struct prop_item_s *)NULL) {
  407.         /* output current property selections */
  408.         for (i=0; proplist[i].name[0]; i++) {
  409.         if (strcmp(proplist[i].value, not_defined) != 0)
  410.             fprintf(optfile,"-%s=%s\n", proplist[i].name, proplist[i].value);
  411.         }
  412.         free((char *)proplist);
  413.     }
  414.     for (p=fname; *p != '\0'; p++)
  415.         if (*p == '\\')
  416.             fputc('/',optfile);
  417.         else
  418.             fputc(*p,optfile);
  419.         fputs("\nquit.ps\n", optfile);
  420.     fclose(optfile);
  421.     return TRUE;
  422. }
  423.  
  424.