Skip to content

Commit 0814347

Browse files
committed
[FEATURE] QgsTextRenderer class for rich text rendering
Moves all the drawing code out of labeling into a new class which just handles rendering text. This allows other parts of the code to utilise all the advanced formatting options that labeling supports, eg rendering text with shadows, buffers and backgrounds.
1 parent 074ae42 commit 0814347

File tree

100 files changed

+2488
-897
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+2488
-897
lines changed

python/core/qgspallabeling.sip

Lines changed: 0 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -649,61 +649,6 @@ class QgsLabelCandidate
649649
double cost;
650650
};
651651

652-
/** \ingroup core
653-
* Maintains current state of more grainular and temporal values when creating/painting
654-
* component parts of an individual label (e.g. buffer, background, shadow, etc.).
655-
*/
656-
class QgsLabelComponent
657-
{
658-
%TypeHeaderCode
659-
#include <qgspallabeling.h>
660-
%End
661-
662-
public:
663-
QgsLabelComponent();
664-
665-
// methods
666-
667-
QString text() const;
668-
void setText( const QString& text );
669-
670-
const QgsPoint& origin() const;
671-
void setOrigin( const QgsPoint& point );
672-
673-
bool useOrigin() const;
674-
void setUseOrigin( const bool use );
675-
676-
double rotation() const;
677-
void setRotation( const double rotation );
678-
679-
double rotationOffset() const;
680-
void setRotationOffset( const double rotation );
681-
682-
bool useRotation() const;
683-
void setUseRotation( const bool use );
684-
685-
const QgsPoint& center() const;
686-
void setCenter( const QgsPoint& point );
687-
688-
bool useCenter() const;
689-
void setUseCenter( const bool use );
690-
691-
const QgsPoint& size() const;
692-
void setSize( const QgsPoint& point );
693-
694-
const QgsPoint& offset() const;
695-
void setOffset( const QgsPoint& point );
696-
697-
const QPicture* picture() const;
698-
void setPicture( QPicture* picture );
699-
700-
double pictureBuffer() const;
701-
void setPictureBuffer( const double buffer );
702-
703-
double dpiRatio() const;
704-
void setDpiRatio( const double ratio );
705-
};
706-
707652

708653
/**
709654
* Class that stores computed placement from labeling engine.
@@ -736,14 +681,6 @@ class QgsPalLabeling : QgsLabelingEngineInterface
736681
%End
737682

738683
public:
739-
enum DrawLabelType
740-
{
741-
LabelText,
742-
LabelBuffer,
743-
LabelShape,
744-
LabelSVG,
745-
LabelShadow
746-
};
747684

748685
QgsPalLabeling();
749686
~QgsPalLabeling();
@@ -830,18 +767,6 @@ class QgsPalLabeling : QgsLabelingEngineInterface
830767
//! @note not available in python bindings
831768
// void drawLabelCandidateRect( pal::LabelPosition* lp, QPainter* painter, const QgsMapToPixel* xform );
832769

833-
static void drawLabelBuffer( QgsRenderContext& context,
834-
const QgsLabelComponent &component,
835-
const QgsTextFormat& format );
836-
837-
static void drawLabelBackground( QgsRenderContext& context,
838-
QgsLabelComponent component,
839-
const QgsTextFormat& format );
840-
841-
static void drawLabelShadow( QgsRenderContext &context,
842-
const QgsLabelComponent &component,
843-
const QgsTextFormat& format );
844-
845770
//! load/save engine settings to project file
846771
void loadEngineSettings();
847772
void saveEngineSettings();

python/core/qgstextrenderer.sip

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,15 @@ class QgsTextFormat
856856
*/
857857
QFont font() const;
858858

859+
/** Returns a font with the size scaled to match the format's size settings (including
860+
* units and map unit scale) for a specified render context.
861+
* @param context destination render context
862+
* @returns font with scaled size
863+
* @see font()
864+
* @see size()
865+
*/
866+
QFont scaledFont( const QgsRenderContext& context ) const;
867+
859868
/** Sets the font used for rendering text. Note that the size of the font
860869
* is not used, and setSize() should be called instead to explicitly set the size
861870
* of rendered text.
@@ -1021,6 +1030,21 @@ class QgsTextRenderer
10211030

10221031
public:
10231032

1033+
enum TextPart
1034+
{
1035+
Text,
1036+
Buffer,
1037+
Background,
1038+
Shadow
1039+
};
1040+
1041+
enum HAlignment
1042+
{
1043+
AlignLeft,
1044+
AlignCenter,
1045+
AlignRight,
1046+
};
1047+
10241048
/** Calculates pixel size (considering output size should be in pixel or map units, scale factors and optionally oversampling)
10251049
* @param size size to convert
10261050
* @param c rendercontext
@@ -1041,4 +1065,63 @@ class QgsTextRenderer
10411065
*/
10421066
static double scaleToPixelContext( double size, const QgsRenderContext& c, QgsUnitTypes::RenderUnit unit, bool rasterfactor = false, const QgsMapUnitScale& mapUnitScale = QgsMapUnitScale() );
10431067

