Fix harfbuzz font lifecycle in CommonSalLayout

The harfbuzz font is attached to the system font face and
therefore inherits its lifecycle. This means it can be used in
multiple CommonSalLayout objects, so the user data parameter of
hb_face_create_for_tables can't be the layout, but must be the
font.

This moves the special Qt5Font handling into it's own function,
so accessing the switching parameter mbUseQt5 is not needed.

Regression from commit b66a7cbd8491fe436126e11975c360f47ae346ed.

Change-Id: Ic34cc5b60e401562c73b239a58176a59fe4bf9be
Reviewed-on: https://gerrit.libreoffice.org/44398
Reviewed-by: Khaled Hosny <khaledhosny@eglug.org>
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Miklos Vajna <vmiklos@collabora.co.uk>
diff --git a/vcl/inc/CommonSalLayout.hxx b/vcl/inc/CommonSalLayout.hxx
index a8808ba..0947f9e 100644
--- a/vcl/inc/CommonSalLayout.hxx
+++ b/vcl/inc/CommonSalLayout.hxx
@@ -60,7 +60,12 @@ class VCL_DLLPUBLIC CommonSalLayout : public GenericSalLayout
#if ENABLE_QT5
    const bool              mbUseQt5;
    Qt5Font*                mpQFont;

    explicit                CommonSalLayout(const FontSelectPattern &rFSP,
                                            FreetypeFont *pFreetypeFont,
                                            Qt5Font *pFont, bool bUseQt5);
#endif
    void                    InitFromFreetypeFont();
#endif

    void                    ParseFeatures(const OUString& name);
@@ -88,9 +93,6 @@ public:
    explicit                CommonSalLayout(FreetypeFont&);
    const FreetypeFont*     getFreetypeFont() const { return mpFreetypeFont; }
#if ENABLE_QT5
    explicit                CommonSalLayout(const FontSelectPattern &rFSP,
                                            FreetypeFont *pFreetypeFont,
                                            Qt5Font *pFont, bool bUseQt5);
    explicit                CommonSalLayout(Qt5Font&);
    const Qt5Font*          getQt5Font() const { return mpQFont; }
    bool                    useQt5() const { return mbUseQt5; }
diff --git a/vcl/source/gdi/CommonSalLayout.cxx b/vcl/source/gdi/CommonSalLayout.cxx
index 2871b99..9b2d7e4 100644
--- a/vcl/source/gdi/CommonSalLayout.cxx
+++ b/vcl/source/gdi/CommonSalLayout.cxx
@@ -34,14 +34,19 @@
#include <QtGui/QRawFont>
#endif

static hb_blob_t* getFontTable(hb_face_t* /*face*/, hb_tag_t nTableTag, void* pUserData)
static inline void decode_hb_tag( const hb_tag_t nTableTag, char *pTagName )
{
    char pTagName[5];
    pTagName[0] = (char)(nTableTag >> 24);
    pTagName[1] = (char)(nTableTag >> 16);
    pTagName[2] = (char)(nTableTag >>  8);
    pTagName[3] = (char)nTableTag;
    pTagName[4] = 0;
}

