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