Skip to content

Commit 2d4ecba

Browse files
committedFeb 19, 2014
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
1 parent be83e7c commit 2d4ecba

File tree

9 files changed

+163
-57
lines changed

9 files changed

+163
-57
lines changed
 

‎python/core/qgsfontutils.sip

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,12 @@ class QgsFontUtils
3333
*/
3434
static bool updateFontViaStyle( QFont& f, const QString& fontstyle, bool fallback = false );
3535

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

‎src/app/CMakeLists.txt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -373,12 +373,9 @@ SET(IMAGE_RCCS ../../images/images.qrc)
373373

374374
QT4_ADD_RESOURCES(IMAGE_RCC_SRCS ${IMAGE_RCCS})
375375

376-
SET(TEST_RCC_SRCS)
377-
IF (ENABLE_TESTS)
378-
# add test resources, e.g. standard test font
379-
SET(TEST_RCCS ../../tests/testdata/testdata.qrc)
380-
QT4_ADD_RESOURCES(TEST_RCC_SRCS ${TEST_RCCS})
381-
ENDIF (ENABLE_TESTS)
376+
# add test resources, e.g. standard test font
377+
SET(TEST_RCCS ../../tests/testdata/testdata.qrc)
378+
QT4_ADD_RESOURCES(TEST_RCC_SRCS ${TEST_RCCS})
382379

383380
QT4_WRAP_CPP(QGIS_APP_MOC_SRCS ${QGIS_APP_MOC_HDRS})
384381

‎src/app/main.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include <QMessageBox>
3636

3737
#include "qgscustomization.h"
38+
#include "qgsfontutils.h"
3839
#include "qgspluginregistry.h"
3940
#include "qgsmessagelog.h"
4041
#include "qgspythonrunner.h"
@@ -736,14 +737,9 @@ int main( int argc, char *argv[] )
736737
}
737738
}
738739

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

748744
// Set the application style. If it's not set QT will use the platform style except on Windows
749745
// as it looks really ugly so we use QPlastiqueStyle.

‎src/core/qgsfontutils.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515

1616
#include "qgsfontutils.h"
1717

18+
#include "qgsapplication.h"
19+
#include "qgslogger.h"
20+
1821
#include <QApplication>
22+
#include <QFile>
1923
#include <QFont>
2024
#include <QFontDatabase>
2125
#include <QFontInfo>
@@ -191,3 +195,67 @@ bool QgsFontUtils::updateFontViaStyle( QFont& f, const QString& fontstyle, bool
191195

192196
return false;
193197
}
198+
199+
bool QgsFontUtils::loadStandardTestFonts( QStringList loadstyles )
200+
{
201+
// load standard test font from filesystem or testdata.qrc (for unit tests and general testing)
202+
QFontDatabase fontDB;
203+
bool fontsLoaded = false;
204+
205+
QString fontFamily( "QGIS Vera Sans" );
206+
QMap<QString, QString> fontStyles;
207+
fontStyles.insert( "Roman", "QGIS-Vera/QGIS-Vera.ttf" );
208+
fontStyles.insert( "Oblique", "QGIS-Vera/QGIS-VeraIt.ttf" );
209+
fontStyles.insert( "Bold", "QGIS-Vera/QGIS-VeraBd.ttf" );
210+
fontStyles.insert( "Bold Oblique", "QGIS-Vera/QGIS-VeraBI.ttf" );
211+
212+
QMap<QString, QString>::const_iterator f = fontStyles.constBegin();
213+
for ( ; f != fontStyles.constEnd(); ++f )
214+
{
215+
QString fontstyle( f.key() );
216+
QString fontpath( f.value() );
217+
if ( ! ( loadstyles.contains( fontstyle ) || loadstyles.contains( "All" ) ) )
218+
{
219+
continue;
220+
}
221+
QString familyStyle = QString( "%1 %2" ).arg( fontFamily ).arg( fontstyle );
222+
223+
if ( fontFamilyOnSystem( fontFamily )
224+
&& fontDB.styles( fontFamily ).contains( fontstyle ) )
225+
{
226+
fontsLoaded = ( fontsLoaded || false );
227+
QgsDebugMsg( QString( "Test font '%1' already available" ).arg( familyStyle ) );
228+
}
229+
else
230+
{
231+
bool loaded = false;
232+
if ( QgsApplication::isRunningFromBuildDir() )
233+
{
234+
// workaround for bugs with Qt 4.8.5 (other versions?) on Mac 10.9, where fonts
235+
// from qrc resources load but fail to work and default font is substituted [LS]:
236+
// https://bugreports.qt-project.org/browse/QTBUG-30917
237+
// https://bugreports.qt-project.org/browse/QTBUG-32789
238+
QString fontPath( QgsApplication::buildSourcePath() + "/tests/testdata/font/" + fontpath );
239+
int fontID = QFontDatabase::addApplicationFont( fontPath );
240+
loaded = ( fontID != -1 );
241+
fontsLoaded = ( fontsLoaded || loaded );
242+
QgsDebugMsg( QString( "Test font '%1' %2 from filesystem")
243+
.arg( familyStyle ).arg( loaded ? "loaded" : "FAILED to load" ) );
244+
}
245+
else
246+
{
247+
QFile fontResource( ":/testdata/font/" + fontpath );
248+
if ( fontResource.open( QIODevice::ReadOnly ) )
249+
{
250+
int fontID = QFontDatabase::addApplicationFontFromData( fontResource.readAll() );
251+
loaded = ( fontID != -1 );
252+
fontsLoaded = ( fontsLoaded || loaded );
253+
}
254+
QgsDebugMsg( QString( "Test font '%1' %2 from testdata.qrc")
255+
.arg( familyStyle ).arg( loaded ? "loaded" : "FAILED to load" ) );
256+
}
257+
}
258+
}
259+
260+
return fontsLoaded;
261+
}

