1 /*
2  *	svgout
3  */
4 
5 #include <stdio.h>
6 #include "dvicom.h"
7 #include "dvistr.h"
8 #include "xmlsvg.h"
9 
10 #define GLOBAL extern
11 #define LOCAL static
12 
13 #define STRBUF 4096
14 
15 #define HORIGIN 72.27
16 #define VORIGIN 72.27
17 
18 /* from dvibase.h */
19 GLOBAL float unit_pt;
20 
conv(unsigned char * instr)21 LOCAL char *conv(unsigned char *instr) {
22 	static char unicstr[STRBUF*5];
23 	int pp = 0, ss = 0;
24 #include "svgout.h" 		/* T1toUTF8_2, T1toUTF8_3 */
25 	while(instr[pp] != '\0') {
26 		if(instr[pp] < 0x20) {
27 /*
28 			if(instr[pp] > 0x0a && instr[pp] < 0x10) {
29 				unicstr[ss] = T1toUTF8_3[instr[pp]-0x0b][0];
30 				unicstr[ss+1] = T1toUTF8_3[instr[pp]-0x0b][1];
31 				unicstr[ss+2] = T1toUTF8_3[instr[pp]-0x0b][2];
32 				ss+=3;
33 			}
34 */
35 			/*
36 			   00 01 02  03  04
37 			   ff fi fl ffi ffl
38 			      02 03
39 			*/
40 			if(instr[pp] == 0x02) {
41 				unicstr[ss] = T1toUTF8_3[1][0];
42 				unicstr[ss+1] = T1toUTF8_3[1][1];
43 				unicstr[ss+2] = T1toUTF8_3[1][2];
44 				ss+=3;
45 			}
46 			else if(instr[pp] == 0x03) {
47 				unicstr[ss] = T1toUTF8_3[2][0];
48 				unicstr[ss+1] = T1toUTF8_3[2][1];
49 				unicstr[ss+2] = T1toUTF8_3[2][2];
50 				ss+=3;
51 			}
52 			else {
53 				printf("Warning: conv()");
54 				unicstr[ss] = instr[pp];
55 				ss++;
56 			}
57 		}
58 		else if(instr[pp] < 0x80) {
59 			if(instr[pp] == '<') {
60 				unicstr[ss] = '&';
61 				unicstr[ss+1] = 'l';
62 				unicstr[ss+2] = 't';
63 				unicstr[ss+3] = ';';
64 				ss+=4;
65 			}
66 			else if(instr[pp] == '>') {
67 				unicstr[ss] = '&';
68 				unicstr[ss+1] = 'g';
69 				unicstr[ss+2] = 't';
70 				unicstr[ss+3] = ';';
71 				ss+=4;
72 			}
73 			else if(instr[pp] == '&') {
74 				unicstr[ss] = '&';
75 				unicstr[ss+1] = 'a';
76 				unicstr[ss+2] = 'm';
77 				unicstr[ss+3] = 'p';
78 				unicstr[ss+4] = ';';
79 				ss+=5;
80 			}
81 			else {
82 				unicstr[ss] = instr[pp];
83 				ss++;
84 			}
85 		}
86 		else {
87 			/*
88 			printf("\n0x%2x: 0x%2x 0x%2x", instr[pp],
89 				T1toUTF8_2[instr[pp]-0x80][0],
90 				T1toUTF8_2[instr[pp]-0x80][1]);
91 			*/
92 			unicstr[ss] = T1toUTF8_2[instr[pp]-0x80][0];
93 			unicstr[ss+1] = T1toUTF8_2[instr[pp]-0x80][1];
94 			ss+=2;
95 		}
96 		pp++;
97 	}
98 	unicstr[ss] = '\0';
99 	return (char *) unicstr;
100 }
101 
svgout(int c,int d,int e,dvi_state_struct state,int bfrchrstate,FILE * svg_out,FILE * log_out)102 int svgout(int c, int d, int e, dvi_state_struct state, int bfrchrstate,
103 	       FILE *svg_out, FILE *log_out) {
104 
105 	static unsigned char SM_state = 0;
106 	static char str[STRBUF];
107 	static int strl = 0;
108 	static int fstchrstateh, fstchrstatev;
109 
110 	switch(SM_state) {
111 		case 0:
112 			if(c == DVI_BOP) {
113 				fprintf(svg_out, svg_text_id, d);
114 				SM_state = 1;
115 			}
116 			break;
117 		case 1:
118 			if((c >= DVI_FNT_NUM_0 && c <= DVI_FNT_NUM_63)
119 			    || (c == DVI_FNT_1)) {
120 				fprintf(svg_out, svg_tspan_fnt, d);
121 				SM_state = 2;
122 			}
123 			break;
124 		case 2:
125 			if(c == DVI_EOP) {
126 				fprintf(svg_out, svg_text2);
127 				SM_state = 0;
128 			}
129 			else if((c >= DVI_SET_CHAR_0 && c <= DVI_SET_CHAR_127)
130 			    || (c == DVI_SET_1) || (c == DVI_PUT_1)) {
131 				fstchrstateh = bfrchrstate;
132 				fstchrstatev = state.v;
133 				str[strl] = d;
134 				str[strl+1] = '\0';
135 				strl++;
136 				fprintf(log_out, "SM=(%s)", str);
137 				SM_state = 3;
138 			}
139 			break;
140 		case 3:
141 			if(c == DVI_EOP) {
142 				fprintf(svg_out, svg_tspan_xy, (fstchrstateh * unit_pt) + HORIGIN, (fstchrstatev * unit_pt) + VORIGIN, conv(str));
143 				strl = 0;
144 				str[strl] = '\0';
145 				fprintf(svg_out, svg_tspan2);
146 				fprintf(svg_out, svg_text2);
147 				SM_state = 0;
148 			}
149 			else if((c >= DVI_FNT_NUM_0 && c <= DVI_FNT_NUM_63)
150 			    || (c == DVI_FNT_1)) {
151 				fprintf(svg_out, svg_tspan2);
152 				fprintf(svg_out, svg_tspan_fnt, d);
153 				SM_state = 2;
154 			}
155 			else if((c >= DVI_SET_CHAR_0 && c <= DVI_SET_CHAR_127)
156 			    || (c == DVI_SET_1) || (c == DVI_PUT_1)) {
157 				str[strl] = d;
158 				/* fprintf(log_out, "SM=[%c]", d); */
159 				str[strl+1] = '\0';
160 				strl++;
161 				fprintf(log_out, "SM=(%s)", str);
162 				SM_state = 3;
163 			}
164 			else if(c >= DVI_RIGHT_1 && c <= DVI_Z_4) {
165 				fprintf(svg_out, svg_tspan_xy, (fstchrstateh * unit_pt) + HORIGIN, (fstchrstatev * unit_pt) + VORIGIN, conv(str));
166 				strl = 0;
167 				str[strl] = '\0';
168 				SM_state = 4;
169 			}
170 			break;
171 		case 4:
172 			if(c == DVI_EOP) {
173 				fprintf(svg_out, svg_text2);
174 				SM_state = 0;
175 			}
176 			else if((c >= DVI_FNT_NUM_0 && c <= DVI_FNT_NUM_63)
177 			    || (c == DVI_FNT_1)) {
178 				fprintf(svg_out, svg_tspan2);
179 				fprintf(svg_out, svg_tspan_fnt, d);
180 				SM_state = 2;
181 			}
182 			else if((c >= DVI_SET_CHAR_0 && c <= DVI_SET_CHAR_127)
183 			    || (c == DVI_SET_1) || (c == DVI_PUT_1)) {
184 				fstchrstateh = bfrchrstate;
185 				fstchrstatev = state.v;
186 				str[strl] = d;
187 				str[strl+1] = '\0';
188 				strl++;
189 				fprintf(log_out, "SM=(%s)", str);
190 				SM_state = 3;
191 			}
192 			if(c >= DVI_RIGHT_1 && c <= DVI_Z_4)
193 				SM_state = 4;
194 			break;
195 		}
196 	fprintf(log_out, "\nSM=<%d>",SM_state);
197 	if(c == DVI_SET_RULE || c == DVI_PUT_RULE)
198 		fprintf(svg_out, svg_rect_wh, d, e);
199 }
200