]> git.saurik.com Git - wxWidgets.git/blame - utils/tex2rtf/src/tex2any.cpp
Move [GS]etDefaultItem up to wxWindow
[wxWidgets.git] / utils / tex2rtf / src / tex2any.cpp
CommitLineData
9a29912f
JS
1/////////////////////////////////////////////////////////////////////////////
2// Name: tex2any.cpp
3// Purpose: Utilities for Latex conversion.
4// Author: Julian Smart
5// Modified by:
6// Created: 01/01/99
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation
14#endif
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
20#pragma hdrstop
21#endif
22
23#ifndef WX_PRECOMP
24#include "wx/wx.h"
25#endif
26
27#include <ctype.h>
28#include "tex2any.h"
29#include <stdlib.h>
30#include <time.h>
31
32/*
33 * Variables accessible from clients
34 *
35 */
36
37TexChunk * DocumentTitle = NULL;
38TexChunk * DocumentAuthor = NULL;
39TexChunk * DocumentDate = NULL;
40
41// Header/footers/pagestyle
42TexChunk * LeftHeaderEven = NULL;
43TexChunk * LeftFooterEven = NULL;
44TexChunk * CentreHeaderEven = NULL;
45TexChunk * CentreFooterEven = NULL;
46TexChunk * RightHeaderEven = NULL;
47TexChunk * RightFooterEven = NULL;
48TexChunk * LeftHeaderOdd = NULL;
49TexChunk * LeftFooterOdd = NULL;
50TexChunk * CentreHeaderOdd = NULL;
51TexChunk * CentreFooterOdd = NULL;
52TexChunk * RightHeaderOdd = NULL;
53TexChunk * RightFooterOdd = NULL;
54char * PageStyle = copystring("plain");
55
56int DocumentStyle = LATEX_REPORT;
57int MinorDocumentStyle = 0;
58wxPathList TexPathList;
59char * BibliographyStyleString = copystring("plain");
60char * DocumentStyleString = copystring("report");
61char * MinorDocumentStyleString = NULL;
62int ParSkip = 0;
63int ParIndent = 0;
64
65int normalFont = 10;
66int smallFont = 8;
67int tinyFont = 6;
68int largeFont1 = 12;
69int LargeFont2 = 14;
70int LARGEFont3 = 18;
71int hugeFont1 = 20;
72int HugeFont2 = 24;
73int HUGEFont3 = 28;
74
fad535ee
GT
75// All of these tokens MUST be found on a line by themselves (no other
76// text) and must start at the first character of the line, or tex2rtf
77// will fail to process them correctly (a limitation of tex2rtf, not TeX)
7cbe4e79 78static const wxString syntaxTokens[] =
fad535ee
GT
79{ "\\begin{verbatim}",
80 "\\begin{toocomplex}",
81 "\\end{verbatim}",
82 "\\end{toocomplex}",
83 "\\verb",
84 "\\begin{comment}",
85 "\\end{comment}",
86 "\\verbatiminput",
341479ff 87// "\\par",
fad535ee
GT
88 "\\input",
89 "\\helpinput",
90 "\\include",
91 wxEmptyString
92};
93
94
9a29912f
JS
95/*
96 * USER-ADJUSTABLE SETTINGS
97 *
98 */
99
100// Section font sizes
101int chapterFont = 12; // LARGEFont3;
102int sectionFont = 12; // LargeFont2;
103int subsectionFont = 12; // largeFont1;
104int titleFont = LARGEFont3;
105int authorFont = LargeFont2;
106int mirrorMargins = TRUE;
107bool winHelp = FALSE; // Output in Windows Help format if TRUE, linear otherwise
108bool isInteractive = FALSE;
109bool runTwice = FALSE;
110int convertMode = TEX_RTF;
fad535ee
GT
111bool checkCurleyBraces = FALSE;
112bool checkSyntax = FALSE;
9a29912f
JS
113bool headerRule = FALSE;
114bool footerRule = FALSE;
115bool compatibilityMode = FALSE; // If TRUE, maximum Latex compatibility
116 // (Quality of RTF generation deteriorate)
117bool generateHPJ; // Generate WinHelp Help Project file
118char *winHelpTitle = NULL; // Windows Help title
119int defaultTableColumnWidth = 2000;
120
121int labelIndentTab = 18; // From left indent to item label (points)
122int itemIndentTab = 40; // From left indent to item (points)
123
124bool useUpButton = TRUE;
125int htmlBrowseButtons = HTML_BUTTONS_TEXT;
126
127bool truncateFilenames = FALSE; // Truncate for DOS
128int winHelpVersion = 3; // WinHelp Version (3 for Windows 3.1, 4 for Win95)
129bool winHelpContents = FALSE; // Generate .cnt file for WinHelp 4
130bool htmlIndex = FALSE; // Generate .htx file for HTML
131bool htmlFrameContents = FALSE; // Use frames for HTML contents page
132bool useHeadingStyles = TRUE; // Insert \s1, s2 etc.
133bool useWord = TRUE; // Insert proper Word table of contents, etc etc
134int contentsDepth = 4; // Depth of Word table of contents
135bool indexSubsections = TRUE; // Index subsections in linear RTF
136// Linear RTF method of including bitmaps. Can be "includepicture", "hex"
137char *bitmapMethod = copystring("includepicture");
138bool upperCaseNames = FALSE;
139// HTML background and text colours
140char *backgroundImageString = NULL;
141char *backgroundColourString = copystring("255;255;255");
142char *textColourString = NULL;
143char *linkColourString = NULL;
144char *followedLinkColourString = NULL;
145bool combineSubSections = FALSE;
14204c7a 146bool htmlWorkshopFiles = FALSE;
9a29912f 147
3924dd22
GT
148extern int passNumber;
149
150extern wxHashTable TexReferences;
151
9a29912f
JS
152/*
153 * International support
154 */
155
156// Names to help with internationalisation
157char *ContentsNameString = copystring("Contents");
158char *AbstractNameString = copystring("Abstract");
159char *GlossaryNameString = copystring("Glossary");
160char *ReferencesNameString = copystring("References");
161char *FiguresNameString = copystring("List of Figures");
162char *TablesNameString = copystring("List of Tables");
163char *FigureNameString = copystring("Figure");
164char *TableNameString = copystring("Table");
165char *IndexNameString = copystring("Index");
166char *ChapterNameString = copystring("chapter");
167char *SectionNameString = copystring("section");
168char *SubsectionNameString = copystring("subsection");
169char *SubsubsectionNameString = copystring("subsubsection");
170char *UpNameString = copystring("Up");
171
172/*
173 * Section numbering
174 *
175 */
176
177int chapterNo = 0;
178int sectionNo = 0;
179int subsectionNo = 0;
180int subsubsectionNo = 0;
181int figureNo = 0;
182int tableNo = 0;
183
184/*
185 * Other variables
186 *
187 */
188
189FILE *CurrentOutput1 = NULL;
190FILE *CurrentOutput2 = NULL;
191FILE *Inputs[15];
192int LineNumbers[15];
193char *FileNames[15];
194int CurrentInputIndex = 0;
195
196char *TexFileRoot = NULL;
197char *TexBibName = NULL; // Bibliography output file name
198char *TexTmpBibName = NULL; // Temporary bibliography output file name
199bool isSync = FALSE; // If TRUE, should not yield to other processes.
200bool stopRunning = FALSE; // If TRUE, should abort.
201
202static int currentColumn = 0;
203char *currentArgData = NULL;
204bool haveArgData = FALSE; // If TRUE, we're simulating the data.
205TexChunk *currentArgument = NULL;
206TexChunk *nextChunk = NULL;
207bool isArgOptional = FALSE;
446dd881 208int noArgs = 0;
9a29912f
JS
209
210TexChunk *TopLevel = NULL;
211// wxList MacroDefs(wxKEY_STRING);
212wxHashTable MacroDefs(wxKEY_STRING);
213wxStringList IgnorableInputFiles; // Ignorable \input files, e.g. psbox.tex
214char *BigBuffer = NULL; // For reading in large chunks of text
215TexMacroDef *SoloBlockDef = NULL;
216TexMacroDef *VerbatimMacroDef = NULL;
217
218#define IncrementLineNumber() LineNumbers[CurrentInputIndex] ++
219
3924dd22 220
bbd08c6a
GD
221TexRef::TexRef(const char *label, const char *file,
222 const char *section, const char *sectionN)
3924dd22
GT
223{
224 refLabel = copystring(label);
225 refFile = file ? copystring(file) : (char*) NULL;
226 sectionNumber = section ? copystring(section) : copystring("??");
227 sectionName = sectionN ? copystring(sectionN) : copystring("??");
228}
229
230TexRef::~TexRef(void)
231{
232 delete [] refLabel; refLabel = NULL;
233 delete [] refFile; refFile = NULL;
234 delete [] sectionNumber; sectionNumber = NULL;
235 delete [] sectionName; sectionName = NULL;
236}
237
238
239CustomMacro::~CustomMacro()
240{
241 if (macroName)
242 delete [] macroName;
243 if (macroBody)
244 delete [] macroBody;
245}
246
e4a22366 247void TexOutput(const char *s, bool ordinaryText)
9a29912f
JS
248{
249 int len = strlen(s);
250
251 // Update current column, but only if we're guaranteed to
252 // be ordinary text (not mark-up stuff)
253 int i;
254 if (ordinaryText)
255 for (i = 0; i < len; i++)
256 {
257 if (s[i] == 13 || s[i] == 10)
258 currentColumn = 0;
259 else
260 currentColumn ++;
261 }
262
263 if (CurrentOutput1)
264 fprintf(CurrentOutput1, "%s", s);
265 if (CurrentOutput2)
266 fprintf(CurrentOutput2, "%s", s);
267}
268
269/*
270 * Try to find a Latex macro, in one of the following forms:
271 * (1) \begin{} ... \end{}
272 * (2) \macroname{arg1}...{argn}
273 * (3) {\bf arg1}
274 */
275
276void ForbidWarning(TexMacroDef *def)
277{
04b9c5bb 278 wxString informBuf;
9a29912f
JS
279 switch (def->forbidden)
280 {
281 case FORBID_WARN:
282 {
04b9c5bb
GT
283 informBuf.Printf("Warning: it is recommended that command %s is not used.", def->name);
284 OnInform((char *)informBuf.c_str());
9a29912f
JS
285 break;
286 }
287 case FORBID_ABSOLUTELY:
288 {
04b9c5bb
GT
289 informBuf.Printf("Error: command %s cannot be used and will lead to errors.", def->name);
290 OnInform((char *)informBuf.c_str());
9a29912f
JS
291 break;
292 }
293 default:
294 break;
295 }
296}
297
298TexMacroDef *MatchMacro(char *buffer, int *pos, char **env, bool *parseToBrace)
299{
300 *parseToBrace = TRUE;
301 int i = (*pos);
302 TexMacroDef *def = NULL;
303 char macroBuf[40];
304
305 // First, try to find begin{thing}
306 if (strncmp(buffer+i, "begin{", 6) == 0)
307 {
308 i += 6;
309
310 int j = i;
311 while ((isalpha(buffer[j]) || buffer[j] == '*') && ((j - i) < 39))
312 {
313 macroBuf[j-i] = buffer[j];
314 j ++;
315 }
316 macroBuf[j-i] = 0;
317 def = (TexMacroDef *)MacroDefs.Get(macroBuf);
318
319 if (def)
320 {
321 *pos = j + 1; // BUGBUG Should this be + 1???
322 *env = def->name;
323 ForbidWarning(def);
324 return def;
325 }
326 else return NULL;
327 }
328
329 // Failed, so try to find macro from definition list
330 int j = i;
331
332 // First try getting a one-character macro, but ONLY
333 // if these TWO characters are not both alphabetical (could
334 // be a longer macro)
335 if (!(isalpha(buffer[i]) && isalpha(buffer[i+1])))
336 {
337 macroBuf[0] = buffer[i];
338 macroBuf[1] = 0;
339
340 def = (TexMacroDef *)MacroDefs.Get(macroBuf);
341 if (def) j ++;
342 }
343
344 if (!def)
345 {
346 while ((isalpha(buffer[j]) || buffer[j] == '*') && ((j - i) < 39))
347 {
348 macroBuf[j-i] = buffer[j];
349 j ++;
350 }
351 macroBuf[j-i] = 0;
352 def = (TexMacroDef *)MacroDefs.Get(macroBuf);
353 }
354
355 if (def)
356 {
357 i = j;
358
359 // We want to check whether this is a space-consuming macro
360 // (e.g. {\bf word})
361 // No brace, e.g. \input thing.tex instead of \input{thing};
362 // or a numeric argument, such as \parindent0pt
363 if ((def->no_args > 0) && ((buffer[i] == 32) || (buffer[i] == '=') || (isdigit(buffer[i]))))
364 {
365 if ((buffer[i] == 32) || (buffer[i] == '='))
366 i ++;
367
368 *parseToBrace = FALSE;
369 }
370 *pos = i;
371 ForbidWarning(def);
372 return def;
373 }
374 return NULL;
375}
376
377void EatWhiteSpace(char *buffer, int *pos)
378{
379 int len = strlen(buffer);
380 int j = *pos;
381 bool keepGoing = TRUE;
382 bool moreLines = TRUE;
383 while ((j < len) && keepGoing &&
384 (buffer[j] == 10 || buffer[j] == 13 || buffer[j] == ' ' || buffer[j] == 9))
385 {
386 j ++;
387 if (j >= len)
388 {
389 if (moreLines)
390 {
391 moreLines = read_a_line(buffer);
392 len = strlen(buffer);
393 j = 0;
394 }
395 else
396 keepGoing = FALSE;
397 }
398 }
399 *pos = j;
400}
401
402bool FindEndEnvironment(char *buffer, int *pos, char *env)
403{
404 int i = (*pos);
405
406 // Try to find end{thing}
407 if ((strncmp(buffer+i, "end{", 4) == 0) &&
408 (strncmp(buffer+i+4, env, strlen(env)) == 0))
409 {
410 *pos = i + 5 + strlen(env);
411 return TRUE;
412 }
413 else return FALSE;
414}
415
416bool readingVerbatim = FALSE;
417bool readInVerbatim = FALSE; // Within a verbatim, but not nec. verbatiminput
418
f6bcfd97
BP
419// Switched this off because e.g. \verb${$ causes it to fail. There is no
420// detection of \verb yet.
fad535ee 421// #define CHECK_BRACES 1
f6bcfd97 422
341479ff
GT
423unsigned long leftCurley = 0;
424unsigned long rightCurley = 0;
f6bcfd97
BP
425static wxString currentFileName = "";
426
9a29912f
JS
427bool read_a_line(char *buf)
428{
429 if (CurrentInputIndex < 0)
430 {
431 buf[0] = 0;
432 return FALSE;
433 }
fad535ee 434
9a29912f 435 int ch = -2;
0e6ca394 436 int bufIndex = 0;
9a29912f 437 buf[0] = 0;
f6bcfd97 438
9a29912f
JS
439 while (ch != EOF && ch != 10)
440 {
0e6ca394
GT
441 if (bufIndex >= MAX_LINE_BUFFER_SIZE)
442 {
443 wxString errBuf;
444 errBuf.Printf("Line %lu of file %s is too long. Lines can be no longer than %lu characters. Truncated.",
445 LineNumbers[CurrentInputIndex], (const char*) currentFileName.c_str(),MAX_LINE_BUFFER_SIZE);
446 OnError((char *)errBuf.c_str());
447 return FALSE;
448 }
449
450 if (((bufIndex == 14) && (strncmp(buf, "\\end{verbatim}", 14) == 0)) ||
451 ((bufIndex == 16) && (strncmp(buf, "\\end{toocomplex}", 16) == 0)))
9a29912f
JS
452 readInVerbatim = FALSE;
453
454 ch = getc(Inputs[CurrentInputIndex]);
f6bcfd97 455
fad535ee 456 if (checkCurleyBraces)
f6bcfd97 457 {
fad535ee 458 if (ch == '{' && !readInVerbatim)
341479ff 459 leftCurley++;
fad535ee
GT
460 if (ch == '}' && !readInVerbatim)
461 {
341479ff
GT
462 rightCurley++;
463 if (rightCurley > leftCurley)
fad535ee
GT
464 {
465 wxString errBuf;
341479ff 466 errBuf.Printf("An extra right Curley brace ('}') was detected at line %lu inside file %s",LineNumbers[CurrentInputIndex], (const char*) currentFileName.c_str());
fad535ee
GT
467 OnError((char *)errBuf.c_str());
468
341479ff 469 // Reduce the count of right Curley braces, so the mismatched count
fad535ee 470 // isn't reported on every line that has a '}' after the first mismatch
341479ff 471 rightCurley--;
fad535ee
GT
472 }
473 }
f6bcfd97 474 }
f6bcfd97 475
9a29912f
JS
476 if (ch != EOF)
477 {
478 // Check for 2 consecutive newlines and replace with \par
479 if (ch == 10 && !readInVerbatim)
480 {
481 int ch1 = getc(Inputs[CurrentInputIndex]);
482 if ((ch1 == 10) || (ch1 == 13))
483 {
484 // Eliminate newline (10) following DOS linefeed
0e6ca394
GT
485 if (ch1 == 13)
486 ch1 = getc(Inputs[CurrentInputIndex]);
487 buf[bufIndex] = 0;
9a29912f
JS
488 IncrementLineNumber();
489// strcat(buf, "\\par\n");
490// i += 6;
0e6ca394
GT
491 if (bufIndex+5 >= MAX_LINE_BUFFER_SIZE)
492 {
493 wxString errBuf;
494 errBuf.Printf("Line %lu of file %s is too long. Lines can be no longer than %lu characters. Truncated.",
495 LineNumbers[CurrentInputIndex], (const char*) currentFileName.c_str(),MAX_LINE_BUFFER_SIZE);
496 OnError((char *)errBuf.c_str());
497 return FALSE;
498 }
9a29912f 499 strcat(buf, "\\par");
0e6ca394
GT
500 bufIndex += 5;
501
9a29912f
JS
502 }
503 else
504 {
505 ungetc(ch1, Inputs[CurrentInputIndex]);
0e6ca394
GT
506 if (bufIndex >= MAX_LINE_BUFFER_SIZE)
507 {
508 wxString errBuf;
509 errBuf.Printf("Line %lu of file %s is too long. Lines can be no longer than %lu characters. Truncated.",
510 LineNumbers[CurrentInputIndex], (const char*) currentFileName.c_str(),MAX_LINE_BUFFER_SIZE);
511 OnError((char *)errBuf.c_str());
512 return FALSE;
513 }
514
515 buf[bufIndex] = ch;
516 bufIndex ++;
9a29912f
JS
517 }
518 }
519 else
520 {
521
522 // Convert embedded characters to RTF equivalents
fad535ee
GT
523 switch(ch)
524 {
525