]> git.lyx.org Git - features.git/blobdiff - src/mathed/InsetMathDecoration.cpp
Introduce a return value for mathmlize(). We will need this to be able
[features.git] / src / mathed / InsetMathDecoration.cpp
index ecf5f58c2bf56ab2c97d0ec02e9042fc9dc723c0..825b212c9de8970c59640982ade338e02b241a50 100644 (file)
@@ -4,7 +4,7 @@
  * Licence details can be found in the file COPYING.
  *
  * \author Alejandro Aguilar Sierra
- * \author André Pönitz
+ * \author André Pönitz
  *
  * Full author contact details are available in file CREDITS.
  */
 #include "InsetMathDecoration.h"
 
 #include "MathData.h"
+#include "MathExtern.h"
 #include "MathParser.h"
 #include "MathSupport.h"
 #include "MathStream.h"
 #include "MetricsInfo.h"
 
 #include "LaTeXFeatures.h"
+
 #include "support/debug.h"
+#include "support/lassert.h"
 
 #include <ostream>
 
+using namespace std;
 
 namespace lyx {
 
 
-InsetMathDecoration::InsetMathDecoration(latexkeys const * key)
-       : InsetMathNest(1), key_(key)
+InsetMathDecoration::InsetMathDecoration(Buffer * buf, latexkeys const * key)
+       : InsetMathNest(buf, 1), key_(key)
 {
 //     lyxerr << " creating deco " << key->name << endl;
 }
@@ -93,16 +97,6 @@ bool InsetMathDecoration::wide() const
 }
 
 
-bool InsetMathDecoration::ams() const
-{
-       return
-                       key_->name == "overleftrightarrow" ||
-                       key_->name == "underleftarrow" ||
-                       key_->name == "underrightarrow" ||
-                       key_->name == "underleftrightarrow";
-}
-
-
 void InsetMathDecoration::metrics(MetricsInfo & mi, Dimension & dim) const
 {
        cell(0).metrics(mi, dim);
@@ -119,8 +113,6 @@ void InsetMathDecoration::metrics(MetricsInfo & mi, Dimension & dim) const
        }
 
        metricsMarkers(dim);
-       // Cache the inset dimension. 
-       setDimCache(mi, dim);
 }
 
 
@@ -140,6 +132,7 @@ void InsetMathDecoration::draw(PainterInfo & pi, int x, int y) const
 
 void InsetMathDecoration::write(WriteStream & os) const
 {
+       MathEnsurer ensurer(os);
        if (os.fragile() && protect())
                os << "\\protect";
        os << '\\' << key_->name << '{' << cell(0) << '}';
@@ -160,10 +153,72 @@ void InsetMathDecoration::infoize(odocstream & os) const
 
 void InsetMathDecoration::validate(LaTeXFeatures & features) const
 {
-       if (ams())
-               features.require("amsmath");
+       if (!key_->requires.empty())
+               features.require(to_utf8(key_->requires));
        InsetMathNest::validate(features);
 }
 
+namespace {
+       struct Attributes {
+               Attributes() {}
+               Attributes(bool o, string t)
+                       : over(o), tag(t) {}
+               bool over;
+               string tag;
+       };
+
+       typedef map<string, Attributes> Translator;
+
+       void buildTranslator(Translator & t) {
+               // the decorations we need to support are listed in lib/symbols
+               t["acute"] = Attributes(true, "&acute;");
+               t["bar"]   = Attributes(true, "&OverBar;");
+               t["breve"] = Attributes(true, "&breve;");
+               t["check"] = Attributes(true, "&caron;");
+               t["ddddot"] = Attributes(true, "&DotDot;");
+               t["dddot"] = Attributes(true, "&TripleDot;");
+               t["ddot"] = Attributes(true, "&Dot;");
+               t["dot"] = Attributes(true, "&dot;");
+               t["grave"] = Attributes(true, "&grave;");
+               t["hat"] = Attributes(true, "&circ;");
+               t["mathring"] = Attributes(true, "&ring;");
+               t["overbrace"] = Attributes(true, "&OverBrace;");
+               t["overleftarrow"] = Attributes(true, "&xlarr;");
+               t["overleftrightarrow"] = Attributes(true, "&xharr;");
+               t["overrightarrow"] = Attributes(true, "&xrarr;");
+               t["tilde"] = Attributes(true, "&tilde;");
+               t["underbar"] = Attributes(false, "&UnderBar;");
+               t["underbrace"] = Attributes(false, "&UnderBrace;");
+               t["underleftarrow"] = Attributes(false, "&xlarr;");
+               t["underleftrightarrow"] = Attributes(false, "&xharr;");
+               t["underline"] = Attributes(false, "&;");
+               t["underrightarrow"] = Attributes(false, "&xrarr;");
+               t["vec"] = Attributes(true, "&rarr;");
+               t["widehat"] = Attributes(true, "&Hat;");
+               t["widetilde"] = Attributes(true, "&Tilde;");
+       }
+
+       Translator const & translator() {
+               static Translator t;
+               if (t.empty())
+                       buildTranslator(t);
+               return t;
+       }
+}
+
+docstring InsetMathDecoration::mathmlize(MathStream & os) const
+{
+       Translator const & t = translator();
+       Translator::const_iterator cur = t.find(to_utf8(key_->name));
+       LASSERT(cur != t.end(), return docstring());
+       char const * const outag = cur->second.over ? "mover" : "munder";
+       os << MTag(outag) << MTag("mrow");
+       docstring const rv = lyx::mathmlize(cell(0), os);
+       os << ETag("mrow")
+                << from_ascii("<mo stretchy=\"true\">" + cur->second.tag + "</mo>")
+                << ETag(outag);
+       return rv;
+}
+
 
 } // namespace lyx