3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Jean-Marc Lasgouttes
8 * \author Angus Leeming
10 * \author André Pönitz
12 * Full author contact details are available in file CREDITS.
17 #include "TextClass.h"
24 #include "FloatList.h"
28 #include "frontends/alert.h"
30 #include "support/lstrings.h"
31 #include "support/lyxlib.h"
32 #include "support/filetools.h"
33 #include "support/os.h"
45 using support::FileName;
46 using support::libFileSearch;
47 using support::makeDisplayPath;
48 using support::quoteName;
51 using support::addName;
53 extern FontInfo lyxRead(Lexer &, FontInfo const & fi = sane_font);
57 class LayoutNamesEqual : public std::unary_function<LayoutPtr, bool> {
59 LayoutNamesEqual(docstring const & name)
62 bool operator()(LayoutPtr const & c) const
64 return c->name() == name_;
74 bool layout2layout(FileName const & filename, FileName const & tempfile)
76 FileName const script = libFileSearch("scripts", "layout2layout.py");
78 lyxerr << "Could not find layout conversion "
79 "script layout2layout.py." << endl;
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();
89 LYXERR(Debug::TCLASS, "Running `" << command_str << '\'');
91 support::cmd_ret const ret =
92 support::runCommand(command_str);
94 lyxerr << "Could not run layout conversion "
95 "script layout2layout.py." << endl;
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)
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";
126 bool TextClass::isTeXClassAvailable() const
128 return texClassAvail_;
132 bool TextClass::readStyle(Lexer & lexrc, Layout & lay)
134 LYXERR(Debug::TCLASS, "Reading style " << to_utf8(lay.name()));
135 if (!lay.read(lexrc, *this)) {
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
143 lyxerr << "Error parsing style `" << to_utf8(lay.name()) << '\'' << endl;
176 // Reads a textclass structure from file.
177 bool TextClass::read(FileName const & filename, ReadType rt)
179 if (!filename.isFileReadable()) {
180 lyxerr << "Cannot read layout file `" << filename << "'."
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 }
214 LYXERR(Debug::TCLASS, "Reading textclass ");
217 LYXERR(Debug::TCLASS, "Reading input file ");
220 LYXERR(Debug::TCLASS, "Reading module file ");
225 LYXERR(Debug::TCLASS, to_utf8(makeDisplayPath(filename.absFilename())));
227 Lexer lexrc(textClassTags,
228 sizeof(textClassTags) / sizeof(textClassTags[0]));
230 lexrc.setFile(filename);
231 bool error = !lexrc.isOK();
233 // Format of files before the 'Format' tag was introduced
237 while (lexrc.isOK() && !error) {
238 int le = lexrc.lex();
241 case Lexer::LEX_FEOF:
244 case Lexer::LEX_UNDEF:
245 lexrc.printError("Unknown TextClass tag `$$Token'");
253 switch (static_cast<TextClassTags>(le)) {
257 format = lexrc.getInteger();
260 case TC_OUTPUTTYPE: // output type definition
261 readOutputType(lexrc);
264 case TC_INPUT: // Include file
266 string const inc = lexrc.getString();
267 FileName tmp = libFileSearch("layouts", inc,
271 lexrc.printError("Could not find input"
274 } else if (read(tmp, MERGE)) {
275 lexrc.printError("Error reading input"
276 "file: " + tmp.absFilename());
282 case TC_DEFAULTSTYLE:
284 docstring const name = from_utf8(subst(lexrc.getString(),
286 defaultlayout_ = name;
293 docstring const name = from_utf8(subst(lexrc.getString(),
296 string s = "Could not read name for style: `$$Token' "
297 + lexrc.getString() + " is probably not valid UTF-8!";
298 lexrc.printError(s.c_str());
300 error = readStyle(lexrc, lay);
301 } else if (hasLayout(name)) {
302 Layout * lay = operator[](name).get();
303 error = readStyle(lexrc, *lay);
307 if (le == TC_ENVIRONMENT)
308 lay.is_environment = true;
309 error = readStyle(lexrc, lay);
311 layoutlist_.push_back(
312 boost::shared_ptr<Layout>(new Layout(lay))
315 if (defaultlayout_.empty()) {
316 // We do not have a default
317 // layout yet, so we choose
318 // the first layout we
320 defaultlayout_ = name;
325 lexrc.printError("No name given for style: `$$Token'.");
332 docstring const style = from_utf8(subst(lexrc.getString(),
334 if (!deleteLayout(style))
335 lyxerr << "Cannot delete style `"
336 << to_utf8(style) << '\'' << endl;
337 // lexrc.printError("Cannot delete style"
344 columns_ = lexrc.getInteger();
349 switch (lexrc.getInteger()) {
350 case 1: sides_ = OneSide; break;
351 case 2: sides_ = TwoSides; break;
353 lyxerr << "Impossible number of page"
354 " sides, setting to one."
364 pagestyle_ = rtrim(lexrc.getString());
368 defaultfont_ = lyxRead(lexrc);
369 if (!defaultfont_.resolved()) {
370 lexrc.printError("Warning: defaultfont should "
371 "be fully instantiated!");
372 defaultfont_.realize(sane_font);
378 secnumdepth_ = lexrc.getInteger();
383 tocdepth_ = lexrc.getInteger();
386 // First step to support options
387 case TC_CLASSOPTIONS:
388 readClassOptions(lexrc);
392 preamble_ = from_utf8(lexrc.getLongString("EndPreamble"));
397 string const feature = lexrc.getString();
399 if (lexrc.getInteger())
400 provides_.insert(feature);
402 provides_.erase(feature);
406 case TC_LEFTMARGIN: // left margin type
408 leftmargin_ = lexrc.getDocString();
411 case TC_RIGHTMARGIN: // right margin type
413 rightmargin_ = lexrc.getDocString();
417 docstring const name = subst(lexrc.getDocString(), '_', ' ');
418 readInsetLayout(lexrc, name);
427 case TC_TITLELATEXTYPE:
428 readTitleType(lexrc);
430 case TC_TITLELATEXNAME:
432 titlename_ = lexrc.getString();
436 string const nofloat = lexrc.getString();
437 floatlist_->erase(nofloat);
441 if (format != FORMAT)
445 if (format != FORMAT) {
446 LYXERR(Debug::TCLASS, "Converting layout file from format "
447 << format << " to " << FORMAT);
448 FileName const tempfile(support::tempName());
449 error = !layout2layout(filename, tempfile);
451 error = read(tempfile, rt);
452 support::unlink(tempfile);
457 LYXERR(Debug::TCLASS, "Finished reading module file "
458 << to_utf8(makeDisplayPath(filename.absFilename())));
459 else if (rt == MERGE)
460 LYXERR(Debug::TCLASS, "Finished reading input file "
461 << to_utf8(makeDisplayPath(filename.absFilename())));
462 else { // we are at top level here.
463 LYXERR(Debug::TCLASS, "Finished reading textclass "
464 << to_utf8(makeDisplayPath(filename.absFilename())));
465 if (defaultlayout_.empty()) {
466 lyxerr << "Error: Textclass '" << name_
467 << "' is missing a defaultstyle." << endl;
471 min_toclevel_ = Layout::NOT_IN_TOC;
472 max_toclevel_ = Layout::NOT_IN_TOC;
473 const_iterator cit = begin();
474 const_iterator the_end = end();
475 for ( ; cit != the_end ; ++cit) {
476 int const toclevel = (*cit)->toclevel;
477 if (toclevel != Layout::NOT_IN_TOC) {
478 if (min_toclevel_ == Layout::NOT_IN_TOC)
479 min_toclevel_ = toclevel;
481 min_toclevel_ = std::min(min_toclevel_,
483 max_toclevel_ = std::max(max_toclevel_,
487 LYXERR(Debug::TCLASS, "Minimum TocLevel is " << min_toclevel_
488 << ", maximum is " << max_toclevel_);
496 void TextClass::readTitleType(Lexer & lexrc)
498 keyword_item titleTypeTags[] = {
499 { "commandafter", TITLE_COMMAND_AFTER },
500 { "environment", TITLE_ENVIRONMENT }
503 PushPopHelper pph(lexrc, titleTypeTags, TITLE_ENVIRONMENT);
505 int le = lexrc.lex();
507 case Lexer::LEX_UNDEF:
508 lexrc.printError("Unknown output type `$$Token'");
510 case TITLE_COMMAND_AFTER:
511 case TITLE_ENVIRONMENT:
512 titletype_ = static_cast<TitleLatexType>(le);
515 lyxerr << "Unhandled value " << le
516 << " in TextClass::readTitleType." << endl;
523 void TextClass::readOutputType(Lexer & lexrc)
525 keyword_item outputTypeTags[] = {
526 { "docbook", DOCBOOK },
528 { "literate", LITERATE }
531 PushPopHelper pph(lexrc, outputTypeTags, LITERATE);
533 int le = lexrc.lex();
535 case Lexer::LEX_UNDEF:
536 lexrc.printError("Unknown output type `$$Token'");
541 outputType_ = static_cast<OutputType>(le);
544 lyxerr << "Unhandled value " << le
545 << " in TextClass::readOutputType." << endl;
552 enum ClassOptionsTags {
561 void TextClass::readClassOptions(Lexer & lexrc)
563 keyword_item classOptionsTags[] = {
565 {"fontsize", CO_FONTSIZE },
566 {"header", CO_HEADER },
567 {"other", CO_OTHER },
568 {"pagestyle", CO_PAGESTYLE }
571 lexrc.pushTable(classOptionsTags, CO_END);
573 while (!getout && lexrc.isOK()) {
574 int le = lexrc.lex();
576 case Lexer::LEX_UNDEF:
577 lexrc.printError("Unknown ClassOption tag `$$Token'");
581 switch (static_cast<ClassOptionsTags>(le)) {
584 opt_fontsize_ = rtrim(lexrc.getString());
588 opt_pagestyle_ = rtrim(lexrc.getString());
592 options_ = lexrc.getString();
596 class_header_ = subst(lexrc.getString(), """, "\"");
607 enum InsetLayoutTags {
628 void TextClass::readInsetLayout(Lexer & lexrc, docstring const & name)
630 keyword_item elementTags[] = {
631 { "bgcolor", IL_BGCOLOR },
632 { "decoration", IL_DECORATION },
635 { "forceltr", IL_FORCELTR },
636 { "freespacing", IL_FREESPACING },
637 { "keepempty", IL_KEEPEMPTY },
638 { "labelfont", IL_LABELFONT },
639 { "labelstring", IL_LABELSTRING },
640 { "latexname", IL_LATEXNAME },
641 { "latexparam", IL_LATEXPARAM },
642 { "latextype", IL_LATEXTYPE },
643 { "lyxtype", IL_LYXTYPE },
644 { "multipar", IL_MULTIPAR },
645 { "needprotect", IL_NEEDPROTECT },
646 { "passthru", IL_PASSTHRU },
647 { "preamble", IL_PREAMBLE }
650 lexrc.pushTable(elementTags, IL_END);
653 docstring labelstring;
658 FontInfo font = inherit_font;
659 FontInfo labelfont = inherit_font;
660 ColorCode bgcolor(Color_background);
662 bool multipar = false;
663 bool passthru = false;
664 bool needprotect = false;
665 bool keepempty = false;
666 bool freespacing = false;
667 bool forceltr = false;
670 while (!getout && lexrc.isOK()) {
671 int le = lexrc.lex();
673 case Lexer::LEX_UNDEF:
674 lexrc.printError("Unknown ClassOption tag `$$Token'");
678 switch (static_cast<InsetLayoutTags>(le)) {
681 lyxtype = lexrc.getString();
685 latextype = lexrc.getString();
689 labelstring = lexrc.getDocString();
693 decoration = lexrc.getString();
697 latexname = lexrc.getString();
701 latexparam = subst(lexrc.getString(), """, "\"");
704 labelfont = lyxRead(lexrc, inherit_font);
708 forceltr = lexrc.getBool();
712 multipar = lexrc.getBool();
716 passthru = lexrc.getBool();
720 keepempty = lexrc.getBool();
724 freespacing = lexrc.getBool();
728 needprotect = lexrc.getBool();
731 font = lyxRead(lexrc, inherit_font);
732 // So: define font before labelfont
737 string const token = lexrc.getString();
738 bgcolor = lcolor.getFromLyXName(token);
742 preamble = lexrc.getLongString("EndPreamble");
750 // Here add element to list if getout == true
753 il.name = to_ascii(name);
754 il.lyxtype = lyxtype;
755 il.labelstring = labelstring;
756 il.decoration = decoration;
757 il.latextype = latextype;
758 il.latexname = latexname;
759 il.latexparam = latexparam;
760 il.multipar = multipar;
761 il.passthru = passthru;
762 il.needprotect = needprotect;
763 il.freespacing = freespacing;
764 il.forceltr = forceltr;
765 il.keepempty = keepempty;
767 // The label font is generally used as-is without
768 // any realization against a given context.
769 labelfont.realize(sane_font);
770 il.labelfont = labelfont;
771 il.bgcolor = bgcolor;
772 il.preamble = preamble;
773 insetlayoutlist_[name] = il;
794 void TextClass::readFloat(Lexer & lexrc)
796 keyword_item floatTags[] = {
798 { "extension", FT_EXT },
799 { "guiname", FT_NAME },
800 { "latexbuiltin", FT_BUILTIN },
801 { "listname", FT_LISTNAME },
802 { "numberwithin", FT_WITHIN },
803 { "placement", FT_PLACEMENT },
804 { "style", FT_STYLE },
808 lexrc.pushTable(floatTags, FT_END);
817 bool builtin = false;
820 while (!getout && lexrc.isOK()) {
821 int le = lexrc.lex();
823 case Lexer::LEX_UNDEF:
824 lexrc.printError("Unknown ClassOption tag `$$Token'");
828 switch (static_cast<FloatTags>(le)) {
831 type = lexrc.getString();
832 if (floatlist_->typeExist(type)) {
833 Floating const & fl = floatlist_->getType(type);
834 placement = fl.placement();
836 within = fl.within();
839 listName = fl.listName();
840 builtin = fl.builtin();
845 name = lexrc.getString();
849 placement = lexrc.getString();
853 ext = lexrc.getString();
857 within = lexrc.getString();
858 if (within == "none")
863 style = lexrc.getString();
867 listName = lexrc.getString();
871 builtin = lexrc.getBool();
879 // Here if have a full float if getout == true
881 Floating fl(type, placement, ext, within,
882 style, name, listName, builtin);
883 floatlist_->newFloat(fl);
884 // each float has its own counter
885 counters_->newCounter(from_ascii(type), from_ascii(within),
886 docstring(), docstring());
897 CT_LABELSTRING_APPENDIX,
902 void TextClass::readCounter(Lexer & lexrc)
904 keyword_item counterTags[] = {
906 { "labelstring", CT_LABELSTRING },
907 { "labelstringappendix", CT_LABELSTRING_APPENDIX },
909 { "within", CT_WITHIN }
912 lexrc.pushTable(counterTags, CT_END);
916 docstring labelstring;
917 docstring labelstring_appendix;
920 while (!getout && lexrc.isOK()) {
921 int le = lexrc.lex();
923 case Lexer::LEX_UNDEF:
924 lexrc.printError("Unknown ClassOption tag `$$Token'");
928 switch (static_cast<CounterTags>(le)) {
931 name = lexrc.getDocString();
932 if (counters_->hasCounter(name))
933 LYXERR(Debug::TCLASS, "Reading existing counter " << to_utf8(name));
935 LYXERR(Debug::TCLASS, "Reading new counter " << to_utf8(name));
939 within = lexrc.getDocString();
940 if (within == "none")
945 labelstring = lexrc.getDocString();
946 labelstring_appendix = labelstring;
948 case CT_LABELSTRING_APPENDIX:
950 labelstring_appendix = lexrc.getDocString();
958 // Here if have a full counter if getout == true
960 counters_->newCounter(name, within,
961 labelstring, labelstring_appendix);
967 FontInfo const & TextClass::defaultfont() const
973 docstring const & TextClass::leftmargin() const
979 docstring const & TextClass::rightmargin() const
985 bool TextClass::hasLayout(docstring const & n) const
987 docstring const name = n.empty() ? defaultLayoutName() : n;
989 return find_if(layoutlist_.begin(), layoutlist_.end(),
990 LayoutNamesEqual(name))
991 != layoutlist_.end();
996 LayoutPtr const & TextClass::operator[](docstring const & name) const
998 BOOST_ASSERT(!name.empty());
1000 LayoutList::const_iterator cit =
1001 find_if(layoutlist_.begin(),
1003 LayoutNamesEqual(name));
1005 if (cit == layoutlist_.end()) {
1006 lyxerr << "We failed to find the layout '" << to_utf8(name)
1007 << "' in the layout list. You MUST investigate!"
1009 for (LayoutList::const_iterator it = layoutlist_.begin();
1010 it != layoutlist_.end(); ++it)
1011 lyxerr << " " << to_utf8(it->get()->name()) << endl;
1013 // we require the name to exist
1014 BOOST_ASSERT(false);
1021 bool TextClass::deleteLayout(docstring const & name)
1023 if (name == defaultLayoutName())
1026 LayoutList::iterator it =
1027 remove_if(layoutlist_.begin(), layoutlist_.end(),
1028 LayoutNamesEqual(name));
1030 LayoutList::iterator end = layoutlist_.end();
1031 bool const ret = (it != end);
1032 layoutlist_.erase(it, end);
1037 // Load textclass info if not loaded yet
1038 bool TextClass::load(string const & path) const
1043 // Read style-file, provided path is searched before the system ones
1044 FileName layout_file;
1046 layout_file = FileName(addName(path, name_ + ".layout"));
1047 if (layout_file.empty() || !layout_file.exists())
1048 layout_file = libFileSearch("layouts", name_, "layout");
1049 loaded_ = const_cast<TextClass*>(this)->read(layout_file) == 0;
1052 lyxerr << "Error reading `"
1053 << to_utf8(makeDisplayPath(layout_file.absFilename()))
1054 << "'\n(Check `" << name_
1055 << "')\nCheck your installation and "
1056 "try Options/Reconfigure..." << endl;
1063 FloatList & TextClass::floats()
1065 return *floatlist_.get();
1069 FloatList const & TextClass::floats() const
1071 return *floatlist_.get();
1075 Counters & TextClass::counters() const
1077 return *counters_.get();
1081 // Return the layout object of an inset given by name. If the name
1082 // is not found as such, the part after the ':' is stripped off, and
1083 // searched again. In this way, an error fallback can be provided:
1084 // An erroneous 'CharStyle:badname' (e.g., after a documentclass switch)
1085 // will invoke the layout object defined by name = 'CharStyle'.
1086 // If that doesn't work either, an empty object returns (shouldn't
1087 // happen). -- Idea JMarc, comment MV
1088 InsetLayout const & TextClass::insetlayout(docstring const & name) const
1091 while (!n.empty()) {
1092 if (insetlayoutlist_.count(n) > 0)
1093 return insetlayoutlist_[n];
1094 docstring::size_type i = n.find(':');
1095 if (i == string::npos)
1099 static InsetLayout empty;
1100 empty.labelstring = from_utf8("UNDEFINED");
1101 empty.labelfont = sane_font;
1102 empty.labelfont.setColor(Color_error);
1103 empty.bgcolor = Color_error;
1108 docstring const & TextClass::defaultLayoutName() const
1110 // This really should come from the actual layout... (Lgb)
1111 return defaultlayout_;
1115 LayoutPtr const & TextClass::defaultLayout() const
1117 return operator[](defaultLayoutName());
1121 string const & TextClass::name() const
1127 string const & TextClass::latexname() const
1129 const_cast<TextClass*>(this)->load();
1134 string const & TextClass::description() const
1136 return description_;
1140 string const & TextClass::opt_fontsize() const
1142 return opt_fontsize_;
1146 string const & TextClass::opt_pagestyle() const
1148 return opt_pagestyle_;
1152 string const & TextClass::options() const
1158 string const & TextClass::class_header() const
1160 return class_header_;
1164 string const & TextClass::pagestyle() const
1170 docstring const & TextClass::preamble() const
1176 PageSides TextClass::sides() const
1182 int TextClass::secnumdepth() const
1184 return secnumdepth_;
1188 int TextClass::tocdepth() const
1194 OutputType TextClass::outputType() const
1200 bool TextClass::provides(string const & p) const
1202 return provides_.find(p) != provides_.end();
1206 unsigned int TextClass::columns() const
1212 TitleLatexType TextClass::titletype() const
1218 string const & TextClass::titlename() const
1224 int TextClass::size() const
1226 return layoutlist_.size();
1230 int TextClass::min_toclevel() const
1232 return min_toclevel_;
1236 int TextClass::max_toclevel() const
1238 return max_toclevel_;
1242 bool TextClass::hasTocLevels() const
1244 return min_toclevel_ != Layout::NOT_IN_TOC;
1248 ostream & operator<<(ostream & os, PageSides p)