]> git.lyx.org Git - features.git/commitdiff
Fix bug 3522.
authorJürgen Spitzmüller <spitz@lyx.org>
Mon, 28 May 2007 07:43:15 +0000 (07:43 +0000)
committerJürgen Spitzmüller <spitz@lyx.org>
Mon, 28 May 2007 07:43:15 +0000 (07:43 +0000)
* src/BufferParams.{cpp,h} (writeLaTeX):
- move inputenc generation to an own function,
   writeEncodingPreamble

* src/graphics/PreviewLoader.cpp (startLoading):
- use the inputenc command and encoding of the buffer
  by using the aforementioned new function.

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

src/BufferParams.cpp
src/BufferParams.h
src/Thesaurus.cpp
src/Thesaurus.h
src/graphics/PreviewLoader.cpp

index c958a2dc5854933468b0fd2dc1d149a525f1ee6c..916756e147630b6536a7eb189cfdf6cd05525f0b 100644 (file)
@@ -906,61 +906,8 @@ bool BufferParams::writeLaTeX(odocstream & os, LaTeXFeatures & features,
                texrow.newline();
        }
 
-       if (inputenc == "auto") {
-               string const doc_encoding =
-                       language->encoding()->latexName();
-               Encoding::Package const package =
-                       language->encoding()->package();
-
-               // Create a list with all the input encodings used
-               // in the document
-               std::set<string> encodings =
-                       features.getEncodingSet(doc_encoding);
-
-               if (!encodings.empty() || package == Encoding::inputenc) {
-                       os << "\\usepackage[";
-                       std::set<string>::const_iterator it = encodings.begin();
-                       std::set<string>::const_iterator const end = encodings.end();
-                       if (it != end) {
-                               os << from_ascii(*it);
-                               ++it;
-                       }
-                       for (; it != end; ++it)
-                               os << ',' << from_ascii(*it);
-                       if (package == Encoding::inputenc) {
-                               if (!encodings.empty())
-                                       os << ',';
-                               os << from_ascii(doc_encoding);
-                       }
-                       os << "]{inputenc}\n";
-                       texrow.newline();
-               }
-               if (package == Encoding::CJK) {
-                       os << "\\usepackage{CJK}\n";
-                       texrow.newline();
-               }
-       } else if (inputenc != "default") {
-               switch (language->encoding()->package()) {
-               case Encoding::none:
-                       break;
-               case Encoding::inputenc:
-                       os << "\\usepackage[" << from_ascii(inputenc)
-                          << "]{inputenc}\n";
-                       texrow.newline();
-                       break;
-               case Encoding::CJK:
-                       os << "\\usepackage{CJK}\n";
-                       texrow.newline();
-                       break;
-               }
-       }
-
-       // The encoding "armscii8" is only available when the package "armtex" is loaded.
-       // armscii8 is used for Armenian.
-       if (language->encoding()->latexName() == "armscii8" || inputenc == "armscii8") {
-               os << "\\usepackage{armtex}\n";
-               texrow.newline();
-       }
+       // handle inputenc etc.
+       os << writeEncodingPreamble(features, texrow);
 
        if (!listings_params.empty()) {
                os << "\\usepackage{listings}\n";
@@ -1422,6 +1369,71 @@ string const BufferParams::babelCall(string const & lang_opts) const
 }
 
 
