Followup 2dc5d95, add unit tests

This commit is contained in:
Nyall Dawson 2015-05-31 07:58:05 +10:00
parent 680b1ced70
commit 51bd0b2bc5
5 changed files with 49 additions and 9 deletions

View File

@ -772,6 +772,15 @@ class QgsPalLabeling : QgsLabelingEngineInterface
* @note added in QGIS 2.9
*/
static QStringList splitToLines( const QString& text, const QString& wrapCharacter );
/** Splits a text string to a list of graphemes, which are the smallest allowable character
* divisions in the string. This accounts for scripts were individual characters are not
* allowed to be split apart (eg Arabic and Indic based scripts)
* @param text string to split
* @returns list of graphemes
* @note added in QGIS 2.10
*/
static QStringList splitToGraphemes( const QString& text );
protected:
// update temporary QgsPalLayerSettings with any data defined text style values

View File

@ -2,9 +2,9 @@
#define QGSPALGEOMETRY_H
#include "qgsgeometry.h"
#include "qgspallabeling.h"
#include <pal/feature.h>
#include <pal/palgeometry.h>
#include <QTextBoundaryFinder>
using namespace pal;
@ -90,14 +90,7 @@ class QgsPalGeometry : public PalGeometry
qreal wordSpaceFix;
//split string by valid grapheme boundaries - required for certain scripts (see #6883)
QTextBoundaryFinder boundaryFinder( QTextBoundaryFinder::Grapheme, mText );
int currentBoundary = -1;
int previousBoundary = 0;
while (( currentBoundary = boundaryFinder.toNextBoundary() ) > 0 )
{
mClusters << mText.mid( previousBoundary, currentBoundary - previousBoundary );
previousBoundary = currentBoundary;
}
mClusters = QgsPalLabeling::splitToGraphemes( mText );
mInfo = new pal::LabelInfo( mClusters.count(), labelHeight, maxinangle, maxoutangle );
for ( int i = 0; i < mClusters.count(); i++ )

View File

@ -3412,6 +3412,20 @@ QStringList QgsPalLabeling::splitToLines( const QString &text, const QString &wr
return multiLineSplit;
}
QStringList QgsPalLabeling::splitToGraphemes( const QString &text )
{
QStringList graphemes;
QTextBoundaryFinder boundaryFinder( QTextBoundaryFinder::Grapheme, text );
int currentBoundary = -1;
int previousBoundary = 0;
while (( currentBoundary = boundaryFinder.toNextBoundary() ) > 0 )
{
graphemes << text.mid( previousBoundary, currentBoundary - previousBoundary );
previousBoundary = currentBoundary;
}
return graphemes;
}
QgsGeometry* QgsPalLabeling::prepareGeometry( const QgsGeometry* geometry, const QgsRenderContext& context, const QgsCoordinateTransform* ct, double minSize, QgsGeometry* clipGeometry )
{
if ( !geometry )

View File

@ -843,6 +843,15 @@ class CORE_EXPORT QgsPalLabeling : public QgsLabelingEngineInterface
*/
static QStringList splitToLines( const QString& text, const QString& wrapCharacter );
/** Splits a text string to a list of graphemes, which are the smallest allowable character
* divisions in the string. This accounts for scripts were individual characters are not
* allowed to be split apart (eg Arabic and Indic based scripts)
* @param text string to split
* @returns list of graphemes
* @note added in QGIS 2.10
*/
static QStringList splitToGraphemes( const QString& text );
protected:
// update temporary QgsPalLayerSettings with any data defined text style values
void dataDefinedTextStyle( QgsPalLayerSettings& tmpLyr,

View File

@ -30,6 +30,7 @@ class TestQgsPalLabeling: public QObject
void init();// will be called before each testfunction is executed.
void cleanup();// will be called after every testfunction.
void wrapChar();//test wrapping text lines
void graphemes(); //test splitting strings to graphemes
private:
};
@ -63,5 +64,19 @@ void TestQgsPalLabeling::wrapChar()
QCOMPARE( QgsPalLabeling::splitToLines( "no\nmatching\nchars", QString( "#" ) ), QStringList() << "no" << "matching" << "chars" );
}
void TestQgsPalLabeling::graphemes()
{
QCOMPARE( QgsPalLabeling::splitToGraphemes( QString() ) , QStringList() );
QCOMPARE( QgsPalLabeling::splitToGraphemes( "abcd" ) , QStringList() << "a" << "b" << "c" << "d" );
QCOMPARE( QgsPalLabeling::splitToGraphemes( "ab cd" ) , QStringList() << "a" << "b" << " " << "c" << "d" );
QCOMPARE( QgsPalLabeling::splitToGraphemes( "ab cd " ) , QStringList() << "a" << "b" << " " << "c" << "d" << " " );
QCOMPARE( QgsPalLabeling::splitToGraphemes( QString::fromUtf8( "\u179F\u17D2\u178F\u17D2\u179A\u17B8\u179B\u17D2" ) ) , QStringList() << QString::fromUtf8( "\u179F\u17D2\u178F\u17D2\u179A\u17B8" ) << QString::fromUtf8( "\u179B\u17D2" ) );
QCOMPARE( QgsPalLabeling::splitToGraphemes( QString::fromUtf8( "\u1780\u17D2\u179A\u17BB\u1798\u17A2\u1784\u17D2\u1782\u1780\u17B6\u179A\u179F\u17B7\u1791\u17D2\u1792\u17B7\u1798\u1793\u17BB\u179F\u17D2\u179F" ) ) ,
QStringList() << QString::fromUtf8( "\u1780\u17D2\u179A\u17BB" ) << QString::fromUtf8( "\u1798" ) << QString::fromUtf8( "\u17A2" )
<< QString::fromUtf8( "\u1784\u17D2\u1782" ) << QString::fromUtf8( "\u1780\u17B6" ) << QString::fromUtf8( "\u179A" )
<< QString::fromUtf8( "\u179F\u17B7" ) << QString::fromUtf8( "\u1791\u17D2\u1792\u17B7" ) << QString::fromUtf8( "\u1798" )
<< QString::fromUtf8( "\u1793\u17BB" ) << QString::fromUtf8( "\u179F\u17D2\u179F" ) );
}
QTEST_MAIN( TestQgsPalLabeling )
#include "testqgspallabeling.moc"