1068+
/** Draws text within a rectangle using the specified settings.
1069+
* @param rect destination rectangle for text
1070+
* @param rotation text rotation
1071+
* @param textLines list of lines of text to draw
1072+
* @param context render context
1073+
* @param format text format
1074+
* @param drawAsOutlines set to false to render text as text. This allows outputs to
1075+
* formats like SVG to maintain text as text objects, but at the cost of degraded
1076+
* rendering and may result in side effects like misaligned text buffers.
1077+
*/
1078+
static void drawText( const QRectF& rect, double rotation, HAlignment alignment, const QStringList& textLines,
1079+
QgsRenderContext& context, const QgsTextFormat& format,
1080+
bool drawAsOutlines = true );
1081+
1082+
/** Draws text at a point origin using the specified settings.
1083+
* @param point origin of text
1084+
* @param rotation text rotation
1085+
* @param textLines list of lines of text to draw
1086+
* @param context render context
1087+
* @param format text format
1088+
* @param drawAsOutlines set to false to render text as text. This allows outputs to
1089+
* formats like SVG to maintain text as text objects, but at the cost of degraded
1090+
* rendering and may result in side effects like misaligned text buffers.
1091+
*/
1092+
static void drawText( const QPointF& point, double rotation, HAlignment alignment, const QStringList& textLines,
1093+
QgsRenderContext& context, const QgsTextFormat& format,
1094+
bool drawAsOutlines = true );
1095+
1096+
/** Draws a single component of rendered text using the specified settings.
1097+
* @param rect destination rectangle for text
1098+
* @param rotation text rotation
1099+
* @param textLines list of lines of text to draw
1100+
* @param context render context
1101+
* @param format text format
1102+
* @param part component of text to draw
1103+
* @param drawAsOutlines set to false to render text as text. This allows outputs to
1104+
* formats like SVG to maintain text as text objects, but at the cost of degraded
1105+
* rendering and may result in side effects like misaligned text buffers.
1106+
*/
1107+
static void drawPart( const QRectF& rect, double rotation, HAlignment alignment, const QStringList& textLines,
1108+
QgsRenderContext& context, const QgsTextFormat& format,
1109+
TextPart part, bool drawAsOutlines = true );
1110+
1111+
/** Draws a single component of rendered text using the specified settings.
1112+
* @param origin origin for start of text. Y coordinate will be used as baseline.
1113+
* @param rotation text rotation
1114+
* @param textLines list of lines of text to draw
1115+
* @param context render context
1116+
* @param format text format
1117+
* @param part component of text to draw. Note that Shadow parts cannot be drawn
1118+
* individually and instead are drawn with their associated part (eg drawn together
1119+
* with the text or background parts)
1120+
* @param drawAsOutlines set to false to render text as text. This allows outputs to
1121+
* formats like SVG to maintain text as text objects, but at the cost of degraded
1122+
* rendering and may result in side effects like misaligned text buffers.
1123+
*/
1124+
static void drawPart( const QPointF& origin, double rotation, HAlignment alignment, const QStringList& textLines,
1125+
QgsRenderContext& context, const QgsTextFormat& format,
1126+
TextPart part, bool drawAsOutlines = true );
10441127
};

src/app/qgslabelinggui.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,7 @@ void QgsLabelingGui::init()
771771
mZIndexSpinBox->setValue( lyr.zIndex );
772772

773773
mRefFont = format.font();
774-
mFontSizeSpinBox->setValue( format.font().pointSizeF() );
774+
mFontSizeSpinBox->setValue( format.size() );
775775
btnTextColor->setColor( format.color() );
776776
mFontTranspSpinBox->setValue( 100 - 100 * format.opacity() );
777777
comboBlendMode->setBlendMode( format.blendMode() );

src/app/qgslabelpreview.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ void QgsLabelPreview::paintEvent( QPaintEvent *e )
7373
if ( mFormat.buffer().size() != 0 )
7474
{
7575
mContext.setPainter( &p );
76-
QgsLabelComponent component;
77-
component.setText( text() );
78-
QgsPalLabeling::drawLabelBuffer( mContext, component, mFormat );
76+
QgsTextRenderer::Component component;
77+
component.text = text();
78+
QgsTextRenderer::drawBuffer( mContext, component, mFormat );
7979
}
8080

8181
QPainterPath path;

0 commit comments

Comments
 (0)