#include "qt_helpers.h"
-#include "Language.h"
#include "Dimension.h"
+#include "Language.h"
+#include "LyXRC.h"
#include "insets/Inset.h"
#include "support/lassert.h"
using namespace std;
+using namespace lyx::support;
namespace lyx {
namespace frontend {
* 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)
{
} // anon namespace
-GuiFontMetrics::GuiFontMetrics(QFont const & font) : metrics_(font, 0)
+// Limit strwidth_cache_ size to 512kB of string data
+GuiFontMetrics::GuiFontMetrics(QFont const & font)
+ : font_(font), metrics_(font, 0),
+ strwidth_cache_(1 << 19),
+ tl_cache_rtl_(false), tl_cache_wordspacing_(-1.0)
{
}
}
+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;
int GuiFontMetrics::width(docstring const & s) const
{
- size_t ls = s.size();
- int w = 0;
- for (unsigned int i = 0; i < ls; ++i) {
- //FIXME: we need to detect surrogate pairs and act accordingly
- /**
- if isSurrogateBase(s[i]) {
- docstring c = s[i];
- w += metrics_.width(toqstr(c + s[i + 1]));
- ++i;
- }
- else
- */
- w += width(s[i]);
- }
-
+ QByteArray qba =
+ QByteArray(reinterpret_cast<char const *>(s.data()),
+ s.size() * sizeof(docstring::value_type));
+ int * pw = strwidth_cache_[qba];
+ if (pw)
+ return *pw;
+ int w = metrics_.width(toqstr(s));
+ strwidth_cache_.insert(qba, new int(w), qba.size());
return w;
}
}
+QTextLayout const &
+GuiFontMetrics::getTextLayout(docstring const & s, QFont font,
+ bool const rtl, double const wordspacing) const
+{
+ if (s != tl_cache_s_ || font != tl_cache_font_ || rtl != tl_cache_rtl_
+ || wordspacing != tl_cache_wordspacing_) {
+ tl_cache_.setText(toqstr(s));
+ font.setWordSpacing(wordspacing);
+ tl_cache_.setFont(font);
+ // Note that both setFlags and the enums are undocumented
+ tl_cache_.setFlags(rtl ? Qt::TextForceRightToLeft : Qt::TextForceLeftToRight);
+ tl_cache_.beginLayout();
+ tl_cache_.createLine();
+ tl_cache_.endLayout();
+ tl_cache_s_ = s;
+ tl_cache_font_ = font;
+ tl_cache_rtl_ = rtl;
+ tl_cache_wordspacing_ = wordspacing;
+ }
+ return tl_cache_;
+}
+
+
+int GuiFontMetrics::pos2x(docstring const & s, int const pos, bool const rtl,
+ double const wordspacing) const
+{
+ QFont copy = font_;
+ copy.setWordSpacing(wordspacing);
+ QTextLayout const & tl = getTextLayout(s, font_, rtl, wordspacing);
+ return static_cast<int>(tl.lineForTextPosition(pos).cursorToX(pos));
+}
+
+
+int GuiFontMetrics::x2pos(docstring const & s, int & x, bool const rtl,
+ double const wordspacing) const
+{
+ QTextLayout const & tl = getTextLayout(s, font_, rtl, wordspacing);
+ int pos = tl.lineForTextPosition(0).xToCursor(x);
+ // correct x value to the actual cursor position.
+ 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
{