]> git.lyx.org Git - features.git/blobdiff - src/TextMetrics.cpp
Change getColumnNearX to getPosNearX
[features.git] / src / TextMetrics.cpp
index ca6408d2bc41add4f75e3b64f4f50053733ebc02..9e775e0defe35746c50bc8cb11619018d3a6b9a1 100644 (file)
@@ -15,7 +15,7 @@
  * Full author contact details are available in file CREDITS.
  */
 
-//#define KEEP_OLD_METRICS_CODE 1
+#define KEEP_OLD_METRICS_CODE 1
 
 #include <config.h>
 
@@ -810,8 +810,8 @@ void TextMetrics::breakRow(Row & row, int const right_margin, pit_type const pit
        int const width = max_width_ - right_margin;
        pos_type const body_pos = par.beginOfBody();
        row.clear();
-       row.dimension().wid = leftMargin(max_width_, pit, pos);
-       row.x = row.width();
+       row.x = leftMargin(max_width_, pit, pos);
+       row.dimension().wid = row.x;
        row.right_margin = right_margin;
 
        if (pos >= end || row.width() > width) {
@@ -1114,7 +1114,7 @@ void TextMetrics::setRowHeight(Row & row, pit_type const pit,
 // x is an absolute screen coord
 // returns the column near the specified x-coordinate of the row
 // x is set to the real beginning of this column
-pos_type TextMetrics::getColumnNearX(pit_type const pit,
+pos_type TextMetrics::getPosNearX(pit_type const pit,
                Row const & row, int & x, bool & boundary) const
 {
 
@@ -1129,11 +1129,16 @@ pos_type TextMetrics::getColumnNearX(pit_type const pit,
 
        pos_type pos = row.pos();
        boundary = false;
-       if (row.x >= x || row.empty())
+       if (row.empty())
                x = row.x;
-       else if (x >= row.width() - row.right_margin) {
+       else if (x < row.x) {
+               pos = row.front().font.isVisibleRightToLeft() ?
+                       row.front().endpos : row.front().pos;
+               x = row.x;
+       } else if (x > row.width() - row.right_margin) {
+               pos = row.back().font.isVisibleRightToLeft() ?
+                       row.back().pos : row.back().endpos;
                x = row.width() - row.right_margin;
-               pos = row.back().endpos;
        } else {
                double w = row.x;
                Row::const_iterator cit = row.begin();
@@ -1170,9 +1175,7 @@ pos_type TextMetrics::getColumnNearX(pit_type const pit,
                boundary = row.right_boundary();
 
        x += xo;
-#if !defined(KEEP_OLD_METRICS_CODE)
-       return pos - row.pos();
-#else
+#ifdef KEEP_OLD_METRICS_CODE
        Buffer const & buffer = bv_->buffer();
 
        int x2 = x_orig;
@@ -1284,14 +1287,15 @@ pos_type TextMetrics::getColumnNearX(pit_type const pit,
 #endif
 
        x2 = int(tmpx) + xo;
-       pos_type const col = c - row.pos();
+       //pos_type const col = c - row.pos();
 
        if (abs(x2 - x) > 0.1 || boundary != boundary
            || c != pos) {
-               lyxerr << "getColumnNearX: new=(x=" << x - xo << ", b=" << boundary << ", p=" << pos << "), "
+               lyxerr << "getPosNearX(" << x_orig << "): new=(x=" << x - xo << ", b=" << boundary << ", p=" << pos << "), "
                       << "old=(x=" << x2 - xo << ", b=" << boundary2 << ", p=" << c << "), " << row;
        }
 
+#if 0
        if (!c || end == par.size())
                return col;
 
@@ -1301,7 +1305,9 @@ pos_type TextMetrics::getColumnNearX(pit_type const pit,
        }
 
        return min(col, end - 1 - row.pos());
-#endif
+#endif // 0
+#endif // KEEP_OLD_METRICS_CODE
+       return pos;
 }
 
 
@@ -1316,7 +1322,7 @@ pos_type TextMetrics::x2pos(pit_type pit, int row, int x) const
        LBUFERR(row < int(pm.rows().size()));
        bool bound = false;
        Row const & r = pm.rows()[row];
-       return r.pos() + getColumnNearX(pit, r, x, bound);
+       return getPosNearX(pit, r, x, bound);
 }
 
 
@@ -1476,10 +1482,9 @@ Inset * TextMetrics::editXY(Cursor & cur, int x, int y,
 
        if (!it) {
                // No inset, set position in the text
-               bool bound = false; // is modified by getColumnNearX
-               int xx = x; // is modified by getColumnNearX
-               cur.pos() = row.pos()
-                       + getColumnNearX(pit, row, xx, bound);
+               bool bound = false; // is modified by getPosNearX
+               int xx = x; // is modified by getPosNearX
+               cur.pos() = getPosNearX(pit, row, xx, bound);
                cur.boundary(bound);
                cur.setCurrentFont();
                cur.setTargetX(xx);
@@ -1530,7 +1535,7 @@ void TextMetrics::setCursorFromCoordinates(Cursor & cur, int const x, int const
 
        bool bound = false;
        int xx = x;
-       pos_type const pos = row.pos() + getColumnNearX(pit, row, xx, bound);
+       pos_type const pos = getPosNearX(pit, row, xx, bound);
 
        LYXERR(Debug::DEBUG, "setting cursor pit: " << pit << " pos: " << pos);
 
@@ -1607,17 +1612,18 @@ int TextMetrics::cursorX(CursorSlice const & sl,
        double x = row.x;
 
        /**
-        * When boundary is true, position is on the row element (pos, endpos)
+        * When boundary is true, position i is in the row element (pos, endpos)
         * if
-        *    pos < pos <= endpos
+        *    pos < i <= endpos
         * whereas, when boundary is false, the test is
-        *    pos <= pos < endpos
+        *    pos <= i < endpos
         * The correction below allows to handle both cases.
        */
        int const boundary_corr = (boundary && pos) ? -1 : 0;
 
+       //?????
        if (row.empty()
-           || (row.begin()->font.isRightToLeft()
+           || (row.begin()->font.isVisibleRightToLeft()
                && pos == row.begin()->endpos))
                return int(x);
 
@@ -1632,10 +1638,9 @@ int TextMetrics::cursorX(CursorSlice const & sl,
        }
 
        if (cit == row.end()
-           && (row.back().font.isRightToLeft() || pos != row.back().endpos))
-               lyxerr << "NOT FOUND!"
-                      << "pos=" << pos << "(" << boundary_corr << ")" << "\n"
-                      << row;
+           && (row.back().font.isVisibleRightToLeft() || pos != row.back().endpos))
+               LYXERR0("cursorX(" << pos - boundary_corr
+                       << ", " << boundary_corr << "): NOT FOUND! " << row);
 
 #ifdef KEEP_OLD_METRICS_CODE
        Paragraph const & par = text_->paragraphs()[pit];
@@ -1774,18 +1779,16 @@ int TextMetrics::cursorX(CursorSlice const & sl,
        }
 
        if (abs(x2 - x) > 0.01) {
-               lyxerr << "cursorX: x2=" << x2 << ", x=" << x;
+               lyxerr << "cursorX(" << pos - boundary_corr << ", " << boundary_corr
+                      << "): old=" << x2 << ", new=" << x;
                if (cit == row.end())
-                       lyxerr << "Element not found for "
-                              << pos - boundary_corr << "(" << boundary_corr << ")";
+                       lyxerr << "Element not found\n";
                else
-                       lyxerr << " in [" << cit->pos << "/"
-                              << pos - boundary_corr << "(" << boundary_corr << ")"
-                              << "/" << cit->endpos << "] of " << *cit << "\n";
+                       lyxerr << " found in " << *cit << "\n";
                lyxerr << row <<endl;
        }
-#endif
 
+#endif // KEEP_OLD_METRICS_CODE
        return int(x);
 }