]> git.lyx.org Git - lyx.git/blob - src/insets/InsetLayout.cpp
Add ResetsFont tag to InsetLayout, toward fixing bug #5657.
[lyx.git] / src / insets / InsetLayout.cpp
1 // -*- C++ -*-
2 /**
3  * \file InsetLayout.cpp
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Martin Vermeer
8  * \author Richard Heck
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "InsetLayout.h"
16
17 #include "ColorSet.h"
18 #include "Lexer.h"
19 #include "TextClass.h"
20
21 #include "support/debug.h"
22 #include "support/lstrings.h"
23
24 #include <vector>
25
26 using std::string;
27 using std::set;
28 using std::vector;
29
30 namespace lyx {
31
32 InsetLayout::InsetLayout() :
33         name_(from_ascii("undefined")), lyxtype_(STANDARD),
34         labelstring_(from_ascii("UNDEFINED")), contentaslabel_(false),
35         decoration_(DEFAULT), latextype_(NOLATEXTYPE), font_(sane_font), 
36         labelfont_(sane_font), bgcolor_(Color_error), 
37         htmlforcecss_ (false), htmlisblock_(true),
38         multipar_(true), custompars_(true), forceplain_(false), 
39         passthru_(false), parbreakisnewline_(false), freespacing_(false), 
40         keepempty_(false), forceltr_(false), 
41         needprotect_(false), intoc_(false), spellcheck_(true), 
42         resetsfont_(true)
43
44         labelfont_.setColor(Color_error);
45 }
46
47
48 namespace {
49
50 InsetLayout::InsetDecoration translateDecoration(std::string const & str) 
51 {
52         if (support::compare_ascii_no_case(str, "classic") == 0)
53                 return InsetLayout::CLASSIC;
54         if (support::compare_ascii_no_case(str, "minimalistic") == 0)
55                 return InsetLayout::MINIMALISTIC;
56         if (support::compare_ascii_no_case(str, "conglomerate") == 0)
57                 return InsetLayout::CONGLOMERATE;
58         return InsetLayout::DEFAULT;
59 }
60
61 InsetLayout::InsetLaTeXType translateLaTeXType(std::string const & str)
62 {
63         if (support::compare_ascii_no_case(str, "command") == 0)
64                 return InsetLayout::COMMAND;
65         if (support::compare_ascii_no_case(str, "environment") == 0)
66                 return InsetLayout::ENVIRONMENT;
67         if (support::compare_ascii_no_case(str, "none") == 0)
68                 return InsetLayout::NOLATEXTYPE;
69         return InsetLayout::ILT_ERROR;
70 }
71
72 } // namespace anon
73
74
75 bool InsetLayout::read(Lexer & lex, TextClass const & tclass)
76 {
77         enum {
78                 IL_BGCOLOR,
79                 IL_CONTENTASLABEL,
80                 IL_COPYSTYLE,
81                 IL_COUNTER,
82                 IL_CUSTOMPARS,
83                 IL_DECORATION,
84                 IL_FONT,
85                 IL_FORCELTR,
86                 IL_FORCEPLAIN,
87                 IL_FREESPACING,
88                 IL_HTMLTAG,
89                 IL_HTMLATTR,
90                 IL_HTMLFORCECSS,
91                 IL_HTMLINNERTAG,
92                 IL_HTMLINNERATTR,
93                 IL_HTMLISBLOCK,
94                 IL_HTMLLABEL,
95                 IL_HTMLSTYLE,
96                 IL_HTMLPREAMBLE,
97                 IL_INTOC,
98                 IL_LABELFONT,
99                 IL_LABELSTRING,
100                 IL_LATEXNAME,
101                 IL_LATEXPARAM,
102                 IL_LATEXTYPE,
103                 IL_LYXTYPE,
104                 IL_KEEPEMPTY,
105                 IL_MULTIPAR,
106                 IL_NEEDPROTECT,
107                 IL_PASSTHRU,
108                 IL_PARBREAKISNEWLINE,
109                 IL_PREAMBLE,
110                 IL_REQUIRES,
111                 IL_SPELLCHECK,
112                 IL_REFPREFIX,
113                 IL_RESETSFONT,
114                 IL_END
115         };
116
117
118         LexerKeyword elementTags[] = {
119                 { "bgcolor", IL_BGCOLOR },
120                 { "contentaslabel", IL_CONTENTASLABEL },
121                 { "copystyle", IL_COPYSTYLE }, 
122                 { "counter", IL_COUNTER},
123                 { "custompars", IL_CUSTOMPARS },
124                 { "decoration", IL_DECORATION },
125                 { "end", IL_END },
126                 { "font", IL_FONT },
127                 { "forceltr", IL_FORCELTR },
128                 { "forceplain", IL_FORCEPLAIN },
129                 { "freespacing", IL_FREESPACING },
130                 { "htmlattr", IL_HTMLATTR },
131                 { "htmlforcecss", IL_HTMLFORCECSS },
132                 { "htmlinnerattr", IL_HTMLINNERATTR},
133                 { "htmlinnertag", IL_HTMLINNERTAG},
134                 { "htmlisblock", IL_HTMLISBLOCK},
135                 { "htmllabel", IL_HTMLLABEL },
136                 { "htmlpreamble", IL_HTMLPREAMBLE },
137                 { "htmlstyle", IL_HTMLSTYLE },
138                 { "htmltag", IL_HTMLTAG },
139                 { "intoc", IL_INTOC },
140                 { "keepempty", IL_KEEPEMPTY },
141                 { "labelfont", IL_LABELFONT },
142                 { "labelstring", IL_LABELSTRING },
143                 { "latexname", IL_LATEXNAME },
144                 { "latexparam", IL_LATEXPARAM },
145                 { "latextype", IL_LATEXTYPE },
146                 { "lyxtype", IL_LYXTYPE },
147                 { "multipar", IL_MULTIPAR },
148                 { "needprotect", IL_NEEDPROTECT },
149                 { "parbreakisnewline", IL_PARBREAKISNEWLINE },
150                 { "passthru", IL_PASSTHRU },
151                 { "preamble", IL_PREAMBLE },
152                 { "refprefix", IL_REFPREFIX },
153                 { "requires", IL_REQUIRES },
154                 { "resetsfont", IL_RESETSFONT },
155                 { "spellcheck", IL_SPELLCHECK }
156         };
157
158         lex.pushTable(elementTags);
159
160         FontInfo font = inherit_font;
161         labelfont_ = inherit_font;
162         bgcolor_ = Color_none;
163         bool getout = false;
164         // whether we've read the CustomPars or ForcePlain tag
165         // for issuing a warning in case MultiPars comes later
166         bool readCustomOrPlain = false;
167
168         string tmp;     
169         while (!getout && lex.isOK()) {
170                 int le = lex.lex();
171                 switch (le) {
172                 case Lexer::LEX_UNDEF:
173                         lex.printError("Unknown InsetLayout tag");
174                         continue;
175                 default:
176                         break;
177                 }
178                 switch (le) {
179                 // FIXME
180                 // Perhaps a more elegant way to deal with the next two would be the
181                 // way this sort of thing is handled in Layout::read(), namely, by
182                 // using the Lexer.
183                 case IL_LYXTYPE: {
184                         string lt;
185                         lex >> lt;
186                         lyxtype_ = translateLyXType(lt);
187                         if (lyxtype_  == NOLYXTYPE)
188                                 LYXERR0("Unknown LyXType `" << lt << "'.");
189                         if (lyxtype_ == CHARSTYLE)
190                                 multipar_ = false;
191                         break;
192                 }
193                 case IL_LATEXTYPE:  {
194                         string lt;
195                         lex >> lt;
196                         latextype_ = translateLaTeXType(lt);
197                         if (latextype_  == ILT_ERROR)
198                                 LYXERR0("Unknown LaTeXType `" << lt << "'.");
199                         break;
200                 }
201                 case IL_LABELSTRING:
202                         lex >> labelstring_;
203                         break;
204                 case IL_DECORATION:
205                         lex >> tmp;
206                         decoration_ = translateDecoration(tmp);
207                         break;
208                 case IL_LATEXNAME:
209                         lex >> latexname_;
210                         break;
211                 case IL_LATEXPARAM:
212                         lex >> tmp;
213                         latexparam_ = support::subst(tmp, "&quot;", "\"");
214                         break;
215                 case IL_LABELFONT:
216                         labelfont_ = lyxRead(lex, inherit_font);
217                         break;
218                 case IL_FORCELTR:
219                         lex >> forceltr_;
220                         break;
221                 case IL_INTOC:
222                         lex >> intoc_;
223                         break;
224                 case IL_MULTIPAR:
225                         lex >> multipar_;
226                         // the defaults for these depend upon multipar_
227                         if (readCustomOrPlain)
228                                 LYXERR0("Warning: Read MultiPar after CustomPars or ForcePlain. "
229                                         "Previous value may be overwritten!");
230                         readCustomOrPlain = false;
231                         custompars_ = multipar_;
232                         forceplain_ = !multipar_;
233                         break;
234                 case IL_COUNTER:
235                         lex >> counter_;
236                         break;
237                 case IL_CUSTOMPARS:
238                         lex >> custompars_;
239                         readCustomOrPlain = true;
240                         break;
241                 case IL_FORCEPLAIN:
242                         lex >> forceplain_;
243                         readCustomOrPlain = true;
244                         break;
245                 case IL_PASSTHRU:
246                         lex >> passthru_;
247                         break;
248                 case IL_PARBREAKISNEWLINE:
249                         lex >> parbreakisnewline_;
250                         break;
251                 case IL_KEEPEMPTY:
252                         lex >> keepempty_;
253                         break;
254                 case IL_FREESPACING:
255                         lex >> freespacing_;
256                         break;
257                 case IL_NEEDPROTECT:
258                         lex >> needprotect_;
259                         break;
260                 case IL_CONTENTASLABEL:
261                         lex >> contentaslabel_;
262                         break;
263                 case IL_COPYSTYLE: {
264                         // initialize with a known style
265                         docstring style;
266                         lex >> style;
267                         style = support::subst(style, '_', ' ');
268
269                         // We don't want to apply the algorithm in DocumentClass::insetLayout()
270                         // here. So we do it the long way.
271                         TextClass::InsetLayouts::const_iterator it = 
272                                         tclass.insetLayouts().find(style);
273                         if (it != tclass.insetLayouts().end()) {
274                                 docstring const tmpname = name_;
275                                 this->operator=(it->second);
276                                 name_ = tmpname;
277                         } else {
278                                 LYXERR0("Cannot copy unknown InsetLayout `"
279                                         << style << "'\n"
280                                         << "All InsetLayouts so far:");
281                                 TextClass::InsetLayouts::const_iterator lit = 
282                                                 tclass.insetLayouts().begin();
283                                 TextClass::InsetLayouts::const_iterator len = 
284                                                 tclass.insetLayouts().end();
285                                 for (; lit != len; ++lit)
286                                         lyxerr << lit->second.name() << "\n";
287                         }
288                         break;
289                 }
290
291                 case IL_FONT: {
292                         font_ = lyxRead(lex, inherit_font);
293                         // If you want to define labelfont, you need to do so after
294                         // font is defined.
295                         labelfont_ = font_;
296                         break;
297                 }
298                 case IL_BGCOLOR:
299                         lex >> tmp;
300                         bgcolor_ = lcolor.getFromLyXName(tmp);
301                         break;
302                 case IL_PREAMBLE:
303                         preamble_ = from_utf8(lex.getLongString("EndPreamble"));
304                         break;
305                 case IL_REFPREFIX:
306                         lex >> refprefix_;
307                         break;
308                 case IL_HTMLTAG:
309                         lex >> htmltag_;
310                         break;
311                 case IL_HTMLATTR:
312                         lex >> htmlattr_;
313                         break;
314                 case IL_HTMLFORCECSS:
315                         lex >> htmlforcecss_;
316                         break;
317                 case IL_HTMLINNERTAG:
318                         lex >> htmlinnertag_;
319                         break;
320                 case IL_HTMLINNERATTR:
321                         lex >> htmlinnerattr_;
322                         break;
323                 case IL_HTMLLABEL:
324                         lex >> htmllabel_;
325                         break;
326                 case IL_HTMLISBLOCK:
327                         lex >> htmlisblock_;
328                         break;
329                 case IL_HTMLSTYLE:
330                         htmlstyle_ = from_utf8(lex.getLongString("EndHTMLStyle"));
331                         break;
332                 case IL_HTMLPREAMBLE:
333                         htmlpreamble_ = from_utf8(lex.getLongString("EndPreamble"));
334                         break;
335                 case IL_REQUIRES: {
336                         lex.eatLine();
337                         vector<string> const req 
338                                 = support::getVectorFromString(lex.getString());
339                         requires_.insert(req.begin(), req.end());
340                         break;
341                 }
342                 case IL_SPELLCHECK:
343                         lex >> spellcheck_;
344                         break;
345                 case IL_RESETSFONT:
346                         lex >> resetsfont_;
347                         break;
348                 case IL_END:
349                         getout = true;
350                         break;
351                 }
352         }
353
354         // Here add element to list if getout == true
355         if (!getout)
356                 return false;
357         
358         // The label font is generally used as-is without
359         // any realization against a given context.
360         labelfont_.realize(sane_font);
361
362         lex.popTable();
363         return true;
364 }
365
366
367 InsetLayout::InsetLyXType translateLyXType(std::string const & str) 
368 {
369         
370         if (support::compare_ascii_no_case(str, "charstyle") == 0)
371                 return InsetLayout::CHARSTYLE;
372         if (support::compare_ascii_no_case(str, "custom") == 0)
373                 return InsetLayout::CUSTOM;
374         if (support::compare_ascii_no_case(str, "element") == 0)
375                 return InsetLayout::ELEMENT;
376         if (support::compare_ascii_no_case(str, "end") == 0)
377                 return InsetLayout::END;
378         if (support::compare_ascii_no_case(str, "standard") == 0)
379                 return InsetLayout::STANDARD;
380         return InsetLayout::NOLYXTYPE;
381 }
382
383
384 string const & InsetLayout::htmltag() const
385 {
386         if (htmltag_.empty())
387                 htmltag_ = multipar_ ? "div" : "span";
388         return htmltag_; 
389 }
390
391
392 string const & InsetLayout::htmlattr() const
393 {
394         if (htmlattr_.empty())
395                 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
396         return htmlattr_; 
397 }
398
399
400 string const & InsetLayout::htmlinnerattr() const
401 {
402         if (htmlinnerattr_.empty())
403                 htmlinnerattr_ = "class=\"" + defaultCSSClass() + "_inner\"";
404         return htmlinnerattr_; 
405 }
406
407
408 string InsetLayout::defaultCSSClass() const
409
410         if (!defaultcssclass_.empty())
411                 return defaultcssclass_;
412         string d;
413         string n = to_utf8(name());
414         string::const_iterator it = n.begin();
415         string::const_iterator en = n.end();
416         for (; it != en; ++it) {
417                 if (!isalpha(*it))
418                         d += "_";
419                 else if (islower(*it))
420                         d += *it;
421                 else
422                         d += support::lowercase(*it);
423         }
424         // are there other characters we need to remove?
425         defaultcssclass_ = d;
426         return defaultcssclass_;
427 }
428
429
430 void InsetLayout::makeDefaultCSS() const
431 {
432         if (!htmldefaultstyle_.empty()) 
433                 return;
434         docstring const mainfontCSS = font_.asCSS();
435         if (!mainfontCSS.empty())
436                 htmldefaultstyle_ = 
437                                 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
438                                 mainfontCSS + from_ascii("\n}\n");
439 }
440
441
442 docstring InsetLayout::htmlstyle() const 
443
444         if (!htmlstyle_.empty() && !htmlforcecss_)
445                 return htmlstyle_;
446         if (htmldefaultstyle_.empty())
447                 makeDefaultCSS();
448         docstring retval = htmldefaultstyle_;
449         if (!htmlstyle_.empty())
450                 retval += '\n' + htmlstyle_ + '\n';
451         return retval;
452 }
453
454
455 } //namespace lyx