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