]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/MathStream.cpp
Assure correct spacing of colored items in mathed
[lyx.git] / src / mathed / MathStream.cpp
index 803023876cb07a91da566251532706655dc6738e..f93ff72cf0519fe0d9fbd40708e5aed28188a26b 100644 (file)
 
 #include "MathStream.h"
 
+#include "MathFactory.h"
 #include "MathData.h"
 #include "MathExtern.h"
 
-#include "support/textutils.h"
+#include "TexRow.h"
+
 #include "support/docstring.h"
+#include "support/RefChanger.h"
+#include "support/textutils.h"
 
 #include <algorithm>
 #include <cstring>
@@ -107,7 +111,7 @@ WriteStream & operator<<(WriteStream & ws, docstring const & s)
        }
        ws.os() << s.substr(first);
        int lf = 0;
-       char_type lastchar(0);
+       char_type lastchar = 0;
        docstring::const_iterator dit = s.begin() + first;
        docstring::const_iterator end = s.end();
        for (; dit != end; ++dit) {
@@ -121,20 +125,13 @@ WriteStream & operator<<(WriteStream & ws, docstring const & s)
 }
 
 
-WriteStream::WriteStream(odocstream & os, bool fragile, bool latex, OutputType output,
-                       Encoding const * encoding)
+WriteStream::WriteStream(otexrowstream & os, bool fragile, bool latex,
+                                                OutputType output, Encoding const * encoding)
        : os_(os), fragile_(fragile), firstitem_(false), latex_(latex),
          output_(output), pendingspace_(false), pendingbrace_(false),
          textmode_(false), locked_(0), ascii_(0), canbreakline_(true),
-         line_(0), encoding_(encoding)
-{}
-
-
-WriteStream::WriteStream(odocstream & os)
-       : os_(os), fragile_(false), firstitem_(false), latex_(false),
-         output_(wsDefault), pendingspace_(false), pendingbrace_(false),
-         textmode_(false), locked_(0), ascii_(0), canbreakline_(true),
-         line_(0), encoding_(0)
+         mathsout_(false), ulemcmd_(NONE), line_(0), encoding_(encoding),
+         row_entry_(TexRow::row_none), mathclass_(false)
 {}
 
 
@@ -183,6 +180,20 @@ void WriteStream::asciiOnly(bool ascii)
 }
 
 
