]> git.lyx.org Git - features.git/commitdiff
Prevent invalid latex for multilingual sections and inputenc == auto.
authorGeorg Baum <Georg.Baum@post.rwth-aachen.de>
Mon, 16 Apr 2007 18:06:01 +0000 (18:06 +0000)
committerGeorg Baum <Georg.Baum@post.rwth-aachen.de>
Mon, 16 Apr 2007 18:06:01 +0000 (18:06 +0000)
We output the \inputencoding command before the section if possible, and we
ignore all encoding changes in the section. Commands from the unicodesymbols
file will be used for characters that can't be encoded in the current
encoding instead of switching the encoding.

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

src/encoding.C
src/encoding.h
src/output_latex.C
src/output_latex.h
src/paragraph.C
src/paragraph_pimpl.C
src/paragraph_pimpl.h

index b1a41fd396f5958dc1e7ea5130a83bbdb9bdbf1d..2bfb4a8a2fdb3aae7a02fb5be6bee78a3648a711 100644 (file)
@@ -179,11 +179,8 @@ docstring const Encoding::latexChar(char_type c) const
 }
 
 
-void Encoding::validate(char_type c, LaTeXFeatures & features) const
+void Encodings::validate(char_type c, LaTeXFeatures & features)
 {
-       // Add the preamble stuff even if c can be encoded in this encoding,
-       // since the inputenc package only maps the code point c to a command,
-       // it does not make this command available.
        CharInfoMap::const_iterator const it = unicodesymbols.find(c);
        if (it != unicodesymbols.end() && !it->second.preamble.empty()) {
                if (it->second.feature)
index f07fb645a47090ea27c9622a6d8bbdbb7c6177c1..61ee7c68112a52d87530dcedec6213581e373716 100644 (file)
@@ -48,9 +48,6 @@ public:
         * character is returned.
         */
        docstring const latexChar(char_type c) const;
-       /// Add the preamble snippet needed for the output of latexChar(c)
-       /// to \p features.
-       void validate(char_type c, LaTeXFeatures & features) const;
 private:
        ///
        std::string Name_;
@@ -123,6 +120,14 @@ public:
        static char_type transformChar(char_type c, Letter_Form form);
        /// Is this a combining char?
        static bool isCombiningChar(char_type c);
+       /**
+        * Add the preamble snippet needed for the output of \p c to
+        * \p features.
+        * This does not depend on the used encoding, since the inputenc
+        * package only maps the code point \p c to a command, it does not
+        * make this command available.
+        */
+       static void validate(char_type c, LaTeXFeatures & features);
 
 private:
        ///
index 138d560e2cc7524c94fc7e6d3d0a737106abd60e..f1f43f48a596a728cfc0804f38f22686c0f924f1 100644 (file)
@@ -292,6 +292,33 @@ TeXOnePar(Buffer const & buf,
                }
        }
 
+       // Switch file encoding if necessary
+       if (bparams.inputenc == "auto") {
+               // Look ahead for future encoding changes.
+               // We try to output them at the beginning of the paragraph,
+               // since the \inputencoding command is not allowed e.g. in
+               // sections.
+               for (pos_type i = 0; i < pit->size(); ++i) {
+                       char_type const c = pit->getChar(i);
+                       if (c < 0x80)
+                               continue;
+                       if (pit->isInset(i))
+                               break;
+                       // All characters before c are in the ASCII range, and
+                       // c is non-ASCII (but no inset), so change the
+                       // encoding to that required by the language of c.
+                       Encoding const * const encoding =
+                               pit->getFontSettings(bparams, i).language()->encoding();
+                       if (switchEncoding(os, bparams, false,
+                                          *(runparams.encoding), *encoding) > 0) {
+                               runparams.encoding = encoding;
+                               os << '\n';
+                               texrow.newline();
+                       }
+                       break;
+               }
+       }
+
        // In an inset with unlimited length (all in one row),
        // don't allow any special options in the paragraph
        if (!pit->forceDefaultParagraphs()) {
@@ -563,13 +590,17 @@ void latexParagraphs(Buffer const & buf,
 
 
 int switchEncoding(odocstream & os, BufferParams const & bparams,
-                   Encoding const & oldEnc, Encoding const & newEnc)
+                   bool moving_arg, Encoding const & oldEnc,
+                   Encoding const & newEnc)
 {
        // FIXME thailatex does not support the inputenc package, so we
        // ignore switches from/to tis620-0 encoding here. This does of
        // course only work as long as the non-thai text contains ASCII
        // only, but it is the best we can do.
-       if ((bparams.inputenc == "auto" || bparams.inputenc == "default") &&
+       // Since the \inputencoding command does not work inside sections
+       // we ignore the encoding switch also in moving arguments.
+       if (((bparams.inputenc == "auto" && !moving_arg) ||
+            bparams.inputenc == "default") &&
            oldEnc.name() != newEnc.name() &&
            oldEnc.name() != "ascii" && newEnc.name() != "ascii" &&
            oldEnc.name() != "tis620-0" && newEnc.name() != "tis620-0") {
index ca6618c7a727bc707999895de5598eb628b28167..ef18a081f370ff21ca32bfd7b43b99ea85785314 100644 (file)
@@ -45,7 +45,8 @@ void latexParagraphs(Buffer const & buf,
 /// Switch the encoding of \p os from \p oldEnc to \p newEnc if needed.
 /// \return the number of characters written to \p os.
 int switchEncoding(odocstream & os, BufferParams const & bparams,
-                   Encoding const & oldEnc, Encoding const & newEnc);
+                   bool moving_arg, Encoding const & oldEnc,
+                   Encoding const & newEnc);
 
 } // namespace lyx
 
index 0e3cb22475a6683548ca52813439d1b0c58750fc..22626a73a5f7bf970fb05ed6ee500f5b97d7e8ba 100644 (file)
@@ -1007,9 +1007,6 @@ bool Paragraph::simpleTeXOnePar(Buffer const & buf,
                                                    runparams.moving_arg);
        }
 
-       // Computed only once per paragraph since bparams.encoding() is expensive
-       Encoding const & doc_encoding = bparams.encoding();
-
        for (pos_type i = 0; i < size(); ++i) {
                // First char in paragraph or after label?
                if (i == body_pos) {
@@ -1076,7 +1073,7 @@ bool Paragraph::simpleTeXOnePar(Buffer const & buf,
 
                // Switch file encoding if necessary
                int const count = switchEncoding(os, bparams,
-                               *(runparams.encoding),
+                               runparams.moving_arg, *(runparams.encoding),
                                *(font.language()->encoding()));
                if (count > 0) {
                        column += count;
@@ -1101,7 +1098,7 @@ bool Paragraph::simpleTeXOnePar(Buffer const & buf,
                        // style->pass_thru is false.
                        if (i != body_pos - 1) {
                                if (pimpl_->simpleTeXBlanks(bparams,
-                                               doc_encoding, os, texrow,
+                                               *(runparams.encoding), os, texrow,
                                                i, column, font, *style))
                                        // A surrogate pair was output. We
                                        // must not call simpleTeXSpecialChars
@@ -1117,7 +1114,7 @@ bool Paragraph::simpleTeXOnePar(Buffer const & buf,
                rp.free_spacing = style->free_spacing;
                rp.local_font = &font;
                rp.intitle = style->intitle;
-               pimpl_->simpleTeXSpecialChars(buf, bparams, doc_encoding, os,
+               pimpl_->simpleTeXSpecialChars(buf, bparams, os,
                                        texrow, rp, running_font,
                                        basefont, outerfont, open_font,
                                        runningChangeType, *style, i, column, c);
index 55126353775f5feb1a434b0c82e7b3a7058ad5ba..70b21edfe66a6a68b7247eb71cfaaca22013bdaf 100644 (file)
@@ -58,18 +58,6 @@ special_phrase const special_phrases[] = {
 
 size_t const phrases_nr = sizeof(special_phrases)/sizeof(special_phrase);
 
-
-/// Get the real encoding of a character with font \p font.
-/// doc_encoding == bparams.encoding(), but we use a precomputed variable
-/// since bparams.encoding() is expensive
-inline Encoding const & getEncoding(BufferParams const & bparams,
-               Encoding const & doc_encoding, LyXFont const & font)
-{
-       if (bparams.inputenc == "auto" || bparams.inputenc == "default")
-               return *(font.language()->encoding());
-       return doc_encoding;
-}
-
 } // namespace anon
 
 
@@ -398,7 +386,7 @@ int Paragraph::Pimpl::latexSurrogatePair(odocstream & os, value_type c,
 
 
 bool Paragraph::Pimpl::simpleTeXBlanks(BufferParams const & bparams,
-                                       Encoding const & doc_encoding,
+                                       Encoding const & encoding,
                                        odocstream & os, TexRow & texrow,
                                        pos_type & i,
                                       unsigned int & column,
@@ -412,7 +400,6 @@ bool Paragraph::Pimpl::simpleTeXBlanks(BufferParams const & bparams,
                char_type next = getChar(i + 1);
                if (Encodings::isCombiningChar(next)) {
                        // This space has an accent, so we must always output it.
-                       Encoding const & encoding = getEncoding(bparams, doc_encoding, font);
                        column += latexSurrogatePair(os, ' ', next, encoding) - 1;
                        ++i;
                        return true;
@@ -478,7 +465,6 @@ bool Paragraph::Pimpl::isTextAt(string const & str, pos_type pos) const
 
 void Paragraph::Pimpl::simpleTeXSpecialChars(Buffer const & buf,
                                             BufferParams const & bparams,
-                                            Encoding const & doc_encoding,
                                             odocstream & os,
                                             TexRow & texrow,
                                             OutputParams const & runparams,
@@ -738,8 +724,7 @@ void Paragraph::Pimpl::simpleTeXSpecialChars(Buffer const & buf,
                        }
 
                        if (pnr == phrases_nr && c != '\0') {
-                               Encoding const & encoding =
-                                       getEncoding(bparams, doc_encoding, running_font);
+                               Encoding const & encoding = *(runparams.encoding);
                                if (i < size() - 1) {
                                        char_type next = getChar(i + 1);
                                        if (Encodings::isCombiningChar(next)) {
@@ -837,7 +822,6 @@ void Paragraph::Pimpl::validate(LaTeXFeatures & features,
        }
 
        // then the contents
-       Encoding const & doc_encoding = bparams.encoding();
        for (pos_type i = 0; i < size() ; ++i) {
                for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
                        if (!special_phrases[pnr].builtin
@@ -846,12 +830,7 @@ void Paragraph::Pimpl::validate(LaTeXFeatures & features,
                                break;
                        }
                }
-               // We do not need the completely realized font, since we are
-               // only interested in the language, and that is never inherited.
-               // Therefore we can use getFontSettings instead of getFont.
-               LyXFont const & font = owner_->getFontSettings(bparams, i);
-               Encoding const & encoding = getEncoding(bparams, doc_encoding, font);
-               encoding.validate(getChar(i), features);
+               Encodings::validate(getChar(i), features);
        }
 }
 
index df899b695db235082778e9e99b41d4ebadd16959..7d4aba0c585ed17647bd009ccf93daeafed98d62 100644 (file)
@@ -139,7 +139,7 @@ public:
                             LyXLayout const & style);
        ///
        void simpleTeXSpecialChars(Buffer const &, BufferParams const &,
-                                  Encoding const &, odocstream &,
+                                  odocstream &,
                                   TexRow & texrow, OutputParams const &,
                                   LyXFont & running_font,
                                   LyXFont & basefont,