]> git.lyx.org Git - lyx.git/blobdiff - src/Encoding.cpp
Fix the rest of bug 5010.
[lyx.git] / src / Encoding.cpp
index e08095cf2d82831a06e16c28887f420cef47ef19..398a7d16b63230a6a6f1c32642d4c064199b07e3 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;
@@ -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;
+               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