Skip to content
Permalink
Browse files
Followup 2dc5d95, add unit tests
  • Loading branch information
nyalldawson committed May 30, 2015
1 parent 680b1ce commit 51bd0b2bc5a13695375d2b9f4dbedd14050175d1
Showing with 49 additions and 9 deletions.
  1. +9 −0 python/core/qgspallabeling.sip
  2. +2 −9 src/core/qgspalgeometry.h
  3. +14 −0 src/core/qgspallabeling.cpp
  4. +9 −0 src/core/qgspallabeling.h
  5. +15 −0 tests/src/core/testqgspallabeling.cpp
@@ -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
@@ -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++ )
@@ -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 )
@@ -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,
@@ -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"

0 comments on commit 51bd0b2

Please sign in to comment.