On Mon, Jul 14, 2008 at 03:07:10PM +0200, Jürgen Spitzmüller wrote:

> Enrico Forestieri wrote:
> > He cannot get LateX output. I think that the problem is due to the fact
> > that mathed doesn't even try to see whether a character could be encoded
> > in the current encoding, and only the unicodesymbols file is used to
> > see whether a latex translation is available. When the character is not
> > found, that message is issued. The fix would be to simply try to encode
> > the character before going the unicodesymbols file route.
> 
> I guess you are right.

That would be the attached patch. It works for me. Please test.
Note that unicode support in mathed is still flaky (even if it is
better than in the past). For example, if you set the document
language to Greek and then use \textrm{\'e} in mathed, maybe you
really meant to obtain an accented epsilon, but if you input é,
you definitely don't want it. There's much room for improvements.
However, I don't know if we have to care for all these details,
essentially due to the fact that math mode in TeX was not really
thought for more-than-7 bit characters.

-- 
Enrico
Index: src/mathed/InsetMathHull.cpp
===================================================================
--- src/mathed/InsetMathHull.cpp        (revisione 25623)
+++ src/mathed/InsetMathHull.cpp        (copia locale)
@@ -1561,7 +1561,7 @@
 }
 
 
-int InsetMathHull::plaintext(odocstream & os, OutputParams const &) const
+int InsetMathHull::plaintext(odocstream & os, OutputParams const & runparams) 
const
 {
        if (0 && display()) {
                Dimension dim;
@@ -1575,7 +1575,7 @@
                return tpain.textheight();
        } else {
                odocstringstream oss;
-               WriteStream wi(oss, false, true, false);
+               WriteStream wi(oss, false, true, false, runparams.encoding);
                wi << cell(0);
 
                docstring const str = oss.str();
@@ -1607,7 +1607,7 @@
                // Workaround for db2latex: db2latex always includes equations 
with
                // \ensuremath{} or \begin{display}\end{display}
                // so we strip LyX' math environment
-               WriteStream wi(ls, false, false, false);
+               WriteStream wi(ls, false, false, false, runparams.encoding);
                InsetMathGrid::write(wi);
                ms << from_utf8(subst(subst(to_utf8(ls.str()), "&", "&amp;"), 
"<", "&lt;"));
                ms << ETag("alt");
Index: src/mathed/InsetMathString.cpp
===================================================================
--- src/mathed/InsetMathString.cpp      (revisione 25623)
+++ src/mathed/InsetMathString.cpp      (copia locale)
@@ -121,7 +121,7 @@
                char_type const c = *cit;
                try {
                        docstring command(1, c);
-                       if (c < 0x80 || Encodings::latexMathChar(c, command)) {
+                       if (c < 0x80 || Encodings::latexMathChar(c, 
os.encoding(), command)) {
                                if (os.textMode()) {
                                        if (in_forced_mode) {
                                                // we were inside \lyxmathsym
Index: src/mathed/InsetFormulaMacro.cpp
===================================================================
--- src/mathed/InsetFormulaMacro.cpp    (revisione 25623)
+++ src/mathed/InsetFormulaMacro.cpp    (copia locale)
@@ -74,16 +74,17 @@
                             OutputParams const & runparams) const
 {
        //lyxerr << "InsetFormulaMacro::latex" << endl;
-       WriteStream wi(os, runparams.moving_arg, true, runparams.dryrun);
+       WriteStream wi(os, runparams.moving_arg, true, runparams.dryrun,
+                       runparams.encoding);
        tmpl()->write(wi);
        return 2;
 }
 
 
-int InsetFormulaMacro::plaintext(odocstream & os, OutputParams const &) const
+int InsetFormulaMacro::plaintext(odocstream & os, OutputParams const & 
runparams) const
 {
        odocstringstream oss;
-       WriteStream wi(oss, false, true, false);
+       WriteStream wi(oss, false, true, false, runparams.encoding);
        tmpl()->write(wi);
 
        docstring const str = oss.str();
Index: src/mathed/MathStream.cpp
===================================================================
--- src/mathed/MathStream.cpp   (revisione 25623)
+++ src/mathed/MathStream.cpp   (copia locale)
@@ -107,17 +107,18 @@
 }
 
 
-WriteStream::WriteStream(odocstream & os, bool fragile, bool latex, bool 
dryrun)
+WriteStream::WriteStream(odocstream & os, bool fragile, bool latex, bool 
dryrun,
+                       Encoding const * encoding)
        : os_(os), fragile_(fragile), firstitem_(false), latex_(latex),
          dryrun_(dryrun), pendingspace_(false), pendingbrace_(false),
-         textmode_(false), line_(0)
+         textmode_(false), line_(0), encoding_(encoding)
 {}
 
 
 WriteStream::WriteStream(odocstream & os)
        : os_(os), fragile_(false), firstitem_(false), latex_(false),
          dryrun_(false), pendingspace_(false), pendingbrace_(false),
-         textmode_(false), line_(0)
+         textmode_(false), line_(0), encoding_(0)
 {}
 
 
Index: src/mathed/InsetMathNest.cpp
===================================================================
--- src/mathed/InsetMathNest.cpp        (revisione 25623)
+++ src/mathed/InsetMathNest.cpp        (copia locale)
@@ -361,7 +361,8 @@
 
 int InsetMathNest::latex(odocstream & os, OutputParams const & runparams) const
 {
-       WriteStream wi(os, runparams.moving_arg, true, runparams.dryrun);
+       WriteStream wi(os, runparams.moving_arg, true, runparams.dryrun,
+                       runparams.encoding);
        write(wi);
        return wi.line();
 }
Index: src/mathed/MathStream.h
===================================================================
--- src/mathed/MathStream.h     (revisione 25623)
+++ src/mathed/MathStream.h     (copia locale)
@@ -21,9 +21,10 @@
 
 namespace lyx {
 
-class MathData;
+class Encoding;
 class InsetMath;
 class MathAtom;
+class MathData;
 
 //
 // LaTeX/LyX
@@ -32,7 +33,8 @@
 class WriteStream {
 public:
        ///
-       WriteStream(odocstream & os, bool fragile, bool latex, bool dryrun);
+       WriteStream(odocstream & os, bool fragile, bool latex, bool dryrun,
+               Encoding const * encoding = 0);
        ///
        explicit WriteStream(odocstream & os);
        ///
@@ -63,6 +65,8 @@
        void textMode(bool textmode);
        /// tell whether we are in text mode or not when producing latex code
        bool textMode() const { return textmode_; }
+       /// LaTeX encoding
+       Encoding const * encoding() const { return encoding_; }
 private:
        ///
        odocstream & os_;
@@ -82,6 +86,8 @@
        bool textmode_;
        ///
        int line_;
+       ///
+       Encoding const * encoding_;
 };
 
 ///
Index: src/Encoding.cpp
===================================================================
--- src/Encoding.cpp    (revisione 25623)
+++ src/Encoding.cpp    (copia locale)
@@ -333,7 +333,7 @@
 }
 
 
-docstring Encoding::latexChar(char_type c) const
+docstring Encoding::latexChar(char_type c, bool for_mathed) const
 {
        // assure the used encoding is properly initialized
        init();
@@ -344,6 +344,8 @@
                return docstring(1, c);
        if (encodable_.find(c) != encodable_.end())
                return docstring(1, c);
+       if (for_mathed)
+               return docstring();
 
        // c cannot (or should not) be encoded in this encoding
        CharInfoMap::const_iterator const it = unicodesymbols.find(c);
@@ -375,8 +377,14 @@
 }
 
 
-bool Encodings::latexMathChar(char_type c, docstring & command)
+bool Encodings::latexMathChar(char_type c, Encoding const * encoding,
+                               docstring & command)
 {
+       if (encoding) {
+               command = encoding->latexChar(c, true);
+               if (!command.empty())
+                       return false;
+       }
        CharInfoMap::const_iterator const it = unicodesymbols.find(c);
        if (it == unicodesymbols.end())
                throw EncodingException(c);
Index: src/Encoding.h
===================================================================
--- src/Encoding.h      (revisione 25623)
+++ src/Encoding.h      (copia locale)
@@ -71,7 +71,7 @@
         * LaTeX macro is known, a warning is given of lyxerr, and the
         * character is returned.
         */
-       docstring latexChar(char_type c) const;
+       docstring latexChar(char_type c, bool for_mathed = false) const;
        /// Which LaTeX package handles this encoding?
        Package package() const { return package_; }
        /// A list of all characters usable in this encoding
@@ -178,10 +178,11 @@
         */
        static bool isForced(char_type c);
        /**
-        * Convert \p c to something that LaTeX can understand in math mode.
+        * If \p c cannot be encoded in the given \p encoding, convert
+        * it to something that LaTeX can understand in math mode.
         * \return whether \p command is a math mode command
         */
-       static bool latexMathChar(char_type c, docstring & command);
+       static bool latexMathChar(char_type c, Encoding const * encoding, 
docstring & command);
 
        /**
         * Convert the LaTeX command in \p cmd to the corresponding unicode

Reply via email to