X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Ffrontends%2Fqt4%2FGuiFontMetrics.C;h=ecfe48722e07ae3c5bf8d75683f58db5147b9d11;hb=591d47a64b8ba894be5c793472397374d0496984;hp=6fcc79930a665a007854097e3b846c5f4c698881;hpb=a21259dd02954a6aa5707bb0bf4da0f826e44a4b;p=lyx.git diff --git a/src/frontends/qt4/GuiFontMetrics.C b/src/frontends/qt4/GuiFontMetrics.C index 6fcc79930a..ecfe48722e 100644 --- a/src/frontends/qt4/GuiFontMetrics.C +++ b/src/frontends/qt4/GuiFontMetrics.C @@ -17,20 +17,13 @@ #include "language.h" -#include "frontends/lyx_gui.h" - #include "support/unicode.h" -using lyx::char_type; -using lyx::docstring; - using std::string; - namespace lyx { namespace frontend { - GuiFontMetrics::GuiFontMetrics(QFont const & font) : metrics_(font), smallcaps_metrics_(font), smallcaps_shape_(false) { @@ -45,67 +38,26 @@ GuiFontMetrics::GuiFontMetrics(QFont const & font, QFont const & smallcaps_font) int GuiFontMetrics::maxAscent() const { - if (!lyx_gui::use_gui) - return 1; return metrics_.ascent(); } int GuiFontMetrics::maxDescent() const { - if (!lyx_gui::use_gui) - return 1; // We add 1 as the value returned by QT is different than X // See http://doc.trolltech.com/2.3/qfontmetrics.html#200b74 return metrics_.descent() + 1; } -int GuiFontMetrics::ascent(char_type c) const -{ - if (!lyx_gui::use_gui) - return 1; - QRect const & r = metrics_.boundingRect(ucs4_to_qchar(c)); - // Qt/Win 3.2.1nc (at least) corrects the GetGlyphOutlineA|W y - // value by the height: (x, -y-height, width, height). - // Other versions return: (x, -y, width, height) -#if defined(Q_WS_WIN) && (QT_VERSION == 0x030201) - return -r.top() - r.height(); -#else - return -r.top(); -#endif -} - - -int GuiFontMetrics::descent(char_type c) const -{ - if (!lyx_gui::use_gui) - return 1; - QRect const & r = metrics_.boundingRect(ucs4_to_qchar(c)); - // Qt/Win 3.2.1nc (at least) corrects the GetGlyphOutlineA|W y - // value by the height: (x, -y-height, width, height). - // Other versions return: (x, -y, width, height) -#if defined(Q_WS_WIN) && (QT_VERSION == 0x030201) - return r.bottom() + r.height() + 1; -#else - return r.bottom() + 1; -#endif -} - - int GuiFontMetrics::lbearing(char_type c) const { - if (!lyx_gui::use_gui) - return 1; return metrics_.leftBearing(ucs4_to_qchar(c)); } int GuiFontMetrics::rbearing(char_type c) const { - if (!lyx_gui::use_gui) - return 1; - // Qt rbearing is from the right edge of the char's width(). QChar sc = ucs4_to_qchar(c); return metrics_.width(sc) - metrics_.rightBearing(sc); @@ -114,9 +66,6 @@ int GuiFontMetrics::rbearing(char_type c) const int GuiFontMetrics::smallcapsWidth(QString const & s) const { - if (!lyx_gui::use_gui) - return 1; - int w = 0; int const ls = s.size(); @@ -134,20 +83,40 @@ int GuiFontMetrics::smallcapsWidth(QString const & s) const int GuiFontMetrics::width(char_type const * s, size_t ls) const { - if (!lyx_gui::use_gui) - return ls; + // Caution: The following ucs4_to_something conversions work for + // symbol fonts only because they are no real conversions but simple + // casts in reality. - QString ucs2; - ucs4_to_qstring(s, ls, ucs2); + if (ls == 1 && !smallcaps_shape_) { + return width(s[0]); + } - if (smallcaps_shape_) + if (smallcaps_shape_) { + QString ucs2; + ucs4_to_qstring(s, ls, ucs2); return smallcapsWidth(ucs2); + } + + int w = 0; + for (unsigned int i = 0; i < ls; ++i) + w += width(s[i]); - if (ls == 1) + return w; +} + + +int GuiFontMetrics::width(QString const & ucs2) const +{ + int const ls = ucs2.size(); + if (ls == 1 && !smallcaps_shape_) { return width(ucs2[0].unicode()); + } + + if (smallcaps_shape_) + return smallcapsWidth(ucs2); int w = 0; - for (unsigned int i = 0; i < ls; ++i) + for (int i = 0; i < ls; ++i) w += width(ucs2[i].unicode()); return w; @@ -156,8 +125,11 @@ int GuiFontMetrics::width(char_type const * s, size_t ls) const int GuiFontMetrics::signedWidth(docstring const & s) const { + if (s.empty()) + return 0; + if (s[0] == '-') - return -FontMetrics::width(s.substr(1, s.length() - 1)); + return -width(&(s[1]), s.length() - 1); else return FontMetrics::width(s); } @@ -177,25 +149,67 @@ void GuiFontMetrics::rectText(docstring const & str, void GuiFontMetrics::buttonText(docstring const & str, int & w, int & ascent, int & descent) const { - QFontMetrics const & m = metrics_; static int const d = 3; w = FontMetrics::width(str) + d * 2 + 2; ascent = metrics_.ascent() + d; descent = metrics_.descent() + d; } -#ifdef USE_LYX_FONTCACHE -int GuiFontMetrics::width(unsigned short val) const +#ifndef USE_LYX_FONTCACHE + +int GuiFontMetrics::ascent(char_type c) const { - GuiFontMetrics::WidthCache::const_iterator cit = widthcache.find(val); - if (cit != widthcache.end()) - return cit->second; + QRect const & r = metrics_.boundingRect(ucs4_to_qchar(c)); + return -r.top(); +} - int const w = metrics_.width(QChar(val)); - widthcache[val] = w; - return w; + +int GuiFontMetrics::descent(char_type c) const +{ + QRect const & r = metrics_.boundingRect(ucs4_to_qchar(c)); + return r.bottom() + 1; } -#endif +#else + +void GuiFontMetrics::fillMetricsCache(char_type c) const +{ + QRect const & r = metrics_.boundingRect(ucs4_to_qchar(c)); + AscendDescend ad = { -r.top(), r.bottom() + 1}; + // We could as well compute the width but this is not really + // needed for now as it is done directly in width() below. + metrics_cache_.insert(c, ad); +} + + +int GuiFontMetrics::width(char_type c) const +{ + if (!width_cache_.contains(c)) { + width_cache_.insert(c, metrics_.width(ucs4_to_qchar(c))); + } + + return width_cache_.value(c); } + + +int GuiFontMetrics::ascent(char_type c) const +{ + if (!metrics_cache_.contains(c)) + fillMetricsCache(c); + + return metrics_cache_.value(c).ascent; } + + +int GuiFontMetrics::descent(char_type c) const +{ + if (!metrics_cache_.contains(c)) + fillMetricsCache(c); + + return metrics_cache_.value(c).descent; +} + +#endif + +} // frontend +} // lyx