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"
21 #include "support/debug.h"
22 #include "support/gettext.h"
24 #include "FloatList.h"
29 #include "frontends/alert.h"
31 #include "support/lstrings.h"
32 #include "support/FileName.h"
33 #include "support/filetools.h"
34 #include "support/os.h"
39 using namespace lyx::support;
45 class LayoutNamesEqual : public unary_function<LayoutPtr, bool> {
47 LayoutNamesEqual(docstring const & name)
50 bool operator()(LayoutPtr const & c) const
52 return c->name() == name_;
62 bool layout2layout(FileName const & filename, FileName const & tempfile)
64 FileName const script = libFileSearch("scripts", "layout2layout.py");
66 lyxerr << "Could not find layout conversion "
67 "script layout2layout.py." << endl;
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();
77 LYXERR(Debug::TCLASS, "Running `" << command_str << '\'');
80 runCommand(command_str);
82 lyxerr << "Could not run layout conversion "
83 "script layout2layout.py." << endl;
90 std::string translateRT(TextClass::ReadType rt)
93 case TextClass::BASECLASS:
95 case TextClass::MERGE:
97 case TextClass::MODULE:
105 TextClass::TextClass(string const & fn, string const & cln,
106 string const & desc, bool texClassAvail )
107 : name_(fn), latexname_(cln), description_(desc),
108 floatlist_(new FloatList), counters_(new Counters),
109 texClassAvail_(texClassAvail)
117 pagestyle_ = "default";
118 defaultfont_ = sane_font;
119 opt_fontsize_ = "10|11|12";
120 opt_pagestyle_ = "empty|plain|headings|fancy";
121 titletype_ = TITLE_COMMAND_AFTER;
122 titlename_ = "maketitle";
127 bool TextClass::isTeXClassAvailable() const
129 return texClassAvail_;
133 bool TextClass::readStyle(Lexer & lexrc, Layout & lay)
135 LYXERR(Debug::TCLASS, "Reading style " << to_utf8(lay.name()));
136 if (!lay.read(lexrc, *this)) {
138 lay.resfont = lay.font;
139 lay.resfont.realize(defaultfont());
140 lay.reslabelfont = lay.labelfont;
141 lay.reslabelfont.realize(defaultfont());
142 return false; // no errors
144 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 { "requires", TC_REQUIRES },
206 { "rightmargin", TC_RIGHTMARGIN },
207 { "secnumdepth", TC_SECNUMDEPTH },
208 { "sides", TC_SIDES },
209 { "style", TC_STYLE },
210 { "titlelatexname", TC_TITLELATEXNAME },
211 { "titlelatextype", TC_TITLELATEXTYPE },
212 { "tocdepth", TC_TOCDEPTH }
215 LYXERR(Debug::TCLASS, "Reading " + translateRT(rt) + ": " +
216 to_utf8(makeDisplayPath(filename.absFilename())));
218 Lexer lexrc(textClassTags,
219 sizeof(textClassTags) / sizeof(textClassTags[0]));
221 lexrc.setFile(filename);
222 bool error = !lexrc.isOK();
224 // Format of files before the 'Format' tag was introduced
228 while (lexrc.isOK() && !error) {
229 int le = lexrc.lex();
232 case Lexer::LEX_FEOF:
235 case Lexer::LEX_UNDEF:
236 lexrc.printError("Unknown TextClass tag `$$Token'");
244 switch (static_cast<TextClassTags>(le)) {
248 format = lexrc.getInteger();
251 case TC_OUTPUTTYPE: // output type definition
252 readOutputType(lexrc);
255 case TC_INPUT: // Include file
257 string const inc = lexrc.getString();
258 FileName tmp = libFileSearch("layouts", inc,
262 lexrc.printError("Could not find input file: " + inc);
264 } else if (read(tmp, MERGE)) {
265 lexrc.printError("Error reading input"
266 "file: " + tmp.absFilename());
272 case TC_DEFAULTSTYLE:
274 docstring const name = from_utf8(subst(lexrc.getString(),
276 defaultlayout_ = name;
283 docstring const name = from_utf8(subst(lexrc.getString(),
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());
290 error = readStyle(lexrc, lay);
291 } else if (hasLayout(name)) {
292 Layout * lay = operator[](name).get();
293 error = readStyle(lexrc, *lay);
297 if (le == TC_ENVIRONMENT)
298 lay.is_environment = true;
299 error = readStyle(lexrc, lay);
301 layoutlist_.push_back(
302 boost::shared_ptr<Layout>(new Layout(lay))
305 if (defaultlayout_.empty()) {
306 // We do not have a default
307 // layout yet, so we choose
308 // the first layout we
310 defaultlayout_ = name;
315 lexrc.printError("No name given for style: `$$Token'.");
322 docstring const style = from_utf8(subst(lexrc.getString(),
324 if (!deleteLayout(style))
325 lyxerr << "Cannot delete style `"
326 << to_utf8(style) << '\'' << endl;
327 // lexrc.printError("Cannot delete style"
334 columns_ = lexrc.getInteger();
339 switch (lexrc.getInteger()) {
340 case 1: sides_ = OneSide; break;
341 case 2: sides_ = TwoSides; break;
343 lyxerr << "Impossible number of page"
344 " sides, setting to one."
354 pagestyle_ = rtrim(lexrc.getString());
358 defaultfont_ = lyxRead(lexrc);
359 if (!defaultfont_.resolved()) {
360 lexrc.printError("Warning: defaultfont should "
361 "be fully instantiated!");
362 defaultfont_.realize(sane_font);
368 secnumdepth_ = lexrc.getInteger();
373 tocdepth_ = lexrc.getInteger();
376 // First step to support options
377 case TC_CLASSOPTIONS:
378 readClassOptions(lexrc);
382 preamble_ = from_utf8(lexrc.getLongString("EndPreamble"));
387 string const feature = lexrc.getString();
389 if (lexrc.getInteger())
390 provides_.insert(feature);
392 provides_.erase(feature);
398 vector<string> const req
399 = getVectorFromString(lexrc.getString());
400 requires_.insert(req.begin(), req.end());
404 case TC_LEFTMARGIN: // left margin type
406 leftmargin_ = lexrc.getDocString();
409 case TC_RIGHTMARGIN: // right margin type
411 rightmargin_ = lexrc.getDocString();
415 docstring const name = subst(lexrc.getDocString(), '_', ' ');
416 readInsetLayout(lexrc, name);
425 case TC_TITLELATEXTYPE:
426 readTitleType(lexrc);
428 case TC_TITLELATEXNAME:
430 titlename_ = lexrc.getString();
434 string const nofloat = lexrc.getString();
435 floatlist_->erase(nofloat);
439 if (format != FORMAT)
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);
449 error = read(tempfile, rt);
450 tempfile.removeFile();
454 LYXERR(Debug::TCLASS, "Finished reading " + translateRT(rt) + ": " +
455 to_utf8(makeDisplayPath(filename.absFilename())));
457 if (rt == BASECLASS) {
458 if (defaultlayout_.empty()) {
459 lyxerr << "Error: Textclass '" << name_
460 << "' is missing a defaultstyle." << endl;
464 min_toclevel_ = Layout::NOT_IN_TOC;
465 max_toclevel_ = Layout::NOT_IN_TOC;
466 const_iterator cit = begin();
467 const_iterator the_end = end();
468 for ( ; cit != the_end ; ++cit) {
469 int const toclevel = (*cit)->toclevel;
470 if (toclevel != Layout::NOT_IN_TOC) {
471 if (min_toclevel_ == Layout::NOT_IN_TOC)
472 min_toclevel_ = toclevel;
474 min_toclevel_ = min(min_toclevel_,
476 max_toclevel_ = max(max_toclevel_,
480 LYXERR(Debug::TCLASS, "Minimum TocLevel is " << min_toclevel_
481 << ", maximum is " << max_toclevel_);
489 void TextClass::readTitleType(Lexer & lexrc)
491 keyword_item titleTypeTags[] = {
492 { "commandafter", TITLE_COMMAND_AFTER },
493 { "environment", TITLE_ENVIRONMENT }
496 PushPopHelper pph(lexrc, titleTypeTags, TITLE_ENVIRONMENT);
498 int le = lexrc.lex();
500 case Lexer::LEX_UNDEF:
501 lexrc.printError("Unknown output type `$$Token'");
503 case TITLE_COMMAND_AFTER:
504 case TITLE_ENVIRONMENT:
505 titletype_ = static_cast<TitleLatexType>(le);
508 lyxerr << "Unhandled value " << le
509 << " in TextClass::readTitleType." << endl;
516 void TextClass::readOutputType(Lexer & lexrc)
518 keyword_item outputTypeTags[] = {
519 { "docbook", DOCBOOK },
521 { "literate", LITERATE }
524 PushPopHelper pph(lexrc, outputTypeTags, LITERATE);
526 int le = lexrc.lex();
528 case Lexer::LEX_UNDEF:
529 lexrc.printError("Unknown output type `$$Token'");
534 outputType_ = static_cast<OutputType>(le);
537 lyxerr << "Unhandled value " << le
538 << " in TextClass::readOutputType." << endl;
545 enum ClassOptionsTags {
554 void TextClass::readClassOptions(Lexer & lexrc)
556 keyword_item classOptionsTags[] = {
558 {"fontsize", CO_FONTSIZE },
559 {"header", CO_HEADER },
560 {"other", CO_OTHER },
561 {"pagestyle", CO_PAGESTYLE }
564 lexrc.pushTable(classOptionsTags, CO_END);
566 while (!getout && lexrc.isOK()) {
567 int le = lexrc.lex();
569 case Lexer::LEX_UNDEF:
570 lexrc.printError("Unknown ClassOption tag `$$Token'");
574 switch (static_cast<ClassOptionsTags>(le)) {
577 opt_fontsize_ = rtrim(lexrc.getString());
581 opt_pagestyle_ = rtrim(lexrc.getString());
585 options_ = lexrc.getString();
589 class_header_ = subst(lexrc.getString(), """, "\"");
600 enum InsetLayoutTags {
622 void TextClass::readInsetLayout(Lexer & lexrc, docstring const & name)
624 keyword_item elementTags[] = {
625 { "bgcolor", IL_BGCOLOR },
626 { "decoration", IL_DECORATION },
629 { "forceltr", IL_FORCELTR },
630 { "freespacing", IL_FREESPACING },
631 { "keepempty", IL_KEEPEMPTY },
632 { "labelfont", IL_LABELFONT },
633 { "labelstring", IL_LABELSTRING },
634 { "latexname", IL_LATEXNAME },
635 { "latexparam", IL_LATEXPARAM },
636 { "latextype", IL_LATEXTYPE },
637 { "lyxtype", IL_LYXTYPE },
638 { "multipar", IL_MULTIPAR },
639 { "needprotect", IL_NEEDPROTECT },
640 { "passthru", IL_PASSTHRU },
641 { "preamble", IL_PREAMBLE },
642 { "requires", IL_REQUIRES }
645 lexrc.pushTable(elementTags, IL_END);
648 docstring labelstring;
653 FontInfo font = inherit_font;
654 FontInfo labelfont = inherit_font;
655 ColorCode bgcolor(Color_background);
657 set<string> requires;
658 bool multipar = false;
659 bool passthru = false;
660 bool needprotect = false;
661 bool keepempty = false;
662 bool freespacing = false;
663 bool forceltr = false;
666 while (!getout && lexrc.isOK()) {
667 int le = lexrc.lex();
669 case Lexer::LEX_UNDEF:
670 lexrc.printError("Unknown InsetLayout tag `$$Token'");
674 switch (static_cast<InsetLayoutTags>(le)) {
677 lyxtype = lexrc.getString();
681 latextype = lexrc.getString();
685 labelstring = lexrc.getDocString();
689 decoration = lexrc.getString();
693 latexname = lexrc.getString();
697 latexparam = subst(lexrc.getString(), """, "\"");
700 labelfont = lyxRead(lexrc, inherit_font);
704 forceltr = lexrc.getBool();
708 multipar = lexrc.getBool();
712 passthru = lexrc.getBool();
716 keepempty = lexrc.getBool();
720 freespacing = lexrc.getBool();
724 needprotect = lexrc.getBool();
727 font = lyxRead(lexrc, inherit_font);
728 // So: define font before labelfont
733 string const token = lexrc.getString();
734 bgcolor = lcolor.getFromLyXName(token);
738 preamble = lexrc.getLongString("EndPreamble");
742 vector<string> const req
743 = getVectorFromString(lexrc.getString());
744 requires.insert(req.begin(), req.end());
753 // Here add element to list if getout == true
756 il.name = to_ascii(name);
757 il.lyxtype = lyxtype;
758 il.labelstring = labelstring;
759 il.decoration = decoration;
760 il.latextype = latextype;
761 il.latexname = latexname;
762 il.latexparam = latexparam;
763 il.multipar = multipar;
764 il.passthru = passthru;
765 il.needprotect = needprotect;
766 il.freespacing = freespacing;
767 il.forceltr = forceltr;
768 il.keepempty = keepempty;
770 // The label font is generally used as-is without
771 // any realization against a given context.
772 labelfont.realize(sane_font);
773 il.labelfont = labelfont;
774 il.bgcolor = bgcolor;
775 il.preamble = preamble;
776 il.requires = requires;
777 insetlayoutlist_[name] = il;
798 void TextClass::readFloat(Lexer & lexrc)
800 keyword_item floatTags[] = {
802 { "extension", FT_EXT },
803 { "guiname", FT_NAME },
804 { "latexbuiltin", FT_BUILTIN },
805 { "listname", FT_LISTNAME },
806 { "numberwithin", FT_WITHIN },
807 { "placement", FT_PLACEMENT },
808 { "style", FT_STYLE },
812 lexrc.pushTable(floatTags, FT_END);
821 bool builtin = false;
824 while (!getout && lexrc.isOK()) {
825 int le = lexrc.lex();
827 case Lexer::LEX_UNDEF:
828 lexrc.printError("Unknown float tag `$$Token'");
832 switch (static_cast<FloatTags>(le)) {
835 type = lexrc.getString();
836 if (floatlist_->typeExist(type)) {
837 Floating const & fl = floatlist_->getType(type);
838 placement = fl.placement();
840 within = fl.within();
843 listName = fl.listName();
844 builtin = fl.builtin();
849 name = lexrc.getString();
853 placement = lexrc.getString();
857 ext = lexrc.getString();
861 within = lexrc.getString();
862 if (within == "none")
867 style = lexrc.getString();
871 listName = lexrc.getString();
875 builtin = lexrc.getBool();
883 // Here if have a full float if getout == true
885 Floating fl(type, placement, ext, within,
886 style, name, listName, builtin);
887 floatlist_->newFloat(fl);
888 // each float has its own counter
889 counters_->newCounter(from_ascii(type), from_ascii(within),
890 docstring(), docstring());
901 CT_LABELSTRING_APPENDIX,
906 void TextClass::readCounter(Lexer & lexrc)
908 keyword_item counterTags[] = {
910 { "labelstring", CT_LABELSTRING },
911 { "labelstringappendix", CT_LABELSTRING_APPENDIX },
913 { "within", CT_WITHIN }
916 lexrc.pushTable(counterTags, CT_END);
920 docstring labelstring;
921 docstring labelstring_appendix;
924 while (!getout && lexrc.isOK()) {
925 int le = lexrc.lex();
927 case Lexer::LEX_UNDEF:
928 lexrc.printError("Unknown counter tag `$$Token'");
932 switch (static_cast<CounterTags>(le)) {
935 name = lexrc.getDocString();
936 if (counters_->hasCounter(name))
937 LYXERR(Debug::TCLASS, "Reading existing counter " << to_utf8(name));
939 LYXERR(Debug::TCLASS, "Reading new counter " << to_utf8(name));
943 within = lexrc.getDocString();
944 if (within == "none")
949 labelstring = lexrc.getDocString();
950 labelstring_appendix = labelstring;
952 case CT_LABELSTRING_APPENDIX:
954 labelstring_appendix = lexrc.getDocString();
962 // Here if have a full counter if getout == true
964 counters_->newCounter(name, within,
965 labelstring, labelstring_appendix);
971 FontInfo const & TextClass::defaultfont() const
977 docstring const & TextClass::leftmargin() const
983 docstring const & TextClass::rightmargin() const
989 bool TextClass::hasLayout(docstring const & n) const
991 docstring const name = n.empty() ? defaultLayoutName() : n;
993 return find_if(layoutlist_.begin(), layoutlist_.end(),
994 LayoutNamesEqual(name))
995 != layoutlist_.end();
1000 LayoutPtr const & TextClass::operator[](docstring const & name) const
1002 BOOST_ASSERT(!name.empty());
1004 LayoutList::const_iterator cit =
1005 find_if(layoutlist_.begin(),
1007 LayoutNamesEqual(name));
1009 if (cit == layoutlist_.end()) {
1010 lyxerr << "We failed to find the layout '" << to_utf8(name)
1011 << "' in the layout list. You MUST investigate!"
1013 for (LayoutList::const_iterator it = layoutlist_.begin();
1014 it != layoutlist_.end(); ++it)
1015 lyxerr << " " << to_utf8(it->get()->name()) << endl;
1017 // we require the name to exist
1018 BOOST_ASSERT(false);
1025 bool TextClass::deleteLayout(docstring const & name)
1027 if (name == defaultLayoutName())
1030 LayoutList::iterator it =
1031 remove_if(layoutlist_.begin(), layoutlist_.end(),
1032 LayoutNamesEqual(name));
1034 LayoutList::iterator end = layoutlist_.end();
1035 bool const ret = (it != end);
1036 layoutlist_.erase(it, end);
1041 // Load textclass info if not loaded yet
1042 bool TextClass::load(string const & path) const
1047 // Read style-file, provided path is searched before the system ones
1048 FileName layout_file;
1050 layout_file = FileName(addName(path, name_ + ".layout"));
1051 if (layout_file.empty() || !layout_file.exists())
1052 layout_file = libFileSearch("layouts", name_, "layout");
1053 loaded_ = const_cast<TextClass*>(this)->read(layout_file) == 0;
1056 lyxerr << "Error reading `"
1057 << to_utf8(makeDisplayPath(layout_file.absFilename()))
1058 << "'\n(Check `" << name_
1059 << "')\nCheck your installation and "
1060 "try Options/Reconfigure..." << endl;
1067 FloatList & TextClass::floats()
1069 return *floatlist_.get();
1073 FloatList const & TextClass::floats() const
1075 return *floatlist_.get();
1079 Counters & TextClass::counters() const
1081 return *counters_.get();
1085 // Return the layout object of an inset given by name. If the name
1086 // is not found as such, the part after the ':' is stripped off, and
1087 // searched again. In this way, an error fallback can be provided:
1088 // An erroneous 'CharStyle:badname' (e.g., after a documentclass switch)
1089 // will invoke the layout object defined by name = 'CharStyle'.
1090 // If that doesn't work either, an empty object returns (shouldn't
1091 // happen). -- Idea JMarc, comment MV
1092 InsetLayout const & TextClass::insetlayout(docstring const & name) const
1095 while (!n.empty()) {
1096 if (insetlayoutlist_.count(n) > 0)
1097 return insetlayoutlist_[n];
1098 docstring::size_type i = n.find(':');
1099 if (i == string::npos)
1103 static InsetLayout empty;
1104 empty.labelstring = from_utf8("UNDEFINED");
1105 empty.labelfont = sane_font;
1106 empty.labelfont.setColor(Color_error);
1107 empty.bgcolor = Color_error;
1112 docstring const & TextClass::defaultLayoutName() const
1114 // This really should come from the actual layout... (Lgb)
1115 return defaultlayout_;
1119 LayoutPtr const & TextClass::defaultLayout() const
1121 return operator[](defaultLayoutName());
1125 string const & TextClass::name() const
1131 string const & TextClass::latexname() const
1133 const_cast<TextClass*>(this)->load();
1138 string const & TextClass::description() const
1140 return description_;
1144 string const & TextClass::opt_fontsize() const
1146 return opt_fontsize_;
1150 string const & TextClass::opt_pagestyle() const
1152 return opt_pagestyle_;
1156 string const & TextClass::options() const
1162 string const & TextClass::class_header() const
1164 return class_header_;
1168 string const & TextClass::pagestyle() const
1174 docstring const & TextClass::preamble() const
1180 PageSides TextClass::sides() const
1186 int TextClass::secnumdepth() const
1188 return secnumdepth_;
1192 int TextClass::tocdepth() const
1198 OutputType TextClass::outputType() const
1204 bool TextClass::provides(string const & p) const
1206 return provides_.find(p) != provides_.end();
1210 unsigned int TextClass::columns() const
1216 TitleLatexType TextClass::titletype() const
1222 string const & TextClass::titlename() const
1228 int TextClass::size() const
1230 return layoutlist_.size();
1234 int TextClass::min_toclevel() const
1236 return min_toclevel_;
1240 int TextClass::max_toclevel() const
1242 return max_toclevel_;
1246 bool TextClass::hasTocLevels() const
1248 return min_toclevel_ != Layout::NOT_IN_TOC;
1252 ostream & operator<<(ostream & os, PageSides p)