home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pp / pp-6.0 / Format / ascii2fax / ia52fax.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-18  |  4.1 KB  |  205 lines

  1. /* ia52fax: convert ia5 to fax */
  2.  
  3. # ifndef lint
  4. static char Rcsid[] = "@(#)$Header: /xtel/pp/pp-beta/Format/ascii2fax/RCS/ia52fax.c,v 6.0 1991/12/18 20:15:19 jpo Rel $";
  5. # endif
  6.  
  7. /*
  8.  * $Header: /xtel/pp/pp-beta/Format/ascii2fax/RCS/ia52fax.c,v 6.0 1991/12/18 20:15:19 jpo Rel $
  9.  *
  10.  * $Log: ia52fax.c,v $
  11.  * Revision 6.0  1991/12/18  20:15:19  jpo
  12.  * Release 6.0
  13.  *
  14.  */
  15.  
  16.  
  17. #include <stdio.h>
  18. #include "util.h"
  19. #include "IOB-types.h"
  20. #include <isode/cmd_srch.h>
  21. #include "table.h"
  22. #include "fonts.h"
  23. #include "pg_sizes.h"
  24.  
  25. #define OPT_TABLE    1
  26. #define OPT_FONT    2
  27.  
  28. CMD_TABLE    tbl_options [] = {
  29.     "-table",    OPT_TABLE,
  30.     "-font",    OPT_FONT,
  31.     0,        -1
  32.     };
  33.  
  34. char    *display_str;
  35. char    *myname;
  36. Table    *table = NULLTBL;
  37. char    *font = NULLCP;
  38.  
  39. extern BitMap    new_bitmap();
  40. extern PPFontPtr    file2font();
  41.  
  42. main(argc, argv)
  43. int    argc;
  44. char    **argv;
  45. {
  46.     PPFontPtr    ppfont;
  47.     int        cont = TRUE;
  48.     struct type_IOB_G3FacsimileBodyPart    *p2;
  49.     BitMap        page;
  50.     int        y;
  51.     FILE        *fp;
  52.     char    buffer[BUFSIZ], *start = NULLCP, *postCtrlL = NULLCP;
  53.  
  54.     myname = *argv++;
  55.  
  56.     sys_init(myname);
  57.  
  58.     p2 = (struct type_IOB_G3FacsimileBodyPart *) 
  59.         calloc (1, sizeof(struct type_IOB_G3FacsimileBodyPart));
  60.  
  61.     p2 -> parameters = (struct type_IOB_G3FacsimileParameters *)
  62.         calloc (1, sizeof(struct type_IOB_G3FacsimileParameters));
  63.     
  64.     p2 -> parameters -> optionals = 
  65.         opt_IOB_G3FacsimileParameters_number__of__pages;
  66.  
  67.  
  68.     while (*argv != NULL) {
  69.         switch (cmd_srch(*argv,tbl_options)) {
  70.             case OPT_TABLE:
  71.             if (*(argv+1) == NULLCP)
  72.                 fprintf(stderr,
  73.                     "no table given with flag %s", *argv);
  74.             else {
  75.                 argv++;
  76.                 if (lexequ(*argv, "none") != 0
  77.                     && ((table = tb_nm2struct (*argv)) == NULLTBL)) {
  78.                     fprintf(stderr,
  79.                         "Cannot initialise table '%s'\n", *argv);
  80.                     exit (1);
  81.                 }
  82.             }
  83.             break;
  84.  
  85.             case OPT_FONT:
  86.             if (*(argv+1) == NULLCP)
  87.                 fprintf(stderr,
  88.                     "no argument given with flag %s", *argv);
  89.             else {
  90.                 argv++;
  91.                 font = *argv;
  92.             }
  93.             break;
  94.  
  95.             default:
  96.             fprintf(stderr,
  97.                 "unknown option '%s'\n", *argv);
  98.             exit(1);
  99.         }
  100.         argv++;
  101.     }
  102.  
  103.     /* Load the font to use */
  104.     
  105.     initialise_globals();
  106.  
  107.     if (font == NULLCP) {
  108.         fprintf(stderr,
  109.             "No font specified");
  110.         exit(1);
  111.     }
  112.     if ((fp = fopen(font, "r")) == NULL
  113.         || (ppfont = file2font(fp)) == (PPFontPtr) NOTOK) {
  114.         fprintf(stderr,
  115.                "unable to read in font from file '%s'",font);
  116.         exit(1);
  117.     }
  118.  
  119.     page = new_bitmap(FAX_WIDTH_LINES, FAX_HEIGHT_LINES);
  120.  
  121.     /* read the stdin a line at a time */
  122.     while( cont == TRUE ) {
  123.         /* do a page */
  124.         clr_bitmap(page,FAX_WIDTH_LINES, FAX_HEIGHT_LINES);
  125.          
  126.         y = TOP_PAD_LINES;
  127.         
  128.         while (y + ppfont-> max_ht <= TEXT_HEIGHT_LINES 
  129.                && cont == TRUE) {
  130.             
  131.             if (start == NULLCP) {
  132.                 if (fgets(buffer, BUFSIZ, stdin) == NULL)
  133.                     cont = FALSE;
  134.                 else {
  135.                     /* knock off trailing \n */
  136.                     if ((start = rindex(buffer, '\n')) != NULLCP)
  137.                         *start = '\0';
  138.                     start = &(buffer[0]);
  139.                 }
  140.             }
  141.             if (cont == TRUE) {
  142.                 if (*start == '\0') {
  143.                     y += ppfont->max_ht;
  144.                     start = NULLCP;
  145.                 } else {
  146.                     int    dy, dx, i = 0;
  147.                     char    *ix;
  148.                     if ((postCtrlL = index(start, ' ')) != NULLCP)
  149.                         *postCtrlL++ = '\0';
  150.                         
  151.                     ix = start;
  152.                     while (ix - start < (int)strlen(start) 
  153.                            && y + ppfont->max_ht <= TEXT_HEIGHT_LINES) {
  154.                         i = str_into_bitmap(page,
  155.                                     ppfont,
  156.                                     LEFT_PAD_LINES,
  157.                                     y,
  158.                                     &dx,
  159.                                     &dy,
  160.                                     FAX_WIDTH_LINES - LEFT_PAD_LINES,
  161.                                     ix,
  162.                                     strlen(ix));
  163.                         ix += i;
  164.                         y += dy;
  165.                     }
  166.                     
  167.                     if (ix - start >= (int) strlen(start))
  168.                         start = NULLCP;
  169.                     else
  170.                         start = ix;
  171.  
  172.                     if (postCtrlL != NULLCP) {
  173.                         /* force new page */
  174.                         if (*postCtrlL == '\0')
  175.                             start = NULLCP;
  176.                         else
  177.                             start = postCtrlL;
  178.                         postCtrlL = NULLCP;
  179.                         y = TEXT_HEIGHT_LINES;
  180.                     }
  181.                 }
  182.             }
  183.         }
  184.         encodeImage(page, 
  185.                 FAX_WIDTH_LINES, FAX_HEIGHT_LINES,
  186.                 p2);
  187.     }
  188.     
  189.     outputFax(p2, stdout);
  190.     exit(0);
  191.  
  192. int
  193. initialise_globals()
  194. {
  195.     char    buffer[BUFSIZ];
  196.  
  197.     if (table != NULLTBL) {
  198.         if (font == NULLCP
  199.             && tb_k2val (table, "textfont", buffer, TRUE) != NOTOK)
  200.             font = strdup(buffer);
  201.     }
  202. }
  203.     
  204.