]> git.lyx.org Git - lyx.git/commitdiff
* Paragraph:
authorAbdelrazak Younes <younes@lyx.org>
Mon, 22 Oct 2007 13:09:16 +0000 (13:09 +0000)
committerAbdelrazak Younes <younes@lyx.org>
Mon, 22 Oct 2007 13:09:16 +0000 (13:09 +0000)
- simpleTeXOnePar(): renamed to latex(), beginning of de-spaghettization by truly splitting inset and character cases.
- appendChar(), appendString(): new method for buffer reading.

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@21117 a592a061-630c-0410-9148-cb99ea01b6c8

src/OutputParams.h
src/Paragraph.cpp
src/Paragraph.h
src/Text.cpp
src/output_latex.cpp

index dc2bc365f7da19987c096e76bd99132674703e2a..066366b5c3566422da653a5a53f1443a6ea71230 100644 (file)
@@ -73,7 +73,7 @@ public:
        /** Current stream encoding. Only used for LaTeX.
            This must be set to the document encoding (via the constructor)
            before output starts. Afterwards it must be kept up to date for
-           each single character (\see Paragraph::simpleTeXOnePar).
+           each single character (\sa Paragraph::latex).
            This does also mean that you need to set it back if you use a
            copy (e.g. in insets): \code
            int InsetFoo::latex(..., OutputParams const & runparams_in) const
index 70dfc40617959a4faf97cbbce2ab7a3e42c9f102..60549da19c94b0decf901249870db146df7cade1 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,
@@ -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,25 @@ 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;
@@ -1145,6 +1138,33 @@ 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();
+       // FIXME: Optimize this!
+       text_.reserve(text_.size() + end);
+       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]);
+               setFont(i, font);
+       }
+}
+
+
 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
                           bool trackChanges)
 {
@@ -1746,7 +1766,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,
@@ -1868,8 +1888,6 @@ bool Paragraph::simpleTeXOnePar(Buffer const & buf,
 
                ++column;
 
-               value_type const c = getChar(i);
-
                // Fully instantiated font
                Font const font = getFont(bparams, i, outerfont);
 
@@ -1899,6 +1917,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()) &&
@@ -1921,21 +1941,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;
+                               }
                        }
                }
 
@@ -1943,10 +1964,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)
index 9a5bc37704aae39f73a324014e0586c9ce06e0d2..2425e2490b1f3da33990e859b3f15804b4abe6be 100644 (file)
@@ -128,7 +128,7 @@ public:
 
 
        ///
-       bool simpleTeXOnePar(Buffer const &, BufferParams const &,
+       bool latex(Buffer const &, BufferParams const &,
                             Font const & outerfont, odocstream &,
                             TexRow & texrow, OutputParams const &) const;
 
@@ -292,6 +292,12 @@ public:
        ///
        void insert(pos_type pos, docstring const & str,
                    Font const & font, Change const & change);
+
+       ///
+       void appendString(docstring const & s, Font const & font,
+               Change const & change);
+       ///
+       void appendChar(value_type c, Font const & font, Change const & change);
        ///
        void insertChar(pos_type pos, value_type c, bool trackChanges);
        ///
index 50b6a83f7611ee4fedbfab10d5a9d509ff4bd780..2cf62f75183b8e61fb9e89df01e3d022b1f068b3 100644 (file)
@@ -102,17 +102,9 @@ void readParToken(Buffer const & buf, Paragraph & par, Lexer & lex,
        BufferParams const & bp = buf.params();
 
        if (token[0] != '\\') {
-#if 0
-               string::const_iterator cit = token.begin();
-               for (; cit != token.end(); ++cit)
-                       par.insertChar(par.size(), (*cit), font, change);
-#else
                docstring dstr = lex.getDocString();
-               docstring::const_iterator cit = dstr.begin();
-               docstring::const_iterator cend = dstr.end();
-               for (; cit != cend; ++cit)
-                       par.insertChar(par.size(), *cit, font, change);
-#endif
+               par.appendString(dstr, font, change);
+
        } else if (token == "\\begin_layout") {
                lex.eatLine();
                docstring layoutname = lex.getDocString();
@@ -216,12 +208,12 @@ void readParToken(Buffer const & buf, Paragraph & par, Lexer & lex,
                // Insets don't make sense in a free-spacing context! ---Kayvan
                if (par.isFreeSpacing()) {
                        if (token == "\\InsetSpace")
-                               par.insertChar(par.size(), ' ', font, change);
+                               par.appendChar(' ', font, change);
                        else if (lex.isOK()) {
                                lex.next();
                                string const next_token = lex.getString();
                                if (next_token == "\\-")
-                                       par.insertChar(par.size(), '-', font, change);
+                                       par.appendChar('-', font, change);
                                else {
                                        lex.printError("Token `$$Token' "
                                                       "is in free space "
@@ -239,7 +231,7 @@ void readParToken(Buffer const & buf, Paragraph & par, Lexer & lex,
                                        font, change);
                }
        } else if (token == "\\backslash") {
-               par.insertChar(par.size(), '\\', font, change);
+               par.appendChar('\\', font, change);
        } else if (token == "\\newline") {
                auto_ptr<Inset> inset(new InsetNewline);
                inset->read(buf, lex);
index 4d357131a6d300fc2fdeb3f48eb506b7275a3ff4..f5ed124b4507b3d59611139d0da606dd4d25a30f 100644 (file)
@@ -441,7 +441,7 @@ TeXOnePar(Buffer const & buf,
 
        // FIXME UNICODE
        os << from_utf8(everypar);
-       bool need_par = pit->simpleTeXOnePar(buf, bparams, outerfont,
+       bool need_par = pit->latex(buf, bparams, outerfont,
                                             os, texrow, runparams);
 
        // Make sure that \\par is done with the font of the last