Skip to content

Commit

Permalink
Add a QgsStringUtils function to substitute characters with vertical …
Browse files Browse the repository at this point in the history
…representation counterparts
  • Loading branch information
nirvn committed Aug 16, 2019
1 parent 08c1df1 commit bdf18ee
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 19 deletions.
11 changes: 11 additions & 0 deletions python/core/auto_generated/qgsstringutils.sip.in
Expand Up @@ -275,6 +275,17 @@ the string will be wrapped so that each line will ideally exceed ``length`` of c
A custom delimiter can be specified to use instead of space characters.

.. versionadded:: 3.4
%End

static QString substituteVerticalCharacters( QString string );
%Docstring
Returns a string with characters having vertical representation form substituted.

:param string: input string

:return: string with substition applied

.. versionadded:: 3.10
%End
};

Expand Down
22 changes: 22 additions & 0 deletions src/core/qgsstringutils.cpp
Expand Up @@ -518,6 +518,28 @@ QString QgsStringUtils::wordWrap( const QString &string, const int length, const
return newstr;
}

QString QgsStringUtils::substituteVerticalCharacters( QString string )
{
string = string.replace( ',', QChar( 65040 ) ).replace( QChar( 8229 ), QChar( 65072 ) ); // comma & two-dot leader
string = string.replace( QChar( 12289 ), QChar( 65041 ) ).replace( QChar( 12290 ), QChar( 65042 ) ); // ideographic comma & full stop
string = string.replace( ':', QChar( 65043 ) ).replace( ';', QChar( 65044 ) );
string = string.replace( '!', QChar( 65045 ) ).replace( '?', QChar( 65046 ) );
string = string.replace( QChar( 12310 ), QChar( 65047 ) ).replace( QChar( 12311 ), QChar( 65048 ) ); // white lenticular brackets
string = string.replace( QChar( 8230 ), QChar( 65049 ) ); // three-dot ellipse
string = string.replace( QChar( 8212 ), QChar( 65073 ) ).replace( QChar( 8211 ), QChar( 65074 ) ); // em & en dash
string = string.replace( '_', QChar( 65075 ) ).replace( QChar( 65103 ), QChar( 65076 ) ); // low line & wavy low line
string = string.replace( '(', QChar( 65077 ) ).replace( ')', QChar( 65078 ) );
string = string.replace( '{', QChar( 65079 ) ).replace( '}', QChar( 65080 ) );
string = string.replace( '<', QChar( 65087 ) ).replace( '>', QChar( 65088 ) );
string = string.replace( '[', QChar( 65095 ) ).replace( ']', QChar( 65096 ) );
string = string.replace( QChar( 12308 ), QChar( 65081 ) ).replace( QChar( 12309 ), QChar( 65082 ) ); // tortoise shell brackets
string = string.replace( QChar( 12304 ), QChar( 65083 ) ).replace( QChar( 12305 ), QChar( 65084 ) ); // black lenticular brackets
string = string.replace( QChar( 12298 ), QChar( 65085 ) ).replace( QChar( 12299 ), QChar( 65086 ) ); // double angle brackets
string = string.replace( QChar( 12300 ), QChar( 65089 ) ).replace( QChar( 12301 ), QChar( 65090 ) ); // corner brackets
string = string.replace( QChar( 12302 ), QChar( 65091 ) ).replace( QChar( 12303 ), QChar( 65092 ) ); // white corner brackets
return string;
}

QgsStringReplacement::QgsStringReplacement( const QString &match, const QString &replacement, bool caseSensitive, bool wholeWordOnly )
: mMatch( match )
, mReplacement( replacement )
Expand Down
8 changes: 8 additions & 0 deletions src/core/qgsstringutils.h
Expand Up @@ -275,6 +275,14 @@ class CORE_EXPORT QgsStringUtils
* \since QGIS 3.4
*/
static QString wordWrap( const QString &string, int length, bool useMaxLineLength = true, const QString &customDelimiter = QString() );

