Skip to content
Permalink
Browse files

Move loading of test fonts to QgsFontUtils

- Always add testdata.qrc to desktop and server, but only auto-load Roman and Bold if built in debug mode
- Test fonts can now be loaded directly, after startup
- Add sip binding and unit tests
  • Loading branch information
dakcarto committed Feb 19, 2014
1 parent be83e7c commit 2d4ecbac0091b4b107fc381b4bee77cd3e2d659f
@@ -33,4 +33,12 @@ class QgsFontUtils
*/
static bool updateFontViaStyle( QFont& f, const QString& fontstyle, bool fallback = false );

/** Loads standard test fonts from filesystem or qrc resource
* @param loadstyles List of styles to load, e.g. Roman, Oblique, Bold, Bold Oblique
* @returns Whether any font was loaded
* @note Done by default on debug app/server startup to ensure fonts available for unit tests (Roman and Bold)
* @note Added in QGIS 2.1
*/
static bool loadStandardTestFonts( QStringList loadstyles );

};
@@ -373,12 +373,9 @@ SET(IMAGE_RCCS ../../images/images.qrc)

QT4_ADD_RESOURCES(IMAGE_RCC_SRCS ${IMAGE_RCCS})

SET(TEST_RCC_SRCS)
IF (ENABLE_TESTS)
# add test resources, e.g. standard test font
SET(TEST_RCCS ../../tests/testdata/testdata.qrc)
QT4_ADD_RESOURCES(TEST_RCC_SRCS ${TEST_RCCS})
ENDIF (ENABLE_TESTS)
# add test resources, e.g. standard test font
SET(TEST_RCCS ../../tests/testdata/testdata.qrc)
QT4_ADD_RESOURCES(TEST_RCC_SRCS ${TEST_RCCS})

QT4_WRAP_CPP(QGIS_APP_MOC_SRCS ${QGIS_APP_MOC_HDRS})

@@ -35,6 +35,7 @@
#include <QMessageBox>

#include "qgscustomization.h"
#include "qgsfontutils.h"
#include "qgspluginregistry.h"
#include "qgsmessagelog.h"
#include "qgspythonrunner.h"
@@ -736,14 +737,9 @@ int main( int argc, char *argv[] )
}
}

// load standard test font from testdata.qrc (for unit tests)
QFile testFont( ":/testdata/font/QGIS-Vera/QGIS-Vera.ttf" );
if ( testFont.open( QIODevice::ReadOnly ) )
{
int fontID = QFontDatabase::addApplicationFontFromData( testFont.readAll() );
Q_UNUSED( fontID );
QgsDebugMsg( QString( "Test font %1loaded from testdata.qrc" ).arg( fontID != -1 ? "" : "NOT " ) );
} // else app wasn't built with ENABLE_TESTS
#ifdef QGISDEBUG
QgsFontUtils::loadStandardTestFonts( QStringList() << "Roman" << "Bold" );
#endif

// Set the application style. If it's not set QT will use the platform style except on Windows
// as it looks really ugly so we use QPlastiqueStyle.
@@ -15,7 +15,11 @@

#include "qgsfontutils.h"

#include "qgsapplication.h"
#include "qgslogger.h"

#include <QApplication>
#include <QFile>
#include <QFont>
#include <QFontDatabase>
#include <QFontInfo>
@@ -191,3 +195,67 @@ bool QgsFontUtils::updateFontViaStyle( QFont& f, const QString& fontstyle, bool

return false;
}