+Changer WriteStream::changeRowEntry(TexRow::RowEntry entry)
+{
+       return make_change(row_entry_, entry);
+}
+
+
+bool WriteStream::startOuterRow()
+{
+       if (TexRow::isNone(row_entry_))
+               return false;
+       return texrow().start(row_entry_);
+}
+
+
 WriteStream & operator<<(WriteStream & ws, MathAtom const & at)
 {
        at->write(ws);
@@ -258,8 +269,8 @@ WriteStream & operator<<(WriteStream & ws, unsigned int i)
 //////////////////////////////////////////////////////////////////////
 
 
-MathStream::MathStream(odocstream & os)
-       : os_(os), tab_(0), line_(0), in_text_(false)
+MathStream::MathStream(odocstream & os, std::string xmlns, bool xmlMode)
+       : os_(os), tab_(0), line_(0), in_text_(false), xmlns_(xmlns), xml_mode_(xmlMode)
 {}
 
 
@@ -284,7 +295,7 @@ void MathStream::defer(string const & s)
 
 
 docstring MathStream::deferred() const
-{ 
+{
        return deferred_.str();
 }
 
@@ -328,10 +339,10 @@ MathStream & operator<<(MathStream & ms, MTag const & t)
 {
        ++ms.tab();
        ms.cr();
-       ms.os() << '<' << from_ascii(t.tag_);
+       ms.os() << '<' << from_ascii(ms.namespacedTag(t.tag_));
        if (!t.attr_.empty())
                ms.os() << " " << from_ascii(t.attr_);
-       ms << '>';
+       ms << ">";
        return ms;
 }
 
@@ -341,7 +352,18 @@ MathStream & operator<<(MathStream & ms, ETag const & t)
        ms.cr();
        if (ms.tab() > 0)
                --ms.tab();
-       ms.os() << "</" << from_ascii(t.tag_) << '>';
+       ms.os() << "</" << from_ascii(ms.namespacedTag(t.tag_)) << ">";
+       return ms;
+}
+
+
+MathStream & operator<<(MathStream & ms, CTag const & t)
+{
+       ms.cr();
+       ms.os() << "<" << from_ascii(ms.namespacedTag(t.tag_));
+    if (!t.attr_.empty())
+        ms.os() << " " << from_utf8(t.attr_);
+    ms.os() << "/>";
        return ms;
 }
 
@@ -374,7 +396,7 @@ void HtmlStream::defer(string const & s)
 
 
 docstring HtmlStream::deferred() const
-{ 
+{
        return deferred_.str();
 }
 
@@ -441,56 +463,17 @@ HtmlStream & operator<<(HtmlStream & ms, docstring const & s)
 //////////////////////////////////////////////////////////////////////
 
 
-SetMode::SetMode(MathStream & os, bool text)
-       : os_(os), opened_(false)
+SetMode::SetMode(MathStream & ms, bool text)
+       : ms_(ms)
 {
-       init(text, "");
-}
-
-
-SetMode::SetMode(MathStream & os, bool text, string const & attrs)
-       : os_(os), opened_(false)
-{
-       init(text, attrs);
-}
-
-
-void SetMode::init(bool text, string const & attrs)
-{
-       was_text_ = os_.inText();
-       if (was_text_)
-               os_ << "</mtext>";
-       if (text) {
-               os_.setTextMode();
-               os_ << "<mtext";
-               if (!attrs.empty())
-                       os_ << " " << from_utf8(attrs);
-               os_ << ">";
-               opened_ = true;
-       } else {
-               if (!attrs.empty()) {
-                       os_ << "<mstyle " << from_utf8(attrs) << ">";
-                       opened_ = true;
-               }
-               os_.setMathMode();
-       }
+       was_text_ = ms_.inText();
+       ms_.setTextMode(text);
 }
 
 
 SetMode::~SetMode()
 {
-       if (opened_) {
-               if (os_.inText())
-                       os_ << "</mtext>";
-               else
-                       os_ << "</mstyle>";
-       }
-       if (was_text_) {
-               os_.setTextMode();
-               os_ << "<mtext>";
-       } else {
-               os_.setMathMode();
-       }
+       ms_.setTextMode(was_text_);
 }
 
 
@@ -498,42 +481,16 @@ SetMode::~SetMode()
 
 
 SetHTMLMode::SetHTMLMode(HtmlStream & os, bool text)
-       : os_(os), opened_(false)
-{
-       init(text, "");
-}
-
-
-SetHTMLMode::SetHTMLMode(HtmlStream & os, bool text, string attrs)
-       : os_(os), opened_(true)
-{
-       init(text, attrs);
-}
-
-
-void SetHTMLMode::init(bool text, string const & attrs)
+       : os_(os)
 {
        was_text_ = os_.inText();
-       if (text) {
-               os_.setTextMode();
-               if (attrs.empty())
-                       os_ << MTag("span");
-               else
-                       os_ << MTag("span", attrs);
-               opened_ = true;
-       } else
-               os_.setMathMode();
+       os_.setTextMode(text);
 }
 
 
 SetHTMLMode::~SetHTMLMode()
 {
-       if (opened_)
-               os_ << ETag("span");
-       if (was_text_)
-               os_.setTextMode();
-       else
-               os_.setMathMode();
+       os_.setTextMode(was_text_);
 }
 
 
@@ -752,4 +709,33 @@ OctaveStream & operator<<(OctaveStream & os, string const & s)
 }
 
 
+docstring convertDelimToXMLEscape(docstring const & name, bool xmlmode)
+{
+       // For the basic symbols, no difference between XML and HTML.
+       if (name.size() == 1) {
+               char_type const c = name[0];
+               if (c == '<')
+                       return from_ascii("&lt;");
+               else if (c == '>')
+                       return from_ascii("&gt;");
+               else
+                       return name;
+       } else if (name.size() == 2 && name[0] == '\\') {
+               char_type const c = name[1];
+               if (c == '{')
+                       return from_ascii("&#123;");
+               else if (c == '}')
+                       return from_ascii("&#125;");
+       }
+       MathWordList const & words = mathedWordList();
+       MathWordList::const_iterator it = words.find(name);
+       if (it != words.end()) {
+               // Only difference between XML and HTML, based on the contents read by MathFactory.
+               docstring const escape = xmlmode ? it->second.xmlname : it->second.htmlname;
+               return escape;
+       }
+       LYXERR0("Unable to find `" << name <<"' in the mathWordList.");
+       return name;
+}
+
 } // namespace lyx