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