1 #include "tfilter.h"
2 
3 #include "textout.h"
4 #include "textpath.h"
5 #include <string.h>
6 
7 /*
8    This defines the LaTeX versions of the output.  Mostly, this setups up
9    the general engine and defines the PutChar routine (which handles the
10    special LaTeX cases)
11  */
12 
Setup()13 int TextOutTeX::Setup( )
14 {
15     InStream *ins;
16     int      rc = 0;
17 
18     err          = new ErrHandMsg( );
19     lfont	 = 0;
20     mode         = 0;
21     nl           = 0;
22     last_was_nl	 = 1;
23     last_was_par = 1;
24     debug_flag   = 0;
25     next         = 0;
26     strcpy( newline_onoutput, "\n" );
27     userops	 = new SrList( 127 );
28     ins		 = new InStreamFile( TEXTFILTER_PATH,
29     				     "TEXTFILTER_PATH", "latex.def", "r" );
30     if (!ins->status)
31         ReadCommands( ins );
32     else
33     	rc = 1;
34     delete ins;
35     return err->ErrMsg( rc, "Could not open latex.def file in path "
36 	  TEXTFILTER_PATH " or  TEXTFILTER_PATH environment variable" );
37 }
38 
TextOutTeX(OutStream * outs)39 TextOutTeX::TextOutTeX( OutStream *outs )
40 {
41     Setup();
42     out = outs;
43 }
44 
TextOutTeX()45 TextOutTeX::TextOutTeX( )
46 {
47     Setup();
48     out = 0;
49 }
50 
51 /* This handles the special cases for TeX in non-raw mode */
PutChar(const char ch)52 int TextOutTeX::PutChar( const char ch )
53 {
54     int rc;
55 
56     if (ch)
57       UpdateNL( ch == '\n' );
58     if (mode && *mode) {
59       // Verbatim mode
60       if (debug_flag && ch) printf( "putting char %c in verbatim\n", ch );
61       return out->PutChar( ch );
62     }
63     if (debug_flag && ch) printf( "putting char %c in TeX mode\n", ch );
64     switch (ch) {
65 	case '\\': rc = out->PutQuoted( 0, "{\\tt \\char`\\\\}" ); break;
66 	case '<': rc = out->PutQuoted( 0, "$<$" ); break;
67 	case '>': rc = out->PutQuoted( 0, "$>$" ); break;
68 	case '=': rc = out->PutQuoted( 0, "$=$" ); break;
69 	case '^': rc = out->PutQuoted( 0, "{\\tt \\char`\\^}" ); break;
70 	case '_': rc = out->PutQuoted( 0, "{\\tt \\char`\\_}" ); break;
71 	case '#': rc = out->PutQuoted( 0, "{\\tt \\char`\\#}" ); break;
72 	case '&': rc = out->PutQuoted( 0, "\\&" ); break;
73 	case '$': rc = out->PutQuoted( 0, "\\$" ); break;
74 	case '%': rc = out->PutQuoted( 0, "\\%" ); break;
75 	case '|': rc = out->PutQuoted( 0, "{\\tt \\char`\\|}" ); break;
76 	case '{': rc = out->PutQuoted( 0, "\\{" ); break;
77 	case '}': rc = out->PutQuoted( 0, "\\}" ); break;
78 	default: rc = out->PutChar( ch );
79     }
80 	if (rc)
81         	return err->ErrMsg( rc, "Error writing character" );
82        	else return 0;
83 }
84 
PutToken(int nsp,const char * token)85 int TextOutTeX::PutToken( int nsp, const char *token )
86 {
87     int rc;
88     rc = out->PutToken( nsp, (char *)0 );
89     if (!rc) err->ErrMsg( rc, "Error writing token" );
90     while (*token) {
91 	 rc = PutChar( *token++ );
92 	 if (rc) { err->ErrMsg( rc, "Error writing character" ); break; }
93 	 }
94      return rc;
95 }
96 
PutTokenRaw(int nsp,const char * token)97 int TextOutTeX::PutTokenRaw(int nsp, const char *token)
98 {
99     // This is the special case: Assume in LaTeX verbatim mode
100     // or equivalent.  Pass characters through to the underlying
101     // stream
102     int rc;
103     rc = out->PutToken( nsp, (char *)0 );
104     if (!rc) err->ErrMsg( rc, "Error writing token" );
105     while (*token) {
106 	 rc = out->PutChar( *token++ );
107 	 if (rc) { err->ErrMsg( rc, "Error writing character" ); break; }
108     }
109     return rc;
110 }
111