-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.c
794 lines (663 loc) · 18.3 KB
/
convert.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
/* convert.c - high level routines for LaTeX to RTF conversion
Copyright (C) 2002 The Free Software Foundation
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
This file is available from http://sourceforge.net/projects/latex2rtf/
TeX has six modes:
MODE_VERTICAL Building the main vertical list, from which the
pages of output are derived
MODE_INTERNAL_VERTICAL Building a vertical list from a vbox
MODE_HORIZONTAL Building a horizontal list for a paragraph
MODE_RESTICTED_HORIZONTAL Building a horizontal list for an hbox
MODE_MATH Building a mathematical formula to be placed in a
horizontal list
MODE_DISPLAYMATH Building a mathematical formula to be placed on a
line by itself, temporarily interrupting the current paragraph
LaTeX has three modes: paragraph mode, math mode, or left-to-right mode.
This is not a particularly useful, since paragraph mode is a combination of
vertical and horizontal modes.
Why bother keeping track of modes? Mostly so that paragraph indentation gets handled
correctly, as well as vertical and horizontal space.
*/
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <ctype.h>
#include "main.h"
#include "convert.h"
#include "commands.h"
#include "chars.h"
#include "funct1.h"
#include "l2r_fonts.h"
#include "stack.h"
#include "tables.h"
#include "equation.h"
#include "direct.h"
#include "ignore.h"
#include "cfg.h"
#include "encode.h"
#include "util.h"
#include "parser.h"
#include "lengths.h"
#include "counters.h"
#include "preamble.h"
static void TranslateCommand(); /* converts commands */
static int ret = 0;
static int g_TeX_mode = MODE_VERTICAL;
char TexModeName[7][25] = {"bad", "internal vertical", "horizontal",
"restricted horizontal", "math", "displaymath","vertical"};
void SetTexMode(int mode)
{
if (abs(mode) != g_TeX_mode)
diagnostics(4, "TeX mode changing from [%s] -> [%s]", TexModeName[g_TeX_mode], TexModeName[abs(mode)]);
if (mode < 0) { /* hack to allow CmdStartParagraph to set mode directly */
g_TeX_mode = -mode;
return;
}
if (g_TeX_mode == MODE_VERTICAL && mode == MODE_HORIZONTAL)
CmdStartParagraph(ANY_PAR);
if (g_TeX_mode == MODE_HORIZONTAL && mode == MODE_VERTICAL)
CmdEndParagraph(0);
g_TeX_mode = mode;
}
int GetTexMode(void)
{
return g_TeX_mode;
}
void
ConvertString(char *string)
/******************************************************************************
purpose : converts string in TeX-format to Rtf-format
******************************************************************************/
{
char temp[256];
if (string==NULL) return;
if (strlen(string)<250)
strncpy(temp,string,250);
else {
strncpy(temp,string,125);
strncpy(temp+125,"\n...\n", 5);
strncpy(temp+130,string+strlen(string)-125,125);
temp[255] = '\0';
}
if (PushSource(NULL, string) == 0) {
diagnostics(4, "Entering Convert() from StringConvert()\n******\n%s\n*****",temp);
while (StillSource())
Convert();
PopSource();
diagnostics(4, "Exiting Convert() from StringConvert()");
}
}
void
ConvertAllttString(char *s)
/******************************************************************************
purpose : converts string in TeX-format to Rtf-format
according to the alltt environment, which is like
verbatim environment except that \, {, and } have
their usual meanings
******************************************************************************/
{
char cThis;
if (s==NULL) return;
diagnostics(4, "Entering Convert() from StringAllttConvert()");
if(PushSource(NULL, s)==0) {
while (StillSource()) {
cThis = getRawTexChar(); /* it is a verbatim like environment */
switch (cThis) {
case '\\':
PushLevels();
TranslateCommand();
CleanStack();
break;
case '{':
PushBrace();
fprintRTF("{");
break;
case '}':
ret = RecursionLevel - PopBrace();
fprintRTF("}");
break;
default:
putRtfChar(cThis);
break;
}
}
PopSource();
}
diagnostics(4, "Exiting Convert() from StringAllttConvert()");
}
void
Convert()
/****************************************************************************
purpose: converts inputfile and writes result to outputfile
globals: fTex, fRtf and all global flags for convert (see above)
****************************************************************************/
{
char cThis = '\n';
char cLast = '\0';
char cNext;
int mode, count,pending_new_paragraph;
diagnostics(3, "Entering Convert ret = %d", ret);
RecursionLevel++;
PushLevels();
while ((cThis = getTexChar()) && cThis != '\0') {
if (cThis == '\n')
diagnostics(5, "Current character is '\\n' mode = %d ret = %d level = %d", GetTexMode(), ret, RecursionLevel);
else
diagnostics(5, "Current character is '%c' mode = %d ret = %d level = %d", cThis, GetTexMode(), ret, RecursionLevel);
mode = GetTexMode();
pending_new_paragraph--;
switch (cThis) {
case '\\':
PushLevels();
TranslateCommand();
CleanStack();
if (ret > 0) {
diagnostics(5, "Exiting Convert via TranslateCommand ret = %d level = %d", ret, RecursionLevel);
ret--;
RecursionLevel--;
return;
}
break;
case '{':
if (mode==MODE_VERTICAL) SetTexMode(MODE_HORIZONTAL);
CleanStack();
PushBrace();
fprintRTF("{");
break;
case '}':
CleanStack();
ret = RecursionLevel - PopBrace();
fprintRTF("}");
if (ret > 0) {
diagnostics(5, "Exiting Convert via '}' ret = %d level = %d", ret, RecursionLevel);
ret--;
RecursionLevel--;
return;
}
break;
case ' ':
if (mode==MODE_VERTICAL || mode==MODE_MATH || mode==MODE_DISPLAYMATH)
cThis = cLast;
else if ( cLast != ' ' && cLast != '\n' ) {
if (GetTexMode()==MODE_RESTRICTED_HORIZONTAL)
fprintRTF("\\~");
else
fprintRTF(" ");
}
break;
case '\n':
tabcounter = 0;
if (mode==MODE_MATH || mode==MODE_DISPLAYMATH) {
cNext = getNonBlank();
ungetTexChar(cNext);
} else {
cNext = getNonSpace();
if (cNext == '\n') { /* new paragraph ... skip all ' ' and '\n' */
pending_new_paragraph=2;
CmdEndParagraph(0);
cNext = getNonBlank();
ungetTexChar(cNext);
} else { /* add a space if needed */
ungetTexChar(cNext);
if (mode != MODE_VERTICAL && cLast != ' ')
fprintRTF(" ");
}
}
break;
case '$':
cNext = getTexChar();
diagnostics(5,"Processing $, next char <%c>",cNext);
if (cNext == '$' && GetTexMode() != MODE_MATH)
CmdEquation(EQN_DOLLAR_DOLLAR | ON);
else {
ungetTexChar(cNext);
CmdEquation(EQN_DOLLAR | ON);
}
/*
Formulas need to close all Convert() operations when they end
This works for \begin{equation} but not $$ since the BraceLevel
and environments don't get pushed properly. We do it explicitly here.
*/
/*
if (GetTexMode() == MODE_MATH || GetTexMode() == MODE_DISPLAYMATH)
PushBrace();
else {
ret = RecursionLevel - PopBrace();
if (ret > 0) {
ret--;
RecursionLevel--;
diagnostics(5, "Exiting Convert via Math ret = %d", ret);
return;
}
}
*/
break;
case '&':
if (g_processing_arrays) {
fprintRTF("%c",g_field_separator);
break;
}
if (GetTexMode() == MODE_MATH || GetTexMode() == MODE_DISPLAYMATH) { /* in eqnarray */
fprintRTF("\\tab ");
g_equation_column++;
break;
}
if (g_processing_tabular) { /* in tabular */
actCol++;
fprintRTF("\\cell\\pard\\intbl\\q%c ", colFmt[actCol]);
break;
}
fprintRTF("&");
break;
case '~':
fprintRTF("\\~");
break;
case '^':
CmdSuperscript(0);
break;
case '_':
CmdSubscript(0);
break;
case '-':
if (mode == MODE_MATH || mode == MODE_DISPLAYMATH)
fprintRTF("-");
else {
SetTexMode(MODE_HORIZONTAL);
count = getSameChar('-')+1;
if (count == 1)
fprintRTF("-");
else if (count == 2)
fprintRTF("\\endash ");
else if (count == 3)
fprintRTF("\\emdash ");
else
while (count--) fprintRTF("-");
}
break;
case '|':
if (mode == MODE_MATH || mode == MODE_DISPLAYMATH)
fprintRTF("|");
else
fprintRTF("\\emdash ");
break;
case '\'':
if (mode == MODE_MATH || mode == MODE_DISPLAYMATH)
fprintRTF("'");
else {
SetTexMode(MODE_HORIZONTAL);
count = getSameChar('\'')+1;
if (count == 2)
fprintRTF("\\rdblquote ");
else
while (count--) fprintRTF("\\rquote ");
}
break;
case '`':
SetTexMode(MODE_HORIZONTAL);
count = getSameChar('`')+1;
if (count == 2)
fprintRTF("\\ldblquote ");
else
while (count--) fprintRTF("\\lquote ");
break;
case '\"':
SetTexMode(MODE_HORIZONTAL);
if (GermanMode)
TranslateGerman();
else
fprintRTF("\"");
break;
case '<':
if (mode == MODE_VERTICAL) SetTexMode(MODE_HORIZONTAL);
if (GetTexMode() == MODE_HORIZONTAL){
cNext = getTexChar();
if (cNext == '<') {
if (FrenchMode) { /* not quite right */
skipSpaces();
cNext = getTexChar();
if (cNext == '~')
skipSpaces();
else
ungetTexChar(cNext);
fprintRTF("\\'ab\\~");
} else
fprintRTF("\\'ab");
} else {
ungetTexChar(cNext);
fprintRTF("<");
}
} else
fprintRTF("<");
break;
case '>':
if (mode == MODE_VERTICAL) SetTexMode(MODE_HORIZONTAL);
if (GetTexMode() == MODE_HORIZONTAL){
cNext = getTexChar();
if (cNext == '>')
fprintRTF("\\'bb");
else {
ungetTexChar(cNext);
fprintRTF(">");
}
} else
fprintRTF(">");
break;
case '!':
if (mode == MODE_MATH || mode == MODE_DISPLAYMATH)
fprintRTF("!");
else {
SetTexMode(MODE_HORIZONTAL);
if ((cNext = getTexChar()) && cNext == '`') {
fprintRTF("\\'a1 ");
} else {
fprintRTF("! ");
ungetTexChar(cNext);
}
}
break;
case '?':
SetTexMode(MODE_HORIZONTAL);
if ((cNext = getTexChar()) && cNext == '`') {
fprintRTF("\\'bf ");
} else {
fprintRTF("? ");
ungetTexChar(cNext);
}
break;
case ':':
if (mode == MODE_MATH || mode == MODE_DISPLAYMATH)
fprintRTF(":");
else {
SetTexMode(MODE_HORIZONTAL);
if (FrenchMode)
fprintRTF("\\~:");
else
fprintRTF(":");
}
break;
case '.':
if (mode == MODE_MATH || mode == MODE_DISPLAYMATH)
fprintRTF(".");
else {
SetTexMode(MODE_HORIZONTAL);
fprintRTF(".");
/* try to simulate double spaces after sentences */
cNext = getTexChar();
if (0 && cNext == ' ' && (isalpha((int)cLast) && !isupper((int) cLast)))
fprintRTF(" ");
ungetTexChar(cNext);
}
break;
case '\t':
diagnostics(WARNING, "This should not happen, ignoring \\t");
cThis = ' ';
break;
case '\r':
diagnostics(WARNING, "This should not happen, ignoring \\r");
cThis = ' ';
break;
case '%':
diagnostics(WARNING, "This should not happen, ignoring %%");
cThis = ' ';
break;
case '(':
if (g_processing_fields&&g_escape_parent)
fprintRTF("\\\\(");
else
fprintRTF("(");
break;
case ')':
if (g_processing_fields&&g_escape_parent)
fprintRTF("\\\\)");
else
fprintRTF(")");
break;
case ';':
if (g_field_separator == ';' && g_processing_fields)
fprintRTF("\\\\;");
else
if (FrenchMode)
fprintRTF("\\~;");
else
fprintRTF(";");
break;
case ',':
if (g_field_separator == ',' && g_processing_fields)
fprintRTF("\\\\,");
else
fprintRTF(",");
break;
default:
if (mode == MODE_MATH || mode == MODE_DISPLAYMATH) {
if (('a' <= cThis && cThis <= 'z') || ('A' <= cThis && cThis <= 'Z'))
fprintRTF("{\\i %c}", cThis);
else
fprintRTF("%c", cThis);
} else {
SetTexMode(MODE_HORIZONTAL);
fprintRTF("%c", cThis);
}
break;
}
tabcounter++;
cLast = cThis;
}
RecursionLevel--;
diagnostics(5, "Exiting Convert via exhaustion ret = %d", ret);
}
void
TranslateCommand()
/****************************************************************************
purpose: The function is called on a backslash in input file and
tries to call the command-function for the following command.
returns: success or not
globals: fTex, fRtf, command-functions have side effects or recursive calls;
global flags for convert
****************************************************************************/
{
char cCommand[MAXCOMMANDLEN];
int i,mode;
int cThis;
cThis = getTexChar();
mode = GetTexMode();
diagnostics(5, "Beginning TranslateCommand() \\%c", cThis);
switch (cThis) {
case '}':
if (mode == MODE_VERTICAL) SetTexMode(MODE_HORIZONTAL);
fprintRTF("\\}");
return;
case '{':
if (mode == MODE_VERTICAL) SetTexMode(MODE_HORIZONTAL);
fprintRTF("\\{");
return;
case '#':
if (mode == MODE_VERTICAL) SetTexMode(MODE_HORIZONTAL);
fprintRTF("#");
return;
case '$':
if (mode == MODE_VERTICAL) SetTexMode(MODE_HORIZONTAL);
fprintRTF("$");
return;
case '&':
if (mode == MODE_VERTICAL) SetTexMode(MODE_HORIZONTAL);
fprintRTF("&");
return;
case '%':
if (mode == MODE_VERTICAL) SetTexMode(MODE_HORIZONTAL);
fprintRTF("%%");
return;
case '_':
if (mode == MODE_VERTICAL) SetTexMode(MODE_HORIZONTAL);
fprintRTF("_");
return;
case '\\': /* \\[1mm] or \\*[1mm] possible */
CmdSlashSlash(0);
return;
case ' ':
case '\n':
if (mode == MODE_VERTICAL) SetTexMode(MODE_HORIZONTAL);
fprintRTF(" "); /* ordinary interword space */
skipSpaces();
return;
/* \= \> \< \+ \- \' \` all have different meanings in a tabbing environment */
case '-':
if (mode == MODE_VERTICAL) SetTexMode(MODE_HORIZONTAL);
if (g_processing_tabbing){
(void) PopBrace();
PushBrace();
} else
fprintRTF("\\-");
return;
case '+':
if (mode == MODE_VERTICAL) SetTexMode(MODE_HORIZONTAL);
if (g_processing_tabbing){
(void) PopBrace();
PushBrace();
}
return;
case '<':
if (mode == MODE_VERTICAL) SetTexMode(MODE_HORIZONTAL);
if (g_processing_tabbing){
(void) PopBrace();
PushBrace();
}
return;
case '>':
if (mode == MODE_VERTICAL) SetTexMode(MODE_HORIZONTAL);
if (g_processing_tabbing){
(void) PopBrace();
CmdTabjump();
PushBrace();
} else
CmdSpace(0.50); /* medium space */
return;
case '`':
if (mode == MODE_VERTICAL) SetTexMode(MODE_HORIZONTAL);
if (g_processing_tabbing){
(void) PopBrace();
PushBrace();
} else
CmdLApostrophChar(0);
return;
case '\'':
if (mode == MODE_VERTICAL) SetTexMode(MODE_HORIZONTAL);
if (g_processing_tabbing){
(void) PopBrace();
PushBrace();
return;
} else
CmdRApostrophChar(0); /* char ' =?= \' */
return;
case '=':
if (mode == MODE_VERTICAL) SetTexMode(MODE_HORIZONTAL);
if (g_processing_tabbing){
(void) PopBrace();
CmdTabset();
PushBrace();
}
else
CmdMacronChar(0);
return;
case '~':
if (mode == MODE_VERTICAL) SetTexMode(MODE_HORIZONTAL);
CmdTildeChar(0);
return;
case '^':
if (mode == MODE_VERTICAL) SetTexMode(MODE_HORIZONTAL);
CmdHatChar(0);
return;
case '.':
if (mode == MODE_VERTICAL) SetTexMode(MODE_HORIZONTAL);
CmdDotChar(0);
return;
case '\"':
if (mode == MODE_VERTICAL) SetTexMode(MODE_HORIZONTAL);
CmdUmlauteChar(0);
return;
case '(':
CmdEquation(EQN_RND_OPEN | ON);
/*PushBrace();*/
return;
case '[':
CmdEquation(EQN_BRACKET_OPEN | ON);
/*PushBrace();*/
return;
case ')':
CmdEquation(EQN_RND_CLOSE | OFF);
/*ret = RecursionLevel - PopBrace();*/
return;
case ']':
CmdEquation(EQN_BRACKET_CLOSE | OFF);
/*ret = RecursionLevel - PopBrace();*/
return;
case '/':
CmdIgnore(0); /* italic correction */
return;
case '!':
CmdIgnore(0); /* \! negative thin space */
return;
case ',':
if (mode == MODE_VERTICAL) SetTexMode(MODE_HORIZONTAL);
CmdSpace(0.33); /* \, produces a small space */
return;
case ';':
if (mode == MODE_VERTICAL) SetTexMode(MODE_HORIZONTAL);
CmdSpace(0.75); /* \; produces a thick space */
return;
case '@':
CmdIgnore(0); /* \@ produces an "end of sentence" space */
return;
case '3':
if (mode == MODE_VERTICAL) SetTexMode(MODE_HORIZONTAL);
fprintRTF("{\\'df}"); /* german symbol 'á' */
return;
}
/* LEG180498 Commands consist of letters and can have an optional * at the end */
for (i = 0; i < MAXCOMMANDLEN; i++) {
if (!isalpha((int)cThis) && (cThis != '*')) {
bool found_nl = FALSE;
if (cThis == '%'){ /* put the % back and get the next char */
ungetTexChar('%');
cThis=getTexChar();
}
/* all spaces after commands are ignored, a single \n may occur */
while (cThis == ' ' || (cThis == '\n' && !found_nl)) {
if (cThis == '\n')
found_nl = TRUE;
cThis = getTexChar();
}
ungetTexChar(cThis); /* put back first non-space char after command */
break; /* done skipping spaces */
} else
cCommand[i] = cThis;
cThis = getRawTexChar(); /* Necessary because % ends a command */
}
cCommand[i] = '\0'; /* mark end of string with zero */
diagnostics(5, "TranslateCommand() <%s>", cCommand);
if (i == 0)
return;
if (strcmp(cCommand,"begin")==0){
fprintRTF("{");
PushBrace();
}
if (CallCommandFunc(cCommand)){ /* call handling function for command */
if (strcmp(cCommand,"end")==0) {
ret = RecursionLevel - PopBrace();
fprintRTF("}");
}
return;
}
if (TryDirectConvert(cCommand)) return;
if (TryVariableIgnore(cCommand)) return;
diagnostics(WARNING, "Command \\%s not found - ignored", cCommand);
}