Skip to content
Permalink
Browse files
Correctly set font style (bold/italic) when converting MapBox styles
  • Loading branch information
nyalldawson authored and nirvn committed Dec 31, 2021
1 parent 23d6559 commit c40d32a89be8999c94aba208bed52a489897632c
Showing with 15 additions and 9 deletions.
  1. +15 −9 src/core/vectortile/qgsmapboxglstyleconverter.cpp
@@ -1043,6 +1043,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
@@ -1094,7 +1096,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;
@@ -1119,7 +1120,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(),
@@ -1128,7 +1129,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
{
@@ -1139,10 +1140,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
{
@@ -1163,12 +1164,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;
}
}
@@ -1181,13 +1181,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
@@ -1315,7 +1317,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 c40d32a

Please sign in to comment.