Skip to content

Commit

Permalink
Followup 2dc5d95, add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed May 30, 2015
1 parent 680b1ce commit 51bd0b2
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 9 deletions.
9 changes: 9 additions & 0 deletions python/core/qgspallabeling.sip
Expand Up @@ -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
Expand Down
11 changes: 2 additions & 9 deletions src/core/qgspalgeometry.h
Expand Up @@ -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;

Expand Down Expand Up @@ -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++ )
Expand Down
14 changes: 14 additions & 0 deletions src/core/qgspallabeling.cpp
Expand Up @@ -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 )
Expand Down
9 changes: 9 additions & 0 deletions src/core/qgspallabeling.h
Expand Up @@ -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,
Expand Down
15 changes: 15 additions & 0 deletions tests/src/core/testqgspallabeling.cpp
Expand Up @@ -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:
};
Expand Down Expand Up @@ -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.