Skip to content

Commit e75022e

Browse files
committed
[labeling] Make test font available for app, tests, server without requiring font install
- Load test font from testdata.qrc if app or server built with ENABLE_TESTS - Add global loading of test font in utilities.py
1 parent a634f18 commit e75022e

File tree

8 files changed

+74
-21
lines changed

8 files changed

+74
-21
lines changed

src/app/CMakeLists.txt

+9-2
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,13 @@ SET(IMAGE_RCCS ../../images/images.qrc)
366366

367367
QT4_ADD_RESOURCES(IMAGE_RCC_SRCS ${IMAGE_RCCS})
368368

369+
SET(TEST_RCC_SRCS)
370+
IF (ENABLE_TESTS)
371+
# add test resources, e.g. standard test font
372+
SET(TEST_RCCS ../../tests/testdata/testdata.qrc)
373+
QT4_ADD_RESOURCES(TEST_RCC_SRCS ${TEST_RCCS})
374+
ENDIF (ENABLE_TESTS)
375+
369376
QT4_WRAP_CPP(QGIS_APP_MOC_SRCS ${QGIS_APP_MOC_HDRS})
370377

371378
IF (WIN32)
@@ -444,9 +451,9 @@ ENDIF (POSTGRES_FOUND)
444451

445452
#############
446453
IF (ANDROID)
447-
ADD_LIBRARY(${QGIS_APP_NAME} SHARED ${QGIS_APP_SRCS} ${QGIS_APP_MOC_SRCS} ${QGIS_APP_HDRS} ${QGIS_APP_MOC_HDRS} ${IMAGE_RCC_SRCS})
454+
ADD_LIBRARY(${QGIS_APP_NAME} SHARED ${QGIS_APP_SRCS} ${QGIS_APP_MOC_SRCS} ${QGIS_APP_HDRS} ${QGIS_APP_MOC_HDRS} ${IMAGE_RCC_SRCS} ${TEST_RCC_SRCS})
448455
ELSE (ANDROID)
449-
ADD_EXECUTABLE(${QGIS_APP_NAME} MACOSX_BUNDLE WIN32 ${QGIS_APP_SRCS} ${QGIS_APP_MOC_SRCS} ${IMAGE_RCC_SRCS})
456+
ADD_EXECUTABLE(${QGIS_APP_NAME} MACOSX_BUNDLE WIN32 ${QGIS_APP_SRCS} ${QGIS_APP_MOC_SRCS} ${IMAGE_RCC_SRCS} ${TEST_RCC_SRCS})
450457
ENDIF (ANDROID)
451458

452459
TARGET_LINK_LIBRARIES(${QGIS_APP_NAME}

src/app/main.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <QFile>
2222
#include <QFileInfo>
2323
#include <QFont>
24+
#include <QFontDatabase>
2425
#include <QPixmap>
2526
#include <QLocale>
2627
#include <QSettings>
@@ -810,6 +811,14 @@ int main( int argc, char *argv[] )
810811
}
811812
}
812813

814+
// load standard test font from testdata.qrc (for unit tests)
815+
QFile testFont( ":/testdata/font/FreeSansQGIS.ttf" );
816+
if ( testFont.open( QIODevice::ReadOnly ) )
817+
{
818+
int fontID = QFontDatabase::addApplicationFontFromData( testFont.readAll() );
819+
QgsDebugMsg( QString( "Test font %1loaded from testdata.qrc" ).arg( fontID != -1 ? "" : "NOT " ) );
820+
} // else app wasn't built with ENABLE_TESTS
821+
813822
// Set the application style. If it's not set QT will use the platform style except on Windows
814823
// as it looks really ugly so we use QPlastiqueStyle.
815824
QString style = mySettings.value( "/qgis/style" ).toString();

src/mapserver/CMakeLists.txt

+8
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,19 @@ QT4_WRAP_CPP (qgis_mapserv_MOC_SRCS ${qgis_mapserv_MOC_HDRS})
6666

6767
QT4_ADD_RESOURCES(qgis_mapserv_RCC_SRCS ${qgis_mapserv_RCCS})
6868

69+
SET(qgis_mapserv_TESTRCC_SRCS)
70+
IF (ENABLE_TESTS)
71+
# add test resources, e.g. standard test font
72+
SET(qgis_mapserv_TESTRCCS ../../tests/testdata/testdata.qrc)
73+
QT4_ADD_RESOURCES(qgis_mapserv_TESTRCC_SRCS ${qgis_mapserv_TESTRCCS})
74+
ENDIF(ENABLE_TESTS)
75+
6976
ADD_EXECUTABLE(qgis_mapserv.fcgi
7077
${qgis_mapserv_SRCS}
7178
${qgis_mapserv_MOC_SRCS}
7279
${qgis_mapserv_RCC_SRCS}
7380
${qgis_mapserv_UIS_H}
81+
${qgis_mapserv_TESTRCC_SRCS}
7482
)
7583

