Encodings: Split off the functions that need Buffer

This is needed to be able to use Encodings in tex2lyx without the need to
compile Buffer as well, or to use a TEX2LYX define.
This commit is contained in:
Stephan Witt 2013-08-23 07:46:35 +02:00 committed by Vincent van Ravesteijn
parent 01e94f5e03
commit 2e23774c6c
5 changed files with 168 additions and 113 deletions

109
src/BufferEncodings.cpp Normal file
View File

@ -0,0 +1,109 @@
/**
* \file BufferEncodings.cpp
* This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING.
*
* \author Lars Gullik Bjønnes
* \author Jean-Marc Lasgouttes
* \author Dekel Tsur
* \author Stephan Witt
*
* Full author contact details are available in file CREDITS.
*/
#include <config.h>
#include "BufferEncodings.h"
#include "Buffer.h"
#include "InsetIterator.h"
#include "LaTeXFeatures.h"
#include "support/lstrings.h"
using namespace std;
using namespace lyx::support;
namespace lyx {
void BufferEncodings::initUnicodeMath(Buffer const & buffer, bool for_master)
{
if (for_master) {
mathcmd.clear();
textcmd.clear();
mathsym.clear();
}
// Check this buffer
Inset & inset = buffer.inset();
InsetIterator it = inset_iterator_begin(inset);
InsetIterator const end = inset_iterator_end(inset);
for (; it != end; ++it)
it->initUnicodeMath();
if (!for_master)
return;
// Check children
ListOfBuffers blist = buffer.getDescendents();
ListOfBuffers::const_iterator bit = blist.begin();
ListOfBuffers::const_iterator const bend = blist.end();
for (; bit != bend; ++bit)
initUnicodeMath(**bit, false);
}
void BufferEncodings::validate(char_type c, LaTeXFeatures & features, bool for_mathed)
{
CharInfo const & ci = Encodings::unicodeCharInfo(c);
if (ci.isUnicodeSymbol()) {
// In mathed, c could be used both in textmode and mathmode
docstring const textcommand = ci.textcommand;
bool const math_mode = for_mathed && isMathCmd(c);
bool const use_math = math_mode ||
(!for_mathed && textcommand.empty());
bool const use_text = (for_mathed && isTextCmd(c)) ||
(!for_mathed && !textcommand.empty());
bool const plain_utf8 = (features.runparams().encoding->name() == "utf8-plain");
bool const unicode_math = (features.isRequired("unicode-math")
&& features.isAvailable("unicode-math"));
// with utf8-plain, we only load packages when in mathed (see #7766)
// and if we do not use unicode-math
if ((math_mode && !unicode_math)
|| (use_math && !plain_utf8)) {
string const mathpreamble = ci.mathpreamble;
if (!mathpreamble.empty()) {
if (ci.mathfeature()) {
string feats = mathpreamble;
while (!feats.empty()) {
string feat;
feats = split(feats, feat, ',');
features.require(feat);
}
} else
features.addPreambleSnippet(mathpreamble);
}
}
// with utf8-plain, we do not load packages (see #7766)
if (use_text && !plain_utf8) {
string const textpreamble = ci.textpreamble;
if (!textpreamble.empty()) {
if (ci.textfeature()) {
string feats = textpreamble;
while (!feats.empty()) {
string feat;
feats = split(feats, feat, ',');
features.require(feat);
}
} else
features.addPreambleSnippet(textpreamble);
}
}
}
if (for_mathed && isMathSym(c)) {
features.require("amstext");
features.require("lyxmathsym");
}
}
} // namespace lyx

44
src/BufferEncodings.h Normal file
View File

@ -0,0 +1,44 @@
// -*- C++ -*-
/**
* \file BufferEncodings.h
* This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING.
*
* \author Lars Gullik Bjønnes
* \author Jean-Marc Lasgouttes
* \author Stephan Witt
*
* Full author contact details are available in file CREDITS.
*/
#ifndef BUFFER_ENCODINGS_H
#define BUFFER_ENCODINGS_H
#include "Encoding.h"
#include "support/docstring.h"
#include "support/types.h"
namespace lyx {
class Buffer;
class LaTeXFeatures;
class BufferEncodings : public Encodings {
public:
/**
* Initialize mathcmd, textcmd, and mathsym sets.
*/
static void initUnicodeMath(Buffer const & buffer, bool for_master = true);
/**
* If \p c cannot be encoded in the given \p encoding, convert
* it to something that LaTeX can understand in mathmode.
* \p needsTermination indicates whether the command needs to be
* terminated by {} or a space.
* \return whether \p command is a mathmode command
*/
static void validate(char_type c, LaTeXFeatures & features, bool for_mathed = false);
};
} // namespace lyx
#endif // BUFFER_ENCODINGS_H

View File

