]> git.lyx.org Git - lyx.git/blob - src/mathed/math_charinset.C
prepare hanling of chars the nee a backslash when written...
[lyx.git] / src / mathed / math_charinset.C
1 #ifdef __GNUG__
2 #pragma implementation
3 #endif
4
5 #include <cctype>
6
7 #include "math_charinset.h"
8 #include "LColor.h"
9 #include "Painter.h"
10 #include "support/LOstream.h"
11 #include "mathed/support.h"
12 #include "math_parser.h"
13 #include "debug.h"
14
15
16 MathCharInset::MathCharInset(char c)
17         : char_(c), code_(nativeCode(c)), needbs_(false)
18 {
19 //lyxerr << "creating char '" << char_ << "' with code " << int(code_) << endl;
20 }
21
22
23 MathCharInset::MathCharInset(char c, MathTextCodes t)
24         : char_(c), code_((t == LM_TC_MIN) ? nativeCode(c) : t), needbs_(false)
25 {
26 //lyxerr << "creating char '" << char_ << "' with code " << int(code_) << endl;
27 }
28
29
30 MathCharInset::MathCharInset(char c, MathTextCodes t, bool needbs)
31         : char_(c), code_((t == LM_TC_MIN) ? nativeCode(c) : t), needbs_(needbs)
32 {
33 //lyxerr << "creating char '" << char_ << "' with code " << int(code_) << endl;
34 }
35
36
37 MathTextCodes MathCharInset::nativeCode(char c) const
38 {
39         if (isalpha(c))
40                 return LM_TC_VAR;
41         if (strchr("#$%{|}", c))
42                 return LM_TC_SPECIAL;
43         //if (strchr("0123456789;:!|[]().,?+/-*<>=", c)
44         return LM_TC_CONST;
45 }
46
47
48 MathInset * MathCharInset::clone() const
49 {   
50         return new MathCharInset(*this);
51 }
52
53
54 int MathCharInset::ascent() const
55 {
56         return mathed_char_ascent(code_, size(), char_);
57 }
58
59
60 int MathCharInset::descent() const
61 {
62         return mathed_char_descent(code_, size(), char_);
63 }
64
65
66 int MathCharInset::width() const
67 {
68         return mathed_char_width(code_, size(), char_);
69 }
70
71
72 void MathCharInset::metrics(MathStyles st) const
73 {
74         size_ = st;
75 }
76
77
78 void MathCharInset::draw(Painter & pain, int x, int y) const
79
80         xo(x);
81         yo(y);
82         //lyxerr << "drawing '" << char_ << "' code: " << code_ << endl;
83         drawChar(pain, code_, size_, x, y, char_);
84 }
85
86
87 void MathCharInset::writeHeader(std::ostream & os) const
88 {
89         if (code_ >= LM_TC_RM && code_ <= LM_TC_TEXTRM) 
90                 os << '\\' << math_font_name[code_ - LM_TC_RM] << '{';
91 }
92
93
94 void MathCharInset::writeTrailer(std::ostream & os) const
95 {
96         if (code_ >= LM_TC_RM && code_ <= LM_TC_TEXTRM)
97                 os << '}';
98 }
99
100
101 void MathCharInset::writeRaw(std::ostream & os) const
102 {
103         if (needbs_)
104                 os << "\\";
105         os << char_;
106 }
107
108
109 void MathCharInset::write(std::ostream & os, bool) const
110 {
111         writeHeader(os);
112         writeRaw(os);
113         writeTrailer(os);
114 }
115
116
117 void MathCharInset::writeNormal(std::ostream & os) const
118 {
119         if (needbs_)
120                 os << "\\";
121         os << char_;
122 }
123
124
125 bool MathCharInset::isRelOp() const
126 {
127         return char_ == '=' || char_ == '<' || char_ == '>';
128 }
129
130
131 void MathCharInset::handleFont(MathTextCodes t)
132 {
133         code_ = (code_ == t) ? LM_TC_VAR : t;
134 }