Encodings: Make members of CharInfo private

This commit is contained in:
Vincent van Ravesteijn 2013-09-29 15:07:22 +02:00
parent 2e23774c6c
commit 88d0939166
3 changed files with 114 additions and 80 deletions

View File

@ -58,7 +58,7 @@ void BufferEncodings::validate(char_type c, LaTeXFeatures & features, bool for_m
CharInfo const & ci = Encodings::unicodeCharInfo(c); CharInfo const & ci = Encodings::unicodeCharInfo(c);
if (ci.isUnicodeSymbol()) { if (ci.isUnicodeSymbol()) {
// In mathed, c could be used both in textmode and mathmode // In mathed, c could be used both in textmode and mathmode
docstring const textcommand = ci.textcommand; docstring const textcommand = ci.textcommand();
bool const math_mode = for_mathed && isMathCmd(c); bool const math_mode = for_mathed && isMathCmd(c);
bool const use_math = math_mode || bool const use_math = math_mode ||
(!for_mathed && textcommand.empty()); (!for_mathed && textcommand.empty());
@ -71,7 +71,7 @@ void BufferEncodings::validate(char_type c, LaTeXFeatures & features, bool for_m
// and if we do not use unicode-math // and if we do not use unicode-math
if ((math_mode && !unicode_math) if ((math_mode && !unicode_math)
|| (use_math && !plain_utf8)) { || (use_math && !plain_utf8)) {
string const mathpreamble = ci.mathpreamble; string const mathpreamble = ci.mathpreamble();
if (!mathpreamble.empty()) { if (!mathpreamble.empty()) {
if (ci.mathfeature()) { if (ci.mathfeature()) {
string feats = mathpreamble; string feats = mathpreamble;
@ -86,7 +86,7 @@ void BufferEncodings::validate(char_type c, LaTeXFeatures & features, bool for_m
} }
// with utf8-plain, we do not load packages (see #7766) // with utf8-plain, we do not load packages (see #7766)
if (use_text && !plain_utf8) { if (use_text && !plain_utf8) {
string const textpreamble = ci.textpreamble; string const textpreamble = ci.textpreamble();
if (!textpreamble.empty()) { if (!textpreamble.empty()) {
if (ci.textfeature()) { if (ci.textfeature()) {
string feats = textpreamble; string feats = textpreamble;

View File

@ -255,6 +255,16 @@ const char * EncodingException::what() const throw()
} }
CharInfo::CharInfo(
docstring const textcommand, docstring const mathcommand,
std::string const textpreamble, std::string const mathpreamble,
std::string const tipashortcut, unsigned int flags)
: textcommand_(textcommand), mathcommand_(mathcommand),
textpreamble_(textpreamble), mathpreamble_(mathpreamble),
tipashortcut_(tipashortcut), flags_(flags)
{
}
Encoding::Encoding(string const & n, string const & l, string const & g, Encoding::Encoding(string const & n, string const & l, string const & g,
string const & i, bool f, bool u, Encoding::Package p) string const & i, bool f, bool u, Encoding::Package p)
: name_(n), latexName_(l), guiName_(g), iconvName_(i), fixedwidth_(f), : name_(n), latexName_(l), guiName_(g), iconvName_(i), fixedwidth_(f),
@ -362,10 +372,10 @@ pair<docstring, bool> Encoding::latexChar(char_type c) const
if (it == unicodesymbols.end()) if (it == unicodesymbols.end())
throw EncodingException(c); throw EncodingException(c);
// at least one of mathcommand and textcommand is nonempty // at least one of mathcommand and textcommand is nonempty
if (it->second.textcommand.empty()) if (it->second.textcommand().empty())
return make_pair( return make_pair(
"\\ensuremath{" + it->second.mathcommand + '}', false); "\\ensuremath{" + it->second.mathcommand() + '}', false);
return make_pair(it->second.textcommand, !it->second.textnotermination()); return make_pair(it->second.textcommand(), !it->second.textnotermination());
} }
@ -445,15 +455,15 @@ bool Encodings::latexMathChar(char_type c, bool mathmode,
return false; return false;
} }
// at least one of mathcommand and textcommand is nonempty // at least one of mathcommand and textcommand is nonempty
bool use_math = (mathmode && !it->second.mathcommand.empty()) || bool use_math = (mathmode && !it->second.mathcommand().empty()) ||
(!mathmode && it->second.textcommand.empty()); (!mathmode && it->second.textcommand().empty());
if (use_math) { if (use_math) {
command = it->second.mathcommand; command = it->second.mathcommand();
needsTermination = !it->second.mathnotermination(); needsTermination = !it->second.mathnotermination();
addMathCmd(c); addMathCmd(c);
} else { } else {
if (!encoding || command.empty()) { if (!encoding || command.empty()) {
command = it->second.textcommand; command = it->second.textcommand();
needsTermination = !it->second.textnotermination(); needsTermination = !it->second.textnotermination();
addTextCmd(c); addTextCmd(c);
} }
@ -470,22 +480,22 @@ char_type Encodings::fromLaTeXCommand(docstring const & cmd, int cmdtype,
CharInfoMap::const_iterator const end = unicodesymbols.end(); CharInfoMap::const_iterator const end = unicodesymbols.end();
CharInfoMap::const_iterator it = unicodesymbols.begin(); CharInfoMap::const_iterator it = unicodesymbols.begin();
for (combining = false; it != end; ++it) { for (combining = false; it != end; ++it) {
docstring const math = it->second.mathcommand; docstring const math = it->second.mathcommand();
docstring const text = it->second.textcommand; docstring const text = it->second.textcommand();
if ((cmdtype & MATH_CMD) && math == cmd) { if ((cmdtype & MATH_CMD) && math == cmd) {
combining = it->second.combining(); combining = it->second.combining();
needsTermination = !it->second.mathnotermination(); needsTermination = !it->second.mathnotermination();
if (req && it->second.mathfeature() && if (req && it->second.mathfeature() &&
!it->second.mathpreamble.empty()) !it->second.mathpreamble().empty())
req->insert(it->second.mathpreamble); req->insert(it->second.mathpreamble());
return it->first; return it->first;
} }
if ((cmdtype & TEXT_CMD) && text == cmd) { if ((cmdtype & TEXT_CMD) && text == cmd) {
combining = it->second.combining(); combining = it->second.combining();
needsTermination = !it->second.textnotermination(); needsTermination = !it->second.textnotermination();
if (req && it->second.textfeature() && if (req && it->second.textfeature() &&
!it->second.textpreamble.empty()) !it->second.textpreamble().empty())
req->insert(it->second.textpreamble); req->insert(it->second.textpreamble());
return it->first; return it->first;
} }
} }
@ -552,9 +562,9 @@ docstring Encodings::fromLaTeXCommand(docstring const & cmd, int cmdtype,
size_t unicmd_size = 0; size_t unicmd_size = 0;
char_type c = 0; char_type c = 0;
for (; it != uniend; ++it) { for (; it != uniend; ++it) {
docstring const math = mathmode ? it->second.mathcommand docstring const math = mathmode ? it->second.mathcommand()
: docstring(); : docstring();
docstring const text = textmode ? it->second.textcommand docstring const text = textmode ? it->second.textcommand()
: docstring(); : docstring();
if (!combcmd.empty() && it->second.combining() && if (!combcmd.empty() && it->second.combining() &&
(math == combcmd || text == combcmd)) (math == combcmd || text == combcmd))
@ -632,11 +642,11 @@ docstring Encodings::fromLaTeXCommand(docstring const & cmd, int cmdtype,
needsTermination = !it->second.textnotermination(); needsTermination = !it->second.textnotermination();
if (req) { if (req) {
if (math == tmp && it->second.mathfeature() && if (math == tmp && it->second.mathfeature() &&
!it->second.mathpreamble.empty()) !it->second.mathpreamble().empty())
req->insert(it->second.mathpreamble); req->insert(it->second.mathpreamble());
if (text == tmp && it->second.textfeature() && if (text == tmp && it->second.textfeature() &&
!it->second.textpreamble.empty()) !it->second.textpreamble().empty())
req->insert(it->second.textpreamble); req->insert(it->second.textpreamble());
} }
} }
} }
@ -752,7 +762,7 @@ string const Encodings::TIPAShortcut(char_type c)
{ {
CharInfoMap::const_iterator const it = unicodesymbols.find(c); CharInfoMap::const_iterator const it = unicodesymbols.find(c);
if (it != unicodesymbols.end()) if (it != unicodesymbols.end())
return it->second.tipashortcut; return it->second.tipashortcut();
return string(); return string();
} }
@ -764,14 +774,14 @@ bool Encodings::isKnownScriptChar(char_type const c, string & preamble)
if (it == unicodesymbols.end()) if (it == unicodesymbols.end())
return false; return false;
if (it->second.textpreamble != "textgreek" && it->second.textpreamble != "textcyr") if (it->second.textpreamble() != "textgreek" && it->second.textpreamble() != "textcyr")
return false; return false;
if (preamble.empty()) { if (preamble.empty()) {
preamble = it->second.textpreamble; preamble = it->second.textpreamble();
return true; return true;
} }
return it->second.textpreamble == preamble; return it->second.textpreamble() == preamble;
} }
@ -840,8 +850,6 @@ void Encodings::read(FileName const & encfile, FileName const & symbolsfile)
bool getNextToken = true; bool getNextToken = true;
while (symbolslex.isOK()) { while (symbolslex.isOK()) {
char_type symbol; char_type symbol;
CharInfo info;
string flags;
if (getNextToken) { if (getNextToken) {
if (!symbolslex.next(true)) if (!symbolslex.next(true))
@ -859,53 +867,53 @@ void Encodings::read(FileName const & encfile, FileName const & symbolsfile)
if (!symbolslex.next(true)) if (!symbolslex.next(true))
break; break;
info.textcommand = symbolslex.getDocString(); docstring textcommand = symbolslex.getDocString();
if (!symbolslex.next(true)) if (!symbolslex.next(true))
break; break;
info.textpreamble = symbolslex.getString(); string textpreamble = symbolslex.getString();
if (!symbolslex.next(true)) if (!symbolslex.next(true))
break; break;
flags = symbolslex.getString(); string sflags = symbolslex.getString();
string tipashortcut;
int flags = 0;
info.flags = 0; if (suffixIs(textcommand, '}'))
if (suffixIs(info.textcommand, '}')) flags |= CharInfoTextNoTermination;
info.flags |= CharInfoTextNoTermination; while (!sflags.empty()) {
if (suffixIs(info.mathcommand, '}'))
info.flags |= CharInfoMathNoTermination;
while (!flags.empty()) {
string flag; string flag;
flags = split(flags, flag, ','); sflags = split(sflags, flag, ',');
if (flag == "combining") { if (flag == "combining") {
info.flags |= CharInfoCombining; flags |= CharInfoCombining;
} else if (flag == "force") { } else if (flag == "force") {
info.flags |= CharInfoForce; flags |= CharInfoForce;
forced.insert(symbol); forced.insert(symbol);
} else if (prefixIs(flag, "force=")) { } else if (prefixIs(flag, "force=")) {
vector<string> encodings = vector<string> encodings =
getVectorFromString(flag.substr(6), ";"); getVectorFromString(flag.substr(6), ";");
for (size_t i = 0; i < encodings.size(); ++i) for (size_t i = 0; i < encodings.size(); ++i)
forcedselected[encodings[i]].insert(symbol); forcedselected[encodings[i]].insert(symbol);
info.flags |= CharInfoForceSelected; flags |= CharInfoForceSelected;
} else if (prefixIs(flag, "force!=")) { } else if (prefixIs(flag, "force!=")) {
vector<string> encodings = vector<string> encodings =
getVectorFromString(flag.substr(7), ";"); getVectorFromString(flag.substr(7), ";");
for (size_t i = 0; i < encodings.size(); ++i) for (size_t i = 0; i < encodings.size(); ++i)
forcednotselected[encodings[i]].insert(symbol); forcednotselected[encodings[i]].insert(symbol);
info.flags |= CharInfoForceSelected; flags |= CharInfoForceSelected;
} else if (flag == "mathalpha") { } else if (flag == "mathalpha") {
mathalpha.insert(symbol); mathalpha.insert(symbol);
} else if (flag == "notermination=text") { } else if (flag == "notermination=text") {
info.flags |= CharInfoTextNoTermination; flags |= CharInfoTextNoTermination;
} else if (flag == "notermination=math") { } else if (flag == "notermination=math") {
info.flags |= CharInfoMathNoTermination; flags |= CharInfoMathNoTermination;
} else if (flag == "notermination=both") { } else if (flag == "notermination=both") {
info.flags |= CharInfoTextNoTermination; flags |= CharInfoTextNoTermination;
info.flags |= CharInfoMathNoTermination; flags |= CharInfoMathNoTermination;
} else if (flag == "notermination=none") { } else if (flag == "notermination=none") {
info.flags &= ~CharInfoTextNoTermination; flags &= ~CharInfoTextNoTermination;
info.flags &= ~CharInfoMathNoTermination; flags &= ~CharInfoMathNoTermination;
} else if (contains(flag, "tipashortcut=")) { } else if (contains(flag, "tipashortcut=")) {
info.tipashortcut = split(flag, '='); tipashortcut = split(flag, '=');
} else { } else {
lyxerr << "Ignoring unknown flag `" << flag lyxerr << "Ignoring unknown flag `" << flag
<< "' for symbol `0x" << "' for symbol `0x"
@ -917,19 +925,23 @@ void Encodings::read(FileName const & encfile, FileName const & symbolsfile)
// make them optional so that old files still work. // make them optional so that old files still work.
int const lineno = symbolslex.lineNumber(); int const lineno = symbolslex.lineNumber();
bool breakout = false; bool breakout = false;
docstring mathcommand;
string mathpreamble;
if (symbolslex.next(true)) { if (symbolslex.next(true)) {
if (symbolslex.lineNumber() != lineno) { if (symbolslex.lineNumber() != lineno) {
// line in old format without mathcommand and mathpreamble // line in old format without mathcommand and mathpreamble
getNextToken = false; getNextToken = false;
} else { } else {
info.mathcommand = symbolslex.getDocString(); mathcommand = symbolslex.getDocString();
if (suffixIs(mathcommand, '}'))
flags |= CharInfoMathNoTermination;
if (symbolslex.next(true)) { if (symbolslex.next(true)) {
if (symbolslex.lineNumber() != lineno) { if (symbolslex.lineNumber() != lineno) {
// line in new format with mathcommand only // line in new format with mathcommand only
getNextToken = false; getNextToken = false;
} else { } else {
// line in new format with mathcommand and mathpreamble // line in new format with mathcommand and mathpreamble
info.mathpreamble = symbolslex.getString(); mathpreamble = symbolslex.getString();
} }
} else } else
breakout = true; breakout = true;
@ -939,27 +951,32 @@ void Encodings::read(FileName const & encfile, FileName const & symbolsfile)
} }
// backward compatibility // backward compatibility
if (info.mathpreamble == "esintoramsmath") if (mathpreamble == "esintoramsmath")
info.mathpreamble = "esint|amsmath"; mathpreamble = "esint|amsmath";
if (!info.textpreamble.empty()) if (!textpreamble.empty())
if (info.textpreamble[0] != '\\') if (textpreamble[0] != '\\')
info.flags |= CharInfoTextFeature; flags |= CharInfoTextFeature;
if (!info.mathpreamble.empty()) if (!mathpreamble.empty())
if (info.mathpreamble[0] != '\\') if (mathpreamble[0] != '\\')
info.flags |= CharInfoMathFeature; flags |= CharInfoMathFeature;
CharInfo info = CharInfo(
textcommand, mathcommand,
textpreamble, mathpreamble,
tipashortcut, flags);
LYXERR(Debug::INFO, "Read unicode symbol " << symbol << " '" LYXERR(Debug::INFO, "Read unicode symbol " << symbol << " '"
<< to_utf8(info.textcommand) << "' '" << info.textpreamble << to_utf8(info.textcommand()) << "' '" << info.textpreamble()
<< " '" << info.textfeature() << ' ' << info.textnotermination() << " '" << info.textfeature() << ' ' << info.textnotermination()
<< ' ' << to_utf8(info.mathcommand) << "' '" << info.mathpreamble << ' ' << to_utf8(info.mathcommand()) << "' '" << info.mathpreamble()
<< "' " << info.mathfeature() << ' ' << info.mathnotermination() << "' " << info.mathfeature() << ' ' << info.mathnotermination()
<< ' ' << info.combining() << ' ' << info.force() << ' ' << info.combining() << ' ' << info.force()
<< ' ' << info.forceselected()); << ' ' << info.forceselected());
// we assume that at least one command is nonempty when using unicodesymbols // we assume that at least one command is nonempty when using unicodesymbols
if (!info.textcommand.empty() || !info.mathcommand.empty()) if (info.isUnicodeSymbol()) {
unicodesymbols[symbol] = info; unicodesymbols[symbol] = info;
}
if (breakout) if (breakout)
break; break;

View File

@ -57,37 +57,54 @@ enum CharInfoFlags {
/// Information about a single UCS4 character /// Information about a single UCS4 character
class CharInfo { class CharInfo {
public: public:
CharInfo() {}
CharInfo(
docstring const textcommand, docstring const mathcommand,
std::string const textpreamble, std::string const mathpreamble,
std::string const tipashortcut, unsigned int flags);
// we assume that at least one command is nonempty when using unicodesymbols // we assume that at least one command is nonempty when using unicodesymbols
bool isUnicodeSymbol() const { return !textcommand.empty() || !mathcommand.empty(); } bool isUnicodeSymbol() const { return !textcommand_.empty() || !mathcommand_.empty(); }
/// LaTeX command (text mode) for this character /// LaTeX command (text mode) for this character
docstring textcommand; docstring const textcommand() const { return textcommand_; }
/// LaTeX command (math mode) for this character /// LaTeX command (math mode) for this character
docstring mathcommand; docstring mathcommand() const { return mathcommand_; }
/// Needed LaTeX preamble (or feature) for text mode /// Needed LaTeX preamble (or feature) for text mode
std::string textpreamble; std::string textpreamble() const { return textpreamble_; }
/// Needed LaTeX preamble (or feature) for math mode /// Needed LaTeX preamble (or feature) for math mode
std::string mathpreamble; std::string mathpreamble() const { return mathpreamble_; }
/// Is this a combining character? /// Is this a combining character?
bool combining() const { return flags & CharInfoCombining ? true : false; } bool combining() const { return flags_ & CharInfoCombining ? true : false; }
/// Is \c textpreamble a feature known by LaTeXFeatures, or a raw LaTeX /// Is \c textpreamble a feature known by LaTeXFeatures, or a raw LaTeX
/// command? /// command?
bool textfeature() const { return flags & CharInfoTextFeature ? true : false; } bool textfeature() const { return flags_ & CharInfoTextFeature ? true : false; }
/// Is \c mathpreamble a feature known by LaTeXFeatures, or a raw LaTeX /// Is \c mathpreamble a feature known by LaTeXFeatures, or a raw LaTeX
/// command? /// command?
bool mathfeature() const { return flags & CharInfoMathFeature ? true : false; } bool mathfeature() const { return flags_ & CharInfoMathFeature ? true : false; }
/// Always force the LaTeX command, even if the encoding contains /// Always force the LaTeX command, even if the encoding contains
/// this character? /// this character?
bool force() const { return flags & CharInfoForce ? true : false; } bool force() const { return flags_ & CharInfoForce ? true : false; }
/// Force the LaTeX command for some encodings? /// Force the LaTeX command for some encodings?
bool forceselected() const { return flags & CharInfoForceSelected ? true : false; } bool forceselected() const { return flags_ & CharInfoForceSelected ? true : false; }
/// TIPA shortcut /// TIPA shortcut
std::string tipashortcut; std::string const tipashortcut() const { return tipashortcut_; }
/// \c textcommand needs no termination (such as {} or space). /// \c textcommand needs no termination (such as {} or space).
bool textnotermination() const { return flags & CharInfoTextNoTermination ? true : false; } bool textnotermination() const { return flags_ & CharInfoTextNoTermination ? true : false; }
/// \c mathcommand needs no termination (such as {} or space). /// \c mathcommand needs no termination (such as {} or space).
bool mathnotermination() const { return flags & CharInfoMathNoTermination ? true : false; } bool mathnotermination() const { return flags_ & CharInfoMathNoTermination ? true : false; }
/// ///
unsigned int flags; private:
/// LaTeX command (text mode) for this character
docstring textcommand_;
/// LaTeX command (math mode) for this character
docstring mathcommand_;
/// Needed LaTeX preamble (or feature) for text mode
std::string textpreamble_;
/// Needed LaTeX preamble (or feature) for math mode
std::string mathpreamble_;
/// TIPA shortcut
std::string tipashortcut_;
/// feature flags
unsigned int flags_;
}; };