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