summaryrefslogtreecommitdiff
path: root/support/highlight/src/core/rtfgenerator.cpp
blob: dab2c805cc62e42d64a099d7300ff8904057a9bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/***************************************************************************
                          rtfcode.cpp  -  description
                             -------------------
    begin                : Die Jul 9 2002
    copyright            : (C) 2002-2007 by Andre Simon
    email                : andre.simon1@gmx.de
 ***************************************************************************/


/*
This file is part of Highlight.

Highlight is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Highlight is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Highlight.  If not, see <http://www.gnu.org/licenses/>.
*/


#include <sstream>

#include "charcodes.h"
#include "version.h"
#include "rtfgenerator.h"

using namespace std;

namespace highlight
{

	string RtfGenerator::getAttributes ( const ElementStyle & col )
	{
		stringstream s;
		s  << "\\red"<< col.getColour().getRed ( RTF )
		<< "\\green"<<col.getColour().getGreen ( RTF )
		<< "\\blue"<<col.getColour().getBlue ( RTF )
		<< ";";
		return s.str();
	}

	string  RtfGenerator::getOpenTag ( int styleNumber,const ElementStyle & elem )
	{
		ostringstream s;
		s << "{";
		if ( addCharStyles )
		{
			s<<"\\*\\cs"<< ( styleNumber+2 );
		}
		s << "\\cf"<< ( styleNumber+2 ) <<"{";

		if ( elem.isBold() ) s << "\\b ";
		if ( elem.isItalic() ) s << "\\i ";
		if ( elem.isUnderline() ) s << "\\ul ";
		return  s.str();
	}


	string RtfGenerator::getCharStyle ( int styleNumber,const ElementStyle &elem,
	                                    const string&styleName )
	{
		ostringstream s;
		s << "{\\*\\cs"<< ( styleNumber+2 ) <<"\\additive\\cf"<< ( styleNumber+2 )
		<< "\\f1\\fs";
		int fontSize=0;
		StringTools::str2num<int> ( fontSize, this->getBaseFontSize(), std::dec );
		s << ( ( fontSize ) ? fontSize*2: 20 );  // RTF needs double amount
		if ( elem.isBold() ) s << "\\b";
		if ( elem.isItalic() ) s << "\\i";
		if ( elem.isUnderline() ) s << "\\ul";
		s << "\\sbasedon222\\snext0 "<< styleName << ";}\n";
		return  s.str();
	}
// {\*\cs2\additive\cf2\f1\fs20\sbasedon222\snext0 HL Default;}

	string  RtfGenerator::getCloseTag ( const ElementStyle &elem )
	{
		ostringstream s;
		if ( elem.isBold() ) s << "\\b0 ";
		if ( elem.isItalic() ) s << "\\i0 ";
		if ( elem.isUnderline() ) s << "\\ul0 ";
		s << "}}";
		return  s.str();
	}

	RtfGenerator::RtfGenerator()
			: CodeGenerator ( RTF ),
			pageSize ( "a4" ), // Default: DIN A4
			addCharStyles ( false )
	{
		newLineTag = "}\\par\\pard\n\\cbpat1{";
		spacer = " ";

		// Page dimensions
		psMap["a3"] = PageSize ( 16837,23811 );
		psMap["a4"] = PageSize ( 11905,16837 );
		psMap["a5"] = PageSize ( 8390,11905 );

		psMap["b4"] = PageSize ( 14173,20012 );
		psMap["b5"] = PageSize ( 9977,14173 );
		psMap["b6"] = PageSize ( 7086,9977 );

		psMap["letter"] = PageSize ( 12240,15840 );
		psMap["legal"] = PageSize ( 12240,20163 );
	}

	RtfGenerator::~RtfGenerator()
	{}

	string RtfGenerator::getHeader()
	{
		return string();
	}

