Skip to content

Commit e53adc1

Browse files
committed
Add methods to convert QgsTextFormat to and from QFonts
1 parent e35d1d0 commit e53adc1

File tree

4 files changed

+139
-0
lines changed

4 files changed

+139
-0
lines changed

python/core/qgstextrenderer.sip.in

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,6 +1235,8 @@ of rendered text.
12351235
.. seealso:: :py:func:`setFont`
12361236

12371237
.. seealso:: :py:func:`namedStyle`
1238+
1239+
.. seealso:: :py:func:`toQFont`
12381240
%End
12391241

12401242
QFont scaledFont( const QgsRenderContext &context ) const;
@@ -1262,6 +1264,8 @@ of rendered text.
12621264
.. seealso:: :py:func:`font`
12631265

12641266
.. seealso:: :py:func:`setNamedStyle`
1267+
1268+
.. seealso:: :py:func:`fromQFont`
12651269
%End
12661270

12671271
QString namedStyle() const;
@@ -1445,6 +1449,28 @@ Returns new mime data representing the text format settings.
14451449
Caller takes responsibility for deleting the returned object.
14461450

14471451
.. seealso:: :py:func:`fromMimeData`
1452+
%End
1453+
1454+
static QgsTextFormat fromQFont( const QFont &font );
1455+
%Docstring
1456+
Returns a text format matching the settings from an input ``font``.
1457+
Unlike setFont(), this method also handles the size and size units
1458+
from ``font``.
1459+
1460+
.. versionadded:: 3.2
1461+
1462+
.. seealso:: :py:func:`toQFont`
1463+
%End
1464+
1465+
QFont toQFont() const;
1466+
%Docstring
1467+
Returns a QFont matching the relevant settings from this text format.
1468+
Unlike font(), this method also handles the size and size units
1469+
from the text format.
1470+
1471+
.. versionadded:: 3.2
1472+
1473+
.. seealso:: :py:func:`fromQFont`
14481474
%End
14491475

14501476
static QgsTextFormat fromMimeData( const QMimeData *data, bool *ok /Out/ = 0 );

src/core/qgstextrenderer.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1608,6 +1608,55 @@ QMimeData *QgsTextFormat::toMimeData() const
16081608
return mimeData;
16091609
}
16101610

