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