+docstring const BufferParams::writeEncodingPreamble(LaTeXFeatures & features,
+                             TexRow & texrow) const
+{
+       odocstringstream os;
+
+       if (inputenc == "auto") {
+               string const doc_encoding =
+                       language->encoding()->latexName();
+               Encoding::Package const package =
+                       language->encoding()->package();
+
+               // Create a list with all the input encodings used
+               // in the document
+               std::set<string> encodings =
+                       features.getEncodingSet(doc_encoding);
+
+               if (!encodings.empty() || package == Encoding::inputenc) {
+                       os << "\\usepackage[";
+                       std::set<string>::const_iterator it = encodings.begin();
+                       std::set<string>::const_iterator const end = encodings.end();
+                       if (it != end) {
+                               os << from_ascii(*it);
+                               ++it;
+                       }
+                       for (; it != end; ++it)
+                               os << ',' << from_ascii(*it);
+                       if (package == Encoding::inputenc) {
+                               if (!encodings.empty())
+                                       os << ',';
+                               os << from_ascii(doc_encoding);
+                       }
+                       os << "]{inputenc}\n";
+                       texrow.newline();
+               }
+               if (package == Encoding::CJK) {
+                       os << "\\usepackage{CJK}\n";
+                       texrow.newline();
+               }
+       } else if (inputenc != "default") {
+               switch (language->encoding()->package()) {
+               case Encoding::none:
+                       break;
+               case Encoding::inputenc:
+                       os << "\\usepackage[" << from_ascii(inputenc)
+                          << "]{inputenc}\n";
+                       texrow.newline();
+                       break;
+               case Encoding::CJK:
+                       os << "\\usepackage{CJK}\n";
+                       texrow.newline();
+                       break;
+               }
+       }
+
+       // The encoding "armscii8" is only available when the package "armtex" is loaded.
+       // armscii8 is used for Armenian.
+       if (language->encoding()->latexName() == "armscii8" || inputenc == "armscii8") {
+               os << "\\usepackage{armtex}\n";
+               texrow.newline();
+       }
+
+       return os.str();
+}
+
+
 string const BufferParams::loadFonts(string const & rm,
                                     string const & sf, string const & tt,
                                     bool const & sc, bool const & osf,
index 45c8fbb4e2087f79a335c660215dce48b77a8d38..11049c3380f1da54579c12aac0c51a4e5ca66043 100644 (file)
@@ -253,6 +253,9 @@ public:
        std::string const paperSizeName() const;
        /// set up if and how babel is called
        std::string const babelCall(std::string const & lang_opts) const;
+       /// handle inputenc etc.
+       docstring const writeEncodingPreamble(LaTeXFeatures & features,
+                                             TexRow & texrow) const;
        /// set up the document fonts
        std::string const loadFonts(std::string const & rm,
                                     std::string const & sf, std::string const & tt,
index 14c82f61d3c68605a8ad56393dfc1307b21a32bc..c9e9e8f3b1293ec4fc427faf3ddbaad636aac63e 100644 (file)
@@ -4,6 +4,7 @@
  * Licence details can be found in the file COPYING.
  *
  * \author John Levon
+ * \author Jürgen Spitzmüller
  *
  * Full author contact details are available in file CREDITS.
  */
 
 #include "Thesaurus.h"
 
+#include "debug.h"
 #include "support/lstrings.h"
+#include "support/unicode.h"
 
 #include <algorithm>
 
 
 namespace lyx {
 
-#ifdef HAVE_LIBAIKSAURUS
-
 using std::sort;
 using std::string;
+using std::endl;
 
 
+#ifndef HAVE_LIBMYTHES
+#ifdef HAVE_LIBAIKSAURUS
 Thesaurus::Thesaurus()
-       : aik_(new Aiksaurus)
+       : thes_(new Aiksaurus)
 {}
 
 
 Thesaurus::~Thesaurus()
 {
-       delete aik_;
+       delete thes_;
 }
 
 
@@ -49,7 +53,7 @@ Thesaurus::Meanings Thesaurus::lookup(docstring const & t)
                return meanings;
 
        string const text = to_ascii(t);
-       if (!aik_->find(text.c_str()))
+       if (!thes_->find(text.c_str()))
                return meanings;
 
        // weird api, but ...
@@ -59,19 +63,19 @@ Thesaurus::Meanings Thesaurus::lookup(docstring const & t)
        docstring meaning;
 
        // correct, returns "" at the end
-       string ret = aik_->next(cur_meaning);
+       string ret = thes_->next(cur_meaning);
 
        while (!ret.empty()) {
                if (cur_meaning != prev_meaning) {
                        meaning = from_ascii(ret);
-                       ret = aik_->next(cur_meaning);
+                       ret = thes_->next(cur_meaning);
                        prev_meaning = cur_meaning;
                } else {
                        if (ret != text)
                                meanings[meaning].push_back(from_ascii(ret));
                }
 
-               ret = aik_->next(cur_meaning);
+               ret = thes_->next(cur_meaning);
        }
 
        for (Meanings::iterator it = meanings.begin();
@@ -81,6 +85,88 @@ Thesaurus::Meanings Thesaurus::lookup(docstring const & t)
        return meanings;
 }
 
+#endif // HAVE_LIBAIKSAURUS
+#endif // !HAVE_LIBMYTHES
+
+
+#ifdef HAVE_LIBMYTHES
+
+namespace {
+
+string const to_iconv_encoding(docstring const & s, string const & encoding)
+{
+       std::vector<char> const encoded =
+               ucs4_to_eightbit(s.data(), s.length(), encoding);
+       return string(encoded.begin(), encoded.end());
+}
+
+
+docstring const from_iconv_encoding(string const & s, string const & encoding)
+{
+       std::vector<char_type> const ucs4 =
+               eightbit_to_ucs4(s.data(), s.length(), encoding);
+       return docstring(ucs4.begin(), ucs4.end());
+}
+
+} // namespace anon
+
+
+Thesaurus::Thesaurus()
+{
+       string const idx("/home/juergen/updates/MyThes-1.0/th_de_DE_v2.idx");
+       string const data("/home/juergen/updates/MyThes-1.0/th_de_DE_v2.dat");
+       char const * af = idx.c_str();
+       char const * df = data.c_str();
+       thes_ = new MyThes(af, df);
+}
+
+
+Thesaurus::~Thesaurus()
+{
+       if (thes_)
+               delete thes_;
+}
+
+
+Thesaurus::Meanings Thesaurus::lookup(docstring const & t)
+{
+       Meanings meanings;
+
+       string const encoding = thes_->get_th_encoding();
+       
+       mentry * pmean;
+       string const text = to_iconv_encoding(support::lowercase(t), encoding);
+       int len = strlen(text.c_str());
+       int count = thes_->Lookup(text.c_str(), len, &pmean);
+       if (!count)
+               return meanings;
+
+       // don't change value of pmean or count
+       // they are needed for the CleanUpAfterLookup routine
+       mentry * pm = pmean;
+       docstring meaning;
+       docstring ret;
+       for (int i = 0; i < count; i++) {
+               meaning = from_iconv_encoding(string(pm->defn), encoding);
+               // remove silly item
+               if (support::prefixIs(meaning, '-'))
+                       meaning = support::ltrim(meaning, "- ");
+               for (int j = 0; j < pm->count; j++) {
+                       ret = from_iconv_encoding(string(pm->psyns[j]), encoding);
+               }
+       meanings[meaning].push_back(ret);
+       pm++;
+       }
+        // now clean up all allocated memory
+        thes_->CleanUpAfterLookup(&pmean, count);
+
+       for (Meanings::iterator it = meanings.begin();
+            it != meanings.end(); ++it)
+               sort(it->second.begin(), it->second.end());
+
+       return meanings;
+}
+
 #else
 
 Thesaurus::Thesaurus()
@@ -98,7 +184,7 @@ Thesaurus::Meanings Thesaurus::lookup(docstring const &)
        return Meanings();
 }
 
-#endif // HAVE_LIBAIKSAURUS
+#endif // HAVE_LIBMYTHES
 
 // Global instance
 Thesaurus thesaurus;
index e63f2935236108b73cddbdb0104e5692aa6e515c..f051d102ee0737f973dbdb88275960146e630c02 100644 (file)
 #include <vector>
 #include <map>
 
+#ifdef HAVE_LIBMYTHES
+#include MYTHES_H_LOCATION
+#else
 #ifdef HAVE_LIBAIKSAURUS
 #include AIKSAURUS_H_LOCATION
-#endif
+#endif // HAVE_LIBAIKSAURUS
+#endif // !HAVE_LIBMYTHES
 
 namespace lyx {
 
@@ -42,9 +46,13 @@ public:
        Meanings lookup(docstring const & text);
 
 private:
+#ifdef HAVE_LIBMYTHES
+       MyThes * thes_;
+#else
 #ifdef HAVE_LIBAIKSAURUS
-       Aiksaurus * aik_;
-#endif
+       Aiksaurus * thes_;
+#endif // HAVE_LIBAIKSAURUS
+#endif // !HAVE_LIBMYTHES
 };
 
 extern Thesaurus thesaurus;
index 986346aa9b006a83c814db5f8d973f1b2d466ab9..a06ca467fa161e788b7493cd617bd2bbe0756e71 100644 (file)
 #include "GraphicsCache.h"
 
 #include "Buffer.h"
+#include "BufferParams.h"
 #include "Converter.h"
 #include "debug.h"
+#include "Encoding.h"
 #include "Format.h"
 #include "InsetIterator.h"
 #include "Color.h"
+#include "LaTeXFeatures.h"
 #include "LyXRC.h"
+#include "output.h"
 #include "OutputParams.h"
 #include "Paragraph.h"
+#include "TexRow.h"
 
 #include "frontends/Application.h" // hexName
 
@@ -570,10 +575,16 @@ void PreviewLoader::Impl::startLoading()
        // Output the LaTeX file.
        FileName const latexfile(filename_base + ".tex");
 
-       // FIXME UNICODE
-       // This creates an utf8 encoded file, but the proper inputenc
-       // command is missing.
-       odocfstream of(latexfile.toFilesystemEncoding().c_str());
+       // we use the encoding of the buffer
+       Encoding const & enc = buffer_.params().encoding();
+       odocfstream of(enc.iconvName());
+       TexRow texrow;
+       OutputParams runparams(&enc);
+       LaTeXFeatures features(buffer_, buffer_.params(), runparams);
+
+       if (!openFileWrite(of, latexfile))
+               return;
+
        if (!of) {
                LYXERR(Debug::GRAPHICS) << "PreviewLoader::startLoading()\n"
                                        << "Unable to create LaTeX file\n"
@@ -582,10 +593,18 @@ void PreviewLoader::Impl::startLoading()
        }
        of << "\\batchmode\n";
        dumpPreamble(of);
+       // handle inputenc etc.
+       of << buffer_.params().writeEncodingPreamble(features, texrow);
        of << "\n\\begin{document}\n";
        dumpData(of, inprogress.snippets);
        of << "\n\\end{document}\n";
        of.close();
+       if (of.fail()) {
+               LYXERR(Debug::GRAPHICS)  << "PreviewLoader::startLoading()\n"
+                                        << "File was not closed properly."
+                                        << endl;
+               return;
+       }
 
        // The conversion command.
        ostringstream cs;