static hb_blob_t* getFontTable(hb_face_t* /*face*/, hb_tag_t nTableTag, void* pUserData)
{
    char pTagName[5];
    decode_hb_tag( nTableTag, pTagName );

    sal_uLong nLength = 0;
#if defined(_WIN32)
@@ -67,23 +72,9 @@ static hb_blob_t* getFontTable(hb_face_t* /*face*/, hb_tag_t nTableTag, void* pU
        pFont->GetFontTable(pTagName, pBuffer);
    }
#else
    const char* pBuffer = nullptr;
    CommonSalLayout *pLayout = static_cast<CommonSalLayout*>( pUserData );
#if ENABLE_QT5
    QByteArray aTable;
    if ( pLayout->useQt5() )
    {
        QRawFont aRawFont( QRawFont::fromFont( *pLayout->getQt5Font() ) );
        aTable = aRawFont.fontTable( pTagName );
        pBuffer = reinterpret_cast<const char*>( aTable.data() );
        nLength = aTable.size();
    }
    else
#endif
    {
        pBuffer = reinterpret_cast<const char*>(
            pLayout->getFreetypeFont()->GetTable(pTagName, &nLength) );
    }
    FreetypeFont* pFont = static_cast<FreetypeFont*>( pUserData );
    const char* pBuffer = reinterpret_cast<const char*>(
        pFont->GetTable(pTagName, &nLength) );
#endif

    hb_blob_t* pBlob = nullptr;
@@ -98,6 +89,25 @@ static hb_blob_t* getFontTable(hb_face_t* /*face*/, hb_tag_t nTableTag, void* pU
    return pBlob;
}

#if ENABLE_QT5
static hb_blob_t* getFontTable_Qt5Font(hb_face_t* /*face*/, hb_tag_t nTableTag, void* pUserData)
{
    char pTagName[5];
    decode_hb_tag( nTableTag, pTagName );

    Qt5Font *pFont = static_cast<Qt5Font*>( pUserData );
    QRawFont aRawFont( QRawFont::fromFont( *pFont ) );
    QByteArray aTable = aRawFont.fontTable( pTagName );
    const sal_uLong nLength = aTable.size();

    hb_blob_t* pBlob = nullptr;
    if (nLength > 0)
        pBlob = hb_blob_create(reinterpret_cast<const char*>( aTable.data() ),
                               nLength, HB_MEMORY_MODE_READONLY, nullptr, nullptr);
    return pBlob;
}
#endif

static hb_font_t* createHbFont(hb_face_t* pHbFace)
{
    hb_font_t* pHbFont = hb_font_create(pHbFace);
@@ -254,6 +264,17 @@ CommonSalLayout::CommonSalLayout(const CoreTextStyle& rCoreTextStyle)

#else

void CommonSalLayout::InitFromFreetypeFont()
{
    mpHbFont = mpFreetypeFont->GetHbFont();
    if (!mpHbFont)
    {
        hb_face_t* pHbFace = hb_face_create_for_tables(getFontTable, mpFreetypeFont, nullptr);
        mpHbFont = createHbFont(pHbFace);
        mpFreetypeFont->SetHbFont(mpHbFont);
    }
}

#if ENABLE_QT5
CommonSalLayout::CommonSalLayout(const FontSelectPattern &rFSP,
                                 FreetypeFont *pFreetypeFont,
@@ -265,18 +286,21 @@ CommonSalLayout::CommonSalLayout(const FontSelectPattern &rFSP,
    , mpVertGlyphs(nullptr)
{
    if (mbUseQt5)
        mpHbFont = mpQFont->GetHbFont();
    else
        mpHbFont = mpFreetypeFont->GetHbFont();
    if (!mpHbFont)
    {
        hb_face_t* pHbFace = hb_face_create_for_tables(getFontTable, this, nullptr);

        mpHbFont = createHbFont(pHbFace);
        if (mbUseQt5)
        assert( pQt5Font && !pFreetypeFont );
        mpHbFont = mpQFont->GetHbFont();
        if (!mpHbFont)
        {
            hb_face_t* pHbFace = hb_face_create_for_tables(
                getFontTable_Qt5Font, pQt5Font, nullptr);
            mpHbFont = createHbFont(pHbFace);
            mpQFont->SetHbFont(mpHbFont);
        else
            mpFreetypeFont->SetHbFont(mpHbFont);
        }
    }
    else
    {
        assert( !pQt5Font && pFreetypeFont );
        InitFromFreetypeFont();
    }
}

@@ -299,13 +323,7 @@ CommonSalLayout::CommonSalLayout(FreetypeFont& rFreetypeFont)
    , mpFreetypeFont(&rFreetypeFont)
    , mpVertGlyphs(nullptr)
{
    mpHbFont = mpFreetypeFont->GetHbFont();
    if (!mpHbFont)
    {
        hb_face_t* pHbFace = hb_face_create_for_tables(getFontTable, this, nullptr);
        mpHbFont = createHbFont(pHbFace);
        mpFreetypeFont->SetHbFont(mpHbFont);
    }
    InitFromFreetypeFont();
}

#endif