Skip to content

Commit 251ba5a

Browse files
committedNov 29, 2021
If dingbats font isn't installed on system, find a good candidate
symbol font from the available installed fonts Fixes #46206
1 parent d44fa83 commit 251ba5a

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
 

‎src/gui/symbology/qgslayerpropertieswidget.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#include "qgsmarkersymbol.h"
4848
#include "qgslinesymbol.h"
4949
#include "qgsfillsymbol.h"
50+
#include "qgsmarkersymbollayer.h"
5051

5152
static bool _initWidgetFunction( const QString &name, QgsSymbolLayerWidgetFunc f )
5253
{
@@ -389,6 +390,48 @@ void QgsLayerPropertiesWidget::layerTypeChanged()
389390
}
390391
}
391392

393+
// special logic for when NEW symbol layers are created from GUI only...
394+
// TODO: find a nicer generic way to handle this!
395+
if ( QgsFontMarkerSymbolLayer *fontMarker = dynamic_cast< QgsFontMarkerSymbolLayer * >( newLayer ) )
396+
{
397+
const QString defaultFont = fontMarker->fontFamily();
398+
const QFontDatabase fontDb;
399+
if ( !fontDb.hasFamily( defaultFont ) )
400+
{
401+
// default font marker font choice doesn't exist on system, so just use first available symbol font
402+
const QStringList candidates = fontDb.families( QFontDatabase::WritingSystem::Symbol );
403+
bool foundGoodCandidate = false;
404+
for ( const QString &candidate : candidates )
405+
{
406+
if ( fontDb.writingSystems( candidate ).size() == 1 )
407+
{
408+
// family ONLY offers symbol writing systems, so it's a good candidate!
409+
fontMarker->setFontFamily( candidate );
410+
foundGoodCandidate = true;
411+
break;
412+
}
413+
}
414+
if ( !foundGoodCandidate && !candidates.empty() )
415+
{
416+
// fallback to first available family which advertises symbol writing system
417+
QString candidate = candidates.at( 0 );
418+
fontMarker->setFontFamily( candidate );
419+
}
420+
}
421+
422+
// search (briefly!!) for a unicode character which actually exists in the font
423+
const QFontMetrics fontMetrics( fontMarker->fontFamily() );
424+
ushort character = fontMarker->character().at( 0 ).unicode();
425+
for ( ; character < 1000; ++character )
426+
{
427+
if ( fontMetrics.inFont( QChar( character ) ) )
428+
{
429+
fontMarker->setCharacter( QChar( character ) );
430+
break;
431+
}
432+
}
433+
}
434+
392435
updateSymbolLayerWidget( newLayer );
393436
emit changeLayer( newLayer );
394437
}

0 commit comments

Comments
 (0)
Please sign in to comment.