Skip to content

Commit 51bd0b2

Browse files
committed
Followup 2dc5d95, add unit tests
1 parent 680b1ce commit 51bd0b2

File tree

5 files changed

+49
-9
lines changed

5 files changed

+49
-9
lines changed

python/core/qgspallabeling.sip

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,15 @@ class QgsPalLabeling : QgsLabelingEngineInterface
772772
* @note added in QGIS 2.9
773773
*/
774774
static QStringList splitToLines( const QString& text, const QString& wrapCharacter );
775+
776+
/** Splits a text string to a list of graphemes, which are the smallest allowable character
777+
* divisions in the string. This accounts for scripts were individual characters are not
778+
* allowed to be split apart (eg Arabic and Indic based scripts)
779+
* @param text string to split
780+
* @returns list of graphemes
781+
* @note added in QGIS 2.10
782+
*/
783+
static QStringList splitToGraphemes( const QString& text );
775784

776785
protected:
777786
// update temporary QgsPalLayerSettings with any data defined text style values

src/core/qgspalgeometry.h

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
#define QGSPALGEOMETRY_H
33

44
#include "qgsgeometry.h"
5+
#include "qgspallabeling.h"
56
#include <pal/feature.h>
67
#include <pal/palgeometry.h>
7-
#include <QTextBoundaryFinder>
88

99
using namespace pal;
1010

@@ -90,14 +90,7 @@ class QgsPalGeometry : public PalGeometry
9090
qreal wordSpaceFix;
9191

9292
//split string by valid grapheme boundaries - required for certain scripts (see #6883)
93-
QTextBoundaryFinder boundaryFinder( QTextBoundaryFinder::Grapheme, mText );
94-
int currentBoundary = -1;
95-
int previousBoundary = 0;
96-
while (( currentBoundary = boundaryFinder.toNextBoundary() ) > 0 )
97-
{
98-
mClusters << mText.mid( previousBoundary, currentBoundary - previousBoundary );
99-
previousBoundary = currentBoundary;
100-
}
93+
mClusters = QgsPalLabeling::splitToGraphemes( mText );
10194

10295
mInfo = new pal::LabelInfo( mClusters.count(), labelHeight, maxinangle, maxoutangle );
10396
for ( int i = 0; i < mClusters.count(); i++ )

src/core/qgspallabeling.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3412,6 +3412,20 @@ QStringList QgsPalLabeling::splitToLines( const QString &text, const QString &wr
34123412
return multiLineSplit;
34133413
}
34143414

3415+
QStringList QgsPalLabeling::splitToGraphemes( const QString &text )
3416+
{
3417+
QStringList graphemes;
3418+
QTextBoundaryFinder boundaryFinder( QTextBoundaryFinder::Grapheme, text );
3419+
int currentBoundary = -1;
3420+
int previousBoundary = 0;
3421+
while (( currentBoundary = boundaryFinder.toNextBoundary() ) > 0 )
3422+
{
3423+
graphemes << text.mid( previousBoundary, currentBoundary - previousBoundary );
3424+
previousBoundary = currentBoundary;
3425+
}
3426+
return graphemes;
3427+
}
3428+
34153429
QgsGeometry* QgsPalLabeling::prepareGeometry( const QgsGeometry* geometry, const QgsRenderContext& context, const QgsCoordinateTransform* ct, double minSize, QgsGeometry* clipGeometry )
34163430
{
34173431
if ( !geometry )

src/core/qgspallabeling.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,15 @@ class CORE_EXPORT QgsPalLabeling : public QgsLabelingEngineInterface
843843
*/
844844
static QStringList splitToLines( const QString& text, const QString& wrapCharacter );
845845

846+
/** Splits a text string to a list of graphemes, which are the smallest allowable character
847+
* divisions in the string. This accounts for scripts were individual characters are not
848+
* allowed to be split apart (eg Arabic and Indic based scripts)
849+
* @param text string to split
850+
* @returns list of graphemes
851+
* @note added in QGIS 2.10
852+
*/
853+
static QStringList splitToGraphemes( const QString& text );
854+
846855
protected:
847856
// update temporary QgsPalLayerSettings with any data defined text style values
848857
void dataDefinedTextStyle( QgsPalLayerSettings& tmpLyr,

tests/src/core/testqgspallabeling.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class TestQgsPalLabeling: public QObject
3030
void init();// will be called before each testfunction is executed.
3131
void cleanup();// will be called after every testfunction.
3232
void wrapChar();//test wrapping text lines
33+
void graphemes(); //test splitting strings to graphemes
3334

3435
private:
3536
};
@@ -63,5 +64,19 @@ void TestQgsPalLabeling::wrapChar()
6364
QCOMPARE( QgsPalLabeling::splitToLines( "no\nmatching\nchars", QString( "#" ) ), QStringList() << "no" << "matching" << "chars" );
6465
}
6566

67+
void TestQgsPalLabeling::graphemes()
68+
{
69+
QCOMPARE( QgsPalLabeling::splitToGraphemes( QString() ) , QStringList() );
70+
QCOMPARE( QgsPalLabeling::splitToGraphemes( "abcd" ) , QStringList() << "a" << "b" << "c" << "d" );
71+
QCOMPARE( QgsPalLabeling::splitToGraphemes( "ab cd" ) , QStringList() << "a" << "b" << " " << "c" << "d" );
72+
QCOMPARE( QgsPalLabeling::splitToGraphemes( "ab cd " ) , QStringList() << "a" << "b" << " " << "c" << "d" << " " );
73+
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" ) );
74+
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" ) ) ,
75+
QStringList() << QString::fromUtf8( "\u1780\u17D2\u179A\u17BB" ) << QString::fromUtf8( "\u1798" ) << QString::fromUtf8( "\u17A2" )
76+
<< QString::fromUtf8( "\u1784\u17D2\u1782" ) << QString::fromUtf8( "\u1780\u17B6" ) << QString::fromUtf8( "\u179A" )
77+
<< QString::fromUtf8( "\u179F\u17B7" ) << QString::fromUtf8( "\u1791\u17D2\u1792\u17B7" ) << QString::fromUtf8( "\u1798" )
78+
<< QString::fromUtf8( "\u1793\u17BB" ) << QString::fromUtf8( "\u179F\u17D2\u179F" ) );
79+
}
80+
6681
QTEST_MAIN( TestQgsPalLabeling )
6782
#include "testqgspallabeling.moc"

0 commit comments

Comments
 (0)