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