]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/InsetMathHull.cpp
Move <algorithm> from DocIterator.h
[lyx.git] / src / mathed / InsetMathHull.cpp
index d9f314b3c7a15acc2a212e29bb45cc0e261ab9dc..ebf8843d7939a07191a902dcb5523c7df4e23b26 100644 (file)
@@ -38,7 +38,7 @@
 #include "InsetMathMacro.h"
 #include "InsetMathMacroTemplate.h"
 #include "MetricsInfo.h"
-#include "output_xhtml.h"
+#include "xml.h"
 #include "Paragraph.h"
 #include "ParIterator.h"
 #include "xml.h"
@@ -56,6 +56,7 @@
 #include "graphics/PreviewLoader.h"
 
 #include "frontends/alert.h"
+#include "frontends/FontMetrics.h"
 #include "frontends/Painter.h"
 
 #include "support/convert.h"
@@ -227,8 +228,8 @@ InsetMathHull::InsetMathHull(InsetMathHull const & other) : InsetMathGrid(other)
 
 InsetMathHull::~InsetMathHull()
 {
-       for (size_t i = 0; i < label_.size(); ++i)
-               delete label_[i];
+       for (auto & i : label_)
+               delete i;
 }
 
 
@@ -247,8 +248,8 @@ InsetMathHull & InsetMathHull::operator=(InsetMathHull const & other)
        numbered_ = other.numbered_;
        numbers_ = other.numbers_;
        buffer_ = other.buffer_;
-       for (size_t i = 0; i < label_.size(); ++i)
-               delete label_[i];
+       for (auto & i : label_)
+               delete i;
        label_ = other.label_;
        for (size_t i = 0; i != label_.size(); ++i) {
                if (label_[i])
@@ -520,9 +521,18 @@ bool previewTooSmall(Dimension const & dim)
 
 void InsetMathHull::metrics(MetricsInfo & mi, Dimension & dim) const
 {
-       // true value in LaTeX is 12pt plus 3pt minus 9pt
-       // FIXME: even better would be to handle the short skip case.
-       int const display_margin = display() ? mi.base.inPixels(Length(12, Length::PT)) : 0;
+       /* Compute \(above|below)displayskip
+          true value in LaTeX is 10pt plus 2pt minus 5pt (in normal size at 10pt)
+          FIXME: make this dependent of current size? (minor improvement)
+          FIXME: if would be nice if this was not part of the inset, but
+                 just increased the row ascent/descent.
+          FIXME: even better would be to handle the short skip case.
+       */
+       int const bottom_display_margin = mi.base.inPixels(Length(10, Length::PT));
+       int top_display_margin = bottom_display_margin;
+       // at start of paragraph, add an empty line
+       if (mi.vmode)
+               top_display_margin += theFontMetrics(mi.base.font).maxHeight() + 2;
 
        if (previewState(mi.base.bv)) {
                preview_->metrics(mi, dim);
@@ -535,8 +545,8 @@ void InsetMathHull::metrics(MetricsInfo & mi, Dimension & dim) const
                        // value was hardcoded to 1 pixel
                        dim.wid += mi.base.bv->zoomedPixels(1) ;
                        if (display()) {
-                               dim.asc += display_margin;
-                               dim.des += display_margin;
+                               dim.asc += top_display_margin;
+                               dim.des += bottom_display_margin;
                        }
                }
                return;
@@ -577,8 +587,8 @@ void InsetMathHull::metrics(MetricsInfo & mi, Dimension & dim) const
 
 
        if (display()) {
-               dim.asc += display_margin;
-               dim.des += display_margin;
+               dim.asc += top_display_margin;
+               dim.des += bottom_display_margin;
        }
 
        // reserve some space for marker.
@@ -604,7 +614,7 @@ void InsetMathHull::drawMarkers(PainterInfo & pi, int x, int y) const
        ColorCode pen_color = mouseHovered(pi.base.bv) || editing(pi.base.bv)?
                Color_mathframe : Color_mathcorners;
        // If the corners have the same color as the background, do not paint them.
-       if (lcolor.getX11Name(Color_mathbg) == lcolor.getX11Name(pen_color))
+       if (lcolor.getX11HexName(Color_mathbg) == lcolor.getX11HexName(pen_color))
                return;
 
        Inset::drawMarkers(pi, x, y);
@@ -985,8 +995,8 @@ bool InsetMathHull::ams() const
        case hullEqnArray:
                break;
        }
-       for (size_t row = 0; row < numbered_.size(); ++row)
-               if (numbered_[row] == NOTAG)
+       for (auto const & row : numbered_)
+               if (row == NOTAG)
                        return true;
        return false;
 }
@@ -1569,7 +1579,7 @@ void InsetMathHull::mutate(HullType newtype)
                        numbered(0, false);
                } else {
                        setType(hullEquation);
-                       numbered(0, label_[0] ? true : false);
+                       numbered(0, label_[0] != nullptr);
                        mutate(newtype);
                }
                break;
@@ -2404,69 +2414,81 @@ int InsetMathHull::plaintext(odocstringstream & os,
 }
 
 