	void RtfGenerator::printBody()
	{
		*out << "{\\rtf1\\ansi\\uc0 \\deff1"
		<< "{\\fonttbl{\\f1\\fmodern\\fprq1\\fcharset0 " ;
		*out << this->getBaseFont() ;
		*out << ";}}"
		<< "{\\colortbl;";

		*out << "\\red"    << ( docStyle.getBgColour().getRed ( RTF ) );
		*out << "\\green"    << ( docStyle.getBgColour().getGreen ( RTF ) );
		*out << "\\blue"    << ( docStyle.getBgColour().getBlue ( RTF ) );
		*out << ";";

		*out << getAttributes ( docStyle.getDefaultStyle() );

		*out << getAttributes ( docStyle.getStringStyle() );
		*out << getAttributes ( docStyle.getNumberStyle() );
		*out << getAttributes ( docStyle.getSingleLineCommentStyle() );

		*out << getAttributes ( docStyle.getCommentStyle() );
		*out << getAttributes ( docStyle.getEscapeCharStyle() );
		*out << getAttributes ( docStyle.getDirectiveStyle() );

		*out << getAttributes ( docStyle.getDirectiveStringStyle() );
		*out << getAttributes ( docStyle.getLineStyle() );
		*out << getAttributes ( docStyle.getSymbolStyle() );

		/* For output formats which can refer to external styles it is more safe
		   to use the colour theme's keyword class names, since the language
		   definitions (which may change during a batch conversion) do not have to define
		   all keyword classes, that are needed to highlight all input files correctly.
		   It is ok for RTF to use the language definition's class names, because RTF
		   does not refer to external styles.
		   We cannot use the theme's class names, because KSIterator returns an
		   alphabetically ordered list, which is not good because RTF is dependent
		   on the order. We access the keyword style with an ID, which is calculated
		   ignoring the alphabetic order.
		*/
		vector<string>  keywordClasses = langInfo.getKeywordClasses();
		for ( unsigned int i=0;i<keywordClasses.size();i++ )
		{
			*out << getAttributes ( docStyle.getKeywordStyle ( keywordClasses[i] ) );
		}

		*out << "}\n";

		if ( addCharStyles )
		{
			*out << "{\\stylesheet{\n";
			*out << getCharStyle ( STANDARD, docStyle.getDefaultStyle(), "HL Default" );
			*out << getCharStyle ( STRING, docStyle.getStringStyle(), "HL String" );
			*out << getCharStyle ( NUMBER, docStyle.getNumberStyle(), "HL Number" );
			*out << getCharStyle ( SL_COMMENT, docStyle.getSingleLineCommentStyle(), "HL SL Comment" );
			*out << getCharStyle ( ML_COMMENT, docStyle.getCommentStyle(), "HL ML Comment" );
			*out << getCharStyle ( ESC_CHAR, docStyle.getEscapeCharStyle(), "HL Escape Character" );
			*out << getCharStyle ( DIRECTIVE, docStyle.getDirectiveStyle(), "HL Directive" );
			*out << getCharStyle ( DIRECTIVE_STRING, docStyle.getDirectiveStringStyle(), "HL Directive String" );
			*out << getCharStyle ( LINENUMBER, docStyle.getLineStyle(), "HL Line" );
			*out << getCharStyle ( SYMBOL, docStyle.getSymbolStyle(), "HL Symbol" );
			char styleName[20];
			for ( unsigned int i=0;i<keywordClasses.size();i++ )
			{
				sprintf ( styleName, "HL Keyword %c", 'A'+i ); //maybe better simple numbering
				*out << getCharStyle ( KEYWORD+i, docStyle.getKeywordStyle ( keywordClasses[i] ), string ( styleName ) );
			}
			*out << "}}\n";
		}

		*out  << "\\paperw"<< psMap[pageSize].width <<"\\paperh"<< psMap[pageSize].height
		<< "\\margl1134\\margr1134\\margt1134\\margb1134\\sectd" // page margins
		<< "\\plain\\f1\\fs" ;  // Font formatting
		int fontSize=0;
		StringTools::str2num<int> ( fontSize, this->getBaseFontSize(), std::dec );
		*out << ( ( fontSize ) ? fontSize*2: 20 );  // RTF needs double amount
		*out << "\n\\pard \\cbpat1{";

		processRootState();

		*out << "}}"<<endl;
	}

	string RtfGenerator::getFooter()
	{
		return string();
	}

