Skip to content

Commit

Permalink
[fonts] Insure that text formats written to XML will save the loaded …
Browse files Browse the repository at this point in the history
…family name on system missing the font family unless the font has been changed
  • Loading branch information
nirvn authored and nyalldawson committed Oct 23, 2024
1 parent 58fcd9c commit b311222
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/core/textrenderer/qgstextformat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ void QgsTextFormat::setFont( const QFont &font )
{
d->isValid = true;
d->textFont = font;
d->originalFontFamily.clear();
}

QString QgsTextFormat::namedStyle() const
Expand Down Expand Up @@ -466,7 +467,8 @@ void QgsTextFormat::readFromLayer( QgsVectorLayer *layer )
{
d->isValid = true;
QFont appFont = QApplication::font();
mTextFontFamily = QgsApplication::fontManager()->processFontFamilyName( layer->customProperty( QStringLiteral( "labeling/fontFamily" ), QVariant( appFont.family() ) ).toString() );
d->originalFontFamily = QgsApplication::fontManager()->processFontFamilyName( layer->customProperty( QStringLiteral( "labeling/fontFamily" ), QVariant( appFont.family() ) ).toString() );
mTextFontFamily = d->originalFontFamily;
QString fontFamily = mTextFontFamily;
if ( mTextFontFamily != appFont.family() && !QgsFontUtils::fontFamilyMatchOnSystem( mTextFontFamily ) )
{
Expand Down Expand Up @@ -555,7 +557,8 @@ void QgsTextFormat::readXml( const QDomElement &elem, const QgsReadWriteContext
else
textStyleElem = elem.firstChildElement( QStringLiteral( "text-style" ) );
QFont appFont = QApplication::font();
mTextFontFamily = QgsApplication::fontManager()->processFontFamilyName( textStyleElem.attribute( QStringLiteral( "fontFamily" ), appFont.family() ) );
d->originalFontFamily = QgsApplication::fontManager()->processFontFamilyName( textStyleElem.attribute( QStringLiteral( "fontFamily" ), appFont.family() ) );
mTextFontFamily = d->originalFontFamily;
QString fontFamily = mTextFontFamily;

const QDomElement familiesElem = textStyleElem.firstChildElement( QStringLiteral( "families" ) );
Expand Down Expand Up @@ -749,7 +752,7 @@ QDomElement QgsTextFormat::writeXml( QDomDocument &doc, const QgsReadWriteContex
{
// text style
QDomElement textStyleElem = doc.createElement( QStringLiteral( "text-style" ) );
textStyleElem.setAttribute( QStringLiteral( "fontFamily" ), d->textFont.family() );
textStyleElem.setAttribute( QStringLiteral( "fontFamily" ), !d->originalFontFamily.isEmpty() ? d->originalFontFamily : d->textFont.family() );

QDomElement familiesElem = doc.createElement( QStringLiteral( "families" ) );
for ( const QString &family : std::as_const( d->families ) )
Expand Down
3 changes: 3 additions & 0 deletions src/core/textrenderer/qgstextrenderer_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ class QgsTextSettingsPrivate : public QSharedData
QgsTextSettingsPrivate( const QgsTextSettingsPrivate &other )
: QSharedData( other )
, isValid( other.isValid )
, originalFontFamily( other.originalFontFamily )
, textFont( other.textFont )
, families( other.families )
, textNamedStyle( other.textNamedStyle )
Expand All @@ -289,6 +290,8 @@ class QgsTextSettingsPrivate : public QSharedData
}

bool isValid = false;

QString originalFontFamily;
QFont textFont;
QStringList families;
QString textNamedStyle;
Expand Down
1 change: 1 addition & 0 deletions tests/src/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,7 @@ if (WITH_GUI)
ADD_PYTHON_TEST(PyQgsSymbolButton test_qgssymbolbutton.py)
ADD_PYTHON_TEST(PyQgsTabWidget test_qgstabwidget.py)
ADD_PYTHON_TEST(PyQgsTextFormatWidget test_qgstextformatwidget.py)
ADD_PYTHON_TEST(PyQgsTextFormat test_qgstextformat.py)
ADD_PYTHON_TEST(PyQgsTreeWidgetItem test_qgstreewidgetitem.py)
ADD_PYTHON_TEST(PyQgsValidityResultsWidget test_qgsvalidityresultswidget.py)
ADD_PYTHON_TEST(PyQgsVectorLayer test_qgsvectorlayer.py)
Expand Down
55 changes: 55 additions & 0 deletions tests/src/python/test_qgstextformat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""QGIS Unit tests for QgsTextFormat.
.. note:: This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
"""
__author__ = 'Mathieu Pellerin'
__date__ = '2024-10-20'
__copyright__ = 'Copyright 2024, The QGIS Project'

from qgis.PyQt.QtGui import QFont
from qgis.PyQt.QtXml import (
QDomDocument,
QDomElement,
)
from qgis.core import (
QgsTextFormat,
QgsReadWriteContext,
)
import unittest
from qgis.testing import start_app, QgisTestCase
from utilities import getTestFont

start_app()


class PyQgsTextFormat(QgisTestCase):

def testRestoringAndSavingMissingFont(self):
# test that a missing font on text format load will still save with the same missing font unless manually changed
document = QDomDocument()
document.setContent('<text-style fontFamily="__MISSING__" previewBkgrdColor="255,255,255,255,rgb:1,1,1,1" fieldName="" tabStopDistance="80" tabStopDistanceMapUnitScale="3x:0,0,0,0,0,0" useSubstitutions="0" forcedItalic="0" fontWeight="50" multilineHeightUnit="Percentage" allowHtml="0" fontKerning="1" multilineHeight="1" fontSizeUnit="Point" fontSize="11" blendMode="0" tabStopDistanceUnit="Point" isExpression="1" fontStrikeout="0" fontLetterSpacing="0" fontSizeMapUnitScale="3x:0,0,0,0,0,0" textColor="50,50,50,255,rgb:0.19607843137254902,0.19607843137254902,0.19607843137254902,1" legendString="Aa" textOrientation="horizontal" namedStyle="Regular" fontItalic="0" fontUnderline="0" forcedBold="0" capitalization="0" textOpacity="1" fontWordSpacing="0"><families/><substitutions/></text-style>')

context = QgsReadWriteContext()
text_format = QgsTextFormat()
text_format.readXml(document.documentElement(), context)

self.assertFalse(text_format.fontFound())
self.assertTrue(text_format.font().family() != "__MISSING__")

# when writign the settings to XML, the missing font family should still be there
element = text_format.writeXml(document, context)
self.assertEqual(element.attribute("fontFamily"), "__MISSING__")

font = getTestFont()
text_format.setFont(font)

# when writign the settings to XML, the originally missing font family should have been replaced by the new font family
element = text_format.writeXml(document, context)
self.assertEqual(element.attribute("fontFamily"), "QGIS Vera Sans")


if __name__ == '__main__':
unittest.main()

0 comments on commit b311222

Please sign in to comment.