Skip to content

Commit

Permalink
Push text format rendering setting into QgsLayoutExporter
Browse files Browse the repository at this point in the history
(cherry picked from commit 96507c4)
  • Loading branch information
nyalldawson committed Dec 11, 2018
1 parent 8b3762c commit 4631a0b
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
4 changes: 4 additions & 0 deletions python/core/auto_generated/layout/qgslayoutexporter.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ Constructor for PdfExportSettings

QgsLayoutRenderContext::Flags flags;

QgsRenderContext::TextRenderFormat textRenderFormat;

};

ExportResult exportToPdf( const QString &filePath, const QgsLayoutExporter::PdfExportSettings &settings );
Expand Down Expand Up @@ -285,6 +287,8 @@ Constructor for SvgExportSettings

QgsLayoutRenderContext::Flags flags;

QgsRenderContext::TextRenderFormat textRenderFormat;

};

ExportResult exportToSvg( const QString &filePath, const QgsLayoutExporter::SvgExportSettings &settings );
Expand Down
8 changes: 8 additions & 0 deletions src/core/layout/qgslayoutexporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ class LayoutContextSettingsRestorer
: mLayout( layout )
, mPreviousDpi( layout->renderContext().dpi() )
, mPreviousFlags( layout->renderContext().flags() )
, mPreviousTextFormat( layout->renderContext().textRenderFormat() )
, mPreviousExportLayer( layout->renderContext().currentExportLayer() )
{
}
Expand All @@ -305,6 +306,7 @@ class LayoutContextSettingsRestorer
{
mLayout->renderContext().setDpi( mPreviousDpi );
mLayout->renderContext().setFlags( mPreviousFlags );
mLayout->renderContext().setTextRenderFormat( mPreviousTextFormat );
mLayout->renderContext().setCurrentExportLayer( mPreviousExportLayer );
}

