]> git.lyx.org Git - lyx.git/blob - src/insets/InsetLayout.cpp
Cocoa based Qt-4.6 needs to paint every character separately to match metrics computa...
[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), display_(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_DISPLAY,
85                 IL_FONT,
86                 IL_FORCELTR,
87                 IL_FORCEPLAIN,
88                 IL_FREESPACING,
89                 IL_HTMLTAG,
90                 IL_HTMLATTR,
91                 IL_HTMLFORCECSS,
92                 IL_HTMLINNERTAG,
93                 IL_HTMLINNERATTR,
94                 IL_HTMLISBLOCK,
95                 IL_HTMLLABEL,
96                 IL_HTMLSTYLE,
97                 IL_HTMLPREAMBLE,
98                 IL_INTOC,
99                 IL_LABELFONT,
100                 IL_LABELSTRING,
101                 IL_LATEXNAME,
102                 IL_LATEXPARAM,
103                 IL_LATEXTYPE,
104                 IL_LYXTYPE,
105                 IL_KEEPEMPTY,
106                 IL_MULTIPAR,
107                 IL_NEEDPROTECT,
108                 IL_PASSTHRU,
109                 IL_PARBREAKISNEWLINE,
110                 IL_PREAMBLE,
111                 IL_REQUIRES,
112                 IL_SPELLCHECK,
113                 IL_REFPREFIX,
114                 IL_RESETSFONT,
115                 IL_END
116         };
117
118
119         LexerKeyword elementTags[] = {
120                 { "bgcolor", IL_BGCOLOR },
121                 { "contentaslabel", IL_CONTENTASLABEL },
122                 { "copystyle", IL_COPYSTYLE }, 
123                 { "counter", IL_COUNTER},
124                 { "custompars", IL_CUSTOMPARS },
125                 { "decoration", IL_DECORATION },
126                 { "display", IL_DISPLAY },
127                 { "end", IL_END },
128                 { "font", IL_FONT },
129                 { "forceltr", IL_FORCELTR },
130                 { "forceplain", IL_FORCEPLAIN },
131                 { "freespacing", IL_FREESPACING },
132                 { "htmlattr", IL_HTMLATTR },
133                 { "htmlforcecss", IL_HTMLFORCECSS },
134                 { "htmlinnerattr", IL_HTMLINNERATTR},
135                 { "htmlinnertag", IL_HTMLINNERTAG},
136                 { "htmlisblock", IL_HTMLISBLOCK},
137                 { "htmllabel", IL_HTMLLABEL },
138                 { "htmlpreamble", IL_HTMLPREAMBLE },
139                 { "htmlstyle", IL_HTMLSTYLE },
140                 { "htmltag", IL_HTMLTAG },
141                 { "intoc", IL_INTOC },
142                 { "keepempty", IL_KEEPEMPTY },
143                 { "labelfont", IL_LABELFONT },
144                 { "labelstring", IL_LABELSTRING },
145                 { "latexname", IL_LATEXNAME },
146                 { "latexparam", IL_LATEXPARAM },
147                 { "latextype", IL_LATEXTYPE },
148                 { "lyxtype", IL_LYXTYPE },
149                 { "multipar", IL_MULTIPAR },
150                 { "needprotect", IL_NEEDPROTECT },
151                 { "parbreakisnewline", IL_PARBREAKISNEWLINE },
152                 { "passthru", IL_PASSTHRU },
153                 { "preamble", IL_PREAMBLE },
154                 { "refprefix", IL_REFPREFIX },
155                 { "requires", IL_REQUIRES },
156                 { "resetsfont", IL_RESETSFONT },
157                 { "spellcheck", IL_SPELLCHECK }
158         };
159
160         lex.pushTable(elementTags);
161
162         FontInfo font = inherit_font;
163         labelfont_ = inherit_font;
164         bgcolor_ = Color_none;
165         bool getout = false;
166         // whether we've read the CustomPars or ForcePlain tag
167         // for issuing a warning in case MultiPars comes later
168         bool readCustomOrPlain = false;
169
170         string tmp;     
171         while (!getout && lex.isOK()) {
172                 int le = lex.lex();
173                 switch (le) {
174                 case Lexer::LEX_UNDEF:
175                         lex.printError("Unknown InsetLayout tag");
176                         continue;
177                 default:
178                         break;
179                 }
180                 switch (le) {
181                 // FIXME
182                 // Perhaps a more elegant way to deal with the next two would be the
183                 // way this sort of thing is handled in Layout::read(), namely, by
184                 // using the Lexer.
185                 case IL_LYXTYPE: {
186                         // make sure that we have the right sort of name.
187                         if (name_ != from_ascii("undefined")
188                             && name_.substr(0,5) != from_ascii("Flex:")) {
189                                 LYXERR0("Flex insets must have names of the form `Flex:<name>'.\n"
190                                         "This one has the name `" << to_utf8(name_) << "'\n"
191                                         "Ignoring LyXType declaration.");
192                                 break;
193                         }
194                         string lt;
195                         lex >> lt;
196                         lyxtype_ = translateLyXType(lt);
197                         if (lyxtype_  == NOLYXTYPE)
198                                 LYXERR0("Unknown LyXType `" << lt << "'.");
199                         if (lyxtype_ == CHARSTYLE)
200                                 multipar_ = false;
201                         break;
202                 }
203                 case IL_LATEXTYPE:  {
204                         string lt;
205                         lex >> lt;
206                         latextype_ = translateLaTeXType(lt);
207                         if (latextype_  == ILT_ERROR)
208                                 LYXERR0("Unknown LaTeXType `" << lt << "'.");
209                         break;
210                 }
211                 case IL_LABELSTRING:
212                         lex >> labelstring_;
213                         break;
214                 case IL_DECORATION:
215                         lex >> tmp;
216                         decoration_ = translateDecoration(tmp);
217                         break;
218                 case IL_LATEXNAME:
219                         lex >> latexname_;
220                         break;
221                 case IL_LATEXPARAM:
222                         lex >> tmp;
223                         latexparam_ = support::subst(tmp, "&quot;", "\"");
224                         break;
225                 case IL_LABELFONT:
226                         labelfont_ = lyxRead(lex, inherit_font);
227                         break;
228                 case IL_FORCELTR:
229                         lex >> forceltr_;
230                         break;
231                 case IL_INTOC:
232                         lex >> intoc_;
233                         break;
234                 case IL_MULTIPAR:
235                         lex >> multipar_;
236                         // the defaults for these depend upon multipar_
237                         if (readCustomOrPlain)
238                                 LYXERR0("Warning: Read MultiPar after CustomPars or ForcePlain. "
239                                         "Previous value may be overwritten!");
240                         readCustomOrPlain = false;
241                         custompars_ = multipar_;
242                         forceplain_ = !multipar_;
243                         break;
244                 case IL_COUNTER:
245                         lex >> counter_;
246                         break;
247                 case IL_CUSTOMPARS:
248                         lex >> custompars_;
249                         readCustomOrPlain = true;
250                         break;
251                 case IL_FORCEPLAIN:
252                         lex >> forceplain_;
253                         readCustomOrPlain = true;
254                         break;
255                 case IL_PASSTHRU:
256                         lex >> passthru_;
257                         break;
258                 case IL_PARBREAKISNEWLINE:
259                         lex >> parbreakisnewline_;
260                         break;
261                 case IL_KEEPEMPTY:
262                         lex >> keepempty_;
263                         break;
264                 case IL_FREESPACING:
265                         lex >> freespacing_;
266                         break;
267                 case IL_NEEDPROTECT:
268                         lex >> needprotect_;
269                         break;
270                 case IL_CONTENTASLABEL:
271                         lex >> contentaslabel_;
272                         break;
273                 case IL_COPYSTYLE: {
274                         // initialize with a known style
275                         docstring style;
276                         lex >> style;
277                         style = support::subst(style, '_', ' ');
278
279                         // We don't want to apply the algorithm in DocumentClass::insetLayout()
280                         // here. So we do it the long way.
281                         TextClass::InsetLayouts::const_iterator it = 
282                                         tclass.insetLayouts().find(style);
283                         if (it != tclass.insetLayouts().end()) {
284                                 docstring const tmpname = name_;
285                                 this->operator=(it->second);
286                                 name_ = tmpname;
287                         } else {
288                                 LYXERR0("Cannot copy unknown InsetLayout `"
289                                         << style << "'\n"
290                                         << "All InsetLayouts so far:");
291                                 TextClass::InsetLayouts::const_iterator lit = 
292                                                 tclass.insetLayouts().begin();
293                                 TextClass::InsetLayouts::const_iterator len = 
294                                                 tclass.insetLayouts().end();
295                                 for (; lit != len; ++lit)
296                                         lyxerr << lit->second.name() << "\n";
297                         }
298                         break;
299                 }
300
301                 case IL_FONT: {
302                         font_ = lyxRead(lex, inherit_font);
303                         // If you want to define labelfont, you need to do so after
304                         // font is defined.
305                         labelfont_ = font_;
306                         break;
307                 }
308                 case IL_BGCOLOR:
309                         lex >> tmp;
310                         bgcolor_ = lcolor.getFromLyXName(tmp);
311                         break;
312                 case IL_PREAMBLE:
313                         preamble_ = from_utf8(lex.getLongString("EndPreamble"));
314                         break;
315                 case IL_REFPREFIX:
316                         lex >> refprefix_;
317                         break;
318                 case IL_HTMLTAG:
319                         lex >> htmltag_;
320                         break;
321                 case IL_HTMLATTR:
322                         lex >> htmlattr_;
323                         break;
324                 case IL_HTMLFORCECSS:
325                         lex >> htmlforcecss_;
326                         break;
327                 case IL_HTMLINNERTAG:
328                         lex >> htmlinnertag_;
329                         break;
330                 case IL_HTMLINNERATTR:
331                         lex >> htmlinnerattr_;
332                         break;
333                 case IL_HTMLLABEL:
334                         lex >> htmllabel_;
335                         break;
336                 case IL_HTMLISBLOCK:
337                         lex >> htmlisblock_;
338                         break;
339                 case IL_HTMLSTYLE:
340                         htmlstyle_ = from_utf8(lex.getLongString("EndHTMLStyle"));
341                         break;
342                 case IL_HTMLPREAMBLE:
343                         htmlpreamble_ = from_utf8(lex.getLongString("EndPreamble"));
344                         break;
345                 case IL_REQUIRES: {
346                         lex.eatLine();
347                         vector<string> const req 
348                                 = support::getVectorFromString(lex.getString());
349                         requires_.insert(req.begin(), req.end());
350                         break;
351                 }
352                 case IL_SPELLCHECK:
353                         lex >> spellcheck_;
354                         break;
355                 case IL_RESETSFONT:
356                         lex >> resetsfont_;
357                         break;
358                 case IL_DISPLAY:
359                         lex >> display_;
360                         break;
361                 case IL_END:
362                         getout = true;
363                         break;
364                 }
365         }
366
367         // Here add element to list if getout == true
368         if (!getout)
369                 return false;
370         
371         // The label font is generally used as-is without
372         // any realization against a given context.
373         labelfont_.realize(sane_font);
374
375         lex.popTable();
376         return true;
377 }
378
379
380 InsetLayout::InsetLyXType translateLyXType(std::string const & str) 
381 {
382         
383         if (support::compare_ascii_no_case(str, "charstyle") == 0)
384                 return InsetLayout::CHARSTYLE;
385         if (support::compare_ascii_no_case(str, "custom") == 0)
386                 return InsetLayout::CUSTOM;
387         if (support::compare_ascii_no_case(str, "element") == 0)
388                 return InsetLayout::ELEMENT;
389         if (support::compare_ascii_no_case(str, "end") == 0)
390                 return InsetLayout::END;
391         if (support::compare_ascii_no_case(str, "standard") == 0)
392                 return InsetLayout::STANDARD;
393         return InsetLayout::NOLYXTYPE;
394 }
395
396
397 string const & InsetLayout::htmltag() const
398 {
399         if (htmltag_.empty())
400                 htmltag_ = multipar_ ? "div" : "span";
401         return htmltag_; 
402 }
403
404
405 string const & InsetLayout::htmlattr() const
406 {
407         if (htmlattr_.empty())
408                 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
409         return htmlattr_; 
410 }
411
412
413 string const & InsetLayout::htmlinnerattr() const
414 {
415         if (htmlinnerattr_.empty())
416                 htmlinnerattr_ = "class=\"" + defaultCSSClass() + "_inner\"";
417         return htmlinnerattr_; 
418 }
419
420
421 string InsetLayout::defaultCSSClass() const
422
423         if (!defaultcssclass_.empty())
424                 return defaultcssclass_;
425         string d;
426         string n = to_utf8(name());
427         string::const_iterator it = n.begin();
428         string::const_iterator en = n.end();
429         for (; it != en; ++it) {
430                 if (!isalpha(*it))
431                         d += "_";
432                 else if (islower(*it))
433                         d += *it;
434                 else
435                         d += support::lowercase(*it);
436         }
437         // are there other characters we need to remove?
438         defaultcssclass_ = d;
439         return defaultcssclass_;
440 }
441
442
443 void InsetLayout::makeDefaultCSS() const
444 {
445         if (!htmldefaultstyle_.empty()) 
446                 return;
447         docstring const mainfontCSS = font_.asCSS();
448         if (!mainfontCSS.empty())
449                 htmldefaultstyle_ = 
450                                 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
451                                 mainfontCSS + from_ascii("\n}\n");
452 }
453
454
455 docstring InsetLayout::htmlstyle() const 
456
457         if (!htmlstyle_.empty() && !htmlforcecss_)
458                 return htmlstyle_;
459         if (htmldefaultstyle_.empty())
460                 makeDefaultCSS();
461         docstring retval = htmldefaultstyle_;
462         if (!htmlstyle_.empty())
463                 retval += '\n' + htmlstyle_ + '\n';
464         return retval;
465 }
466
467
468 } //namespace lyx