]> git.lyx.org Git - lyx.git/blobdiff - src/Paragraph.cpp
* Paragraph: reserve memory by chunks of 100 chars. This improve the loading of big...
[lyx.git] / src / Paragraph.cpp
index 9e8483432d0a6905924f4c62d55b1c18a23929fe..2bd9f45f2488064b5d453c2bd74dd787fbfda86f 100644 (file)
@@ -99,7 +99,7 @@ public:
        /// \return whether a surrogate pair was output.
        bool simpleTeXBlanks(Encoding const &,
                             odocstream &, TexRow & texrow,
-                            pos_type i,
+                            pos_type i,
                             unsigned int & column,
                             Font const & font,
                             Layout const & style);
@@ -110,7 +110,7 @@ public:
        int knownLangChars(odocstream & os, value_type c, string & preamble,
                           Change &, Encoding const &, pos_type &);
        ///
-       void simpleTeXSpecialChars(Buffer const &, BufferParams const &,
+       void latexInset(Buffer const &, BufferParams const &,
                                   odocstream &,
                                   TexRow & texrow, OutputParams &,
                                   Font & running_font,
@@ -120,17 +120,17 @@ public:
                                   Change & running_change,
                                   Layout const & style,
                                   pos_type & i,
-                                  unsigned int & column, value_type const c);
+                                  unsigned int & column);
 
        ///