Expand All @@ -315,6 +317,7 @@ class LayoutContextSettingsRestorer
QgsLayout *mLayout = nullptr;
double mPreviousDpi = 0;
QgsLayoutRenderContext::Flags mPreviousFlags = nullptr;
QgsRenderContext::TextRenderFormat mPreviousTextFormat = QgsRenderContext::TextFormatAlwaysOutlines;
int mPreviousExportLayer = 0;
};
///@endcond PRIVATE
Expand Down Expand Up @@ -494,6 +497,8 @@ QgsLayoutExporter::ExportResult QgsLayoutExporter::exportToPdf( const QString &f

mLayout->renderContext().setFlag( QgsLayoutRenderContext::FlagForceVectorOutput, settings.forceVectorOutput );

mLayout->renderContext().setTextRenderFormat( settings.textRenderFormat );

QPrinter printer;
preparePrintAsPdf( mLayout, printer, filePath );
preparePrint( mLayout, printer, false );
Expand Down Expand Up @@ -563,6 +568,8 @@ QgsLayoutExporter::ExportResult QgsLayoutExporter::exportToPdf( QgsAbstractLayou

iterator->layout()->renderContext().setFlag( QgsLayoutRenderContext::FlagForceVectorOutput, settings.forceVectorOutput );

iterator->layout()->renderContext().setTextRenderFormat( settings.textRenderFormat );

if ( first )
{
preparePrintAsPdf( iterator->layout(), printer, fileName );
Expand Down Expand Up @@ -778,6 +785,7 @@ QgsLayoutExporter::ExportResult QgsLayoutExporter::exportToSvg( const QString &f
mLayout->renderContext().setDpi( settings.dpi );

mLayout->renderContext().setFlag( QgsLayoutRenderContext::FlagForceVectorOutput, settings.forceVectorOutput );
mLayout->renderContext().setTextRenderFormat( s.textRenderFormat );

QFileInfo fi( filePath );
PageExportDetails pageDetails;
Expand Down
16 changes: 16 additions & 0 deletions src/core/layout/qgslayoutexporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,14 @@ class CORE_EXPORT QgsLayoutExporter
*/
QgsLayoutRenderContext::Flags flags = nullptr;

/**
* Text rendering format, which controls how text should be rendered in the export (e.g.
* as paths or real text objects).
*
* \since QGIS 3.4.3
*/
QgsRenderContext::TextRenderFormat textRenderFormat = QgsRenderContext::TextFormatAlwaysOutlines;

};

/**
Expand Down Expand Up @@ -415,6 +423,14 @@ class CORE_EXPORT QgsLayoutExporter
*/
QgsLayoutRenderContext::Flags flags = nullptr;

/**
* Text rendering format, which controls how text should be rendered in the export (e.g.
* as paths or real text objects).
*
* \since QGIS 3.4.3
*/
QgsRenderContext::TextRenderFormat textRenderFormat = QgsRenderContext::TextFormatAlwaysOutlines;

};

/**
Expand Down
49 changes: 49 additions & 0 deletions tests/src/python/test_qgslayoutexporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
QgsRectangle,
QgsLayoutItemPage,
QgsLayoutItemMap,
QgsLayoutItemScaleBar,
QgsLayoutPoint,
QgsLayoutMeasurement,
QgsUnitTypes,
Expand All @@ -40,6 +41,7 @@
QgsCoordinateReferenceSystem,
QgsPrintLayout,
QgsSingleSymbolRenderer,
QgsRenderContext,
QgsReport)
from qgis.PyQt.QtCore import QSize, QSizeF, QDir, QRectF, Qt, QDateTime, QDate, QTime, QTimeZone
from qgis.PyQt.QtGui import QImage, QPainter
Expand Down Expand Up @@ -615,6 +617,53 @@ def checkMetadata(f, expected):
for f in [svg_file_path, svg_file_path_2]:
checkMetadata(f, False)

def testExportToSvgTextRenderFormat(self):
l = QgsLayout(QgsProject.instance())
l.initializeDefaults()

# add a map and scalebar
mapitem = QgsLayoutItemMap(l)
mapitem.attemptSetSceneRect(QRectF(110, 120, 200, 250))
mapitem.zoomToExtent(QgsRectangle(1, 1, 10, 10))
mapitem.setScale(666) # unlikely to appear in the SVG by accident... unless... oh no! RUN!
l.addItem(mapitem)

item1 = QgsLayoutItemScaleBar(l)
item1.attemptSetSceneRect(QRectF(10, 20, 100, 150))
item1.setLinkedMap(mapitem)
item1.setStyle('Numeric')
l.addItem(item1)

exporter = QgsLayoutExporter(l)
# setup settings
settings = QgsLayoutExporter.SvgExportSettings()
settings.dpi = 80
settings.forceVectorOutput = False
settings.exportMetadata = True
settings.textRenderFormat = QgsRenderContext.TextFormatAlwaysText

svg_file_path = os.path.join(self.basetestpath, 'test_exporttosvgtextformattext.svg')
self.assertEqual(exporter.exportToSvg(svg_file_path, settings), QgsLayoutExporter.Success)
self.assertTrue(os.path.exists(svg_file_path))

# expect svg to contain a text object with the scale
with open(svg_file_path, 'r') as f:
lines = ''.join(f.readlines())
self.assertIn('<text', lines)
self.assertIn('>1:666<', lines)

# force use of outlines
os.unlink(svg_file_path)
settings.textRenderFormat = QgsRenderContext.TextFormatAlwaysOutlines
self.assertEqual(exporter.exportToSvg(svg_file_path, settings), QgsLayoutExporter.Success)
self.assertTrue(os.path.exists(svg_file_path))

# expect svg NOT to contain a text object with the scale
with open(svg_file_path, 'r') as f:
lines = ''.join(f.readlines())
self.assertNotIn('<text', lines)
self.assertNotIn('>1:666<', lines)

def testPrint(self):
l = QgsLayout(QgsProject.instance())
l.initializeDefaults()
Expand Down

0 comments on commit 4631a0b

Please sign in to comment.