/**
* Returns a string with characters having vertical representation form substituted.
* \param string input string
* \returns string with substition applied
* \since QGIS 3.10
*/
static QString substituteVerticalCharacters( QString string );
};

#endif //QGSSTRINGUTILS_H
20 changes: 1 addition & 19 deletions src/core/qgstextrenderer.cpp
Expand Up @@ -3552,25 +3552,7 @@ void QgsTextRenderer::drawTextInternal( TextPart drawType,

// apply some character replacement to draw symbols in vertical presentation
QString lines = textLines.join( '\n' );
lines = lines.replace( ',', QChar( 65040 ) ).replace( QChar( 8229 ), QChar( 65072 ) ); // comma & two-dot leader
lines = lines.replace( QChar( 12289 ), QChar( 65041 ) ).replace( QChar( 12290 ), QChar( 65042 ) ); // ideographic comma & full stop
lines = lines.replace( ':', QChar( 65043 ) ).replace( ';', QChar( 65044 ) );
lines = lines.replace( '!', QChar( 65045 ) ).replace( '?', QChar( 65046 ) );
lines = lines.replace( QChar( 12310 ), QChar( 65047 ) ).replace( QChar( 12311 ), QChar( 65048 ) ); // white lenticular brackets
lines = lines.replace( QChar( 8230 ), QChar( 65049 ) ); // three-dot ellipse
lines = lines.replace( QChar( 8212 ), QChar( 65073 ) ).replace( QChar( 8211 ), QChar( 65074 ) ); // em & en dash
lines = lines.replace( '_', QChar( 65075 ) ).replace( QChar( 65103 ), QChar( 65076 ) ); // low line & wavy low line
lines = lines.replace( '(', QChar( 65077 ) ).replace( ')', QChar( 65078 ) );
lines = lines.replace( '{', QChar( 65079 ) ).replace( '}', QChar( 65080 ) );
lines = lines.replace( '<', QChar( 65087 ) ).replace( '>', QChar( 65088 ) );
lines = lines.replace( '[', QChar( 65095 ) ).replace( ']', QChar( 65096 ) );
lines = lines.replace( QChar( 12308 ), QChar( 65081 ) ).replace( QChar( 12309 ), QChar( 65082 ) ); // tortoise shell brackets
lines = lines.replace( QChar( 12304 ), QChar( 65083 ) ).replace( QChar( 12305 ), QChar( 65084 ) ); // black lenticular brackets
lines = lines.replace( QChar( 12298 ), QChar( 65085 ) ).replace( QChar( 12299 ), QChar( 65086 ) ); // double angle brackets
lines = lines.replace( QChar( 12300 ), QChar( 65089 ) ).replace( QChar( 12301 ), QChar( 65090 ) ); // corner brackets
lines = lines.replace( QChar( 12302 ), QChar( 65091 ) ).replace( QChar( 12303 ), QChar( 65092 ) ); // white corner brackets

const auto constTextLines = lines.split( '\n' );
const auto constTextLines = QgsStringUtils::substituteVerticalCharacters( lines ).split( '\n' );
for ( QString line : constTextLines )
{
context.painter()->save();
Expand Down
4 changes: 4 additions & 0 deletions tests/src/python/test_qgsstringutils.py
Expand Up @@ -175,6 +175,10 @@ def testCapitalizeFirst(self):
self.assertEqual(QgsStringUtils.capitalize(' testing abc', QgsStringUtils.ForceFirstLetterToCapital),
' Testing Abc')

def testSubstituteVerticalCharacters(self):
""" test subsitute vertical characters """
self.assertEqual(QgsStringUtils.substituteVerticalCharacters('123{[(45654)]}321'), '123︷﹇︵45654︶﹈︸321')


if __name__ == '__main__':
unittest.main()

0 comments on commit bdf18ee

Please sign in to comment.