‎src/core/qgsfontutils.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ class CORE_EXPORT QgsFontUtils
5151
*/
5252
static bool updateFontViaStyle( QFont& f, const QString& fontstyle, bool fallback = false );
5353

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

5664
#endif // QGSFONTUTILS_H

‎src/mapserver/CMakeLists.txt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,9 @@ QT4_WRAP_CPP (qgis_mapserv_MOC_SRCS ${qgis_mapserv_MOC_HDRS})
6767

6868
QT4_ADD_RESOURCES(qgis_mapserv_RCC_SRCS ${qgis_mapserv_RCCS})
6969

70-
SET(qgis_mapserv_TESTRCC_SRCS)
71-
IF (ENABLE_TESTS)
72-
# add test resources, e.g. standard test font
73-
SET(qgis_mapserv_TESTRCCS ../../tests/testdata/testdata.qrc)
74-
QT4_ADD_RESOURCES(qgis_mapserv_TESTRCC_SRCS ${qgis_mapserv_TESTRCCS})
75-
ENDIF(ENABLE_TESTS)
70+
# add test resources, e.g. standard test font
71+
SET(qgis_mapserv_TESTRCCS ../../tests/testdata/testdata.qrc)
72+
QT4_ADD_RESOURCES(qgis_mapserv_TESTRCC_SRCS ${qgis_mapserv_TESTRCCS})
7673

