]> git.lyx.org Git - features.git/commitdiff
Adapt caret height to context in mathed.
authorJean-Marc Lasgouttes <lasgouttes@lyx.org>
Thu, 26 Apr 2018 22:03:48 +0000 (00:03 +0200)
committerJean-Marc Lasgouttes <lasgouttes@lyx.org>
Thu, 10 May 2018 10:53:30 +0000 (12:53 +0200)
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
src/BufferView.h
src/mathed/MathData.cpp
status.23x

index 2101ede70210d68c7aa91afc138db81eb214d0f7..da09fcd273c1d3976f9dcadf51e89eead460a497 100644 (file)
@@ -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;
index 733e66f9261003119dafd5da8926d81c1a4b69ec..1089781ffdb79ab5d0f64ad6e12f1c34accf0253 100644 (file)
@@ -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;
 
index a4a960a7d2ef89c0bdcc4b36c0b7dfa69b419c8d..059b8b56b71bff1b064ca74525f146caddb12818 100644 (file)
@@ -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);
 }
 
 
index 49f5458b030fa0b6991ae11a66d50c2e5de10f78..881f7cd8cb6ee7deb68a6aea7088cf125d973737 100644 (file)
@@ -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).