]> git.saurik.com Git - wxWidgets.git/blame - utils/tex2rtf/src/rtfutils.cpp
Applied patch [ 868373 ] Missed Unicode markup in menu sample
[wxWidgets.git] / utils / tex2rtf / src / rtfutils.cpp
CommitLineData
9a29912f
JS
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
9a29912f
JS
24#endif
25
26#include "tex2any.h"
27#include "tex2rtf.h"
28#include <ctype.h>
29#include <stdlib.h>
30#include <stdio.h>
31
32#ifdef __WIN32__
33#include <windows.h>
34#endif
35
36#include "bmputils.h"
37#include "table.h"
38
ea172e69
MB
39#if !WXWIN_COMPATIBILITY_2_4
40static inline wxChar* copystring(const wxChar* s)
41 { return wxStrcpy(new wxChar[wxStrlen(s) + 1], s); }
42#endif
43
9a29912f
JS
44wxList itemizeStack;
45static int indentLevel = 0;
46static int forbidParindent = 0; // if > 0, no parindent (e.g. in center environment)
47int forbidResetPar = 0; // If > 0, don't reset memory of having output a new par
48
49static char *contentsLineSection = NULL;
50static char *contentsLineValue = NULL;
51static TexChunk *descriptionItemArg = NULL;
52static wxStringList environmentStack; // Stack of paragraph styles we need to remember
53static int footnoteCount = 0;
54static int citeCount = 1;
55extern char *FileRoot;
56extern bool winHelp;
57extern bool startedSections;
58extern FILE *Contents;
59extern FILE *Chapters;
60extern FILE *Popups;
61extern FILE *WinHelpContentsFile;
62extern char *RTFCharset;
63// This is defined in the Tex2Any library and isn't in use after parsing
64extern char *BigBuffer;
3924dd22 65
3924dd22
GT
66extern wxHashTable TexReferences;
67
9a29912f
JS
68// Are we in verbatim mode? If so, format differently.
69static bool inVerbatim = FALSE;
70
71// We're in a series of PopRef topics, so don't output section headings
72bool inPopRefSection = FALSE;
73
74// Green colour?
75static bool hotSpotColour = TRUE;
76static bool hotSpotUnderline = TRUE;
77
78// Transparency (WHITE = transparent)
79static bool bitmapTransparency = TRUE;
80
81// Linear RTF requires us to set the style per section.
82static char *currentNumberStyle = NULL;
83static int currentItemSep = 8;
84static int CurrentTextWidth = 8640; // Say, six inches
85static int CurrentLeftMarginOdd = 400;
86static int CurrentLeftMarginEven = 1440;
87static int CurrentRightMarginOdd = 1440;
88static int CurrentRightMarginEven = 400;
89static int CurrentMarginParWidth = 2000;
90static int CurrentMarginParSep = 400; // Gap between marginpar and text
91static int CurrentMarginParX = CurrentLeftMarginOdd + CurrentTextWidth + CurrentMarginParSep;
92static int GutterWidth = 2300;
93
94// Two-column table dimensions, in twips
95static int TwoColWidthA = 1500;
96static int TwoColWidthB = 3000;
97
98const int PageWidth = 12242; // 8.25 inches wide for A4
99
bf16085d
JS
100// Remember the anchor in a helpref
101static TexChunk *helpRefText = NULL;
9a29912f
JS
102
103/*
104 * Flag to say we've just issued a \par\pard command, so don't
105 * repeat this unnecessarily.
106 *
107 */
108
109int issuedNewParagraph = 0;
110
111// Need to know whether we're in a table or figure for benefit
112// of listoffigures/listoftables
113static bool inFigure = FALSE;
114static bool inTable = FALSE;
115
116/*
117 * Current topics
118 *
119 */
120static char *CurrentChapterName = NULL;
121static char *CurrentSectionName = NULL;
122static char *CurrentSubsectionName = NULL;
123static char *CurrentTopic = NULL;
124
125static bool InPopups()
126{
127 if (CurrentChapterName && (strcmp(CurrentChapterName, "popups") == 0))
128 return TRUE;
129 if (CurrentSectionName && (strcmp(CurrentSectionName, "popups") == 0))
130 return TRUE;
131 return FALSE;
132}
133
134static void SetCurrentTopic(char *s)
135{
136 if (CurrentTopic) delete[] CurrentTopic;
137 CurrentTopic = copystring(s);
138}
139
140void SetCurrentChapterName(char *s)
141{
142 if (CurrentChapterName) delete[] CurrentChapterName;
143 CurrentChapterName = copystring(s);
144 SetCurrentTopic(s);
145}
146void SetCurrentSectionName(char *s)
147{
148 if (CurrentSectionName) delete[] CurrentSectionName;
149 CurrentSectionName = copystring(s);
150 SetCurrentTopic(s);
151}
152void SetCurrentSubsectionName(char *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.
161void NotifyParentHasChildren(int parentLevel)
162{
163 char *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
203bool ContentsLevels[5];
204
205// Reset below this level (starts from 1)
206void 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.
218void OutputSectionKeyword(FILE *fd)
219{
aed3314d 220 OutputCurrentSectionToString(wxTex2RTFBuffer);
9a29912f 221
241c426c 222 unsigned int i;
aed3314d
JS
223 for (i = 0; i < strlen(wxTex2RTFBuffer); i++)
224 if (wxTex2RTFBuffer[i] == ':')
225 wxTex2RTFBuffer[i] = ' ';
9a29912f 226 // Don't write to index if there's some RTF in the string
aed3314d 227 else if ( wxTex2RTFBuffer[i] == '{' )
9a29912f
JS
228 return;
229
230 fprintf(fd, "K{\\footnote {K} ");
aed3314d 231 fprintf(fd, "%s", wxTex2RTFBuffer);
9a29912f
JS
232
233 fprintf(fd, "}\n");
234}
235
236// Write a line for the .cnt file, if we're doing this.
237void WriteWinHelpContentsFileLine(char *topicName, char *xitle, int level)
238{
239 // First, convert any RTF characters to ASCII
240 char title[255];
241 int s=0;
242 int d=0;
243 while ( (xitle[s]!=0)&&(d<255) )
244 {
245 char ch=xitle[s]&0xff;
246 if (ch==0x5c) {
247 char ch1=xitle[s+1]&0xff;
248 char ch2=xitle[s+2]&0xff;
249 char ch3=xitle[s+3]&0xff;
9a29912f
JS
250 s+=4; // next character
251 char a=0;
252