}
+SetMode::SetMode(MathStream & os, bool text)
+ : os_(os), opened_(false)
+{
+ init(text, from_ascii(""));
+}
+
+
+SetMode::SetMode(MathStream & os, bool text, docstring attrs)
+ : os_(os), opened_(false)
+{
+ init(text, attrs);
+}
+
+
+void SetMode::init(bool text, docstring attrs)
+{
+ was_text_ = os_.inText();
+ if (was_text_)
+ os_ << "</mtext>";
+ if (text) {
+ os_.setTextMode();
+ os_ << "<mtext";
+ if (!attrs.empty())
+ os_ << " " << attrs;
+ os_ << ">";
+ opened_ = true;
+ } else {
+ if (!attrs.empty()) {
+ os_ << "<mstyle " << attrs << ">";
+ opened_ = true;
+ }
+ os_.setMathMode();
+ }
+}
+
+
+SetMode::~SetMode()
+{
+ if (opened_) {
+ if (os_.inText())
+ os_ << "</mtext>";
+ else
+ os_ << "</mstyle>";
+ }
+ if (was_text_) {
+ os_.setTextMode();
+ os_ << "<mtext>";
+ } else {
+ os_.setMathMode();
+ }
+}
+
+
//////////////////////////////////////////////////////////////////////