-       void simpleTeXSpecialChar(
+       void latexSpecialChar(
                                   odocstream & os,
                                   OutputParams & runparams,
                                   Font & running_font,
                                   Change & running_change,
+                                  Layout const & style,
                                   pos_type & i,
-                                  unsigned int & column,
-                                  value_type const c);
+                                  unsigned int & column);
 
        ///
        void validate(LaTeXFeatures & features,
@@ -186,10 +186,10 @@ struct special_phrase {
 };
 
 special_phrase const special_phrases[] = {
-       { "LyX", from_ascii("\\LyX{}"), false },
-       { "TeX", from_ascii("\\TeX{}"), true },
-       { "LaTeX2e", from_ascii("\\LaTeXe{}"), true },
-       { "LaTeX", from_ascii("\\LaTeX{}"), true },
+       { "LyX", from_ascii("\\protect\\LyX{}"), false },
+       { "TeX", from_ascii("\\protect\\TeX{}"), true },
+       { "LaTeX2e", from_ascii("\\protect\\LaTeXe{}"), true },
+       { "LaTeX", from_ascii("\\protect\\LaTeX{}"), true },
 };
 
 size_t const phrases_nr = sizeof(special_phrases)/sizeof(special_phrase);
@@ -489,7 +489,7 @@ int Paragraph::Private::latexSurrogatePair(odocstream & os, value_type c,
 
 bool Paragraph::Private::simpleTeXBlanks(Encoding const & encoding,
                                       odocstream & os, TexRow & texrow,
-                                      pos_type i,
+                                      pos_type i,
                                       unsigned int & column,
                                       Font const & font,
                                       Layout const & style)
@@ -502,7 +502,6 @@ bool Paragraph::Private::simpleTeXBlanks(Encoding const & encoding,
                if (Encodings::isCombiningChar(next)) {
                        // This space has an accent, so we must always output it.
                        column += latexSurrogatePair(os, ' ', next, encoding) - 1;
-                       ++i;
                        return true;
                }
        }
@@ -614,7 +613,7 @@ bool Paragraph::Private::isTextAt(string const & str, pos_type pos) const
 }
 
 
-void Paragraph::Private::simpleTeXSpecialChars(Buffer const & buf,
+void Paragraph::Private::latexInset(Buffer const & buf,
                                             BufferParams const & bparams,
                                             odocstream & os,
                                             TexRow & texrow,
@@ -626,32 +625,16 @@ void Paragraph::Private::simpleTeXSpecialChars(Buffer const & buf,
                                             Change & running_change,
                                             Layout const & style,
                                             pos_type & i,
-                                            unsigned int & column,
-                                            value_type const c)
+                                            unsigned int & column)
 {
-       if (style.pass_thru) {
-               if (c != Paragraph::META_INSET) {
-                       if (c != '\0')
-                               // FIXME UNICODE: This can fail if c cannot
-                               // be encoded in the current encoding.
-                               os.put(c);
-               } else
-                       owner_->getInset(i)->plaintext(buf, os, runparams);
-               return;
-       }
+       Inset * inset = owner_->getInset(i);
+       BOOST_ASSERT(inset);
 
-       // Two major modes:  LaTeX or plain
-       // Handle here those cases common to both modes
-       // and then split to handle the two modes separately.
-       if (c != Paragraph::META_INSET) {
-               simpleTeXSpecialChar(os, runparams, running_font, running_change,
-                       i, column, c);
+       if (style.pass_thru) {
+               inset->plaintext(buf, os, runparams);
                return;
        }
 
-       Inset * inset = owner_->getInset(i);
-       BOOST_ASSERT(inset);
-
        // FIXME: move this to InsetNewline::latex
        if (inset->lyxCode() == NEWLINE_CODE) {
                // newlines are handled differently here than
@@ -766,15 +749,30 @@ void Paragraph::Private::simpleTeXSpecialChars(Buffer const & buf,
 }
 
 
-void Paragraph::Private::simpleTeXSpecialChar(
+void Paragraph::Private::latexSpecialChar(
                                             odocstream & os,
                                             OutputParams & runparams,
                                             Font & running_font,
                                             Change & running_change,
+                                            Layout const & style,
                                             pos_type & i,
-                                            unsigned int & column,
-                                            value_type const c)
+                                            unsigned int & column)
 {
+       value_type const c = getChar(i);
+
+       if (style.pass_thru) {
+               if (c != '\0')
+                       // FIXME UNICODE: This can fail if c cannot
+                       // be encoded in the current encoding.
+                       os.put(c);
+               return;
+       }
+
+       if (runparams.verbatim) {
+               os.put(c);
+               return;
+       }
+
        switch (c) {
        case '\\':
                os << "\\textbackslash{}";
@@ -990,6 +988,7 @@ Paragraph::Paragraph()
 {
        itemdepth = 0;
        d->params_.clear();
+       text_.reserve(100);
 }
 
 
@@ -1140,6 +1139,38 @@ void Paragraph::insert(pos_type start, docstring const & str,
 }
 
 
+void Paragraph::appendChar(value_type c, Font const & font,
+               Change const & change)
+{
+       // track change
+       d->changes_.insert(change, text_.size());
+       // when appending characters, no need to update tables
+       text_.push_back(c);
+       setFont(text_.size() - 1, font);
+}
+
+
+void Paragraph::appendString(docstring const & s, Font const & font,
+               Change const & change)
+{
+       size_t end = s.size();
+       size_t oldsize = text_.size();
+       size_t newsize = oldsize + end;
+       size_t capacity = text_.capacity();
+       if (newsize >= capacity)
+               text_.reserve(std::max(capacity + 100, newsize));
+
+       // FIXME: Optimize this!
+       for (pos_type i = 0; i != end; ++i) {
+               // track change
+               d->changes_.insert(change, i);
+               // when appending characters, no need to update tables
+               text_.push_back(s[i]);
+       }
+       d->fontlist_.setRange(oldsize, newsize, font);
+}
+
+
 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
                           bool trackChanges)
 {
@@ -1741,7 +1772,7 @@ int Paragraph::endTeXParParams(BufferParams const & bparams,
 
 
 // This one spits out the text of the paragraph
-bool Paragraph::simpleTeXOnePar(Buffer const & buf,
+bool Paragraph::latex(Buffer const & buf,
                                BufferParams const & bparams,
                                Font const & outerfont,
                                odocstream & os, TexRow & texrow,
@@ -1863,8 +1894,6 @@ bool Paragraph::simpleTeXOnePar(Buffer const & buf,
 
                ++column;
 
-               value_type const c = getChar(i);
-
                // Fully instantiated font
                Font const font = getFont(bparams, i, outerfont);
 
@@ -1894,6 +1923,8 @@ bool Paragraph::simpleTeXOnePar(Buffer const & buf,
                        }
                }
 
+               value_type const c = getChar(i);
+
                // Do we need to change font?
                if ((font != running_font ||
                     font.language() != running_font.language()) &&
@@ -1916,21 +1947,22 @@ bool Paragraph::simpleTeXOnePar(Buffer const & buf,
                }
 
                if (c == ' ') {
+                       // FIXME: integrate this case in latexSpecialChar
                        // Do not print the separation of the optional argument
                        // if style->pass_thru is false. This works because
-                       // simpleTeXSpecialChars ignores spaces if
+                       // latexSpecialChar ignores spaces if
                        // style->pass_thru is false.
                        if (i != body_pos - 1) {
                                if (d->simpleTeXBlanks(
                                                *(runparams.encoding), os, texrow,
-                                               i, column, font, *style))
+                                               i, column, font, *style)) {
                                        // A surrogate pair was output. We
-                                       // must not call simpleTeXSpecialChars
-                                       // in this iteration, since
-                                       // simpleTeXBlanks incremented i, and
-                                       // simpleTeXSpecialChars would output
+                                       // must not call latexSpecialChar
+                                       // in this iteration, since it would output
                                        // the combining character again.
+                                       ++i;
                                        continue;
+                               }
                        }
                }
 
@@ -1938,10 +1970,18 @@ bool Paragraph::simpleTeXOnePar(Buffer const & buf,
                rp.free_spacing = style->free_spacing;
                rp.local_font = &font;
                rp.intitle = style->intitle;
-               d->simpleTeXSpecialChars(buf, bparams, os,
+
+               // Two major modes:  LaTeX or plain
+               // Handle here those cases common to both modes
+               // and then split to handle the two modes separately.
+               if (c == Paragraph::META_INSET)
+                       d->latexInset(buf, bparams, os,
                                        texrow, rp, running_font,
                                        basefont, outerfont, open_font,
-                                       runningChange, *style, i, column, c);
+                                       runningChange, *style, i, column);
+               else
+                       d->latexSpecialChar(os, rp, running_font, runningChange,
+                               *style, i, column);
 
                // Set the encoding to that returned from simpleTeXSpecialChars (see
                // comment for encoding member in OutputParams.h)
@@ -2499,4 +2539,18 @@ Inset const * Paragraph::getInset(pos_type pos) const
        return d->insetlist_.get(pos);
 }
 
+
+int Paragraph::numberOfOptArgs() const
+{
+       int num = 0;
+       InsetList::const_iterator it = insetList().begin();
+       InsetList::const_iterator end = insetList().end();
+       for (; it != end ; ++it) {
+               if (it->inset->lyxCode() == OPTARG_CODE)
+                       ++num;
+       }
+       return num;
+}
+
+
 } // namespace lyx