Bug 114618

Summary: Monospace fonts are not working in QtWebKit 2.3.0
Product: WebKit Reporter: Dmitry Shachnev <mitya57>
Component: WebKit QtAssignee: Nobody <webkit-unassigned>
Status: RESOLVED INVALID    
Severity: Major CC: allan.jensen, anonymous, thiago
Priority: P2    
Version: 528+ (Nightly build)   
Hardware: PC   
OS: Linux   
Bug Depends on:    
Bug Blocks: 88186    

Description Dmitry Shachnev 2013-04-15 04:36:13 PDT
With QtWebKit 2.3.0, any QtWebKit-based browser displays "font-family: monospace" blocks using non-monospace font. I can reproduce this Ubuntu 13.04, which has this QtWebKit version. User agent version is 537.20.

This didn't happen with QtWebKit 2.2 and does not happen with Qt 5.

- A screenshot: https://ubuntuone.com/6JmFjyEcqrxkSZ8CDS31N1.
- A minimal test case: https://gist.github.com/mitya57/5312601.

Severity Major because it breaks a lot of web sites and other apps (at least ReText is affected).
Comment 1 Allan Sandfeld Jensen 2013-04-16 14:45:35 PDT
It works fine here. Perhaps you are missing the default monospace font, so it falls back to sans-serif?

Note, that the web-inspector is also rendered using webkit and correctly uses a monospace font.
Comment 2 Allan Sandfeld Jensen 2013-04-16 15:05:41 PDT
The default monospace font can be set by the application, but if your test example is failing it means the default default is not working in your case.

The default fonts in qtwebkit is set using this code:

    QFont defaultFont;
    defaultFont.setStyleHint(QFont::Serif);
    d->fontFamilies.insert(QWebSettings::StandardFont, defaultFont.defaultFamily());
    d->fontFamilies.insert(QWebSettings::SerifFont, defaultFont.defaultFamily());

    defaultFont.setStyleHint(QFont::Fantasy);
    d->fontFamilies.insert(QWebSettings::FantasyFont, defaultFont.defaultFamily());

    defaultFont.setStyleHint(QFont::Cursive);
    d->fontFamilies.insert(QWebSettings::CursiveFont, defaultFont.defaultFamily());

    defaultFont.setStyleHint(QFont::SansSerif);
    d->fontFamilies.insert(QWebSettings::SansSerifFont, defaultFont.defaultFamily());

    defaultFont.setStyleHint(QFont::Monospace);
    d->fontFamilies.insert(QWebSettings::FixedFont, defaultFont.defaultFamily());

Could you test what font you get on your machine with a QFont::setStyleHint(QFont::Monospace) and QFont::defaultFamily() ? 

It sounds like an issue in Qt, fontconfig, or probably their configuration on Ubuntu 13.04.
Comment 3 Dmitry Shachnev 2013-04-17 11:24:21 PDT
defaultFamily() of QFont::Monospace is "Courier New", but that font doesn't exist in the system, so probably this is the issue.

But, for some reason, monospace font works in Web Inspector and other widgets. In example, this works OK (and the font is really monospace):

QFont font;
QTextEdit edit;
font.setFamily ("monospace");
edit.setFont (font);

I tested this on Kubuntu 13.04 live image, will test on my home PC tomorrow.
Comment 4 Dmitry Shachnev 2013-04-17 11:36:49 PDT
Oh, I just found this in qfont_x11.cpp:

QString QFont::defaultFamily() const
{
...
    case QFont::Monospace:
        return QString::fromLatin1("Courier New");
...
}

If (for some reasons) Qt doesn't use font from system settings, maybe it'll make sense to use "monospace" instead of "Courier New", that works in GNOME, KDE and Windows (at least).
Comment 5 Allan Sandfeld Jensen 2013-04-18 02:35:00 PDT
(In reply to comment #4)
> Oh, I just found this in qfont_x11.cpp:
> 
> QString QFont::defaultFamily() const
> {
> ...
>     case QFont::Monospace:
>         return QString::fromLatin1("Courier New");
> ...
> }
> 
> If (for some reasons) Qt doesn't use font from system settings, maybe it'll make sense to use "monospace" instead of "Courier New", that works in GNOME, KDE and Windows (at least).

Yes. Is that the same in the official Qt 4.8 release? If so, I would suggest opening a bug report on it at qt-project.org
Comment 6 Dmitry Shachnev 2013-04-18 09:25:57 PDT
Probably this should fix the issue:

--- a/src/plugins/platforms/fontdatabases/fontconfig/qfontconfigdatabase.cpp
+++ b/src/plugins/platforms/fontdatabases/fontconfig/qfontconfigdatabase.cpp
@@ -285,6 +285,7 @@ static const char *getFcFamilyForStyleHint(const QFont::StyleHint style)
         stylehint = "serif";
         break;
     case QFont::TypeWriter:
+    case QFont::Monospace:
         stylehint = "monospace";
         break;
     default:

Not tested yet, will test and submit to codereview later.
Comment 7 Dmitry Shachnev 2013-04-20 02:50:47 PDT
(In reply to comment #6)
> Probably this should fix the issue:

No, it didn’t help :(

I have now reported https://bugreports.qt-project.org/browse/QTBUG-30785.
Comment 8 Dmitry Shachnev 2013-04-22 07:37:58 PDT
After some debugging, I would say that this is *not* a Qt issue. Though there is no “Courier New” font in my system, FontConfig considers it as an alias for “Liberation Mono” font, using the rule defined in /etc/fonts/conf.d/30-metric-aliases.conf.

If I create a QFont with family “Courier New” and apply it to a widget (i.e. QTextEdit), it will work properly. Even more, if I create an HTML block with “font-family: 'Courier New'” style, QtWebKit *will render it properly*.

So, I believe it is a bug in QtWebKit. To be precise, QtWebKit is able to render that font properly (as we can see in the web inspector), but doesn’t do that.
Comment 9 Dmitry Shachnev 2013-04-23 07:46:49 PDT
Sorry for the noise, I’m going to correct an error in my previous comment:

> Even more, if I create an HTML block with “font-family: 'Courier New'” style, QtWebKit *will render it properly*.

That’s not true. QTextEdit will render it, but QtWebKit won’t (which again indicates that it is a bug in QtWebKit).
Comment 10 Thiago Macieira 2014-02-20 11:41:54 PST
Allan, do you have any further ideas? I can confirm this bug.
Comment 11 Alexey Proskuryakov 2014-03-19 10:33:32 PDT
Marking INVALID, as Qt port no longer exists in WebKit trunk.