From b5dcdbc9606e04171bd089f90a34ad86edda60b8 Mon Sep 17 00:00:00 2001 From: Enrico Forestieri Date: Thu, 25 Sep 2008 18:38:25 +0000 Subject: [PATCH] When in mathmode, always use the mathcommand replacement from the unicodesymbols file. Also take into account that a unicode symbol may appear in both mathmode and textmode and thus both the mathmode and textmode features may be required. As a side effect, the problem that the \lyxmathsym definition is added to the preamble even when a unicode symbol appears only in textmode is solved. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@26548 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/Encoding.cpp | 56 ++++++++++++++++++++++------------ src/Encoding.h | 50 +++++++++++++++++++++++++++--- src/mathed/InsetMathString.cpp | 7 +++-- src/output_latex.cpp | 2 ++ 4 files changed, 87 insertions(+), 28 deletions(-) diff --git a/src/Encoding.cpp b/src/Encoding.cpp index 99d216f418..0620a149cc 100644 --- a/src/Encoding.cpp +++ b/src/Encoding.cpp @@ -34,6 +34,10 @@ namespace lyx { Encodings encodings; +Encodings::MathCommandSet Encodings::mathcmd; +Encodings::TextCommandSet Encodings::textcmd; +Encodings::MathSymbolSet Encodings::mathsym; + namespace { char_type arabic_table[172][4] = { @@ -377,25 +381,34 @@ vector Encoding::symbolsList() const } -bool Encodings::latexMathChar(char_type c, Encoding const * encoding, - docstring & command) +bool Encodings::latexMathChar(char_type c, bool mathmode, + Encoding const * encoding, docstring & command) { - if (encoding) { + if (encoding) command = encoding->latexChar(c, true); - if (!command.empty()) - return false; - } + CharInfoMap::const_iterator const it = unicodesymbols.find(c); - if (it == unicodesymbols.end()) - throw EncodingException(c); - if (it->second.mathcommand.empty()) { - if (it->second.textcommand.empty()) + if (it == unicodesymbols.end()) { + if (!encoding || command.empty()) throw EncodingException(c); - command = it->second.textcommand; + if (mathmode) + addMathSym(c); return false; } - command = it->second.mathcommand; - return true; + // at least one of mathcommand and textcommand is nonempty + bool use_math = (mathmode && !it->second.mathcommand.empty()) || + (!mathmode && it->second.textcommand.empty()); + if (use_math) { + command = it->second.mathcommand; + addMathCmd(c); + } else { + if (!encoding || command.empty()) { + command = it->second.textcommand; + addTextCmd(c); + } else if (mathmode) + addMathSym(c); + } + return use_math; } @@ -496,9 +509,11 @@ void Encodings::validate(char_type c, LaTeXFeatures & features, bool for_mathed) { CharInfoMap::const_iterator const it = unicodesymbols.find(c); if (it != unicodesymbols.end()) { - // at least one of mathcommand and textcommand is nonempty - bool const use_math = (for_mathed && !it->second.mathcommand.empty()) || + // In mathed, c could be used both in textmode and mathmode + bool const use_math = (for_mathed && isMathCmd(c)) || (!for_mathed && it->second.textcommand.empty()); + bool const use_text = (for_mathed && isTextCmd(c)) || + (!for_mathed && !it->second.textcommand.empty()); if (use_math) { if (!it->second.mathpreamble.empty()) { if (it->second.mathfeature) @@ -506,19 +521,20 @@ void Encodings::validate(char_type c, LaTeXFeatures & features, bool for_mathed) else features.addPreambleSnippet(it->second.mathpreamble); } - } else { + } + if (use_text) { if (!it->second.textpreamble.empty()) { if (it->second.textfeature) features.require(it->second.textpreamble); else features.addPreambleSnippet(it->second.textpreamble); } - if (for_mathed) { - features.require("relsize"); - features.require("lyxmathsym"); - } } } + if (for_mathed && isMathSym(c)) { + features.require("relsize"); + features.require("lyxmathsym"); + } } diff --git a/src/Encoding.h b/src/Encoding.h index dec09502cd..df44680527 100644 --- a/src/Encoding.h +++ b/src/Encoding.h @@ -113,6 +113,12 @@ private: class Encodings { public: + /// + typedef std::set MathCommandSet; + /// + typedef std::set TextCommandSet; + /// + typedef std::set MathSymbolSet; /// typedef std::map EncodingList; /// iterator to iterate over all encodings. @@ -181,12 +187,40 @@ public: */ static bool isForced(char_type c); /** - * If \p c cannot be encoded in the given \p encoding, convert - * it to something that LaTeX can understand in math mode. - * \return whether \p command is a math mode command + * Register \p c as a mathmode command. */ - static bool latexMathChar(char_type c, Encoding const * encoding, docstring & command); - + static void addMathCmd(char_type c) { mathcmd.insert(c); } + /** + * Register \p c as a textmode command. + */ + static void addTextCmd(char_type c) { textcmd.insert(c); } + /** + * Register \p c as a mathmode symbol. + */ + static void addMathSym(char_type c) { mathsym.insert(c); } + /** + * Tell whether \p c is registered as a mathmode command. + */ + static bool isMathCmd(char_type c) { return mathcmd.count(c); } + /** + * Tell whether \p c is registered as a textmode command. + */ + static bool isTextCmd(char_type c) { return textcmd.count(c); } + /** + * Tell whether \p c is registered as a mathmode symbol. + */ + static bool isMathSym(char_type c) { return mathsym.count(c); } + /** + * Initialize mathcmd, textcmd, and mathsym sets. + */ + static void initMathAndTextSets() { mathcmd.clear(); textcmd.clear(); mathsym.clear(); } + /** + * If \p c cannot be encoded in the given \p encoding, convert + * it to something that LaTeX can understand in mathmode. + * \return whether \p command is a mathmode command + */ + static bool latexMathChar(char_type c, bool mathmode, + Encoding const * encoding, docstring & command); /** * Convert the LaTeX command in \p cmd to the corresponding unicode * point and set \p combining to true if it is a combining symbol @@ -211,6 +245,12 @@ public: private: /// EncodingList encodinglist; + /// + static MathCommandSet mathcmd; + /// + static TextCommandSet textcmd; + /// + static MathSymbolSet mathsym; }; extern Encodings encodings; diff --git a/src/mathed/InsetMathString.cpp b/src/mathed/InsetMathString.cpp index 44b4507877..0dd17fe6d1 100644 --- a/src/mathed/InsetMathString.cpp +++ b/src/mathed/InsetMathString.cpp @@ -118,10 +118,11 @@ void InsetMathString::write(WriteStream & os) const os.pendingBrace(false); while (cit != end) { + bool mathmode = in_forced_mode ? os.textMode() : !os.textMode(); char_type const c = *cit; + docstring command(1, c); try { - docstring command(1, c); - if (c < 0x80 || Encodings::latexMathChar(c, os.encoding(), command)) { + if (c < 0x80 || Encodings::latexMathChar(c, mathmode, os.encoding(), command)) { if (os.textMode()) { if (in_forced_mode) { // we were inside \lyxmathsym @@ -129,7 +130,7 @@ void InsetMathString::write(WriteStream & os) const os.textMode(false); in_forced_mode = false; } - if (c >= 0x80) { + if (c >= 0x80 && os.textMode()) { os << "\\ensuremath{"; os.textMode(false); in_forced_mode = true; diff --git a/src/output_latex.cpp b/src/output_latex.cpp index 646e836480..0f387719b4 100644 --- a/src/output_latex.cpp +++ b/src/output_latex.cpp @@ -749,6 +749,8 @@ void latexParagraphs(Buffer const & buf, ParagraphList::const_iterator par = paragraphs.begin(); ParagraphList::const_iterator endpar = paragraphs.end(); + Encodings::initMathAndTextSets(); + LASSERT(runparams.par_begin <= runparams.par_end, /**/); // if only part of the paragraphs will be outputed if (runparams.par_begin != runparams.par_end) {