From 00edcc582ff7cfe9cbbc8f7458ea29649bd642d9 Mon Sep 17 00:00:00 2001 From: Abdelrazak Younes Date: Sat, 7 Oct 2006 16:15:06 +0000 Subject: [PATCH] This commit is a big rework of the FontLoader/FontMetrics interaction. Only Qt4 for now, I would be grateful is somebody steps up for qt3 and gtk. Basically, I replaced all methods in the font_metrics namespace by a proper virtual interface FontMetrics. The FontLoader is _the_ container for FontMetrics. This patch should also bring some optimizations in a number of place in the code. This is because we do not need any more to search for the LyXFont at each font_metrics call. In effect, the speed advantage is not as sensible and this is a bit deceiving considering that this was my primary motivation behind the patch. But I like the patch anyway as it cleans up the relation and interfacing between fonts, metrics and frontends. * frontends/FontMetrics.h: new virtual interface. Renamed from font_metrics.h * qt4/GuiFontMetrics: corresponding qt4 implememtation. Renamed from qfont_metrics.C. The smallCaps particular case treatment has been transfered here as well as the width cache for MacOSX and Windows. * qt4/QLPainter.C: the smallCapsText has been reworked to return the width of the drawn text.C all other files: replace font_metric helper function call with corresponding FontMetrics method calls. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@15265 a592a061-630c-0410-9148-cb99ea01b6c8 --- development/scons/scons_manifest.py | 4 +- src/BufferView.C | 10 +- src/cursor.C | 1 - src/dimension.C | 9 +- src/frontends/FontLoader.h | 5 + .../{font_metrics.h => FontMetrics.h} | 100 +++++---- src/frontends/Makefile.am | 2 +- src/frontends/Painter.C | 19 +- src/frontends/WorkArea.C | 12 +- src/frontends/qt4/GuiFontLoader.C | 38 ++-- src/frontends/qt4/GuiFontLoader.h | 36 +--- src/frontends/qt4/GuiFontMetrics.C | 200 ++++++++++++++++++ src/frontends/qt4/GuiFontMetrics.h | 83 ++++++++ src/frontends/qt4/Makefile.am | 2 +- src/frontends/qt4/QLPainter.C | 26 +-- src/frontends/qt4/QLPainter.h | 5 +- src/frontends/qt4/qfont_metrics.C | 186 ---------------- src/frontends/qt4/qt_helpers.C | 1 + src/insets/insetbibitem.C | 9 +- src/insets/insetcaption.C | 10 +- src/insets/insetcharstyle.C | 8 +- src/insets/insetcollapsable.C | 9 +- src/insets/insetlatexaccent.C | 73 ++++--- src/insets/insetlatexaccent.h | 4 - src/insets/insetnewline.C | 19 +- src/insets/insetpagebreak.C | 6 +- src/insets/insetquotes.C | 19 +- src/insets/insetspace.C | 18 +- src/insets/insetspecialchar.C | 22 +- src/insets/insettabular.C | 1 - src/insets/insettext.C | 1 - src/insets/insetvspace.C | 8 +- src/insets/render_button.C | 14 +- src/insets/render_graphic.C | 13 +- src/insets/render_preview.C | 8 +- src/lyxlength.C | 2 +- src/mathed/InsetFormulaMacro.C | 11 +- src/mathed/InsetMathBig.C | 7 +- src/mathed/InsetMathChar.C | 6 +- src/mathed/MathMacroTemplate.C | 9 +- src/mathed/MathSupport.C | 50 ++--- src/mathed/MathSupport.h | 2 - src/rowpainter.C | 66 +++--- src/text.C | 101 ++++----- src/text2.C | 11 +- 45 files changed, 713 insertions(+), 533 deletions(-) rename src/frontends/{font_metrics.h => FontMetrics.h} (61%) create mode 100644 src/frontends/qt4/GuiFontMetrics.C create mode 100644 src/frontends/qt4/GuiFontMetrics.h delete mode 100644 src/frontends/qt4/qfont_metrics.C diff --git a/development/scons/scons_manifest.py b/development/scons/scons_manifest.py index 6edf247c94..1b0b5c058d 100644 --- a/development/scons/scons_manifest.py +++ b/development/scons/scons_manifest.py @@ -433,6 +433,7 @@ src_frontends_header_files = Split(''' Dialogs.h FileDialog.h FontLoader.h + FontMetrics.h Gui.h LyXKeySym.h LyXKeySymFactory.h @@ -443,7 +444,6 @@ src_frontends_header_files = Split(''' Timeout.h Toolbars.h WorkArea.h - font_metrics.h guiapi.h key_state.h lyx_gui.h @@ -1236,6 +1236,7 @@ src_frontends_qt4_files = Split(''' GuiApplication.C GuiClipboard.C GuiFontLoader.C + GuiFontMetrics.C GuiImplementation.C GuiSelection.C GuiView.C @@ -1335,7 +1336,6 @@ src_frontends_qt4_files = Split(''' lengthcombo.C lyx_gui.C panelstack.C - qfont_metrics.C qfontexample.C qsetborder.C qtTimeout.C diff --git a/src/BufferView.C b/src/BufferView.C index cea4268b0c..c91a92ddfb 100644 --- a/src/BufferView.C +++ b/src/BufferView.C @@ -62,7 +62,8 @@ #include "frontends/Alert.h" #include "frontends/Application.h" #include "frontends/FileDialog.h" -#include "frontends/font_metrics.h" +#include "frontends/FontLoader.h" +#include "frontends/FontMetrics.h" #include "graphics/Previews.h" @@ -330,9 +331,10 @@ void BufferView::resize() bool BufferView::fitCursor() { if (bv_funcs::status(this, cursor_) == bv_funcs::CUR_INSIDE) { - LyXFont const font = cursor_.getFont(); - int const asc = font_metrics::maxAscent(font); - int const des = font_metrics::maxDescent(font); + lyx::frontend::FontMetrics const & fm + = theApp->fontLoader().metrics(cursor_.getFont()); + int const asc = fm.maxAscent(); + int const des = fm.maxDescent(); Point const p = bv_funcs::getPos(cursor_, cursor_.boundary()); if (p.y_ - asc >= 0 && p.y_ + des < height_) return false; diff --git a/src/cursor.C b/src/cursor.C index a94fdc2800..c93d642390 100644 --- a/src/cursor.C +++ b/src/cursor.C @@ -44,7 +44,6 @@ #include "support/limited_stack.h" #include "frontends/Application.h" -#include "frontends/font_metrics.h" #include #include diff --git a/src/dimension.C b/src/dimension.C index c1bb595405..1edb794ebc 100644 --- a/src/dimension.C +++ b/src/dimension.C @@ -11,7 +11,9 @@ #include #include "dimension.h" -#include "frontends/font_metrics.h" +#include "frontends/Application.h" +#include "frontends/FontLoader.h" +#include "frontends/FontMetrics.h" void Dimension::operator+=(Dimension const & dim) @@ -26,7 +28,8 @@ void Dimension::operator+=(Dimension const & dim) void Dimension::clear(LyXFont const & font) { - asc = font_metrics::maxAscent(font); - des = font_metrics::maxDescent(font); + lyx::frontend::FontMetrics const & fm = theApp->fontLoader().metrics(font); + asc = fm.maxAscent(); + des = fm.maxDescent(); wid = 0; } diff --git a/src/frontends/FontLoader.h b/src/frontends/FontLoader.h index ea5cf7388b..e674a26ff1 100644 --- a/src/frontends/FontLoader.h +++ b/src/frontends/FontLoader.h @@ -17,6 +17,8 @@ class LyXFont; namespace lyx { namespace frontend { +class FontMetrics; + /// Hold info about a particular font class FontLoader { @@ -31,6 +33,9 @@ public: /// Is the given font available ? virtual bool available(LyXFont const & f) = 0; + + /// Get the Font metrics for this LyXFont + virtual FontMetrics const & metrics(LyXFont const & f) = 0; }; } // namespace frontend diff --git a/src/frontends/font_metrics.h b/src/frontends/FontMetrics.h similarity index 61% rename from src/frontends/font_metrics.h rename to src/frontends/FontMetrics.h index 9c6becdab9..9a190f4393 100644 --- a/src/frontends/font_metrics.h +++ b/src/frontends/FontMetrics.h @@ -1,11 +1,12 @@ // -*- C++ -*- /** - * \file font_metrics.h + * \file FontMetrics.h * This file is part of LyX, the document processor. * Licence details can be found in the file COPYING. * * \author unknown * \author John Levon + * \author Abdelrazak Younes * * Full author contact details are available in file CREDITS. */ @@ -15,11 +16,8 @@ #include "support/docstring.h" - -class LyXFont; - /** - * A namespace holding helper functions for determining + * A class holding helper functions for determining * the screen dimensions of fonts. * * The geometry is the standard typographical geometry, @@ -41,63 +39,79 @@ class LyXFont; * --------------+----------+------- -#include "Painter.h" -#include "font_metrics.h" +#include "frontends/Painter.h" + +#include "frontends/Application.h" +#include "frontends/FontLoader.h" +#include "frontends/FontMetrics.h" #include "LColor.h" #include "lyxfont.h" @@ -59,7 +62,8 @@ void Painter::rectText(int x, int y, int ascent; int descent; - font_metrics::rectText(str, font, width, ascent, descent); + FontMetrics const & fm = theApp->fontLoader().metrics(font); + fm.rectText(str, width, ascent, descent); if (back != LColor::none) fillRectangle(x + 1, y - ascent + 1, width - 1, @@ -78,7 +82,8 @@ void Painter::buttonText(int x, int y, docstring const & str, LyXFont const & fo int ascent; int descent; - font_metrics::buttonText(str, font, width, ascent, descent); + FontMetrics const & fm = theApp->fontLoader().metrics(font); + fm.buttonText(str, width, ascent, descent); button(x, y - ascent, width, descent + ascent); text(x + 4, y, str, font); @@ -87,8 +92,10 @@ void Painter::buttonText(int x, int y, docstring const & str, LyXFont const & fo void Painter::underline(LyXFont const & f, int x, int y, int width) { - int const below = max(font_metrics::maxDescent(f) / 2, 2); - int const height = max((font_metrics::maxDescent(f) / 4) - 1, 1); + FontMetrics const & fm = theApp->fontLoader().metrics(f); + + int const below = max(fm.maxDescent() / 2, 2); + int const height = max((fm.maxDescent() / 4) - 1, 1); if (height < 2) line(x, y + below, x + width, y + below, f.color()); diff --git a/src/frontends/WorkArea.C b/src/frontends/WorkArea.C index aa4089a26c..0f6d88d689 100644 --- a/src/frontends/WorkArea.C +++ b/src/frontends/WorkArea.C @@ -13,9 +13,12 @@ #include -#include "WorkArea.h" +#include "frontends/WorkArea.h" + +#include "frontends/Application.h" +#include "frontends/FontLoader.h" +#include "frontends/FontMetrics.h" -#include "font_metrics.h" #include "funcrequest.h" #include "lyx_gui.h" #include "lyxfunc.h" @@ -372,8 +375,9 @@ void WorkArea::showCursor() shape = BAR_SHAPE; LyXFont const font = buffer_view_->cursor().getFont(); - int const asc = font_metrics::maxAscent(font); - int const des = font_metrics::maxDescent(font); + FontMetrics const & fm = theApp->fontLoader().metrics(font); + int const asc = fm.maxAscent(); + int const des = fm.maxDescent(); int h = asc + des; int x = 0; int y = 0; diff --git a/src/frontends/qt4/GuiFontLoader.C b/src/frontends/qt4/GuiFontLoader.C index e75bbad849..f98c4a7324 100644 --- a/src/frontends/qt4/GuiFontLoader.C +++ b/src/frontends/qt4/GuiFontLoader.C @@ -220,9 +220,7 @@ void GuiFontLoader::update() QLFontInfo::QLFontInfo(LyXFont const & f) - : metrics(font) { - string const pat = symbolFamily(f.family()); if (!pat.empty()) { bool tmp; @@ -246,9 +244,6 @@ QLFontInfo::QLFontInfo(LyXFont const & f) } } - font.setPointSizeF(convert(lyxrc.font_sizes[f.size()]) - * lyxrc.zoom / 100.0); - switch (f.series()) { case LyXFont::MEDIUM_SERIES: font.setWeight(QFont::Normal); @@ -274,9 +269,6 @@ QLFontInfo::QLFontInfo(LyXFont const & f) << "' matched by\n" << fromqstr(font.rawName()) << endl; } - lyxerr[Debug::FONT] << "The font has size: " - << font.pointSizeF() << endl; - // Is this an exact match? if (font.exactMatch()) lyxerr[Debug::FONT] << "This font is an exact match" << endl; @@ -286,21 +278,27 @@ QLFontInfo::QLFontInfo(LyXFont const & f) lyxerr[Debug::FONT] << "XFLD: " << fromqstr(font.rawName()) << endl; - metrics = QFontMetrics(font); -} + font.setPointSizeF(convert(lyxrc.font_sizes[f.size()]) + * lyxrc.zoom / 100.0); -#ifdef USE_LYX_FONTCACHE -int QLFontInfo::width(Uchar val) -{ - QLFontInfo::WidthCache::const_iterator cit = widthcache.find(val); - if (cit != widthcache.end()) - return cit->second; + lyxerr[Debug::FONT] << "The font has size: " + << font.pointSizeF() << endl; + + if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) { + metrics.reset(new GuiFontMetrics(font)); + } + else { + // handle small caps ourselves ... + LyXFont smallfont = f; + smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE); + QFont font2(font); + font2.setPointSizeF(convert(lyxrc.font_sizes[smallfont.size()]) + * lyxrc.zoom / 100.0); + + metrics.reset(new GuiFontMetrics(font, font2)); + } - int const w = metrics.width(QChar(val)); - widthcache[val] = w; - return w; } -#endif bool GuiFontLoader::available(LyXFont const & f) diff --git a/src/frontends/qt4/GuiFontLoader.h b/src/frontends/qt4/GuiFontLoader.h index d526ff3ef1..71d10b26d2 100644 --- a/src/frontends/qt4/GuiFontLoader.h +++ b/src/frontends/qt4/GuiFontLoader.h @@ -14,18 +14,12 @@ #include "frontends/FontLoader.h" +#include "GuiFontMetrics.h" + #include "encoding.h" #include "lyxfont.h" #include -#include - -// Starting with version 3.1.0, Qt/X11 does its own caching of -// character width, so it is not necessary to provide ours. -#if defined(Q_WS_MACX) || defined(Q_WS_WIN32) -#define USE_LYX_FONTCACHE -#include -#endif namespace lyx { namespace frontend { @@ -41,21 +35,7 @@ public: /// The font instance QFont font; /// Metrics on the font - QFontMetrics metrics; - -#ifndef USE_LYX_FONTCACHE - /// Return pixel width for the given unicode char - int width(Uchar val) { return metrics.width(QChar(val)); } - -#else - /// Return pixel width for the given unicode char - int width(Uchar val); - -private: - typedef std::map WidthCache; - /// Cache of char widths - WidthCache widthcache; -#endif // USE_LYX_FONTCACHE + boost::scoped_ptr metrics; }; @@ -69,21 +49,17 @@ public: /// Destructor virtual ~GuiFontLoader(); - /// Update fonts after zoom, dpi, font names, or norm change virtual void update(); - - /// Do we have anything matching? virtual bool available(LyXFont const & f); + inline virtual FontMetrics const & metrics(LyXFont const & f) { + return *fontinfo(f).metrics.get(); + } /// Get the QFont for this LyXFont QFont const & get(LyXFont const & f) { return fontinfo(f).font; } - /// Get the QFont metrics for this LyXFont - QFontMetrics const & metrics(LyXFont const & f) { - return fontinfo(f).metrics; - } /// Get font info (font + metrics) for the given LyX font. QLFontInfo & fontinfo(LyXFont const & f) { diff --git a/src/frontends/qt4/GuiFontMetrics.C b/src/frontends/qt4/GuiFontMetrics.C new file mode 100644 index 0000000000..b5a2b0a3d6 --- /dev/null +++ b/src/frontends/qt4/GuiFontMetrics.C @@ -0,0 +1,200 @@ +/** + * \file GuiFontMetrics.C + * This file is part of LyX, the document processor. + * Licence details can be found in the file COPYING. + * + * \author unknown + * \author John Levon + * + * Full author contact details are available in file CREDITS. + */ + +#include + +#include "GuiFontMetrics.h" + +#include "qt_helpers.h" + +#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) +{ +} + + +GuiFontMetrics::GuiFontMetrics(QFont const & font, QFont const & smallcaps_font) +: metrics_(font), smallcaps_metrics_(smallcaps_font), smallcaps_shape_(true) +{ +} + + +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); +} + + +int GuiFontMetrics::smallcapsWidth(QString const & s) const +{ + if (!lyx_gui::use_gui) + return 1; + + int w = 0; + int const ls = s.size(); + + for (int i = 0; i < ls; ++i) { + QChar const & c = s[i]; + QChar const uc = c.toUpper(); + if (c != uc) + w += smallcaps_metrics_.width(uc); + else + w += metrics_.width(c); + } + return w; +} + + +int GuiFontMetrics::width(char_type const * s, size_t ls) const +{ + if (!lyx_gui::use_gui) + return ls; + + QString ucs2 = ucs4_to_qstring(s, ls); + + if (smallcaps_shape_) + return smallcapsWidth(ucs2); + + if (ls == 1) + return width(ucs2[0].unicode()); + + int w = 0; + for (unsigned int i = 0; i < ls; ++i) + w += width(ucs2[i].unicode()); + + return w; +} + + +int GuiFontMetrics::signedWidth(docstring const & s) const +{ + if (s[0] == '-') + return -FontMetrics::width(s.substr(1, s.length() - 1)); + else + return FontMetrics::width(s); +} + + +void GuiFontMetrics::rectText(docstring const & str, + int & w, int & ascent, int & descent) const +{ + static int const d = 2; + w = FontMetrics::width(str) + d * 2 + 2; + ascent = metrics_.ascent() + d; + descent = metrics_.descent() + d; +} + + + +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 +{ + GuiFontMetrics::WidthCache::const_iterator cit = widthcache.find(val); + if (cit != widthcache.end()) + return cit->second; + + int const w = metrics_.width(QChar(val)); + widthcache[val] = w; + return w; +} +#endif + +} +} diff --git a/src/frontends/qt4/GuiFontMetrics.h b/src/frontends/qt4/GuiFontMetrics.h new file mode 100644 index 0000000000..44ee9c3820 --- /dev/null +++ b/src/frontends/qt4/GuiFontMetrics.h @@ -0,0 +1,83 @@ +// -*- C++ -*- +/** + * \file FontMetrics.h + * This file is part of LyX, the document processor. + * Licence details can be found in the file COPYING. + * + * \author Abdelrazak Younes + * + * Full author contact details are available in file CREDITS. + */ + +#ifndef QT4_FONT_METRICS_H +#define QT4_FONT_METRICS_H + +#include "frontends/FontMetrics.h" + +#include "support/docstring.h" + +#include + +// Starting with version 3.1.0, Qt/X11 does its own caching of +// character width, so it is not necessary to provide ours. +#if defined(Q_WS_MACX) || defined(Q_WS_WIN32) +#define USE_LYX_FONTCACHE +#include +#endif + +namespace lyx { +namespace frontend { + +class GuiFontMetrics: public FontMetrics +{ +public: + + GuiFontMetrics(QFont const & font); + GuiFontMetrics(QFont const & font, QFont const & smallcaps_font); + + virtual ~GuiFontMetrics() {} + + virtual int maxAscent() const; + virtual int maxDescent() const; + virtual int ascent(lyx::char_type c) const; + int descent(lyx::char_type c) const; + virtual int lbearing(lyx::char_type c) const; + virtual int rbearing(lyx::char_type c) const; + virtual int width(lyx::char_type const * s, size_t n) const; + virtual int signedWidth(lyx::docstring const & s) const; + virtual void rectText(lyx::docstring const & str, + int & width, + int & ascent, + int & descent) const; + virtual void buttonText(lyx::docstring const & str, + int & width, + int & ascent, + int & descent) const; + +private: + int smallcapsWidth(QString const & s) const; + + /// Metrics on the font + QFontMetrics metrics_; + QFontMetrics smallcaps_metrics_; + + bool smallcaps_shape_; + +#ifndef USE_LYX_FONTCACHE + /// Return pixel width for the given unicode char + int width(unsigned short val) const { return metrics_.width(QChar(val)); } + +#else + /// Return pixel width for the given unicode char + int width(unsigned short val) const; + + typedef std::map WidthCache; + /// Cache of char widths + mutable WidthCache widthcache; +#endif // USE_LYX_FONTCACHE +}; + +} // namespace frontend +} // namespace lyx + +#endif // QT4_FONT_METRICS_H diff --git a/src/frontends/qt4/Makefile.am b/src/frontends/qt4/Makefile.am index c20a0970ab..650ff7b343 100644 --- a/src/frontends/qt4/Makefile.am +++ b/src/frontends/qt4/Makefile.am @@ -37,6 +37,7 @@ libqt4_la_SOURCES = \ GuiApplication.C GuiApplication.h \ GuiClipboard.h GuiClipboard.C \ GuiFontLoader.h GuiFontLoader.C \ + GuiFontMetrics.h GuiFontMetrics.C \ GuiSelection.h GuiSelection.C \ GuiImplementation.h GuiImplementation.C \ LyXKeySymFactory.C \ @@ -86,7 +87,6 @@ libqt4_la_SOURCES = \ lyx_gui.C \ panelstack.h panelstack.C \ qfontexample.h qfontexample.C \ - qfont_metrics.C \ qlkey.h \ qt_helpers.h qt_helpers.C \ qtTimeout.C qtTimeout.h \ diff --git a/src/frontends/qt4/QLPainter.C b/src/frontends/qt4/QLPainter.C index f306ebc330..3fcfba0ee1 100644 --- a/src/frontends/qt4/QLPainter.C +++ b/src/frontends/qt4/QLPainter.C @@ -25,7 +25,7 @@ #include "language.h" #include "LColor.h" -#include "frontends/font_metrics.h" +#include "frontends/FontMetrics.h" #include "support/unicode.h" @@ -196,7 +196,7 @@ void QLPainter::text(int x, int y, char_type c, LyXFont const & f) } -void QLPainter::smallCapsText(int x, int y, +int QLPainter::smallCapsText(int x, int y, QString const & s, LyXFont const & f) { LyXFont smallfont(f); @@ -204,24 +204,21 @@ void QLPainter::smallCapsText(int x, int y, QFont const & qfont = guiApp->guiFontLoader().get(f); QFont const & qsmallfont = guiApp->guiFontLoader().get(smallfont); - QFontMetrics const & qfontm = QFontMetrics(qfont); - QFontMetrics const & qsmallfontm = QFontMetrics(qsmallfont); setQPainterPen(f.realColor()); - int tmpx = x; + int textwidth = 0; size_t ls = s.length(); for (unsigned int i = 0; i < ls; ++i) { QChar const c = s[i].toUpper(); if (c != s.at(i)) { qp_->setFont(qsmallfont); - qp_->drawText(tmpx, y, c); - tmpx += qsmallfontm.width(c); } else { qp_->setFont(qfont); - qp_->drawText(tmpx, y, c); - tmpx += qfontm.width(c); } + qp_->drawText(x + textwidth, y, c); + textwidth += qp_->fontMetrics().width(c); } + return textwidth; } @@ -250,18 +247,23 @@ void QLPainter::text(int x, int y, char_type const * s, size_t ls, str = ' ' + str; #endif + QLFontInfo & fi = guiApp->guiFontLoader().fontinfo(f); + + int textwidth; + if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) { setQPainterPen(f.realColor()); - qp_->setFont(guiApp->guiFontLoader().get(f)); + qp_->setFont(fi.font); // We need to draw the text as LTR as we use our own bidi code. qp_->setLayoutDirection(Qt::LeftToRight); qp_->drawText(x, y, str); + textwidth = qp_->fontMetrics().width(str); } else { - smallCapsText(x, y, str, f); + textwidth = smallCapsText(x, y, str, f); } if (f.underbar() == LyXFont::ON) { - underline(f, x, y, font_metrics::width(s, ls, f)); + underline(f, x, y, textwidth); } } diff --git a/src/frontends/qt4/QLPainter.h b/src/frontends/qt4/QLPainter.h index 291b337f34..f53b0e67c9 100644 --- a/src/frontends/qt4/QLPainter.h +++ b/src/frontends/qt4/QLPainter.h @@ -125,7 +125,10 @@ public: private: /// draw small caps text - void smallCapsText(int x, int y, + /** + \return width of the drawn text. + */ + int smallCapsText(int x, int y, QString const & str, LyXFont const & f); /// set pen parameters diff --git a/src/frontends/qt4/qfont_metrics.C b/src/frontends/qt4/qfont_metrics.C deleted file mode 100644 index 00f924c011..0000000000 --- a/src/frontends/qt4/qfont_metrics.C +++ /dev/null @@ -1,186 +0,0 @@ -/** - * \file qfont_metrics.C - * This file is part of LyX, the document processor. - * Licence details can be found in the file COPYING. - * - * \author unknown - * \author John Levon - * - * Full author contact details are available in file CREDITS. - */ - -#include - -#include "frontends/font_metrics.h" -#include "frontends/lyx_gui.h" - -#include "GuiApplication.h" -#include "FontLoader.h" -#include "qt_helpers.h" - -#include "language.h" - -#include "support/unicode.h" - -using lyx::char_type; -using lyx::docstring; - -using std::string; - - -namespace { - -int smallcapswidth(QString const & s, LyXFont const & f) -{ - if (!lyx_gui::use_gui) - return 1; - // handle small caps ourselves ... - - LyXFont smallfont = f; - smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE); - - QFontMetrics const & qm = guiApp->guiFontLoader().metrics(f); - QFontMetrics const & qsmallm = guiApp->guiFontLoader().metrics(smallfont); - - int w = 0; - - int const ls = s.size(); - - for (int i = 0; i < ls; ++i) { - QChar const & c = s[i]; - QChar const uc = c.toUpper(); - if (c != uc) - w += qsmallm.width(uc); - else - w += qm.width(c); - } - return w; -} - - -} // anon namespace - - -int font_metrics::maxAscent(LyXFont const & f) -{ - if (!lyx_gui::use_gui) - return 1; - return guiApp->guiFontLoader().metrics(f).ascent(); -} - - -int font_metrics::maxDescent(LyXFont const & f) -{ - 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 guiApp->guiFontLoader().metrics(f).descent() + 1; -} - - -int font_metrics::ascent(char_type c, LyXFont const & f) -{ - if (!lyx_gui::use_gui) - return 1; - QRect const & r = guiApp->guiFontLoader().metrics(f).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 font_metrics::descent(char_type c, LyXFont const & f) -{ - if (!lyx_gui::use_gui) - return 1; - QRect const & r = guiApp->guiFontLoader().metrics(f).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 font_metrics::lbearing(char_type c, LyXFont const & f) -{ - if (!lyx_gui::use_gui) - return 1; - return guiApp->guiFontLoader().metrics(f).leftBearing(ucs4_to_qchar(c)); -} - - -int font_metrics::rbearing(char_type c, LyXFont const & f) -{ - if (!lyx_gui::use_gui) - return 1; - QFontMetrics const & m = guiApp->guiFontLoader().metrics(f); - - // Qt rbearing is from the right edge of the char's width(). - QChar sc = ucs4_to_qchar(c); - return m.width(sc) - m.rightBearing(sc); -} - - -int font_metrics::width(char_type const * s, size_t ls, LyXFont const & f) -{ - if (!lyx_gui::use_gui) - return ls; - - QString ucs2 = ucs4_to_qstring(s, ls); - - if (f.realShape() == LyXFont::SMALLCAPS_SHAPE) - return smallcapswidth(ucs2, f); - - lyx::frontend::QLFontInfo & fi = guiApp->guiFontLoader().fontinfo(f); - - if (ls == 1) - return fi.width(ucs2[0].unicode()); - - int w = 0; - for (unsigned int i = 0; i < ls; ++i) - w += fi.width(ucs2[i].unicode()); - - return w; -} - - -int font_metrics::signedWidth(docstring const & s, LyXFont const & f) -{ - if (s[0] == '-') - return -width(s.substr(1, s.length() - 1), f); - else - return width(s, f); -} - - -void font_metrics::rectText(docstring const & str, LyXFont const & f, - int & w, int & ascent, int & descent) -{ - QFontMetrics const & m = guiApp->guiFontLoader().metrics(f); - static int const d = 2; - w = width(str, f) + d * 2 + 2; - ascent = m.ascent() + d; - descent = m.descent() + d; -} - - - -void font_metrics::buttonText(docstring const & str, LyXFont const & f, - int & w, int & ascent, int & descent) -{ - QFontMetrics const & m = guiApp->guiFontLoader().metrics(f); - static int const d = 3; - w = width(str, f) + d * 2 + 2; - ascent = m.ascent() + d; - descent = m.descent() + d; -} diff --git a/src/frontends/qt4/qt_helpers.C b/src/frontends/qt4/qt_helpers.C index 3f419624f3..394c13341d 100644 --- a/src/frontends/qt4/qt_helpers.C +++ b/src/frontends/qt4/qt_helpers.C @@ -123,6 +123,7 @@ QString const toqstr(string const & str) QString const ucs4_to_qstring(char_type const * str, size_t ls) { QString s; + s.reserve(ls); for (size_t i = 0; i < ls; ++i) s.append(ucs4_to_qchar(str[i])); diff --git a/src/insets/insetbibitem.C b/src/insets/insetbibitem.C index ca11c02129..4cb361ca18 100644 --- a/src/insets/insetbibitem.C +++ b/src/insets/insetbibitem.C @@ -20,7 +20,9 @@ #include "paragraph.h" #include "ParagraphList.h" -#include "frontends/font_metrics.h" +#include "frontends/Application.h" +#include "frontends/FontLoader.h" +#include "frontends/FontMetrics.h" #include "support/lstrings.h" #include "support/std_ostream.h" @@ -136,7 +138,10 @@ string const bibitemWidest(Buffer const & buffer) // Does look like a hack? It is! (but will change at 0.13) InsetBibitem const * bitem = 0; + // FIXME font is used unitialized, is that correct? LyXFont font; + lyx::frontend::FontMetrics const & fm = + theApp->fontLoader().metrics(font); ParagraphList::const_iterator it = buffer.paragraphs().begin(); ParagraphList::const_iterator end = buffer.paragraphs().end(); @@ -147,7 +152,7 @@ string const bibitemWidest(Buffer const & buffer) docstring const dlab(label.begin(), label.end()); int const wx = - font_metrics::width(dlab, font); + fm.width(dlab); if (wx > w) { w = wx; bitem = it->bibitem(); diff --git a/src/insets/insetcaption.C b/src/insets/insetcaption.C index 4a34e60fdc..6c6c1755b4 100644 --- a/src/insets/insetcaption.C +++ b/src/insets/insetcaption.C @@ -26,7 +26,9 @@ #include "metricsinfo.h" #include "paragraph.h" -#include "frontends/font_metrics.h" +#include "frontends/Application.h" +#include "frontends/FontLoader.h" +#include "frontends/FontMetrics.h" #include "frontends/Painter.h" #include "support/lstrings.h" @@ -125,7 +127,7 @@ void InsetCaption::metrics(MetricsInfo & mi, Dimension & dim) const LCursor cur = mi.base.bv->cursor(); setLabel(cur); docstring dlab(label.begin(), label.end()); - labelwidth_ = font_metrics::width(dlab, mi.base.font); + labelwidth_ = theApp->fontLoader().metrics(mi.base.font).width(dlab); dim.wid = labelwidth_; Dimension textdim; InsetText::metrics(mi, textdim); @@ -153,7 +155,9 @@ void InsetCaption::draw(PainterInfo & pi, int x, int y) const LCursor cur = pi.base.bv->cursor(); setLabel(cur); docstring dlab(label.begin(), label.end()); - labelwidth_ = font_metrics::width(dlab, pi.base.font); + // FXIME: instead of using the fontLoader metrics, we should make + // painter::text() returns the drawn text witdh. + labelwidth_ = theApp->fontLoader().metrics(pi.base.font).width(dlab); pi.pain.text(x, y, dlab, pi.base.font); InsetText::draw(pi, x + labelwidth_, y); setPosCache(pi, x, y); diff --git a/src/insets/insetcharstyle.C b/src/insets/insetcharstyle.C index 853b02b3ca..77bb8698db 100644 --- a/src/insets/insetcharstyle.C +++ b/src/insets/insetcharstyle.C @@ -31,7 +31,9 @@ #include "paragraph_funcs.h" #include "sgml.h" -#include "frontends/font_metrics.h" +#include "frontends/Application.h" +#include "frontends/FontLoader.h" +#include "frontends/FontMetrics.h" #include "frontends/Painter.h" #include @@ -157,7 +159,7 @@ void InsetCharStyle::metrics(MetricsInfo & mi, Dimension & dim) const // FIXME UNICODE s = lyx::to_utf8(_("Undef: ")) + s; docstring ds(s.begin(), s.end()); - font_metrics::rectText(ds, font, w, a, d); + theApp->fontLoader().metrics(font).rectText(ds, w, a, d); dim.wid = max(dim.wid, w); } dim.asc += TEXT_TO_INSET_OFFSET; @@ -205,7 +207,7 @@ void InsetCharStyle::draw(PainterInfo & pi, int x, int y) const // FIXME UNICODE s = lyx::to_utf8(_("Undef: ")) + s; docstring ds(s.begin(), s.end()); - font_metrics::rectText(ds, font, w, a, d); + theApp->fontLoader().metrics(font).rectText(ds, w, a, d); pi.pain.rectText(x + (dim_.wid - w) / 2, y + desc + a, ds, font, LColor::none, LColor::none); } diff --git a/src/insets/insetcollapsable.C b/src/insets/insetcollapsable.C index ad2fbea91d..e7a547f475 100644 --- a/src/insets/insetcollapsable.C +++ b/src/insets/insetcollapsable.C @@ -26,7 +26,9 @@ #include "metricsinfo.h" #include "paragraph.h" -#include "frontends/font_metrics.h" +#include "frontends/Application.h" +#include "frontends/FontLoader.h" +#include "frontends/FontMetrics.h" #include "frontends/Painter.h" using lyx::docstring; @@ -121,8 +123,9 @@ void InsetCollapsable::read(Buffer const & buf, LyXLex & lex) Dimension InsetCollapsable::dimensionCollapsed() const { Dimension dim; - docstring dlab(label.begin(), label.end()); - font_metrics::buttonText(dlab, labelfont_, dim.wid, dim.asc, dim.des); + docstring dlab(label.begin(), label.end()); + theApp->fontLoader().metrics(labelfont_).buttonText( + dlab, dim.wid, dim.asc, dim.des); return dim; } diff --git a/src/insets/insetlatexaccent.C b/src/insets/insetlatexaccent.C index c880b967a2..9f15861be3 100644 --- a/src/insets/insetlatexaccent.C +++ b/src/insets/insetlatexaccent.C @@ -19,7 +19,9 @@ #include "lyxrc.h" #include "metricsinfo.h" -#include "frontends/font_metrics.h" +#include "frontends/Application.h" +#include "frontends/FontLoader.h" +#include "frontends/FontMetrics.h" #include "frontends/Painter.h" #include "support/lstrings.h" @@ -244,48 +246,39 @@ void InsetLatexAccent::checkContents() void InsetLatexAccent::metrics(MetricsInfo & mi, Dimension & dim) const { LyXFont & font = mi.base.font; + lyx::frontend::FontMetrics const & fm = + theApp->fontLoader().metrics(font); + // This function is a bit too simplistic and is just a // "try to make a fit for all accents" approach, to // make it better we need to know what kind of accent is // used and add to max based on that. if (candisp) { if (ic == ' ') - dim.asc = font_metrics::ascent('a', font); + dim.asc = fm.ascent('a'); else - dim.asc = font_metrics::ascent(ic, font); + dim.asc = fm.ascent(ic); if (plusasc) - dim.asc += (font_metrics::maxAscent(font) + 3) / 3; + dim.asc += (fm.maxAscent() + 3) / 3; if (ic == ' ') - dim.des = font_metrics::descent('a', font); + dim.des = fm.descent('a'); else - dim.des = font_metrics::descent(ic, font); + dim.des = fm.descent(ic); if (plusdesc) dim.des += 3; - dim.wid = font_metrics::width(ic, font); + dim.wid = fm.width(ic); } else { - dim.asc = font_metrics::maxAscent(font) + 4; - dim.des = font_metrics::maxDescent(font) + 4; + dim.asc = fm.maxAscent() + 4; + dim.des = fm.maxDescent() + 4; docstring dcon(contents.begin(), contents.end()); - dim.wid = font_metrics::width(dcon, font) + 4; + dim.wid = fm.width(dcon) + 4; } dim_ = dim; } -int InsetLatexAccent::lbearing(LyXFont const & font) const -{ - return font_metrics::lbearing(ic, font); -} - - -int InsetLatexAccent::rbearing(LyXFont const & font) const -{ - return font_metrics::rbearing(ic, font); -} - - bool InsetLatexAccent::displayISO8859_9(PainterInfo & pi, int x, int y) const { unsigned char tmpic = ic; @@ -338,10 +331,13 @@ void InsetLatexAccent::drawAccent(PainterInfo const & pi, int x, int y, char_type accent) const { LyXFont const & font = pi.base.font; - x -= font_metrics::center(accent, font); - y -= font_metrics::ascent(ic, font); - y -= font_metrics::descent(accent, font); - y -= font_metrics::height(accent, font) / 2; + lyx::frontend::FontMetrics const & fm = + theApp->fontLoader().metrics(font); + + x -= fm.center(accent); + y -= fm.ascent(ic); + y -= fm.descent(accent); + y -= fm.height(accent) / 2; pi.pain.text(x, y, accent, font); } @@ -360,13 +356,16 @@ void InsetLatexAccent::draw(PainterInfo & pi, int x, int baseline) const if (lyxrc.font_norm_type == LyXRC::ISO_10646_1) font.setLanguage(english_language); + lyx::frontend::FontMetrics const & fm = + theApp->fontLoader().metrics(font); + if (candisp) { - int x2 = int(x + (rbearing(font) - lbearing(font)) / 2); + int x2 = int(x + (fm.rbearing(ic) - fm.lbearing(ic)) / 2); int hg; int y; if (plusasc) { // mark at the top - hg = font_metrics::maxDescent(font); + hg = fm.maxDescent(); y = baseline - dim_.asc; if (font.shape() == LyXFont::ITALIC_SHAPE) x2 += int(0.8 * hg); // italic @@ -383,15 +382,15 @@ void InsetLatexAccent::draw(PainterInfo & pi, int x, int baseline) const pi.pain.text(x, baseline, ic, font); if (remdot) { - int tmpvar = baseline - font_metrics::ascent('i', font); + int tmpvar = baseline - fm.ascent('i'); int tmpx = 0; if (font.shape() == LyXFont::ITALIC_SHAPE) tmpx += int(0.8 * hg); // italic lyxerr[Debug::KEY] << "Removing dot." << endl; // remove the dot first pi.pain.fillRectangle(x + tmpx, tmpvar, dim_.wid, - font_metrics::ascent('i', pi.base.font) - - font_metrics::ascent('x', pi.base.font) - 1, + fm.ascent('i') - + fm.ascent('x') - 1, backgroundColor()); // the five lines below is a simple hack to // make the display of accent 'i' and 'j' @@ -430,21 +429,21 @@ void InsetLatexAccent::draw(PainterInfo & pi, int x, int baseline) const case UNDERBAR: { char_type const underbar = 0x5F; //('\x5F'); - pi.pain.text(x2 - font_metrics::center(underbar, font), + pi.pain.text(x2 - fm.center(underbar), baseline, underbar, font); break; } case CEDILLA: { char_type const cedilla = 0xB8; //('\xB8'); - pi.pain.text(x2 - font_metrics::center(cedilla, font), + pi.pain.text(x2 - fm.center(cedilla), baseline, cedilla, font); break; } case UNDERDOT: - pi.pain.text(x2 - font_metrics::center('.', font), - int(baseline + 1.5 * font_metrics::height('.', font)), + pi.pain.text(x2 - fm.center('.'), + int(baseline + 1.5 * fm.height('.')), '.', font); break; @@ -496,8 +495,8 @@ void InsetLatexAccent::draw(PainterInfo & pi, int x, int baseline) const } case HUNGARIAN_UMLAUT: - drawAccent(pi, x2 - font_metrics::center('´', font), baseline, '´'); - drawAccent(pi, x2 + font_metrics::center('´', font), baseline, '´'); + drawAccent(pi, x2 - fm.center('´'), baseline, '´'); + drawAccent(pi, x2 + fm.center('´'), baseline, '´'); break; case UMLAUT: diff --git a/src/insets/insetlatexaccent.h b/src/insets/insetlatexaccent.h index 6e7e0ca234..9d270e1bcf 100644 --- a/src/insets/insetlatexaccent.h +++ b/src/insets/insetlatexaccent.h @@ -37,10 +37,6 @@ public: /// void draw(PainterInfo & pi, int x, int y) const; /// - int lbearing(LyXFont const & font) const; - /// - int rbearing(LyXFont const & font) const; - /// bool displayISO8859_9(PainterInfo & pi, int x, int y) const; /// void write(Buffer const &, std::ostream &) const; diff --git a/src/insets/insetnewline.C b/src/insets/insetnewline.C index 403ba0d707..e9fa97b89e 100644 --- a/src/insets/insetnewline.C +++ b/src/insets/insetnewline.C @@ -19,7 +19,9 @@ #include "paragraph.h" #include "paragraph_funcs.h" -#include "frontends/font_metrics.h" +#include "frontends/Application.h" +#include "frontends/FontLoader.h" +#include "frontends/FontMetrics.h" #include "frontends/Painter.h" using std::endl; @@ -40,10 +42,11 @@ void InsetNewline::write(Buffer const &, ostream & os) const void InsetNewline::metrics(MetricsInfo & mi, Dimension & dim) const { - LyXFont & font = mi.base.font; - dim.asc = font_metrics::maxAscent(font); - dim.des = font_metrics::maxDescent(font); - dim.wid = font_metrics::width('n', font); + lyx::frontend::FontMetrics const & fm = + theApp->fontLoader().metrics(mi.base.font); + dim.asc = fm.maxAscent(); + dim.des = fm.maxDescent(); + dim.wid = fm.width('n'); dim_ = dim; } @@ -74,8 +77,10 @@ int InsetNewline::docbook(Buffer const &, std::ostream & os, void InsetNewline::draw(PainterInfo & pi, int x, int y) const { - int const wid = font_metrics::width('n', pi.base.font); - int const asc = font_metrics::maxAscent(pi.base.font); + lyx::frontend::FontMetrics const & fm = + theApp->fontLoader().metrics(pi.base.font); + int const wid = fm.width('n'); + int const asc = fm.maxAscent(); int xp[3]; int yp[3]; diff --git a/src/insets/insetpagebreak.C b/src/insets/insetpagebreak.C index 7c4b3d56e0..037e9b7293 100644 --- a/src/insets/insetpagebreak.C +++ b/src/insets/insetpagebreak.C @@ -18,8 +18,10 @@ #include "metricsinfo.h" #include "gettext.h" +#include "frontends/Application.h" +#include "frontends/FontLoader.h" +#include "frontends/FontMetrics.h" #include "frontends/Painter.h" -#include "frontends/font_metrics.h" using lyx::docstring; using lyx::frontend::Painter; @@ -60,7 +62,7 @@ void InsetPagebreak::draw(PainterInfo & pi, int x, int y) const int w = 0; int a = 0; int d = 0; - font_metrics::rectText(label, font, w, a, d); + theApp->fontLoader().metrics(font).rectText(label, w, a, d); int const text_start = int(x + (dim_.wid - w) / 2); int const text_end = text_start + w; diff --git a/src/insets/insetquotes.C b/src/insets/insetquotes.C index 7d9d4edddc..26f5e53916 100644 --- a/src/insets/insetquotes.C +++ b/src/insets/insetquotes.C @@ -24,7 +24,9 @@ #include "paragraph.h" #include "paragraph_funcs.h" -#include "frontends/font_metrics.h" +#include "frontends/Application.h" +#include "frontends/FontLoader.h" +#include "frontends/FontMetrics.h" #include "frontends/Painter.h" #include "support/lstrings.h" @@ -199,18 +201,20 @@ string const InsetQuotes::dispString(Language const * loclang) const void InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const { LyXFont & font = mi.base.font; - dim.asc = font_metrics::maxAscent(font); - dim.des = font_metrics::maxDescent(font); + lyx::frontend::FontMetrics const & fm = + theApp->fontLoader().metrics(font); + dim.asc = fm.maxAscent(); + dim.des = fm.maxDescent(); dim.wid = 0; string const text = dispString(font.language()); for (string::size_type i = 0; i < text.length(); ++i) { if (text[i] == ' ') - dim.wid += font_metrics::width('i', font); + dim.wid += fm.width('i'); else if (i == 0 || text[i] != text[i - 1]) - dim.wid += font_metrics::width(text[i], font); + dim.wid += fm.width(text[i]); else - dim.wid += font_metrics::width(',', font); + dim.wid += fm.width(','); } dim_ = dim; } @@ -235,7 +239,8 @@ void InsetQuotes::draw(PainterInfo & pi, int x, int y) const if (text.length() == 2 && text[0] == text[1]) { pi.pain.text(x, y, text[0], pi.base.font); - int const t = font_metrics::width(',', pi.base.font); + int const t = theApp->fontLoader().metrics(pi.base.font) + .width(','); pi.pain.text(x + t, y, text[0], pi.base.font); } else { docstring dtext(text.begin(), text.end()); diff --git a/src/insets/insetspace.C b/src/insets/insetspace.C index c97a3ebb08..9be6ee484a 100644 --- a/src/insets/insetspace.C +++ b/src/insets/insetspace.C @@ -21,7 +21,9 @@ #include "metricsinfo.h" #include "outputparams.h" -#include "frontends/font_metrics.h" +#include "frontends/Application.h" +#include "frontends/FontLoader.h" +#include "frontends/FontMetrics.h" #include "frontends/Painter.h" @@ -48,18 +50,19 @@ InsetSpace::Kind InsetSpace::kind() const void InsetSpace::metrics(MetricsInfo & mi, Dimension & dim) const { - LyXFont & font = mi.base.font; - dim.asc = font_metrics::maxAscent(font); - dim.des = font_metrics::maxDescent(font); + lyx::frontend::FontMetrics const & fm = + theApp->fontLoader().metrics(mi.base.font); + dim.asc = fm.maxAscent(); + dim.des = fm.maxDescent(); switch (kind_) { case THIN: case NEGTHIN: - dim.wid = font_metrics::width(lyx::char_type('x'), font) / 3; + dim.wid = fm.width(lyx::char_type('x')) / 3; break; case PROTECTED: case NORMAL: - dim.wid = font_metrics::width(lyx::char_type('x'), font); + dim.wid = fm.width(lyx::char_type('x')); break; case QUAD: dim.wid = 20; @@ -79,7 +82,8 @@ void InsetSpace::metrics(MetricsInfo & mi, Dimension & dim) const void InsetSpace::draw(PainterInfo & pi, int x, int y) const { int const w = width(); - int const h = font_metrics::ascent('x', pi.base.font); + int const h = theApp->fontLoader().metrics(pi.base.font) + .ascent('x'); int xp[4], yp[4]; xp[0] = x; diff --git a/src/insets/insetspecialchar.C b/src/insets/insetspecialchar.C index 665c0dc923..edf1053557 100644 --- a/src/insets/insetspecialchar.C +++ b/src/insets/insetspecialchar.C @@ -20,7 +20,9 @@ #include "lyxlex.h" #include "metricsinfo.h" -#include "frontends/font_metrics.h" +#include "frontends/Application.h" +#include "frontends/FontLoader.h" +#include "frontends/FontMetrics.h" #include "frontends/Painter.h" using lyx::docstring; @@ -43,9 +45,10 @@ InsetSpecialChar::Kind InsetSpecialChar::kind() const void InsetSpecialChar::metrics(MetricsInfo & mi, Dimension & dim) const { - LyXFont & font = mi.base.font; - dim.asc = font_metrics::maxAscent(font); - dim.des = font_metrics::maxDescent(font); + lyx::frontend::FontMetrics const & fm = + theApp->fontLoader().metrics(mi.base.font); + dim.asc = fm.maxAscent(); + dim.des = fm.maxDescent(); string s; switch (kind_) { @@ -56,7 +59,7 @@ void InsetSpecialChar::metrics(MetricsInfo & mi, Dimension & dim) const case HYPHENATION: s = "-"; break; } docstring ds(s.begin(), s.end()); - dim.wid = font_metrics::width(ds, font); + dim.wid = fm.width(ds); if (kind_ == HYPHENATION && dim.wid > 5) dim.wid -= 2; // to make it look shorter dim_ = dim; @@ -96,10 +99,13 @@ void InsetSpecialChar::draw(PainterInfo & pi, int x, int y) const } case MENU_SEPARATOR: { + lyx::frontend::FontMetrics const & fm = + theApp->fontLoader().metrics(font); + // A triangle the width and height of an 'x' - int w = font_metrics::width(lyx::char_type('x'), font); - int ox = font_metrics::width(lyx::char_type(' '), font) + x; - int h = font_metrics::ascent(lyx::char_type('x'), font); + int w = fm.width(lyx::char_type('x')); + int ox = fm.width(lyx::char_type(' ')) + x; + int h = fm.ascent(lyx::char_type('x')); int xp[4], yp[4]; xp[0] = ox; yp[0] = y; diff --git a/src/insets/insettabular.C b/src/insets/insettabular.C index 3d772296c4..bbe11a42fd 100644 --- a/src/insets/insettabular.C +++ b/src/insets/insettabular.C @@ -38,7 +38,6 @@ #include "frontends/Alert.h" #include "frontends/Application.h" -#include "frontends/font_metrics.h" #include "frontends/Clipboard.h" #include "frontends/Painter.h" #include "frontends/Selection.h" diff --git a/src/insets/insettext.C b/src/insets/insettext.C index 35d229d798..7519d91470 100644 --- a/src/insets/insettext.C +++ b/src/insets/insettext.C @@ -43,7 +43,6 @@ #include "undo.h" #include "frontends/Alert.h" -#include "frontends/font_metrics.h" #include "frontends/Painter.h" #include "support/lyxalgo.h" // lyx::count diff --git a/src/insets/insetvspace.C b/src/insets/insetvspace.C index 8a80dd3853..7a42d5024d 100644 --- a/src/insets/insetvspace.C +++ b/src/insets/insetvspace.C @@ -23,7 +23,9 @@ #include "lyxtext.h" #include "metricsinfo.h" -#include "frontends/font_metrics.h" +#include "frontends/Application.h" +#include "frontends/FontLoader.h" +#include "frontends/FontMetrics.h" #include "frontends/Painter.h" #include @@ -131,7 +133,7 @@ void InsetVSpace::metrics(MetricsInfo & mi, Dimension & dim) const int d = 0; string lab = label(); docstring dlab(lab.begin(), lab.end()); - font_metrics::rectText(dlab, font, w, a, d); + theApp->fontLoader().metrics(font).rectText(dlab, w, a, d); height = max(height, a + d); @@ -183,7 +185,7 @@ void InsetVSpace::draw(PainterInfo & pi, int x, int y) const font.decSize(); string lab = label(); docstring dlab(lab.begin(), lab.end()); - font_metrics::rectText(dlab, font, w, a, d); + theApp->fontLoader().metrics(font).rectText(dlab, w, a, d); pi.pain.rectText(x + 2 * arrow_size + 5, start + (end - start) / 2 + (a - d) / 2, diff --git a/src/insets/render_button.C b/src/insets/render_button.C index 1537ecf14c..883b6244fe 100644 --- a/src/insets/render_button.C +++ b/src/insets/render_button.C @@ -15,7 +15,9 @@ #include "LColor.h" #include "metricsinfo.h" -#include "frontends/font_metrics.h" +#include "frontends/Application.h" +#include "frontends/FontLoader.h" +#include "frontends/FontMetrics.h" #include "frontends/Painter.h" using lyx::docstring; @@ -46,13 +48,15 @@ void RenderButton::metrics(MetricsInfo &, Dimension & dim) const { LyXFont font(LyXFont::ALL_SANE); font.decSize(); - - docstring dtext(text_.begin(), text_.end()); + lyx::frontend::FontMetrics const & fm = + theApp->fontLoader().metrics(font); + + docstring dtext(text_.begin(), text_.end()); if (editable_) - font_metrics::buttonText(dtext, font, dim.wid, dim.asc, dim.des); + fm.buttonText(dtext, dim.wid, dim.asc, dim.des); else - font_metrics::rectText(dtext, font, dim.wid, dim.asc, dim.des); + fm.rectText(dtext, dim.wid, dim.asc, dim.des); dim.wid += 4; } diff --git a/src/insets/render_graphic.C b/src/insets/render_graphic.C index 442d89d5a3..3b470bde30 100644 --- a/src/insets/render_graphic.C +++ b/src/insets/render_graphic.C @@ -20,7 +20,9 @@ #include "lyxrc.h" #include "metricsinfo.h" -#include "frontends/font_metrics.h" +#include "frontends/Application.h" +#include "frontends/FontLoader.h" +#include "frontends/FontMetrics.h" #include "frontends/Painter.h" #include "graphics/GraphicsImage.h" @@ -163,15 +165,16 @@ void RenderGraphic::metrics(MetricsInfo & mi, Dimension & dim) const docstring djust(justname.begin(), justname.end()); if (!justname.empty()) { msgFont.setSize(LyXFont::SIZE_FOOTNOTE); - font_width = font_metrics::width(djust, msgFont); + font_width = theApp->fontLoader().metrics(msgFont) + .width(djust); } string const msg = statusMessage(params_, loader_.status()); if (!msg.empty()) { docstring dmsg(msg.begin(), msg.end()); msgFont.setSize(LyXFont::SIZE_TINY); - font_width = std::max(font_width, - font_metrics::width(dmsg, msgFont)); + font_width = std::max(font_width, theApp->fontLoader() + .metrics(msgFont).width(dmsg)); } dim.wid = std::max(50, font_width + 15); @@ -216,7 +219,7 @@ void RenderGraphic::draw(PainterInfo & pi, int x, int y) const docstring djust(justname.begin(), justname.end()); msgFont.setSize(LyXFont::SIZE_FOOTNOTE); pi.pain.text(x + InsetOld::TEXT_TO_INSET_OFFSET + 6, - y - font_metrics::maxAscent(msgFont) - 4, + y - theApp->fontLoader().metrics(msgFont).maxAscent() - 4, djust, msgFont); } diff --git a/src/insets/render_preview.C b/src/insets/render_preview.C index dd61fe7134..c6532a4e55 100644 --- a/src/insets/render_preview.C +++ b/src/insets/render_preview.C @@ -21,7 +21,9 @@ #include "lyxrc.h" #include "metricsinfo.h" -#include "frontends/font_metrics.h" +#include "frontends/Application.h" +#include "frontends/FontLoader.h" +#include "frontends/FontMetrics.h" #include "frontends/Painter.h" #include "graphics/PreviewImage.h" @@ -133,7 +135,7 @@ void RenderPreview::metrics(MetricsInfo & mi, Dimension & dim) const font.setSize(LyXFont::SIZE_FOOTNOTE); string stat = statusMessage(mi.base.bv, snippet_); docstring dstat(stat.begin(), stat.end()); - dim.wid = 15 + font_metrics::width(dstat, font); + dim.wid = 15 + theApp->fontLoader().metrics(font).width(dstat); } dim_ = dim; @@ -168,7 +170,7 @@ void RenderPreview::draw(PainterInfo & pi, int x, int y) const string stat = statusMessage(pi.base.bv, snippet_); docstring dstat(stat.begin(), stat.end()); pi.pain.text(x + offset + 6, - y - font_metrics::maxAscent(font) - 4, + y - theApp->fontLoader().metrics(font).maxAscent() - 4, dstat, font); } } diff --git a/src/lyxlength.C b/src/lyxlength.C index 70a8aaad42..f08d9191a4 100644 --- a/src/lyxlength.C +++ b/src/lyxlength.C @@ -136,7 +136,7 @@ int LyXLength::inPixels(int text_width, int em_width_base) const ? em_width_base : 10*(dpi/72.27)*zoom; // A different estimate for em_width is - // font_metrics::width('M', LyXFont(LyXFont::ALL_SANE)) + // theApp->fontLoader().metrics(LyXFont(LyXFont::ALL_SANE)).width('M') // but this estimate might not be more accurate as the screen font // is different then the latex font. diff --git a/src/mathed/InsetFormulaMacro.C b/src/mathed/InsetFormulaMacro.C index 4544151305..3b644b65e4 100644 --- a/src/mathed/InsetFormulaMacro.C +++ b/src/mathed/InsetFormulaMacro.C @@ -24,8 +24,10 @@ #include "lyxlex.h" #include "outputparams.h" +#include "frontends/Application.h" +#include "frontends/FontLoader.h" +#include "frontends/FontMetrics.h" #include "frontends/Painter.h" -#include "frontends/font_metrics.h" #include "support/lstrings.h" @@ -127,7 +129,8 @@ void InsetFormulaMacro::metrics(MetricsInfo & mi, Dimension & dim) const tmpl()->metrics(mi, dim); dim.asc += 5; dim.des += 5; - dim.wid += 10 + font_metrics::width(prefix(), mi.base.font); + dim.wid += 10 + + theApp->fontLoader().metrics(mi.base.font).width(prefix()); dim_ = dim; } @@ -162,7 +165,9 @@ void InsetFormulaMacro::draw(PainterInfo & p, int x, int y) const pi.pain.text(x + 2, y, prefix(), font); // body - tmpl()->draw(pi, x + font_metrics::width(prefix(), p.base.font) + 5, y); + tmpl()->draw(pi, + x + theApp->fontLoader().metrics(p.base.font).width(prefix()) + 5, + y); setPosCache(pi, x, y); } diff --git a/src/mathed/InsetMathBig.C b/src/mathed/InsetMathBig.C index b06654ae9e..3b8b87abe3 100644 --- a/src/mathed/InsetMathBig.C +++ b/src/mathed/InsetMathBig.C @@ -15,6 +15,10 @@ #include "MathMLStream.h" #include "MathStream.h" +#include "frontends/Application.h" +#include "frontends/FontLoader.h" +#include "frontends/FontMetrics.h" + #include "support/lstrings.h" @@ -61,7 +65,8 @@ double InsetMathBig::increase() const void InsetMathBig::metrics(MetricsInfo & mi, Dimension & dim) const { - double const h = mathed_char_ascent(mi.base.font, 'I'); + double const h + = theApp->fontLoader().metrics(mi.base.font).ascent('I'); double const f = increase(); dim_.wid = 6; dim_.asc = int(h + f * h); diff --git a/src/mathed/InsetMathChar.C b/src/mathed/InsetMathChar.C index edec8c43d0..2d3c12026d 100644 --- a/src/mathed/InsetMathChar.C +++ b/src/mathed/InsetMathChar.C @@ -20,6 +20,10 @@ #include "support/lstrings.h" #include "TextPainter.h" +#include "frontends/Application.h" +#include "frontends/FontLoader.h" +#include "frontends/FontMetrics.h" + using std::auto_ptr; extern bool has_math_fonts; @@ -76,7 +80,7 @@ void InsetMathChar::metrics(MetricsInfo & mi, Dimension & dim) const whichFont(font_, code_, mi); mathed_char_dim(font_, char_, dim_); if (isBinaryOp(char_, code_)) - width_ += 2 * font_metrics::width(' ', font_); + width_ += 2 * theApp->fontLoader().metrics(font_).width(' '); lyxerr << "InsetMathChar::metrics: " << dim << endl; #endif width_ = dim.wid; diff --git a/src/mathed/MathMacroTemplate.C b/src/mathed/MathMacroTemplate.C index 35e433b2ee..32e7bc885b 100644 --- a/src/mathed/MathMacroTemplate.C +++ b/src/mathed/MathMacroTemplate.C @@ -21,8 +21,10 @@ #include "lyxlex.h" #include "LColor.h" +#include "frontends/Application.h" +#include "frontends/FontLoader.h" +#include "frontends/FontMetrics.h" #include "frontends/Painter.h" -#include "frontends/font_metrics.h" #include "support/lstrings.h" @@ -117,7 +119,7 @@ void MathMacroTemplate::metrics(MetricsInfo & mi, Dimension & dim) const cell(1).metrics(mi); docstring dp = prefix(); dim.wid = cell(0).width() + cell(1).width() + 20 - + font_metrics::width(dp, mi.base.font); + + theApp->fontLoader().metrics(mi.base.font).width(dp); dim.asc = std::max(cell(0).ascent(), cell(1).ascent()) + 7; dim.des = std::max(cell(0).descent(), cell(1).descent()) + 7; dim_ = dim; @@ -155,7 +157,8 @@ void MathMacroTemplate::draw(PainterInfo & p, int x, int y) const #endif docstring dp = prefix(); pi.pain.text(x + 2, y, dp, font); - x += font_metrics::width(dp, pi.base.font) + 6; + // FIXME: Painter text should retain the drawn text width + x += theApp->fontLoader().metrics(font).width(dp) + 6; int const w0 = cell(0).width(); int const w1 = cell(1).width(); diff --git a/src/mathed/MathSupport.C b/src/mathed/MathSupport.C index 448b74055e..f89289b5b7 100644 --- a/src/mathed/MathSupport.C +++ b/src/mathed/MathSupport.C @@ -21,9 +21,9 @@ #include "LColor.h" #include "frontends/Application.h" -#include "frontends/Painter.h" -#include "frontends/font_metrics.h" #include "frontends/FontLoader.h" +#include "frontends/FontMetrics.h" +#include "frontends/Painter.h" #include #include @@ -368,52 +368,44 @@ deco_struct const * search_deco(string const & name) void mathed_char_dim(LyXFont const & font, unsigned char c, Dimension & dim) { - dim.des = font_metrics::descent(c, font); - dim.asc = font_metrics::ascent(c, font); - dim.wid = mathed_char_width(font, c); -} - - -int mathed_char_ascent(LyXFont const & font, unsigned char c) -{ - return font_metrics::ascent(c, font); -} - - -int mathed_char_descent(LyXFont const & font, unsigned char c) -{ - return font_metrics::descent(c, font); + lyx::frontend::FontMetrics const & fm = + theApp->fontLoader().metrics(font); + dim.des = fm.descent(c); + dim.asc = fm.ascent(c); + dim.wid = fm.width(c); } int mathed_char_width(LyXFont const & font, unsigned char c) { - return font_metrics::width(c, font); + return theApp->fontLoader().metrics(font).width(c); } void mathed_string_dim(LyXFont const & font, string const & s, Dimension & dim) { + lyx::frontend::FontMetrics const & fm = + theApp->fontLoader().metrics(font); #if 1 dim.asc = 0; dim.des = 0; for (string::const_iterator it = s.begin(); it != s.end(); ++it) { - dim.asc = max(dim.asc, font_metrics::ascent(*it, font)); - dim.des = max(dim.des, font_metrics::descent(*it, font)); + dim.asc = max(dim.asc, fm.ascent(*it)); + dim.des = max(dim.des, fm.descent(*it)); } #else - dim.asc = font_metrics::maxAscent(font); - dim.des = font_metrics::maxDescent(font); + dim.asc = fm.maxAscent(); + dim.des = fm.maxDescent(); #endif - docstring ds(s.begin(), s.end()); - dim.wid = font_metrics::width(ds, font); + docstring ds(s.begin(), s.end()); + dim.wid = fm.width(ds); } int mathed_string_width(LyXFont const & font, string const & s) { - docstring ds(s.begin(), s.end()); - return font_metrics::width(ds, font); + docstring ds(s.begin(), s.end()); + return theApp->fontLoader().metrics(font).width(ds); } @@ -507,8 +499,10 @@ void drawStrBlack(PainterInfo & pi, int x, int y, string const & str) void math_font_max_dim(LyXFont const & font, int & asc, int & des) { - asc = font_metrics::maxAscent(font); - des = font_metrics::maxDescent(font); + lyx::frontend::FontMetrics const & fm = + theApp->fontLoader().metrics(font); + asc = fm.maxAscent(); + des = fm.maxDescent(); } diff --git a/src/mathed/MathSupport.h b/src/mathed/MathSupport.h index 3874735e1e..68fd49d6e1 100644 --- a/src/mathed/MathSupport.h +++ b/src/mathed/MathSupport.h @@ -25,8 +25,6 @@ class InsetMath; void mathed_char_dim(LyXFont const &, unsigned char c, Dimension & dim); int mathed_char_width(LyXFont const &, unsigned char c); -int mathed_char_ascent(LyXFont const &, unsigned char c); -int mathed_char_descent(LyXFont const &, unsigned char c); void mathed_draw_deco(PainterInfo & pi, int x, int y, int w, int h, std::string const & name); diff --git a/src/rowpainter.C b/src/rowpainter.C index a280defd3f..bb22359928 100644 --- a/src/rowpainter.C +++ b/src/rowpainter.C @@ -32,7 +32,9 @@ #include "ParagraphParameters.h" #include "vspace.h" -#include "frontends/font_metrics.h" +#include "frontends/Application.h" +#include "frontends/FontLoader.h" +#include "frontends/FontMetrics.h" #include "frontends/nullpainter.h" #include "frontends/Painter.h" @@ -45,6 +47,8 @@ using lyx::docstring; using lyx::frontend::Painter; using lyx::frontend::NullPainter; +using lyx::frontend::FontMetrics; + using lyx::char_type; using lyx::pos_type; using lyx::pit_type; @@ -198,7 +202,7 @@ void RowPainter::paintHebrewComposeChar(pos_type & vpos, LyXFont const & font) str += c; ++vpos; - int const width = font_metrics::width(c, font); + int const width = theApp->fontLoader().metrics(font).width(c); int dx = 0; for (pos_type i = pos - 1; i >= 0; --i) { @@ -233,7 +237,7 @@ void RowPainter::paintArabicComposeChar(pos_type & vpos, LyXFont const & font) str += c; ++vpos; - int const width = font_metrics::width(c, font); + int const width = theApp->fontLoader().metrics(font).width(c); int dx = 0; for (pos_type i = pos - 1; i >= 0; --i) { @@ -313,10 +317,10 @@ void RowPainter::paintChars(pos_type & vpos, LyXFont font, //lyxerr << "paint row: yo_ " << yo_ << "\n"; #if 0 pain_.text(int(x_), yo_, str, font); - x_ += font_metrics::width(str, font); + x_ += theApp->fontLoader().metrics(font).width(str); #else pain_.text(int(x_), yo_, &str[0], str.size(), font); - x_ += font_metrics::width(&str[0], str.size(), font); + x_ += theApp->fontLoader().metrics(font).width(&str[0], str.size()); #endif } @@ -460,7 +464,7 @@ int RowPainter::paintAppendixStart(int y) int a = 0; int d = 0; docstring dlab(label.begin(), label.end()); - font_metrics::rectText(dlab, pb_font, w, a, d); + theApp->fontLoader().metrics(pb_font).rectText(dlab, w, a, d); int const text_start = int(xo_ + (width_ - w) / 2); int const text_end = text_start + w; @@ -515,6 +519,9 @@ void RowPainter::paintFirst() || is_seq)) { LyXFont const font = getLabelFont(); + FontMetrics const & fm = + theApp->fontLoader().metrics(font); + string const str = par_.getLabelstring(); if (!str.empty()) { double x = x_; @@ -531,14 +538,14 @@ void RowPainter::paintFirst() spacing_val = buffer.params().spacing().getValue(); } - int const labeladdon = int(font_metrics::maxHeight(font) * layout->spacing.getValue() * spacing_val); + int const labeladdon = int(fm.maxHeight() * layout->spacing.getValue() * spacing_val); - int const maxdesc = int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val) + int const maxdesc = int(fm.maxDescent() * layout->spacing.getValue() * spacing_val) + int(layout->parsep) * defaultRowHeight(); if (is_rtl) { x = width_ - leftMargin() - - font_metrics::width(dstr, font); + fm.width(dstr); } pain_.text(int(x), yo_ - maxdesc - labeladdon, dstr, font); @@ -547,10 +554,10 @@ void RowPainter::paintFirst() docstring dlab(lab.begin(), lab.end()); if (is_rtl) { x = width_ - leftMargin() - + font_metrics::width(dlab, font); + + fm.width(dlab); } else { - x = x_ - font_metrics::width(dlab, font) - - font_metrics::width(dstr, font); + x = x_ - fm.width(dlab) + - fm.width(dstr); } pain_.text(int(x), yo_, dstr, font); @@ -573,10 +580,14 @@ void RowPainter::paintFirst() else spacing_val = buffer.params().spacing().getValue(); - int const labeladdon = int(font_metrics::maxHeight(font) * layout->spacing.getValue() * spacing_val); + FontMetrics const & fm = + theApp->fontLoader().metrics(font); + + int const labeladdon = int(fm.maxHeight() + * layout->spacing.getValue() * spacing_val); int maxdesc = - int(font_metrics::maxDescent(font) * layout->spacing.getValue() * spacing_val + int(fm.maxDescent() * layout->spacing.getValue() * spacing_val + (layout->labelbottomsep * defaultRowHeight())); double x = x_; @@ -584,10 +595,9 @@ void RowPainter::paintFirst() if (is_rtl) x = leftMargin(); x += (width_ - text_.rightMargin(par_) - leftMargin()) / 2; - x -= font_metrics::width(dstr, font) / 2; + x -= fm.width(dstr) / 2; } else if (is_rtl) { - x = width_ - leftMargin() - - font_metrics::width(dstr, font); + x = width_ - leftMargin() - fm.width(dstr); } pain_.text(int(x), yo_ - maxdesc - labeladdon, dstr, font); } @@ -605,7 +615,8 @@ void RowPainter::paintLast() case END_LABEL_BOX: case END_LABEL_FILLED_BOX: { LyXFont const font = getLabelFont(); - int const size = int(0.75 * font_metrics::maxAscent(font)); + FontMetrics const & fm = theApp->fontLoader().metrics(font); + int const size = int(0.75 * fm.maxAscent()); int const y = yo_ - size; int x = is_rtl ? nestMargin() + changebarMargin() : width_ - size; @@ -621,10 +632,11 @@ void RowPainter::paintLast() case END_LABEL_STATIC: { LyXFont font = getLabelFont(); + FontMetrics const & fm = theApp->fontLoader().metrics(font); string const & str = par_.layout()->endlabelstring(); docstring dstr(str.begin(), str.end()); double const x = is_rtl ? - x_ - font_metrics::width(dstr, font) + x_ - fm.width(dstr) : - text_.rightMargin(par_) - row_.width(); pain_.text(int(x), yo_, dstr, font); break; @@ -695,9 +707,9 @@ void RowPainter::paintText() // We also don't paint across things like tables if (running_strikeout && (highly_editable_inset || !is_struckout)) { // Calculate 1/3 height of the buffer's default font - int const middle = - yo_ - - font_metrics::maxAscent(bv_.buffer()->params().getFont()) / 3; + FontMetrics const & fm = theApp->fontLoader().metrics( + bv_.buffer()->params().getFont()); + int const middle = yo_ - fm.maxAscent() / 3; pain_.line(last_strikeout_x, middle, int(x_), middle, LColor::strikeout, Painter::line_solid, Painter::line_thin); running_strikeout = false; @@ -706,8 +718,8 @@ void RowPainter::paintText() if (body_pos > 0 && pos == body_pos - 1) { string lab = layout->labelsep; docstring dlab(lab.begin(), lab.end()); - int const lwidth = font_metrics::width(dlab, - getLabelFont()); + int const lwidth = + theApp->fontLoader().metrics(getLabelFont()).width(dlab); x_ += label_hfill_ + lwidth - width_pos; } @@ -751,9 +763,9 @@ void RowPainter::paintText() // if we reach the end of a struck out range, paint it if (running_strikeout) { // calculate 1/3 height of the buffer's default font - int const middle = - yo_ - - font_metrics::maxAscent(bv_.buffer()->params().getFont()) / 3; + FontMetrics const & fm = theApp->fontLoader().metrics( + bv_.buffer()->params().getFont()); + int const middle = yo_ - fm.maxAscent() / 3; pain_.line(last_strikeout_x, middle, int(x_), middle, LColor::strikeout, Painter::line_solid, Painter::line_thin); running_strikeout = false; diff --git a/src/text.C b/src/text.C index b7eccfcadb..26b3190511 100644 --- a/src/text.C +++ b/src/text.C @@ -51,7 +51,9 @@ #include "vspace.h" #include "WordLangTuple.h" -#include "frontends/font_metrics.h" +#include "frontends/Application.h" +#include "frontends/FontLoader.h" +#include "frontends/FontMetrics.h" #include "frontends/Painter.h" #include "insets/insettext.h" @@ -89,6 +91,8 @@ using lyx::support::uppercase; using lyx::cap::cutSelection; using lyx::cap::pasteParagraphList; +using lyx::frontend::FontMetrics; + using std::auto_ptr; using std::advance; using std::distance; @@ -462,13 +466,13 @@ int LyXText::singleWidth(Paragraph const & par, Encodings::isComposeChar_hebrew(c)) return 0; } - return font_metrics::width(c, font); + return theApp->fontLoader().metrics(font).width(c); } if (c == Paragraph::META_INSET) return par.getInset(pos)->width(); - return font_metrics::width(c, font); + return theApp->fontLoader().metrics(font).width(c); } @@ -501,7 +505,7 @@ int LyXText::leftMargin(pit_type const pit, pos_type const pos) const string leftm = tclass.leftmargin(); docstring dleft(leftm.begin(), leftm.end()); - l_margin += font_metrics::signedWidth(dleft, params.getFont()); + l_margin += theApp->fontLoader().metrics(params.getFont()).signedWidth(dleft); if (par.getDepth() != 0) { // find the next level paragraph @@ -527,43 +531,41 @@ int LyXText::leftMargin(pit_type const pit, pos_type const pos) const parindent.erase(); LyXFont const labelfont = getLabelFont(par); + FontMetrics const & labelfont_metrics = theApp->fontLoader().metrics(labelfont); + switch (layout->margintype) { case MARGIN_DYNAMIC: if (!layout->leftmargin.empty()) { string leftm = layout->leftmargin; docstring dleft(leftm.begin(), leftm.end()); - l_margin += font_metrics::signedWidth(dleft, - params.getFont()); + l_margin += theApp->fontLoader().metrics(params.getFont()).signedWidth(dleft); } if (!par.getLabelstring().empty()) { string labin = layout->labelindent; docstring dlabin(labin.begin(), labin.end()); - l_margin += font_metrics::signedWidth(dlabin, - labelfont); + l_margin += labelfont_metrics.signedWidth(dlabin); string labstr = par.getLabelstring(); docstring dlabstr(labstr.begin(), labstr.end()); - l_margin += font_metrics::width(dlabstr, - labelfont); + l_margin += labelfont_metrics.width(dlabstr); string labsep = layout->labelsep; docstring dlabsep(labsep.begin(), labsep.end()); - l_margin += font_metrics::width(dlabsep, labelfont); + l_margin += labelfont_metrics.width(dlabsep); } break; case MARGIN_MANUAL: { string labin = layout->labelindent; docstring dlabin(labin.begin(), labin.end()); - l_margin += font_metrics::signedWidth(dlabin, labelfont); + l_margin += labelfont_metrics.signedWidth(dlabin); // The width of an empty par, even with manual label, should be 0 if (!par.empty() && pos >= par.beginOfBody()) { if (!par.getLabelWidthString().empty()) { string labstr = par.getLabelWidthString(); docstring dlabstr(labstr.begin(), labstr.end()); - l_margin += font_metrics::width(dlabstr, - labelfont); + l_margin += labelfont_metrics.width(dlabstr); string labsep = layout->labelsep; docstring dlabsep(labsep.begin(), labsep.end()); - l_margin += font_metrics::width(dlabsep, labelfont); + l_margin += labelfont_metrics.width(dlabsep); } } break; @@ -572,8 +574,9 @@ int LyXText::leftMargin(pit_type const pit, pos_type const pos) const case MARGIN_STATIC: { string leftm = layout->leftmargin; docstring dleft(leftm.begin(), leftm.end()); - l_margin += font_metrics::signedWidth(dleft, params.getFont()) * 4 - / (par.getDepth() + 4); + l_margin += + theApp->fontLoader().metrics(params.getFont()).signedWidth(dleft) + * 4 / (par.getDepth() + 4); break; } @@ -582,13 +585,11 @@ int LyXText::leftMargin(pit_type const pit, pos_type const pos) const if (pos >= par.beginOfBody()) { string leftm = layout->leftmargin; docstring dleft(leftm.begin(), leftm.end()); - l_margin += font_metrics::signedWidth(dleft, - labelfont); + l_margin += labelfont_metrics.signedWidth(dleft); } else { string labin = layout->labelindent; docstring dlabin(labin.begin(), labin.end()); - l_margin += font_metrics::signedWidth(dlabin, - labelfont); + l_margin += labelfont_metrics.signedWidth(dlabin); } } else if (pos != 0 // Special case to fix problems with @@ -598,22 +599,20 @@ int LyXText::leftMargin(pit_type const pit, pos_type const pos) const && !isFirstInSequence(pit, pars_))) { string leftm = layout->leftmargin; docstring dleft(leftm.begin(), leftm.end()); - l_margin += font_metrics::signedWidth(dleft, - labelfont); + l_margin += labelfont_metrics.signedWidth(dleft); } else if (layout->labeltype != LABEL_TOP_ENVIRONMENT && layout->labeltype != LABEL_BIBLIO && layout->labeltype != LABEL_CENTERED_TOP_ENVIRONMENT) { string labin = layout->labelindent; docstring dlabin(labin.begin(), labin.end()); - l_margin += font_metrics::signedWidth(dlabin, - labelfont); + l_margin += labelfont_metrics.signedWidth(dlabin); string labsep = layout->labelsep; docstring dlabsep(labsep.begin(), labsep.end()); - l_margin += font_metrics::width(dlabsep, labelfont); + l_margin += labelfont_metrics.width(dlabsep); string labstr = par.getLabelstring(); docstring dlabstr(labstr.begin(), labstr.end()); - l_margin += font_metrics::width(dlabstr, labelfont); + l_margin += labelfont_metrics.width(dlabstr); } break; @@ -630,8 +629,7 @@ int LyXText::leftMargin(pit_type const pit, pos_type const pos) const for ( ; rit != end; ++rit) if (rit->fill() < minfill) minfill = rit->fill(); - l_margin += font_metrics::signedWidth(layout->leftmargin, - params.getFont()); + l_margin += theApp->fontLoader().metrics(params.getFont()).signedWidth(layout->leftmargin); l_margin += minfill; #endif // also wrong, but much shorter. @@ -671,7 +669,7 @@ int LyXText::leftMargin(pit_type const pit, pos_type const pos) const BufferParams::PARSEP_INDENT)) { docstring din(parindent.begin(), parindent.end()); - l_margin += font_metrics::signedWidth(din, params.getFont()); + l_margin += theApp->fontLoader().metrics(params.getFont()).signedWidth(din); } return l_margin; @@ -693,12 +691,11 @@ int LyXText::rightMargin(Paragraph const & par) const docstring dtrmarg(trmarg.begin(), trmarg.end()); string lrmarg = par.layout()->rightmargin; docstring dlrmarg(lrmarg.begin(), lrmarg.end()); + FontMetrics const & fm = theApp->fontLoader().metrics(params.getFont()); int const r_margin = ::rightMargin() - + font_metrics::signedWidth(dtrmarg, - params.getFont()) - + font_metrics::signedWidth(dlrmarg, - params.getFont()) + + fm.signedWidth(dtrmarg) + + fm.signedWidth(dlrmarg) * 4 / (par.getDepth() + 4); return r_margin; @@ -772,6 +769,7 @@ void LyXText::rowBreakPoint(pit_type const pit, Row & row) const FontIterator fi = FontIterator(*this, par, pos); pos_type point = end; pos_type i = pos; + FontMetrics const & fm = theApp->fontLoader().metrics(getLabelFont(par)); for ( ; i < end; ++i, ++fi) { char_type const c = par.getChar(i); int thiswidth = singleWidth(par, i, c, *fi); @@ -780,7 +778,7 @@ void LyXText::rowBreakPoint(pit_type const pit, Row & row) const if (body_pos && i == body_pos) { string lsep = layout->labelsep; docstring dlsep(lsep.begin(), lsep.end()); - int add = font_metrics::width(dlsep, getLabelFont(par)); + int add = fm.width(dlsep); if (par.isLineSeparator(i - 1)) add -= singleWidth(par, i - 1); @@ -860,11 +858,13 @@ void LyXText::setRowWidth(pit_type const pit, Row & row) const pos_type const body_pos = par.beginOfBody(); pos_type i = row.pos(); + FontMetrics const & fm = theApp->fontLoader().metrics(getLabelFont(par)); + if (i < end) { FontIterator fi = FontIterator(*this, par, i); for ( ; i < end; ++i, ++fi) { if (body_pos > 0 && i == body_pos) { - w += font_metrics::width(dlsep, getLabelFont(par)); + w += fm.width(dlsep); if (par.isLineSeparator(i - 1)) w -= singleWidth(par, i - 1); w = max(w, labelEnd(pit)); @@ -875,7 +875,7 @@ void LyXText::setRowWidth(pit_type const pit, Row & row) const } if (body_pos > 0 && body_pos >= end) { - w += font_metrics::width(dlsep, getLabelFont(par)); + w += fm.width(dlsep); if (end > 0 && par.isLineSeparator(end - 1)) w -= singleWidth(par, end - 1); w = max(w, labelEnd(pit)); @@ -908,7 +908,10 @@ int LyXText::labelFill(Paragraph const & par, Row const & row) const return 0; docstring dlab(label.begin(), label.end()); - return max(0, font_metrics::width(dlab, getLabelFont(par)) - w); + + FontMetrics const & fm = theApp->fontLoader().metrics(getLabelFont(par)); + + return max(0, fm.width(dlab) - w); } @@ -943,11 +946,14 @@ void LyXText::setHeightOfRow(pit_type const pit, Row & row) LyXFont labelfont = getLabelFont(par); + FontMetrics const & labelfont_metrics = theApp->fontLoader().metrics(labelfont); + FontMetrics const & fontmetrics = theApp->fontLoader().metrics(font); + // these are minimum values double const spacing_val = layout->spacing.getValue() * spacing(par); //lyxerr << "spacing_val = " << spacing_val << endl; - int maxasc = int(font_metrics::maxAscent(font) * spacing_val); - int maxdesc = int(font_metrics::maxDescent(font) * spacing_val); + int maxasc = int(fontmetrics.maxAscent() * spacing_val); + int maxdesc = int(fontmetrics.maxDescent() * spacing_val); // insets may be taller InsetList::const_iterator ii = par.insetlist.begin(); @@ -969,8 +975,8 @@ void LyXText::setHeightOfRow(pit_type const pit, Row & row) par.highestFontInRange(row.pos(), pos_end, size); if (maxsize > font.size()) { font.setSize(maxsize); - maxasc = max(maxasc, font_metrics::maxAscent(font)); - maxdesc = max(maxdesc, font_metrics::maxDescent(font)); + maxasc = max(maxasc, fontmetrics.maxAscent()); + maxdesc = max(maxdesc, fontmetrics.maxDescent()); } // This is nicer with box insets: @@ -1000,7 +1006,7 @@ void LyXText::setHeightOfRow(pit_type const pit, Row & row) // layout is printed in an extra row if (layout->counter == "chapter" && !par.params().labelString().empty()) { - labeladdon = int(font_metrics::maxHeight(labelfont) + labeladdon = int(labelfont_metrics.maxHeight() * layout->spacing.getValue() * spacing(par)); } @@ -1013,7 +1019,7 @@ void LyXText::setHeightOfRow(pit_type const pit, Row & row) && !par.getLabelstring().empty()) { labeladdon = int( - font_metrics::maxHeight(labelfont) + labelfont_metrics.maxHeight() * layout->spacing.getValue() * spacing(par) + (layout->topsep + layout->labelbottomsep) * dh); @@ -1390,7 +1396,7 @@ LyXText::computeRowMetrics(pit_type const pit, Row const & row) const { string lsep = layout->labelsep; docstring dlsep(lsep.begin(), lsep.end()); - result.x += font_metrics::width(dlsep, getLabelFont(par)); + result.x += theApp->fontLoader().metrics(getLabelFont(par)).width(dlsep); if (body_pos <= end) result.x += result.label_hfill; } @@ -2294,15 +2300,14 @@ int LyXText::cursorX(CursorSlice const & sl, bool boundary) const // Use font span to speed things up, see below FontSpan font_span; LyXFont font; + FontMetrics const & labelfm = theApp->fontLoader().metrics(getLabelFont(par)); for (pos_type vpos = row_pos; vpos < cursor_vpos; ++vpos) { pos_type pos = bidi.vis2log(vpos); if (body_pos > 0 && pos == body_pos - 1) { string lsep = par.layout()->labelsep; docstring dlsep(lsep.begin(), lsep.end()); - x += m.label_hfill - + font_metrics::width(dlsep, - getLabelFont(par)); + x += m.label_hfill + labelfm.width(dlsep); if (par.isLineSeparator(body_pos - 1)) x -= singleWidth(par, body_pos - 1); } diff --git a/src/text2.C b/src/text2.C index 403300a987..2ce03f2d09 100644 --- a/src/text2.C +++ b/src/text2.C @@ -51,7 +51,8 @@ #include "vspace.h" #include "frontends/Application.h" -#include "frontends/font_metrics.h" +#include "frontends/FontLoader.h" +#include "frontends/FontMetrics.h" #include "insets/insetenv.h" @@ -804,14 +805,16 @@ pos_type LyXText::getColumnNearX(pit_type const pit, return 0; } + lyx::frontend::FontMetrics const & fm = theApp->fontLoader().metrics( + getLabelFont(par)); + while (vc < end && tmpx <= x) { c = bidi.vis2log(vc); last_tmpx = tmpx; if (body_pos > 0 && c == body_pos - 1) { string lsep = layout->labelsep; docstring dlsep(lsep.begin(), lsep.end()); - tmpx += r.label_hfill + - font_metrics::width(dlsep, getLabelFont(par)); + tmpx += r.label_hfill + fm.width(dlsep); if (par.isLineSeparator(body_pos - 1)) tmpx -= singleWidth(par, body_pos - 1); } @@ -1342,5 +1345,5 @@ void LyXText::recUndo(pit_type par) const int defaultRowHeight() { - return int(font_metrics::maxHeight(LyXFont(LyXFont::ALL_SANE)) * 1.2); + return int(theApp->fontLoader().metrics(LyXFont(LyXFont::ALL_SANE)).maxHeight() * 1.2); } -- 2.39.2