1611+
QgsTextFormat QgsTextFormat::fromQFont( const QFont &font )
1612+
{
1613+
QgsTextFormat format;
1614+
format.setFont( font );
1615+
if ( font.pointSizeF() > 0 )
1616+
{
1617+
format.setSize( font.pointSizeF() );
1618+
format.setSizeUnit( QgsUnitTypes::RenderPoints );
1619+
}
1620+
else if ( font.pixelSize() > 0 )
1621+
{
1622+
format.setSize( font.pixelSize() );
1623+
format.setSizeUnit( QgsUnitTypes::RenderPixels );
1624+
}
1625+
1626+
return format;
1627+
}
1628+
1629+
QFont QgsTextFormat::toQFont() const
1630+
{
1631+
QFont f = font();
1632+
switch ( sizeUnit() )
1633+
{
1634+
case QgsUnitTypes::RenderPoints:
1635+
f.setPointSizeF( size() );
1636+
break;
1637+
1638+
case QgsUnitTypes::RenderMillimeters:
1639+
f.setPointSizeF( size() * 2.83464567 );
1640+
break;
1641+
1642+
case QgsUnitTypes::RenderInches:
1643+
f.setPointSizeF( size() * 72 );
1644+
break;
1645+
1646+
case QgsUnitTypes::RenderPixels:
1647+
f.setPixelSize( static_cast< int >( std::round( size() ) ) );
1648+
break;
1649+
1650+
case QgsUnitTypes::RenderMapUnits:
1651+
case QgsUnitTypes::RenderMetersInMapUnits:
1652+
case QgsUnitTypes::RenderUnknownUnit:
1653+
case QgsUnitTypes::RenderPercentage:
1654+
// no meaning here
1655+
break;
1656+
}
1657+
return f;
1658+
}
1659+
16111660
QgsTextFormat QgsTextFormat::fromMimeData( const QMimeData *data, bool *ok )
16121661
{
16131662
if ( ok )

src/core/qgstextrenderer.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,6 +1055,7 @@ class CORE_EXPORT QgsTextFormat
10551055
* \see scaledFont()
10561056
* \see setFont()
10571057
* \see namedStyle()
1058+
* \see toQFont()
10581059
*/
10591060
QFont font() const;
10601061

@@ -1075,6 +1076,7 @@ class CORE_EXPORT QgsTextFormat
10751076
* \param font desired font
10761077
* \see font()
10771078
* \see setNamedStyle()
1079+
* \see fromQFont()
10781080
*/
10791081
void setFont( const QFont &font );
10801082

@@ -1224,6 +1226,24 @@ class CORE_EXPORT QgsTextFormat
12241226
*/
12251227
QMimeData *toMimeData() const SIP_FACTORY;
12261228

1229+
/**
1230+
* Returns a text format matching the settings from an input \a font.
1231+
* Unlike setFont(), this method also handles the size and size units
1232+
* from \a font.
1233+
* \since QGIS 3.2
1234+
* \see toQFont()
1235+
*/
1236+
static QgsTextFormat fromQFont( const QFont &font );
1237+
1238+
/**
1239+
* Returns a QFont matching the relevant settings from this text format.
1240+
* Unlike font(), this method also handles the size and size units
1241+
* from the text format.
1242+
* \since QGIS 3.2
1243+
* \see fromQFont()
1244+
*/
1245+
QFont toQFont() const;
1246+
12271247
/**
12281248
* Attempts to parse the provided mime \a data as a QgsTextFormat.
12291249
* If data can be parsed as a text format, \a ok will be set to true.

tests/src/python/test_qgstextrenderer.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,50 @@ def testFontFoundFromXml(self):
392392
f.readXml(parent, QgsReadWriteContext())
393393
self.assertTrue(f.fontFound())
394394

395+
def testFromQFont(self):
396+
qfont = getTestFont()
397+
qfont.setPointSizeF(16.5)
398+
qfont.setLetterSpacing(QFont.AbsoluteSpacing, 3)
399+
400+
format = QgsTextFormat.fromQFont(qfont)
401+
self.assertEqual(format.font().family(), qfont.family())
402+
self.assertEqual(format.font().letterSpacing(), 3.0)
403+
self.assertEqual(format.size(), 16.5)
404+
self.assertEqual(format.sizeUnit(), QgsUnitTypes.RenderPoints)
405+
406+
qfont.setPixelSize(12)
407+
format = QgsTextFormat.fromQFont(qfont)
408+
self.assertEqual(format.size(), 12.0)
409+
self.assertEqual(format.sizeUnit(), QgsUnitTypes.RenderPixels)
410+
411+
def testToQFont(self):
412+
s = QgsTextFormat()
413+
f = getTestFont()
414+
f.setLetterSpacing(QFont.AbsoluteSpacing, 3)
415+
s.setFont(f)
416+
s.setNamedStyle('Italic')
417+
s.setSize(5.5)
418+
s.setSizeUnit(QgsUnitTypes.RenderPoints)
419+
420+
qfont = s.toQFont()
421+
self.assertEqual(qfont.family(), f.family())
422+
self.assertEqual(qfont.pointSizeF(), 5.5)
423+
self.assertEqual(qfont.letterSpacing(), 3.0)
424+
425+
s.setSize(5)
426+
s.setSizeUnit(QgsUnitTypes.RenderPixels)
427+
qfont = s.toQFont()
428+
self.assertEqual(qfont.pixelSize(), 5)
429+
430+
s.setSize(5)
431+
s.setSizeUnit(QgsUnitTypes.RenderMillimeters)
432+
qfont = s.toQFont()
433+
self.assertAlmostEqual(qfont.pointSizeF(), 14.17, 2)
434+
435+
s.setSizeUnit(QgsUnitTypes.RenderInches)
436+
qfont = s.toQFont()
437+
self.assertAlmostEqual(qfont.pointSizeF(), 360.0, 2)
438+
395439
def imageCheck(self, name, reference_image, image):
396440
self.report += "<h2>Render {}</h2>\n".format(name)
397441
temp_dir = QDir.tempPath() + '/'

0 commit comments

Comments
 (0)