Saturday, June 27, 2015

Major oops! revealed in windows test

So I had intended to release PPQT2 on Windows 7 64-bit only. But a query from an alpha user made me rethink that, and I decided better to build and release it on Windows 7 32-bit, which I trust and hope will work also on a 64-bit system. Unlike Linux, where I have to create versions for both widths.

Anyway, that meant setting up a 32-bit Win7 system. A couple weeks ago I went on eBay and bought a Win7 Professional 32-bit DVD. And installed it in a VM and provisioned it for development -- a process I meant to blog about today. But...

When I got to the point where I could run PPQT from source, I did so, and did a superficial checkout of various features. And immediately noticed that the Edit panel appeared to be using a non-monospaced font, probably Arial or whatever the Win7 default font is. Opened the preferences dialog, looked at the Edit Font choice popup menu. There were my expected Liberation Mono and Cousine font choices, along with several mono fonts from the local environment, like Consolas and Courier New. But selecting either Liberation Mono or Cousine from the menu and clicking Apply had no effect. The Edit panel remained showing Arial. Selecting a local font like Consolas did have immediate effect, so the whole mechanism of choosing and applying a font was working. But the two preferred fonts were not.

Hmmmm.

They were working perfectly well on Mac OS and Linux.

Weren't they?

Well, they seem to work.

To review, these two fonts are good looking mono fonts that both have a very wide repertoire of Unicode glyphs. I want them available to all PPQT users. To make that happen, I carefully built a resource file naming them, along with some minor graphic icon pngs, and compiled it with pyrcc5 into a Python file, resources.py. As a result, this code at the head of fonts.py:

_FONT_DB = QFontDatabase()
_FONT_DB.addApplicationFont(':/liberation_mono.ttf')
_FONT_DB.addApplicationFont(':/cousine.ttf')

...should prepare a local font database that includes all locally-available fonts plus those two, loaded from the Qt resources. They should then show up in the following code which prepares the list of families from which the "choose an edit font" popup is built.

# Return a list of available monospaced families for use in the preferences
# dialog. Because for some stupid reason the font database refuses to
# acknowledge that liberation mono and cousine are in fact, monospaced,
# insert those names too.

def list_of_good_families():
    selection = _FONT_DB.families() # all known family names
    short_list = [family for family in selection if _FONT_DB.isFixedPitch(family) ]
    short_list.insert(0,'Liberation Mono')
    short_list.insert(0,'Cousine')
    return short_list

Read the comments... I think I may know what that "stupid reason" was, now. Because, although those two family names were in the menu, choosing them had no effect. The Qt font database is carefully designed so that it always returns a font when you ask for one, falling back to some default if it can't give you what you ask for. Clearly in Windows, it couldn't provide the "Cousine" family and was falling back to the system default.

But not in Mac OS or Linux...

But you know, in both those dev systems, I believe I had installed both fonts locally...

Could it be that the font database had never been supplying those fonts from the resources module?

OK, so where did I import that module? I apply Wing IDE's "search in files" widget to look for "resources" in all project files.

I never did import it. I built the ****ing resources file and never imported it to the running app!

Added one line, import resources, to PPQT2.py and reran the app. Bingo! I can now set Liberation Mono or Cousine as Edit fonts!

It's a bit of a mystery how I managed to get along so far without encountering any problem from the missing resources. But I don't care. Just glad to have found it before shipping.

No comments: