]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiFontMetrics.cpp
Use QFontMetrics information for underlines (and friends) width and position
[lyx.git] / src / frontends / qt4 / GuiFontMetrics.cpp
index 2ac588cebaacf347d0669bfbaf02626c80d5af45..2214623d3b7b9b4befc7c3aff5125f21e8957217 100644 (file)
@@ -26,6 +26,7 @@
 #include <QTextLayout>
 
 using namespace std;
+using namespace lyx::support;
 
 namespace lyx {
 namespace frontend {
@@ -42,7 +43,7 @@ namespace {
  * why this works well for symbol fonts used in mathed too, even though
  * these are not real ucs4 characters. These are codepoints in the
  * computer modern fonts used, nothing unicode related.
- * See comment in QLPainter::text() for more explanation.
+ * See comment in GuiPainter::text() for more explanation.
  **/
 inline QChar const ucs4_to_qchar(char_type const ucs4)
 {
@@ -71,10 +72,34 @@ int GuiFontMetrics::maxDescent() const
 }
 
 
+int GuiFontMetrics::em() const
+{
+       return QFontInfo(font_).pixelSize();
+}
+
+
+int GuiFontMetrics::lineWidth() const
+{
+       return metrics_.lineWidth();
+}
+
+
+int GuiFontMetrics::underlinePos() const
+{
+       return metrics_.underlinePos();
+}
+
+
+int GuiFontMetrics::strikeoutPos() const
+{
+       return metrics_.strikeOutPos();
+}
+
+
 int GuiFontMetrics::lbearing(char_type c) const
 {
        if (!is_utf16(c))
-               // FIXME: QFontMetrics::leftBearingdoes not support the
+               // FIXME: QFontMetrics::leftBearing does not support the
                //        full unicode range. Once it does, we could use:
                //return metrics_.leftBearing(toqstr(docstring(1, c)));
                return 0;
@@ -143,27 +168,14 @@ int GuiFontMetrics::signedWidth(docstring const & s) const
 }
 
 namespace {
-void setTextLayout(QTextLayout & tl, docstring const & s, QFont const & font,
-                            bool const rtl)
+void setTextLayout(QTextLayout & tl, docstring const & s, QFont font,
+                   bool const rtl, double const wordspacing)
 {
-       QString qs;
-       /* In LyX, the character direction is forced by the language.
-        * Therefore, we have to signal that fact to Qt.
-        * Source: http://www.iamcal.com/understanding-bidirectional-text/
-        */
-       // Left-to-right override: forces to draw text left-to-right
-       char_type const LRO = 0x202D;
-       // Right-to-left override: forces to draw text right-to-left
-       char_type const RLO = 0x202E;
-       // Pop directional formatting: return to previous state
-       char_type const PDF = 0x202C;
-       if (rtl)
-               qs = toqstr(RLO + s + PDF);
-       else
-               qs = toqstr(LRO + s + PDF);
-
-       tl.setText(qs);
+       tl.setText(toqstr(s));
+       font.setWordSpacing(wordspacing);
        tl.setFont(font);
+       // Note that both setFlags and the enums are undocumented
+       tl.setFlags(rtl ? Qt::TextForceRightToLeft : Qt::TextForceLeftToRight);
        tl.beginLayout();
        tl.createLine();
        tl.endLayout();
@@ -171,30 +183,54 @@ void setTextLayout(QTextLayout & tl, docstring const & s, QFont const & font,
 }
 
 
-int GuiFontMetrics::pos2x(docstring const & s, int const pos, bool const rtl) const
+int GuiFontMetrics::pos2x(docstring const & s, int const pos, bool const rtl,
+                          double const wordspacing) const
 {
        QTextLayout tl;
-       setTextLayout(tl, s, font_, rtl);
-       return tl.lineForTextPosition(pos + 1).cursorToX(pos + 1);
+       QFont copy = font_;
+       copy.setWordSpacing(wordspacing);
+       setTextLayout(tl, s, font_, rtl, wordspacing);
+       return static_cast<int>(tl.lineForTextPosition(pos).cursorToX(pos));
 }
 
 
-int GuiFontMetrics::x2pos(docstring const & s, int & x, bool const rtl) const
+int GuiFontMetrics::x2pos(docstring const & s, int & x, bool const rtl,
+                          double const wordspacing) const
 {
        QTextLayout tl;
-       setTextLayout(tl, s, font_, rtl);
+       setTextLayout(tl, s, font_, rtl, wordspacing);
        int pos = tl.lineForTextPosition(0).xToCursor(x);
-       // take into account the unicode formatting characters
-       if (pos > 0)
-               --pos;
-       if (pos > int(s.length()))
-               pos = s.length();
        // correct x value to the actual cursor position.
-       x = tl.lineForTextPosition(0).cursorToX(pos + 1);
+       x = static_cast<int>(tl.lineForTextPosition(0).cursorToX(pos));
        return pos;
 }
 
 
+bool GuiFontMetrics::breakAt(docstring & s, int & x, bool const rtl, bool const force) const
+{
+       if (s.empty())
+               return false;
+       QTextLayout tl;
+       tl.setText(toqstr(s));
+       tl.setFont(font_);
+       // Note that both setFlags and the enums are undocumented
+       tl.setFlags(rtl ? Qt::TextForceRightToLeft : Qt::TextForceLeftToRight);
+       QTextOption to;
+       to.setWrapMode(force ? QTextOption::WrapAnywhere : QTextOption::WordWrap);
+       tl.setTextOption(to);
+       tl.beginLayout();
+       QTextLine line = tl.createLine();
+       line.setLineWidth(x);
+       tl.createLine();
+       tl.endLayout();
+       if (int(line.naturalTextWidth()) > x)
+               return false;
+       x = int(line.naturalTextWidth());
+       s = s.substr(0, line.textLength());
+       return true;
+}
+
+
 void GuiFontMetrics::rectText(docstring const & str,
        int & w, int & ascent, int & descent) const
 {