]> git.lyx.org Git - lyx.git/blobdiff - src/encoding.C
Fix bug 845 (now really).
[lyx.git] / src / encoding.C
index 9c8f15c67d9432fa7d14913a1840aabda42aa09b..a4a5f4932ae2f80bd82c31125079373e2bdcec37 100644 (file)
 #include "encoding.h"
 
 #include "debug.h"
+#include "LaTeXFeatures.h"
 #include "lyxlex.h"
 #include "lyxrc.h"
 
+#include "support/filename.h"
+#include "support/lstrings.h"
+#include "support/unicode.h"
+
+#include <sstream>
+
 
 namespace lyx {
 
+using support::FileName;
+
 #ifndef CXX_GLOBAL_CSTD
 using std::strtol;
 #endif
@@ -33,79 +42,6 @@ Encodings encodings;
 
 namespace {
 
-char_type tab_iso8859_1[256] = {
-   0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
-   0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
-   0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
-   0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
-   0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
-   0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
-   0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
-   0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0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0021, 0x2200, 0x0023, 0x2203, 0x0025, 0x0026, 0x220b,
-   0x0028, 0x0029, 0x002a, 0x002b, 0x002c, 0x002d, 0x002e, 0x002f,
-
-   0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
-
-//   0x2245, 0x0391, 0x0392, 0x03a7, 0x0394, 0x0395, 0x03a6, 0x0393,
-//   0x0397, 0x0399, 0x03d1, 0x039a, 0x039b, 0x039c, 0x039d, 0x039f,
-
-//   0x03a0, 0x0398, 0x03a1, 0x03a3, 0x03a4, 0x03a5, 0x03c2, 0x03a9,
-//   0x039e, 0x03a8, 0x0396, 0x005b, 0x2234, 0x005d, 0x22a5, 0xffff,
-   0x2245, 0x0041, 0x0042, 0x0058, 0x0394, 0x0045, 0x03c2, 0x03a9,
-   0x0048, 0x0049, 0x03d1, 0x004b, 0x039b, 0x004d, 0x004e, 0x004f,
-
-   0x03a0, 0x0398, 0x0050, 0x03a3, 0x0054, 0x0059, 0x03c2, 0x03a9,
-   0x039e, 0x03a8, 0x005a, 0x005b, 0x2234, 0x005d, 0x22a5, 0xffff,
-
-   0xffff, 0x03b1, 0x03b2, 0x03c7, 0x03b4, 0x03b5, 0x03d5, 0x03b3,
-   0x03b7, 0x03b9, 0x03c6, 0x03ba, 0x03bb, 0x03bc, 0x03bd, 0x03bf,
-
-   0x03c0, 0x03b8, 0x03c1, 0x03c3, 0x03c4, 0x03c5, 0x03d6, 0x03c9,
-   0x03be, 0x03c8, 0x03b6, 0x007b, 0x007c, 0x007d, 0x007e, 0xffff,
-
-   0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
-   0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
-
-   0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
-   0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
-
-   0xffff, 0x03d2, 0x2032, 0x2264, 0x2215, 0x221e, 0xffff, 0x2663,
-   0x2666, 0x2665, 0x2660, 0x2194, 0x2190, 0x2191, 0x2192, 0x2193,
-
-   0x00b0, 0x00b1, 0x2033, 0x2265, 0x00d7, 0x221d, 0x2202, 0x2022,
-   0x00f7, 0x2260, 0x2261, 0x2248, 0x22ef, 0xffff, 0xffff, 0x21b5,
-
-   0x2135, 0x2111, 0x211c, 0x2118, 0x2297, 0x2295, 0x2205, 0x2229,
-   0x222a, 0x2283, 0x2287, 0x2284, 0x2282, 0x2286, 0x2208, 0x2209,
-
-   0x2220, 0x2207, 0x00ae, 0x00a9, 0x2122, 0x220f, 0x221a, 0x22c5,
-   0x00ac, 0x2227, 0x2228, 0x21d4, 0x21d0, 0x21d1, 0x21d2, 0x21d3,
-
-   0x2662, 0x2329, 0x00ae, 0x00a9, 0x2122, 0x2211, 0xffff, 0xffff,
-   0xffff, 0x2308, 0xffff, 0x230a, 0xffff, 0xffff, 0xffff, 0xffff,
-
-   0xffff, 0x232a, 0x222b, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
-   0xffff, 0x2309, 0xffff, 0x230b, 0xffff, 0xffff, 0xffff, 0xffff
-};
-#endif
-
-
 char_type arabic_table2[63][4] = {
        {0x41, 0x41, 0x41, 0x41}, // 0xc1 = hamza
        {0x42, 0xa1, 0x42, 0xa1}, // 0xc2 = ligature madda on alef
@@ -248,14 +184,94 @@ char_type arabic_table[63][2] = {
 
 char_type const arabic_start = 0xc1;
 
+
+/// Information about a single UCS4 character
+struct CharInfo {
+       /// LaTeX command for this character
+       docstring command;
+       /// Needed LaTeX preamble (or feature)
+       string preamble;
+       /// Is this a combining character?
+       bool combining;
+       /// Is \c preamble a feature known by LaTeXFeatures, or a raw LaTeX
+       /// command?
+       bool feature;
+       /// Always force the LaTeX command, even if the encoding contains
+       /// this character?
+       bool force;
+};
+
+
+typedef std::map<char_type, CharInfo> CharInfoMap;
+CharInfoMap unicodesymbols;
+
 } // namespace anon
 
 
+Encoding::Encoding(string const & n, string const & l, string const & i)
+       : Name_(n), LatexName_(l), iconvName_(i)
+{
+       if (n == "ascii")
+               // ASCII can encode 128 code points and nothing else
+               start_encodable_ = 128;
+       if (n == "utf8" || n == "utf8x")
+               // UTF8 can encode all 1<<20 + 1<<16 UCS4 code points
+               start_encodable_ = 0x110000;
+       else {
+               start_encodable_ = 0;
+               // temporarily switch off lyxerr, since we will generate iconv errors
+               lyxerr.disable();
+               for (unsigned short j = 0; j < 256; ++j) {
+                       char const c = j;
+                       std::vector<char_type> const ucs4 = eightbit_to_ucs4(&c, 1, i);
+                       if (ucs4.size() == 1) {
+                               char_type const c = ucs4[0];
+                               CharInfoMap::const_iterator const it = unicodesymbols.find(c);
+                               if (it == unicodesymbols.end() || !it->second.force)
+                                       encodable_.insert(c);
+                       }
+               }
+               lyxerr.enable();
+               CharSet::iterator it = encodable_.find(start_encodable_);
+               while (it != encodable_.end()) {
+                       encodable_.erase(it);
+                       ++start_encodable_;
+                       it = encodable_.find(start_encodable_);
+               }
+       }
+}
+
+
+docstring const Encoding::latexChar(char_type c) const
+{
+       if (c < start_encodable_)
+               return docstring(1, c);
+       if (encodable_.find(c) == encodable_.end()) {
+               // c cannot be encoded in this encoding
+               CharInfoMap::const_iterator const it = unicodesymbols.find(c);
+               if (it == unicodesymbols.end())
+                       lyxerr << "Could not find LaTeX command for character 0x"
+                              << std::hex << c << ".\nLaTeX export will fail."
+                              << endl;
+               else
+                       return it->second.command;
+       }
+       return docstring(1, c);
+}
+
 
-char_type Encoding::ucs(char_type c) const
+void Encoding::validate(char_type c, LaTeXFeatures & features) const
 {
-       BOOST_ASSERT(c < 256);
-       return encoding_table[c];
+       // 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)
+                       features.require(it->second.preamble);
+               else
+                       features.addPreambleSnippet(it->second.preamble);
+       }
 }
 
 
@@ -304,28 +320,106 @@ char_type Encodings::transformChar(char_type c,
 }
 
 
-Encoding const * Encodings::getEncoding(string const & encoding) const
+bool Encodings::isCombiningChar(char_type c)
+{
+       CharInfoMap::const_iterator const it = unicodesymbols.find(c);
+       if (it != unicodesymbols.end())
+               return it->second.combining;
+       return false;
+}
+
+
+Encoding const * Encodings::getFromLyXName(string const & name) const
 {
-       EncodingList::const_iterator it = encodinglist.find(encoding);
+       EncodingList::const_iterator it = encodinglist.find(name);
        if (it != encodinglist.end())
                return &it->second;
        else
                return 0;
 }
 
+
+Encoding const * Encodings::getFromLaTeXName(string const & name) const
+{
+       // We don't use std::find_if because it makes copies of the pairs in
+       // the map.
+       // This linear search is OK since we don't have many encodings.
+       // Users could even optimize it by putting the encodings they use
+       // most at the top of lib/encodings.
+       EncodingList::const_iterator const end = encodinglist.end();
+       for (EncodingList::const_iterator it = encodinglist.begin(); it != end; ++it)
+               if (it->second.latexName() == name)
+                       return &it->second;
+       return 0;
+}
+
+
 Encodings::Encodings()
 {
-       encodinglist["iso8859-1"] = Encoding("iso8859-1", "latin1", tab_iso8859_1);
-       symbol_encoding_ =
-#ifdef USE_UNICODE_FOR_SYMBOLS
-               Encoding("symbol", "", tab_symbol);
-#else
-               Encoding("symbol", "", tab_iso8859_1);
-#endif
 }
 
-void Encodings::read(string const & filename)
+
+void Encodings::read(FileName const & encfile, FileName const & symbolsfile)
 {
+       // We must read the symbolsfile first, because the Encoding
+       // constructor depends on it.
+       LyXLex symbolslex(0, 0);
+       symbolslex.setFile(symbolsfile);
+       while (symbolslex.isOK()) {
+               char_type symbol;
+               CharInfo info;
+               string flags;
+
+               if (symbolslex.next(true)) {
+                       std::istringstream is(symbolslex.getString());
+                       // reading symbol directly does not work if
+                       // char_type == std::wchar_t.
+                       boost::uint32_t tmp;
+                       if(!(is >> std::hex >> tmp))
+                               break;
+                       symbol = tmp;
+               } else
+                       break;
+               if (symbolslex.next(true))
+                       info.command = symbolslex.getDocString();
+               else
+                       break;
+               if (symbolslex.next(true))
+                       info.preamble = symbolslex.getString();
+               else
+                       break;
+               if (symbolslex.next(true))
+                       flags = symbolslex.getString();
+               else
+                       break;
+
+               info.combining = false;
+               info.force = false;
+               while (!flags.empty()) {
+                       string flag;
+                       flags = support::split(flags, flag, ',');
+                       if (flag == "combining")
+                               info.combining = true;
+                       else if (flag == "force")
+                               info.force = true;
+                       else
+                               lyxerr << "Ignoring unknown flag `" << flag
+                                      << "' for symbol `0x" << std::hex
+                                      << symbol << "'." << endl;
+               }
+
+               if (!info.preamble.empty())
+                       info.feature = info.preamble[0] != '\\';
+
+               lyxerr[Debug::INFO]
+                       << "Read unicode symbol " << symbol << " '"
+                       << to_utf8(info.command) << "' '" << info.preamble
+                       << "' " << info.combining << ' ' << info.feature
+                       << endl;
+               unicodesymbols[symbol] = info;
+       }
+
+       // Now read the encodings
        enum Encodingtags {
                et_encoding = 1,
                et_end,
@@ -338,7 +432,7 @@ void Encodings::read(string const & filename)
        };
 
        LyXLex lex(encodingtags, et_last - 1);
-       lex.setFile(filename);
+       lex.setFile(encfile);
        while (lex.isOK()) {
                switch (lex.lex()) {
                case et_encoding:
@@ -347,14 +441,10 @@ void Encodings::read(string const & filename)
                        string const name = lex.getString();
                        lex.next();
                        string const latexname = lex.getString();
+                       lex.next();
+                       string const iconvname = lex.getString();
                        lyxerr[Debug::INFO] << "Reading encoding " << name << endl;
-                       char_type table[256];
-                       for (unsigned int i = 0; i < 256; ++i) {
-                               lex.next();
-                               string const tmp = lex.getString();
-                               table[i] = ::strtol(tmp.c_str(), 0 , 16);
-                       }
-                       encodinglist[name] = Encoding(name, latexname, table);
+                       encodinglist[name] = Encoding(name, latexname, iconvname);
                        if (lex.lex() != et_end)
                                lex.printError("Encodings::read: "
                                               "missing end");