X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2FTextClass.cpp;h=13adf04675cdf0a43e6673bded0843fd5f566558;hb=b7406da7f0326fbd895c700ad5ded4cc5112094f;hp=4df69a680c82375f2b45e84b6d37af3b57e929c0;hpb=0fb8f3fba12ab9fb395ef40e21b5ba50a5e0b9df;p=lyx.git diff --git a/src/TextClass.cpp b/src/TextClass.cpp index 4df69a680c..13adf04675 100644 --- a/src/TextClass.cpp +++ b/src/TextClass.cpp @@ -16,6 +16,7 @@ #include "TextClass.h" +#include "LayoutFile.h" #include "Color.h" #include "Counters.h" #include "Floating.h" @@ -34,8 +35,11 @@ #include "support/lstrings.h" #include "support/os.h" +#include #include +#include "boost/assert.hpp" + using namespace std; using namespace lyx::support; @@ -43,14 +47,14 @@ namespace lyx { namespace { -class LayoutNamesEqual : public unary_function { +class LayoutNamesEqual : public unary_function { public: LayoutNamesEqual(docstring const & name) : name_(name) {} - bool operator()(LayoutPtr const & c) const + bool operator()(Layout const & c) const { - return c->name() == name_; + return c.name() == name_; } private: docstring name_; @@ -105,13 +109,14 @@ std::string translateRT(TextClass::ReadType rt) } // namespace anon -TextClass::TextClass(string const & fn, string const & cln, - string const & desc, bool texClassAvail ) - : name_(fn), latexname_(cln), description_(desc), - floatlist_(new FloatList), counters_(new Counters), - texClassAvail_(texClassAvail) +docstring const TextClass::emptylayout_ = from_ascii("PlainLayout"); + + +InsetLayout DocumentClass::empty_insetlayout_; + + +TextClass::TextClass() { - modular_ = false; outputType_ = LATEX; columns_ = 1; sides_ = OneSide; @@ -130,18 +135,6 @@ TextClass::TextClass(string const & fn, string const & cln, } -docstring const TextClass::emptylayout_ = from_ascii("PlainLayout"); - - -InsetLayout TextClass::empty_insetlayout_; - - -bool TextClass::isTeXClassAvailable() const -{ - return texClassAvail_; -} - - bool TextClass::readStyle(Lexer & lexrc, Layout & lay) { LYXERR(Debug::TCLASS, "Reading style " << to_utf8(lay.name())); @@ -151,9 +144,9 @@ bool TextClass::readStyle(Lexer & lexrc, Layout & lay) } // Resolve fonts lay.resfont = lay.font; - lay.resfont.realize(defaultfont()); + lay.resfont.realize(defaultfont_); lay.reslabelfont = lay.labelfont; - lay.reslabelfont.realize(defaultfont()); + lay.reslabelfont.realize(defaultfont_); return true; // no errors } @@ -247,8 +240,8 @@ bool TextClass::read(FileName const & filename, ReadType rt) // The only way this happens is because the hardcoded layout above // is wrong. BOOST_ASSERT(false); - } - layoutlist_.push_back(boost::shared_ptr(new Layout(lay))); + }; + layoutlist_.push_back(lay); } Lexer lexrc(textClassTags, @@ -328,8 +321,8 @@ bool TextClass::read(FileName const & filename, ReadType rt) //error to true, since we couldn't even read the name? error = !readStyle(lexrc, lay); } else if (hasLayout(name)) { - Layout * lay = operator[](name).get(); - error = !readStyle(lexrc, *lay); + Layout & lay = operator[](name); + error = !readStyle(lexrc, lay); } else { Layout lay; lay.setName(name); @@ -337,7 +330,7 @@ bool TextClass::read(FileName const & filename, ReadType rt) lay.is_environment = true; error = !readStyle(lexrc, lay); if (!error) - layoutlist_.push_back(boost::shared_ptr(new Layout(lay))); + layoutlist_.push_back(lay); if (defaultlayout_.empty()) { // We do not have a default layout yet, so we choose @@ -478,7 +471,7 @@ bool TextClass::read(FileName const & filename, ReadType rt) case TC_NOFLOAT: if (lexrc.next()) { string const nofloat = lexrc.getString(); - floatlist_->erase(nofloat); + floatlist_.erase(nofloat); } break; } @@ -493,11 +486,11 @@ bool TextClass::read(FileName const & filename, ReadType rt) LYXERR(Debug::TCLASS, "Converting layout file from format " << format << " to " << FORMAT); FileName const tempfile = FileName::tempName(); - error = !layout2layout(filename, tempfile); - if (!error) - error = read(tempfile, rt); + bool success = layout2layout(filename, tempfile); + if (success) + read(tempfile, rt); tempfile.removeFile(); - return !error; + return success; } LYXERR(Debug::TCLASS, "Finished reading " + translateRT(rt) + ": " + @@ -539,8 +532,10 @@ bool TextClass::read(FileName const & filename, ReadType rt) min_toclevel_ = Layout::NOT_IN_TOC; max_toclevel_ = Layout::NOT_IN_TOC; - for (size_t i = 0; i != layoutCount(); ++i) { - int const toclevel = layout(i)->toclevel; + const_iterator lit = begin(); + const_iterator len = end(); + for (; lit != len; ++lit) { + int const toclevel = lit->toclevel; if (toclevel != Layout::NOT_IN_TOC) { if (min_toclevel_ == Layout::NOT_IN_TOC) min_toclevel_ = toclevel; @@ -718,8 +713,8 @@ void TextClass::readFloat(Lexer & lexrc) case FT_TYPE: lexrc.next(); type = lexrc.getString(); - if (floatlist_->typeExist(type)) { - Floating const & fl = floatlist_->getType(type); + if (floatlist_.typeExist(type)) { + Floating const & fl = floatlist_.getType(type); placement = fl.placement(); ext = fl.ext(); within = fl.within(); @@ -769,10 +764,14 @@ void TextClass::readFloat(Lexer & lexrc) if (getout) { Floating fl(type, placement, ext, within, style, name, listName, builtin); - floatlist_->newFloat(fl); + floatlist_.newFloat(fl); // each float has its own counter - counters_->newCounter(from_ascii(type), from_ascii(within), + counters_.newCounter(from_ascii(type), from_ascii(within), docstring(), docstring()); + // also define sub-float counters + docstring const subtype = "sub-" + from_ascii(type); + counters_.newCounter(subtype, from_ascii(type), + "\\alph{" + subtype + "}", docstring()); } lexrc.popTable(); @@ -818,7 +817,7 @@ void TextClass::readCounter(Lexer & lexrc) case CT_NAME: lexrc.next(); name = lexrc.getDocString(); - if (counters_->hasCounter(name)) + if (counters_.hasCounter(name)) LYXERR(Debug::TCLASS, "Reading existing counter " << to_utf8(name)); else LYXERR(Debug::TCLASS, "Reading new counter " << to_utf8(name)); @@ -846,64 +845,64 @@ void TextClass::readCounter(Lexer & lexrc) // Here if have a full counter if getout == true if (getout) - counters_->newCounter(name, within, + counters_.newCounter(name, within, labelstring, labelstring_appendix); lexrc.popTable(); } -FontInfo const & TextClass::defaultfont() const +bool TextClass::hasLayout(docstring const & n) const { - return defaultfont_; -} - + docstring const name = n.empty() ? defaultLayoutName() : n; -docstring const & TextClass::leftmargin() const -{ - return leftmargin_; + return find_if(layoutlist_.begin(), layoutlist_.end(), + LayoutNamesEqual(name)) + != layoutlist_.end(); } -docstring const & TextClass::rightmargin() const + +Layout const & TextClass::operator[](docstring const & name) const { - return rightmargin_; -} + BOOST_ASSERT(!name.empty()); + const_iterator it = + find_if(begin(), end(), LayoutNamesEqual(name)); -bool TextClass::hasLayout(docstring const & n) const -{ - docstring const name = n.empty() ? defaultLayoutName() : n; + if (it == end()) { + lyxerr << "We failed to find the layout '" << to_utf8(name) + << "' in the layout list. You MUST investigate!" + << endl; + for (const_iterator cit = begin(); cit != end(); ++cit) + lyxerr << " " << to_utf8(cit->name()) << endl; - return find_if(layoutlist_.begin(), layoutlist_.end(), - LayoutNamesEqual(name)) - != layoutlist_.end(); -} + // we require the name to exist + BOOST_ASSERT(false); + } + return *it; +} -LayoutPtr const & TextClass::operator[](docstring const & name) const +Layout & TextClass::operator[](docstring const & name) { BOOST_ASSERT(!name.empty()); - LayoutList::const_iterator cit = - find_if(layoutlist_.begin(), - layoutlist_.end(), - LayoutNamesEqual(name)); + iterator it = find_if(begin(), end(), LayoutNamesEqual(name)); - if (cit == layoutlist_.end()) { + if (it == end()) { lyxerr << "We failed to find the layout '" << to_utf8(name) << "' in the layout list. You MUST investigate!" << endl; - for (LayoutList::const_iterator it = layoutlist_.begin(); - it != layoutlist_.end(); ++it) - lyxerr << " " << to_utf8(it->get()->name()) << endl; + for (const_iterator cit = begin(); cit != end(); ++cit) + lyxerr << " " << to_utf8(cit->name()) << endl; // we require the name to exist BOOST_ASSERT(false); } - return *cit; + return *it; } @@ -949,37 +948,14 @@ bool TextClass::load(string const & path) const } -FloatList & TextClass::floats() -{ - return *floatlist_.get(); -} - - -FloatList const & TextClass::floats() const -{ - return *floatlist_.get(); -} - - -Counters & TextClass::counters() const -{ - return *counters_.get(); -} - - -// Return the layout object of an inset given by name. If the name -// is not found as such, the part after the ':' is stripped off, and -// searched again. In this way, an error fallback can be provided: -// An erroneous 'CharStyle:badname' (e.g., after a documentclass switch) -// will invoke the layout object defined by name = 'CharStyle'. -// If that doesn't work either, an empty object returns (shouldn't -// happen). -- Idea JMarc, comment MV -InsetLayout const & TextClass::insetLayout(docstring const & name) const +InsetLayout const & DocumentClass::insetLayout(docstring const & name) const { docstring n = name; + InsetLayouts::const_iterator cen = insetlayoutlist_.end(); while (!n.empty()) { - if (insetlayoutlist_.count(n) > 0) - return insetlayoutlist_[n]; + InsetLayouts::const_iterator cit = insetlayoutlist_.lower_bound(n); + if (cit != cen && cit->first == n) + return cit->second; size_t i = n.find(':'); if (i == string::npos) break; @@ -996,134 +972,62 @@ docstring const & TextClass::defaultLayoutName() const } -LayoutPtr const & TextClass::defaultLayout() const +Layout const & TextClass::defaultLayout() const { return operator[](defaultLayoutName()); } -string const & TextClass::name() const +bool TextClass::isDefaultLayout(Layout const & lay) const { - return name_; + return lay.name() == defaultLayoutName(); } -string const & TextClass::latexname() const +bool TextClass::isEmptyLayout(Layout const & lay) const { - const_cast(this)->load(); - return latexname_; + return lay.name() == emptyLayoutName(); } -string const & TextClass::description() const +DocumentClass & DocumentClassBundle::newClass(LayoutFile const & baseClass) { - return description_; + DocumentClass * dc = new DocumentClass(baseClass); + tc_list_.push_back(dc); + return *tc_list_.back(); } -string const & TextClass::opt_fontsize() const +DocumentClassBundle & DocumentClassBundle::get() { - return opt_fontsize_; + static DocumentClassBundle singleton; + return singleton; } -string const & TextClass::opt_pagestyle() const -{ - return opt_pagestyle_; -} - - -string const & TextClass::options() const -{ - return options_; -} - - -string const & TextClass::class_header() const -{ - return class_header_; -} - - -string const & TextClass::pagestyle() const -{ - return pagestyle_; -} - - -docstring const & TextClass::preamble() const -{ - return preamble_; -} - - -PageSides TextClass::sides() const -{ - return sides_; -} - - -int TextClass::secnumdepth() const -{ - return secnumdepth_; -} - +DocumentClass::DocumentClass(LayoutFile const & tc) + : TextClass(tc) +{} -int TextClass::tocdepth() const -{ - return tocdepth_; -} - -OutputType TextClass::outputType() const +bool DocumentClass::hasLaTeXLayout(std::string const & lay) const { - return outputType_; + LayoutList::const_iterator it = layoutlist_.begin(); + LayoutList::const_iterator end = layoutlist_.end(); + for (; it != end; ++it) + if (it->latexname() == lay) + return true; + return false; } -bool TextClass::provides(string const & p) const +bool DocumentClass::provides(string const & p) const { return provides_.find(p) != provides_.end(); } -unsigned int TextClass::columns() const -{ - return columns_; -} - - -TitleLatexType TextClass::titletype() const -{ - return titletype_; -} - - -string const & TextClass::titlename() const -{ - return titlename_; -} - - -int TextClass::size() const -{ - return layoutlist_.size(); -} - - -int TextClass::min_toclevel() const -{ - return min_toclevel_; -} - - -int TextClass::max_toclevel() const -{ - return max_toclevel_; -} - - -bool TextClass::hasTocLevels() const +bool DocumentClass::hasTocLevels() const { return min_toclevel_ != Layout::NOT_IN_TOC; }