]> git.lyx.org Git - lyx.git/blob - src/TextClass.cpp
affafa6c5cc9c228a7f3848db3228574aa772914
[lyx.git] / src / TextClass.cpp
1 /**
2  * \file TextClass.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Jean-Marc Lasgouttes
8  * \author Angus Leeming
9  * \author John Levon
10  * \author André Pönitz
11  *
12  * Full author contact details are available in file CREDITS.
13  */
14
15 #include <config.h>
16
17 #include "TextClass.h"
18
19 #include "Color.h"
20 #include "Counters.h"
21 #include "support/debug.h"
22 #include "support/gettext.h"
23 #include "Floating.h"
24 #include "FloatList.h"
25 #include "Layout.h"
26 #include "Lexer.h"
27 #include "Font.h"
28
29 #include "frontends/alert.h"
30
31 #include "support/lstrings.h"
32 #include "support/FileName.h"
33 #include "support/filetools.h"
34 #include "support/os.h"
35
36 #include <sstream>
37
38 using namespace std;
39 using namespace lyx::support;
40
41 namespace lyx {
42
43 namespace {
44
45 class LayoutNamesEqual : public unary_function<LayoutPtr, bool> {
46 public:
47         LayoutNamesEqual(docstring const & name)
48                 : name_(name)
49         {}
50         bool operator()(LayoutPtr const & c) const
51         {
52                 return c->name() == name_;
53         }
54 private:
55         docstring name_;
56 };
57
58
59 int const FORMAT = 6;
60
61
62 bool layout2layout(FileName const & filename, FileName const & tempfile)
63 {
64         FileName const script = libFileSearch("scripts", "layout2layout.py");
65         if (script.empty()) {
66                 lyxerr << "Could not find layout conversion "
67                           "script layout2layout.py." << endl;
68                 return false;
69         }
70
71         ostringstream command;
72         command << os::python() << ' ' << quoteName(script.toFilesystemEncoding())
73                 << ' ' << quoteName(filename.toFilesystemEncoding())
74                 << ' ' << quoteName(tempfile.toFilesystemEncoding());
75         string const command_str = command.str();
76
77         LYXERR(Debug::TCLASS, "Running `" << command_str << '\'');
78
79         cmd_ret const ret =
80                 runCommand(command_str);
81         if (ret.first != 0) {
82                 lyxerr << "Could not run layout conversion "
83                           "script layout2layout.py." << endl;
84                 return false;
85         }
86         return true;
87 }
88
89
90 std::string translateRT(TextClass::ReadType rt) 
91 {
92         switch (rt) {
93         case TextClass::BASECLASS:
94                 return "textclass";
95         case TextClass::MERGE:
96                 return "input file";
97         case TextClass::MODULE:
98                 return "module file";
99         }
100         // shutup warning
101         return string();
102 }
103
104 } // namespace anon
105
106
107 TextClass::TextClass(string const & fn, string const & cln,
108                            string const & desc, bool texClassAvail )
109         : name_(fn), latexname_(cln), description_(desc),
110           floatlist_(new FloatList), counters_(new Counters),
111           texClassAvail_(texClassAvail)
112 {
113         modular_ = false;
114         outputType_ = LATEX;
115         columns_ = 1;
116         sides_ = OneSide;
117         secnumdepth_ = 3;
118         tocdepth_ = 3;
119         pagestyle_ = "default";
120         defaultfont_ = sane_font;
121         opt_fontsize_ = "10|11|12";
122         opt_pagestyle_ = "empty|plain|headings|fancy";
123         titletype_ = TITLE_COMMAND_AFTER;
124         titlename_ = "maketitle";
125         loaded_ = false;
126 }
127
128
129 bool TextClass::isTeXClassAvailable() const
130 {
131         return texClassAvail_;
132 }
133
134
135 bool TextClass::readStyle(Lexer & lexrc, Layout & lay)
136 {
137         LYXERR(Debug::TCLASS, "Reading style " << to_utf8(lay.name()));
138         if (!lay.read(lexrc, *this)) {
139                 // Resolve fonts
140                 lay.resfont = lay.font;
141                 lay.resfont.realize(defaultfont());
142                 lay.reslabelfont = lay.labelfont;
143                 lay.reslabelfont.realize(defaultfont());
144                 return false; // no errors
145         }
146         lyxerr << "Error parsing style `" << to_utf8(lay.name()) << '\'' << endl;
147         return true;
148 }
149
150
151 enum TextClassTags {
152         TC_OUTPUTTYPE = 1,
153         TC_INPUT,
154         TC_STYLE,
155         TC_DEFAULTSTYLE,
156         TC_INSETLAYOUT,
157         TC_ENVIRONMENT,
158         TC_NOSTYLE,
159         TC_COLUMNS,
160         TC_SIDES,
161         TC_PAGESTYLE,
162         TC_DEFAULTFONT,
163         TC_SECNUMDEPTH,
164         TC_TOCDEPTH,
165         TC_CLASSOPTIONS,
166         TC_PREAMBLE,
167         TC_PROVIDES,
168         TC_REQUIRES,
169         TC_LEFTMARGIN,
170         TC_RIGHTMARGIN,
171         TC_FLOAT,
172         TC_COUNTER,
173         TC_NOFLOAT,
174         TC_TITLELATEXNAME,
175         TC_TITLELATEXTYPE,
176         TC_FORMAT
177 };
178
179
180 // Reads a textclass structure from file.
181 bool TextClass::read(FileName const & filename, ReadType rt)
182 {
183         if (!filename.isReadableFile()) {
184                 lyxerr << "Cannot read layout file `" << filename << "'."
185                        << endl;
186                 return true;
187         }
188
189         keyword_item textClassTags[] = {
190                 { "classoptions",    TC_CLASSOPTIONS },
191                 { "columns",         TC_COLUMNS },
192                 { "counter",         TC_COUNTER },
193                 { "defaultfont",     TC_DEFAULTFONT },
194                 { "defaultstyle",    TC_DEFAULTSTYLE },
195                 { "environment",     TC_ENVIRONMENT },
196                 { "float",           TC_FLOAT },
197                 { "format",          TC_FORMAT },
198                 { "input",           TC_INPUT },
199                 { "insetlayout",     TC_INSETLAYOUT },
200                 { "leftmargin",      TC_LEFTMARGIN },
201                 { "nofloat",         TC_NOFLOAT },
202                 { "nostyle",         TC_NOSTYLE },
203                 { "outputtype",      TC_OUTPUTTYPE },
204                 { "pagestyle",       TC_PAGESTYLE },
205                 { "preamble",        TC_PREAMBLE },
206                 { "provides",        TC_PROVIDES },
207                 { "requires",        TC_REQUIRES },
208                 { "rightmargin",     TC_RIGHTMARGIN },
209                 { "secnumdepth",     TC_SECNUMDEPTH },
210                 { "sides",           TC_SIDES },
211                 { "style",           TC_STYLE },
212                 { "titlelatexname",  TC_TITLELATEXNAME },
213                 { "titlelatextype",  TC_TITLELATEXTYPE },
214                 { "tocdepth",        TC_TOCDEPTH }
215         };
216
217         LYXERR(Debug::TCLASS, "Reading " + translateRT(rt) + ": " +
218                 to_utf8(makeDisplayPath(filename.absFilename())));
219
220         Lexer lexrc(textClassTags,
221                 sizeof(textClassTags) / sizeof(textClassTags[0]));
222
223         lexrc.setFile(filename);
224         bool error = !lexrc.isOK();
225
226         // Format of files before the 'Format' tag was introduced
227         int format = 1;
228
229         // parsing
230         while (lexrc.isOK() && !error) {
231                 int le = lexrc.lex();
232
233                 switch (le) {
234                 case Lexer::LEX_FEOF:
235                         continue;
236
237                 case Lexer::LEX_UNDEF:
238                         lexrc.printError("Unknown TextClass tag `$$Token'");
239                         error = true;
240                         continue;
241
242                 default:
243                         break;
244                 }
245
246                 switch (static_cast<TextClassTags>(le)) {
247
248                 case TC_FORMAT:
249                         if (lexrc.next())
250                                 format = lexrc.getInteger();
251                         break;
252
253                 case TC_OUTPUTTYPE:   // output type definition
254                         readOutputType(lexrc);
255                         break;
256
257                 case TC_INPUT: // Include file
258                         if (lexrc.next()) {
259                                 string const inc = lexrc.getString();
260                                 FileName tmp = libFileSearch("layouts", inc,
261                                                             "layout");
262
263                                 if (tmp.empty()) {
264                                         lexrc.printError("Could not find input file: " + inc);
265                                         error = true;
266                                 } else if (read(tmp, MERGE)) {
267                                         lexrc.printError("Error reading input"
268                                                          "file: " + tmp.absFilename());
269                                         error = true;
270                                 }
271                         }
272                         break;
273
274                 case TC_DEFAULTSTYLE:
275                         if (lexrc.next()) {
276                                 docstring const name = from_utf8(subst(lexrc.getString(),
277                                                           '_', ' '));
278                                 defaultlayout_ = name;
279                         }
280                         break;
281
282                 case TC_ENVIRONMENT:
283                 case TC_STYLE:
284                         if (lexrc.next()) {
285                                 docstring const name = from_utf8(subst(lexrc.getString(),
286                                                     '_', ' '));
287                                 if (name.empty()) {
288                                         string s = "Could not read name for style: `$$Token' "
289                                                 + lexrc.getString() + " is probably not valid UTF-8!";
290                                         lexrc.printError(s.c_str());
291                                         Layout lay;
292                                         error = readStyle(lexrc, lay);
293                                 } else if (hasLayout(name)) {
294                                         Layout * lay = operator[](name).get();
295                                         error = readStyle(lexrc, *lay);
296                                 } else {
297                                         Layout lay;
298                                         lay.setName(name);
299                                         if (le == TC_ENVIRONMENT)
300                                                 lay.is_environment = true;
301                                         error = readStyle(lexrc, lay);
302                                         if (!error)
303                                                 layoutlist_.push_back(
304                                                         boost::shared_ptr<Layout>(new Layout(lay))
305                                                         );
306
307                                         if (defaultlayout_.empty()) {
308                                                 // We do not have a default
309                                                 // layout yet, so we choose
310                                                 // the first layout we
311                                                 // encounter.
312                                                 defaultlayout_ = name;
313                                         }
314                                 }
315                         }
316                         else {
317                                 lexrc.printError("No name given for style: `$$Token'.");
318                                 error = true;
319                         }
320                         break;
321
322                 case TC_NOSTYLE:
323                         if (lexrc.next()) {
324                                 docstring const style = from_utf8(subst(lexrc.getString(),
325                                                      '_', ' '));
326                                 if (!deleteLayout(style))
327                                         lyxerr << "Cannot delete style `"
328                                                << to_utf8(style) << '\'' << endl;
329 //                                      lexrc.printError("Cannot delete style"
330 //                                                       " `$$Token'");
331                         }
332                         break;
333
334                 case TC_COLUMNS:
335                         if (lexrc.next())
336                                 columns_ = lexrc.getInteger();
337                         break;
338
339                 case TC_SIDES:
340                         if (lexrc.next()) {
341                                 switch (lexrc.getInteger()) {
342                                 case 1: sides_ = OneSide; break;
343                                 case 2: sides_ = TwoSides; break;
344                                 default:
345                                         lyxerr << "Impossible number of page"
346                                                 " sides, setting to one."
347                                                << endl;
348                                         sides_ = OneSide;
349                                         break;
350                                 }
351                         }
352                         break;
353
354                 case TC_PAGESTYLE:
355                         lexrc.next();
356                         pagestyle_ = rtrim(lexrc.getString());
357                         break;
358
359                 case TC_DEFAULTFONT:
360                         defaultfont_ = lyxRead(lexrc);
361                         if (!defaultfont_.resolved()) {
362                                 lexrc.printError("Warning: defaultfont should "
363                                                  "be fully instantiated!");
364                                 defaultfont_.realize(sane_font);
365                         }
366                         break;
367
368                 case TC_SECNUMDEPTH:
369                         lexrc.next();
370                         secnumdepth_ = lexrc.getInteger();
371                         break;
372
373                 case TC_TOCDEPTH:
374                         lexrc.next();
375                         tocdepth_ = lexrc.getInteger();
376                         break;
377
378                         // First step to support options
379                 case TC_CLASSOPTIONS:
380                         readClassOptions(lexrc);
381                         break;
382
383                 case TC_PREAMBLE:
384                         preamble_ = from_utf8(lexrc.getLongString("EndPreamble"));
385                         break;
386
387                 case TC_PROVIDES: {
388                         lexrc.next();
389                         string const feature = lexrc.getString();
390                         lexrc.next();
391                         if (lexrc.getInteger())
392                                 provides_.insert(feature);
393                         else
394                                 provides_.erase(feature);
395                         break;
396                 }
397
398                 case TC_REQUIRES: {
399                         lexrc.eatLine();
400                         vector<string> const req 
401                                 = getVectorFromString(lexrc.getString());
402                         requires_.insert(req.begin(), req.end());
403                         break;
404                 }
405
406                 case TC_LEFTMARGIN:     // left margin type
407                         if (lexrc.next())
408                                 leftmargin_ = lexrc.getDocString();
409                         break;
410
411                 case TC_RIGHTMARGIN:    // right margin type
412                         if (lexrc.next())
413                                 rightmargin_ = lexrc.getDocString();
414                         break;
415                 case TC_INSETLAYOUT:
416                         if (lexrc.next()) {
417                                 docstring const name = subst(lexrc.getDocString(), '_', ' ');
418                                 readInsetLayout(lexrc, name);
419                         }
420                         break;
421                 case TC_FLOAT:
422                         readFloat(lexrc);
423                         break;
424                 case TC_COUNTER:
425                         readCounter(lexrc);
426                         break;
427                 case TC_TITLELATEXTYPE:
428                         readTitleType(lexrc);
429                         break;
430                 case TC_TITLELATEXNAME:
431                         if (lexrc.next())
432                                 titlename_ = lexrc.getString();
433                         break;
434                 case TC_NOFLOAT:
435                         if (lexrc.next()) {
436                                 string const nofloat = lexrc.getString();
437                                 floatlist_->erase(nofloat);
438                         }
439                         break;
440                 }
441                 if (format != FORMAT)
442                         break;
443         }
444
445         if (format != FORMAT) {
446                 LYXERR(Debug::TCLASS, "Converting layout file from format "
447                                       << format << " to " << FORMAT);
448                 FileName const tempfile = FileName::tempName();
449                 error = !layout2layout(filename, tempfile);
450                 if (!error)
451                         error = read(tempfile, rt);
452                 tempfile.removeFile();
453                 return error;
454         }
455
456         LYXERR(Debug::TCLASS, "Finished reading " + translateRT(rt) + ": " +
457                         to_utf8(makeDisplayPath(filename.absFilename())));
458
459         if (rt != BASECLASS) 
460                 return error;
461
462         if (defaultlayout_.empty()) {
463                 lyxerr << "Error: Textclass '" << name_
464                                                 << "' is missing a defaultstyle." << endl;
465                 error = true;
466         }
467                 
468         //Try to erase "stdinsets" from the provides_ set. 
469         //The
470         //  Provides stdinsets 1
471         //declaration simply tells us that the standard insets have been
472         //defined. (It's found in stdinsets.inc but could also be used in
473         //user-defined files.) There isn't really any such package. So we
474         //might as well go ahead and erase it.
475         //If we do not succeed, then it was not there, which means that
476         //the textclass did not provide the definitions of the standard
477         //insets. So we need to try to load them.
478         int erased = provides_.erase("stdinsets");
479         if (!erased) {
480                 FileName tmp = libFileSearch("layouts", "stdinsets.inc");
481
482                 if (tmp.empty()) {
483                         frontend::Alert::warning(_("Missing File"),
484                                         _("Could not find stdinsets.inc! This may lead to data loss!"));
485                         error = true;
486                 } else if (read(tmp, MERGE)) {
487                         frontend::Alert::warning(_("Corrupt File"),
488                                         _("Could not read stdinsets.inc! This may lead to data loss!"));
489                         error = true;
490                 }
491         }
492
493         min_toclevel_ = Layout::NOT_IN_TOC;
494         max_toclevel_ = Layout::NOT_IN_TOC;
495         const_iterator cit = begin();
496         const_iterator the_end = end();
497         for ( ; cit != the_end ; ++cit) {
498                 int const toclevel = (*cit)->toclevel;
499                 if (toclevel != Layout::NOT_IN_TOC) {
500                         if (min_toclevel_ == Layout::NOT_IN_TOC)
501                                 min_toclevel_ = toclevel;
502                         else
503                                 min_toclevel_ = min(min_toclevel_,
504                                                         toclevel);
505                         max_toclevel_ = max(max_toclevel_,
506                                                         toclevel);
507                 }
508         }
509         LYXERR(Debug::TCLASS, "Minimum TocLevel is " << min_toclevel_
510                 << ", maximum is " << max_toclevel_);
511
512         return error;
513 }
514
515
516 void TextClass::readTitleType(Lexer & lexrc)
517 {
518         keyword_item titleTypeTags[] = {
519                 { "commandafter", TITLE_COMMAND_AFTER },
520                 { "environment", TITLE_ENVIRONMENT }
521         };
522
523         PushPopHelper pph(lexrc, titleTypeTags, TITLE_ENVIRONMENT);
524
525         int le = lexrc.lex();
526         switch (le) {
527         case Lexer::LEX_UNDEF:
528                 lexrc.printError("Unknown output type `$$Token'");
529                 return;
530         case TITLE_COMMAND_AFTER:
531         case TITLE_ENVIRONMENT:
532                 titletype_ = static_cast<TitleLatexType>(le);
533                 break;
534         default:
535                 lyxerr << "Unhandled value " << le
536                        << " in TextClass::readTitleType." << endl;
537
538                 break;
539         }
540 }
541
542
543 void TextClass::readOutputType(Lexer & lexrc)
544 {
545         keyword_item outputTypeTags[] = {
546                 { "docbook", DOCBOOK },
547                 { "latex", LATEX },
548                 { "literate", LITERATE }
549         };
550
551         PushPopHelper pph(lexrc, outputTypeTags, LITERATE);
552
553         int le = lexrc.lex();
554         switch (le) {
555         case Lexer::LEX_UNDEF:
556                 lexrc.printError("Unknown output type `$$Token'");
557                 return;
558         case LATEX:
559         case DOCBOOK:
560         case LITERATE:
561                 outputType_ = static_cast<OutputType>(le);
562                 break;
563         default:
564                 lyxerr << "Unhandled value " << le
565                        << " in TextClass::readOutputType." << endl;
566
567                 break;
568         }
569 }
570
571
572 enum ClassOptionsTags {
573         CO_FONTSIZE = 1,
574         CO_PAGESTYLE,
575         CO_OTHER,
576         CO_HEADER,
577         CO_END
578 };
579
580
581 void TextClass::readClassOptions(Lexer & lexrc)
582 {
583         keyword_item classOptionsTags[] = {
584                 {"end", CO_END },
585                 {"fontsize", CO_FONTSIZE },
586                 {"header", CO_HEADER },
587                 {"other", CO_OTHER },
588                 {"pagestyle", CO_PAGESTYLE }
589         };
590
591         lexrc.pushTable(classOptionsTags, CO_END);
592         bool getout = false;
593         while (!getout && lexrc.isOK()) {
594                 int le = lexrc.lex();
595                 switch (le) {
596                 case Lexer::LEX_UNDEF:
597                         lexrc.printError("Unknown ClassOption tag `$$Token'");
598                         continue;
599                 default: break;
600                 }
601                 switch (static_cast<ClassOptionsTags>(le)) {
602                 case CO_FONTSIZE:
603                         lexrc.next();
604                         opt_fontsize_ = rtrim(lexrc.getString());
605                         break;
606                 case CO_PAGESTYLE:
607                         lexrc.next();
608                         opt_pagestyle_ = rtrim(lexrc.getString());
609                         break;
610                 case CO_OTHER:
611                         lexrc.next();
612                         options_ = lexrc.getString();
613                         break;
614                 case CO_HEADER:
615                         lexrc.next();
616                         class_header_ = subst(lexrc.getString(), "&quot;", "\"");
617                         break;
618                 case CO_END:
619                         getout = true;
620                         break;
621                 }
622         }
623         lexrc.popTable();
624 }
625
626
627 enum InsetLayoutTags {
628         IL_FONT = 1,
629         IL_BGCOLOR,
630         IL_DECORATION,
631         IL_FREESPACING,
632         IL_FORCELTR,
633         IL_LABELFONT,
634         IL_LABELSTRING,
635         IL_LATEXNAME,
636         IL_LATEXPARAM,
637         IL_LATEXTYPE,
638         IL_LYXTYPE,
639         IL_KEEPEMPTY,
640         IL_MULTIPAR,
641         IL_NEEDPROTECT,
642         IL_PASSTHRU,
643         IL_PREAMBLE,
644         IL_REQUIRES,
645         IL_END
646 };
647
648
649 void TextClass::readInsetLayout(Lexer & lexrc, docstring const & name)
650 {
651         keyword_item elementTags[] = {
652                 { "bgcolor", IL_BGCOLOR },
653                 { "decoration", IL_DECORATION },
654                 { "end", IL_END },
655                 { "font", IL_FONT },
656                 { "forceltr", IL_FORCELTR },
657                 { "freespacing", IL_FREESPACING },
658                 { "keepempty", IL_KEEPEMPTY },
659                 { "labelfont", IL_LABELFONT },
660                 { "labelstring", IL_LABELSTRING },
661                 { "latexname", IL_LATEXNAME },
662                 { "latexparam", IL_LATEXPARAM },
663                 { "latextype", IL_LATEXTYPE },
664                 { "lyxtype", IL_LYXTYPE },
665                 { "multipar", IL_MULTIPAR },
666                 { "needprotect", IL_NEEDPROTECT },
667                 { "passthru", IL_PASSTHRU },
668                 { "preamble", IL_PREAMBLE },
669                 { "requires", IL_REQUIRES }
670         };
671
672         lexrc.pushTable(elementTags, IL_END);
673
674         string lyxtype;
675         docstring labelstring;
676         string latextype;
677         string decoration;
678         string latexname;
679         string latexparam;
680         FontInfo font = inherit_font;
681         FontInfo labelfont = inherit_font;
682         ColorCode bgcolor(Color_background);
683         string preamble;
684         set<string> requires;
685         bool multipar = false;
686         bool passthru = false;
687         bool needprotect = false;
688         bool keepempty = false;
689         bool freespacing = false;
690         bool forceltr = false;
691
692         bool getout = false;
693         while (!getout && lexrc.isOK()) {
694                 int le = lexrc.lex();
695                 switch (le) {
696                 case Lexer::LEX_UNDEF:
697                         lexrc.printError("Unknown InsetLayout tag `$$Token'");
698                         continue;
699                 default: break;
700                 }
701                 switch (static_cast<InsetLayoutTags>(le)) {
702                 case IL_LYXTYPE:
703                         lexrc.next();
704                         lyxtype = lexrc.getString();
705                         break;
706                 case IL_LATEXTYPE:
707                         lexrc.next();
708                         latextype = lexrc.getString();
709                         break;
710                 case IL_LABELSTRING:
711                         lexrc.next();
712                         labelstring = lexrc.getDocString();
713                         break;
714                 case IL_DECORATION:
715                         lexrc.next();
716                         decoration = lexrc.getString();
717                         break;
718                 case IL_LATEXNAME:
719                         lexrc.next();
720                         latexname = lexrc.getString();
721                         break;
722                 case IL_LATEXPARAM:
723                         lexrc.next();
724                         latexparam = subst(lexrc.getString(), "&quot;", "\"");
725                         break;
726                 case IL_LABELFONT:
727                         labelfont = lyxRead(lexrc, inherit_font);
728                         break;
729                 case IL_FORCELTR:
730                         lexrc.next();
731                         forceltr = lexrc.getBool();
732                         break;
733                 case IL_MULTIPAR:
734                         lexrc.next();
735                         multipar = lexrc.getBool();
736                         break;
737                 case IL_PASSTHRU:
738                         lexrc.next();
739                         passthru = lexrc.getBool();
740                         break;
741                 case IL_KEEPEMPTY:
742                         lexrc.next();
743                         keepempty = lexrc.getBool();
744                         break;
745                 case IL_FREESPACING:
746                         lexrc.next();
747                         freespacing = lexrc.getBool();
748                         break;
749                 case IL_NEEDPROTECT:
750                         lexrc.next();
751                         needprotect = lexrc.getBool();
752                         break;
753                 case IL_FONT:
754                         font = lyxRead(lexrc, inherit_font);
755                         // So: define font before labelfont
756                         labelfont = font;
757                         break;
758                 case IL_BGCOLOR: {
759                         lexrc.next();
760                         string const token = lexrc.getString();
761                         bgcolor = lcolor.getFromLyXName(token);
762                         break;
763                 }
764                 case IL_PREAMBLE:
765                         preamble = lexrc.getLongString("EndPreamble");
766                         break;
767                 case IL_REQUIRES: {
768                         lexrc.eatLine();
769                         vector<string> const req 
770                                 = getVectorFromString(lexrc.getString());
771                         requires.insert(req.begin(), req.end());
772                         break;
773                 }
774                 case IL_END:
775                         getout = true;
776                         break;
777                 }
778         }
779
780         // Here add element to list if getout == true
781         if (getout) {
782                 InsetLayout il;
783                 il.name = to_ascii(name);
784                 il.lyxtype = lyxtype;
785                 il.labelstring = labelstring;
786                 il.decoration = decoration;
787                 il.latextype = latextype;
788                 il.latexname = latexname;
789                 il.latexparam = latexparam;
790                 il.multipar = multipar;
791                 il.passthru = passthru;
792                 il.needprotect = needprotect;
793                 il.freespacing = freespacing;
794                 il.forceltr = forceltr;
795                 il.keepempty = keepempty;
796                 il.font = font;
797                 // The label font is generally used as-is without
798                 // any realization against a given context.
799                 labelfont.realize(sane_font);
800                 il.labelfont = labelfont;
801                 il.bgcolor = bgcolor;
802                 il.preamble = preamble;
803                 il.requires = requires;
804                 insetlayoutlist_[name] = il;
805         }
806
807         lexrc.popTable();
808 }
809
810
811
812 enum FloatTags {
813         FT_TYPE = 1,
814         FT_NAME,
815         FT_PLACEMENT,
816         FT_EXT,
817         FT_WITHIN,
818         FT_STYLE,
819         FT_LISTNAME,
820         FT_BUILTIN,
821         FT_END
822 };
823
824
825 void TextClass::readFloat(Lexer & lexrc)
826 {
827         keyword_item floatTags[] = {
828                 { "end", FT_END },
829                 { "extension", FT_EXT },
830                 { "guiname", FT_NAME },
831                 { "latexbuiltin", FT_BUILTIN },
832                 { "listname", FT_LISTNAME },
833                 { "numberwithin", FT_WITHIN },
834                 { "placement", FT_PLACEMENT },
835                 { "style", FT_STYLE },
836                 { "type", FT_TYPE }
837         };
838
839         lexrc.pushTable(floatTags, FT_END);
840
841         string type;
842         string placement;
843         string ext;
844         string within;
845         string style;
846         string name;
847         string listName;
848         bool builtin = false;
849
850         bool getout = false;
851         while (!getout && lexrc.isOK()) {
852                 int le = lexrc.lex();
853                 switch (le) {
854                 case Lexer::LEX_UNDEF:
855                         lexrc.printError("Unknown float tag `$$Token'");
856                         continue;
857                 default: break;
858                 }
859                 switch (static_cast<FloatTags>(le)) {
860                 case FT_TYPE:
861                         lexrc.next();
862                         type = lexrc.getString();
863                         if (floatlist_->typeExist(type)) {
864                                 Floating const & fl = floatlist_->getType(type);
865                                 placement = fl.placement();
866                                 ext = fl.ext();
867                                 within = fl.within();
868                                 style = fl.style();
869                                 name = fl.name();
870                                 listName = fl.listName();
871                                 builtin = fl.builtin();
872                         } 
873                         break;
874                 case FT_NAME:
875                         lexrc.next();
876                         name = lexrc.getString();
877                         break;
878                 case FT_PLACEMENT:
879                         lexrc.next();
880                         placement = lexrc.getString();
881                         break;
882                 case FT_EXT:
883                         lexrc.next();
884                         ext = lexrc.getString();
885                         break;
886                 case FT_WITHIN:
887                         lexrc.next();
888                         within = lexrc.getString();
889                         if (within == "none")
890                                 within.erase();
891                         break;
892                 case FT_STYLE:
893                         lexrc.next();
894                         style = lexrc.getString();
895                         break;
896                 case FT_LISTNAME:
897                         lexrc.next();
898                         listName = lexrc.getString();
899                         break;
900                 case FT_BUILTIN:
901                         lexrc.next();
902                         builtin = lexrc.getBool();
903                         break;
904                 case FT_END:
905                         getout = true;
906                         break;
907                 }
908         }
909
910         // Here if have a full float if getout == true
911         if (getout) {
912                 Floating fl(type, placement, ext, within,
913                             style, name, listName, builtin);
914                 floatlist_->newFloat(fl);
915                 // each float has its own counter
916                 counters_->newCounter(from_ascii(type), from_ascii(within), 
917                                       docstring(), docstring());
918         }
919
920         lexrc.popTable();
921 }
922
923
924 enum CounterTags {
925         CT_NAME = 1,
926         CT_WITHIN,
927         CT_LABELSTRING,
928         CT_LABELSTRING_APPENDIX,
929         CT_END
930 };
931
932
933 void TextClass::readCounter(Lexer & lexrc)
934 {
935         keyword_item counterTags[] = {
936                 { "end", CT_END },
937                 { "labelstring", CT_LABELSTRING },
938                 { "labelstringappendix", CT_LABELSTRING_APPENDIX },
939                 { "name", CT_NAME },
940                 { "within", CT_WITHIN }
941         };
942
943         lexrc.pushTable(counterTags, CT_END);
944
945         docstring name;
946         docstring within;
947         docstring labelstring;
948         docstring labelstring_appendix;
949
950         bool getout = false;
951         while (!getout && lexrc.isOK()) {
952                 int le = lexrc.lex();
953                 switch (le) {
954                 case Lexer::LEX_UNDEF:
955                         lexrc.printError("Unknown counter tag `$$Token'");
956                         continue;
957                 default: break;
958                 }
959                 switch (static_cast<CounterTags>(le)) {
960                 case CT_NAME:
961                         lexrc.next();
962                         name = lexrc.getDocString();
963                         if (counters_->hasCounter(name))
964                                 LYXERR(Debug::TCLASS, "Reading existing counter " << to_utf8(name));
965                         else
966                                 LYXERR(Debug::TCLASS, "Reading new counter " << to_utf8(name));
967                         break;
968                 case CT_WITHIN:
969                         lexrc.next();
970                         within = lexrc.getDocString();
971                         if (within == "none")
972                                 within.erase();
973                         break;
974                 case CT_LABELSTRING:
975                         lexrc.next();
976                         labelstring = lexrc.getDocString();
977                         labelstring_appendix = labelstring;
978                         break;
979                 case CT_LABELSTRING_APPENDIX:
980                         lexrc.next();
981                         labelstring_appendix = lexrc.getDocString();
982                         break;
983                 case CT_END:
984                         getout = true;
985                         break;
986                 }
987         }
988
989         // Here if have a full counter if getout == true
990         if (getout)
991                 counters_->newCounter(name, within, 
992                                       labelstring, labelstring_appendix);
993
994         lexrc.popTable();
995 }
996
997
998 FontInfo const & TextClass::defaultfont() const
999 {
1000         return defaultfont_;
1001 }
1002
1003
1004 docstring const & TextClass::leftmargin() const
1005 {
1006         return leftmargin_;
1007 }
1008
1009
1010 docstring const & TextClass::rightmargin() const
1011 {
1012         return rightmargin_;
1013 }
1014
1015
1016 bool TextClass::hasLayout(docstring const & n) const
1017 {
1018         docstring const name = n.empty() ? defaultLayoutName() : n;
1019
1020         return find_if(layoutlist_.begin(), layoutlist_.end(),
1021                        LayoutNamesEqual(name))
1022                 != layoutlist_.end();
1023 }
1024
1025
1026
1027 LayoutPtr const & TextClass::operator[](docstring const & name) const
1028 {
1029         BOOST_ASSERT(!name.empty());
1030
1031         LayoutList::const_iterator cit =
1032                 find_if(layoutlist_.begin(),
1033                         layoutlist_.end(),
1034                         LayoutNamesEqual(name));
1035
1036         if (cit == layoutlist_.end()) {
1037                 lyxerr << "We failed to find the layout '" << to_utf8(name)
1038                        << "' in the layout list. You MUST investigate!"
1039                        << endl;
1040                 for (LayoutList::const_iterator it = layoutlist_.begin();
1041                          it != layoutlist_.end(); ++it)
1042                         lyxerr  << " " << to_utf8(it->get()->name()) << endl;
1043
1044                 // we require the name to exist
1045                 BOOST_ASSERT(false);
1046         }
1047
1048         return *cit;
1049 }
1050
1051
1052 bool TextClass::deleteLayout(docstring const & name)
1053 {
1054         if (name == defaultLayoutName())
1055                 return false;
1056
1057         LayoutList::iterator it =
1058                 remove_if(layoutlist_.begin(), layoutlist_.end(),
1059                           LayoutNamesEqual(name));
1060
1061         LayoutList::iterator end = layoutlist_.end();
1062         bool const ret = (it != end);
1063         layoutlist_.erase(it, end);
1064         return ret;
1065 }
1066
1067
1068 // Load textclass info if not loaded yet
1069 bool TextClass::load(string const & path) const
1070 {
1071         if (loaded_)
1072                 return true;
1073
1074         // Read style-file, provided path is searched before the system ones
1075         FileName layout_file;
1076         if (!path.empty())
1077                 layout_file = FileName(addName(path, name_ + ".layout"));
1078         if (layout_file.empty() || !layout_file.exists())
1079                 layout_file = libFileSearch("layouts", name_, "layout");
1080         loaded_ = const_cast<TextClass*>(this)->read(layout_file) == 0;
1081
1082         if (!loaded_) {
1083                 lyxerr << "Error reading `"
1084                        << to_utf8(makeDisplayPath(layout_file.absFilename()))
1085                        << "'\n(Check `" << name_
1086                        << "')\nCheck your installation and "
1087                         "try Options/Reconfigure..." << endl;
1088         }
1089
1090         return loaded_;
1091 }
1092
1093
1094 FloatList & TextClass::floats()
1095 {
1096         return *floatlist_.get();
1097 }
1098
1099
1100 FloatList const & TextClass::floats() const
1101 {
1102         return *floatlist_.get();
1103 }
1104
1105
1106 Counters & TextClass::counters() const
1107 {
1108         return *counters_.get();
1109 }
1110
1111
1112 // Return the layout object of an inset given by name. If the name
1113 // is not found as such, the part after the ':' is stripped off, and
1114 // searched again. In this way, an error fallback can be provided:
1115 // An erroneous 'CharStyle:badname' (e.g., after a documentclass switch)
1116 // will invoke the layout object defined by name = 'CharStyle'.
1117 // If that doesn't work either, an empty object returns (shouldn't
1118 // happen).  -- Idea JMarc, comment MV
1119 InsetLayout const & TextClass::insetlayout(docstring const & name) const 
1120 {
1121         docstring n = name;
1122         while (!n.empty()) {
1123                 if (insetlayoutlist_.count(n) > 0)
1124                         return insetlayoutlist_[n];
1125                 docstring::size_type i = n.find(':');
1126                 if (i == string::npos)
1127                         break;
1128                 n = n.substr(0,i);
1129         }
1130         static InsetLayout empty;
1131         empty.labelstring = from_utf8("UNDEFINED");
1132         empty.labelfont = sane_font;
1133         empty.labelfont.setColor(Color_error);
1134         empty.bgcolor = Color_error;
1135         return empty;
1136 }
1137
1138
1139 docstring const & TextClass::defaultLayoutName() const
1140 {
1141         // This really should come from the actual layout... (Lgb)
1142         return defaultlayout_;
1143 }
1144
1145
1146 LayoutPtr const & TextClass::defaultLayout() const
1147 {
1148         return operator[](defaultLayoutName());
1149 }
1150
1151
1152 string const & TextClass::name() const
1153 {
1154         return name_;
1155 }
1156
1157
1158 string const & TextClass::latexname() const
1159 {
1160         const_cast<TextClass*>(this)->load();
1161         return latexname_;
1162 }
1163
1164
1165 string const & TextClass::description() const
1166 {
1167         return description_;
1168 }
1169
1170
1171 string const & TextClass::opt_fontsize() const
1172 {
1173         return opt_fontsize_;
1174 }
1175
1176
1177 string const & TextClass::opt_pagestyle() const
1178 {
1179         return opt_pagestyle_;
1180 }
1181
1182
1183 string const & TextClass::options() const
1184 {
1185         return options_;
1186 }
1187
1188
1189 string const & TextClass::class_header() const
1190 {
1191         return class_header_;
1192 }
1193
1194
1195 string const & TextClass::pagestyle() const
1196 {
1197         return pagestyle_;
1198 }
1199
1200
1201 docstring const & TextClass::preamble() const
1202 {
1203         return preamble_;
1204 }
1205
1206
1207 PageSides TextClass::sides() const
1208 {
1209         return sides_;
1210 }
1211
1212
1213 int TextClass::secnumdepth() const
1214 {
1215         return secnumdepth_;
1216 }
1217
1218
1219 int TextClass::tocdepth() const
1220 {
1221         return tocdepth_;
1222 }
1223
1224
1225 OutputType TextClass::outputType() const
1226 {
1227         return outputType_;
1228 }
1229
1230
1231 bool TextClass::provides(string const & p) const
1232 {
1233         return provides_.find(p) != provides_.end();
1234 }
1235
1236
1237 unsigned int TextClass::columns() const
1238 {
1239         return columns_;
1240 }
1241
1242
1243 TitleLatexType TextClass::titletype() const
1244 {
1245         return titletype_;
1246 }
1247
1248
1249 string const & TextClass::titlename() const
1250 {
1251         return titlename_;
1252 }
1253
1254
1255 int TextClass::size() const
1256 {
1257         return layoutlist_.size();
1258 }
1259
1260
1261 int TextClass::min_toclevel() const
1262 {
1263         return min_toclevel_;
1264 }
1265
1266
1267 int TextClass::max_toclevel() const
1268 {
1269         return max_toclevel_;
1270 }
1271
1272
1273 bool TextClass::hasTocLevels() const
1274 {
1275         return min_toclevel_ != Layout::NOT_IN_TOC;
1276 }
1277
1278
1279 ostream & operator<<(ostream & os, PageSides p)
1280 {
1281         switch (p) {
1282         case OneSide:
1283                 os << '1';
1284                 break;
1285         case TwoSides:
1286                 os << '2';
1287                 break;
1288         }
1289         return os;
1290 }
1291
1292
1293 } // namespace lyx