From: Jürgen Spitzmüller Date: Mon, 28 May 2007 07:43:15 +0000 (+0000) Subject: Fix bug 3522. X-Git-Tag: 1.6.10~9599 X-Git-Url: https://git.lyx.org/gitweb/?a=commitdiff_plain;ds=sidebyside;h=d2d31b9130a3a0eafef6378864baaddde90999f7;p=features.git Fix bug 3522. * 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 --- diff --git a/src/BufferParams.cpp b/src/BufferParams.cpp index c958a2dc58..916756e147 100644 --- a/src/BufferParams.cpp +++ b/src/BufferParams.cpp @@ -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 encodings = - features.getEncodingSet(doc_encoding); - - if (!encodings.empty() || package == Encoding::inputenc) { - os << "\\usepackage["; - std::set::const_iterator it = encodings.begin(); - std::set::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 encodings = + features.getEncodingSet(doc_encoding); + + if (!encodings.empty() || package == Encoding::inputenc) { + os << "\\usepackage["; + std::set::const_iterator it = encodings.begin(); + std::set::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, diff --git a/src/BufferParams.h b/src/BufferParams.h index 45c8fbb4e2..11049c3380 100644 --- a/src/BufferParams.h +++ b/src/BufferParams.h @@ -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, diff --git a/src/Thesaurus.cpp b/src/Thesaurus.cpp index 14c82f61d3..c9e9e8f3b1 100644 --- a/src/Thesaurus.cpp +++ b/src/Thesaurus.cpp @@ -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. */ @@ -12,27 +13,30 @@ #include "Thesaurus.h" +#include "debug.h" #include "support/lstrings.h" +#include "support/unicode.h" #include 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 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 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; diff --git a/src/Thesaurus.h b/src/Thesaurus.h index e63f293523..f051d102ee 100644 --- a/src/Thesaurus.h +++ b/src/Thesaurus.h @@ -17,9 +17,13 @@ #include #include +#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; diff --git a/src/graphics/PreviewLoader.cpp b/src/graphics/PreviewLoader.cpp index 986346aa9b..a06ca467fa 100644 --- a/src/graphics/PreviewLoader.cpp +++ b/src/graphics/PreviewLoader.cpp @@ -15,14 +15,19 @@ #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;