-int InsetMathHull::docbook(odocstream & os, OutputParams const & runparams) const
+void InsetMathHull::docbook(XMLStream & xs, OutputParams const & runparams) const
 {
-       // With DocBook 5, MathML must be within its own namespace; defined in Buffer.cpp::writeDocBookSource as "m".
-       // Output everything in a separate stream so that this does not interfere with the standard flow of DocBook tags.
-       odocstringstream osmath;
-       MathStream ms(osmath, "m", true);
-       int res = 0;
-
        // Choose the tag around the MathML equation.
        docstring name;
+       bool doCR = false;
        if (getType() == hullSimple)
                name = from_ascii("inlineequation");
-       else
+       else {
+               doCR = true; // This is a block equation, always have <informalequation> on its own line.
                name = from_ascii("informalequation");
+       }
+
+       // DocBook also has <equation>, but it comes with a title.
+       // TODO: recognise \tag from amsmath? This would allow having <equation> with a proper title.
 
-       docstring bname = name;
+       docstring attr;
        for (row_type i = 0; i < nrows(); ++i) {
                if (!label(i).empty()) {
-                       bname += " xml:id=\"" + xml::cleanID(label(i)) + "\"";
+                       attr = "xml:id=\"" + xml::cleanID(label(i)) + "\"";
                        break;
                }
        }
 
-       ++ms.tab(); ms.cr(); ms.os() << '<' << bname << '>';
+       if (doCR)
+               if (!xs.isLastTagCR())
+                       xs << xml::CR();
 
+       xs << xml::StartTag(name, attr);
+       xs << xml::CR();
+
+       // With DocBook 5, MathML must be within its own namespace; defined in Buffer.cpp::writeDocBookSource as "m".
+       // Output everything in a separate stream so that this does not interfere with the standard flow of DocBook tags.
+       odocstringstream osmath;
+       MathStream ms(osmath, "m", true);
+
+       // Output the MathML subtree.
        odocstringstream ls;
        otexstream ols(ls);
-       if (runparams.flavor == OutputParams::DOCBOOK5) {
-               ms << MTag("alt role='tex' ");
-               // Workaround for db2latex: db2latex always includes equations with
-               // \ensuremath{} or \begin{display}\end{display}
-               // so we strip LyX' math environment
-               WriteStream wi(ols, false, false, WriteStream::wsDefault, runparams.encoding);
-               InsetMathGrid::write(wi);
-               ms << from_utf8(subst(subst(to_utf8(ls.str()), "&", "&amp;"), "<", "&lt;"));
-               ms << ETag("alt");
-               ms << MTag("math");
-               ms << ETag("alt");
+
+       // TeX transcription. Avoid MTag/ETag so that there are no extraneous spaces.
+       ms << "<" << from_ascii("alt") << " role='tex'" << ">";
+       // Workaround for db2latex: db2latex always includes equations with
+       // \ensuremath{} or \begin{display}\end{display}
+       // so we strip LyX' math environment
+       WriteStream wi(ols, false, false, WriteStream::wsDefault, runparams.encoding);
+       InsetMathGrid::write(wi);
+       ms << from_utf8(subst(subst(to_utf8(ls.str()), "&", "&amp;"), "<", "&lt;"));
+       ms << "</" << from_ascii("alt") << ">";
+
+       // Actual transformation of the formula into MathML. This translation may fail (for example, due to custom macros).
+       // The new output stream is required to deal with the errors: first write completely the formula into this
+       // temporary stream; then, if it is possible without error, then copy it back to the "real" stream. Otherwise,
+       // some incomplete tags might be put into the real stream.
+       try {
+               // First, generate the MathML expression.
+               odocstringstream ostmp;
+               MathStream mstmp(ostmp, ms.xmlns(), ms.xmlMode());
+               InsetMathGrid::mathmlize(mstmp);
+
+               // Then, output it (but only if the generation can be done without errors!).
                ms << MTag("math");
-               InsetMathGrid::mathmlize(ms);
+               ms.cr();
+               osmath << ostmp.str(); // osmath is not a XMLStream, so no need for XMLStream::ESCAPE_NONE.
                ms << ETag("math");
-       } else {
-               ms << MTag("alt role='tex'");
-               latex(ols, runparams);
-               res = ols.texrow().rows();
-               ms << from_utf8(subst(subst(to_utf8(ls.str()), "&", "&amp;"), "<", "&lt;"));
-               ms << ETag("alt");
+       } catch (MathExportException const &) {
+               ms.cr();
+               osmath << "<mathphrase>MathML export failed. Please report this as a bug.</mathphrase>";
        }
 
-       ms << from_ascii("<graphic fileref=\"eqn/");
-       if (!label(0).empty())
-               ms << xml::cleanID(label(0));
-       else
-               ms << xml::uniqueID(from_ascii("anon"));
-
-       if (runparams.flavor == OutputParams::DOCBOOK5)
-               ms << from_ascii("\"/>");
-       else
-               ms << from_ascii("\">");
-
-       ms.cr(); --ms.tab(); ms.os() << "</" << name << '>';
-
-       return ms.line() + res;
+       // Output the complete formula to the DocBook stream.
+       xs << XMLStream::ESCAPE_NONE << osmath.str();
+       xs << xml::CR();
+       xs << xml::EndTag(name);
+       if (doCR)
+               xs << xml::CR();
 }