]> git.lyx.org Git - lyx.git/blob - src/TextClass.cpp
ab92d33eb51d89b4b2939dfc22e3033938d8ba13
[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                         vector<string> const req 
399                                 = getVectorFromString(lexrc.getString());
400                         requires_.insert(req.begin(), req.end());
401                         break;
402                 }
403
404                 case TC_LEFTMARGIN:     // left margin type
405                         if (lexrc.next())
406                                 leftmargin_ = lexrc.getDocString();
407                         break;
408
409                 case TC_RIGHTMARGIN:    // right margin type
410                         if (lexrc.next())
411                                 rightmargin_ = lexrc.getDocString();
412                         break;
413                 case TC_INSETLAYOUT:
414                         if (lexrc.next()) {
415                                 docstring const name = subst(lexrc.getDocString(), '_', ' ');
416                                 readInsetLayout(lexrc, name);
417                         }
418                         break;
419                 case TC_FLOAT:
420                         readFloat(lexrc);
421                         break;
422                 case TC_COUNTER:
423                         readCounter(lexrc);
424                         break;
425                 case TC_TITLELATEXTYPE:
426                         readTitleType(lexrc);
427                         break;
428                 case TC_TITLELATEXNAME:
429                         if (lexrc.next())
430                                 titlename_ = lexrc.getString();
431                         break;
432                 case TC_NOFLOAT:
433                         if (lexrc.next()) {
434                                 string const nofloat = lexrc.getString();
435                                 floatlist_->erase(nofloat);
436                         }
437                         break;
438                 }
439                 if (format != FORMAT)
440                         break;
441         }
442
443         if (format != FORMAT) {
444                 LYXERR(Debug::TCLASS, "Converting layout file from format "
445                                       << format << " to " << FORMAT);
446                 FileName const tempfile = FileName::tempName();
447                 error = !layout2layout(filename, tempfile);
448                 if (!error)
449                         error = read(tempfile, rt);
450                 tempfile.removeFile();
451                 return error;
452         }
453
454         if (rt == MODULE) 
455                 LYXERR(Debug::TCLASS, "Finished reading module file "
456                                 << to_utf8(makeDisplayPath(filename.absFilename())));
457         else if (rt == MERGE)
458                 LYXERR(Debug::TCLASS, "Finished reading input file "
459                                 << to_utf8(makeDisplayPath(filename.absFilename())));
460         else { // we are at top level here.
461                 LYXERR(Debug::TCLASS, "Finished reading textclass "
462                                       << to_utf8(makeDisplayPath(filename.absFilename())));
463                 if (defaultlayout_.empty()) {
464                         lyxerr << "Error: Textclass '" << name_
465                                << "' is missing a defaultstyle." << endl;
466                         error = true;
467                 }
468
469                 min_toclevel_ = Layout::NOT_IN_TOC;
470                 max_toclevel_ = Layout::NOT_IN_TOC;
471                 const_iterator cit = begin();
472                 const_iterator the_end = end();
473                 for ( ; cit != the_end ; ++cit) {
474                         int const toclevel = (*cit)->toclevel;
475                         if (toclevel != Layout::NOT_IN_TOC) {
476                                 if (min_toclevel_ == Layout::NOT_IN_TOC)
477                                         min_toclevel_ = toclevel;
478                                 else
479                                         min_toclevel_ = min(min_toclevel_,
480                                                          toclevel);
481                                 max_toclevel_ = max(max_toclevel_,
482                                                          toclevel);
483                         }
484                 }
485                 LYXERR(Debug::TCLASS, "Minimum TocLevel is " << min_toclevel_
486                         << ", maximum is " << max_toclevel_);
487
488         }
489
490         return error;
491 }
492
493
494 void TextClass::readTitleType(Lexer & lexrc)
495 {
496         keyword_item titleTypeTags[] = {
497                 { "commandafter", TITLE_COMMAND_AFTER },
498                 { "environment", TITLE_ENVIRONMENT }
499         };
500
501         PushPopHelper pph(lexrc, titleTypeTags, TITLE_ENVIRONMENT);
502
503         int le = lexrc.lex();
504         switch (le) {
505         case Lexer::LEX_UNDEF:
506                 lexrc.printError("Unknown output type `$$Token'");
507                 return;
508         case TITLE_COMMAND_AFTER:
509         case TITLE_ENVIRONMENT:
510                 titletype_ = static_cast<TitleLatexType>(le);
511                 break;
512         default:
513                 lyxerr << "Unhandled value " << le
514                        << " in TextClass::readTitleType." << endl;
515
516                 break;
517         }
518 }
519
520
521 void TextClass::readOutputType(Lexer & lexrc)
522 {
523         keyword_item outputTypeTags[] = {
524                 { "docbook", DOCBOOK },
525                 { "latex", LATEX },
526                 { "literate", LITERATE }
527         };
528
529         PushPopHelper pph(lexrc, outputTypeTags, LITERATE);
530
531         int le = lexrc.lex();
532         switch (le) {
533         case Lexer::LEX_UNDEF:
534                 lexrc.printError("Unknown output type `$$Token'");
535                 return;
536         case LATEX:
537         case DOCBOOK:
538         case LITERATE:
539                 outputType_ = static_cast<OutputType>(le);
540                 break;
541         default:
542                 lyxerr << "Unhandled value " << le
543                        << " in TextClass::readOutputType." << endl;
544
545                 break;
546         }
547 }
548
549
550 enum ClassOptionsTags {
551         CO_FONTSIZE = 1,
552         CO_PAGESTYLE,
553         CO_OTHER,
554         CO_HEADER,
555         CO_END
556 };
557
558
559 void TextClass::readClassOptions(Lexer & lexrc)
560 {
561         keyword_item classOptionsTags[] = {
562                 {"end", CO_END },
563                 {"fontsize", CO_FONTSIZE },
564                 {"header", CO_HEADER },
565                 {"other", CO_OTHER },
566                 {"pagestyle", CO_PAGESTYLE }
567         };
568
569         lexrc.pushTable(classOptionsTags, CO_END);
570         bool getout = false;
571         while (!getout && lexrc.isOK()) {
572                 int le = lexrc.lex();
573                 switch (le) {
574                 case Lexer::LEX_UNDEF:
575                         lexrc.printError("Unknown ClassOption tag `$$Token'");
576                         continue;
577                 default: break;
578                 }
579                 switch (static_cast<ClassOptionsTags>(le)) {
580                 case CO_FONTSIZE:
581                         lexrc.next();
582                         opt_fontsize_ = rtrim(lexrc.getString());
583                         break;
584                 case CO_PAGESTYLE:
585                         lexrc.next();
586                         opt_pagestyle_ = rtrim(lexrc.getString());
587                         break;
588                 case CO_OTHER:
589                         lexrc.next();
590                         options_ = lexrc.getString();
591                         break;
592                 case CO_HEADER:
593                         lexrc.next();
594                         class_header_ = subst(lexrc.getString(), "&quot;", "\"");
595                         break;
596                 case CO_END:
597                         getout = true;
598                         break;
599                 }
600         }
601         lexrc.popTable();
602 }
603
604
605 enum InsetLayoutTags {
606         IL_FONT = 1,
607         IL_BGCOLOR,
608         IL_DECORATION,
609         IL_FREESPACING,
610         IL_FORCELTR,
611         IL_LABELFONT,
612         IL_LABELSTRING,
613         IL_LATEXNAME,
614         IL_LATEXPARAM,
615         IL_LATEXTYPE,
616         IL_LYXTYPE,
617         IL_KEEPEMPTY,
618         IL_MULTIPAR,
619         IL_NEEDPROTECT,
620         IL_PASSTHRU,
621         IL_PREAMBLE,
622         IL_REQUIRES,
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                 { "requires", IL_REQUIRES }
648         };
649
650         lexrc.pushTable(elementTags, IL_END);
651
652         string lyxtype;
653         docstring labelstring;
654         string latextype;
655         string decoration;
656         string latexname;
657         string latexparam;
658         FontInfo font = inherit_font;
659         FontInfo labelfont = inherit_font;
660         ColorCode bgcolor(Color_background);
661         string preamble;
662         set<string> requires;
663         bool multipar = false;
664         bool passthru = false;
665         bool needprotect = false;
666         bool keepempty = false;
667         bool freespacing = false;
668         bool forceltr = false;
669
670         bool getout = false;
671         while (!getout && lexrc.isOK()) {
672                 int le = lexrc.lex();
673                 switch (le) {
674                 case Lexer::LEX_UNDEF:
675                         lexrc.printError("Unknown InsetLayout tag `$$Token'");
676                         continue;
677                 default: break;
678                 }
679                 switch (static_cast<InsetLayoutTags>(le)) {
680                 case IL_LYXTYPE:
681                         lexrc.next();
682                         lyxtype = lexrc.getString();
683                         break;
684                 case IL_LATEXTYPE:
685                         lexrc.next();
686                         latextype = lexrc.getString();
687                         break;
688                 case IL_LABELSTRING:
689                         lexrc.next();
690                         labelstring = lexrc.getDocString();
691                         break;
692                 case IL_DECORATION:
693                         lexrc.next();
694                         decoration = lexrc.getString();
695                         break;
696                 case IL_LATEXNAME:
697                         lexrc.next();
698                         latexname = lexrc.getString();
699                         break;
700                 case IL_LATEXPARAM:
701                         lexrc.next();
702                         latexparam = subst(lexrc.getString(), "&quot;", "\"");
703                         break;
704                 case IL_LABELFONT:
705                         labelfont = lyxRead(lexrc, inherit_font);
706                         break;
707                 case IL_FORCELTR:
708                         lexrc.next();
709                         forceltr = lexrc.getBool();
710                         break;
711                 case IL_MULTIPAR:
712                         lexrc.next();
713                         multipar = lexrc.getBool();
714                         break;
715                 case IL_PASSTHRU:
716                         lexrc.next();
717                         passthru = lexrc.getBool();
718                         break;
719                 case IL_KEEPEMPTY:
720                         lexrc.next();
721                         keepempty = lexrc.getBool();
722                         break;
723                 case IL_FREESPACING:
724                         lexrc.next();
725                         freespacing = lexrc.getBool();
726                         break;
727                 case IL_NEEDPROTECT:
728                         lexrc.next();
729                         needprotect = lexrc.getBool();
730                         break;
731                 case IL_FONT:
732                         font = lyxRead(lexrc, inherit_font);
733                         // So: define font before labelfont
734                         labelfont = font;
735                         break;
736                 case IL_BGCOLOR: {
737                         lexrc.next();
738                         string const token = lexrc.getString();
739                         bgcolor = lcolor.getFromLyXName(token);
740                         break;
741                 }
742                 case IL_PREAMBLE:
743                         preamble = lexrc.getLongString("EndPreamble");
744                         break;
745                 case IL_REQUIRES: {
746                         lexrc.eatLine();
747                         vector<string> const req 
748                                 = getVectorFromString(lexrc.getString());
749                         requires.insert(req.begin(), req.end());
750                         break;
751                 }
752                 case IL_END:
753                         getout = true;
754                         break;
755                 }
756         }
757
758         // Here add element to list if getout == true
759         if (getout) {
760                 InsetLayout il;
761                 il.name = to_ascii(name);
762                 il.lyxtype = lyxtype;
763                 il.labelstring = labelstring;
764                 il.decoration = decoration;
765                 il.latextype = latextype;
766                 il.latexname = latexname;
767                 il.latexparam = latexparam;
768                 il.multipar = multipar;
769                 il.passthru = passthru;
770                 il.needprotect = needprotect;
771                 il.freespacing = freespacing;
772                 il.forceltr = forceltr;
773                 il.keepempty = keepempty;
774                 il.font = font;
775                 // The label font is generally used as-is without
776                 // any realization against a given context.
777                 labelfont.realize(sane_font);
778                 il.labelfont = labelfont;
779                 il.bgcolor = bgcolor;
780                 il.preamble = preamble;
781                 il.requires = requires;
782                 insetlayoutlist_[name] = il;
783         }
784
785         lexrc.popTable();
786 }
787
788
789
790 enum FloatTags {
791         FT_TYPE = 1,
792         FT_NAME,
793         FT_PLACEMENT,
794         FT_EXT,
795         FT_WITHIN,
796         FT_STYLE,
797         FT_LISTNAME,
798         FT_BUILTIN,
799         FT_END
800 };
801
802
803 void TextClass::readFloat(Lexer & lexrc)
804 {
805         keyword_item floatTags[] = {
806                 { "end", FT_END },
807                 { "extension", FT_EXT },
808                 { "guiname", FT_NAME },
809                 { "latexbuiltin", FT_BUILTIN },
810                 { "listname", FT_LISTNAME },
811                 { "numberwithin", FT_WITHIN },
812                 { "placement", FT_PLACEMENT },
813                 { "style", FT_STYLE },
814                 { "type", FT_TYPE }
815         };
816
817         lexrc.pushTable(floatTags, FT_END);
818
819         string type;
820         string placement;
821         string ext;
822         string within;
823         string style;
824         string name;
825         string listName;
826         bool builtin = false;
827
828         bool getout = false;
829         while (!getout && lexrc.isOK()) {
830                 int le = lexrc.lex();
831                 switch (le) {
832                 case Lexer::LEX_UNDEF:
833                         lexrc.printError("Unknown float tag `$$Token'");
834                         continue;
835                 default: break;
836                 }
837                 switch (static_cast<FloatTags>(le)) {
838                 case FT_TYPE:
839                         lexrc.next();
840                         type = lexrc.getString();
841                         if (floatlist_->typeExist(type)) {
842                                 Floating const & fl = floatlist_->getType(type);
843                                 placement = fl.placement();
844                                 ext = fl.ext();
845                                 within = fl.within();
846                                 style = fl.style();
847                                 name = fl.name();
848                                 listName = fl.listName();
849                                 builtin = fl.builtin();
850                         } 
851                         break;
852                 case FT_NAME:
853                         lexrc.next();
854                         name = lexrc.getString();
855                         break;
856                 case FT_PLACEMENT:
857                         lexrc.next();
858                         placement = lexrc.getString();
859                         break;
860                 case FT_EXT:
861                         lexrc.next();
862                         ext = lexrc.getString();
863                         break;
864                 case FT_WITHIN:
865                         lexrc.next();
866                         within = lexrc.getString();
867                         if (within == "none")
868                                 within.erase();
869                         break;
870                 case FT_STYLE:
871                         lexrc.next();
872                         style = lexrc.getString();
873                         break;
874                 case FT_LISTNAME:
875                         lexrc.next();
876                         listName = lexrc.getString();
877                         break;
878                 case FT_BUILTIN:
879                         lexrc.next();
880                         builtin = lexrc.getBool();
881                         break;
882                 case FT_END:
883                         getout = true;
884                         break;
885                 }
886         }
887
888         // Here if have a full float if getout == true
889         if (getout) {
890                 Floating fl(type, placement, ext, within,
891                             style, name, listName, builtin);
892                 floatlist_->newFloat(fl);
893                 // each float has its own counter
894                 counters_->newCounter(from_ascii(type), from_ascii(within), 
895                                       docstring(), docstring());
896         }
897
898         lexrc.popTable();
899 }
900
901
902 enum CounterTags {
903         CT_NAME = 1,
904         CT_WITHIN,
905         CT_LABELSTRING,
906         CT_LABELSTRING_APPENDIX,
907         CT_END
908 };
909
910
911 void TextClass::readCounter(Lexer & lexrc)
912 {
913         keyword_item counterTags[] = {
914                 { "end", CT_END },
915                 { "labelstring", CT_LABELSTRING },
916                 { "labelstringappendix", CT_LABELSTRING_APPENDIX },
917                 { "name", CT_NAME },
918                 { "within", CT_WITHIN }
919         };
920
921         lexrc.pushTable(counterTags, CT_END);
922
923         docstring name;
924         docstring within;
925         docstring labelstring;
926         docstring labelstring_appendix;
927
928         bool getout = false;
929         while (!getout && lexrc.isOK()) {
930                 int le = lexrc.lex();
931                 switch (le) {
932                 case Lexer::LEX_UNDEF:
933                         lexrc.printError("Unknown counter tag `$$Token'");
934                         continue;
935                 default: break;
936                 }
937                 switch (static_cast<CounterTags>(le)) {
938                 case CT_NAME:
939                         lexrc.next();
940                         name = lexrc.getDocString();
941                         if (counters_->hasCounter(name))
942                                 LYXERR(Debug::TCLASS, "Reading existing counter " << to_utf8(name));
943                         else
944                                 LYXERR(Debug::TCLASS, "Reading new counter " << to_utf8(name));
945                         break;
946                 case CT_WITHIN:
947                         lexrc.next();
948                         within = lexrc.getDocString();
949                         if (within == "none")
950                                 within.erase();
951                         break;
952                 case CT_LABELSTRING:
953                         lexrc.next();
954                         labelstring = lexrc.getDocString();
955                         labelstring_appendix = labelstring;
956                         break;
957                 case CT_LABELSTRING_APPENDIX:
958                         lexrc.next();
959                         labelstring_appendix = lexrc.getDocString();
960                         break;
961                 case CT_END:
962                         getout = true;
963                         break;
964                 }
965         }
966
967         // Here if have a full counter if getout == true
968         if (getout)
969                 counters_->newCounter(name, within, 
970                                       labelstring, labelstring_appendix);
971
972         lexrc.popTable();
973 }
974
975
976 FontInfo const & TextClass::defaultfont() const
977 {
978         return defaultfont_;
979 }
980
981
982 docstring const & TextClass::leftmargin() const
983 {
984         return leftmargin_;
985 }
986
987
988 docstring const & TextClass::rightmargin() const
989 {
990         return rightmargin_;
991 }
992
993
994 bool TextClass::hasLayout(docstring const & n) const
995 {
996         docstring const name = n.empty() ? defaultLayoutName() : n;
997
998         return find_if(layoutlist_.begin(), layoutlist_.end(),
999                        LayoutNamesEqual(name))
1000                 != layoutlist_.end();
1001 }
1002
1003
1004
1005 LayoutPtr const & TextClass::operator[](docstring const & name) const
1006 {
1007         BOOST_ASSERT(!name.empty());
1008
1009         LayoutList::const_iterator cit =
1010                 find_if(layoutlist_.begin(),
1011                         layoutlist_.end(),
1012                         LayoutNamesEqual(name));
1013
1014         if (cit == layoutlist_.end()) {
1015                 lyxerr << "We failed to find the layout '" << to_utf8(name)
1016                        << "' in the layout list. You MUST investigate!"
1017                        << endl;
1018                 for (LayoutList::const_iterator it = layoutlist_.begin();
1019                          it != layoutlist_.end(); ++it)
1020                         lyxerr  << " " << to_utf8(it->get()->name()) << endl;
1021
1022                 // we require the name to exist
1023                 BOOST_ASSERT(false);
1024         }
1025
1026         return *cit;
1027 }
1028
1029
1030 bool TextClass::deleteLayout(docstring const & name)
1031 {
1032         if (name == defaultLayoutName())
1033                 return false;
1034
1035         LayoutList::iterator it =
1036                 remove_if(layoutlist_.begin(), layoutlist_.end(),
1037                           LayoutNamesEqual(name));
1038
1039         LayoutList::iterator end = layoutlist_.end();
1040         bool const ret = (it != end);
1041         layoutlist_.erase(it, end);
1042         return ret;
1043 }
1044
1045
1046 // Load textclass info if not loaded yet
1047 bool TextClass::load(string const & path) const
1048 {
1049         if (loaded_)
1050                 return true;
1051
1052         // Read style-file, provided path is searched before the system ones
1053         FileName layout_file;
1054         if (!path.empty())
1055                 layout_file = FileName(addName(path, name_ + ".layout"));
1056         if (layout_file.empty() || !layout_file.exists())
1057                 layout_file = libFileSearch("layouts", name_, "layout");
1058         loaded_ = const_cast<TextClass*>(this)->read(layout_file) == 0;
1059
1060         if (!loaded_) {
1061                 lyxerr << "Error reading `"
1062                        << to_utf8(makeDisplayPath(layout_file.absFilename()))
1063                        << "'\n(Check `" << name_
1064                        << "')\nCheck your installation and "
1065                         "try Options/Reconfigure..." << endl;
1066         }
1067
1068         return loaded_;
1069 }
1070
1071
1072 FloatList & TextClass::floats()
1073 {
1074         return *floatlist_.get();
1075 }
1076
1077
1078 FloatList const & TextClass::floats() const
1079 {
1080         return *floatlist_.get();
1081 }
1082
1083
1084 Counters & TextClass::counters() const
1085 {
1086         return *counters_.get();
1087 }
1088
1089
1090 // Return the layout object of an inset given by name. If the name
1091 // is not found as such, the part after the ':' is stripped off, and
1092 // searched again. In this way, an error fallback can be provided:
1093 // An erroneous 'CharStyle:badname' (e.g., after a documentclass switch)
1094 // will invoke the layout object defined by name = 'CharStyle'.
1095 // If that doesn't work either, an empty object returns (shouldn't
1096 // happen).  -- Idea JMarc, comment MV
1097 InsetLayout const & TextClass::insetlayout(docstring const & name) const 
1098 {
1099         docstring n = name;
1100         while (!n.empty()) {
1101                 if (insetlayoutlist_.count(n) > 0)
1102                         return insetlayoutlist_[n];
1103                 docstring::size_type i = n.find(':');
1104                 if (i == string::npos)
1105                         break;
1106                 n = n.substr(0,i);
1107         }
1108         static InsetLayout empty;
1109         empty.labelstring = from_utf8("UNDEFINED");
1110         empty.labelfont = sane_font;
1111         empty.labelfont.setColor(Color_error);
1112         empty.bgcolor = Color_error;
1113         return empty;
1114 }
1115
1116
1117 docstring const & TextClass::defaultLayoutName() const
1118 {
1119         // This really should come from the actual layout... (Lgb)
1120         return defaultlayout_;
1121 }
1122
1123
1124 LayoutPtr const & TextClass::defaultLayout() const
1125 {
1126         return operator[](defaultLayoutName());
1127 }
1128
1129
1130 string const & TextClass::name() const
1131 {
1132         return name_;
1133 }
1134
1135
1136 string const & TextClass::latexname() const
1137 {
1138         const_cast<TextClass*>(this)->load();
1139         return latexname_;
1140 }
1141
1142
1143 string const & TextClass::description() const
1144 {
1145         return description_;
1146 }
1147
1148
1149 string const & TextClass::opt_fontsize() const
1150 {
1151         return opt_fontsize_;
1152 }
1153
1154
1155 string const & TextClass::opt_pagestyle() const
1156 {
1157         return opt_pagestyle_;
1158 }
1159
1160
1161 string const & TextClass::options() const
1162 {
1163         return options_;
1164 }
1165
1166
1167 string const & TextClass::class_header() const
1168 {
1169         return class_header_;
1170 }
1171
1172
1173 string const & TextClass::pagestyle() const
1174 {
1175         return pagestyle_;
1176 }
1177
1178
1179 docstring const & TextClass::preamble() const
1180 {
1181         return preamble_;
1182 }
1183
1184
1185 PageSides TextClass::sides() const
1186 {
1187         return sides_;
1188 }
1189
1190
1191 int TextClass::secnumdepth() const
1192 {
1193         return secnumdepth_;
1194 }
1195
1196
1197 int TextClass::tocdepth() const
1198 {
1199         return tocdepth_;
1200 }
1201
1202
1203 OutputType TextClass::outputType() const
1204 {
1205         return outputType_;
1206 }
1207
1208
1209 bool TextClass::provides(string const & p) const
1210 {
1211         return provides_.find(p) != provides_.end();
1212 }
1213
1214
1215 unsigned int TextClass::columns() const
1216 {
1217         return columns_;
1218 }
1219
1220
1221 TitleLatexType TextClass::titletype() const
1222 {
1223         return titletype_;
1224 }
1225
1226
1227 string const & TextClass::titlename() const
1228 {
1229         return titlename_;
1230 }
1231
1232
1233 int TextClass::size() const
1234 {
1235         return layoutlist_.size();
1236 }
1237
1238
1239 int TextClass::min_toclevel() const
1240 {
1241         return min_toclevel_;
1242 }
1243
1244
1245 int TextClass::max_toclevel() const
1246 {
1247         return max_toclevel_;
1248 }
1249
1250
1251 bool TextClass::hasTocLevels() const
1252 {
1253         return min_toclevel_ != Layout::NOT_IN_TOC;
1254 }
1255
1256
1257 ostream & operator<<(ostream & os, PageSides p)
1258 {
1259         switch (p) {
1260         case OneSide:
1261                 os << '1';
1262                 break;
1263         case TwoSides:
1264                 os << '2';
1265                 break;
1266         }
1267         return os;
1268 }
1269
1270
1271 } // namespace lyx