Skip to content
Permalink
Browse files

Convert text-rotation-alignment:viewport for line labels

  • Loading branch information
nirvn authored and nyalldawson committed Sep 13, 2020
1 parent 31b2b89 commit 06f00da53ce503cca658508add68186ad5783f0a
Showing with 51 additions and 39 deletions.
  1. +51 −39 src/core/vectortile/qgsmapboxglstyleconverter.cpp
@@ -1030,54 +1030,66 @@ void QgsMapBoxGlStyleConverter::parseSymbolLayer( const QVariantMap &jsonLayer,
labelSettings.lineSettings().setPlacementFlags( QgsLabeling::OnLine );
geometryType = QgsWkbTypes::LineGeometry;

QPointF textOffset;
QString textOffsetProperty;
if ( jsonLayout.contains( QStringLiteral( "text-offset" ) ) )
if ( jsonLayout.contains( QStringLiteral( "text-rotation-alignment" ) ) )
{
const QVariant jsonTextOffset = jsonLayout.value( QStringLiteral( "text-offset" ) );

// units are ems!
switch ( jsonTextOffset.type() )
const QString textRotationAlignment = jsonLayout.value( QStringLiteral( "text-rotation-alignment" ) ).toString();
if ( textRotationAlignment == QLatin1String( "viewport" ) )
{
case QVariant::Map:
textOffsetProperty = parseInterpolatePointByZoom( jsonTextOffset.toMap(), context, textSizeProperty.isEmpty() ? textSize : 1.0, &textOffset );
if ( textSizeProperty.isEmpty() )
{
ddLabelProperties.setProperty( QgsPalLayerSettings::LabelDistance, QStringLiteral( "abs(array_get(%1,1))-%2" ).arg( textOffsetProperty ).arg( textSize ) );
}
else
{
ddLabelProperties.setProperty( QgsPalLayerSettings::LabelDistance, QStringLiteral( "with_variable('text_size',%2,abs(array_get(%1,1))*@text_size-@text_size)" ).arg( textOffsetProperty ).arg( textSizeProperty ) );
}
ddLabelProperties.setProperty( QgsPalLayerSettings::LinePlacementOptions, QStringLiteral( "if(array_get(%1,1)>0,'BL','AL')" ).arg( textOffsetProperty ) );
break;

case QVariant::List:
case QVariant::StringList:
textOffset = QPointF( jsonTextOffset.toList().value( 0 ).toDouble() * textSize,
jsonTextOffset.toList().value( 1 ).toDouble() * textSize );
break;

default:
context.pushWarning( QObject::tr( "Skipping non-implemented text-offset expression" ) );
break;
labelSettings.placement = QgsPalLayerSettings::Horizontal;
}
}

if ( !textOffset.isNull() )
if ( labelSettings.placement == QgsPalLayerSettings::Curved )
{
QPointF textOffset;
QString textOffsetProperty;
if ( jsonLayout.contains( QStringLiteral( "text-offset" ) ) )
{
labelSettings.distUnits = context.targetUnit();
labelSettings.dist = std::abs( textOffset.y() ) - textSize;
labelSettings.lineSettings().setPlacementFlags( textOffset.y() > 0.0 ? QgsLabeling::BelowLine : QgsLabeling::AboveLine );
if ( !textSizeProperty.isEmpty() && textOffsetProperty.isEmpty() )
const QVariant jsonTextOffset = jsonLayout.value( QStringLiteral( "text-offset" ) );

// units are ems!
switch ( jsonTextOffset.type() )
{
ddLabelProperties.setProperty( QgsPalLayerSettings::LabelDistance, QStringLiteral( "with_variable('text_size',%2,%1*@text_size-@text_size)" ).arg( std::abs( textOffset.y() / textSize ) ).arg( ( textSizeProperty ) ) );
case QVariant::Map:
textOffsetProperty = parseInterpolatePointByZoom( jsonTextOffset.toMap(), context, textSizeProperty.isEmpty() ? textSize : 1.0, &textOffset );
if ( textSizeProperty.isEmpty() )
{
ddLabelProperties.setProperty( QgsPalLayerSettings::LabelDistance, QStringLiteral( "abs(array_get(%1,1))-%2" ).arg( textOffsetProperty ).arg( textSize ) );
}
else
{
ddLabelProperties.setProperty( QgsPalLayerSettings::LabelDistance, QStringLiteral( "with_variable('text_size',%2,abs(array_get(%1,1))*@text_size-@text_size)" ).arg( textOffsetProperty ).arg( textSizeProperty ) );
}
ddLabelProperties.setProperty( QgsPalLayerSettings::LinePlacementOptions, QStringLiteral( "if(array_get(%1,1)>0,'BL','AL')" ).arg( textOffsetProperty ) );
break;

case QVariant::List:
case QVariant::StringList:
textOffset = QPointF( jsonTextOffset.toList().value( 0 ).toDouble() * textSize,
jsonTextOffset.toList().value( 1 ).toDouble() * textSize );
break;

default:
context.pushWarning( QObject::tr( "Skipping non-implemented text-offset expression" ) );
break;
}

if ( !textOffset.isNull() )
{
labelSettings.distUnits = context.targetUnit();
labelSettings.dist = std::abs( textOffset.y() ) - textSize;
labelSettings.lineSettings().setPlacementFlags( textOffset.y() > 0.0 ? QgsLabeling::BelowLine : QgsLabeling::AboveLine );
if ( !textSizeProperty.isEmpty() && textOffsetProperty.isEmpty() )
{
ddLabelProperties.setProperty( QgsPalLayerSettings::LabelDistance, QStringLiteral( "with_variable('text_size',%2,%1*@text_size-@text_size)" ).arg( std::abs( textOffset.y() / textSize ) ).arg( ( textSizeProperty ) ) );
}
}
}
}

if ( textOffset.isNull() )
{
labelSettings.lineSettings().setPlacementFlags( QgsLabeling::OnLine );
if ( textOffset.isNull() )
{
labelSettings.lineSettings().setPlacementFlags( QgsLabeling::OnLine );
}
}
}
}

0 comments on commit 06f00da

Please sign in to comment.
You can’t perform that action at this time.