Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
New QgsFontUtils::asCSS method for converting a QFont to a CSS
string representation (as close as possible)
  • Loading branch information
nyalldawson committed Mar 30, 2016
1 parent 567b4be commit 6526cf5
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
11 changes: 11 additions & 0 deletions python/core/qgsfontutils.sip
Expand Up @@ -110,4 +110,15 @@ class QgsFontUtils
* @see translateNamedStyle
*/
static QString untranslateNamedStyle( const QString& namedStyle );

/** Returns a CSS string representing the specified font as closely as possible.
* @param font QFont to convert
* @param pointToPixelMultiplier scaling factor to apply to convert point sizes to pixel font sizes.
* The CSS returned by this function will always use pixels for font sizes, so this parameter
* should be set to a suitable value to convert point sizes to pixels (eg taking into account
* desination DPI)
* @returns partial CSS string, eg "font-family: Comic Sans; font-size: 12px;"
* @note added in QGIS 2.16
*/
static QString asCSS( const QFont& font, double pointToPixelMultiplier = 1.0 );
};
62 changes: 62 additions & 0 deletions src/core/qgsfontutils.cpp
Expand Up @@ -400,3 +400,65 @@ QString QgsFontUtils::untranslateNamedStyle( const QString& namedStyle )
}
return words.join( " " );
}

QString QgsFontUtils::asCSS( const QFont& font, double pointToPixelScale )
{
QString css = QString( "font-family: " ) + font.family() + ';';

//style
css += "font-style: ";
switch ( font.style() )
{
case QFont::StyleNormal:
css += "normal";
break;
case QFont::StyleItalic:
css += "italic";
break;
case QFont::StyleOblique:
css += "oblique";
break;
}
css += ';';

//weight
int cssWeight = 400;
switch ( font.weight() )
{
case QFont::Light:
cssWeight = 300;
break;
case QFont::Normal:
cssWeight = 400;
break;
case QFont::DemiBold:
cssWeight = 600;
break;
case QFont::Bold:
cssWeight = 700;
break;
case QFont::Black:
cssWeight = 900;
break;
#if QT_VERSION >= 0x050000
case QFont::Thin:
cssWeight = 100;
break;
case QFont::ExtraLight:
cssWeight = 200;
break;
case QFont::Medium:
cssWeight = 500;
break;
case QFont::ExtraBold:
cssWeight = 800;
break;
#endif
}
css += QString( "font-weight: %1;" ).arg( cssWeight );

//size
css += QString( "font-size: %1px;" ).arg( font.pointSizeF() >= 0 ? font.pointSizeF() * pointToPixelScale : font.pixelSize() );

return css;
}
11 changes: 11 additions & 0 deletions src/core/qgsfontutils.h
Expand Up @@ -129,6 +129,17 @@ class CORE_EXPORT QgsFontUtils
* @see translateNamedStyle
*/
static QString untranslateNamedStyle( const QString& namedStyle );

/** Returns a CSS string representing the specified font as closely as possible.
* @param font QFont to convert
* @param pointToPixelMultiplier scaling factor to apply to convert point sizes to pixel font sizes.
* The CSS returned by this function will always use pixels for font sizes, so this parameter
* should be set to a suitable value to convert point sizes to pixels (eg taking into account
* desination DPI)
* @returns partial CSS string, eg "font-family: Comic Sans; font-size: 12px;"
* @note added in QGIS 2.16
*/
static QString asCSS( const QFont& font, double pointToPixelMultiplier = 1.0 );
};

#endif // QGSFONTUTILS_H
17 changes: 17 additions & 0 deletions tests/src/core/testqgsfontutils.cpp
Expand Up @@ -33,6 +33,7 @@ class TestQgsFontUtils: public QObject
void cleanup();// will be called after every testfunction.
void xmlMethods(); //test saving and reading from xml
void fromChildNode(); //test reading from child node
void toCss(); //test converting font to CSS

private:

Expand Down Expand Up @@ -147,5 +148,21 @@ void TestQgsFontUtils::fromChildNode()
QCOMPARE( f2.styleName(), f1.styleName() );
}

void TestQgsFontUtils::toCss()
{
QFont f1 = QgsFontUtils::getStandardTestFont();
f1.setPixelSize( 48 );
QCOMPARE( QgsFontUtils::asCSS( f1 ), QString( "font-family: QGIS Vera Sans;font-style: normal;font-weight: 400;font-size: 48px;" ) );
f1.setItalic( true );
QCOMPARE( QgsFontUtils::asCSS( f1 ), QString( "font-family: QGIS Vera Sans;font-style: italic;font-weight: 400;font-size: 48px;" ) );
f1.setStyle( QFont::StyleOblique );
QCOMPARE( QgsFontUtils::asCSS( f1 ), QString( "font-family: QGIS Vera Sans;font-style: oblique;font-weight: 400;font-size: 48px;" ) );
f1.setBold( true );
QCOMPARE( QgsFontUtils::asCSS( f1 ), QString( "font-family: QGIS Vera Sans;font-style: oblique;font-weight: 700;font-size: 48px;" ) );
f1.setPointSizeF( 12.5 );
QCOMPARE( QgsFontUtils::asCSS( f1 ), QString( "font-family: QGIS Vera Sans;font-style: oblique;font-weight: 700;font-size: 12.5px;" ) );
QCOMPARE( QgsFontUtils::asCSS( f1, 10 ), QString( "font-family: QGIS Vera Sans;font-style: oblique;font-weight: 700;font-size: 125px;" ) );
}

QTEST_MAIN( TestQgsFontUtils )
#include "testqgsfontutils.moc"

0 comments on commit 6526cf5

Please sign in to comment.