]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: rtfutils.cpp | |
3 | // Purpose: Converts Latex to Word RTF/WinHelp RTF | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 7.9.93 | |
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 "tex2any.h" | |
28 | #include "tex2rtf.h" | |
29 | #include <ctype.h> | |
30 | #include <stdlib.h> | |
31 | #include <stdio.h> | |
32 | ||
33 | #ifdef __WIN32__ | |
34 | #include <windows.h> | |
35 | #endif | |
36 | ||
37 | #include "bmputils.h" | |
38 | #include "table.h" | |
39 | ||
40 | wxList itemizeStack; | |
41 | static int indentLevel = 0; | |
42 | static int forbidParindent = 0; // if > 0, no parindent (e.g. in center environment) | |
43 | int forbidResetPar = 0; // If > 0, don't reset memory of having output a new par | |
44 | ||
45 | static char *contentsLineSection = NULL; | |
46 | static char *contentsLineValue = NULL; | |
47 | static TexChunk *descriptionItemArg = NULL; | |
48 | static wxStringList environmentStack; // Stack of paragraph styles we need to remember | |
49 | static int footnoteCount = 0; | |
50 | static int citeCount = 1; | |
51 | extern char *FileRoot; | |
52 | extern bool winHelp; | |
53 | extern bool startedSections; | |
54 | extern FILE *Contents; | |
55 | extern FILE *Chapters; | |
56 | extern FILE *Popups; | |
57 | extern FILE *WinHelpContentsFile; | |
58 | extern char *RTFCharset; | |
59 | // This is defined in the Tex2Any library and isn't in use after parsing | |
60 | extern char *BigBuffer; | |
61 | ||
62 | ||
63 | extern wxHashTable TexReferences; | |
64 | ||
65 | ||
66 | // Are we in verbatim mode? If so, format differently. | |
67 | static bool inVerbatim = FALSE; | |
68 | ||
69 | // We're in a series of PopRef topics, so don't output section headings | |
70 | bool inPopRefSection = FALSE; | |
71 | ||
72 | // Green colour? | |
73 | static bool hotSpotColour = TRUE; | |
74 | static bool hotSpotUnderline = TRUE; | |
75 | ||
76 | // Transparency (WHITE = transparent) | |
77 | static bool bitmapTransparency = TRUE; | |
78 | ||
79 | // Linear RTF requires us to set the style per section. | |
80 | static char *currentNumberStyle = NULL; | |
81 | static int currentItemSep = 8; | |
82 | static int CurrentTextWidth = 8640; // Say, six inches | |
83 | static int CurrentLeftMarginOdd = 400; | |
84 | static int CurrentLeftMarginEven = 1440; | |
85 | static int CurrentRightMarginOdd = 1440; | |
86 | static int CurrentRightMarginEven = 400; | |
87 | static int CurrentMarginParWidth = 2000; | |
88 | static int CurrentMarginParSep = 400; // Gap between marginpar and text | |
89 | static int CurrentMarginParX = CurrentLeftMarginOdd + CurrentTextWidth + CurrentMarginParSep; | |
90 | static int GutterWidth = 2300; | |
91 | ||
92 | // Two-column table dimensions, in twips | |
93 | static int TwoColWidthA = 1500; | |
94 | static int TwoColWidthB = 3000; | |
95 | ||
96 | const int PageWidth = 12242; // 8.25 inches wide for A4 | |
97 | ||
98 | ||
99 | /* | |
100 | * Flag to say we've just issued a \par\pard command, so don't | |
101 | * repeat this unnecessarily. | |
102 | * | |
103 | */ | |
104 | ||
105 | int issuedNewParagraph = 0; | |
106 | ||
107 | // Need to know whether we're in a table or figure for benefit | |
108 | // of listoffigures/listoftables | |
109 | static bool inFigure = FALSE; | |
110 | static bool inTable = FALSE; | |
111 | ||
112 | /* | |
113 | * Current topics | |
114 | * | |
115 | */ | |
116 | static char *CurrentChapterName = NULL; | |
117 | static char *CurrentSectionName = NULL; | |
118 | static char *CurrentSubsectionName = NULL; | |
119 | static char *CurrentTopic = NULL; | |
120 | ||
121 | static bool InPopups() | |
122 | { | |
123 | if (CurrentChapterName && (strcmp(CurrentChapterName, "popups") == 0)) | |
124 | return TRUE; | |
125 | if (CurrentSectionName && (strcmp(CurrentSectionName, "popups") == 0)) | |
126 | return TRUE; | |
127 | return FALSE; | |
128 | } | |
129 | ||
130 | static void SetCurrentTopic(char *s) | |
131 | { | |
132 | if (CurrentTopic) delete[] CurrentTopic; | |
133 | CurrentTopic = copystring(s); | |
134 | } | |
135 | ||
136 | void SetCurrentChapterName(char *s) | |
137 | { | |
138 | if (CurrentChapterName) delete[] CurrentChapterName; | |
139 | CurrentChapterName = copystring(s); | |
140 | SetCurrentTopic(s); | |
141 | } | |
142 | void SetCurrentSectionName(char *s) | |
143 | { | |
144 | if (CurrentSectionName) delete[] CurrentSectionName; | |
145 | CurrentSectionName = copystring(s); | |
146 | SetCurrentTopic(s); | |
147 | } | |
148 | void SetCurrentSubsectionName(char *s) | |
149 | { | |
150 | if (CurrentSubsectionName) delete[] CurrentSubsectionName; | |
151 | CurrentSubsectionName = copystring(s); | |
152 | SetCurrentTopic(s); | |
153 | } | |
154 | ||
155 | // Indicate that a parent topic at level 'level' has children. | |
156 | // Level 1 is a chapter, 2 is a section, etc. | |
157 | void NotifyParentHasChildren(int parentLevel) | |
158 | { | |
159 | char *parentTopic = NULL; | |
160 | switch (parentLevel) | |
161 | { | |
162 | case 1: | |
163 | { | |
164 | parentTopic = CurrentChapterName; | |
165 | break; | |
166 | } | |
167 | case 2: | |
168 | { | |
169 | parentTopic = CurrentSectionName; | |
170 | break; | |
171 | } | |
172 | case 3: | |
173 | { | |
174 | parentTopic = CurrentSubsectionName; | |
175 | break; | |
176 | } | |
177 | default: | |
178 | { | |
179 | break; | |
180 | } | |
181 | } | |
182 | if (parentTopic) | |
183 | { | |
184 | TexTopic *texTopic = (TexTopic *)TopicTable.Get(parentTopic); | |
185 | if (!texTopic) | |
186 | { | |
187 | texTopic = new TexTopic; | |
188 | TopicTable.Put(parentTopic, texTopic); | |
189 | } | |
190 | texTopic->hasChildren = TRUE; | |
191 | } | |
192 | } | |
193 | ||
194 | // Have to keep a count of what levels are books, what are pages, | |
195 | // in order to correct for a Win95 bug which means that if you | |
196 | // have a book at level n, and then a page at level n, the page | |
197 | // ends up on level n + 1. | |
198 | ||
199 | bool ContentsLevels[5]; | |
200 | ||
201 | // Reset below this level (starts from 1) | |
202 | void ResetContentsLevels(int l) | |
203 | { | |
204 | int i; | |
205 | for (i = l; i < 5; i++) | |
206 | ContentsLevels[i] = FALSE; | |
207 | ||
208 | // There are always books on the top level | |
209 | ContentsLevels[0] = TRUE; | |
210 | } | |
211 | ||
212 | // Output a WinHelp section as a keyword, substituting | |
213 | // : for space. | |
214 | void OutputSectionKeyword(FILE *fd) | |
215 | { | |
216 | OutputCurrentSectionToString(wxBuffer); | |
217 | ||
218 | unsigned int i; | |
219 | for (i = 0; i < strlen(wxBuffer); i++) | |
220 | if (wxBuffer[i] == ':') | |
221 | wxBuffer[i] = ' '; | |
222 | // Don't write to index if there's some RTF in the string | |
223 | else if ( wxBuffer[i] == '{' ) | |
224 | return; | |
225 | ||
226 | fprintf(fd, "K{\\footnote {K} "); | |
227 | fprintf(fd, "%s", wxBuffer); | |
228 | ||
229 | fprintf(fd, "}\n"); | |
230 | } | |
231 | ||
232 | // Write a line for the .cnt file, if we're doing this. | |
233 | void WriteWinHelpContentsFileLine(char *topicName, char *xitle, int level) | |
234 | { | |
235 | // First, convert any RTF characters to ASCII | |
236 | char title[255]; | |
237 | int s=0; | |
238 | int d=0; | |
239 | while ( (xitle[s]!=0)&&(d<255) ) | |
240 | { | |
241 | char ch=xitle[s]&0xff; | |
242 | if (ch==0x5c) { | |
243 | char ch1=xitle[s+1]&0xff; | |
244 | char ch2=xitle[s+2]&0xff; | |
245 | char ch3=xitle[s+3]&0xff; | |
246 | char ch4=xitle[s+4]&0xff; | |
247 | s+=4; // next character | |
248 | char a=0; | |
249 |