[PySide] Support for 5.2

Sean Fisk sean at seanfisk.com
Tue Feb 18 23:38:39 CET 2014


Hi Srini,

Here is our py2app configuration dictionary:

'py2app': {
    # ...
    'resources': [
        # Part of a workaround for
        # <https://bugreports.qt-project.org/browse/QTBUG-30917>
        'resources/fonts', # project directory which contains our fonts
    ]
}

Here is our module responsible for font loading.

# fonts.py"""Initialize fonts embedded using the Qt resource system."""
import sysimport loggingfrom glob import iglobimport platform
from PySide import QtCore, QtGui

logger = logging.getLogger(__name__)
def _load_font(font_path):
    """Load a font from a path.

    :param font_path: path to the font; can be a filesystem or resource path
    :type font_path: :class:`str`
    """
    # It's a tough deciding what to do when a font can't be loaded. If it
    # can't, it's an error because we are writing the application to work with
    # our own custom fonts. However, custom fonts are not necessary for the
    # application to achieve its purpose. Because the application will still be
    # useful without custom fonts loaded, we are choosing to report this as an
    # error but still let the program continue running.
    font_id = QtGui.QFontDatabase.addApplicationFont(font_path)
    if font_id == -1:
        logger.error('Could not load font from path: ' + font_path)
        return
    # It's possible that a font can be loaded correctly (i.e., it has a
    # font_id) but cannot be used correctly. In my opinion, this
    # constitutes a bug in Qt. The workaround is to add this extra check to
    # make sure that font can /actually/ be used by calling exactMatch().
    for font_family in QtGui.QFontDatabase.applicationFontFamilies(font_id):
        if not QtGui.QFont(font_family).exactMatch():
            logger.error(
                'Font family is loaded but cannot be used: ' + font_family)
def init():
    """Initialize fonts."""
    if platform.system() == 'Darwin':
        # Workaround for <https://bugreports.qt-project.org/browse/QTBUG-30917>

        # py2app sets sys.frozen = 'macosx_app' when bundled.
        if hasattr(sys, 'frozen'):
            # When the app bundle is loaded, the current directory will be
            # 'OurApp.app/Content/Resources'. There should be a fonts/
            # directory there.
            fonts_prefix = 'fonts'
        else:
            # For development only
            fonts_prefix = 'resources/fonts'
        for font_path in iglob(fonts_prefix + '/*.[to]tf'):
            _load_font(font_path)
    else:
        font_dir_resource = QtCore.QResource(':/fonts')
        font_dir_resource_path = font_dir_resource.absoluteFilePath()
        for font_filename in font_dir_resource.children():
            # DON'T use `os.path.join()' here because Qt always uses UNIX-style
            # paths. On Windows `os.sep' is '\\'.
            font_resource_path = '/'.join(
                [font_dir_resource_path, font_filename])
            _load_font(font_resource_path)

>From our main, we simply call:

from our_package import fonts
fonts.init()

Hope this helps!


--
Sean Fisk


On Sun, Feb 16, 2014 at 7:55 PM, Sean Fisk <sean at seanfisk.com> wrote:

> Hi Srini,
>
> I am waiting for members of my team to test out my solution to make sure I
> didn't break anything on platforms other than Mac OS X. That should happen
> tomorrow, and I will be able to send you the snippet. I don't want to give
> you broken code :)
>
> Cheers,
>
>
> --
> Sean Fisk
>
>
> On Fri, Feb 14, 2014 at 3:54 PM, Srini Kommoori <vasure at gmail.com> wrote:
>
>> Sean,
>>
>> Thanks for reaching out. I am also in the process of building
>> QFontDatabase manually.
>>
>> Yes. It would great if you could share your code snippet on how you are
>> handling this.
>>
>> thanks,
>> -Srini
>>
>>
>> On Fri, Feb 14, 2014 at 11:01 AM, Sean Fisk <sean at seanfisk.com> wrote:
>>
>>> We are having an issue where embedded fonts (i.e., from a QRC file) are
>>> not loading properly on OS X Mavericks. Srini, I can't tell for certain
>>> based on your description, but it might be the issue you are having too.
>>>
>>> We are using py2app <http://pythonhosted.org/py2app/> to distribute our
>>> application. Although this is not a solution to the bug, we were able to
>>> work around the issue by including the fonts as data files with py2app.
>>> This works because it loads the font from an "external" file instead of an
>>> embedded resource.
>>>
>>> If that is an option for you and you would like to adapt our code, I
>>> would be happy to provide it.
>>>
>>> Cheers,
>>>
>>>
>>> --
>>> Sean Fisk
>>>
>>>
>>> On Wed, Feb 5, 2014 at 3:10 PM, Srini Kommoori <vasure at gmail.com> wrote:
>>>
>>>> Thanks John. I got that from search. My issue is not really with Lucida
>>>> Grande support. I have custom font which is having issues. I have ton of
>>>> these messages on the console.
>>>>
>>>> 2014-02-05 08:05:36.498 Python[25536:1107] CoreText performance note:
>>>> Client called CTFontCreateWithName() using name "Open Sans" and got font
>>>> with PostScript name "OpenSans". For best performance, only use PostScript
>>>> names when calling this API.
>>>>
>>>> Seems like https://bugreports.qt-project.org/browse/QTBUG-32789 is
>>>> fixed. We need to merge the fix or wait for 4.8.6 to be released.
>>>>
>>>>
>>>>
>>>>
>>>> On Wed, Feb 5, 2014 at 10:10 AM, John Ehresman <jpe at wingware.com>wrote:
>>>>
>>>>> On 2/4/14, 10:38 PM, Srini Kommoori wrote:
>>>>>
>>>>>> Is anyone working on porting to Qt5.2?
>>>>>>
>>>>>> I just updated my mac to Mavericks and fonts are all messed up. I see
>>>>>> that all of them are resolved in Qt5.2.
>>>>>>
>>>>>
>>>>> PySide only supports Qt4 right now.  The workaround for the Mavericks
>>>>> font problem (or at least a problem with the system font) is to use:
>>>>>
>>>>>       QFont.insertSubstitution(".Lucida Grande UI", "Lucida Grande")
>>>>>
>>>>> There's also another 4.8 release in the works.
>>>>>
>>>>> Cheers,
>>>>>
>>>>> John
>>>>>
>>>>>
>>>>
>>>> _______________________________________________
>>>> PySide mailing list
>>>> PySide at qt-project.org
>>>> http://lists.qt-project.org/mailman/listinfo/pyside
>>>>
>>>>
>>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20140218/c5ed0cd8/attachment.html>


More information about the PySide mailing list