Doxygen
Loading...
Searching...
No Matches
rtfstyle.cpp
Go to the documentation of this file.
1/******************************************************************************
2 *
3 * Copyright (C) 1997-2021 by Dimitri van Heesch.
4 *
5 * Permission to use, copy, modify, and distribute this software and its
6 * documentation under the terms of the GNU General Public License is hereby
7 * granted. No representations are made about the suitability of this software
8 * for any purpose. It is provided "as is" without express or implied warranty.
9 * See the GNU General Public License for more details.
10 *
11 * Documents produced by Doxygen are derivative works derived from the
12 * input used in their production; they are not affected by this license.
13 *
14 */
15
16#include <string>
17#include <fstream>
18
19#include "rtfstyle.h"
20#include "message.h"
21#include "regex.h"
22
33
34static std::map<std::string,QCString &> g_styleMap =
35{
36 { "Title", rtf_title },
37 { "Subject", rtf_subject },
38 { "Comments", rtf_comments },
39 { "Company", rtf_company },
40 { "LogoFilename", rtf_logoFilename },
41 { "Author", rtf_author },
42 { "Manager", rtf_manager },
43 { "DocumentType", rtf_documentType },
44 { "DocumentId", rtf_documentId },
45 { "Keywords", rtf_keywords }
46};
47
48
49char rtf_Style_Reset[] = "\\pard\\plain ";
50
51#define RTF_LatexToc(lvl,nest,nxt,pos,twps) \
52 \
53 { "LatexTOC"#lvl, \
54 "\\s"#nest"\\li"#pos"\\sa"#twps"\\sb"#twps"\\widctlpar\\tqr\\tldot\\tx8640\\adjustright \\fs20\\cgrid ",\
55 "\\sbasedon0 \\snext"#nxt" LatexTOC "#lvl \
56 }
57
58#define RTF_ListBullet(lvl,nest,nxt,pos,lvl2) \
59 { "ListBullet"#lvl, \
60 "\\s"#nest"\\fi-360\\li"#pos"\\widctlpar\\jclisttab\\tx"#pos"{\\*\\pn \\pnlvlbody\\ilvl0\\ls"#lvl2"\\pnrnot0\\pndec }\\ls1\\adjustright \\fs20\\cgrid ", \
61 "\\sbasedon0 \\snext"#nxt" \\sautoupd List Bullet "#lvl \
62 }
63
64#define RTF_ListEnum(lvl,nest,nxt,pos) \
65 { "ListEnum"#lvl, \
66 "\\s"#nest"\\fi-360\\li"#pos"\\widctlpar\\fs20\\cgrid ", \
67 "\\sbasedon0 \\snext"#nxt" \\sautoupd List Enum "#lvl \
68 }
69
70#define RTF_CodeExample(lvl,nest,nxt,pos) \
71 { "CodeExample"#lvl, \
72 "\\s"#nest"\\li"#pos"\\widctlpar\\adjustright \\shading1000\\cbpat8 \\f2\\fs16\\cgrid ", \
73 "\\sbasedon0 \\snext"#nxt" Code Example "#lvl \
74 }
75
76#define RTF_ListContinue(lvl,nest,nxt,pos) \
77 { "ListContinue"#lvl, \
78 "\\s"#nest"\\li"#pos"\\sa60\\sb30\\qj\\widctlpar\\qj\\adjustright \\fs20\\cgrid ", \
79 "\\sbasedon0 \\snext"#nxt" List Continue "#lvl \
80 }
81
82#define RTF_DescContinue(lvl,nest,nxt,pos) \
83 { "DescContinue"#lvl, \
84 "\\s"#nest"\\li"#pos"\\widctlpar\\ql\\adjustright \\fs20\\cgrid ", \
85 "\\sbasedon0 \\snext"#nxt" DescContinue "#lvl \
86 }
87
89{
90 { "Heading1",
91 "\\s1\\sb240\\sa60\\keepn\\widctlpar\\adjustright \\b\\f1\\fs36\\kerning36\\cgrid ",
92 "\\sbasedon0 \\snext0 heading 1"
93 },
94 { "Heading2",
95 "\\s2\\sb240\\sa60\\keepn\\widctlpar\\adjustright \\b\\f1\\fs28\\kerning28\\cgrid ",
96 "\\sbasedon0 \\snext0 heading 2"
97 },
98 { "Heading3",
99 "\\s3\\sb240\\sa60\\keepn\\widctlpar\\adjustright \\b\\f1\\cgrid ",
100 "\\sbasedon0 \\snext0 heading 3"
101 },
102 { "Heading4",
103 "\\s4\\sb240\\sa60\\keepn\\widctlpar\\adjustright \\b\\f1\\fs20\\cgrid ",
104 "\\sbasedon0 \\snext0 heading 4;}{\\*\\cs10 \\additive Default Paragraph Font"
105 },
106 { "Heading5",
107 "\\s5\\sb90\\sa30\\keepn\\widctlpar\\adjustright \\b\\f1\\fs16\\cgrid ",
108 "\\sbasedon0 \\snext0 heading 5;}{\\*\\cs10 \\additive Default Paragraph Font"
109 },
110 { "Heading6",
111 "\\s6\\sb90\\sa30\\keepn\\widctlpar\\adjustright \\b\\f1\\fs12\\cgrid ",
112 "\\sbasedon0 \\snext0 heading 6;}{\\*\\cs10 \\additive Default Paragraph Font"
113 },
114 { "Title",
115 "\\s15\\qc\\sb240\\sa60\\widctlpar\\outlinelevel0\\adjustright \\b\\f1\\fs32\\kerning28\\cgrid ",
116 "\\sbasedon0 \\snext15 Title"
117 },
118 { "SubTitle",
119 "\\s16\\qc\\sa60\\widctlpar\\outlinelevel1\\adjustright \\f1\\cgrid ",
120 "\\sbasedon0 \\snext16 Subtitle"
121 },
122 { "BodyText",
123 "\\s17\\sa60\\sb30\\widctlpar\\qj \\fs22\\cgrid ",
124 "\\sbasedon0 \\snext17 BodyText"
125 },
126 { "DenseText",
127 "\\s18\\widctlpar\\fs22\\cgrid ",
128 "\\sbasedon0 \\snext18 DenseText"
129 },
130 { "Header",
131 "\\s28\\widctlpar\\tqc\\tx4320\\tqr\\tx8640\\adjustright \\fs20\\cgrid ",
132 "\\sbasedon0 \\snext28 header"
133 },
134 { "Footer",
135 "\\s29\\widctlpar\\tqc\\tx4320\\tqr\\tx8640\\qr\\adjustright \\fs20\\cgrid ",
136 "\\sbasedon0 \\snext29 footer"
137 },
138 { "GroupHeader",
139 "\\s30\\li360\\sa60\\sb120\\keepn\\widctlpar\\adjustright \\b\\f1\\fs20\\cgrid ",
140 "\\sbasedon0 \\snext30 GroupHeader"
141 },
142
143 RTF_CodeExample( 0, 40, 41, 0),
144 RTF_CodeExample( 1, 41, 42, 360),
145 RTF_CodeExample( 2, 42, 43, 720),
146 RTF_CodeExample( 3, 43, 44,1080),
147 RTF_CodeExample( 4, 44, 45,1440),
148 RTF_CodeExample( 5, 45, 46,1800),
149 RTF_CodeExample( 6, 46, 47,2160),
150 RTF_CodeExample( 7, 47, 48,2520),
151 RTF_CodeExample( 8, 48, 49,2880),
152 RTF_CodeExample( 9, 49, 50,3240),
153 RTF_CodeExample(10, 50, 51,3600),
154 RTF_CodeExample(11, 51, 52,3960),
155 RTF_CodeExample(12, 52, 53,4320),
156 RTF_CodeExample(13, 53, 53,4680),
157
158 RTF_ListContinue( 0, 60, 61, 0),
159 RTF_ListContinue( 1, 61, 62, 360),
160 RTF_ListContinue( 2, 62, 63, 720),
161 RTF_ListContinue( 3, 63, 64,1080),
162 RTF_ListContinue( 4, 64, 65,1440),
163 RTF_ListContinue( 5, 65, 66,1800),
164 RTF_ListContinue( 6, 66, 67,2160),
165 RTF_ListContinue( 7, 67, 68,2520),
166 RTF_ListContinue( 8, 68, 69,2880),
167 RTF_ListContinue( 9, 69, 70,3240),
168 RTF_ListContinue(10, 70, 71,3600),
169 RTF_ListContinue(11, 71, 72,3960),
170 RTF_ListContinue(12, 72, 73,4320),
171 RTF_ListContinue(13, 73, 73,4680),
172
173 RTF_DescContinue( 0, 80, 81, 0),
174 RTF_DescContinue( 1, 81, 82, 360),
175 RTF_DescContinue( 2, 82, 83, 720),
176 RTF_DescContinue( 3, 83, 84,1080),
177 RTF_DescContinue( 4, 84, 85,1440),
178 RTF_DescContinue( 5, 85, 86,1800),
179 RTF_DescContinue( 6, 86, 87,2160),
180 RTF_DescContinue( 7, 87, 88,2520),
181 RTF_DescContinue( 8, 88, 89,2880),
182 RTF_DescContinue( 9, 89, 90,3240),
183 RTF_DescContinue(10, 90, 91,3600),
184 RTF_DescContinue(11, 91, 92,3960),
185 RTF_DescContinue(12, 92, 93,4320),
186 RTF_DescContinue(13, 93, 93,4680),
187
188 RTF_LatexToc( 0,100,101, 0,30),
189 RTF_LatexToc( 1,101,102, 360,27),
190 RTF_LatexToc( 2,102,103, 720,24),
191 RTF_LatexToc( 3,103,104,1080,21),
192 RTF_LatexToc( 4,104,105,1440,18),
193 RTF_LatexToc( 5,105,106,1800,15),
194 RTF_LatexToc( 6,106,107,2160,12),
195 RTF_LatexToc( 7,107,108,2520, 9),
196 RTF_LatexToc( 8,108,109,2880, 6),
197 RTF_LatexToc( 9,109,110,3240, 3),
198 RTF_LatexToc(10,110,111,3600, 3),
199 RTF_LatexToc(11,111,112,3960, 3),
200 RTF_LatexToc(12,112,113,4320, 3),
201 RTF_LatexToc(13,113,113,4680, 3),
202
203 RTF_ListBullet( 0,120,121, 360, 1),
204 RTF_ListBullet( 1,121,122, 720, 2),
205 RTF_ListBullet( 2,122,123,1080, 3),
206 RTF_ListBullet( 3,123,124,1440, 4),
207 RTF_ListBullet( 4,124,125,1800, 5),
208 RTF_ListBullet( 5,125,126,2160, 6),
209 RTF_ListBullet( 6,126,127,2520, 7),
210 RTF_ListBullet( 7,127,128,2880, 8),
211 RTF_ListBullet( 8,128,129,3240, 9),
212 RTF_ListBullet( 9,129,130,3600,10),
213 RTF_ListBullet(10,130,131,3960,11),
214 RTF_ListBullet(11,131,132,4320,12),
215 RTF_ListBullet(12,132,133,4680,13),
216 RTF_ListBullet(13,133,133,5040,14),
217
218 RTF_ListEnum( 0,140,141, 360),
219 RTF_ListEnum( 1,141,142, 720),
220 RTF_ListEnum( 2,142,143,1080),
221 RTF_ListEnum( 3,143,144,1440),
222 RTF_ListEnum( 4,144,145,1800),
223 RTF_ListEnum( 5,145,146,2160),
224 RTF_ListEnum( 6,146,147,2520),
225 RTF_ListEnum( 7,147,148,2880),
226 RTF_ListEnum( 8,148,149,3240),
227 RTF_ListEnum( 9,149,150,3600),
228 RTF_ListEnum(10,150,151,3960),
229 RTF_ListEnum(11,151,152,4320),
230 RTF_ListEnum(12,152,153,4680),
231 RTF_ListEnum(13,153,153,5040),
232
233 { nullptr,
234 nullptr,
235 nullptr
236 }
237};
238
239#define RTF_ListElement(id,lvl,pos,chr) \
240 { id, lvl, \
241 "\\listlevel\\levelnfc23\\leveljc0\\levelstartat1\\levelfollow0{\\leveltext \\'01\\u"#chr" ?;}{\\levelnumbers;}\\f8\\dbch\\af3\\fi-360\\li"#pos, \
242 "{\\*\\hyphen2\\hyphlead2\\hyphtrail2\\hyphmax0}\\nowidctlpar\\cf0\\hich\\af0\\langfe2052\\dbch\\af0\\afs24\\lang1081\\loch\\f0\\fs24\\lang1033{\\listtext\\pard\\plain \\hich\\af3\\dbch\\af3\\loch\\f8 \\'01\\u"#chr"\\tab}\\ilvl"#lvl"\\ls"#id" \\li0\\ri0\\lin0\\rin0\\fi-360\\tx"#pos"\\li"#pos"\\ri0\\lin"#pos"\\rin0\\fi-360\\kerning1\\hich\\af4\\dbch\\af5\\rtlch \\ltrch\\loch\\fs20" \
243 }
244
246{
247 RTF_ListElement( 1,0, 360, 8226),
248 RTF_ListElement( 1,1, 720, 9702),
249 RTF_ListElement( 1,2,1080, 9642),
250 RTF_ListElement( 1,3,1440, 8226),
251 RTF_ListElement( 1,4,1800, 9702),
252 RTF_ListElement( 1,5,2160, 9642),
253 RTF_ListElement( 1,6,2520, 8662),
254 RTF_ListElement( 1,7,2880, 9702),
255 RTF_ListElement( 1,8,3600, 9642),
256
257 RTF_ListElement( 2,0, 360, 9744),
258 RTF_ListElement( 2,1, 720, 9744),
259 RTF_ListElement( 2,2,1080, 9744),
260 RTF_ListElement( 2,3,1440, 9744),
261 RTF_ListElement( 2,4,1800, 9744),
262 RTF_ListElement( 2,5,2160, 9744),
263 RTF_ListElement( 2,6,2520, 9744),
264 RTF_ListElement( 2,7,2880, 9744),
265 RTF_ListElement( 2,8,3600, 9744),
266
267 RTF_ListElement( 3,0, 360, 9746),
268 RTF_ListElement( 3,1, 720, 9746),
269 RTF_ListElement( 3,2,1080, 9746),
270 RTF_ListElement( 3,3,1440, 9746),
271 RTF_ListElement( 3,4,1800, 9746),
272 RTF_ListElement( 3,5,2160, 9746),
273 RTF_ListElement( 3,6,2520, 9746),
274 RTF_ListElement( 3,7,2880, 9746),
275 RTF_ListElement( 3,8,3600, 9746),
276
277 { 0,
278 0,
279 nullptr,
280 nullptr
281 }
282};
283
284static const reg::Ex s_clause(R"(\\s(\d+)\s*)"); // match, e.g. '\s30' and capture '30'
285
286StyleData::StyleData(const std::string &reference, const std::string &definition)
287{
288 reg::Match match;
289 if (reg::search(reference,match,s_clause))
290 {
291 m_index = static_cast<int>(std::stoul(match[1].str()));
292 }
293 else // error
294 {
295 m_index = 0;
296 }
299}
300
301bool StyleData::setStyle(const std::string &command, const std::string &styleName)
302{
303 reg::Match match;
304 if (!reg::search(command,match,s_clause))
305 {
306 err("Style sheet '%s' contains no '\\s' clause.\n{%s}\n", styleName.c_str(), command.c_str());
307 return false;
308 }
309 m_index = static_cast<int>(std::stoul(match[1].str()));
310
311 size_t index = command.find("\\sbasedon");
312 if (index!=std::string::npos)
313 {
314 m_reference = command.substr(0,index);
315 m_definition = command.substr(index);
316 }
317
318 return true;
319}
320
321
322void loadStylesheet(const QCString &name, StyleDataMap& map)
323{
324 std::ifstream file(name.str());
325 if (!file.is_open())
326 {
327 err("Can't open RTF style sheet file %s. Using defaults.\n",qPrint(name));
328 return;
329 }
330 msg("Loading RTF style sheet %s...\n",qPrint(name));
331
332 uint32_t lineNr=1;
333
334 for (std::string line ; getline(file,line) ; ) // for each line
335 {
336 if (line.empty() || line[0]=='#') continue; // skip blanks & comments
337 static const reg::Ex assignment_splitter(R"(\s*=\s*)");
338 reg::Match match;
339 if (reg::search(line,match,assignment_splitter))
340 {
341 std::string key = match.prefix().str();
342 std::string value = match.suffix().str();
343 auto it = map.find(key);
344 if (it!=map.end())
345 {
346 StyleData& styleData = it->second;
347 styleData.setStyle(value,key);
348 }
349 else
350 {
351 warn(name,lineNr,"Unknown style sheet name %s ignored.",key.data());
352 }
353 }
354 else
355 {
356 warn(name,lineNr,"Assignment of style sheet name expected line='%s'!",line.c_str());
357 }
358 lineNr++;
359 }
360}
361
363
364void loadExtensions(const QCString &name)
365{
366 std::ifstream file(name.str());
367 if (!file.is_open())
368 {
369 err("Can't open RTF extensions file %s. Using defaults.\n",qPrint(name));
370 return;
371 }
372 msg("Loading RTF extensions %s...\n",qPrint(name));
373
374 uint32_t lineNr=1;
375
376 for (std::string line ; getline(file,line) ; ) // for each line
377 {
378 if (line.empty() || line[0]=='#') continue; // skip blanks & comments
379 static const reg::Ex assignment_splitter(R"(\s*=\s*)");
380 reg::Match match;
381 if (reg::search(line,match,assignment_splitter))
382 {
383 std::string key = match.prefix().str();
384 std::string value = match.suffix().str();
385 auto it = g_styleMap.find(key);
386 if (it!=g_styleMap.end())
387 {
388 it->second = value;
389 }
390 else
391 {
392 warn(name,lineNr,"Ignoring unknown extension key '%s'...",key.c_str());
393 }
394 }
395 else
396 {
397 warn(name,lineNr,"Assignment of style sheet name expected!");
398 }
399 lineNr++;
400 }
401}
402
This is an alternative implementation of QCString.
Definition qcstring.h:101
const std::string & str() const
Definition qcstring.h:537
const char * data() const
Returns a pointer to the contents of the string in the form of a 0-terminated C string.
Definition qcstring.h:159
Class representing a regular expression.
Definition regex.h:39
Object representing the matching results.
Definition regex.h:153
void msg(const char *fmt,...)
Definition message.cpp:98
#define warn(file, line, fmt,...)
Definition message.h:59
#define err(fmt,...)
Definition message.h:84
bool search(std::string_view str, Match &match, const Ex &re, size_t pos)
Search in a given string str starting at position pos for a match against regular expression re.
Definition regex.cpp:748
const char * qPrint(const char *s)
Definition qcstring.h:672
QCString rtf_company
Definition rtfstyle.cpp:26
#define RTF_ListEnum(lvl, nest, nxt, pos)
Definition rtfstyle.cpp:64
StyleDataMap rtf_Style
Definition rtfstyle.cpp:362
QCString rtf_documentType
Definition rtfstyle.cpp:30
void loadStylesheet(const QCString &name, StyleDataMap &map)
Definition rtfstyle.cpp:322
Rtf_Style_Default rtf_Style_Default[]
Definition rtfstyle.cpp:88
#define RTF_CodeExample(lvl, nest, nxt, pos)
Definition rtfstyle.cpp:70
char rtf_Style_Reset[]
Definition rtfstyle.cpp:49
void loadExtensions(const QCString &name)
Definition rtfstyle.cpp:364
#define RTF_LatexToc(lvl, nest, nxt, pos, twps)
Definition rtfstyle.cpp:51
static std::map< std::string, QCString & > g_styleMap
Definition rtfstyle.cpp:34
QCString rtf_logoFilename
Definition rtfstyle.cpp:27
static const reg::Ex s_clause(R"(\\s(\d+)\s*)")
QCString rtf_title
Definition rtfstyle.cpp:23
#define RTF_DescContinue(lvl, nest, nxt, pos)
Definition rtfstyle.cpp:82
#define RTF_ListContinue(lvl, nest, nxt, pos)
Definition rtfstyle.cpp:76
Rtf_Table_Default rtf_Table_Default[]
Definition rtfstyle.cpp:245
QCString rtf_keywords
Definition rtfstyle.cpp:32
QCString rtf_author
Definition rtfstyle.cpp:28
#define RTF_ListElement(id, lvl, pos, chr)
Definition rtfstyle.cpp:239
QCString rtf_manager
Definition rtfstyle.cpp:29
QCString rtf_comments
Definition rtfstyle.cpp:25
QCString rtf_subject
Definition rtfstyle.cpp:24
#define RTF_ListBullet(lvl, nest, nxt, pos, lvl2)
Definition rtfstyle.cpp:58
QCString rtf_documentId
Definition rtfstyle.cpp:31
std::map< std::string, StyleData > StyleDataMap
Definition rtfstyle.h:79
StyleData()=default
uint32_t m_index
Definition rtfstyle.h:74
uint32_t index() const
Definition rtfstyle.h:71
bool setStyle(const std::string &command, const std::string &styleName)
Definition rtfstyle.cpp:301
const char * definition() const
Definition rtfstyle.h:70
const char * reference() const
Definition rtfstyle.h:69
std::string m_reference
Definition rtfstyle.h:75
std::string m_definition
Definition rtfstyle.h:76