]> git.saurik.com Git - wxWidgets.git/blame - utils/tex2rtf/src/tex2any.cpp
Some .tex tweaks; pngdemo fix.
[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
75/*
76 * USER-ADJUSTABLE SETTINGS
77 *
78 */
79
80// Section font sizes
81int chapterFont = 12; // LARGEFont3;
82int sectionFont = 12; // LargeFont2;
83int subsectionFont = 12; // largeFont1;
84int titleFont = LARGEFont3;
85int authorFont = LargeFont2;
86int mirrorMargins = TRUE;
87bool winHelp = FALSE; // Output in Windows Help format if TRUE, linear otherwise
88bool isInteractive = FALSE;
89bool runTwice = FALSE;
90int convertMode = TEX_RTF;
91bool headerRule = FALSE;
92bool footerRule = FALSE;
93bool compatibilityMode = FALSE; // If TRUE, maximum Latex compatibility
94 // (Quality of RTF generation deteriorate)
95bool generateHPJ; // Generate WinHelp Help Project file
96char *winHelpTitle = NULL; // Windows Help title
97int defaultTableColumnWidth = 2000;
98
99int labelIndentTab = 18; // From left indent to item label (points)
100int itemIndentTab = 40; // From left indent to item (points)
101
102bool useUpButton = TRUE;
103int htmlBrowseButtons = HTML_BUTTONS_TEXT;
104
105bool truncateFilenames = FALSE; // Truncate for DOS
106int winHelpVersion = 3; // WinHelp Version (3 for Windows 3.1, 4 for Win95)
107bool winHelpContents = FALSE; // Generate .cnt file for WinHelp 4
108bool htmlIndex = FALSE; // Generate .htx file for HTML
109bool htmlFrameContents = FALSE; // Use frames for HTML contents page
110bool useHeadingStyles = TRUE; // Insert \s1, s2 etc.
111bool useWord = TRUE; // Insert proper Word table of contents, etc etc
112int contentsDepth = 4; // Depth of Word table of contents
113bool indexSubsections = TRUE; // Index subsections in linear RTF
114// Linear RTF method of including bitmaps. Can be "includepicture", "hex"
115char *bitmapMethod = copystring("includepicture");
116bool upperCaseNames = FALSE;
117// HTML background and text colours
118char *backgroundImageString = NULL;
119char *backgroundColourString = copystring("255;255;255");
120char *textColourString = NULL;
121char *linkColourString = NULL;
122char *followedLinkColourString = NULL;
123bool combineSubSections = FALSE;
124
125/*
126 * International support
127 */
128
129// Names to help with internationalisation
130char *ContentsNameString = copystring("Contents");
131char *AbstractNameString = copystring("Abstract");
132char *GlossaryNameString = copystring("Glossary");
133char *ReferencesNameString = copystring("References");
134char *FiguresNameString = copystring("List of Figures");
135char *TablesNameString = copystring("List of Tables");
136char *FigureNameString = copystring("Figure");
137char *TableNameString = copystring("Table");
138char *IndexNameString = copystring("Index");
139char *ChapterNameString = copystring("chapter");
140char *SectionNameString = copystring("section");
141char *SubsectionNameString = copystring("subsection");
142char *SubsubsectionNameString = copystring("subsubsection");
143char *UpNameString = copystring("Up");
144
145/*
146 * Section numbering
147 *
148 */
149
150int chapterNo = 0;
151int sectionNo = 0;
152int subsectionNo = 0;
153int subsubsectionNo = 0;
154int figureNo = 0;
155int tableNo = 0;
156
157/*
158 * Other variables
159 *
160 */
161
162FILE *CurrentOutput1 = NULL;
163FILE *CurrentOutput2 = NULL;
164FILE *Inputs[15];
165int LineNumbers[15];
166char *FileNames[15];
167int CurrentInputIndex = 0;
168
169char *TexFileRoot = NULL;
170char *TexBibName = NULL; // Bibliography output file name
171char *TexTmpBibName = NULL; // Temporary bibliography output file name
172bool isSync = FALSE; // If TRUE, should not yield to other processes.
173bool stopRunning = FALSE; // If TRUE, should abort.
174
175static int currentColumn = 0;
176char *currentArgData = NULL;
177bool haveArgData = FALSE; // If TRUE, we're simulating the data.
178TexChunk *currentArgument = NULL;
179TexChunk *nextChunk = NULL;
180bool isArgOptional = FALSE;
181bool noArgs = 0;
182
183TexChunk *TopLevel = NULL;
184// wxList MacroDefs(wxKEY_STRING);
185wxHashTable MacroDefs(wxKEY_STRING);
186wxStringList IgnorableInputFiles; // Ignorable \input files, e.g. psbox.tex
187char *BigBuffer = NULL; // For reading in large chunks of text
188TexMacroDef *SoloBlockDef = NULL;
189TexMacroDef *VerbatimMacroDef = NULL;
190
191#define IncrementLineNumber() LineNumbers[CurrentInputIndex] ++
192
193void TexOutput(char *s, bool ordinaryText)
194{
195 int len = strlen(s);
196
197 // Update current column, but only if we're guaranteed to
198 // be ordinary text (not mark-up stuff)
199 int i;
200 if (ordinaryText)
201 for (i = 0; i < len; i++)
202 {
203 if (s[i] == 13 || s[i] == 10)
204 currentColumn = 0;
205 else
206 currentColumn ++;
207 }
208
209 if (CurrentOutput1)
210 fprintf(CurrentOutput1, "%s", s);
211 if (CurrentOutput2)
212 fprintf(CurrentOutput2, "%s", s);
213}
214
215/*
216 * Try to find a Latex macro, in one of the following forms:
217 * (1) \begin{} ... \end{}
218 * (2) \macroname{arg1}...{argn}
219 * (3) {\bf arg1}
220 */
221
222void ForbidWarning(TexMacroDef *def)
223{
224 char buf[100];
225 switch (def->forbidden)
226 {
227 case FORBID_WARN:
228 {
229 sprintf(buf, "Warning: it is recommended that command %s is not used.", def->name);
230 OnInform(buf);
231 break;
232 }
233 case FORBID_ABSOLUTELY:
234 {
235 sprintf(buf, "Error: command %s cannot be used and will lead to errors.", def->name);
236 OnInform(buf);
237 break;
238 }
239 default:
240 break;
241 }
242}
243
244TexMacroDef *MatchMacro(char *buffer, int *pos, char **env, bool *parseToBrace)
245{
246 *parseToBrace = TRUE;
247 int i = (*pos);
248 TexMacroDef *def = NULL;
249 char macroBuf[40];
250
251 // First, try to find begin{thing}
252 if (strncmp(buffer+i, "begin{", 6) == 0)
253 {
254 i += 6;
255
256 int j = i;
257 while ((isalpha(buffer[j]) || buffer[j] == '*') && ((j - i) < 39))
258 {
259 macroBuf[j-i] = buffer[j];
260 j ++;
261 }
262 macroBuf[j-i] = 0;
263 def = (TexMacroDef *)MacroDefs.Get(macroBuf);
264
265 if (def)
266 {
267 *pos = j + 1; // BUGBUG Should this be + 1???
268 *env = def->name;
269 ForbidWarning(def);
270 return def;
271 }
272 else return NULL;
273 }
274
275 // Failed, so try to find macro from definition list
276 int j = i;
277
278 // First try getting a one-character macro, but ONLY
279 // if these TWO characters are not both alphabetical (could
280 // be a longer macro)
281 if (!(isalpha(buffer[i]) && isalpha(buffer[i+1])))
282 {
283 macroBuf[0] = buffer[i];
284 macroBuf[1] = 0;
285
286 def = (TexMacroDef *)MacroDefs.Get(macroBuf);
287 if (def) j ++;
288 }
289
290 if (!def)
291 {
292 while ((isalpha(buffer[j]) || buffer[j] == '*') && ((j - i) < 39))
293 {
294 macroBuf[j-i] = buffer[j];
295 j ++;
296 }
297 macroBuf[j-i] = 0;
298 def = (TexMacroDef *)MacroDefs.Get(macroBuf);
299 }
300
301 if (def)
302 {
303 i = j;
304
305 // We want to check whether this is a space-consuming macro
306 // (e.g. {\bf word})
307 // No brace, e.g. \input thing.tex instead of \input{thing};
308 // or a numeric argument, such as \parindent0pt
309 if ((def->no_args > 0) && ((buffer[i] == 32) || (buffer[i] == '=') || (isdigit(buffer[i]))))
310 {
311 if ((buffer[i] == 32) || (buffer[i] == '='))
312 i ++;
313
314 *parseToBrace = FALSE;
315 }
316 *pos = i;
317 ForbidWarning(def);
318 return def;
319 }
320 return NULL;
321}
322
323void EatWhiteSpace(char *buffer, int *pos)
324{
325 int len = strlen(buffer);
326 int j = *pos;
327 bool keepGoing = TRUE;
328 bool moreLines = TRUE;
329 while ((j < len) && keepGoing &&
330 (buffer[j] == 10 || buffer[j] == 13 || buffer[j] == ' ' || buffer[j] == 9))
331 {
332 j ++;
333 if (j >= len)
334 {
335 if (moreLines)
336 {
337 moreLines = read_a_line(buffer);
338 len = strlen(buffer);
339 j = 0;
340 }
341 else
342 keepGoing = FALSE;
343 }
344 }
345 *pos = j;
346}
347
348bool FindEndEnvironment(char *buffer, int *pos, char *env)
349{
350 int i = (*pos);
351
352 // Try to find end{thing}
353 if ((strncmp(buffer+i, "end{", 4) == 0) &&
354 (strncmp(buffer+i+4, env, strlen(env)) == 0))
355 {
356 *pos = i + 5 + strlen(env);
357 return TRUE;
358 }
359 else return FALSE;
360}
361
362bool readingVerbatim = FALSE;
363bool readInVerbatim = FALSE; // Within a verbatim, but not nec. verbatiminput
364
365bool read_a_line(char *buf)
366{
367 if (CurrentInputIndex < 0)
368 {
369 buf[0] = 0;
370 return FALSE;
371 }
372
373 int ch = -2;
374 int i = 0;
375 buf[0] = 0;
376 while (ch != EOF && ch != 10)
377 {
378 if (((i == 14) && (strncmp(buf, "\\end{verbatim}", 14) == 0)) ||
379 ((i == 16) && (strncmp(buf, "\\end{toocomplex}", 16) == 0)))
380 readInVerbatim = FALSE;
381
382 ch = getc(Inputs[CurrentInputIndex]);
383 if (ch != EOF)
384 {
385 // Check for 2 consecutive newlines and replace with \par
386 if (ch == 10 && !readInVerbatim)
387 {
388 int ch1 = getc(Inputs[CurrentInputIndex]);
389 if ((ch1 == 10) || (ch1 == 13))
390 {
391 // Eliminate newline (10) following DOS linefeed
392 if (ch1 == 13) ch1 = getc(Inputs[CurrentInputIndex]);
393 buf[i] = 0;
394 IncrementLineNumber();
395// strcat(buf, "\\par\n");
396// i += 6;
397 strcat(buf, "\\par");
398 i += 5;
399 }
400 else
401 {
402 ungetc(ch1, Inputs[CurrentInputIndex]);
403 buf[i] = ch;
404 i ++;
405 }
406 }
407 else
408 {
409
410 // Convert embedded characters to RTF equivalents
411 switch(ch)
412 {
413