bool QgsFontUtils::loadStandardTestFonts( QStringList loadstyles )
{
// load standard test font from filesystem or testdata.qrc (for unit tests and general testing)
QFontDatabase fontDB;
bool fontsLoaded = false;

QString fontFamily( "QGIS Vera Sans" );
QMap<QString, QString> fontStyles;
fontStyles.insert( "Roman", "QGIS-Vera/QGIS-Vera.ttf" );
fontStyles.insert( "Oblique", "QGIS-Vera/QGIS-VeraIt.ttf" );
fontStyles.insert( "Bold", "QGIS-Vera/QGIS-VeraBd.ttf" );
fontStyles.insert( "Bold Oblique", "QGIS-Vera/QGIS-VeraBI.ttf" );

QMap<QString, QString>::const_iterator f = fontStyles.constBegin();
for ( ; f != fontStyles.constEnd(); ++f )
{
QString fontstyle( f.key() );
QString fontpath( f.value() );
if ( ! ( loadstyles.contains( fontstyle ) || loadstyles.contains( "All" ) ) )
{
continue;
}
QString familyStyle = QString( "%1 %2" ).arg( fontFamily ).arg( fontstyle );

if ( fontFamilyOnSystem( fontFamily )
&& fontDB.styles( fontFamily ).contains( fontstyle ) )
{
fontsLoaded = ( fontsLoaded || false );
QgsDebugMsg( QString( "Test font '%1' already available" ).arg( familyStyle ) );
}
else
{
bool loaded = false;
if ( QgsApplication::isRunningFromBuildDir() )
{
// workaround for bugs with Qt 4.8.5 (other versions?) on Mac 10.9, where fonts
// from qrc resources load but fail to work and default font is substituted [LS]:
// https://bugreports.qt-project.org/browse/QTBUG-30917
// https://bugreports.qt-project.org/browse/QTBUG-32789
QString fontPath( QgsApplication::buildSourcePath() + "/tests/testdata/font/" + fontpath );
int fontID = QFontDatabase::addApplicationFont( fontPath );
loaded = ( fontID != -1 );
fontsLoaded = ( fontsLoaded || loaded );
QgsDebugMsg( QString( "Test font '%1' %2 from filesystem")
.arg( familyStyle ).arg( loaded ? "loaded" : "FAILED to load" ) );
}
else
{
QFile fontResource( ":/testdata/font/" + fontpath );
if ( fontResource.open( QIODevice::ReadOnly ) )
{
int fontID = QFontDatabase::addApplicationFontFromData( fontResource.readAll() );
loaded = ( fontID != -1 );
fontsLoaded = ( fontsLoaded || loaded );
}
QgsDebugMsg( QString( "Test font '%1' %2 from testdata.qrc")
.arg( familyStyle ).arg( loaded ? "loaded" : "FAILED to load" ) );
}
}
}

return fontsLoaded;
}
@@ -51,6 +51,14 @@ class CORE_EXPORT QgsFontUtils
*/
static bool updateFontViaStyle( QFont& f, const QString& fontstyle, bool fallback = false );

/** Loads standard test fonts from filesystem or qrc resource
* @param loadstyles List of styles to load, e.g. All, Roman, Oblique, Bold, Bold Oblique
* @returns Whether any font was loaded
* @note Done by default on debug app/server startup to ensure fonts available for unit tests (Roman and Bold)
* @note Added in QGIS 2.1
*/
static bool loadStandardTestFonts( QStringList loadstyles );

};

#endif // QGSFONTUTILS_H
@@ -67,12 +67,9 @@ QT4_WRAP_CPP (qgis_mapserv_MOC_SRCS ${qgis_mapserv_MOC_HDRS})

QT4_ADD_RESOURCES(qgis_mapserv_RCC_SRCS ${qgis_mapserv_RCCS})

SET(qgis_mapserv_TESTRCC_SRCS)
IF (ENABLE_TESTS)
# add test resources, e.g. standard test font
SET(qgis_mapserv_TESTRCCS ../../tests/testdata/testdata.qrc)
QT4_ADD_RESOURCES(qgis_mapserv_TESTRCC_SRCS ${qgis_mapserv_TESTRCCS})
ENDIF(ENABLE_TESTS)
# add test resources, e.g. standard test font
SET(qgis_mapserv_TESTRCCS ../../tests/testdata/testdata.qrc)
QT4_ADD_RESOURCES(qgis_mapserv_TESTRCC_SRCS ${qgis_mapserv_TESTRCCS})

