home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 2 / goldfish_vol2_cd1.bin / files / comm / misc / elcheapofax / rcs / asc2fax.c,v < prev    next >
Text File  |  1993-12-21  |  10KB  |  484 lines

  1. head    1.2;
  2. access;
  3. symbols
  4.     OCT93:1.2;
  5. locks;
  6. comment    @ * @;
  7.  
  8.  
  9. 1.2
  10. date    93.06.11.16.33.37;    author Rhialto;    state Exp;
  11. branches;
  12. next    1.1;
  13.  
  14. 1.1
  15. date    93.06.11.14.53.53;    author Rhialto;    state Exp;
  16. branches;
  17. next    ;
  18.  
  19.  
  20. desc
  21. @Convert ASCII text to g3 files
  22. @
  23.  
  24.  
  25. 1.2
  26. log
  27. @First real RCS checkin
  28. @
  29. text
  30. @/*
  31.  * asc2fax.c
  32.  *
  33.  * Copyright (C) 1993 by Olaf 'Rhialto' Seibert. All rights reserved.
  34.  *
  35.  * V24.05.93: Initial release
  36.  * V29.05.93: Fixed unsigned character bug
  37.  *
  38.  * $Id$
  39.  * $Log$
  40.  */
  41.  
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #include <string.h>
  45. #include <ctype.h>
  46.  
  47. #define INTUI_V36_NAMES_ONLY
  48. #include <utility/tagitem.h>
  49. #include <intuition/intuition.h>
  50. #include <clib/exec_protos.h>
  51. #include <clib/alib_protos.h>
  52. #include <clib/graphics_protos.h>
  53. #include <clib/intuition_protos.h>
  54. #include <clib/diskfont_protos.h>
  55.  
  56. #include "faxfile.h"
  57.  
  58. #define RASTERWIDTH LINE_BITS
  59. #define ESC        "\33"
  60. #define CSI        "\233"
  61. #define uchar(x)    ((unsigned char)(x))
  62.  
  63. int        verbose;
  64. int        xoffset = 50;
  65. int        yoffset = 50;
  66. int        invert;
  67. void           *IntuitionBase;
  68. void           *GfxBase;
  69. void           *DiskFontBase;
  70. struct BitMap    BitMap;
  71. struct RastPort EmergencyRastPort;
  72. struct RastPort *RastPort;
  73. struct Window  *Window;
  74. struct TextFont *Font;
  75. struct TextFont *OldFont;
  76. struct NewWindow NewWindow = {
  77.     0, 20,            /* LeftEdge, TopEdge */
  78.     640, 0,            /* Width, Height (calculated and set) */
  79.     1, 1,            /* DetailPen, BlockPen */
  80.     0,                /* IDCMPFlags */
  81.     WFLG_SUPER_BITMAP | WFLG_GIMMEZEROZERO | WFLG_NOCAREREFRESH |
  82.     WFLG_DRAGBAR | WFLG_DEPTHGADGET | WFLG_SIZEGADGET | WFLG_SIZEBRIGHT, /* Flags */
  83.     NULL,            /* FirstGadget */
  84.     NULL,            /* CheckMark */
  85.     NULL,            /* Title */
  86.     NULL,            /* Screen */
  87.     &BitMap,            /* BitMap */
  88.     20,20, -1,0,        /* Min/Max Width/Height */
  89.     WBENCHSCREEN        /* Type */
  90. };
  91. struct TagItem TextTags[] = {
  92.     TA_DeviceDPI, X_DPI | Y_DPI << 16,
  93.     TAG_END
  94. };
  95. struct TTextAttr TextAttr = {
  96.     "courier.font", 30, FSF_TAGGED, 0, TextTags
  97. };
  98.  
  99. void
  100. meminvert(unsigned char *d, int size)
  101. {
  102.     if (((long) d & 0x01) == 0) {
  103.     while (size >= 4) {
  104.         *(long *)d ^= 0xFFFFFFFF;
  105.         d += 4;
  106.         size -= 4;
  107.     }
  108.     }
  109.     while (size > 0) {
  110.     *d++ ^= 0xFF;
  111.     size--;
  112.     }
  113. }
  114.  
  115. struct TextInfo {
  116.     struct RastPort *rp;
  117.     int         xoffset;
  118. } ti;
  119.  
  120. unsigned char  *
  121. DoText(unsigned char *text, int len)
  122. {
  123.     if (len > 0)
  124.     Text(ti.rp, text, len);
  125.     return text + len;
  126. }
  127.  
  128. unsigned char  *
  129. DoCSI(unsigned char *text)
  130. {
  131.     int         arg[8];
  132.     int         narg = 0;
  133.     int         i;
  134.  
  135.     memset(arg, 0, sizeof(arg));
  136.     /* CSI 0 x and CSI x are indistinguishable */
  137.     for(;;text++) {
  138.     while (isdigit(text[0])) {
  139.         if (narg < 8) {
  140.         arg[narg] *= 10;
  141.         arg[narg] += text[0] - '0';
  142.         }
  143.         text++;
  144.     }
  145.     narg++;
  146.     if (text[0] != ';')
  147.         break;
  148.     }
  149.     if (narg > 8)
  150.     narg = 8;
  151.  
  152.     switch (*text++) {
  153.     case 'm':   /* Select graphic rendition */
  154.     for (i = 0; i < narg; i++) {
  155.         switch (arg[i]) {
  156.         case 0:    /* plain */
  157.         SetSoftStyle(ti.rp, FS_NORMAL, FSF_BOLD|FSF_ITALIC|FSF_UNDERLINED);
  158.         SetDrMd(ti.rp, JAM1);
  159.         break;
  160.         case 1:    /* bold */
  161.         SetSoftStyle(ti.rp, FSF_BOLD, FSF_BOLD);
  162.         break;
  163.         case 3:    /* italic */
  164.         SetSoftStyle(ti.rp, FSF_ITALIC, FSF_ITALIC);
  165.         break;
  166.         case 4:    /* underline */
  167.         SetSoftStyle(ti.rp, FSF_UNDERLINED, FSF_UNDERLINED);
  168.         break;
  169.         case 7:    /* inverse video */
  170.         SetDrMd(ti.rp, JAM1 | INVERSVID);
  171.         break;
  172.         case 22:     /* not bold */
  173.         SetSoftStyle(ti.rp, 0, FSF_BOLD);
  174.         break;
  175.         case 23:     /* not italic */
  176.         SetSoftStyle(ti.rp, 0, FSF_ITALIC);
  177.         break;
  178.         case 24:     /* not underline */
  179.         SetSoftStyle(ti.rp, 0, FSF_UNDERLINED);
  180.         break;
  181.         case 27:     /* not inverse video */
  182.         SetDrMd(ti.rp, JAM1);
  183.         break;
  184.         }
  185.     }
  186.     break;
  187.     case 'x':           /* set left offset */
  188.     ti.xoffset = arg[0];
  189.     break;
  190.     }
  191.  
  192.     return text;
  193. }
  194.  
  195. unsigned char  *
  196. DoCtrl(unsigned char *text)
  197. {
  198.     switch (*text++) {
  199.     case '\t':          /* tab */
  200.     {
  201.         int         charpos;
  202.  
  203.         charpos = (ti.rp->cp_x - ti.xoffset) / ti.rp->TxWidth;
  204.         charpos = (charpos + 8) & ~7;
  205.         Move(ti.rp, ti.xoffset + charpos * ti.rp->TxWidth, ti.rp->cp_y);
  206.     }
  207.     break;
  208.     case '\n':          /* newline */
  209.     Move(ti.rp, ti.xoffset, ti.rp->cp_y + ti.rp->TxHeight);
  210.     break;
  211.     case '\f':          /* formfeed */
  212.     SetRast(ti.rp, 0);
  213.     Move(ti.rp, ti.xoffset, ti.rp->TxBaseline);
  214.     break;
  215.     case uchar('\233'): /* control sequence introducer */
  216.     goto csi;
  217.     case '\033':        /* escape */
  218.     switch (*text++) {
  219.     case 'c':       /* reset */
  220.         ti.xoffset = 0;
  221.         ti.rp->Mask = 0x0001;
  222.         SetAPen(ti.rp, 1);
  223.         SetBPen(ti.rp, 0);
  224.         DoCtrl("\f");
  225.         DoCSI("0m");
  226.         break;
  227.     case '[':       /* CSI */
  228.     csi:
  229.         text = DoCSI(text);
  230.         break;
  231.     }
  232.     break;
  233.     }
  234.  
  235.     return text;
  236. }
  237.  
  238. void
  239. WinWrite(unsigned char *text)
  240. {
  241.     while (*text) {
  242.     unsigned char  *p;
  243.     int        len;
  244.  
  245.     /* First, determine how much real text we have */
  246.     for (len = 0, p = text; isprint(*p); p++) {
  247.         len++;
  248.     }
  249.     text = DoText(text, len);
  250.     if (*text && !isprint(*text))
  251.         text = DoCtrl(text);
  252.     }
  253. }
  254.  
  255. void
  256. WinWriteInit(struct RastPort *rp)
  257. {
  258.     ti.rp = rp;
  259.     DoCtrl(ESC"c");
  260. }
  261.  
  262.  
  263. long
  264. dofile(char *ascname, void *faxp)
  265. {
  266.     unsigned char   line[256];
  267.     FILE       *file;
  268.     struct RastPort *rp = RastPort;
  269.  
  270.     file = fopen(ascname, "r");
  271.     if (file == NULL) {
  272.     printf("Can't open file %s.\n", ascname);
  273.     return 1;
  274.     }
  275.  
  276.     faxout_begin_page(faxp, 1);
  277.  
  278.     WinWriteInit(RastPort);
  279.     /* reset, set x offset */
  280.     sprintf(line, ESC"c" CSI"%dx", xoffset);
  281.     WinWrite(line);
  282.  
  283.     if (yoffset) {
  284.     int        i;
  285.  
  286.     for (i = 0; i < yoffset; i++)
  287.         tofax(faxp, BitMap.Planes[0], RASTERWIDTH);
  288.     }
  289.  
  290.     for (;;) {
  291.     int        i;
  292.     unsigned char  *plane;
  293.  
  294.     if (feof(file))
  295.         break;
  296.     if (fgets(line, sizeof(line), file) == NULL)
  297.         break;
  298.     if (plane = strchr(line, '\n'))
  299.         *plane = '\0';
  300.  
  301.     if (verbose)
  302.         printf("%s\n", line);
  303.     WinWrite("\f");
  304.     WinWrite(line);
  305.  
  306.     SyncSBitMap(rp->Layer);
  307.     plane = BitMap.Planes[0];
  308.     for (i = 0; i < Font->tf_YSize; i++) {
  309.         if (invert)
  310.         meminvert(plane, BitMap.BytesPerRow);
  311.         tofax(faxp, plane, RASTERWIDTH);
  312.         plane += BitMap.BytesPerRow;
  313.     }
  314.     }
  315.  
  316.     faxout_end_page(faxp);
  317.     fclose(file);
  318.  
  319.     return 0;
  320. }
  321.  
  322. void
  323. openall(void)
  324. {
  325.     /* Libraries */
  326.     IntuitionBase = OpenLibrary("intuition.library", 33);
  327.     if (IntuitionBase == NULL) {
  328.     printf("Needs intuition V33+.\n");
  329.     exit(10);
  330.     }
  331.     GfxBase = OpenLibrary("graphics.library", 33);
  332.     if (GfxBase == NULL) {
  333.     printf("Needs gfx V33+.\n");
  334.     exit(10);
  335.     }
  336.     DiskFontBase = OpenLibrary("diskfont.library", 34);
  337.     if (DiskFontBase == NULL) {
  338.     printf("Needs diskfont V34+.\n");
  339.     exit(10);
  340.     }
  341.     /* Font for window; sorry for the strange order */
  342.     printf("Opening DiskFont... (This may take a while)\n");
  343.     Font = OpenDiskFont((struct TextAttr *)&TextAttr);
  344.     if (Font == NULL) {
  345.     printf("No %s %d!\n", TextAttr.tta_Name, TextAttr.tta_YSize);
  346.     exit(10);
  347.     }
  348.     NewWindow.Height = Font->tf_YSize + 16;  /* slight safety fudge */
  349.     /* Raster for text */
  350.     InitBitMap(&BitMap, 1, RASTERWIDTH, NewWindow.Height);
  351.     if ((BitMap.Planes[0] = AllocRaster(RASTERWIDTH, NewWindow.Height)) == NULL) {
  352.     printf("No plane\n");
  353.     exit(10);
  354.     }
  355.     /* Window for raster. For showing-off purposes only. */
  356.     if ((Window = OpenWindow(&NewWindow)) == NULL) {
  357.     printf("No window (probably too large). Will do without.\n");
  358.     }
  359.     if (Window) {
  360.     RastPort = Window->RPort;
  361.     } else {
  362.     RastPort = &EmergencyRastPort;
  363.     InitRastPort(RastPort);
  364.     RastPort->BitMap = &BitMap;
  365.     }
  366.     RastPort->Mask = 0x0001;
  367.     OldFont = RastPort->Font;
  368.     SetFont(RastPort, Font);
  369. }
  370.  
  371.  
  372. /*
  373.  * Clean up system stuff in case of exit
  374.  */
  375. void
  376. cleanup(void)
  377. {
  378.     if (Font) {
  379.     SetFont(RastPort, OldFont);
  380.     CloseFont(Font);
  381.     }
  382.     if (Window) {
  383.     CloseWindow(Window);
  384.     }
  385.     if (GfxBase) {
  386.     if (BitMap.Planes[0]) {
  387.         FreeRaster(BitMap.Planes[0], RASTERWIDTH, NewWindow.Height);
  388.         BitMap.Planes[0] = NULL;
  389.     }
  390.     CloseLibrary(GfxBase);
  391.     }
  392.     if (IntuitionBase) {
  393.     CloseLibrary(IntuitionBase);
  394.     }
  395.     if (DiskFontBase) {
  396.     CloseLibrary(DiskFontBase);
  397.     }
  398. }
  399.  
  400. int
  401. main(int argc, char **argv)
  402. {
  403.     char       *outfile = "ascii.g3";
  404.     struct faxout  *faxp;
  405.     int         rawfax = 1;
  406.     int         append = 0;
  407.     extern char    *optarg;
  408.     extern int        optind;
  409.     extern int        getopt(int, char **, char *);
  410.     int         errflg = 0;
  411.     int         c;
  412.  
  413.     while ((c = getopt(argc, argv, "af:io:rs:vx:y:")) != -1) {
  414.     switch (c) {
  415.     case 'a':
  416.         append = 1;
  417.         break;
  418.     case 'f':
  419.         TextAttr.tta_Name = optarg;
  420.         break;
  421.     case 's':
  422.         TextAttr.tta_YSize = atoi(optarg);
  423.         break;
  424.     case 'i':
  425.         invert = 1;
  426.         break;
  427.     case 'o':
  428.         outfile = optarg;
  429.         break;
  430.     case 'r':
  431.         rawfax++;
  432.         break;
  433.     case 'v':
  434.         verbose = TRUE;
  435.         break;
  436.     case 'x':
  437.         xoffset = atoi(optarg);
  438.         break;
  439.     case 'y':
  440.         yoffset = atoi(optarg);
  441.         break;
  442.     case '?':
  443.         errflg++;
  444.         break;
  445.     }
  446.     }
  447.  
  448.     if (errflg || optind >= argc) {
  449.     printf(
  450. "Usage: asc2fax [-o fax-file (ascii.g3)] [-r raw faxfile] [-a (append)]\n"
  451. "               [-x/y x/y-offset (50)] [-v] [-i (invert)]\n"
  452. "               [-f name.font] [-s fontsize] ascii-files\n");
  453.     exit(EXIT_FAILURE);
  454.     }
  455.  
  456.     atexit(cleanup);
  457.     openall();
  458.  
  459.     faxp = faxout_open_fp(fopen(outfile, append? "ab": "wb"), rawfax);
  460.     if (faxp == NULL) {
  461.     fprintf(stderr, "can't open output file %s.\n", outfile);
  462.     goto fail;
  463.     }
  464.  
  465.     while (optind < argc) {
  466.     dofile(argv[optind], faxp);
  467.     optind++;
  468.     }
  469.  
  470.     faxout_close(faxp);
  471. fail:
  472.     /* atexit function cleans up here */
  473. }
  474. @
  475.  
  476.  
  477. 1.1
  478. log
  479. @Initial revision
  480. @
  481. text
  482. @d8 3
  483. @
  484.