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