	void RtfGenerator::initOutputTags ( )
	{
		openTags.push_back ( getOpenTag ( STANDARD, docStyle.getDefaultStyle() ) );
		openTags.push_back ( getOpenTag ( STRING, docStyle.getStringStyle() ) );
		openTags.push_back ( getOpenTag ( NUMBER, docStyle.getNumberStyle() ) );
		openTags.push_back ( getOpenTag ( SL_COMMENT, docStyle.getSingleLineCommentStyle() ) );
		openTags.push_back ( getOpenTag ( ML_COMMENT,docStyle.getCommentStyle() ) );
		openTags.push_back ( getOpenTag ( ESC_CHAR, docStyle.getEscapeCharStyle() ) );
		openTags.push_back ( getOpenTag ( DIRECTIVE, docStyle.getDirectiveStyle() ) );
		openTags.push_back ( getOpenTag ( DIRECTIVE_STRING, docStyle.getDirectiveStringStyle() ) );
		openTags.push_back ( getOpenTag ( LINENUMBER, docStyle.getLineStyle() ) );
		openTags.push_back ( getOpenTag ( SYMBOL, docStyle.getSymbolStyle() ) );

		closeTags.push_back ( getCloseTag ( docStyle.getDefaultStyle() ) );
		closeTags.push_back ( getCloseTag ( docStyle.getStringStyle() ) );
		closeTags.push_back ( getCloseTag ( docStyle.getNumberStyle() ) );
		closeTags.push_back ( getCloseTag ( docStyle.getSingleLineCommentStyle() ) );
		closeTags.push_back ( getCloseTag ( docStyle.getCommentStyle() ) );
		closeTags.push_back ( getCloseTag ( docStyle.getEscapeCharStyle() ) );
		closeTags.push_back ( getCloseTag ( docStyle.getDirectiveStyle() ) );
		closeTags.push_back ( getCloseTag ( docStyle.getDirectiveStringStyle() ) );
		closeTags.push_back ( getCloseTag ( docStyle.getLineStyle() ) );
		closeTags.push_back ( getCloseTag ( docStyle.getSymbolStyle() ) );
	}

	string RtfGenerator::maskCharacter ( unsigned char c )
	{
		switch ( c )
		{
			case '}' :
			case '{' :
			case '\\' :
			{
				string m ( "\\" );
				return m += c;
			}
			break;
			case '0':
			case '1':
			case '2':
			case '3':
			case '4':
			case '5':
			case '6':
			case '7':
			case '8':
			case '9':
			{
				string m ( 1, '{' );
				m += c;
				m += '}';
				return m;
			}
			break;

			case AUML_LC:
				return "\\'e4";
				break;
			case OUML_LC:
				return "\\'f6";
				break;
			case UUML_LC:
				return "\\'fc";
				break;
			case AUML_UC:
				return "\\'c4";
				break;
			case OUML_UC:
				return "\\'d6";
				break;
			case UUML_UC:
				return "\\'dc";
				break;

			case AACUTE_LC:
				return "\\'e1";
				break;
			case EACUTE_LC:
				return "\\'e9";
				break;
			case OACUTE_LC:
				return "\\'f3";
				break;
			case UACUTE_LC:
				return "\\'fa";
				break;

			case AGRAVE_LC:
				return "\\'e0";
				break;
			case EGRAVE_LC:
				return "\\'e8";
				break;
			case OGRAVE_LC:
				return "\\'f2";
				break;
			case UGRAVE_LC:
				return "\\'f9";
				break;

			case AACUTE_UC:
				return "\\'c1";
				break;
			case EACUTE_UC:
				return "\\'c9";
				break;
			case OACUTE_UC:
				return "\\'d3";
				break;
			case UACUTE_UC:
				return "\\'da";
				break;
			case AGRAVE_UC:
				return "\\'c0";
				break;
			case EGRAVE_UC:
				return "\\'c8";
				break;
			case OGRAVE_UC:
				return "\\'d2";
				break;
			case UGRAVE_UC:
				return "\\'d9";
				break;

			case SZLIG:
				return "\\'df";
				break;

			default :
				return string ( 1, c );
		}
	}

	string RtfGenerator::getKeywordOpenTag ( unsigned int styleID )
	{
		return getOpenTag ( KEYWORD+styleID,
		                    docStyle.getKeywordStyle ( langInfo.getKeywordClasses() [styleID] ) );
	}

	string RtfGenerator::getKeywordCloseTag ( unsigned int styleID )
	{
		return getCloseTag ( docStyle.getKeywordStyle ( langInfo.getKeywordClasses() [styleID] ) );
	}

	void RtfGenerator::setRTFPageSize ( const string & ps )
	{
		if ( psMap.count ( ps ) ) pageSize = ps;
	}

	void RtfGenerator::setRTFCharStyles ( bool cs )
	{
		addCharStyles = cs;
	}

}