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