ADD_EXECUTABLE(qgis_mapserv.fcgi
${qgis_mapserv_SRCS}
@@ -20,6 +20,7 @@ map service syntax for SOAP/HTTP POST
#include "qgsapplication.h"
#include "qgscapabilitiescache.h"
#include "qgsconfigcache.h"
#include "qgsfontutils.h"
#include "qgsgetrequesthandler.h"
#include "qgspostrequesthandler.h"
#include "qgssoaprequesthandler.h"
@@ -248,48 +249,12 @@ int main( int argc, char * argv[] )
theMapRenderer->setLabelingEngine( new QgsPalLabeling() );

#ifdef QGSMSDEBUG
// load standard test font from filesystem or testdata.qrc (for unit tests)
bool testFontLoaded = false;
QFontDatabase fontDB;
QFont testFont = fontDB.font( "QGIS Vera Sans", "Roman", 12 );
if ( testFont.family().startsWith( "QGIS", Qt::CaseInsensitive ) )
{
testFontLoaded = true;
QgsDebugMsg( "Test font already available" );
}
else
{
QString fontFromWhere( "" );
if ( QgsApplication::isRunningFromBuildDir() )
{
// [LS] workaround for serious bugs on Mac 10.9, where fonts from qrc resources fail:
// https://bugreports.qt-project.org/browse/QTBUG-30917
// https://bugreports.qt-project.org/browse/QTBUG-32789
QString testFont( QgsApplication::buildSourcePath() + "/tests/testdata/font/QGIS-Vera/QGIS-Vera.ttf" );
int fontID = QFontDatabase::addApplicationFont( testFont );
testFontLoaded = ( fontID != -1 );
fontFromWhere = testFontLoaded ? "filesystem" : "";
}
else
{
QFile testFont( ":/testdata/font/QGIS-Vera/QGIS-Vera.ttf" );
if ( testFont.open( QIODevice::ReadOnly ) )
{
int fontID = QFontDatabase::addApplicationFontFromData( testFont.readAll() );
testFontLoaded = ( fontID != -1 );
} // else app wasn't built with ENABLE_TESTS or not GUI app
fontFromWhere = testFontLoaded ? "testdata.qrc" : "";
}
QgsDebugMsg( QString( "Test font %1loaded from %2 on startup" ).arg( testFontLoaded ? "" : "NOT " ).arg( fontFromWhere ) );
}
QgsFontUtils::loadStandardTestFonts( QStringList() << "Roman" << "Bold" );
#endif

while ( fcgi_accept() >= 0 )
{
printRequestInfos(); //print request infos if in debug mode
#ifdef QGSMSDEBUG
QgsDebugMsg( QString( "Test font %1loaded" ).arg( testFontLoaded ? "" : "NOT " ) );
#endif

//use QgsGetRequestHandler in case of HTTP GET and QgsSOAPRequestHandler in case of HTTP POST
QgsRequestHandler* theRequestHandler = 0;
@@ -1,6 +1,7 @@
INCLUDE(UsePythonTest)
ADD_PYTHON_TEST(PyQgsApplication test_qgsapplication.py)
ADD_PYTHON_TEST(PyQgsLocalServer test_qgis_local_server.py)
ADD_PYTHON_TEST(PyQgsFontUtils test_qgsfontutils.py)
ADD_PYTHON_TEST(PyQgsFeature test_qgsfeature.py)
ADD_PYTHON_TEST(PyQgsFeatureIterator test_qgsfeatureiterator.py)
ADD_PYTHON_TEST(PyQgsGeometry test_qgsgeometry.py)
@@ -0,0 +1,66 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for core QgsFontUtils class
From build dir: ctest -R PyQgsFontUtils -V
.. note:: This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
"""
__author__ = 'Larry Shaffer'
__date__ = '2014/02/19'
__copyright__ = 'Copyright 2014, The QGIS Project'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'


from PyQt4.QtGui import *

from qgis.core import (
QgsFontUtils
)

from utilities import (
TestCase,
getQgisTestApp,
unittest,
expectedFailure,
unitTestDataPath,
)

QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()


class TestQgsFontUtils(TestCase):

@classmethod
def setUpClass(cls):
cls._family = 'QGIS Vera Sans'
cls._fontdb = QFontDatabase()
""":type : QFontDatabase"""

def test_loading_specific_test_font(self):
QgsFontUtils.loadStandardTestFonts(['Roman'])
msg = self._family + ' Roman test font styles could not be loaded'
assert self._has_style(self._family, 'Roman'), msg

def test_loading_all_test_fonts(self):
QgsFontUtils.loadStandardTestFonts(['All'])
# styles = ''
# for style in self._fontdb.styles(self._family):
# styles += ' ' + style
# print self._family + ' styles:' + styles
msg = self._family + ' test font styles could not be loaded'
res = (self._has_style(self._family, 'Roman')
and self._has_style(self._family, 'Oblique')
and self._has_style(self._family, 'Bold')
and self._has_style(self._family, 'Bold Oblique'))
assert res, msg

def _has_style(self, family, style):
return (family in self._fontdb.families()
and style in self._fontdb.styles(family))

if __name__ == '__main__':
unittest.main()

0 comments on commit 2d4ecba

Please sign in to comment.
You can’t perform that action at this time.