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 #if !defined(TEX2LYX_FONT_H) && !defined(FONT_H)
54 extern FontInfo lyxRead(Lexer &, FontInfo const & fi = sane_font);
59 class LayoutNamesEqual : public std::unary_function<LayoutPtr, bool> {
61 LayoutNamesEqual(docstring const & name)
64 bool operator()(LayoutPtr const & c) const
66 return c->name() == name_;
76 bool layout2layout(FileName const & filename, FileName const & tempfile)
78 FileName const script = libFileSearch("scripts", "layout2layout.py");
80 lyxerr << "Could not find layout conversion "
81 "script layout2layout.py." << endl;
85 std::ostringstream command;
86 command << support::os::python() << ' ' << quoteName(script.toFilesystemEncoding())
87 << ' ' << quoteName(filename.toFilesystemEncoding())
88 << ' ' << quoteName(tempfile.toFilesystemEncoding());
89 string const command_str = command.str();
91 LYXERR(Debug::TCLASS, "Running `" << command_str << '\'');
93 support::cmd_ret const ret =
94 support::runCommand(command_str);
96 lyxerr << "Could not run layout conversion "
97 "script layout2layout.py." << endl;
106 TextClass::TextClass(string const & fn, string const & cln,
107 string const & desc, bool texClassAvail )
108 : name_(fn), latexname_(cln), description_(desc),
109 floatlist_(new FloatList), counters_(new Counters),
110 texClassAvail_(texClassAvail)
118 pagestyle_ = "default";
119 defaultfont_ = sane_font;
120 opt_fontsize_ = "10|11|12";
121 opt_pagestyle_ = "empty|plain|headings|fancy";
122 titletype_ = TITLE_COMMAND_AFTER;
123 titlename_ = "maketitle";
128 bool TextClass::isTeXClassAvailable() const
130 return texClassAvail_;
134 bool TextClass::readStyle(Lexer & lexrc, Layout & lay)
136 LYXERR(Debug::TCLASS, "Reading style " << to_utf8(lay.name()));
137 if (!lay.read(lexrc, *this)) {
139 lay.resfont = lay.font;
140 lay.resfont.realize(defaultfont());
141 lay.reslabelfont = lay.labelfont;
142 lay.reslabelfont.realize(defaultfont());
143 return false; // no errors
145 lyxerr << "Error parsing style `" << to_utf8(lay.name()) << '\'' << endl;
178 // Reads a textclass structure from file.
179 bool TextClass::read(FileName const & filename, ReadType rt)
181 if (!filename.isReadableFile()) {
182 lyxerr << "Cannot read layout file `" << filename << "'."
187 keyword_item textClassTags[] = {
188 { "classoptions", TC_CLASSOPTIONS },
189 { "columns", TC_COLUMNS },
190 { "counter", TC_COUNTER },
191 { "defaultfont", TC_DEFAULTFONT },
192 { "defaultstyle", TC_DEFAULTSTYLE },
193 { "environment", TC_ENVIRONMENT },
194 { "float", TC_FLOAT },
195 { "format", TC_FORMAT },
196 { "input", TC_INPUT },
197 { "insetlayout", TC_INSETLAYOUT },
198 { "leftmargin", TC_LEFTMARGIN },
199 { "nofloat", TC_NOFLOAT },
200 { "nostyle", TC_NOSTYLE },
201 { "outputtype", TC_OUTPUTTYPE },
202 { "pagestyle", TC_PAGESTYLE },
203 { "preamble", TC_PREAMBLE },
204 { "provides", TC_PROVIDES },
205 { "rightmargin", TC_RIGHTMARGIN },
206 { "secnumdepth", TC_SECNUMDEPTH },
207 { "sides", TC_SIDES },
208 { "style", TC_STYLE },
209 { "titlelatexname", TC_TITLELATEXNAME },
210 { "titlelatextype", TC_TITLELATEXTYPE },
211 { "tocdepth", TC_TOCDEPTH }
216 LYXERR(Debug::TCLASS, "Reading textclass ");
219 LYXERR(Debug::TCLASS, "Reading input file ");
222 LYXERR(Debug::TCLASS, "Reading module file ");
227 LYXERR(Debug::TCLASS, to_utf8(makeDisplayPath(filename.absFilename())));
229 Lexer lexrc(textClassTags,
230 sizeof(textClassTags) / sizeof(textClassTags[0]));
232 lexrc.setFile(filename);
233 bool error = !lexrc.isOK();
235 // Format of files before the 'Format' tag was introduced
239 while (lexrc.isOK() && !error) {
240 int le = lexrc.lex();
243 case Lexer::LEX_FEOF:
246 case Lexer::LEX_UNDEF:
247 lexrc.printError("Unknown TextClass tag `$$Token'");
255 switch (static_cast<TextClassTags>(le)) {
259 format = lexrc.getInteger();
262 case TC_OUTPUTTYPE: // output type definition
263 readOutputType(lexrc);
266 case TC_INPUT: // Include file
268 string const inc = lexrc.getString();
269 FileName tmp = libFileSearch("layouts", inc,
273 lexrc.printError("Could not find input"
276 } else if (read(tmp, MERGE)) {
277 lexrc.printError("Error reading input"
278 "file: " + tmp.absFilename());
284 case TC_DEFAULTSTYLE:
286 docstring const name = from_utf8(subst(lexrc.getString(),
288 defaultlayout_ = name;
295 docstring const name = from_utf8(subst(lexrc.getString(),
298 string s = "Could not read name for style: `$$Token' "
299 + lexrc.getString() + " is probably not valid UTF-8!";
300 lexrc.printError(s.c_str());
302 error = readStyle(lexrc, lay);
303 } else if (hasLayout(name)) {
304 Layout * lay = operator[](name).get();
305 error = readStyle(lexrc, *lay);
309 if (le == TC_ENVIRONMENT)
310 lay.is_environment = true;
311 error = readStyle(lexrc, lay);
313 layoutlist_.push_back(
314 boost::shared_ptr<Layout>(new Layout(lay))
317 if (defaultlayout_.empty()) {
318 // We do not have a default
319 // layout yet, so we choose
320 // the first layout we
322 defaultlayout_ = name;
327 lexrc.printError("No name given for style: `$$Token'.");
334 docstring const style = from_utf8(subst(lexrc.getString(),
336 if (!deleteLayout(style))
337 lyxerr << "Cannot delete style `"
338 << to_utf8(style) << '\'' << endl;
339 // lexrc.printError("Cannot delete style"
346 columns_ = lexrc.getInteger();
351 switch (lexrc.getInteger()) {
352 case 1: sides_ = OneSide; break;
353 case 2: sides_ = TwoSides; break;
355 lyxerr << "Impossible number of page"
356 " sides, setting to one."
366 pagestyle_ = rtrim(lexrc.getString());
370 defaultfont_ = lyxRead(lexrc);
371 if (!defaultfont_.resolved()) {
372 lexrc.printError("Warning: defaultfont should "
373 "be fully instantiated!");
374 defaultfont_.realize(sane_font);
380 secnumdepth_ = lexrc.getInteger();
385 tocdepth_ = lexrc.getInteger();
388 // First step to support options
389 case TC_CLASSOPTIONS:
390 readClassOptions(lexrc);
394 preamble_ = from_utf8(lexrc.getLongString("EndPreamble"));
399 string const feature = lexrc.getString();
401 if (lexrc.getInteger())
402 provides_.insert(feature);
404 provides_.erase(feature);
408 case TC_LEFTMARGIN: // left margin type
410 leftmargin_ = lexrc.getDocString();
413 case TC_RIGHTMARGIN: // right margin type
415 rightmargin_ = lexrc.getDocString();
419 docstring const name = subst(lexrc.getDocString(), '_', ' ');
420 readInsetLayout(lexrc, name);
429 case TC_TITLELATEXTYPE:
430 readTitleType(lexrc);
432 case TC_TITLELATEXNAME:
434 titlename_ = lexrc.getString();
438 string const nofloat = lexrc.getString();
439 floatlist_->erase(nofloat);
443 if (format != FORMAT)
447 if (format != FORMAT) {
448 LYXERR(Debug::TCLASS, "Converting layout file from format "
449 << format << " to " << FORMAT);
450 FileName const tempfile(support::tempName());
451 error = !layout2layout(filename, tempfile);
453 error = read(tempfile, rt);
454 support::unlink(tempfile);
459 LYXERR(Debug::TCLASS, "Finished reading module file "
460 << to_utf8(makeDisplayPath(filename.absFilename())));
461 else if (rt == MERGE)
462 LYXERR(Debug::TCLASS, "Finished reading input file "
463 << to_utf8(makeDisplayPath(filename.absFilename())));
464 else { // we are at top level here.
465 LYXERR(Debug::TCLASS, "Finished reading textclass "
466 << to_utf8(makeDisplayPath(filename.absFilename())));
467 if (defaultlayout_.empty()) {
468 lyxerr << "Error: Textclass '" << name_
469 << "' is missing a defaultstyle." << endl;
473 min_toclevel_ = Layout::NOT_IN_TOC;
474 max_toclevel_ = Layout::NOT_IN_TOC;
475 const_iterator cit = begin();
476 const_iterator the_end = end();
477 for ( ; cit != the_end ; ++cit) {
478 int const toclevel = (*cit)->toclevel;
479 if (toclevel != Layout::NOT_IN_TOC) {
480 if (min_toclevel_ == Layout::NOT_IN_TOC)
481 min_toclevel_ = toclevel;
483 min_toclevel_ = std::min(min_toclevel_,
485 max_toclevel_ = std::max(max_toclevel_,
489 LYXERR(Debug::TCLASS, "Minimum TocLevel is " << min_toclevel_
490 << ", maximum is " << max_toclevel_);
498 void TextClass::readTitleType(Lexer & lexrc)
500 keyword_item titleTypeTags[] = {
501 { "commandafter", TITLE_COMMAND_AFTER },
502 { "environment", TITLE_ENVIRONMENT }
505 PushPopHelper pph(lexrc, titleTypeTags, TITLE_ENVIRONMENT);
507 int le = lexrc.lex();
509 case Lexer::LEX_UNDEF:
510 lexrc.printError("Unknown output type `$$Token'");
512 case TITLE_COMMAND_AFTER:
513 case TITLE_ENVIRONMENT:
514 titletype_ = static_cast<TitleLatexType>(le);
517 lyxerr << "Unhandled value " << le
518 << " in TextClass::readTitleType." << endl;
525 void TextClass::readOutputType(Lexer & lexrc)
527 keyword_item outputTypeTags[] = {
528 { "docbook", DOCBOOK },
530 { "literate", LITERATE }
533 PushPopHelper pph(lexrc, outputTypeTags, LITERATE);
535 int le = lexrc.lex();
537 case Lexer::LEX_UNDEF:
538 lexrc.printError("Unknown output type `$$Token'");
543 outputType_ = static_cast<OutputType>(le);
546 lyxerr << "Unhandled value " << le
547 << " in TextClass::readOutputType." << endl;
554 enum ClassOptionsTags {
563 void TextClass::readClassOptions(Lexer & lexrc)
565 keyword_item classOptionsTags[] = {
567 {"fontsize", CO_FONTSIZE },
568 {"header", CO_HEADER },
569 {"other", CO_OTHER },
570 {"pagestyle", CO_PAGESTYLE }
573 lexrc.pushTable(classOptionsTags, CO_END);
575 while (!getout && lexrc.isOK()) {
576 int le = lexrc.lex();
578 case Lexer::LEX_UNDEF:
579 lexrc.printError("Unknown ClassOption tag `$$Token'");
583 switch (static_cast<ClassOptionsTags>(le)) {
586 opt_fontsize_ = rtrim(lexrc.getString());
590 opt_pagestyle_ = rtrim(lexrc.getString());
594 options_ = lexrc.getString();
598 class_header_ = subst(lexrc.getString(), """, "\"");
609 enum InsetLayoutTags {
630 void TextClass::readInsetLayout(Lexer & lexrc, docstring const & name)
632 keyword_item elementTags[] = {
633 { "bgcolor", IL_BGCOLOR },
634 { "decoration", IL_DECORATION },
637 { "forceltr", IL_FORCELTR },
638 { "freespacing", IL_FREESPACING },
639 { "keepempty", IL_KEEPEMPTY },
640 { "labelfont", IL_LABELFONT },
641 { "labelstring", IL_LABELSTRING },
642 { "latexname", IL_LATEXNAME },
643 { "latexparam", IL_LATEXPARAM },
644 { "latextype", IL_LATEXTYPE },
645 { "lyxtype", IL_LYXTYPE },
646 { "multipar", IL_MULTIPAR },
647 { "needprotect", IL_NEEDPROTECT },
648 { "passthru", IL_PASSTHRU },
649 { "preamble", IL_PREAMBLE }
652 lexrc.pushTable(elementTags, IL_END);
655 docstring labelstring;
660 FontInfo font = inherit_font;
661 FontInfo labelfont = inherit_font;
662 ColorCode bgcolor(Color_background);
664 bool multipar = false;
665 bool passthru = false;
666 bool needprotect = false;
667 bool keepempty = false;
668 bool freespacing = false;
669 bool forceltr = false;
672 while (!getout && lexrc.isOK()) {
673 int le = lexrc.lex();
675 case Lexer::LEX_UNDEF:
676 lexrc.printError("Unknown ClassOption tag `$$Token'");
680 switch (static_cast<InsetLayoutTags>(le)) {
683 lyxtype = lexrc.getString();
687 latextype = lexrc.getString();
691 labelstring = lexrc.getDocString();
695 decoration = lexrc.getString();
699 latexname = lexrc.getString();
703 latexparam = subst(lexrc.getString(), """, "\"");
706 labelfont = lyxRead(lexrc, inherit_font);
710 forceltr = lexrc.getBool();
714 multipar = lexrc.getBool();
718 passthru = lexrc.getBool();
722 keepempty = lexrc.getBool();
726 freespacing = lexrc.getBool();
730 needprotect = lexrc.getBool();
733 font = lyxRead(lexrc, inherit_font);
734 // So: define font before labelfont
739 string const token = lexrc.getString();
740 bgcolor = lcolor.getFromLyXName(token);
744 preamble = lexrc.getLongString("EndPreamble");
752 // Here add element to list if getout == true
755 il.name = to_ascii(name);
756 il.lyxtype = lyxtype;
757 il.labelstring = labelstring;
758 il.decoration = decoration;
759 il.latextype = latextype;
760 il.latexname = latexname;
761 il.latexparam = latexparam;
762 il.multipar = multipar;
763 il.passthru = passthru;
764 il.needprotect = needprotect;
765 il.freespacing = freespacing;
766 il.forceltr = forceltr;
767 il.keepempty = keepempty;
769 // The label font is generally used as-is without
770 // any realization against a given context.
771 labelfont.realize(sane_font);
772 il.labelfont = labelfont;
773 il.bgcolor = bgcolor;
774 il.preamble = preamble;
775 insetlayoutlist_[name] = il;
796 void TextClass::readFloat(Lexer & lexrc)
798 keyword_item floatTags[] = {
800 { "extension", FT_EXT },
801 { "guiname", FT_NAME },
802 { "latexbuiltin", FT_BUILTIN },
803 { "listname", FT_LISTNAME },
804 { "numberwithin", FT_WITHIN },
805 { "placement", FT_PLACEMENT },
806 { "style", FT_STYLE },
810 lexrc.pushTable(floatTags, FT_END);
819 bool builtin = false;
822 while (!getout && lexrc.isOK()) {
823 int le = lexrc.lex();
825 case Lexer::LEX_UNDEF:
826 lexrc.printError("Unknown ClassOption tag `$$Token'");
830 switch (static_cast<FloatTags>(le)) {
833 type = lexrc.getString();
834 if (floatlist_->typeExist(type)) {
835 Floating const & fl = floatlist_->getType(type);
836 placement = fl.placement();
838 within = fl.within();
841 listName = fl.listName();
842 builtin = fl.builtin();
847 name = lexrc.getString();
851 placement = lexrc.getString();
855 ext = lexrc.getString();
859 within = lexrc.getString();
860 if (within == "none")
865 style = lexrc.getString();
869 listName = lexrc.getString();
873 builtin = lexrc.getBool();
881 // Here if have a full float if getout == true
883 Floating fl(type, placement, ext, within,
884 style, name, listName, builtin);
885 floatlist_->newFloat(fl);
886 // each float has its own counter
887 counters_->newCounter(from_ascii(type), from_ascii(within),
888 docstring(), docstring());
899 CT_LABELSTRING_APPENDIX,
904 void TextClass::readCounter(Lexer & lexrc)
906 keyword_item counterTags[] = {
908 { "labelstring", CT_LABELSTRING },
909 { "labelstringappendix", CT_LABELSTRING_APPENDIX },
911 { "within", CT_WITHIN }
914 lexrc.pushTable(counterTags, CT_END);
918 docstring labelstring;
919 docstring labelstring_appendix;
922 while (!getout && lexrc.isOK()) {
923 int le = lexrc.lex();
925 case Lexer::LEX_UNDEF:
926 lexrc.printError("Unknown ClassOption tag `$$Token'");
930 switch (static_cast<CounterTags>(le)) {
933 name = lexrc.getDocString();
934 if (counters_->hasCounter(name))
935 LYXERR(Debug::TCLASS, "Reading existing counter " << to_utf8(name));
937 LYXERR(Debug::TCLASS, "Reading new counter " << to_utf8(name));
941 within = lexrc.getDocString();
942 if (within == "none")
947 labelstring = lexrc.getDocString();
948 labelstring_appendix = labelstring;
950 case CT_LABELSTRING_APPENDIX:
952 labelstring_appendix = lexrc.getDocString();
960 // Here if have a full counter if getout == true
962 counters_->newCounter(name, within,
963 labelstring, labelstring_appendix);
969 FontInfo const & TextClass::defaultfont() const
975 docstring const & TextClass::leftmargin() const
981 docstring const & TextClass::rightmargin() const
987 bool TextClass::hasLayout(docstring const & n) const
989 docstring const name = n.empty() ? defaultLayoutName() : n;
991 return find_if(layoutlist_.begin(), layoutlist_.end(),
992 LayoutNamesEqual(name))
993 != layoutlist_.end();
998 LayoutPtr const & TextClass::operator[](docstring const & name) const
1000 BOOST_ASSERT(!name.empty());
1002 LayoutList::const_iterator cit =
1003 find_if(layoutlist_.begin(),
1005 LayoutNamesEqual(name));
1007 if (cit == layoutlist_.end()) {
1008 lyxerr << "We failed to find the layout '" << to_utf8(name)
1009 << "' in the layout list. You MUST investigate!"
1011 for (LayoutList::const_iterator it = layoutlist_.begin();
1012 it != layoutlist_.end(); ++it)
1013 lyxerr << " " << to_utf8(it->get()->name()) << endl;
1015 // we require the name to exist
1016 BOOST_ASSERT(false);
1023 bool TextClass::deleteLayout(docstring const & name)
1025 if (name == defaultLayoutName())
1028 LayoutList::iterator it =
1029 remove_if(layoutlist_.begin(), layoutlist_.end(),
1030 LayoutNamesEqual(name));
1032 LayoutList::iterator end = layoutlist_.end();
1033 bool const ret = (it != end);
1034 layoutlist_.erase(it, end);
1039 // Load textclass info if not loaded yet
1040 bool TextClass::load(string const & path) const
1045 // Read style-file, provided path is searched before the system ones
1046 FileName layout_file;
1048 layout_file = FileName(addName(path, name_ + ".layout"));
1049 if (layout_file.empty() || !layout_file.exists())
1050 layout_file = libFileSearch("layouts", name_, "layout");
1051 loaded_ = const_cast<TextClass*>(this)->read(layout_file) == 0;
1054 lyxerr << "Error reading `"
1055 << to_utf8(makeDisplayPath(layout_file.absFilename()))
1056 << "'\n(Check `" << name_
1057 << "')\nCheck your installation and "
1058 "try Options/Reconfigure..." << endl;
1065 FloatList & TextClass::floats()
1067 return *floatlist_.get();
1071 FloatList const & TextClass::floats() const
1073 return *floatlist_.get();
1077 Counters & TextClass::counters() const
1079 return *counters_.get();
1083 // Return the layout object of an inset given by name. If the name
1084 // is not found as such, the part after the ':' is stripped off, and
1085 // searched again. In this way, an error fallback can be provided:
1086 // An erroneous 'CharStyle:badname' (e.g., after a documentclass switch)
1087 // will invoke the layout object defined by name = 'CharStyle'.
1088 // If that doesn't work either, an empty object returns (shouldn't
1089 // happen). -- Idea JMarc, comment MV
1090 InsetLayout const & TextClass::insetlayout(docstring const & name) const
1093 while (!n.empty()) {
1094 if (insetlayoutlist_.count(n) > 0)
1095 return insetlayoutlist_[n];
1096 docstring::size_type i = n.find(':');
1097 if (i == string::npos)
1101 static InsetLayout empty;
1102 empty.labelstring = from_utf8("UNDEFINED");
1103 empty.labelfont = sane_font;
1104 empty.labelfont.setColor(Color_error);
1105 empty.bgcolor = Color_error;
1110 docstring const & TextClass::defaultLayoutName() const
1112 // This really should come from the actual layout... (Lgb)
1113 return defaultlayout_;
1117 LayoutPtr const & TextClass::defaultLayout() const
1119 return operator[](defaultLayoutName());
1123 string const & TextClass::name() const
1129 string const & TextClass::latexname() const
1131 const_cast<TextClass*>(this)->load();
1136 string const & TextClass::description() const
1138 return description_;
1142 string const & TextClass::opt_fontsize() const
1144 return opt_fontsize_;
1148 string const & TextClass::opt_pagestyle() const
1150 return opt_pagestyle_;
1154 string const & TextClass::options() const
1160 string const & TextClass::class_header() const
1162 return class_header_;
1166 string const & TextClass::pagestyle() const
1172 docstring const & TextClass::preamble() const
1178 PageSides TextClass::sides() const
1184 int TextClass::secnumdepth() const
1186 return secnumdepth_;
1190 int TextClass::tocdepth() const
1196 OutputType TextClass::outputType() const
1202 bool TextClass::provides(string const & p) const
1204 return provides_.find(p) != provides_.end();
1208 unsigned int TextClass::columns() const
1214 TitleLatexType TextClass::titletype() const
1220 string const & TextClass::titlename() const
1226 int TextClass::size() const
1228 return layoutlist_.size();
1232 int TextClass::min_toclevel() const
1234 return min_toclevel_;
1238 int TextClass::max_toclevel() const
1240 return max_toclevel_;
1244 bool TextClass::hasTocLevels() const
1246 return min_toclevel_ != Layout::NOT_IN_TOC;
1250 ostream & operator<<(ostream & os, PageSides p)