]> git.lyx.org Git - lyx.git/commitdiff
Improve drawing of math roots (\sqrt and \root)
authorJean-Marc Lasgouttes <lasgouttes@lyx.org>
Fri, 31 Mar 2017 16:50:14 +0000 (18:50 +0200)
committerJean-Marc Lasgouttes <lasgouttes@lyx.org>
Fri, 31 Mar 2017 16:55:08 +0000 (18:55 +0200)
This is preliminary work to make roots look more like in TeX output:

* correct font size for root order

* set minimum size of the root from the max ascent descent of the
  font. This is what TeX does.

* fix drawing, and in particular horizontal size.

More needs to be done, in particular:

* handle zooming and dpi correctly by removing hardcoded pixel values.

* factor the code for the two types of roots in one.

src/mathed/InsetMathRoot.cpp
src/mathed/InsetMathSqrt.cpp

index f61db16e8f1d9dc3b24ea0c7005199274fe20568..db8954f8c2177c6549a527963e0ddce932266c23 100644 (file)
@@ -15,6 +15,7 @@
 
 #include "MathData.h"
 #include "MathStream.h"
+#include "MathSupport.h"
 
 #include "Cursor.h"
 #include "LaTeXFeatures.h"
@@ -42,27 +43,44 @@ Inset * InsetMathRoot::clone() const
 void InsetMathRoot::metrics(MetricsInfo & mi, Dimension & dim) const
 {
        Changer dummy = mi.base.changeEnsureMath();
-       cellsMetrics(mi);
-       Dimension const & dim0 = cell(0).dimension(*mi.base.bv);
-       Dimension const & dim1 = cell(1).dimension(*mi.base.bv);
-       dim.asc = max(dim0.ascent()  + 5, dim1.ascent())  + 2;
-       dim.des = max(dim0.descent() - 5, dim1.descent()) + 2;
-       dim.wid = dim0.width() + dim1.width() + 10;
+       Dimension dim0;
+       {
+               Changer script = mi.base.font.changeStyle(LM_ST_SCRIPTSCRIPT);
+               cell(0).metrics(mi, dim0);
+               // make sure that the dim is high enough for any character
+               Dimension fontDim;
+               math_font_max_dim(mi.base.font, fontDim.asc, fontDim.des);
+               dim0 += fontDim;
+       }
+
+       Dimension dim1;
+       cell(1).metrics(mi, dim1);
+       // make sure that the dim is high enough for any character
+       Dimension fontDim;
+       math_font_max_dim(mi.base.font, fontDim.asc, fontDim.des);
+       dim1 += fontDim;
+
+       dim.asc = max(dim0.ascent()  + 5, dim1.ascent()) + 1;
+       dim.des = max(dim0.descent() - 5, dim1.descent());
+       dim.wid = dim0.width() + dim1.width() + 4;
 }
 
 
 void InsetMathRoot::draw(PainterInfo & pi, int x, int y) const
 {
        Changer dummy = pi.base.changeEnsureMath();
+       Dimension const dim = dimension(*pi.base.bv);
+       int const a = dim.ascent();
+       int const d = dim.descent();
        Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
        int const w = dim0.width();
        // the "exponent"
-       cell(0).draw(pi, x, y - 5 - dim0.descent());
+       {
+               Changer script = pi.base.font.changeStyle(LM_ST_SCRIPTSCRIPT);
+               cell(0).draw(pi, x, y + (d - a)/2 - dim0.descent());
+       }
        // the "base"
-       cell(1).draw(pi, x + w + 8, y);
-       Dimension const dim = dimension(*pi.base.bv);
-       int const a = dim.ascent();
-       int const d = dim.descent();
+       cell(1).draw(pi, x + w + 4, y);
        int xp[4];
        int yp[4];
        pi.pain.line(x + dim.width(), y - a + 1,
index 4505d705671056901cb8faf8fed407edc1d03ada..d11dac084cf9ee640cfaa04b590b562c832e2e59 100644 (file)
@@ -14,6 +14,7 @@
 
 #include "MathData.h"
 #include "MathStream.h"
+#include "MathSupport.h"
 
 #include "LaTeXFeatures.h"
 #include "MetricsInfo.h"
@@ -39,9 +40,13 @@ void InsetMathSqrt::metrics(MetricsInfo & mi, Dimension & dim) const
 {
        Changer dummy = mi.base.changeEnsureMath();
        cell(0).metrics(mi, dim);
-       dim.asc += 4;
-       dim.des += 2;
-       dim.wid += 12;
+       // make sure that the dim is high enough for any character
+       Dimension fontDim;
+       math_font_max_dim(mi.base.font, fontDim.asc, fontDim.des);
+       dim += fontDim;
+       // Some room for the decoration
+       dim.asc += 1;
+       dim.wid += 7;
 }