]> git.lyx.org Git - lyx.git/blobdiff - src/Encoding.cpp
Revert unintentional commits.
[lyx.git] / src / Encoding.cpp
index e08095cf2d82831a06e16c28887f420cef47ef19..efbb1dc8e8bfc2abeddb38e0c70f93540e365a6c 100644 (file)
@@ -247,6 +247,9 @@ struct CharInfo {
 typedef map<char_type, CharInfo> CharInfoMap;
 CharInfoMap unicodesymbols;
 
+typedef std::set<char_type> CharSet;
+CharSet forced;
+
 
 /// The highest code point in UCS4 encoding (1<<20 + 1<<16)
 char_type const max_ucs4 = 0x110000;
@@ -266,9 +269,9 @@ const char * EncodingException::what() const throw()
 }
 
 
-Encoding::Encoding(string const & n, string const & l, string const & i,
-                  bool f, Encoding::Package p)
-       : name_(n), latexName_(l), iconvName_(i), fixedwidth_(f), package_(p)
+Encoding::Encoding(string const & n, string const & l, string const & g,
+                  string const & i, bool f, Encoding::Package p)
+       : name_(n), latexName_(l), guiName_(g), iconvName_(i), fixedwidth_(f), package_(p)
 {
        if (n == "ascii") {
                // ASCII can encode 128 code points and nothing else
@@ -335,7 +338,9 @@ docstring Encoding::latexChar(char_type c) const
        // assure the used encoding is properly initialized
        init();
 
-       if (c < start_encodable_)
+       if (iconvName_ == "UTF-8" && package_ == none)
+               return docstring(1, c);
+       if (c < start_encodable_ && !encodings.isForced(c))
                return docstring(1, c);
        if (encodable_.find(c) != encodable_.end())
                return docstring(1, c);
@@ -386,6 +391,99 @@ bool Encodings::latexMathChar(char_type c, docstring & command)
 }
 
 
+char_type Encodings::fromLaTeXCommand(docstring const & cmd, bool & combining)
+{
+       CharInfoMap::const_iterator const end = unicodesymbols.end();
+       CharInfoMap::const_iterator it = unicodesymbols.begin();
+       for (; it != end; ++it) {
+               docstring const math = it->second.mathcommand;
+               docstring const text = it->second.textcommand;
+               if (math == cmd || text == cmd) {
+                       combining = it->second.combining;
+                       return it->first;
+               }
+       }
+       return 0;
+}
+
+
+docstring Encodings::fromLaTeXCommand(docstring const & cmd, docstring & rem)
+{
+       docstring symbols;
+       size_t i = 0;
+       size_t const cmdend = cmd.size();
+       CharInfoMap::const_iterator const uniend = unicodesymbols.end();
+       for (size_t j = 0; j < cmdend; ++j) {
+               // Also get the char after a backslash
+               if (j + 1 < cmdend && cmd[j] == '\\')
+                       ++j;
+               // If a macro argument follows, get it, too
+               if (j + 1 < cmdend && cmd[j + 1] == '{') {
+                       size_t k = j + 1;
+                       int count = 1;
+                       while (k < cmdend && count && k != docstring::npos) {
+                               k = cmd.find_first_of(from_ascii("{}"), k + 1);
+                               if (cmd[k] == '{')
+                                       ++count;
+                               else
+                                       --count;
+                       }
+                       if (k != docstring::npos)
+                               j = k;
+               }
+               // Start with this substring and try augmenting it when it is
+               // the prefix of some command in the unicodesymbols file
+               docstring const subcmd = cmd.substr(i, j - i + 1);
+
+               CharInfoMap::const_iterator it = unicodesymbols.begin();
+               size_t unicmd_size = 0;
+               char_type c = 0;
+               for (; it != uniend; ++it) {
+                       docstring const math = it->second.mathcommand;
+                       docstring const text = it->second.textcommand;
+                       size_t cur_size = max(math.size(), text.size());
+                       // The current math or text unicode command cannot
+                       // match, or we already matched a longer one
+                       if (cur_size < subcmd.size() || cur_size <= unicmd_size)
+                               continue;
+
+                       docstring tmp = subcmd;
+                       size_t k = j;
+                       while (prefixIs(math, tmp) || prefixIs(text, tmp)) {
+                               ++k;
+                               if (k >= cmdend || cur_size <= tmp.size())
+                                       break;
+                               tmp += cmd[k];
+                       }
+                       // No match
+                       if (k == j)
+                               continue;
+
+                       // The last added char caused a mismatch, because
+                       // we didn't exhaust the chars in cmd and didn't
+                       // exceed the maximum size of the current unicmd
+                       if (k < cmdend && cur_size > tmp.size())
+                               tmp.resize(tmp.size() - 1);
+
+                       // If this is an exact match, we found a (longer)
+                       // matching command in the unicodesymbols file
+                       if (math == tmp || text == tmp) {
+                               c = it->first;
+                               j = k - 1;
+                               i = j + 1;
+                               unicmd_size = cur_size;
+                       }
+               }
+               if (unicmd_size)
+                       symbols += c;
+               else if (j + 1 == cmdend)
+                       // No luck. Return what remains
+                       rem = cmd.substr(i);
+       }
+       return symbols;
+}
+
+
 void Encodings::validate(char_type c, LaTeXFeatures & features, bool for_mathed)
 {
        CharInfoMap::const_iterator const it = unicodesymbols.find(c);
@@ -480,6 +578,12 @@ bool Encodings::isKnownScriptChar(char_type const c, string & preamble)
 }
 
 
+bool Encodings::isForced(char_type c)
+{
+       return (!forced.empty() && forced.find(c) != forced.end());
+}
+
+
 Encoding const * Encodings::fromLyXName(string const & name) const
 {
        EncodingList::const_iterator const it = encodinglist.find(name);
@@ -551,9 +655,10 @@ void Encodings::read(FileName const & encfile, FileName const & symbolsfile)
                        flags = split(flags, flag, ',');
                        if (flag == "combining")
                                info.combining = true;
-                       else if (flag == "force")
+                       else if (flag == "force") {
                                info.force = true;
-                       else
+                               forced.insert(symbol);
+                       } else
                                lyxerr << "Ignoring unknown flag `" << flag
                                       << "' for symbol `0x"
                                       << hex << symbol << dec
@@ -626,6 +731,8 @@ void Encodings::read(FileName const & encfile, FileName const & symbolsfile)
                        lex.next();
                        string const latexname = lex.getString();
                        lex.next();
+                       string const guiname = lex.getString();
+                       lex.next();
                        string const iconvname = lex.getString();
                        lex.next();
                        string const width = lex.getString();
@@ -651,7 +758,7 @@ void Encodings::read(FileName const & encfile, FileName const & symbolsfile)
 
                        LYXERR(Debug::INFO, "Reading encoding " << name);
                        encodinglist[name] = Encoding(name, latexname,
-                               iconvname, fixedwidth, package);
+                               guiname, iconvname, fixedwidth, package);
 
                        if (lex.lex() != et_end)
                                lex.printError("Missing end");