7684
INCLUDE_DIRECTORIES(

src/mapserver/qgis_map_serv.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,21 @@ int main( int argc, char * argv[] )
240240
QgsMapRenderer* theMapRenderer = new QgsMapRenderer();
241241
theMapRenderer->setLabelingEngine( new QgsPalLabeling() );
242242

243+
// load standard test font from testdata.qrc (for unit tests)
244+
bool testFontLoaded = false;
245+
QFile testFont( ":/testdata/font/FreeSansQGIS.ttf" );
246+
if ( testFont.open( QIODevice::ReadOnly ) )
247+
{
248+
int fontID = QFontDatabase::addApplicationFontFromData( testFont.readAll() );
249+
testFontLoaded = ( fontID != -1 );
250+
} // else app wasn't built with ENABLE_TESTS or not GUI app
251+
243252
while ( fcgi_accept() >= 0 )
244253
{
245254
printRequestInfos(); //print request infos if in debug mode
255+
#ifdef QGSMSDEBUG
256+
QgsDebugMsg( QString( "Test font %1loaded from testdata.qrc" ).arg( testFontLoaded ? "" : "NOT " ) );
257+
#endif
246258

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

tests/src/python/test_qgspallabeling_base.py

+12-14
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,12 @@
4747
unittest,
4848
expectedFailure,
4949
unitTestDataPath,
50+
loadTestFont,
5051
openInBrowserTab
5152
)
5253

5354
QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()
55+
TESTFONT = loadTestFont()
5456

5557
PALREPORT = 'PAL_REPORT' in os.environ
5658
PALREPORTS = {}
@@ -61,7 +63,7 @@ class TestQgsPalLabeling(TestCase):
6163
_TestDataDir = unitTestDataPath()
6264
_PalDataDir = os.path.join(_TestDataDir, 'labeling')
6365
_PalFeaturesDb = os.path.join(_PalDataDir, 'pal_features_v3.sqlite')
64-
_TestFontID = -1
66+
_TestFont = TESTFONT
6567
_MapRegistry = None
6668
_MapRenderer = None
6769
_Canvas = None
@@ -81,18 +83,8 @@ def setUpClass(cls):
8183
assert res, msg
8284

8385
# load the FreeSansQGIS labeling test font
84-
fontdb = QFontDatabase()
85-
cls._TestFontID = fontdb.addApplicationFont(
86-
os.path.join(cls._TestDataDir, 'font', 'FreeSansQGIS.ttf'))
87-
msg = ('\nCould not store test font in font database, '
88-
'SKIPPING TEST SUITE')
89-
assert cls._TestFontID != -1, msg
90-
91-
cls._TestFont = fontdb.font('FreeSansQGIS', 'Medium', 48)
92-
appfont = QApplication.font()
93-
msg = ('\nCould not load test font from font database, '
94-
'SKIPPING TEST SUITE')
95-
assert cls._TestFont.toString() != appfont.toString(), msg
86+
msg = '\nCould not load test font, SKIPPING TEST SUITE'
87+
assert TESTFONT is not None, msg
9688

9789
cls._TestFunction = ''
9890
cls._TestGroup = ''
@@ -128,6 +120,10 @@ def tearDownClass(cls):
128120
def removeAllLayers(cls):
129121
cls._MapRegistry.removeAllMapLayers()
130122

123+
@classmethod
124+
def getTestFont(cls):
125+
return QFont(cls._TestFont)
126+
131127
@classmethod
132128
def loadFeatureLayer(cls, table):
133129
uri = QgsDataSourceURI()
@@ -176,7 +172,9 @@ def defaultSettings(self):
176172
lyr = QgsPalLayerSettings()
177173
lyr.enabled = True
178174
lyr.fieldName = 'text' # default in data sources
179-
lyr.textFont = self._TestFont
175+
font = self.getTestFont()
176+
font.setPointSize(48)
177+
lyr.textFont = font
180178
lyr.textNamedStyle = 'Medium'
181179
return lyr
182180

tests/src/python/test_qgspallabeling_tests.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ class TestPointBase(object):
2929
def __init__(self):
3030
"""Dummy assignments, intended to be overriden in subclasses"""
3131
self.lyr = QgsPalLayerSettings()
32-
self._TestFont = QFont()
32+
self._TestFont = QApplication.font()
3333

34-
def checkTest(self):
34+
def checkTest(self, **kwargs):
3535
"""Intended to be overriden in subclasses"""
3636
pass
3737

@@ -42,9 +42,9 @@ def test_default_label(self):
4242
def test_text_size_map_unit(self):
4343
# Label text size in map units
4444
self.lyr.fontSizeInMapUnits = True
45-
tmpFont = QFont(self._TestFont)
46-
tmpFont.setPointSizeF(0.25)
47-
self.lyr.textFont = tmpFont
45+
font = QFont(self._TestFont)
46+
font.setPointSizeF(500)
47+
self.lyr.textFont = font
4848
self.checkTest()
4949

5050
def test_text_color(self):

tests/src/python/utilities.py

+14
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
GEOCRS = 4326 # constant for EPSG:GEOCRS Geographic CRS id
4949
GOOGLECRS = 900913 # constant for EPSG:GOOGLECRS Google Mercator id
5050

51+
TESTFONT = None
5152

5253
def assertHashesForFile(theHashes, theFilename):
5354
"""Assert that a files has matches one of a list of expected hashes"""
@@ -214,6 +215,19 @@ def compareWkt(a, b, tol=0.000001):
214215
return True
215216

216217

218+
def loadTestFont():
219+
# load the FreeSansQGIS test font
220+
global TESTFONT # pylint: disable=W0603
221+
222+
if TESTFONT is None:
223+
fontid = QtGui.QFontDatabase.addApplicationFont(
224+
os.path.join(unitTestDataPath('font'), 'FreeSansQGIS.ttf'))
225+
if fontid != -1:
226+
TESTFONT = QtGui.QFont('FreeSansQGIS')
227+
228+
return TESTFONT
229+
230+
217231
def openInBrowserTab(url):
218232
if sys.platform[:3] in ('win', 'dar'):
219233
webbrowser.open_new_tab(url)

tests/testdata/testdata.qrc

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<RCC>
2+
<qresource prefix="/testdata">
3+
<file>font/FreeSansQGIS.ttf</file>
4+
</qresource>
5+
</RCC>

0 commit comments

Comments
 (0)