home *** CD-ROM | disk | FTP | other *** search
/ FreeWare Collection 2 / FreeSoftwareCollection2pd199x-jp.img / prnout / src / prnout.c < prev    next >
Text File  |  1990-06-14  |  14KB  |  536 lines

  1. #include    <stdio.h>
  2. #include    <ctype.h>
  3. #include    <dos.h>
  4. #include    "defs.h"
  5.  
  6. #define DSP_X   0
  7. #define DSP_Y   19
  8.  
  9. #define    MAX_BUF    0x8000
  10.  
  11.        char     skl_chr[]={ 0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x87 };
  12. static int    left_mrg=0;
  13. static int    page_max=0;
  14. static int    head_pos=0;
  15. static int    line_pos=0;
  16. static int    page_pos=0;
  17. static int    cur_x=0,cur_y=0;
  18. static int    top_flg=0;
  19. static int    act_typ=0;
  20.  
  21. static int      ank_count=0;
  22. static int    kan_spc=0;
  23. static int    ank_spc=0;
  24.  
  25. static int    prb_count=0;
  26. static int    prb_ptr=0;
  27. static int    prb_bas=0;
  28. static int    prb_flg=FALSE;
  29. static UCHAR    prb_buff[MAX_BUF];
  30.  
  31. extern void    prn_out();
  32. extern char     *subname();
  33.  
  34. /*********** Print Out Program Start **************/
  35. int    PRB_status()
  36. {
  37.     int   port;
  38.  
  39.     port = inp(0x0800);
  40.     if ( (port & 0x80) == 0 )
  41.     return (port & 0x01);
  42.     else
  43.     return 0;
  44. }
  45. void    PRB_out()
  46. {
  47.     while ( prb_count > 0 && PRB_status() != 0 ) {
  48.     outp(0x0800,prb_buff[prb_bas++]);
  49.     prb_bas &= (MAX_BUF-1); prb_count--;
  50.     }
  51. }
  52. int    get_key()
  53. {
  54.     int     ch;
  55.  
  56.     while ( kbhit() == 0 )
  57.     PRB_out();
  58.     return getch();
  59. }
  60. void    PRB_chr(ch)
  61. UCHAR    ch;
  62. {
  63.     while ( prb_count >= (MAX_BUF-1000) ) {
  64.     PRB_out();
  65.         if ( kbhit() != 0 )
  66.         break;
  67.     }
  68.     prb_buff[prb_ptr++] = ch;
  69.     prb_ptr &= (MAX_BUF-1); prb_count++;
  70. }
  71. void    PRB_byte(str,n)
  72. char    *str;
  73. int     n;
  74. {
  75.     while ( n-- > 0 )
  76.         PRB_chr(*(str++));
  77. }
  78. void    DMY_chr(ch)     /* DUMMY Out */
  79. UNSIG   ch;
  80. {
  81.     int     x,y,n,off;
  82.     static int  offx,offy;
  83.     static int  sx,sy;
  84.  
  85.     if ( ch == 0x0D )
  86.     return;
  87.  
  88.     if ( cur_x == 0 && cur_y == 0 ) {
  89.     x = (yousi[paper].yoko * 225L) / 254L;
  90.     y = (yousi[paper].tate * 225L) / 254L;
  91.     x = ((GRA_MAXX - GRA_OFFX) - x) / 10;
  92.     y = ((GRA_MAXY - GRA_OFFY) - y) / 30;
  93.     offx = (page_pos % x) * 10;
  94.     offy = (3 * offx / x) + ((page_pos / x) % y) * 30;
  95.     pp_box(offx,offy);
  96.     sx = (printer[typ_flg].yoko * 1800L) / 254L;
  97.     sy = (printer[typ_flg].tate * 1800L) / 254L;
  98.     }
  99.     if ( ch == ' ' ) {
  100.     cur_x++;
  101.     return;
  102.     } else if ( ch == 0x8140 ) {
  103.     cur_x += 2;
  104.     return;
  105.     } else if ( ch == '\n' ) {
  106.     cur_x = 0;
  107.     cur_y++;
  108.     return;
  109.     } else if ( ch == '\x0c' ) {
  110.     cur_x = cur_y = 0;
  111.     return;
  112.     }
  113.     x = sx + cur_x * (yousi[paper].spc + 24);
  114.     y = sy + cur_y * (yousi[paper].lf + 24);
  115.     x = (x >> 4) + GRA_OFFX + offx;
  116.     y = (y >> 3) + GRA_OFFY + offy;
  117.  
  118.     if ( (ch & 0xFF00) != 0 ) {
  119.     cur_x += 2; n = 1;
  120.     } else {
  121.     cur_x += 1; n = 0;
  122.     }
  123.     line(x,y,x+n,y+1,PSET,0,FBOX,LINE_1);
  124. }
  125. UNSIG   sjistojis(cd)
  126. UNSIG   cd;
  127. {
  128.     int    hi,lo;
  129.  
  130.     if ( (cd & 0x8000) == 0 )
  131.     return cd;
  132.  
  133.     hi=(cd >> 8)&0xff;
  134.     lo=cd & 0xff;
  135.     hi -= ( hi <= 0x9f) ? 0x71 : 0xb1;
  136.     hi = hi * 2 +1;
  137.     if ( lo > 0x7f )
  138.         lo--;
  139.     if ( lo >= 0x9e ) {
  140.         lo -= 0x7d;
  141.         hi++;
  142.     }
  143.     else
  144.         lo -= 0x1f;
  145.     return (hi << 8 | lo);
  146. }
  147. static UNSIG   han_to_zen(code)
  148. UNSIG   code;
  149. {
  150.     static UNSIG ank_tbl[]={
  151.         0x8140,0x8149,0x8168,0x8194,0x8190,0x8193,0x8195,0x8166,
  152.         0x8169,0x816A,0x8196,0x817B,0x8143,0x817C,0x8144,0x815E,
  153.         0x824F,0x8250,0x8251,0x8252,0x8253,0x8254,0x8255,0x8256,
  154.         0x8257,0x8258,0x8146,0x8147,0x8183,0x8181,0x8184,0x8148,
  155.         0x8197,0x8260,0x8261,0x8262,0x8263,0x8264,0x8265,0x8266,
  156.     0x8267,0x8268,0x8269,0x826A,0x826B,0x826C,0x826D,0x826E,
  157.         0x826F,0x8270,0x8271,0x8272,0x8273,0x8274,0x8275,0x8276,
  158.         0x8277,0x8278,0x8279,0x816D,0x818F,0x816E,0x814F,0x8151,
  159.         0x8166,0x8281,0x8282,0x8283,0x8284,0x8285,0x8286,0x8287,
  160.         0x8288,0x8289,0x828A,0x828B,0x828C,0x828D,0x828E,0x828F,
  161.         0x8290,0x8291,0x8292,0x8293,0x8294,0x8295,0x8296,0x8297,
  162.         0x8298,0x8299,0x829A,0x816F,0x8162,0x8170,0x8150,0x85A1 };
  163.     static UNSIG kana_tbl[]={
  164.         0x8140,0x8142,0x8175,0x8176,0x8141,0x8145,0x8392,0x8340,
  165.         0x8342,0x8344,0x8346,0x8348,0x8383,0x8385,0x8387,0x8362,
  166.         0x815B,0x8341,0x8343,0x8345,0x8347,0x8349,0x834A,0x834C,
  167.         0x834E,0x8350,0x8352,0x8354,0x8356,0x8358,0x835A,0x835C,
  168.         0x835E,0x8360,0x8363,0x8365,0x8367,0x8369,0x836A,0x836B,
  169.         0x836C,0x836D,0x836E,0x8371,0x8374,0x8377,0x837A,0x837D,
  170.         0x837E,0x8380,0x8381,0x8382,0x8384,0x8386,0x8388,0x8389,
  171.         0x838A,0x838B,0x838C,0x838D,0x838F,0x8393,0x814A,0x814B };
  172.  
  173.     if ( 0x20 <= code && code <= 0x7F )
  174.         return ank_tbl[code-0x20];
  175.     else if ( 0xA0 <= code && code <= 0xDF )
  176.     return kana_tbl[code-0xA0];
  177.     else
  178.         return 0x85A1;
  179. }
  180. /*  .(.全角禁足文字のチェック.).  */
  181. static int     kinsoku_chk(i)
  182. UNSIG   i;
  183. {
  184.     if ( kin_flg != FALSE )
  185.         return FALSE;
  186.  
  187.     if ( (i & 0xFF00) == 0 ) {
  188.         switch(i) {
  189.             case 0x27: case 0x29: case 0x2c: case 0x2e: case 0x3a:
  190.             case 0x3b: case 0x7d:
  191.             case 0xa1: case 0xa3: case 0xa4: case 0xde: case 0xdf:
  192.                 return TRUE;
  193.         }
  194.         return FALSE;
  195.     }
  196.  
  197.     if (i >= 0x8141 && i <= 0x814c)
  198.         return TRUE;
  199.     if (i >= 0x8165 && i <= 0x817a) {
  200.         if ((i % 2) == 0)
  201.             return TRUE;
  202.     }
  203.     switch(i) {
  204.         case 0x829f: case 0x82a1: case 0x82a3: case 0x82a5: case 0x82a7:
  205.         case 0x82c1: case 0x82e1: case 0x82e3: case 0x82e5: case 0x82ec:
  206.         case 0x8340: case 0x8342: case 0x8344: case 0x8346: case 0x8348:
  207.         case 0x8362: case 0x8383: case 0x8385: case 0x8387: case 0x838e:
  208.         case 0x8395: case 0x8396:
  209.             return TRUE;
  210.     }
  211.     return FALSE;
  212. }
  213. static void    prn_chr_out(ch)
  214. UCHAR   ch;
  215. {
  216.     if ( ryo_flg == FALSE && odd_flg != (page_pos & 1) )
  217.     return;
  218.  
  219.     if ( typ_flg != DUMMY ) {
  220.     if ( fnt_flg != 0 )
  221.         fnt_ank(ch);
  222.     else {
  223.         if ( ch == '\n' )
  224.         PRB_chr('\x0D');
  225.         PRB_chr(ch);
  226.     }
  227.     }
  228.     DMY_chr(ch);
  229. }
  230. static void    prn_kan_out(code)
  231. UNSIG   code;
  232. {
  233.     if ( ryo_flg == FALSE && odd_flg != (page_pos & 1) )
  234.     return;
  235.  
  236.     if ( typ_flg != DUMMY ) {
  237.         code = sjistojis(code);
  238.     if ( fnt_flg != 0 )
  239.         fnt_kan(code);
  240.     else {
  241.             PRB_chr(code >> 8);
  242.             PRB_chr((UCHAR)code);
  243.     }
  244.     }
  245. }
  246. void    prn_line_out(str)
  247. char    *str;
  248. {
  249.     if ( ryo_flg == FALSE && odd_flg != (page_pos & 1) )
  250.         return;
  251.  
  252.     if ( typ_flg != DUMMY ) {
  253.         while ( *str != '\0' )
  254.             PRB_chr(*(str++));
  255.     }
  256. }
  257. static void    page_out()
  258. {
  259.     int     i;
  260.     char    *p,tmp[20];
  261.  
  262.     head_pos = 0; line_pos = (-2);
  263.     prn_out('\n');
  264.     for ( i = yousi[paper].lmax / 2 - 2 ; i > 0 ; i-- )
  265.         prn_out(' ');
  266.     sprintf(tmp,"-%d-\n",page_pos+1);
  267.     for ( p = tmp ; *p != '\0' ; p++ )
  268.         prn_out(*p);
  269. }
  270. void    prn_out(code)
  271. UNSIG   code;
  272. {
  273.     int     i;
  274.  
  275.     if ( code >= ' ' &&
  276.         (head_pos + ((code & 0xFF00) != 0 ? 2:1)) > yousi[paper].lmax &&
  277.          kinsoku_chk(code) == FALSE )
  278.         prn_out('\n');
  279.  
  280.     if ( top_flg == FALSE && head_pos == 0 && line_pos == 0 ) {
  281.     top_flg = TRUE;
  282.     for ( i = yousi[paper].pmrg ; i > 0 ; i-- )
  283.             prn_chr_out('\n');
  284.     }
  285.  
  286.     if ( head_pos == 0 && code >= 0x20 ) {
  287.     if ( fnt_flg == 0 ) {
  288.         switch(typ_flg) {
  289.                 case FUJITU: prn_line_out("\x1bQ1 |"); break;
  290.                 case ESCP:   prn_line_out("\x1c&\x1c\x0f");
  291.                              kan_flg = FALSE; break;
  292.         }
  293.         }
  294.     for ( i = 0 ; i < left_mrg ; i++ ) {
  295.         head_pos = ERR;
  296.         prn_out(' ');
  297.         }
  298.     head_pos = 0;
  299.     }
  300.  
  301.     if ( (code & 0xFF00) != 0 ) {
  302.     if ( ryo_flg != FALSE || odd_flg != (page_pos & 1) )
  303.             DMY_chr(code);              /* 注意 */
  304.         switch(typ_flg) {
  305.             case FUJITU:
  306.                 if ( kan_flg == FALSE && fnt_flg == 0 )
  307.                     prn_line_out("\x1b$B");
  308.             prn_kan_out(code);
  309.                 break;
  310.             case ESCP:
  311.                 if ( kan_flg == FALSE && fnt_flg == 0 )
  312.                     prn_line_out("\x1c\x12");
  313.             prn_kan_out(code);
  314.                 break;
  315.             case PC98:
  316.             prn_kan_out(code);
  317.         PRB_chr('\x1b');
  318.         PRB_chr(kan_spc);
  319.                 break;
  320.         }
  321.         kan_flg = TRUE;
  322.         head_pos += 2;
  323.     } else if ( code >= 0x20 ) {
  324.     switch(typ_flg) {
  325.         case FUJITU:
  326.                 if ( kan_flg != FALSE && fnt_flg == 0 )
  327.                     prn_line_out("\x1b(H");
  328.                 prn_chr_out((UCHAR)code);
  329.                 break;
  330.             case ESCP:
  331.         if ( fnt_flg == 0 ) {
  332.             if ( ryo_flg != FALSE || odd_flg != (page_pos & 1) )
  333.                     DMY_chr(code);              /* 注意 */
  334.                     if ( kan_flg != FALSE )
  335.                         prn_line_out("\x1c\x0f");
  336.                     code = han_to_zen(code);
  337.                     prn_kan_out(code);
  338.         } else
  339.             prn_chr_out(code);
  340.                 break;
  341.             case PC98:
  342.         if ( fnt_flg == 0 ) {
  343.             if ( ryo_flg != FALSE || odd_flg != (page_pos & 1) )
  344.                         DMY_chr(code);              /* 注意 */
  345.                     prn_kan_out(code);
  346.             PRB_chr('\x1b');
  347.             if ( (kan_spc  & 1) != 0 && 
  348.              (ank_count & 1) != 0 )
  349.             PRB_chr(ank_spc + 1);
  350.             else
  351.             PRB_chr(ank_spc);
  352.         } else
  353.             prn_chr_out(code);
  354.                 break;
  355.             case DUMMY:
  356.         prn_chr_out((UCHAR)code);
  357.         break;
  358.         }
  359.         kan_flg = FALSE;
  360.         head_pos++;
  361.         ank_count++;
  362.     } else if ( code == '\t' ) {
  363.         i = 8 - (head_pos & 0x07);
  364.         while ( i-- > 0 )
  365.             prn_out(' ');
  366.     } else if ( code == '\x0C' ) {
  367.         if ( page_flg == FALSE ) {
  368.             while ( line_pos++ < page_max )
  369.                 prn_chr_out('\n');
  370.             page_out();
  371.         }
  372.         head_pos = line_pos = ank_count = 0;
  373.         prn_chr_out('\x0C');
  374.         page_pos++;
  375.     top_flg = FALSE;
  376.     } else if ( code == '\n' ) {
  377.         prn_chr_out((UCHAR)code);
  378.         head_pos = ank_count = 0;
  379.         if ( ++line_pos >= page_max )
  380.             prn_out('\x0C');
  381.     } else
  382.         prn_chr_out((UCHAR)code);
  383. }
  384. void    prnerr(str)
  385. char    *str;
  386. {
  387.     locate(DSP_X+1,DSP_Y+3);
  388.     printf("%-34s",str);
  389.     fflush(stdout);
  390. }
  391. void    file_out(file)
  392. char    *file;
  393. {
  394.     int     i,ch,old_mrg;
  395.     int     old_page=(-1);
  396.     long    l,size;
  397.     FILE    *inp_fp;
  398.     char    *p,tmp[80];
  399.  
  400.     if ( prb_count > 0 &&
  401.      typ_flg != DUMMY && act_typ != DUMMY && 
  402.      act_typ != typ_flg ) {
  403.         prnerr("印字中です");
  404.         return;
  405.     }
  406.     act_typ = typ_flg;
  407.  
  408.     if ( (inp_fp = fopen(file,"rb")) == NULL ) {
  409.         prnerr("ファイルが見当たりません");
  410.         return;
  411.     }
  412.     if ( gra_img(inp_fp,file) != FALSE ) {
  413.     fclose(inp_fp);
  414.     return;
  415.     }
  416.     if ( fnt_flg != 0 && typ_flg != DUMMY && fnt_init() != 0 ) {
  417.         prnerr("フォント ファイルが見当たりません");
  418.     fclose(inp_fp);
  419.         return;
  420.     }
  421.     fseek(inp_fp,0L,2);
  422.     size = ftell(inp_fp);
  423.     fseek(inp_fp,0L,0);
  424.  
  425.     if ( dsp_flg != FALSE )
  426.     G_era();
  427.  
  428.     switch(typ_flg) {
  429.         case FUJITU:
  430.         prn_line_out("\x1bQ3 \x5c");
  431.         i = yousi[paper].spc + 24;
  432.         sprintf(tmp,"\x1c$%c%c",i/10+0x20,i%10+0x70);
  433.         prn_line_out(tmp);
  434.         i = yousi[paper].lf + 24;
  435.         sprintf(tmp,"\x1c%%%c%c\x1b(H",i/10+0x20,i%10+0x70);
  436.         prn_line_out(tmp);
  437.         break;
  438.     case ESCP:
  439.         PRB_chr('\x1c');
  440.         PRB_chr('S');
  441.         i = yousi[paper].spc / 2;
  442.         PRB_chr(i);
  443.         i = yousi[paper].spc - i;
  444.         PRB_chr(i);
  445.  
  446.         PRB_chr('\x1c');
  447.         PRB_chr('T');
  448.         i = (yousi[paper].spc + 24) / 2 - 11;
  449.         PRB_chr(i / 2);
  450.         i = i - (i / 2);
  451.         PRB_chr(i);
  452.  
  453.         if ( (yousi[paper].spc & 1) != 0 )
  454.         prn_line_out("\x1cU");
  455.         else
  456.         prn_line_out("\x1cV");
  457.  
  458.         PRB_chr('\x1B'); PRB_chr('3');
  459.         PRB_chr(yousi[paper].lf + 24);
  460.  
  461.         prn_line_out("\x1c\x0f");
  462.         break;
  463.         case PC98:
  464.         prn_line_out("\x1b\x45");
  465.         i = yousi[paper].lf + 24;
  466.         i = (i * 120) / 180;
  467.         sprintf(tmp,"\x1bT%02d\x1bK",i);
  468.         prn_line_out(tmp);
  469.  
  470.         if ( (kan_spc = yousi[paper].spc - 3) < 0 )
  471.         kan_spc = 0;
  472.         kan_spc = (kan_spc * 120) / 180;
  473.         ank_spc = kan_spc / 2;
  474.         break;
  475.     }
  476.     odd_flg = 0;
  477.     old_mrg = left_mrg = yousi[paper].lmrg;
  478.     page_max = yousi[paper].pmax;
  479.     if ( page_flg == 0 )
  480.     page_max -= 2;
  481. REPRINT:
  482.     locate(DSP_X+1,DSP_Y+1);
  483.     repchr(34,' ');
  484.     l = 0l; i = 0;
  485.     cur_x = cur_y = 0; top_flg = FALSE;
  486.     head_pos = line_pos = page_pos = 0;
  487.     while ( (ch = getc(inp_fp)) != EOF ) {
  488.     l += 272;
  489.     if ( old_page != page_pos ) {
  490.         locate(DSP_X+1,DSP_Y+3);
  491.         printf("只今%d枚目を印刷してます  ",page_pos+1);
  492.         old_page = page_pos;
  493.     }
  494.     if ( iskanji(ch) ) {
  495.             ch = (ch << 8) | getc(inp_fp);
  496.             l += 272;
  497.         }
  498.     if ( ch != '\x1a' && ch != '\x0D' )
  499.         prn_out(ch);
  500.         if ( kbhit() != 0 ) {
  501.             getch();
  502.             break;
  503.         }
  504.         while ( l >= size ) {
  505.             locate(DSP_X+1+(i / 8),DSP_Y+1);
  506.             repchr(1,skl_chr[i % 8]);
  507.             l -= size;
  508.             i++;
  509.         }
  510.     PRB_out();
  511.     }
  512.     prn_out('\x0C');
  513.     if ( ryo_flg == 0 && odd_flg == 0 && page_pos > 1 ) {
  514.         fseek(inp_fp,0L,0);
  515.         odd_flg = 1;
  516.         i = (yousi[paper].yoko * 1800L) / 127L;
  517.         i = i / (yousi[paper].spc + 24);
  518.         left_mrg = i - (yousi[paper].lmrg + yousi[paper].lmax);
  519.         locate(DSP_X+1,DSP_Y+3);
  520.         printf("用紙を裏返してください      ");
  521.         fflush(stdout);
  522.         get_key();
  523.         yousi[paper].lmrg = left_mrg;
  524.         pp_box(TRUE);
  525.         goto REPRINT;
  526.     }
  527.     if ( ryo_flg == 0 )
  528.         yousi[paper].lmrg = old_mrg;
  529.     fclose(inp_fp);
  530.  
  531.     locate(DSP_X+1,DSP_Y+3);
  532.     printf("全部で%d枚印刷しました      ",page_pos);
  533.     if ( fnt_flg != 0 && typ_flg != DUMMY )
  534.     fnt_close();
  535. }
  536.