Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Correctly set font style (bold/italic) when converting MapBox styles
  • Loading branch information
nyalldawson committed Dec 31, 2021
1 parent 03d75bf commit cc404c9
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions src/core/vectortile/qgsmapboxglstyleconverter.cpp
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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(),
Expand All @@ -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
{
Expand All @@ -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
{
Expand All @@ -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;
}
}
Expand All @@ -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
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit cc404c9

Please sign in to comment.