]>
Commit | Line | Data |
---|---|---|
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 | |
33 | static 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 | ||
42 | TexChunk * DocumentTitle = NULL; | |
43 | TexChunk * DocumentAuthor = NULL; | |
44 | TexChunk * DocumentDate = NULL; | |
45 | ||
46 | // Header/footers/pagestyle | |
47 | TexChunk * LeftHeaderEven = NULL; | |
48 | TexChunk * LeftFooterEven = NULL; | |
49 | TexChunk * CentreHeaderEven = NULL; | |
50 | TexChunk * CentreFooterEven = NULL; | |
51 | TexChunk * RightHeaderEven = NULL; | |
52 | TexChunk * RightFooterEven = NULL; | |
53 | TexChunk * LeftHeaderOdd = NULL; | |
54 | TexChunk * LeftFooterOdd = NULL; | |
55 | TexChunk * CentreHeaderOdd = NULL; | |
56 | TexChunk * CentreFooterOdd = NULL; | |
57 | TexChunk * RightHeaderOdd = NULL; | |
58 | TexChunk * RightFooterOdd = NULL; | |
59 | wxChar * PageStyle = copystring(_T("plain")); | |
60 | ||
61 | int DocumentStyle = LATEX_REPORT; | |
62 | int MinorDocumentStyle = 0; | |
63 | wxPathList TexPathList; | |
64 | wxChar * BibliographyStyleString = copystring(_T("plain")); | |
65 | wxChar * DocumentStyleString = copystring(_T("report")); | |
66 | wxChar * MinorDocumentStyleString = NULL; | |
67 | int ParSkip = 0; | |
68 | int ParIndent = 0; | |
69 | ||
70 | int normalFont = 10; | |
71 | int smallFont = 8; | |
72 | int tinyFont = 6; | |
73 | int largeFont1 = 12; | |
74 | int LargeFont2 = 14; | |
75 | int LARGEFont3 = 18; | |
76 | int hugeFont1 = 20; | |
77 | int HugeFont2 = 24; | |
78 | int 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) | |
83 | static 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 | |
106 | int chapterFont = 12; // LARGEFont3; | |
107 | int sectionFont = 12; // LargeFont2; | |
108 | int subsectionFont = 12; // largeFont1; | |
109 | int titleFont = LARGEFont3; | |
110 | int authorFont = LargeFont2; | |
111 | int mirrorMargins = true; | |
112 | bool winHelp = false; // Output in Windows Help format if TRUE, linear otherwise | |
113 | bool isInteractive = false; | |
114 | bool runTwice = false; | |
115 | int convertMode = TEX_RTF; | |
116 | bool checkCurleyBraces = false; | |
117 | bool checkSyntax = false; | |
118 | bool headerRule = false; | |
119 | bool footerRule = false; | |
120 | bool compatibilityMode = false; // If TRUE, maximum Latex compatibility | |
121 | // (Quality of RTF generation deteriorate) | |
122 | bool generateHPJ; // Generate WinHelp Help Project file | |
123 | wxChar *winHelpTitle = NULL; // Windows Help title | |
124 | int defaultTableColumnWidth = 2000; | |
125 | ||
126 | int labelIndentTab = 18; // From left indent to item label (points) | |
127 | int itemIndentTab = 40; // From left indent to item (points) | |
128 | ||
129 | bool useUpButton = true; | |
130 | int htmlBrowseButtons = HTML_BUTTONS_TEXT; | |
131 | ||
132 | bool truncateFilenames = false; // Truncate for DOS | |
133 | int winHelpVersion = 3; // WinHelp Version (3 for Windows 3.1, 4 for Win95) | |
134 | bool winHelpContents = false; // Generate .cnt file for WinHelp 4 | |
135 | bool htmlIndex = false; // Generate .htx file for HTML | |
136 | bool htmlFrameContents = false; // Use frames for HTML contents page | |
137 | wxChar *htmlStylesheet = NULL; // Use this CSS stylesheet for HTML pages | |
138 | bool useHeadingStyles = true; // Insert \s1, s2 etc. | |
139 | bool useWord = true; // Insert proper Word table of contents, etc etc | |
140 | int contentsDepth = 4; // Depth of Word table of contents | |
141 | bool indexSubsections = true; // Index subsections in linear RTF | |
142 | // Linear RTF method of including bitmaps. Can be "includepicture", "hex" | |
143 | wxChar *bitmapMethod = copystring(_T("includepicture")); | |
144 | bool upperCaseNames = false; | |
145 | // HTML background and text colours | |
146 | wxChar *backgroundImageString = NULL; | |
147 | wxChar *backgroundColourString = copystring(_T("255;255;255")); | |
148 | wxChar *textColourString = NULL; | |
149 | wxChar *linkColourString = NULL; | |
150 | wxChar *followedLinkColourString = NULL; | |
151 | bool combineSubSections = false; | |
152 | bool htmlWorkshopFiles = false; | |
153 | bool ignoreBadRefs = false; | |
154 | wxChar *htmlFaceName = NULL; | |
155 | ||
156 | extern int passNumber; | |
157 | ||
158 | extern wxHashTable TexReferences; | |
159 | ||
160 | /* | |
161 | * International support | |
162 | */ | |
163 | ||
164 | // Names to help with internationalisation | |
165 | wxChar *ContentsNameString = copystring(_T("Contents")); | |
166 | wxChar *AbstractNameString = copystring(_T("Abstract")); | |
167 | wxChar *GlossaryNameString = copystring(_T("Glossary")); | |
168 | wxChar *ReferencesNameString = copystring(_T("References")); | |
169 | wxChar *FiguresNameString = copystring(_T("List of Figures")); | |
170 | wxChar *TablesNameString = copystring(_T("List of Tables")); | |
171 | wxChar *FigureNameString = copystring(_T("Figure")); | |
172 | wxChar *TableNameString = copystring(_T("Table")); | |
173 | wxChar *IndexNameString = copystring(_T("Index")); | |
174 | wxChar *ChapterNameString = copystring(_T("chapter")); | |
175 | wxChar *SectionNameString = copystring(_T("section")); | |
176 | wxChar *SubsectionNameString = copystring(_T("subsection")); | |
177 | wxChar *SubsubsectionNameString = copystring(_T("subsubsection")); | |
178 | wxChar *UpNameString = copystring(_T("Up")); | |
179 | ||
180 | /* | |
181 | * Section numbering | |
182 | * | |
183 | */ | |
184 | ||
185 | int chapterNo = 0; | |
186 | int sectionNo = 0; | |
187 | int subsectionNo = 0; | |
188 | int subsubsectionNo = 0; | |
189 | int figureNo = 0; | |
190 | int tableNo = 0; | |
191 | ||
192 | /* | |
193 | * Other variables | |
194 | * | |
195 | */ | |
196 | ||
197 | FILE *CurrentOutput1 = NULL; | |
198 | FILE *CurrentOutput2 = NULL; | |
199 | FILE *Inputs[15]; | |
200 | unsigned long LineNumbers[15]; | |
201 | wxChar *FileNames[15]; | |
202 | int CurrentInputIndex = 0; | |
203 | ||
204 | wxChar *TexFileRoot = NULL; | |
205 | wxChar *TexBibName = NULL; // Bibliography output file name | |
206 | wxChar *TexTmpBibName = NULL; // Temporary bibliography output file name | |
207 | bool isSync = false; // If TRUE, should not yield to other processes. | |
208 | bool stopRunning = false; // If TRUE, should abort. | |
209 | ||
210 | static int currentColumn = 0; | |
211 | wxChar *currentArgData = NULL; | |
212 | bool haveArgData = false; // If TRUE, we're simulating the data. | |
213 | TexChunk *currentArgument = NULL; | |
214 | TexChunk *nextChunk = NULL; | |
215 | bool isArgOptional = false; | |
216 | int noArgs = 0; | |
217 | ||
218 | TexChunk *TopLevel = NULL; | |
219 | // wxList MacroDefs(wxKEY_STRING); | |
220 | wxHashTable MacroDefs(wxKEY_STRING); | |
221 | wxStringList IgnorableInputFiles; // Ignorable \input files, e.g. psbox.tex | |
222 | wxChar *BigBuffer = NULL; // For reading in large chunks of text | |
223 | TexMacroDef *SoloBlockDef = NULL; | |
224 | TexMacroDef *VerbatimMacroDef = NULL; | |
225 | ||
226 | #define IncrementLineNumber() LineNumbers[CurrentInputIndex] ++ | |
227 | ||
228 | ||
229 | TexRef::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 | ||
238 | TexRef::~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 | ||
247 | CustomMacro::~CustomMacro() | |
248 | { | |
249 | if (macroName) | |
250 | delete [] macroName; | |
251 | if (macroBody) | |
252 | delete [] macroBody; | |
253 | } | |
254 | ||
255 | void 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 | ||
284 | void 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 | ||
306 | TexMacroDef *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 | ||
385 | void 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 | ||
410 | bool 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 | ||
424 | bool readingVerbatim = false; | |
425 | bool 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 | ||
431 | unsigned long leftCurley = 0; | |
432 | unsigned long rightCurley = 0; | |
433 | static wxString currentFileName = _T(""); | |
434 | ||
435 | bool 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 |