7774
ADD_EXECUTABLE(qgis_mapserv.fcgi
7875
${qgis_mapserv_SRCS}

‎src/mapserver/qgis_map_serv.cpp

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ map service syntax for SOAP/HTTP POST
2020
#include "qgsapplication.h"
2121
#include "qgscapabilitiescache.h"
2222
#include "qgsconfigcache.h"
23+
#include "qgsfontutils.h"
2324
#include "qgsgetrequesthandler.h"
2425
#include "qgspostrequesthandler.h"
2526
#include "qgssoaprequesthandler.h"
@@ -248,48 +249,12 @@ int main( int argc, char * argv[] )
248249
theMapRenderer->setLabelingEngine( new QgsPalLabeling() );
249250

250251
#ifdef QGSMSDEBUG
251-
// load standard test font from filesystem or testdata.qrc (for unit tests)
252-
bool testFontLoaded = false;
253-
QFontDatabase fontDB;
254-
QFont testFont = fontDB.font( "QGIS Vera Sans", "Roman", 12 );
255-
if ( testFont.family().startsWith( "QGIS", Qt::CaseInsensitive ) )
256-
{
257-
testFontLoaded = true;
258-
QgsDebugMsg( "Test font already available" );
259-
}
260-
else
261-
{
262-
QString fontFromWhere( "" );
263-
if ( QgsApplication::isRunningFromBuildDir() )
264-
{
265-
// [LS] workaround for serious bugs on Mac 10.9, where fonts from qrc resources fail:
266-
// https://bugreports.qt-project.org/browse/QTBUG-30917
267-
// https://bugreports.qt-project.org/browse/QTBUG-32789
268-
QString testFont( QgsApplication::buildSourcePath() + "/tests/testdata/font/QGIS-Vera/QGIS-Vera.ttf" );
269-
int fontID = QFontDatabase::addApplicationFont( testFont );
270-
testFontLoaded = ( fontID != -1 );
271-
fontFromWhere = testFontLoaded ? "filesystem" : "";
272-
}
273-
else
274-
{
275-
QFile testFont( ":/testdata/font/QGIS-Vera/QGIS-Vera.ttf" );
276-
if ( testFont.open( QIODevice::ReadOnly ) )
277-
{
278-
int fontID = QFontDatabase::addApplicationFontFromData( testFont.readAll() );
279-
testFontLoaded = ( fontID != -1 );
280-
} // else app wasn't built with ENABLE_TESTS or not GUI app
281-
fontFromWhere = testFontLoaded ? "testdata.qrc" : "";
282-
}
283-
QgsDebugMsg( QString( "Test font %1loaded from %2 on startup" ).arg( testFontLoaded ? "" : "NOT " ).arg( fontFromWhere ) );
284-
}
252+
QgsFontUtils::loadStandardTestFonts( QStringList() << "Roman" << "Bold" );
285253
#endif
286254

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

294259
//use QgsGetRequestHandler in case of HTTP GET and QgsSOAPRequestHandler in case of HTTP POST
295260
QgsRequestHandler* theRequestHandler = 0;

‎tests/src/python/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
INCLUDE(UsePythonTest)
22
ADD_PYTHON_TEST(PyQgsApplication test_qgsapplication.py)
33
ADD_PYTHON_TEST(PyQgsLocalServer test_qgis_local_server.py)
4+
ADD_PYTHON_TEST(PyQgsFontUtils test_qgsfontutils.py)
45
ADD_PYTHON_TEST(PyQgsFeature test_qgsfeature.py)
56
ADD_PYTHON_TEST(PyQgsFeatureIterator test_qgsfeatureiterator.py)
67
ADD_PYTHON_TEST(PyQgsGeometry test_qgsgeometry.py)

‎tests/src/python/test_qgsfontutils.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# -*- coding: utf-8 -*-
2+
"""QGIS Unit tests for core QgsFontUtils class
3+
4+
From build dir: ctest -R PyQgsFontUtils -V
5+
6+
.. note:: This program is free software; you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation; either version 2 of the License, or
9+
(at your option) any later version.
10+
"""
11+
__author__ = 'Larry Shaffer'
12+
__date__ = '2014/02/19'
13+
__copyright__ = 'Copyright 2014, The QGIS Project'
14+
# This will get replaced with a git SHA1 when you do a git archive
15+
__revision__ = '$Format:%H$'
16+
17+
18+
from PyQt4.QtGui import *
19+
20+
from qgis.core import (
21+
QgsFontUtils
22+
)
23+
24+
from utilities import (
25+
TestCase,
26+
getQgisTestApp,
27+
unittest,
28+
expectedFailure,
29+
unitTestDataPath,
30+
)
31+
32+
QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()
33+
34+
35+
class TestQgsFontUtils(TestCase):
36+
37+
@classmethod
38+
def setUpClass(cls):
39+
cls._family = 'QGIS Vera Sans'
40+
cls._fontdb = QFontDatabase()
41+
""":type : QFontDatabase"""
42+
43+
def test_loading_specific_test_font(self):
44+
QgsFontUtils.loadStandardTestFonts(['Roman'])
45+
msg = self._family + ' Roman test font styles could not be loaded'
46+
assert self._has_style(self._family, 'Roman'), msg
47+
48+
def test_loading_all_test_fonts(self):
49+
QgsFontUtils.loadStandardTestFonts(['All'])
50+
# styles = ''
51+
# for style in self._fontdb.styles(self._family):
52+
# styles += ' ' + style
53+
# print self._family + ' styles:' + styles
54+
msg = self._family + ' test font styles could not be loaded'
55+
res = (self._has_style(self._family, 'Roman')
56+
and self._has_style(self._family, 'Oblique')
57+
and self._has_style(self._family, 'Bold')
58+
and self._has_style(self._family, 'Bold Oblique'))
59+
assert res, msg
60+
61+
def _has_style(self, family, style):
62+
return (family in self._fontdb.families()
63+
and style in self._fontdb.styles(family))
64+
65+
if __name__ == '__main__':
66+
unittest.main()

0 commit comments

Comments
 (0)
Please sign in to comment.