QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
qgspalgeometry.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgspalgeometry.h
3  ---------------------
4  begin : May 2009
5  copyright : (C) 2009 by Marco Hugentobler
6  email : marco dot hugentobler at sourcepole dot ch
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 #ifndef QGSPALGEOMETRY_H
16 #define QGSPALGEOMETRY_H
17 
18 #include "qgsgeometry.h"
19 #include "qgspallabeling.h"
20 #include <pal/feature.h>
21 
22 #include "qgslabelingenginev2.h"
23 
31 {
32  public:
34  QgsTextLabelFeature( QgsFeatureId id, GEOSGeometry* geometry, const QSizeF& size )
35  : QgsLabelFeature( id, geometry, size )
36  , mFontMetrics( nullptr )
37  {
38  mDefinedFont = QFont();
39  }
40 
43  {
44  delete mFontMetrics;
45  }
46 
52  QString text( int partId ) const
53  {
54  if ( partId == -1 )
55  return mLabelText;
56  else
57  return mClusters.at( partId );
58  }
59 
61  void calculateInfo( bool curvedLabeling, QFontMetricsF* fm, const QgsMapToPixel* xform, double fontScale, double maxinangle, double maxoutangle )
62  {
63  if ( mInfo )
64  return;
65 
66  mFontMetrics = new QFontMetricsF( *fm ); // duplicate metrics for when drawing label
67 
68  qreal letterSpacing = mDefinedFont.letterSpacing();
69  qreal wordSpacing = mDefinedFont.wordSpacing();
70 
71  // max angle between curved label characters (20.0/-20.0 was default in QGIS <= 1.8)
72  if ( maxinangle < 20.0 )
73  maxinangle = 20.0;
74  if ( 60.0 < maxinangle )
75  maxinangle = 60.0;
76  if ( maxoutangle > -20.0 )
77  maxoutangle = -20.0;
78  if ( -95.0 > maxoutangle )
79  maxoutangle = -95.0;
80 
81  // create label info!
82  double mapScale = xform->mapUnitsPerPixel();
83  double labelHeight = mapScale * fm->height() / fontScale;
84 
85  // mLetterSpacing/mWordSpacing = 0.0 is default for non-curved labels
86  // (non-curved spacings handled by Qt in QgsPalLayerSettings/QgsPalLabeling)
87  qreal charWidth;
88  qreal wordSpaceFix;
89 
90  //split string by valid grapheme boundaries - required for certain scripts (see #6883)
92 
93  mInfo = new pal::LabelInfo( mClusters.count(), labelHeight, maxinangle, maxoutangle );
94  for ( int i = 0; i < mClusters.count(); i++ )
95  {
96  // reconstruct how Qt creates word spacing, then adjust per individual stored character
97  // this will allow PAL to create each candidate width = character width + correct spacing
98  charWidth = fm->width( mClusters[i] );
99  if ( curvedLabeling )
100  {
101  wordSpaceFix = qreal( 0.0 );
102  if ( mClusters[i] == QLatin1String( " " ) )
103  {
104  // word spacing only gets added once at end of consecutive run of spaces, see QTextEngine::shapeText()
105  int nxt = i + 1;
106  wordSpaceFix = ( nxt < mClusters.count() && mClusters[nxt] != QLatin1String( " " ) ) ? wordSpacing : qreal( 0.0 );
107  }
108  // this workaround only works for clusters with a single character. Not sure how it should be handled
109  // with multi-character clusters.
110  if ( mClusters[i].length() == 1 &&
111  !qgsDoubleNear( fm->width( QString( mClusters[i].at( 0 ) ) ), fm->width( mClusters[i].at( 0 ) ) + letterSpacing ) )
112  {
113  // word spacing applied when it shouldn't be
114  wordSpaceFix -= wordSpacing;
115  }
116 
117  charWidth = fm->width( QString( mClusters[i] ) ) + wordSpaceFix;
118  }
119 
120  double labelWidth = mapScale * charWidth / fontScale;
121  mInfo->char_info[i].width = labelWidth;
122  }
123  }
124 
129 
131  void setDefinedFont( const QFont& f ) { mDefinedFont = f; }
134 
137 
138  protected:
147 
148 };
149 
150 #endif //QGSPALGEOMETRY_H
QFontMetricsF * mFontMetrics
Metrics of the font for rendering.
QgsTextLabelFeature(QgsFeatureId id, GEOSGeometry *geometry, const QSizeF &size)
Construct text label feature.
QString mLabelText
text of the label
QStringList mClusters
List of graphemes (used for curved labels)
~QgsTextLabelFeature()
Clean up.
const T & at(int i) const
const QMap< QgsPalLayerSettings::DataDefinedProperties, QVariant > & dataDefinedValues() const
Get data-defined values.
pal::LabelInfo * mInfo
extra information for curved labels (may be null)
Class that adds extra information to QgsLabelFeature for text labels.
GEOSGeometry * geometry() const
Get access to the associated geometry.
void setDefinedFont(const QFont &f)
Set font to be used for rendering.
qreal width(const QString &text) const
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
Compare two doubles (but allow some difference)
Definition: qgis.h:353
CharacterInfo * char_info
Definition: feature.h:75
Perform transforms between map coordinates and device coordinates.
Definition: qgsmaptopixel.h:34
int count(const T &value) const
QMap< QgsPalLayerSettings::DataDefinedProperties, QVariant > mDataDefinedValues
Stores attribute values for data defined properties.
qreal letterSpacing() const
void setDataDefinedValues(const QMap< QgsPalLayerSettings::DataDefinedProperties, QVariant > &values)
Set data-defined values.
Optional additional info about label (for curved labels)
Definition: feature.h:52
void calculateInfo(bool curvedLabeling, QFontMetricsF *fm, const QgsMapToPixel *xform, double fontScale, double maxinangle, double maxoutangle)
calculate data for info(). setDefinedFont() must have been called already.
QString text(int partId) const
Returns the text component corresponding to a specified label part.
double mapUnitsPerPixel() const
Return current map units per pixel.
QSizeF size() const
Size of the label (in map units)
QFont definedFont()
Font to be used for rendering.
The QgsLabelFeature class describes a feature that should be used within the labeling engine...
static QStringList splitToGraphemes(const QString &text)
Splits a text string to a list of graphemes, which are the smallest allowable character divisions in ...
qint64 QgsFeatureId
Definition: qgsfeature.h:31
QFont mDefinedFont
Font for rendering.
QFontMetricsF * labelFontMetrics()
Metrics of the font for rendering.
qreal height() const
qreal wordSpacing() const