]> git.lyx.org Git - features.git/commitdiff
Fix "default" encoding to match "auto" encoding except for the actual output
authorDov Feldstern <dov@lyx.org>
Thu, 5 Jul 2007 19:19:41 +0000 (19:19 +0000)
committerDov Feldstern <dov@lyx.org>
Thu, 5 Jul 2007 19:19:41 +0000 (19:19 +0000)
of \inputencoding commands, which are not needed in some cases (e.g., Hebrew
with ivritex).

This fixes some of the remaining problems (but not all) from bug 3613 (namely, http://bugzilla.lyx.org/show_bug.cgi?id=3613#c9).
See also some example files fixed by this in http://permalink.gmane.org/gmane.editors.lyx.devel/88805 .

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

src/Font.cpp
src/Paragraph.cpp
src/output_latex.cpp
src/output_latex.h

index 74f45e5230b27c3f0927440d15cb18fb41b8b3dd..ea1b8c17a37ca14c4ad873c7dfc2ca15f69842ac 100644 (file)
@@ -40,6 +40,7 @@ using support::subst;
 using std::endl;
 using std::string;
 using std::ostream;
+using std::pair;
 
 #ifndef CXX_GLOBAL_CSTD
 using std::strlen;
@@ -785,12 +786,12 @@ int Font::latexWriteStartChanges(odocstream & os, BufferParams const & bparams,
        }
 
        if (language()->encoding()->package() == Encoding::CJK) {
-               int const c = switchEncoding(os, bparams,
+               pair<bool, int> const c = switchEncoding(os, bparams,
                                runparams.moving_arg, *(runparams.encoding),
                                *(language()->encoding()));
-               if (c > 0) {
+               if (c.first) {
                        open_encoding_ = true;
-                       count += c;
+                       count += c.second;
                        runparams.encoding = language()->encoding();
                }
        }
@@ -943,11 +944,11 @@ int Font::latexWriteEndChanges(odocstream & os, BufferParams const & bparams,
                // We need to close the encoding even if it does not change
                // to do correct environment nesting
                Encoding const * const ascii = encodings.getFromLyXName("ascii");
-               int const c = switchEncoding(os, bparams,
+               pair<bool, int> const c = switchEncoding(os, bparams,
                                runparams.moving_arg, *(runparams.encoding),
                                *ascii);
-               BOOST_ASSERT(c > 0);
-               count += c;
+               BOOST_ASSERT(c.first);
+               count += c.second;
                runparams.encoding = ascii;
                open_encoding_ = false;
        }
index 8054bb9daaf181b00fc90110da76374f7d37f89c..de4021db3be9e3f178119f467327a28d08e7c7c9 100644 (file)
@@ -2056,11 +2056,11 @@ bool Paragraph::simpleTeXOnePar(Buffer const & buf,
                // Switch file encoding if necessary
                if (runparams.encoding->package() == Encoding::inputenc &&
                    font.language()->encoding()->package() == Encoding::inputenc) {
-                       int const count = switchEncoding(os, bparams,
+                       std::pair<bool, int> const enc_switch = switchEncoding(os, bparams,
                                        runparams.moving_arg, *(runparams.encoding),
                                        *(font.language()->encoding()));
-                       if (count > 0) {
-                               column += count;
+                       if (enc_switch.first) {
+                               column += enc_switch.second;
                                runparams.encoding = font.language()->encoding();
                        }
                }
index a6a48d3da6395680477cbcc3dda9bad5ab520bc4..55c2e5d546c745b78898f723b0e52e8b932359b7 100644 (file)
@@ -37,6 +37,8 @@ using support::subst;
 
 using std::endl;
 using std::string;
+using std::pair;
+using std::make_pair;
 
 
 namespace {
@@ -295,7 +297,9 @@ TeXOnePar(Buffer const & buf,
                }
        }
 
-       // Switch file encoding if necessary
+       // Switch file encoding if necessary; no need to do this for "default"
+       // encoding, since this only affects the position of the outputted
+       // \inputencoding command; the encoding switch will occur when necessary
        if (bparams.inputenc == "auto" &&
            runparams.encoding->package() == Encoding::inputenc) {
                // Look ahead for future encoding changes.
@@ -313,12 +317,14 @@ TeXOnePar(Buffer const & buf,
                        // encoding to that required by the language of c.
                        Encoding const * const encoding =
                                pit->getFontSettings(bparams, i).language()->encoding();
-                       if (encoding->package() == Encoding::inputenc &&
-                           switchEncoding(os, bparams, false,
-                                          *(runparams.encoding), *encoding) > 0) {
+                       pair<bool, int> enc_switch = switchEncoding(os, bparams, false,
+                                       *(runparams.encoding), *encoding);
+                       if (encoding->package() == Encoding::inputenc && enc_switch.first) {
                                runparams.encoding = encoding;
-                               os << '\n';
-                               texrow.newline();
+                               if (enc_switch.second > 0) {
+                                       os << '\n';
+                                       texrow.newline();
+                               }
                        }
                        break;
                }
@@ -598,17 +604,17 @@ void latexParagraphs(Buffer const & buf,
 }
 
 
-int switchEncoding(odocstream & os, BufferParams const & bparams,
+pair<bool, int> switchEncoding(odocstream & os, BufferParams const & bparams,
                   bool moving_arg, Encoding const & oldEnc,
                   Encoding const & newEnc)
 {
-       if ((bparams.inputenc != "auto" || moving_arg)
-               && bparams.inputenc != "default")
-               return 0;
+       if ((bparams.inputenc != "auto" && bparams.inputenc != "default")
+               || moving_arg)
+               return make_pair(false, 0);
 
        // Do nothing if the encoding is unchanged.
        if (oldEnc.name() == newEnc.name())
-               return 0;
+               return make_pair(false, 0);
 
        // FIXME We ignore encoding switches from/to encodings that do
        // neither support the inputenc package nor the CJK package here.
@@ -617,19 +623,20 @@ int switchEncoding(odocstream & os, BufferParams const & bparams,
        // but it is the best we can do
        if (oldEnc.package() == Encoding::none
                || newEnc.package() == Encoding::none)
-               return 0;
+               return make_pair(false, 0);
 
        LYXERR(Debug::LATEX) << "Changing LaTeX encoding from "
                << oldEnc.name() << " to "
                << newEnc.name() << endl;
        os << setEncoding(newEnc.iconvName());
        if (bparams.inputenc == "default")
-               return 0;
+               return make_pair(true, 0);
 
        docstring const inputenc(from_ascii(newEnc.latexName()));
        switch (newEnc.package()) {
                case Encoding::none:
-                       return 0;
+                       // shouldn't ever reach here, see above
+                       return make_pair(true, 0);
                case Encoding::inputenc: {
                        int count = inputenc.length();
                        if (oldEnc.package() == Encoding::CJK) {
@@ -637,7 +644,7 @@ int switchEncoding(odocstream & os, BufferParams const & bparams,
                                count += 9;
                        }
                        os << "\\inputencoding{" << inputenc << '}';
-                       return count + 16;
+                       return make_pair(true, count + 16);
                 }
                case Encoding::CJK: {
                        int count = inputenc.length();
@@ -646,11 +653,11 @@ int switchEncoding(odocstream & os, BufferParams const & bparams,
                                count += 9;
                        }
                        os << "\\begin{CJK}{" << inputenc << "}{}";
-                       return count + 15;
+                       return make_pair(true, count + 15);
                }
        }
        // Dead code to avoid a warning:
-       return 0;
+       return make_pair(true, 0);
 }
 
 } // namespace lyx
index f108f1b360c7591cf4ff26143764933d3d1f2ca3..008564ed7942aede56fc44e352ad99be6d46bb64 100644 (file)
@@ -12,6 +12,8 @@
 #ifndef OUTPUT_LATEX_H
 #define OUTPUT_LATEX_H
 
+#include <utility>
+
 #include "support/docstream.h"
 
 
@@ -43,10 +45,11 @@ void latexParagraphs(Buffer const & buf,
                     std::string const & everypar = std::string());
 
 /// 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,
-                  bool moving_arg, Encoding const & oldEnc,
-                  Encoding const & newEnc);
+/// \return (did the encoding change?, number of characters written to \p os)
+std::pair<bool, int> switchEncoding(odocstream & os, 
+                    BufferParams const & bparams,
+                    bool moving_arg, Encoding const & oldEnc,
+                    Encoding const & newEnc);
 
 } // namespace lyx