]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiFontLoader.cpp
Use the command-alternatives sequence as defined in menus.bind
[lyx.git] / src / frontends / qt4 / GuiFontLoader.cpp
index 4559e3374c99ce90a896ec0428742a4ed44c86dd..4f705330f9d33301d24d2dd2a5422d80b0bbd240 100644 (file)
@@ -19,7 +19,6 @@
 
 #include "LyXRC.h"
 
-#include "support/convert.h"
 #include "support/debug.h"
 #include "support/filetools.h"
 #include "support/gettext.h"
@@ -88,22 +87,41 @@ SymbolFont symbol_fonts[] = {
 size_t const nr_symbol_fonts = sizeof(symbol_fonts) / sizeof(symbol_fonts[0]);
 
 /// BUTT ugly !
-static GuiFontInfo * fontinfo_[NUM_FAMILIES][NUM_SERIES][NUM_SHAPE][NUM_SIZE];
+static GuiFontInfo *
+fontinfo_[NUM_FAMILIES][NUM_SERIES][NUM_SHAPE][NUM_SIZE][NUM_STYLE];
+
+
+// returns a reference to the pointer type (GuiFontInfo *) in the
+// fontinfo_ table.
+GuiFontInfo * & fontinfo_ptr(FontInfo const & f)
+{
+       // The display font and the text font are the same
+       size_t const style = (f.style() == LM_ST_DISPLAY) ? LM_ST_TEXT : f.style();
+       return fontinfo_[f.family()][f.series()][f.realShape()][f.size()][style];
+}
 
 
 // Get font info (font + metrics) for the given LyX font.
 // if not cached, create it.
 GuiFontInfo & fontinfo(FontInfo const & f)
 {
-       // LASSERT: Is there anything we might do here besides crash?
-       LBUFERR(f.family() < NUM_FAMILIES);
-       LBUFERR(f.series() < NUM_SERIES);
-       LBUFERR(f.realShape() < NUM_SHAPE);
-       LBUFERR(f.size() < NUM_SIZE);
-       // fi is a reference to the pointer type (GuiFontInfo *) in the
-       // fontinfo_ table.
-       GuiFontInfo * & fi =
-               fontinfo_[f.family()][f.series()][f.realShape()][f.size()];
+    bool const fontIsRealized =
+            (f.family() < NUM_FAMILIES) &&
+            (f.series() < NUM_SERIES) &&
+            (f.realShape() < NUM_SHAPE) &&
+            (f.size() < NUM_SIZE);
+    if (!fontIsRealized) {
+        // We can reset the font to something sensible in release mode.
+        LATTEST(false);
+        LYXERR0("Unrealized font!");
+        FontInfo f2 = f;
+        f2.realize(sane_font);
+        GuiFontInfo * & fi = fontinfo_ptr(f2);
+        if (!fi)
+            fi = new GuiFontInfo(f2);
+        return *fi;
+    }
+    GuiFontInfo * & fi = fontinfo_ptr(f);
        if (!fi)
                fi = new GuiFontInfo(f);
        return *fi;
@@ -139,7 +157,8 @@ bool isSymbolFamily(FontFamily family)
 #endif
 
 
-static bool isChosenFont(QFont & font, QString const & family)
+static bool isChosenFont(QFont & font, QString const & family,
+                        QString const & style)
 {
        // QFontInfo won't find a font that has only a few glyphs at unusual
        // positions, e.g. the original esint10 font.
@@ -149,7 +168,11 @@ static bool isChosenFont(QFont & font, QString const & family)
 
        LYXERR(Debug::FONT, "got: " << fi.family());
 
-       if (fi.family().contains(family)) {
+       if (fi.family().contains(family)
+#if QT_VERSION >= 0x040800
+           && (style.isEmpty() || fi.styleName().contains(style))
+#endif
+           ) {
                LYXERR(Debug::FONT, " got it ");
                return true;
        }
@@ -166,8 +189,20 @@ QFont symbolFont(QString const & family, bool * ok)
 
        QFont font;
        font.setFamily(family);
+#if QT_VERSION >= 0x040800
+       font.setStyleName("LyX");
+
+       if (isChosenFont(font, family, "LyX")) {
+               LYXERR(Debug::FONT, "lyx!");
+               *ok = true;
+               return font;
+       }
+
+       LYXERR(Debug::FONT, "Trying normal " << family << " ... ");
+       font.setStyleName(QString());
+#endif
 
-       if (isChosenFont(font, family)) {
+       if (isChosenFont(font, family, QString())) {
                LYXERR(Debug::FONT, "normal!");
                *ok = true;
                return font;
@@ -176,7 +211,7 @@ QFont symbolFont(QString const & family, bool * ok)
        LYXERR(Debug::FONT, "Trying " << upper << " ... ");
        font.setFamily(upper);
 
-       if (isChosenFont(font, upper)) {
+       if (isChosenFont(font, upper, QString())) {
                LYXERR(Debug::FONT, "upper!");
                *ok = true;
                return font;
@@ -188,7 +223,7 @@ QFont symbolFont(QString const & family, bool * ok)
        LYXERR(Debug::FONT, "Trying " << raw << " ... ");
        font.setRawName(raw);
 
-       if (isChosenFont(font, family)) {
+       if (isChosenFont(font, family, QString())) {
                LYXERR(Debug::FONT, "raw version!");
                *ok = true;
                return font;
@@ -199,7 +234,7 @@ QFont symbolFont(QString const & family, bool * ok)
        return font;
 }
 
-} // namespace anon
+} // namespace
 
 
 FontLoader::FontLoader()
@@ -219,7 +254,8 @@ FontLoader::FontLoader()
                for (int i2 = 0; i2 < NUM_SERIES; ++i2)
                        for (int i3 = 0; i3 < NUM_SHAPE; ++i3)
                                for (int i4 = 0; i4 < NUM_SIZE; ++i4)
-                                       fontinfo_[i1][i2][i3][i4] = 0;
+                                       for (int i5 = 0; i5 < NUM_STYLE; ++i5)
+                                               fontinfo_[i1][i2][i3][i4][i5] = 0;
 }
 
 
@@ -228,9 +264,10 @@ void FontLoader::update()
        for (int i1 = 0; i1 < NUM_FAMILIES; ++i1)
                for (int i2 = 0; i2 < NUM_SERIES; ++i2)
                        for (int i3 = 0; i3 < NUM_SHAPE; ++i3)
-                               for (int i4 = 0; i4 < NUM_SIZE; ++i4) {
-                                       delete fontinfo_[i1][i2][i3][i4];
-                                       fontinfo_[i1][i2][i3][i4] = 0;
+                               for (int i4 = 0; i4 < NUM_SIZE; ++i4)
+                                       for (int i5 = 0; i5 < NUM_STYLE; ++i5) {
+                                       delete fontinfo_[i1][i2][i3][i4][i5];
+                                       fontinfo_[i1][i2][i3][i4][i5] = 0;
                                }
 }
 
@@ -242,8 +279,9 @@ FontLoader::~FontLoader()
 
 /////////////////////////////////////////////////
 
+namespace {
 
-static QString makeFontName(QString const & family, QString const & foundry)
+QString makeFontName(QString const & family, QString const & foundry)
 {
        QString res = family;
        if (!foundry.isEmpty())
@@ -252,9 +290,9 @@ static QString makeFontName(QString const & family, QString const & foundry)
 }
 
 
-GuiFontInfo::GuiFontInfo(FontInfo const & f)
-       : metrics(QFont())
+QFont makeQFont(FontInfo const & f)
 {
+       QFont font;
        QString const pat = symbolFamily(f.family());
        if (!pat.isEmpty()) {
                bool ok;
@@ -263,9 +301,9 @@ GuiFontInfo::GuiFontInfo(FontInfo const & f)
                switch (f.family()) {
                case ROMAN_FAMILY: {
                        QString family = makeFontName(toqstr(lyxrc.roman_font_name),
-                               toqstr(lyxrc.roman_font_foundry)); 
+                               toqstr(lyxrc.roman_font_foundry));
                        font.setFamily(family);
-#ifdef Q_WS_MACX
+#ifdef Q_OS_MAC
 #if QT_VERSION >= 0x040300 //&& QT_VERSION < 0x040800
                        // Workaround for a Qt bug, see http://www.lyx.org/trac/ticket/3684
                        // and http://bugreports.qt.nokia.com/browse/QTBUG-11145.
@@ -325,14 +363,20 @@ GuiFontInfo::GuiFontInfo(FontInfo const & f)
 
        LYXERR(Debug::FONT, "XFLD: " << font.rawName());
 
-       font.setPointSizeF(convert<double>(lyxrc.font_sizes[f.size()])
-                              * lyxrc.zoom / 100.0);
+       font.setPointSizeF(f.realSize() * lyxrc.currentZoom / 100.0);
 
        LYXERR(Debug::FONT, "The font has size: " << font.pointSizeF());
 
-       metrics = GuiFontMetrics(font);
+       return font;
 }
 
+} // namespace
+
+
+GuiFontInfo::GuiFontInfo(FontInfo const & f)
+       : font(makeQFont(f)), metrics(font)
+{}
+
 
 bool FontLoader::available(FontInfo const & f)
 {
@@ -341,7 +385,7 @@ bool FontLoader::available(FontInfo const & f)
        static vector<int> cache(NUM_FAMILIES, false);
 
        FontFamily family = f.family();
-#ifdef Q_WS_MACX
+#ifdef Q_OS_MAC
        // Apple ships a font name "Symbol", which has more or less the same
        // glyphs as the original PostScript Symbol font, but it uses a different
        // encoding (see https://en.wikipedia.org/wiki/Symbol_(typeface)#cite_note-2).