1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
|
/*
blahtex: a TeX to MathML converter designed with MediaWiki in mind
blahtexml: an extension of blahtex with XML processing in mind
http://gva.noekeon.org/blahtexml
Copyright (c) 2006, David Harvey
Copyright (c) 2009, Gilles Van Assche
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the names of the authors nor the names of their affiliation may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef BLAHTEX_MISC_H
#define BLAHTEX_MISC_H
#include <set>
#include <vector>
#include <string>
#include "Token.h"
// I use wishful_hash_set/map wherever I really want to use hash_set/map.
// Unfortunately hash_set/map is not quite standard enough yet, so for now
// it just gets mapped to set/map.
#define wishful_hash_map std::map
#define wishful_hash_set std::set
// The macro END_ARRAY is used in several places to simplify code that
// constructs an STL container from an array of data.
// (Yes, I hate macros too. Sorry.)
#define END_ARRAY(zzz_array) \
((zzz_array) + sizeof(zzz_array)/sizeof((zzz_array)[0]))
namespace blahtex
{
// Stores colours in 0x00rrggbb format.
// Better be 32 bits wide!
typedef unsigned RGBColour;
// The blahtex core throws an Exception object when it detects the input is
// invalid in some way. This doesn't include more serious things like debug
// assertions (these are thrown as std::logic_error).
//
// Each exception consists of an identifying string (mCode) plus zero or
// more arguments (mArgs). This scheme is designed to facilitate
// localisation of error messages. A complete list of corresponding
// messages in English is provided in Messages.cpp (not part of the
// blahtex core).
class Exception
{
private:
std::wstring mCode;
std::vector<std::wstring> mArgs;
public:
Exception()
{
}
Exception(
const std::wstring& code,
const std::wstring& arg1 = L"",
const std::wstring& arg2 = L"",
const std::wstring& arg3 = L""
) :
mCode(code)
{
if (!arg1.empty())
mArgs.push_back(arg1);
if (!arg2.empty())
mArgs.push_back(arg2);
if (!arg3.empty())
mArgs.push_back(arg3);
}
const std::wstring& GetCode() const
{
return mCode;
}
const std::vector<std::wstring>& GetArgs() const
{
return mArgs;
}
};
class TokenException : public Exception
{
private:
Token mToken;
public:
TokenException(const std::wstring & code, const Token & token)
: Exception(code, L"", L"", L""), mToken(token) {}
TokenException(const std::wstring & code, const std::wstring & arg1, const Token & token)
: Exception(code, arg1, L"", L""), mToken(token) {}
const Token & getToken() const {
return mToken;
}
};
// EncodingOptions describes output character encoding options.
struct EncodingOptions
{
// mMathmlEncoding tells what to do with non-ASCII MathML characters.
// It corresponds to the "--mathml-encoding" option on the command line.
enum MathmlEncoding
{
cMathmlEncodingRaw, // directly in unicode
cMathmlEncodingNumeric, // use e.g. "〈"
cMathmlEncodingShort, // use e.g. "⟨"
cMathmlEncodingLong // use e.g. "⟨"
}
mMathmlEncoding;
// mOtherEncodingRaw tells what to do with non-ASCII, non-MathML
// characters:
// * true means use unicode directly
// * false means use e.g. "ሴ"
bool mOtherEncodingRaw;
// mAllowPlane1 tells whether to allow unicode plane-1 characters.
// (This facility is included because some browsers don't have decent
// support for plane 1 characters.)
//
// If this flag is NOT set, then blahtex will never output things like
// "𝔄", even when mMathmlEncoding is set to cMathmlEncodingRaw
// or cMathmlEncodingNumeric. Instead it will fall back on something
// like "𝔄".
//
// (This flag is also present in struct MathmlOptions.)
bool mAllowPlane1;
EncodingOptions() :
mMathmlEncoding(cMathmlEncodingNumeric),
mOtherEncodingRaw(false),
mAllowPlane1(true)
{ }
};
// MathmlOptions stores options that affect the MathML output.
struct MathmlOptions
{
// mSpacingControl controls blahtex's MathML spacing markup output. It
// corresponds to the command line "--spacing" option.
//
// Blahtex always uses TeX's rules (or an approximation thereof) to
// determine spacing, but the SpacingControl values describe how much of
// the time it actually outputs markup (<mspace>, lspace, rspace) to
// implement its spacing decisions.
//
// cSpacingControlStrict:
// Blahtex outputs spacing commands everywhere possible, doesn't
// leave any choice to the MathML renderer.
//
// cSpacingControlModerate:
// Blahtex outputs spacing commands where it thinks a typical MathML
// renderer is likely to do something visually unsatisfactory
// without additional help. The aim is to get good agreement with
// TeX without overly bloated MathML markup. (It's very difficult
// to get this right, so I expect it to be under continual review.)
//
// cSpacingControlRelaxed:
// Blahtex only outputs spacing commands when the user specifically
// asks for them, using TeX commands like "\," or "\quad".
enum SpacingControl
{
cSpacingControlStrict,
cSpacingControlModerate,
cSpacingControlRelaxed
}
mSpacingControl;
// If mUseVersion1FontAttributes is set, blahtex will use MathML version
// 1 font attributes (fontstyle, fontweight, fontfamily) instead of
// mathvariant, and it will handle the fancier fonts (script,
// bold-script, fraktur, bold-fraktur, double-struck) by explicitly
// using appropriate MathML entities (e.g. "𝔄").
bool mUseVersion1FontAttributes;
// Discussed at struct EncodingOptions.
bool mAllowPlane1;
MathmlOptions() :
mSpacingControl(cSpacingControlStrict),
mUseVersion1FontAttributes(false),
mAllowPlane1(true)
{ }
};
// This class contains options to control how blahtex generates
// "purified Tex", that is, the .tex file which is sent to LaTeX to
// generate PNG output.
struct PurifiedTexOptions
{
// Generate display math instead of inline math
bool mDisplayMath;
// Blahtex may use "\usepackage[utf8x]{inputenc}" (which also requires
// the "ucs" package)
bool mAllowUcs;
// Blahtex may use "\usepackage{CJK}"
bool mAllowCJK;
// Blahtex may use the "preview" package.
bool mAllowPreview;
// The font name (e.g. "ipam") which gets passed to "\begin{CJK}..."
// for handling japanese, or blank if no font is available.
std::wstring mJapaneseFont;
// LaTeX preamble
std::wstring mLaTeXPreamble;
// LaTeX content inserted before math
std::wstring mLaTeXBeforeMath;
PurifiedTexOptions() :
mDisplayMath(false),
mAllowUcs(false),
mAllowCJK(false),
mAllowPreview(false)
{ }
};
}
#endif
// end of file @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
|