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