1 // -*- C++ -*-
2 /**
3  * \file Encoding.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Lars Gullik Bjønnes
8  * \author Jean-Marc Lasgouttes
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12 
13 #ifndef ENCODING_H
14 #define ENCODING_H
15 
16 #include "support/docstring.h"
17 #include "support/trivstring.h"
18 #include "support/types.h"
19 
20 #include <map>
21 #include <set>
22 #include <vector>
23 
24 namespace lyx {
25 
26 namespace support { class FileName; }
27 
28 class EncodingException : public std::exception {
29 public:
30 	EncodingException(char_type c);
~EncodingException()31 	virtual ~EncodingException() throw() {}
32 	virtual const char * what() const throw();
33 
34 	char_type failed_char;
35 	int par_id;
36 	pos_type pos;
37 };
38 
39 
40 enum CharInfoFlags {
41 	///
42 	CharInfoCombining = 1,
43 	///
44 	CharInfoTextFeature = 2,
45 	///
46 	CharInfoMathFeature = 4,
47 	///
48 	CharInfoForce = 8,
49 	///
50 	CharInfoTextNoTermination = 16,
51 	///
52 	CharInfoMathNoTermination = 32,
53 	///
54 	CharInfoForceSelected = 64,
55 	///
56 	CharInfoDeprecated = 128
57 };
58 
59 
60 /// Information about a single UCS4 character
61 class CharInfo {
62 public:
CharInfo()63 	CharInfo() : flags_(0) {}
64 	CharInfo(
65 		docstring const & textcommand, docstring const & mathcommand,
66 		std::string const & textpreamble, std::string const & mathpreamble,
67 		std::string const & tipashortcut, unsigned int flags);
68 	// we assume that at least one command is nonempty when using unicodesymbols
isUnicodeSymbol()69 	bool isUnicodeSymbol() const { return !textcommand_.empty() || !mathcommand_.empty(); }
70 	/// LaTeX command (text mode) for this character
textcommand()71 	docstring const textcommand() const { return textcommand_; }
72 	/// LaTeX command (math mode) for this character
mathcommand()73 	docstring mathcommand() const { return mathcommand_; }
74 	/// Needed LaTeX preamble (or feature) for text mode
textpreamble()75 	std::string textpreamble() const { return textpreamble_; }
76 	/// Needed LaTeX preamble (or feature) for math mode
mathpreamble()77 	std::string mathpreamble() const { return mathpreamble_; }
78 	/// Is this a combining character?
combining()79 	bool combining() const { return flags_ & CharInfoCombining ? true : false; }
80 	/// Is \c textpreamble a feature known by LaTeXFeatures, or a raw LaTeX
81 	/// command?
textfeature()82 	bool textfeature() const { return flags_ & CharInfoTextFeature ? true : false; }
83 	/// Is \c mathpreamble a feature known by LaTeXFeatures, or a raw LaTeX
84 	/// command?
mathfeature()85 	bool mathfeature() const { return flags_ & CharInfoMathFeature ? true : false; }
86 	/// Always force the LaTeX command, even if the encoding contains
87 	/// this character?
force()88 	bool force() const { return flags_ & CharInfoForce ? true : false; }
89 	/// Force the LaTeX command for some encodings?
forceselected()90 	bool forceselected() const { return flags_ & CharInfoForceSelected ? true : false; }
91 	/// Disable LaTeX command => char_type conversion for this deprecated symbol?
deprecated()92 	bool deprecated() const { return flags_ & CharInfoDeprecated ? true : false; }
93 	/// TIPA shortcut
tipashortcut()94 	std::string const tipashortcut() const { return tipashortcut_; }
95 	/// \c textcommand needs no termination (such as {} or space).
textnotermination()96 	bool textnotermination() const { return flags_ & CharInfoTextNoTermination ? true : false; }
97 	/// \c mathcommand needs no termination (such as {} or space).
mathnotermination()98 	bool mathnotermination() const { return flags_ & CharInfoMathNoTermination ? true : false; }
99 	///
100 private:
101 	/// LaTeX command (text mode) for this character
102 	trivdocstring textcommand_;
103 	/// LaTeX command (math mode) for this character
104 	trivdocstring mathcommand_;
105 	/// Needed LaTeX preamble (or feature) for text mode
106 	trivstring textpreamble_;
107 	/// Needed LaTeX preamble (or feature) for math mode
108 	trivstring mathpreamble_;
109 	/// TIPA shortcut
110 	trivstring tipashortcut_;
111 	/// feature flags
112 	unsigned int flags_;
113 };
114 
115 
116 /**
117  * An encoding as defined in lib/encodings.
118  * All const methods are thread-safe, so the caller does not need any locking.
119  * This property must be kept when changing the class.
120  */
121 class Encoding {
122 public:
123 	/// Which LaTeX package handles this encoding?
124 	enum Package {
125 		none = 1,
126 		inputenc = 2,
127 		CJK = 4,
128 		japanese = 8
129 	};
130 	/// Represent any of the above packages
131 	static int const any;
132 	///
Encoding()133 	Encoding() : fixedwidth_(true), unsafe_(false), forced_(0),
134 	             start_encodable_(0), package_(none), complete_(false) {}
135 	///
136 	Encoding(std::string const & n, std::string const & l,
137 		 std::string const & g, std::string const & i,
138 		 bool f, bool u, Package p);
139 	///
140 	void init() const;
141 	///
name()142 	std::string const name() const { return name_; }
143 	///
latexName()144 	std::string const latexName() const { return latexName_; }
145 	///
guiName()146 	std::string const guiName() const { return guiName_; }
147 	///
iconvName()148 	std::string const iconvName() const { return iconvName_; }
149 	///
hasFixedWidth()150 	bool hasFixedWidth() const { return fixedwidth_; }
151 	///
unsafe()152 	bool unsafe() const { return unsafe_; }
153 	/// \p c is representable in this encoding without a LaTeX macro
154 	bool encodable(char_type c) const;
155 	/**
156 	 * Convert \p c to something that LaTeX can understand.
157 	 * This is either the character itself (if it is representable
158 	 * in this encoding), or a LaTeX macro.
159 	 * If the character is not representable in this encoding, but no
160 	 * LaTeX macro is known, a warning is given of lyxerr, and the
161 	 * character is returned.
162 	 * \return the converted character and a flag indicating whether
163 	 * the command needs to be terminated by {} or a space.
164 	 */
165 	std::pair<docstring, bool> latexChar(char_type c) const;
166 	/**
167 	 * Convert \p input to something that LaTeX can understand.
168 	 * This is either the string itself (if it is representable
169 	 * in this encoding), or a LaTeX macro.
170 	 * If a character is not representable in this encoding, but no
171 	 * LaTeX macro is known, a warning is given of lyxerr, and the
172 	 * character is returned in the second string of the pair and
173 	 * omitted in the first.
174 	 * \p dryrun specifies whether the string is used within source
175 	 * preview (which yields a special warning).
176 	 */
177 	std::pair<docstring, docstring> latexString(docstring const & input,
178 						    bool dryrun = false) const;
179 	/// Which LaTeX package handles this encoding?
package()180 	Package package() const { return package_; }
181 	/// A list of all characters usable in this encoding
182 	std::vector<char_type> symbolsList() const;
183 private:
184 	/**
185 	 * Do we have to output this character as LaTeX command in any case?
186 	 * This is true if the "force" flag is set.
187 	 * We need this if the inputencoding does not support a certain glyph.
188 	 */
189 	bool isForced(char_type c) const;
190 	///
191 	trivstring name_;
192 	///
193 	trivstring latexName_;
194 	///
195 	trivstring guiName_;
196 	///
197 	trivstring iconvName_;
198 	/// Is this a fixed width encoding?
199 	bool fixedwidth_;
200 	/// Is this encoding TeX unsafe, e.g. control characters like {, }
201 	/// and \\ may appear in high bytes?
202 	bool unsafe_;
203 	///
204 	typedef std::set<char_type> CharSet;
205 	/// Set of UCS4 characters that we can encode (for singlebyte
206 	/// encodings only)
207 	CharSet encodable_;
208 	/// Set of UCS4 characters that we can't encode
209 	CharSet const * forced_;
210 	/// All code points below this are encodable. This helps us to avoid
211 	/// lokup of ASCII characters in encodable_ and gives about 1 sec
212 	/// speedup on export of the Userguide.
213 	char_type start_encodable_;
214 	/// Which LaTeX package handles this encoding?
215 	Package package_;
216 	/**
217 	 * If this is true the stored information about the encoding covers
218 	 * all encodable characters. We set this to false initially so that
219 	 * we only need to query iconv for the actually used encodings.
220 	 * This is needed especially for the multibyte encodings, if we
221 	 * complete all encoding info on startup it takes 2-3 minutes.
222 	 */
223 	bool complete_;
224 };
225 
226 class Encodings {
227 public:
228 	///
229 	typedef std::set<char_type> MathCommandSet;
230 	///
231 	typedef std::set<char_type> TextCommandSet;
232 	///
233 	typedef std::set<char_type> MathSymbolSet;
234 	///
235 	typedef std::map<trivstring, Encoding> EncodingList;
236 	/// iterator to iterate over all encodings.
237 	/// We hide the fact that our encoding list is implemented as a map.
238 	class const_iterator : public EncodingList::const_iterator {
239 		typedef EncodingList::const_iterator base;
240 	public:
const_iterator()241 		const_iterator() : base() {}
const_iterator(base const & b)242 		const_iterator(base const & b) : base(b) {}
243 		Encoding const & operator*() const { return base::operator*().second; }
244 		Encoding const * operator->() const { return &(base::operator*().second); }
245 	};
246 	///
247 	Encodings();
248 	/// Read the encodings.
249 	/// \param encfile encodings definition file
250 	/// \param symbolsfile unicode->LaTeX mapping file
251 	void read(support::FileName const & encfile,
252 		  support::FileName const & symbolsfile);
253 	/// Get encoding from LyX name \p name
254 	Encoding const *
255 	fromLyXName(std::string const & name, bool allowUnsafe = false) const;
256 	/// Get encoding from LaTeX name \p name and package \p package
257 	Encoding const * fromLaTeXName(std::string const & name,
258 		int const & package = Encoding::any, bool allowUnsafe = false) const;
259 	/// Get encoding from iconv name \p name and package \p package
260 	Encoding const * fromIconvName(std::string const & name,
261 		int const & package = Encoding::any, bool allowUnsafe = false) const;
262 
263 	///
begin()264 	const_iterator begin() const { return encodinglist.begin(); }
265 	///
end()266 	const_iterator end() const { return encodinglist.end(); }
267 
268 	/// Accessor for the unicode information table.
269 	static CharInfo const & unicodeCharInfo(char_type c);
270 	/// Is this a combining char?
271 	static bool isCombiningChar(char_type c);
272 	/// Return the TIPA shortcut
273 	static std::string const TIPAShortcut(char_type c);
274 	/**
275 	 * Is this a known char from some language?
276 	 * If \p preamble is empty and code point \p c is known to belong
277 	 * to a supported script, true is returned and \p preamble is set
278 	 * to the corresponding entry in the unicodesymbols file.
279 	 * If \p preamble is not empty, a check is made whether code point
280 	 * \p c is a known character matching the preamble entry.
281 	 */
282 	static bool isKnownScriptChar(char_type const c, std::string & preamble);
283 	/**
284 	 * Do we have to display in italics this character when in mathmode?
285 	 * This is true if the "mathalpha" flag is set. We use this for
286 	 * letters and accented characters that are output as math commands.
287 	 */
288 	static bool isMathAlpha(char_type c);
289 	/**
290 	 * Do we have to wrap in \text this character when in mathmode?
291 	 * This is true if \p c is not ascii and the "mathalpha" flag is not
292 	 * set and a mathcommand is not defined in the unicodesymbols file.
293 	 */
294 	static bool isUnicodeTextOnly(char_type c);
295 	/**
296 	 * Register \p c as a mathmode command.
297 	 */
addMathCmd(char_type c)298 	static void addMathCmd(char_type c) { mathcmd.insert(c); }
299 	/**
300 	 * Register \p c as a textmode command.
301 	 */
addTextCmd(char_type c)302 	static void addTextCmd(char_type c) { textcmd.insert(c); }
303 	/**
304 	 * Register \p c as a mathmode symbol.
305 	 */
addMathSym(char_type c)306 	static void addMathSym(char_type c) { mathsym.insert(c); }
307 	/**
308 	 * Tell whether \p c is registered as a mathmode command.
309 	 */
isMathCmd(char_type c)310 	static bool isMathCmd(char_type c) { return mathcmd.count(c); }
311 	/**
312 	 * Tell whether \p c is registered as a textmode command.
313 	 */
isTextCmd(char_type c)314 	static bool isTextCmd(char_type c) { return textcmd.count(c); }
315 	/**
316 	 * Tell whether \p c is registered as a mathmode symbol.
317 	 */
isMathSym(char_type c)318 	static bool isMathSym(char_type c) { return mathsym.count(c); }
319 	/**
320 	 * If \p c cannot be encoded in the given \p encoding, convert
321 	 * it to something that LaTeX can understand in mathmode.
322 	 * \p needsTermination indicates whether the command needs to be
323 	 * terminated by {} or a space.
324 	 * \return whether \p command is a mathmode command
325 	 */
326 	static bool latexMathChar(char_type c, bool mathmode,
327 			Encoding const * encoding, docstring & command,
328 			bool & needsTermination);
329 	/**
330 	 * Convert the LaTeX command in \p cmd to the corresponding unicode
331 	 * point and set \p combining to true if it is a combining symbol.
332 	 * \p needsTermination indicates whether the command needs to be
333 	 * terminated by {} or a space.
334 	 */
335 	static char_type fromLaTeXCommand(docstring const & cmd, int cmdtype,
336 			bool & combining, bool & needsTermination,
337 			std::set<std::string> * req = 0);
338 	///
339 	enum LatexCmd {
340 		///
341 		MATH_CMD = 1,
342 		///
343 		TEXT_CMD = 2
344 	};
345 	/**
346 	 * Convert the LaTeX commands in \p cmd and \return a docstring
347 	 * of corresponding unicode points. The conversion stops at the
348 	 * first command which could not be converted, and the remaining
349 	 * unconverted commands are returned in \p rem.
350 	 * The \p cmdtype parameter can be used to limit recognized
351 	 * commands to math or text mode commands only.
352 	 * \p needsTermination indicates whether the command needs to be
353 	 * terminated by {} or a space.
354 	 */
355 	static docstring fromLaTeXCommand(docstring const & cmd, int cmdtype,
356 			bool & needsTermination, docstring & rem,
357 			std::set<std::string> * req = 0);
358 
359 protected:
360 	///
361 	EncodingList encodinglist;
362 	///
363 	static MathCommandSet mathcmd;
364 	///
365 	static TextCommandSet textcmd;
366 	///
367 	static MathSymbolSet mathsym;
368 };
369 
370 extern Encodings encodings;
371 
372 
373 } // namespace lyx
374 
375 #endif
376