Skip to content
Permalink
Browse files
Correctly set font style (bold/italic) when converting MapBox styles
  • Loading branch information
nyalldawson committed Dec 31, 2021
1 parent 03d75bf commit cc404c9e81b5cbff562f5090c8b66f698832d371
Showing with 15 additions and 9 deletions.
  1. +15 −9 src/core/vectortile/qgsmapboxglstyleconverter.cpp
@@ -1091,6 +1091,8 @@ void QgsMapBoxGlStyleConverter::parseSymbolLayer( const QVariantMap &jsonLayer,
QFont textFont;
bool foundFont = false;
QString fontName;
QString fontStyleName;

if ( jsonLayout.contains( QStringLiteral( "text-font" ) ) )
{
auto splitFontFamily = []( const QString & fontName, QString & family, QString & style ) -> bool
@@ -1142,7 +1144,6 @@ void QgsMapBoxGlStyleConverter::parseSymbolLayer( const QVariantMap &jsonLayer,
QString familyCaseString = QStringLiteral( "CASE " );
QString styleCaseString = QStringLiteral( "CASE " );
QString fontFamily;
QString fontStyle;
const QVariantList stops = jsonTextFont.toMap().value( QStringLiteral( "stops" ) ).toList();

bool error = false;
@@ -1167,7 +1168,7 @@ void QgsMapBoxGlStyleConverter::parseSymbolLayer( const QVariantMap &jsonLayer,
break;
}

if ( splitFontFamily( bv, fontFamily, fontStyle ) )
if ( splitFontFamily( bv, fontFamily, fontStyleName ) )
{
familyCaseString += QStringLiteral( "WHEN @vector_tile_zoom > %1 AND @vector_tile_zoom <= %2 "
"THEN %3 " ).arg( bz.toString(),
@@ -1176,7 +1177,7 @@ void QgsMapBoxGlStyleConverter::parseSymbolLayer( const QVariantMap &jsonLayer,
styleCaseString += QStringLiteral( "WHEN @vector_tile_zoom > %1 AND @vector_tile_zoom <= %2 "
"THEN %3 " ).arg( bz.toString(),
tz.toString(),
QgsExpression::quotedValue( fontStyle ) );
QgsExpression::quotedValue( fontStyleName ) );
}
else
{
@@ -1187,10 +1188,10 @@ void QgsMapBoxGlStyleConverter::parseSymbolLayer( const QVariantMap &jsonLayer,
break;

const QString bv = stops.constLast().toList().value( 1 ).type() == QVariant::String ? stops.constLast().toList().value( 1 ).toString() : stops.constLast().toList().value( 1 ).toList().value( 0 ).toString();
if ( splitFontFamily( bv, fontFamily, fontStyle ) )
if ( splitFontFamily( bv, fontFamily, fontStyleName ) )
{
familyCaseString += QStringLiteral( "ELSE %1 END" ).arg( QgsExpression::quotedValue( fontFamily ) );
styleCaseString += QStringLiteral( "ELSE %1 END" ).arg( QgsExpression::quotedValue( fontStyle ) );
styleCaseString += QStringLiteral( "ELSE %1 END" ).arg( QgsExpression::quotedValue( fontStyleName ) );
}
else
{
@@ -1211,12 +1212,11 @@ void QgsMapBoxGlStyleConverter::parseSymbolLayer( const QVariantMap &jsonLayer,
}

QString fontFamily;
QString fontStyle;
if ( splitFontFamily( fontName, fontFamily, fontStyle ) )
if ( splitFontFamily( fontName, fontFamily, fontStyleName ) )
{
textFont = QFont( fontFamily );
if ( !fontStyle.isEmpty() )
textFont.setStyleName( fontStyle );
if ( !fontStyleName.isEmpty() )
textFont.setStyleName( fontStyleName );
foundFont = true;
}
}
@@ -1229,13 +1229,15 @@ void QgsMapBoxGlStyleConverter::parseSymbolLayer( const QVariantMap &jsonLayer,
fontName = QStringLiteral( "Open Sans" );
textFont = QFont( fontName );
textFont.setStyleName( QStringLiteral( "Regular" ) );
fontStyleName = QStringLiteral( "Regular" );
foundFont = true;
}
else if ( QgsFontUtils::fontFamilyHasStyle( QStringLiteral( "Arial Unicode MS" ), QStringLiteral( "Regular" ) ) )
{
fontName = QStringLiteral( "Arial Unicode MS" );
textFont = QFont( fontName );
textFont.setStyleName( QStringLiteral( "Regular" ) );
fontStyleName = QStringLiteral( "Regular" );
foundFont = true;
}
else
@@ -1363,7 +1365,11 @@ void QgsMapBoxGlStyleConverter::parseSymbolLayer( const QVariantMap &jsonLayer,
if ( textSize >= 0 )
format.setSize( textSize );
if ( foundFont )
{
format.setFont( textFont );
if ( !fontStyleName.isEmpty() )
format.setNamedStyle( fontStyleName );
}
if ( textLetterSpacing > 0 )
{
QFont f = format.font();

0 comments on commit cc404c9

Please sign in to comment.