@ -14,16 +14,10 @@
#include "Encoding.h"
#include "Buffer.h"
#include "BufferList.h"
#include "InsetIterator.h"
#include "LaTeXFeatures.h"
#include "Lexer.h"
#include "LyXRC.h"
#include "support/debug.h"
#include "support/gettext.h"
#include "support/FileName.h"
#include "support/lstrings.h"
#include "support/textutils.h"
#include "support/unicode.h"
@ -700,97 +694,6 @@ docstring Encodings::fromLaTeXCommand(docstring const & cmd, int cmdtype,
}
void Encodings::initUnicodeMath(Buffer const & buffer, bool for_master)
{
#ifdef TEX2LYX
// The code below is not needed in tex2lyx and requires additional stuff
(void)buffer;
(void)for_master;
#else
if (for_master) {
mathcmd.clear();
textcmd.clear();
mathsym.clear();
}
// Check this buffer
Inset & inset = buffer.inset();
InsetIterator it = inset_iterator_begin(inset);
InsetIterator const end = inset_iterator_end(inset);
for (; it != end; ++it)
it->initUnicodeMath();
if (!for_master)
return;
// Check children
ListOfBuffers blist = buffer.getDescendents();
ListOfBuffers::const_iterator bit = blist.begin();
ListOfBuffers::const_iterator const bend = blist.end();
for (; bit != bend; ++bit)
initUnicodeMath(**bit, false);
#endif
}
void Encodings::validate(char_type c, LaTeXFeatures & features, bool for_mathed)
{
#ifdef TEX2LYX
// The code below is not needed in tex2lyx and requires additional stuff
(void)c;
(void)features;
(void)for_mathed;
#else
CharInfoMap::const_iterator const it = unicodesymbols.find(c);
if (it != unicodesymbols.end()) {
// In mathed, c could be used both in textmode and mathmode
bool const math_mode = for_mathed && isMathCmd(c);
bool const use_math = math_mode ||
(!for_mathed && it->second.textcommand.empty());
bool const use_text = (for_mathed && isTextCmd(c)) ||
(!for_mathed && !it->second.textcommand.empty());
bool const plain_utf8 = (features.runparams().encoding->name() == "utf8-plain");
bool const unicode_math = (features.isRequired("unicode-math")
&& features.isAvailable("unicode-math"));
// with utf8-plain, we only load packages when in mathed (see #7766)
// and if we do not use unicode-math
if ((math_mode && !unicode_math)
|| (use_math && !plain_utf8)) {
if (!it->second.mathpreamble.empty()) {
if (it->second.mathfeature()) {
string feats = it->second.mathpreamble;
while (!feats.empty()) {
string feat;
feats = split(feats, feat, ',');
features.require(feat);
}
} else
features.addPreambleSnippet(it->second.mathpreamble);
}
}
// with utf8-plain, we do not load packages (see #7766)
if (use_text && !plain_utf8) {
if (!it->second.textpreamble.empty()) {
if (it->second.textfeature()) {
string feats = it->second.textpreamble;
while (!feats.empty()) {
string feat;
feats = split(feats, feat, ',');
features.require(feat);
}
} else
features.addPreambleSnippet(it->second.textpreamble);
}
}
}
if (for_mathed && isMathSym(c)) {
features.require("amstext");
features.require("lyxmathsym");
}
#endif
}
bool Encodings::isHebrewComposeChar(char_type c)
{
return c <= 0x05c2 && c >= 0x05b0 && c != 0x05be && c != 0x05c0;
@ -822,6 +725,14 @@ bool Encodings::isArabicChar(char_type c)
}
CharInfo const & Encodings::unicodeCharInfo(char_type c)
{
static CharInfo empty;
CharInfoMap::const_iterator const it = unicodesymbols.find(c);
return it != unicodesymbols.end() ? it->second : empty;
}
char_type Encodings::transformChar(char_type c, Encodings::LetterForm form)
{
return isArabicChar(c) ? arabic_table[c-arabic_start][form] : c;

View File

@ -24,9 +24,6 @@ namespace lyx {
namespace support { class FileName; }
class Buffer;
class LaTeXFeatures;
class EncodingException : public std::exception {
public:
EncodingException(char_type c);
@ -60,6 +57,8 @@ enum CharInfoFlags {
/// Information about a single UCS4 character
class CharInfo {
public:
// we assume that at least one command is nonempty when using unicodesymbols
bool isUnicodeSymbol() const { return !textcommand.empty() || !mathcommand.empty(); }
/// LaTeX command (text mode) for this character
docstring textcommand;
/// LaTeX command (math mode) for this character
@ -258,6 +257,8 @@ public:
static bool isArabicSpecialChar(char_type c);
///
static bool isArabicChar(char_type c);
/// Accessor for the unicode information table.
static CharInfo const & unicodeCharInfo(char_type c);
///
static char_type transformChar(char_type c, LetterForm form);
/// Is this a combining char?
@ -303,10 +304,6 @@ public:
* 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 initUnicodeMath(Buffer const & buffer, bool for_master = true);
/**
* If \p c cannot be encoded in the given \p encoding, convert
* it to something that LaTeX can understand in mathmode.
@ -346,16 +343,8 @@ public:
static docstring fromLaTeXCommand(docstring const & cmd, int cmdtype,
bool & needsTermination, docstring & rem,
std::set<std::string> * req = 0);
/**
* Add the preamble snippet needed for the output of \p c to
* \p features.
* This does not depend on the used encoding, since the inputenc
* package only maps the code point \p c to a command, it does not
* make this command available.
*/
static void validate(char_type c, LaTeXFeatures & features, bool for_mathed = false);
private:
protected:
///
EncodingList encodinglist;
///

View File

@ -119,6 +119,7 @@ SOURCEFILESCORE = \
DepTable.cpp \
DocIterator.cpp \
Encoding.cpp \
BufferEncodings.cpp \
ErrorList.cpp \
Exporter.cpp \
factory.cpp \
@ -194,6 +195,7 @@ HEADERFILESCORE = \
BranchList.h \
buffer_funcs.h \
Buffer.h \
BufferEncodings.h \
BufferList.h \
BufferParams.h \
BufferView.h \