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