From de174939327a2e371340313498d44a44310a867b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Andr=C3=A9=20P=C3=B6nitz?= Date: Thu, 20 Mar 2008 00:00:53 +0000 Subject: [PATCH] simpliofy {Gui}FontLoader architecture a bit git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@23842 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/frontends/FontLoader.h | 19 ++++----- src/frontends/qt4/GuiApplication.h | 6 +-- src/frontends/qt4/GuiDelimiter.cpp | 4 +- src/frontends/qt4/GuiFontLoader.cpp | 62 ++++++++++++++++++++++++----- src/frontends/qt4/GuiFontLoader.h | 60 ++++------------------------ src/frontends/qt4/GuiPainter.cpp | 7 ++-- src/frontends/qt4/GuiView.cpp | 3 +- 7 files changed, 79 insertions(+), 82 deletions(-) diff --git a/src/frontends/FontLoader.h b/src/frontends/FontLoader.h index 497f745988..20fe5b886e 100644 --- a/src/frontends/FontLoader.h +++ b/src/frontends/FontLoader.h @@ -9,8 +9,8 @@ * Full author contact details are available in file CREDITS. */ -#ifndef LYX_FONTLOADER_H -#define LYX_FONTLOADER_H +#ifndef FONTLOADER_H +#define FONTLOADER_H namespace lyx { @@ -25,18 +25,19 @@ class FontLoader { public: /// - FontLoader() {} - /// - virtual ~FontLoader() {} + FontLoader(); + /// Clears cache + ~FontLoader(); /// Update fonts after zoom, dpi, font names, or norm change - virtual void update() = 0; + // (basically by deleting all cached values) + void update(); /// Is the given font available ? - virtual bool available(FontInfo const & f) = 0; + bool available(FontInfo const & f); /// Get the Font metrics for this FontInfo - virtual FontMetrics const & metrics(FontInfo const & f) = 0; + FontMetrics const & metrics(FontInfo const & f); }; @@ -47,4 +48,4 @@ extern frontend::FontLoader & theFontLoader(); } // namespace lyx -#endif // QFONT_LOADER_H +#endif // FONTLOADER_H diff --git a/src/frontends/qt4/GuiApplication.h b/src/frontends/qt4/GuiApplication.h index d9b10bac07..7238b0ed30 100644 --- a/src/frontends/qt4/GuiApplication.h +++ b/src/frontends/qt4/GuiApplication.h @@ -14,7 +14,7 @@ #define GUIAPPLICATION_H #include "ColorCache.h" -#include "GuiFontLoader.h" +#include "FontLoader.h" #include "GuiClipboard.h" #include "GuiSelection.h" #include "Menus.h" @@ -104,8 +104,6 @@ public: /// ColorCache & colorCache() { return color_cache_; } - /// - GuiFontLoader & guiFontLoader() { return font_loader_; } /// return a suitable serif font name. virtual QString const romanFontName(); @@ -142,7 +140,7 @@ private: /// GuiSelection selection_; /// - GuiFontLoader font_loader_; + FontLoader font_loader_; /// ColorCache color_cache_; /// diff --git a/src/frontends/qt4/GuiDelimiter.cpp b/src/frontends/qt4/GuiDelimiter.cpp index b164316a2e..22fd1b6fbd 100644 --- a/src/frontends/qt4/GuiDelimiter.cpp +++ b/src/frontends/qt4/GuiDelimiter.cpp @@ -13,6 +13,7 @@ #include "GuiDelimiter.h" #include "GuiApplication.h" +#include "GuiFontLoader.h" #include "GuiView.h" #include "qt_helpers.h" @@ -91,8 +92,7 @@ GuiDelimiter::GuiDelimiter(GuiView & lv) lwi->setToolTip(toqstr(delim)); FontInfo lyxfont; lyxfont.setFamily(ms.fontfamily); - QFont const & symbol_font = guiApp->guiFontLoader().get(lyxfont); - lwi->setFont(symbol_font); + lwi->setFont(frontend::getFont(lyxfont)); list_items[ms.unicode] = lwi; leftLW->addItem(lwi); } diff --git a/src/frontends/qt4/GuiFontLoader.cpp b/src/frontends/qt4/GuiFontLoader.cpp index 6ec0bc508d..3fde5a0968 100644 --- a/src/frontends/qt4/GuiFontLoader.cpp +++ b/src/frontends/qt4/GuiFontLoader.cpp @@ -1,5 +1,5 @@ /** - * \file GuiFontLoader.cpp + * \file FontLoader.cpp * This file is part of LyX, the document processor. * Licence details can be found in the file COPYING. * @@ -11,6 +11,9 @@ #include +#include "FontLoader.h" + +#include "FontInfo.h" #include "GuiFontLoader.h" #include "qt_helpers.h" @@ -27,6 +30,8 @@ #include #include +#include + using namespace std; using namespace lyx::support; @@ -34,7 +39,6 @@ QString const math_fonts[] = {"cmex10", "cmmi10", "cmr10", "cmsy10", "eufm10", "msam10", "msbm10", "wasy10", "esint10"}; int const num_math_fonts = sizeof(math_fonts) / sizeof(*math_fonts); - namespace lyx { extern docstring const stateText(FontInfo const & f); @@ -91,10 +95,31 @@ SymbolFont symbol_fonts[] = { "-*-esint10-medium-*-*-*-*-*-*-*-*-*-*-*" } }; -size_t const nr_symbol_fonts = sizeof(symbol_fonts) / sizeof(SymbolFont); +size_t const nr_symbol_fonts = sizeof(symbol_fonts) / sizeof(symbol_fonts[0]); +/// BUTT ugly ! +static GuiFontInfo * fontinfo_[NUM_FAMILIES][2][4][10]; -QString getRawName(QString const & family) + +/// Get font info (font + metrics) for the given LyX font. +// if not cached, create it. +GuiFontInfo & fontinfo(FontInfo const & f) +{ + BOOST_ASSERT(f.family() < NUM_FAMILIES); + BOOST_ASSERT(f.series() < 2); + BOOST_ASSERT(f.realShape() < 4); + BOOST_ASSERT(f.size() < 10); + // fi is a reference to the pointer type (GuiFontInfo *) in the + // fontinfo_ table. + GuiFontInfo * & fi = + fontinfo_[f.family()][f.series()][f.realShape()][f.size()]; + if (!fi) + fi = new GuiFontInfo(f); + return *fi; +} + + +QString rawName(QString const & family) { for (size_t i = 0; i < nr_symbol_fonts; ++i) if (family == symbol_fonts[i].family) @@ -105,7 +130,7 @@ QString getRawName(QString const & family) } -QString const symbolFamily(FontFamily family) +QString symbolFamily(FontFamily family) { for (size_t i = 0; i < nr_symbol_fonts; ++i) { if (family == symbol_fonts[i].lyx_family) @@ -168,7 +193,7 @@ QFont symbolFont(QString const & family, bool * ok) // A simple setFamily() fails on Qt 2 - QString const rawName = getRawName(family); + QString const rawName = rawName(family); LYXERR(Debug::FONT, "Trying " << fromqstr(rawName) << " ... "); font.setRawName(rawName); @@ -186,7 +211,7 @@ QFont symbolFont(QString const & family, bool * ok) } // namespace anon -GuiFontLoader::GuiFontLoader() +FontLoader::FontLoader() { QString const fonts_dir = toqstr(addPath(package().system_support().absFilename(), "fonts")); @@ -208,7 +233,7 @@ GuiFontLoader::GuiFontLoader() } -void GuiFontLoader::update() +void FontLoader::update() { for (int i1 = 0; i1 < NUM_FAMILIES; ++i1) { for (int i2 = 0; i2 < 2; ++i2) @@ -221,7 +246,7 @@ void GuiFontLoader::update() } -GuiFontLoader::~GuiFontLoader() +FontLoader::~FontLoader() { update(); } @@ -328,7 +353,7 @@ GuiFontInfo::GuiFontInfo(FontInfo const & f) } -bool GuiFontLoader::available(FontInfo const & f) +bool FontLoader::available(FontInfo const & f) { static vector cache_set(NUM_FAMILIES, false); static vector cache(NUM_FAMILIES, false); @@ -352,5 +377,22 @@ bool GuiFontLoader::available(FontInfo const & f) return true; } +FontMetrics const & FontLoader::metrics(FontInfo const & f) +{ + return fontinfo(f).metrics; +} + +/// Get the QFont for this FontInfo +QFont const & getFont(FontInfo const & f) +{ + return fontinfo(f).font; +} + +/// Get the QFont for this FontInfo +GuiFontInfo const & getFontInfo(FontInfo const & f) +{ + return fontinfo(f); +} + } // namespace frontend } // namespace lyx diff --git a/src/frontends/qt4/GuiFontLoader.h b/src/frontends/qt4/GuiFontLoader.h index b48bfd4cfd..1d09b6d616 100644 --- a/src/frontends/qt4/GuiFontLoader.h +++ b/src/frontends/qt4/GuiFontLoader.h @@ -9,21 +9,13 @@ * Full author contact details are available in file CREDITS. */ -#ifndef QT4_FONTLOADER_H -#define QT4_FONTLOADER_H - -#include "frontends/FontLoader.h" - -#include "FontInfo.h" +#ifndef GUI_FONTLOADER_H +#define GUI_FONTLOADER_H #include "GuiFontMetrics.h" -#include "Encoding.h" - #include -#include - namespace lyx { namespace frontend { @@ -43,50 +35,12 @@ public: }; -/// Hold info about a particular font -class GuiFontLoader : public FontLoader -{ -public: - /// - GuiFontLoader(); - - /// Destructor - virtual ~GuiFontLoader(); - - virtual void update(); - virtual bool available(FontInfo const & f); - inline virtual FontMetrics const & metrics(FontInfo const & f) { - return fontinfo(f).metrics; - } - - /// Get the QFont for this FontInfo - QFont const & get(FontInfo const & f) { - return fontinfo(f).font; - } - - - /// Get font info (font + metrics) for the given LyX font. - GuiFontInfo & fontinfo(FontInfo const & f) { - BOOST_ASSERT(f.family() < NUM_FAMILIES); - BOOST_ASSERT(f.series() < 2); - BOOST_ASSERT(f.realShape() < 4); - BOOST_ASSERT(f.size() < 10); - // fi is a reference to the pointer type (GuiFontInfo *) in the - // fontinfo_ table. - GuiFontInfo * & fi = - fontinfo_[f.family()][f.series()][f.realShape()][f.size()]; - if (!fi) - fi = new GuiFontInfo(f); - return *fi; - } - -private: - /// BUTT ugly ! - GuiFontInfo * fontinfo_[NUM_FAMILIES][2][4][10]; -}; - +// Load font +GuiFontInfo const & getFontInfo(FontInfo const & f); +/// Get the QFont for this FontInfo +QFont const & getFont(FontInfo const & f); } // namespace frontend } // namespace lyx -#endif // QT4_FONTLOADER_H +#endif // GUI_FONTLOADER_H diff --git a/src/frontends/qt4/GuiPainter.cpp b/src/frontends/qt4/GuiPainter.cpp index 275c8d3eea..7c0af8bd9e 100644 --- a/src/frontends/qt4/GuiPainter.cpp +++ b/src/frontends/qt4/GuiPainter.cpp @@ -14,6 +14,7 @@ #include "GuiPainter.h" #include "GuiApplication.h" +#include "GuiFontLoader.h" #include "GuiFontMetrics.h" #include "GuiImage.h" #include "qt_helpers.h" @@ -271,8 +272,8 @@ int GuiPainter::smallCapsText(int x, int y, FontInfo smallfont(f); smallfont.decSize().decSize().setShape(UP_SHAPE); - QFont const & qfont = guiApp->guiFontLoader().get(f); - QFont const & qsmallfont = guiApp->guiFontLoader().get(smallfont); + QFont const & qfont = getFont(f); + QFont const & qsmallfont = getFont(smallfont); setQPainterPen(computeColor(f.realColor())); int textwidth = 0; @@ -318,7 +319,7 @@ int GuiPainter::text(int x, int y, docstring const & s, str = ' ' + str; #endif - GuiFontInfo & fi = guiApp->guiFontLoader().fontinfo(f); + GuiFontInfo const & fi = getFontInfo(f); int textwidth; diff --git a/src/frontends/qt4/GuiView.cpp b/src/frontends/qt4/GuiView.cpp index db7462f7e0..b69f1393af 100644 --- a/src/frontends/qt4/GuiView.cpp +++ b/src/frontends/qt4/GuiView.cpp @@ -35,11 +35,11 @@ #include "BufferView.h" #include "Converter.h" #include "Cursor.h" +#include "Encoding.h" #include "ErrorList.h" #include "Format.h" #include "FuncStatus.h" #include "FuncRequest.h" -#include "support/gettext.h" #include "Intl.h" #include "Layout.h" #include "Lexer.h" @@ -57,6 +57,7 @@ #include "support/FileFilterList.h" #include "support/FileName.h" #include "support/filetools.h" +#include "support/gettext.h" #include "support/ForkedCalls.h" #include "support/lstrings.h" #include "support/os.h" -- 2.39.5