Imported Robodoc.
[robodoc.git] / Source / rtf_generator.c
1 // vi: spell ff=unix
2
3 /*
4 Copyright (C) 1994-2007  Frans Slothouber, Jacco van Weert, Petteri Kettunen,
5 Bernd Koesling, Thomas Aglassinger, Anthon Pang, Stefan Kost, David Druffner,
6 Sasha Vasko, Kai Hofmann, Thierry Pierron, Friedrich Haase, and Gergely Budai.
7
8 This file is part of ROBODoc
9
10 ROBODoc is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
23 */
24
25
26 /****h* ROBODoc/RTF_Generator
27  * FUNCTION
28  *   A collection of functions to generate output in RTF format.
29  *   Supports sections upto 7 levels deep.
30  * TODO
31  *   Documentation
32  * MODIFICATION HISTORY
33  *   ????-??-??   Anthon Pang       V1.0
34  *   2003-02-03   Frans Slothouber  Refactoring
35  *   Anthon Pang
36  *****
37  * $Id: rtf_generator.c,v 1.27 2007/07/10 19:13:52 gumpu Exp $ 
38  */
39
40
41 #include <stdio.h>
42 #include <string.h>
43 #include <assert.h>
44
45 #include "rtf_generator.h"
46 #include "util.h"
47 #include "robodoc.h"
48 #include "globals.h"
49 #include "headers.h"
50 #include "generator.h"
51
52 #ifdef DMALLOC
53 #include <dmalloc.h>
54 #endif
55
56
57 void RB_RTF_Generate_False_Link(
58     FILE *dest_doc,
59     char *name )
60 {
61     /* Find out how to emphasize this in rtf */
62     fprintf( dest_doc, "%s", name );
63 }
64
65 void RB_RTF_Generate_TOC_2(
66     FILE *dest_doc,
67     struct RB_header **headers,
68     int count )
69 {
70     int                 i;
71
72     for ( i = 0; i < count; ++i )
73     {
74         struct RB_header   *cur_header = headers[i];
75
76         if ( cur_header->name && cur_header->function_name )
77         {
78             fprintf( dest_doc, "{" );
79             RB_RTF_Generate_String( dest_doc, cur_header->name );
80             fprintf( dest_doc, "}{\\v " );
81             RB_RTF_Generate_String( dest_doc, cur_header->function_name );
82             fprintf( dest_doc, "}\\line\n" );
83         }
84     }
85 }
86
87 void RB_RTF_Generate_Label(
88     FILE *dest_doc,
89     char *name )
90 {
91     USE( dest_doc );
92     USE( name );
93     /* Empty */
94 }
95
96 void RB_RTF_Generate_Item_Name(
97     FILE *dest_doc,
98     char *name )
99 {
100     fprintf( dest_doc, "\\par \\fs18 %s ", name );
101 }
102
103 char               *RB_RTF_Get_Default_Extension(
104     void )
105 {
106     return ".rtf";
107 }
108
109
110 void RB_RTF_Generate_BeginSection(
111     FILE *dest_doc,
112     int depth,
113     char *name )
114 {
115     fprintf( dest_doc, "\\par {\\s%d %s \\par} \\pard\\plain\n", depth, name );
116 }
117
118 void RB_RTF_Generate_EndSection(
119     FILE *dest_doc,
120     int depth,
121     char *name )
122 {
123     USE( dest_doc );
124     USE( depth );
125     USE( name );
126     /* empty */
127 }
128
129 /*x**f* RTF_Generator/RB_RTF_Generate_Doc_Start
130  * NAME
131  *   RB_RTF_Generate_Doc_Start --
132  ******
133  */
134
135 void RB_RTF_Generate_Doc_Start(
136     FILE *dest_doc,
137     char *src_name,
138     char *name,
139     char toc )
140 {
141     USE( toc );
142
143     /* RTF header */
144     fprintf( dest_doc, "{\\rtf1\\ansi \\deff0"
145              "{\\fonttbl;"
146              "\\f0\\fswiss MS Sans Serif;"
147              "\\f1\\fmodern Courier New;"
148              "\\f2\\ftech Symbol;"
149              "}"
150              "{\\colortbl;"
151              "\\red255\\green255\\blue255;"
152              "\\red0\\green0\\blue0;" "\\red0\\green0\\blue255;" "}" );
153
154     /* RTF document info */
155     fprintf( dest_doc, "{\\info"
156              "{\\title %s}" "{\\comment\n" " Source: %s\n", name, src_name );
157     if ( course_of_action.do_nogenwith )
158     {
159         fprintf( dest_doc, " " "}" "}" );
160     }
161     else
162     {
163         fprintf( dest_doc, " " COMMENT_ROBODOC " " "}" "}" );
164     }
165
166     /* RTF stylesheet */
167     fprintf( dest_doc,
168              "{\\stylesheet\n"
169              "{\\s1 \\fs20 \\keepn\\widctlpar\\jclisttab \\outlinelevel0\\adjustright \\cgrid \\sbasedon0 \\snext0 heading 1;}\n"
170              "{\\s2 \\fs20 \\keepn\\widctlpar\\jclisttab \\outlinelevel1\\adjustright \\cgrid \\sbasedon0 \\snext0 heading 2;}\n"
171              "{\\s3 \\fs20 \\keepn\\widctlpar\\jclisttab \\outlinelevel2\\adjustright \\cgrid \\sbasedon0 \\snext0 heading 3;}\n"
172              "{\\s4 \\fs20 \\keepn\\widctlpar\\jclisttab \\outlinelevel3\\adjustright \\cgrid \\sbasedon0 \\snext0 heading 4;}\n"
173              "{\\s5 \\fs20 \\keepn\\widctlpar\\jclisttab \\outlinelevel4\\adjustright \\cgrid \\sbasedon0 \\snext0 heading 5;}\n"
174              "{\\s6 \\fs20 \\keepn\\widctlpar\\jclisttab \\outlinelevel5\\adjustright \\cgrid \\sbasedon0 \\snext0 heading 6;}\n"
175              "{\\s7 \\fs20 \\keepn\\widctlpar\\jclisttab \\outlinelevel6\\adjustright \\cgrid \\sbasedon0 \\snext0 heading 7;}}\n" );
176     /* RTF document format */
177     fprintf( dest_doc, "{\\margl1440\\margr1440}\\fs18\n" );
178
179     /* RTF document section */
180     fprintf( dest_doc, "\\f0\\cb1\\cf3\\b1\\qc"
181              "{\\super #{\\footnote{\\super #}%s_TOC}}"
182              "{\\super ${\\footnote{\\super $}Contents}}"
183              "{TABLE OF CONTENTS}\\ql\\b0\\fs20\\cf2\\par\n", src_name );
184 }
185
186 /*x**f* RTF_Generator/RB_RTF_Generate_Doc_End
187  * NAME
188  *   RB_RTF_Generate_Doc_End --
189  ******
190  */
191
192 void RB_RTF_Generate_Doc_End(
193     FILE *dest_doc,
194     char *name )
195 {
196     USE( name );
197
198     fputc( '}', dest_doc );
199 }
200
201 /*x**f* RTF_Generator/RB_RTF_Generate_Header_Start
202  * NAME
203  *   RB_RTF_Generate_Header_Start --
204  ******
205  */
206
207 void RB_RTF_Generate_Header_Start(
208     FILE *dest_doc,
209     struct RB_header *cur_header )
210 {
211     if ( cur_header->name && cur_header->function_name )
212     {
213         RB_RTF_Generate_String( dest_doc, cur_header->function_name );
214         fprintf( dest_doc, "\\page {\\super #{\\footnote{\\super #}" );
215         RB_RTF_Generate_String( dest_doc, cur_header->function_name );
216         fprintf( dest_doc, "}}{\\super ${\\footnote{\\super $}" );
217         RB_RTF_Generate_String( dest_doc, cur_header->name );
218         fprintf( dest_doc, "}} " );
219         RB_RTF_Generate_String( dest_doc, cur_header->name );
220         fprintf( dest_doc, "\\line\n" );
221     }
222 }
223
224 /*x**f* RTF_Generator/RB_RTF_Generate_Header_End
225  * NAME
226  *   RB_RTF_Generate_Header_End --
227  ******
228  */
229
230 void RB_RTF_Generate_Header_End(
231     FILE *dest_doc,
232     struct RB_header *cur_header )
233 {
234     USE( cur_header );
235
236     fprintf( dest_doc, "\\par\n" );
237 }
238
239
240 void RB_RTF_Generate_Item_Begin(
241     FILE *dest_doc )
242 {
243     fprintf( dest_doc, "\\line {\\f1{} \\fs18" );
244 }
245
246 void RB_RTF_Generate_Item_End(
247     FILE *dest_doc )
248 {
249     fputc( '}', dest_doc );
250     fputc( '\n', dest_doc );
251 }
252
253 /*x**f* RTF_Generator/RB_RTF_Generate_Empty_Item
254  * NAME
255  *   RB_RTF_Generate_Empty_Item --
256  ******
257  */
258
259 void RB_RTF_Generate_Empty_Item(
260     FILE *dest_doc )
261 {
262     fprintf( dest_doc, "\n" );
263 }
264
265
266 void RB_RTF_Generate_Link(
267     FILE *dest_doc,
268     char *dest_name,
269     char *filename,
270     char *labelname,
271     char *linkname
272      )
273 {
274     USE( dest_name );
275     USE( filename );
276
277     if ( strcmp( labelname, linkname ) )
278     {
279         fprintf( dest_doc, "{\\b " );
280         RB_RTF_Generate_String( dest_doc, linkname );
281         fprintf( dest_doc, "}{\\v " );
282         RB_RTF_Generate_String( dest_doc, labelname );
283         fprintf( dest_doc, "}" );
284     }
285     else
286     {
287         RB_RTF_Generate_String( dest_doc, labelname );
288     }
289 }
290
291 void RB_RTF_Generate_String(
292     FILE *dest_doc,
293     char *a_string )
294 {
295     int                 i;
296     int                 l = strlen( a_string );
297     unsigned char       c;
298
299     for ( i = 0; i < l; ++i )
300     {
301         c = a_string[i];
302         RB_RTF_Generate_Char( dest_doc, c );
303     }
304 }
305
306 void RB_RTF_Generate_Char(
307     FILE *dest_doc,
308     int c )
309 {
310     switch ( c )
311     {
312     case '\n':
313         assert( 0 );
314         break;
315     case '\t':
316         assert( 0 );
317         break;
318     case '\\':
319     case '{':
320     case '}':
321         fputc( '\\', dest_doc );
322         fputc( c, dest_doc );
323         break;
324     default:
325         fputc( c, dest_doc );
326     }
327 }