Skip to content
Browse files

Initial test for PAL engine

- Includes small custom test font based of off Gnu Free Sans
- Test if font can be stored and loaded
- Test if layer can have PAL engine set
- Test render comparison for output of vector line layer
  • Loading branch information
dakcarto committed Oct 14, 2012
1 parent ac3f33d commit 83cb4ef4af462cb90ca77df77659090448571d88
@@ -17,3 +17,4 @@ ADD_PYTHON_TEST(PyQgsSymbolLayerV2
@@ -0,0 +1,207 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for QgsPalLabeling
.. 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__ = '12/10/2012'
__copyright__ = 'Copyright 2012, The Quantum GIS Project'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import os
from PyQt4.QtCore import *
from PyQt4.QtGui import *

from qgis.core import (QgsPalLabeling,

from utilities import (getQgisTestApp,

# Convenience instances in case you may need them
TEST_DATA_DIR = unitTestDataPath()

class TestQgsPalLabeling(TestCase):

def setUpClass(cls):
"""Run before all tests"""

# Store/load the FreeSansQGIS labeling test font
myFontDB = QFontDatabase()
cls._testFontID = myFontDB.addApplicationFont(
os.path.join(TEST_DATA_DIR, 'font', 'FreeSansQGIS.ttf'))
myMessage = ('\nCould not store test font in font database, '
'skipping test suite')
assert cls._testFontID != -1, myMessage

cls._testFont = myFontDB.font('FreeSansQGIS', 'Medium', 12)
myAppFont = QApplication.font()
myMessage = ('\nCould not load test font from font database, '
'skipping test suite')
assert cls._testFont.toString() != myAppFont.toString(), myMessage

# initialize class MapRegistry, Canvas, MapRenderer, Map and PAL
cls._MapRegistry = QgsMapLayerRegistry.instance()
cls._Canvas = CANVAS
# to match render test comparisons background
cls._Canvas.setCanvasColor(QColor(152, 219, 249))
cls._Map =
cls._Map.resize(QSize(600, 400))
cls._MapRenderer = cls._Canvas.mapRenderer()
cls._MapRenderer.setOutputSize(QSize(600, 400), 72)

cls._Pal = QgsPalLabeling();
cls._PalEngine = cls._MapRenderer.labelingEngine()
myMessage = ('\nCould initialize PAL labeling engine, '
'skipping test suite')
assert cls._PalEngine, myMessage

def tearDownClass(cls):
"""Run after all tests"""
# remove test font
myFontDB = QFontDatabase()
myResult = myFontDB.removeApplicationFont(cls._testFontID)
myMessage = ('\nFailed to remove test font from font database')
assert myResult, myMessage

def setUp(self):
"""Run before each test."""

def tearDown(self):
"""Run after each test."""

def test_AddPALToVectorLayer(self):
"""Check if we can set a label field, verify that PAL is assigned
and that output is rendered correctly"""
# TODO: add UTM PAL-specific shps, with 4326 as on-the-fly cross-check
# setCanvasCrs(26913)

myShpFile = os.path.join(TEST_DATA_DIR, 'lines.shp')
myVectorLayer = QgsVectorLayer(myShpFile, 'Lines', 'ogr')


myLayers = QStringList()

# check layer labeling is PAL with customProperty access
# should not be activated on layer load
myPalSet = myVectorLayer.customProperty( "labeling" ).toString()
myMessage = '\nExpected: Empty QString\nGot: %s' % (str(myPalSet))
assert str(myPalSet) == '', myMessage

# simulate clicking checkbox, setting label field and clicking apply
myPalLyr = QgsPalLayerSettings()

myPalLyr.enabled = True
myPalLyr.fieldName = 'Name'
myPalLyr.placement = QgsPalLayerSettings.Line
myPalLyr.placementFlags = QgsPalLayerSettings.AboveLine
myPalLyr.xQuadOffset = 0
myPalLyr.yQuadOffset = 0
myPalLyr.xOffset = 0
myPalLyr.yOffset = 0
myPalLyr.angleOffset = 0
myPalLyr.centroidWhole = False
myPalLyr.textFont = self._testFont
myPalLyr.textNamedStyle = QString("Medium")
myPalLyr.textColor =
myPalLyr.textTransp = 0
myPalLyr.previewBkgrdColor = Qt.white
myPalLyr.priority = 5
myPalLyr.obstacle = True
myPalLyr.dist = 0
myPalLyr.scaleMin = 0
myPalLyr.scaleMax = 0
myPalLyr.bufferSize = 1
myPalLyr.bufferColor = Qt.white
myPalLyr.bufferTransp = 0
myPalLyr.bufferNoFill = False
myPalLyr.bufferJoinStyle = Qt.RoundJoin
myPalLyr.formatNumbers = False
myPalLyr.decimals = 3
myPalLyr.plusSign = False
myPalLyr.labelPerPart = False
myPalLyr.displayAll = True
myPalLyr.mergeLines = False
myPalLyr.minFeatureSize = 0.0
myPalLyr.vectorScaleFactor = 1.0
myPalLyr.rasterCompressFactor = 1.0
myPalLyr.addDirectionSymbol = False
myPalLyr.upsidedownLabels = QgsPalLayerSettings.Upright
myPalLyr.fontSizeInMapUnits = False
myPalLyr.bufferSizeInMapUnits = False
myPalLyr.labelOffsetInMapUnits = True
myPalLyr.distInMapUnits = False
myPalLyr.wrapChar = ""
myPalLyr.preserveRotation = True


# check layer labeling is PAL with customProperty access
myPalSet = myVectorLayer.customProperty( "labeling" ).toString()
myMessage = '\nExpected: pal\nGot: %s' % (str(myPalSet))
assert str(myPalSet) == 'pal', myMessage

# check layer labeling is PAL via engine interface
myMessage = '\nCould not get whether PAL enabled from labelingEngine'
assert self._PalEngine.willUseLayer(myVectorLayer), myMessage

myChecker = QgsRenderChecker()

myResult = myChecker.runTest("pal_aboveLineLabeling_python");
myMessage = ('\nVector layer \'above line\' label engine '
'rendering test failed')
assert myResult, myMessage

# compare against a straight rendering/save as from QgsMapCanvasMap
# unnecessary? works a bit different than QgsRenderChecker, though
# myImage = os.path.join(unicode(QDir.tempPath()),
# 'render_pal_aboveLineLabeling.png')
# self._Map.render()
# self._Canvas.saveAsImage(myImage)
# myChecker.setRenderedImage(myImage)
# myResult = myChecker.compareImages("pal_aboveLineLabeling_python")
# myMessage = ('\nVector layer \'above line\' label engine '
# 'comparison to QgsMapCanvasMap.render() test failed')
# assert myResult, myMessage


# @expectedFailure
# def testIssue####(self):
# """Test we can .
# """
# pass

if __name__ == '__main__':

Binary file not shown.
@@ -0,0 +1,159 @@
-*- mode:text; coding:utf-8; -*-
$Id: AUTHORS,v 1.9 2005/12/03 10:56:08 peterlin Exp $

The free UCS scalable font collection is being maintained by Primo�
Peterlin <primoz.peterlin AT>. The folowing list
cites the other contributors that contributed to particular ISO 10646

* URW++ Design & Development GmbH <>

Basic Latin (U+0041-U+007A)
Latin-1 Supplement (U+00C0-U+00FF) (most)
Latin Extended-A (U+0100-U+017F)
Spacing Modifier Letters (U+02B0-U+02FF)
Mathematical Operators (U+2200-U+22FF) (parts)
Block Elements (U+2580-U+259F)
Dingbats (U+2700-U+27BF)

* Yannis Haralambous <yannis.haralambous AT> and John
Plaice <plaice AT>

Latin Extended-B (U+0180-U+024F)
IPA Extensions (U+0250-U+02AF)
Greek (U+0370-U+03FF)
Armenian (U+0530-U+058F)
Hebrew (U+0590-U+05FF)
Arabic (U+0600-U+06FF)
Currency Symbols (U+20A0-U+20CF)
Arabic Presentation Forms-A (U+FB50-U+FDFF)
Arabic Presentation Forms-B (U+FE70-U+FEFF)

* Young U. Ryu <ryoung AT>

Arrows (U+2190-U+21FF)
Mathematical Symbols (U+2200-U+22FF)

* Valek Filippov <frob AT>

Cyrillic (U+0400-U+04FF)

* Wadalab Kanji Comittee

Hiragana (U+3040-U+309F)
Katakana (U+30A0-U+30FF)

* Angelo Haritsis <ah AT>

Greek (U+0370-U+03FF)

* Yannis Haralambous and Virach Sornlertlamvanich

Thai (U+0E00-U+0E7F)

* Shaheed R. Haque <srhaque AT>

Bengali (U+0980-U+09FF)

* Sam Stepanyan <sam AT>

Armenian (U+0530-U+058F)

* Mohamed Ishan <ishan AT>

Thaana (U+0780-U+07BF)

* Sushant Kumar Dash <sushant AT>

Oriya (U+0B00-U+0B7F)

* Harsh Kumar <harshkumar AT>

Devanagari (U+0900-U+097F)
Bengali (U+0980-U+09FF)
Gurmukhi (U+0A00-U+0A7F)
Gujarati (U+0A80-U+0AFF)

* Prasad A. Chodavarapu <chprasad AT>

Telugu (U+0C00-U+0C7F)

* Frans Velthuis <velthuis AT> and Anshuman Pandey
<apandey AT>

Devanagari (U+0900-U+097F)

* Hardip Singh Pannu <HSPannu AT>

Gurmukhi (U+0A00-U+0A7F)

* Jeroen Hellingman <jehe AT>

Oriya (U+0B00-U+0B7F)
Malayalam (U+0D00-U+0D7F)

* Thomas Ridgeway <email needed>

Tamil (U+0B80-U+0BFF)

* Berhanu Beyene <1beyene AT>,
Prof. Dr. Manfred Kudlek <kudlek AT>, Olaf
Kummer <kummer AT>, and Jochen Metzinger <?>

Ethiopic (U+1200-U+137F)

* Maxim Iorsh <iorsh AT>

Hebrew (U+0590-U+05FF)

* Vyacheslav Dikonov <sdiconov AT>

Syriac (U+0700-U+074A)
Braille (U+2800-U+28FF)

* Panayotis Katsaloulis <panayotis AT>

Greek Extended (U+1F00-U+1FFF)

* M.S. Sridhar <mssridhar AT>

Devanagari (U+0900-U+097F)
Bengali (U+0980-U+09FF)
Gurmukhi (U+0A00-U+0A7F)
Gujarati (U+0A80-U+0AFF)
Oriya (U+0B00-U+0B7F)
Tamil (U+0B80-U+0BFF)
Telugu (U+0C00-U+0C7F)
Kannada (U+0C80-U+0CFF)
Malayalam (U+0D00-U+0D7F)

* DMS Electronics, The Sri Lanka Tipitaka Project, and Noah Levitt
<nlevitt AT>

Sinhala (U+0D80-U+0DFF)

* Dan Shurovich Chirkov <dansh AT>

Cyrillic (U+0400-U+04FF)

* Abbas Izad <abbasizad AT>

Arabic (U+0600-U+06FF)
Arabic Presentation Forms-A (U+FB50-U+FDFF)
Arabic Presentation Forms-B (U+FE70-U+FEFF)

* Denis Jacquerye <moyogo AT>

Latin Extended-B (U+0180-U+024F)
IPA Extensions (U+0250-U+02AF)

* K.H. Hussain <hussain AT> and R. Chitrajan

Malayalam (U+0D00-U+0D7F)

* Solaiman Karim <solaiman AT>

Bengali (U+0980-U+09FF)

Please see the CREDITS file for details on who contributed particular
subsets of the glyphs in font files.

0 comments on commit 83cb4ef

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