commit 4e981b034eba8b04534931e570a8f64b7396ad7d
Author: Jean-Marc Lasgouttes <[email protected]>
Date:   Fri Apr 27 00:03:48 2018 +0200

    Adapt caret height to context in mathed.
    
    Compute a height from current font and current cell vertical
    dimensions in MathData::metrics(), because this is where current font
    is known.
    
    Introduce BufferView::setCaretAscentDescent to remember this value.
    
    This mechanism is not used for text because Cursor::current_font is
    restored by undo, and the caret height would not be changed then. But
    in principle it is doable.
    
    (cherry picked from commit 90cfe4ec3b4ff22ef798a63e98ca70d0d33a1656)
---
 src/BufferView.cpp      |   33 +++++++++++++++++++++++++++------
 src/BufferView.h        |    2 ++
 src/mathed/MathData.cpp |   14 +++++++++++---
 status.23x              |    4 ++++
 4 files changed, 44 insertions(+), 9 deletions(-)

diff --git a/src/BufferView.cpp b/src/BufferView.cpp
index 2101ede..da09fcd 100644
--- a/src/BufferView.cpp
+++ b/src/BufferView.cpp
@@ -229,7 +229,8 @@ enum ScreenUpdateStrategy {
 
 struct BufferView::Private
 {
-       Private(BufferView & bv) : update_strategy_(FullScreenUpdate),
+       Private(BufferView & bv) :
+               update_strategy_(FullScreenUpdate),
                update_flags_(Update::Force),
                wh_(0), cursor_(bv),
                anchor_pit_(0), anchor_ypos_(0),
@@ -237,7 +238,8 @@ struct BufferView::Private
                last_inset_(0), clickable_inset_(false),
                mouse_position_cache_(),
                bookmark_edit_position_(-1), gui_(0),
-               horiz_scroll_offset_(0)
+               horiz_scroll_offset_(0),
+               caret_ascent_(0), caret_descent_(0)
        {
                xsel_cache_.set = false;
        }
@@ -316,6 +318,12 @@ struct BufferView::Private
        /// a slice pointing to the start of the row where cursor was
        /// at previous draw event
        CursorSlice last_row_slice_;
+
+       // The vertical size of the blinking caret. Only used for math
+       // Using it for text could be bad when undo restores the cursor
+       // current font, since the caret size could become wrong.
+       int caret_ascent_;
+       int caret_descent_;
 };
 
 
@@ -2984,13 +2992,26 @@ bool BufferView::paragraphVisible(DocIterator const & 
dit) const
 }
 
 
+void BufferView::setCaretAscentDescent(int asc, int des)
+{
+       d->caret_ascent_ = asc;
+       d->caret_descent_ = des;
+}
+
+
 void BufferView::caretPosAndHeight(Point & p, int & h) const
 {
+       int asc, des;
        Cursor const & cur = cursor();
-       Font const font = cur.real_current_font;
-       frontend::FontMetrics const & fm = theFontMetrics(font);
-       int const asc = fm.maxAscent();
-       int const des = fm.maxDescent();
+       if (cur.inMathed()) {
+               asc = d->caret_ascent_;
+               des = d->caret_descent_;
+       } else {
+               Font const font = cur.real_current_font;
+               frontend::FontMetrics const & fm = theFontMetrics(font);
+               asc = fm.maxAscent();
+               des = fm.maxDescent();
+       }
        h = asc + des;
        p = getPos(cur);
        p.y_ -= asc;
diff --git a/src/BufferView.h b/src/BufferView.h
index 733e66f..1089781 100644
--- a/src/BufferView.h
+++ b/src/BufferView.h
@@ -305,6 +305,8 @@ public:
        bool paragraphVisible(DocIterator const & dit) const;
        /// is the cursor currently visible in the view
        bool cursorInView(Point const & p, int h) const;
+       /// set the ascent and descent of the caret
+       void setCaretAscentDescent(int asc, int des);
        /// get the position and height of the caret
        void caretPosAndHeight(Point & p, int & h) const;
 
diff --git a/src/mathed/MathData.cpp b/src/mathed/MathData.cpp
index a4a960a..059b8b5 100644
--- a/src/mathed/MathData.cpp
+++ b/src/mathed/MathData.cpp
@@ -267,6 +267,7 @@ bool isInside(DocIterator const & it, MathData const & ar,
 void MathData::metrics(MetricsInfo & mi, Dimension & dim, bool tight) const
 {
        frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
+       BufferView * bv = mi.base.bv;
        int const Iascent = fm.dimension('I').ascent();
        int xascent = fm.dimension('x').ascent();
        if (xascent >= Iascent)
@@ -278,8 +279,8 @@ void MathData::metrics(MetricsInfo & mi, Dimension & dim, 
bool tight) const
 
        MathRow mrow(mi, this);
        mrow.metrics(mi, dim);
-       mrow_cache_[mi.base.bv] = mrow;
-       kerning_ = mrow.kerning(mi.base.bv);
+       mrow_cache_[bv] = mrow;
+       kerning_ = mrow.kerning(bv);
 
        // Set a minimal ascent/descent for the cell
        if (tight)
@@ -291,8 +292,15 @@ void MathData::metrics(MetricsInfo & mi, Dimension & dim, 
bool tight) const
                dim.des = max(dim.des, fm.maxDescent());
        }
 
+       // This is one of the the few points where the drawing font is known,
+       // so that we can set the caret vertical dimensions.
+       Cursor & cur = bv->cursor();
+       if (cur.inMathed() && &cur.cell() == this)
+               bv->setCaretAscentDescent(min(dim.asc, fm.maxAscent()),
+                                         min(dim.des, fm.maxDescent()));
+
        // Cache the dimension.
-       mi.base.bv->coordCache().arrays().add(this, dim);
+       bv->coordCache().arrays().add(this, dim);
 }
 
 
diff --git a/status.23x b/status.23x
index 49f5458..881f7cd 100644
--- a/status.23x
+++ b/status.23x
@@ -73,6 +73,8 @@ What's new
   to relevant directories) by setting the preference
   \use_native_filedialog true
 
+- Let caret height depend on character size in math editor.
+
 - Handle properly top/bottom of inset with mac-like cursor movement
   (bug 10701).
 
@@ -197,6 +199,8 @@ What's new
 
 - Improve rendering of square roots in math editor (bug 10814).
 
+- Set minimum height for math cells (bug 11050).
+
 - Fix display of citation labels when pasting from a document
   with other citation type (bug 10829).
 

Reply via email to