1 /*
2 blahtex: a TeX to MathML converter designed with MediaWiki in mind
3 blahtexml: an extension of blahtex with XML processing in mind
4 http://gva.noekeon.org/blahtexml
5 
6 Copyright (c) 2006, David Harvey
7 Copyright (c) 2009, Gilles Van Assche
8 All rights reserved.
9 
10 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
11 
12     * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
13     * 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.
14     * 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.
15 
16 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.
17 */
18 
19 #ifndef BLAHTEX_MATHMLNODE_H
20 #define BLAHTEX_MATHMLNODE_H
21 
22 #include <iostream>
23 #include <map>
24 #include <list>
25 #include <string>
26 #include "Misc.h"
27 
28 #ifdef BLAHTEXML_USING_XERCES
29 #include <xercesc/sax2/ContentHandler.hpp>
30 XERCES_CPP_NAMESPACE_USE
31 #endif
32 
33 namespace blahtex
34 {
35 
36 // MathmlFont lists all possible MathML "mathvariant" values. Blahtex
37 // uses these to record fonts in the layout tree; they get converted to
38 // MathML 1.x font attributes if needed.
39 enum MathmlFont
40 {
41     cMathmlFontNormal,
42     cMathmlFontBold,
43     cMathmlFontItalic,
44     cMathmlFontBoldItalic,
45     cMathmlFontDoubleStruck,
46     cMathmlFontBoldFraktur,
47     cMathmlFontScript,
48     cMathmlFontBoldScript,
49     cMathmlFontFraktur,
50     cMathmlFontSansSerif,
51     cMathmlFontBoldSansSerif,
52     cMathmlFontSansSerifItalic,
53     cMathmlFontSansSerifBoldItalic,
54     cMathmlFontMonospace
55 };
56 
57 // String versions of the MathML mathvariant fonts.
58 // (See enum MathmlFont in LayoutTree.h.)
59 extern std::wstring gMathmlFontStrings[];
60 
61 
62 // Represents a node in an MathML tree.
63 struct MathmlNode
64 {
65     enum Type
66     {
67         // Leaf nodes types ("token elements" in MathML documentation):
68         cTypeMi,
69         cTypeMo,
70         cTypeMn,
71         cTypeMspace,
72         cTypeMtext,
73 
74         // Internal nodes types:
75         cTypeMrow,
76         cTypeMstyle,
77         cTypeMsub,
78         cTypeMsup,
79         cTypeMsubsup,
80         cTypeMunder,
81         cTypeMover,
82         cTypeMunderover,
83         cTypeMfrac,
84         cTypeMsqrt,
85         cTypeMroot,
86         cTypeMtable,
87         cTypeMtr,
88         cTypeMtd,
89         cTypeMpadded
90     }
91     mType;
92 
93     enum Attribute
94     {
95         cAttributeDisplaystyle,
96         cAttributeScriptlevel,
97         cAttributeMathvariant,
98         cAttributeMathcolor,
99         cAttributeLspace,
100         cAttributeRspace,
101         cAttributeWidth,
102         cAttributeStretchy,
103         cAttributeMinsize,
104         cAttributeMaxsize,
105         cAttributeAccent,
106         cAttributeMovablelimits,
107         cAttributeLinethickness,
108         cAttributeColumnalign,
109         cAttributeColumnspacing,
110         cAttributeRowspacing,
111         cAttributeFontfamily,
112         cAttributeFontstyle,
113         cAttributeFontweight
114     };
115 
116     std::map<Attribute, std::wstring> mAttributes;
117 
118     // mText is only used for leaf nodes: it holds the text that is
119     // displayed between the opening and closing tags
120     std::wstring mText;
121 
122     // mChildren is only used for internal nodes
123     std::list<MathmlNode*> mChildren;
124 
125     MathmlNode(Type type, const std::wstring& text = L"") :
mTypeMathmlNode126         mType(type),
127         mText(text)
128     { }
129 
130     ~MathmlNode();
131 
132     // This function adds mathvariant (for MathML 2.0) or fontstyle/
133     // fontweight/fontfamily (for MathML 1.x) as appropriate to this node
134     // to obtain the desired font. It knows about MathML defaults (like the
135     // annoying automatic italic for single character <mi> nodes).
136     void AddFontAttributes(
137         MathmlFont desiredFont,
138         const MathmlOptions& options
139     );
140 
141 
142     // Print() recursively prints the tree rooted at this node to the
143     // given output stream.
144     //
145     // XML entities translated according to EncodingOptions.
146     //
147     // If "indent" is true, it will print each tag pair on a new line, and
148     // add appropriate indenting.
149     void Print(
150         std::wostream& os,
151         const EncodingOptions& options,
152         bool indent,
153         int depth = 0
154     ) const;
155 
156     // Used internally by Print:
157     void PrintType(std::wostream& os) const;
158     void PrintAttributes(std::wostream& os) const;
159 #ifdef BLAHTEXML_USING_XERCES
160     void PrintAsSAX2(ContentHandler& sax, const std::wstring& prefix, bool ignoreFirstmrow) const;
161 #endif
162 };
163 
164 }
165 
166 #endif
167 
168 // end of file @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
169