Skip to content

Commit

Permalink
If dingbats font isn't installed on system, find a good candidate
Browse files Browse the repository at this point in the history
symbol font from the available installed fonts

Fixes #46206
  • Loading branch information
nyalldawson committed Nov 29, 2021
1 parent d44fa83 commit 251ba5a
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/gui/symbology/qgslayerpropertieswidget.cpp
Expand Up @@ -47,6 +47,7 @@
#include "qgsmarkersymbol.h"
#include "qgslinesymbol.h"
#include "qgsfillsymbol.h"
#include "qgsmarkersymbollayer.h"

static bool _initWidgetFunction( const QString &name, QgsSymbolLayerWidgetFunc f )
{
Expand Down Expand Up @@ -389,6 +390,48 @@ void QgsLayerPropertiesWidget::layerTypeChanged()
}
}

// special logic for when NEW symbol layers are created from GUI only...
// TODO: find a nicer generic way to handle this!
if ( QgsFontMarkerSymbolLayer *fontMarker = dynamic_cast< QgsFontMarkerSymbolLayer * >( newLayer ) )
{
const QString defaultFont = fontMarker->fontFamily();
const QFontDatabase fontDb;
if ( !fontDb.hasFamily( defaultFont ) )
{
// default font marker font choice doesn't exist on system, so just use first available symbol font
const QStringList candidates = fontDb.families( QFontDatabase::WritingSystem::Symbol );
bool foundGoodCandidate = false;
for ( const QString &candidate : candidates )
{
if ( fontDb.writingSystems( candidate ).size() == 1 )
{
// family ONLY offers symbol writing systems, so it's a good candidate!
fontMarker->setFontFamily( candidate );
foundGoodCandidate = true;
break;
}
}
if ( !foundGoodCandidate && !candidates.empty() )
{
// fallback to first available family which advertises symbol writing system
QString candidate = candidates.at( 0 );
fontMarker->setFontFamily( candidate );
}
}

// search (briefly!!) for a unicode character which actually exists in the font
const QFontMetrics fontMetrics( fontMarker->fontFamily() );
ushort character = fontMarker->character().at( 0 ).unicode();
for ( ; character < 1000; ++character )
{
if ( fontMetrics.inFont( QChar( character ) ) )
{
fontMarker->setCharacter( QChar( character ) );
break;
}
}
}

updateSymbolLayerWidget( newLayer );
emit changeLayer( newLayer );
}
Expand Down

0 comments on commit 251ba5a

Please sign in to comment.