1#ifndef OSMSCOUT_MAP_LABELLAYOUTER_H
2#define OSMSCOUT_MAP_LABELLAYOUTER_H
39#ifdef OSMSCOUT_DEBUG_LABEL_LAYOUTER
99 double labelWidth)
const;
102 template<
class NativeGlyph>
117 template<
class NativeGlyph,
class NativeLabel>
131 template<
typename... Args>
145 template<
class NativeGlyph,
class NativeLabel>
154 std::shared_ptr<Label<NativeGlyph, NativeLabel>>
160 size_t priority{std::numeric_limits<size_t>::max()};
165 template<
class NativeGlyph>
169#ifdef OSMSCOUT_DEBUG_LABEL_LAYOUTER
180 template <
class NativeGlyph,
class NativeLabel>
191 template <
class NativeGlyph>
204 return a.
glyphs[0].trPosition.GetX() < b.
glyphs[0].trPosition.GetX();
228 template <
class NativeGlyph,
class NativeLabel,
class TextLayouter>
240 textLayouter(textLayouter)
251 layoutOverlap = overlap;
252 layoutViewport.width = visibleViewport.width + (overlap * 2);
253 layoutViewport.height = visibleViewport.height + (overlap * 2);
254 layoutViewport.x = visibleViewport.x - overlap;
255 layoutViewport.y = visibleViewport.y - overlap;
260 contourLabelInstances.clear();
261 labelInstances.clear();
267 return labelData.
alpha < 0.8;
296 const MapParameter& parameter):
298 iconPadding(projection.ConvertWidthToPixel(parameter.GetIconPadding())),
315 void Swap(std::vector<LabelInstanceType> &labelInstances,
316 std::vector<ContourLabelType> &contourLabelInstances)
363 std::vector<LabelInstanceType> &labelInstances)
366 size_t elementCount = currentLabel.
elements.size();
367 std::vector<ScreenRectMask> masks(elementCount);
368 std::vector<ScreenMask*> canvases(elementCount,
nullptr);
371 std::vector<typename LabelInstance<NativeGlyph, NativeLabel>::Element> visibleElements;
373 for (
size_t eli=0; eli < elementCount; eli++) {
375 ScreenRectMask &mask=masks[eli];
406 <<
"label prio " << currentLabel.
priority <<
": "
410 rectangle.
width = element.
label->width + 2*padding;
411 rectangle.
height = element.
label->height + 2*padding;
417 bool collision = canvas->HasCollision(mask);
420 visibleElements.push_back(element);
421 canvases[eli]=canvas;
425 std::cout <<
" -> " << (collision ?
"skipped" :
"added") << std::endl;
432 if (!visibleElements.empty()) {
434 labelInstances.push_back(instanceCopy);
438 for (
size_t eli=0; eli < elementCount; eli++) {
439 if (canvases[eli] !=
nullptr) {
440 canvases[eli]->AddMask(masks[eli]);
447 std::vector<ContourLabelType> &contourLabelInstances)
449 int glyphCnt=currentContourLabel.
glyphs.size();
452 std::cout <<
"Test contour label prio " << currentContourLabel.
priority <<
": " << currentContourLabel.text;
455 std::vector<ScreenRectMask> masks(glyphCnt);
457 bool collision=
false;
458 for (
int gi=0; gi<glyphCnt; gi++) {
459 auto glyph=currentContourLabel.
glyphs[gi];
477 for (
int gi=0; gi<glyphCnt; gi++) {
481 contourLabelInstances.push_back(currentContourLabel);
485 std::cout <<
" -> " << (collision ?
"skipped" :
"added") << std::endl;
490 std::vector<ContourLabelType> &contourLabelInstances)
506 if (contourLabelIter->priority<=labelIter->priority) {
532 const MapParameter& parameter)
535 LayoutJob job(layoutViewport, projection, parameter);
536 job.Swap(labelInstances, contourLabelInstances);
538 job.ProcessLabels(labelInstances, contourLabelInstances);
541 template<
class Pa
inter>
543 const MapParameter& ,
578 template<
class Pa
inter>
580 const MapParameter& parameter,
584 std::vector<const typename LabelInstanceType::Element*> textElements;
585 std::vector<const typename LabelInstanceType::Element*> overlayElements;
596 if (!visibleViewport.Intersects(elementRectangle)){
601 p->DrawSymbol(projection,
618 overlayElements.push_back(&el);
620 textElements.push_back(&el);
628 p->DrawLabel(projection, parameter,
630 el->labelData, el->label->label);
634 p->DrawLabel(projection, parameter,
636 el->labelData, el->label->label);
640 p->DrawGlyphs(projection,
649 const MapParameter& parameter,
650 const Vertex2D& point,
662 element.
x = point.GetX() - data.
iconWidth / 2;
676 element.
label = textLayouter->Layout(projection, parameter,
681 element.
x = point.GetX() - element.
label->width / 2;
683 element.
y = point.GetY() - element.
label->height / 2;
684 offset = point.GetY() + element.
label->height / 2;
688 offset += element.
label->height;
692 instance.
elements.push_back(element);
696 const MapParameter& parameter,
697 const ObjectFileRef& ref,
698 const Vertex2D& point,
700 double objectWidth = 10.0)
715 labelInstances.push_back(instance);
719 const MapParameter& parameter,
720 const ObjectFileRef& ref,
721 const Vertex2D& point,
722 const std::vector<LabelData>& data,
723 double objectWidth = 10.0)
730 for (
const auto& d : data) {
740 labelInstances.push_back(instance);
744 const MapParameter& parameter,
745 const ObjectFileRef& ref,
750 LabelPtr label=textLayouter->Layout(projection,
760 double textBaselineOffset = label->
height * 0.25;
762 std::vector<Glyph<NativeGlyph>> glyphs = label->
ToGlyphs();
764 ContourLabelPositioner positioner;
765 ContourLabelPositioner::Position position=positioner.calculatePositions(projection,
771 double offset=position.offset;
772 size_t currentCount=1;
773 while (currentCount<=position.labelCount){
774 double nextOffset=offset+label->
width+position.labelSpace;
792 cLabel.text = labelData.
text;
793 cLabel.offset = offset;
801 bool upwards=initialAngle>90 && initialAngle<270;
805 double glyphOffset = upwards ?
806 offset - glyph.position.GetX() + label->
width:
807 offset + glyph.position.GetX();
808 osmscout::Vertex2D point=labelPath.
PointAtLength(glyphOffset);
811 double w = textBoundingBox.
width;
812 double h = textBoundingBox.
height;
813 osmscout::Vertex2D tl(textBoundingBox.
x, textBoundingBox.
y);
816 double angle=labelPath.
AngleAtLength(upwards ? glyphOffset - w/2 : glyphOffset + w/2)*-1;
820 double diagonal=w+h+std::abs(textBaselineOffset);
824 point.GetX()-diagonal,
825 point.GetY()-diagonal,
835 double sinA=std::sin(angle);
836 double cosA=std::cos(angle);
839 glyphCopy.
position=osmscout::Vertex2D(point.GetX() - textBaselineOffset * sinA,
840 point.GetY() + textBaselineOffset * cosA);
841 glyphCopy.
angle=angle;
844 std::array<double, 4> x{tl.GetX(), tl.GetX() + w, tl.GetX() + w, tl.GetX()};
845 std::array<double, 4> y{tl.GetY(), tl.GetY(), tl.GetY() + h, tl.GetY() + h};
848 for (
int i=0; i<4; i++){
851 x[i] = ox * cosA - oy * sinA;
852 y[i] = ox * sinA + oy * cosA;
860 for (
int i=1; i<4; i++){
861 minX = std::min(minX, x[i]);
862 maxX = std::max(maxX, x[i]);
863 minY = std::min(minY, y[i]);
864 maxY = std::max(maxY, y[i]);
869 glyphCopy.
trWidth = maxX - minX;
872 cLabel.
glyphs.push_back(glyphCopy);
874 if (!cLabel.
glyphs.empty()) {
875 contourLabelInstances.push_back(cLabel);
882 const std::vector<LabelInstanceType>&
Labels()
const
884 return labelInstances;
889 return contourLabelInstances;
893 TextLayouter *textLayouter;
894 std::vector<ContourLabelType> contourLabelInstances;
895 std::vector<LabelInstanceType> labelInstances;
897 ScreenVectorRectangle layoutViewport{0,0,0,0};
898 uint32_t layoutOverlap=0;
#define OSMSCOUT_MAP_API
Definition MapImportExport.h:45
Position calculatePositions(const Projection &projection, const MapParameter ¶meter, const PathLabelData &labelData, double pathLength, double labelWidth) const
Definition LabelLayouter.h:167
std::vector< Glyph< NativeGlyph > > glyphs
Vector of glyphs of the label text (see text).
Definition LabelLayouter.h:176
osmscout::PathTextStyleRef style
Style for drawing the text of the label.
Definition LabelLayouter.h:177
size_t priority
Priority of the label.
Definition LabelLayouter.h:175
ObjectFileRef ref
Definition LabelLayouter.h:174
Definition LabelLayouter.h:103
double trHeight
Definition LabelLayouter.h:111
Vertex2D trPosition
Definition LabelLayouter.h:109
CairoNativeGlyph glyph
Definition LabelLayouter.h:105
double trWidth
Definition LabelLayouter.h:110
double angle
Definition LabelLayouter.h:107
Vertex2D position
Definition LabelLayouter.h:106
Definition LabelLayouter.h:57
IconStyleRef iconStyle
Icon or symbol style.
Definition LabelLayouter.h:76
size_t priority
Priority of the entry.
Definition LabelLayouter.h:67
double alpha
Alpha value of the label; 0.0 = fully transparent, 1.0 = solid.
Definition LabelLayouter.h:70
std::string text
The label text (type==Text|PathText).
Definition LabelLayouter.h:74
Type
Definition LabelLayouter.h:60
@ Text
Definition LabelLayouter.h:63
@ Symbol
Definition LabelLayouter.h:62
@ Icon
Definition LabelLayouter.h:61
double iconWidth
Definition LabelLayouter.h:77
Type type
Definition LabelLayouter.h:66
LabelStyleRef style
Style for drawing.
Definition LabelLayouter.h:73
double fontSize
Font size to be used.
Definition LabelLayouter.h:71
double iconHeight
Definition LabelLayouter.h:78
size_t position
Relative position of the label.
Definition LabelLayouter.h:68
Definition LabelLayouter.h:119
std::vector< Glyph< NativeGlyph > > ToGlyphs() const
Label(Args &&... args)
Definition LabelLayouter.h:132
double fontSize
Definition LabelLayouter.h:126
double width
Definition LabelLayouter.h:123
std::string text
Definition LabelLayouter.h:127
CairoNativeLabel label
Definition LabelLayouter.h:121
double height
Definition LabelLayouter.h:124
Definition LabelLayouter.h:147
ObjectFileRef ref
Definition LabelLayouter.h:159
std::vector< Element > elements
Definition LabelLayouter.h:162
size_t priority
Definition LabelLayouter.h:160
const std::vector< ContourLabelType > & ContourLabels() const
Definition LabelLayouter.h:887
void Layout(const Projection &projection, const MapParameter ¶meter)
Definition LabelLayouter.h:531
ContourLabel< NativeGlyph > ContourLabelType
Definition LabelLayouter.h:233
void Reset()
Definition LabelLayouter.h:258
void RegisterLabel(const Projection &projection, const MapParameter ¶meter, const ObjectFileRef &ref, const Vertex2D &point, const LabelData &data, double objectWidth=10.0)
Definition LabelLayouter.h:695
void SetViewport(const ScreenVectorRectangle &v)
Definition LabelLayouter.h:243
void RegisterContourLabel(const Projection &projection, const MapParameter ¶meter, const ObjectFileRef &ref, const PathLabelData &labelData, const LabelPath &labelPath)
Definition LabelLayouter.h:743
void ProcessLabel(const Projection &projection, const MapParameter ¶meter, const Vertex2D &point, LabelInstanceType &instance, double &offset, const LabelData &data, double objectWidth)
Definition LabelLayouter.h:648
void RegisterLabel(const Projection &projection, const MapParameter ¶meter, const ObjectFileRef &ref, const Vertex2D &point, const std::vector< LabelData > &data, double objectWidth=10.0)
Definition LabelLayouter.h:718
void DrawLabels(const Projection &projection, const MapParameter ¶meter, Painter *p)
Definition LabelLayouter.h:579
void SetLayoutOverlap(uint32_t overlap)
Definition LabelLayouter.h:249
static bool IsOverlay(const LabelData &labelData)
Definition LabelLayouter.h:265
std::shared_ptr< LabelType > LabelPtr
Definition LabelLayouter.h:235
Label< NativeGlyph, NativeLabel > LabelType
Definition LabelLayouter.h:234
const std::vector< LabelInstanceType > & Labels() const
Definition LabelLayouter.h:882
LabelLayouter(TextLayouter *textLayouter)
Definition LabelLayouter.h:239
void DrawTextLabels(const Projection &, const MapParameter &, Painter *) const
Definition LabelLayouter.h:542
LabelInstance< NativeGlyph, NativeLabel > LabelInstanceType
Definition LabelLayouter.h:236
Definition LabelPath.h:44
double AngleAtLength(double offset) const
bool TestAngleVariance(double startOffset, double endOffset, double maximumAngle) const
Vertex2D PointAtLength(double offset) const
double GetLength() const
Definition LabelPath.h:57
double AngleAtLengthDeg(double offset) const
Definition LabelLayouter.h:46
double contourLabelSpace
Definition LabelLayouter.h:53
size_t priority
Priority of the entry.
Definition LabelLayouter.h:48
std::string text
The label text (type==Text|PathText).
Definition LabelLayouter.h:49
double contourLabelOffset
Definition LabelLayouter.h:52
PathTextStyleRef style
Definition LabelLayouter.h:51
double height
Definition LabelLayouter.h:50
Definition Projection.h:46
std::shared_ptr< PathTextStyle > PathTextStyleRef
Definition Styles.h:890
static bool ContourLabelSorter(const ContourLabel< NativeGlyph > &a, const ContourLabel< NativeGlyph > &b)
Definition LabelLayouter.h:192
constexpr bool debugLabelLayouter
Definition LabelLayouter.h:42
std::shared_ptr< LabelStyle > LabelStyleRef
Definition Styles.h:499
std::shared_ptr< IconStyle > IconStyleRef
Definition Styles.h:987
static bool LabelInstanceSorter(const LabelInstance< NativeGlyph, NativeLabel > &a, const LabelInstance< NativeGlyph, NativeLabel > &b)
Definition LabelLayouter.h:181
Definition PositionAgent.h:65
double labelSpace
Space between individual labels.
Definition LabelLayouter.h:92
double offset
Offset of the first label.
Definition LabelLayouter.h:91
size_t labelCount
Number of labels rendered.
Definition LabelLayouter.h:90
Definition LabelLayouter.h:150
std::shared_ptr< Label< NativeGlyph, NativeLabel > > label
Definition LabelLayouter.h:155
double y
Coordinate of the left, top edge of the text / icon / symbol.
Definition LabelLayouter.h:153
double x
Coordinate of the left, top edge of the text / icon / symbol.
Definition LabelLayouter.h:152
LabelData labelData
Definition LabelLayouter.h:151
LayoutJob(const LayoutJob &)=delete
LayoutJob & operator=(LayoutJob &&)=delete
void ProcessLabelInstance(const LabelInstanceType ¤tLabel, std::vector< LabelInstanceType > &labelInstances)
Definition LabelLayouter.h:362
double GetLabelPadding(const LabelData &labelData) const
Definition LabelLayouter.h:333
ScreenVectorRectangle layoutViewport
Definition LabelLayouter.h:279
double shieldLabelPadding
Definition LabelLayouter.h:283
ScreenMask overlayCanvas
Definition LabelLayouter.h:292
double iconPadding
Definition LabelLayouter.h:281
std::vector< LabelInstanceType > allSortedLabels
Definition LabelLayouter.h:288
ScreenMask * GetCanvas(LabelData data)
Definition LabelLayouter.h:350
double overlayLabelPadding
Definition LabelLayouter.h:285
double labelPadding
Definition LabelLayouter.h:282
std::vector< ContourLabelType > allSortedContourLabels
Definition LabelLayouter.h:287
LayoutJob(const ScreenVectorRectangle &layoutViewport, const Projection &projection, const MapParameter ¶meter)
Definition LabelLayouter.h:294
ScreenMask iconCanvas
Definition LabelLayouter.h:290
void Swap(std::vector< LabelInstanceType > &labelInstances, std::vector< ContourLabelType > &contourLabelInstances)
Definition LabelLayouter.h:315
ScreenMask labelCanvas
Definition LabelLayouter.h:291
void SortLabels()
Definition LabelLayouter.h:322
double contourLabelPadding
Definition LabelLayouter.h:284
void ProcessLabelContourLabel(const ContourLabelType ¤tContourLabel, std::vector< ContourLabelType > &contourLabelInstances)
Definition LabelLayouter.h:446
LayoutJob & operator=(const LayoutJob &)=delete
LayoutJob(LayoutJob &&)=delete
void ProcessLabels(std::vector< LabelInstanceType > &labelInstances, std::vector< ContourLabelType > &contourLabelInstances)
Definition LabelLayouter.h:489
Definition LabelLayouterHelper.h:38
int height
Definition LabelLayouterHelper.h:42
int width
Definition LabelLayouterHelper.h:41
Definition LabelLayouterHelper.h:73
double x
Definition LabelLayouterHelper.h:74
double y
Definition LabelLayouterHelper.h:75
double width
Definition LabelLayouterHelper.h:76
ScreenVectorRectangle & Set(double nx, double ny, double nw, double nh)
Definition LabelLayouterHelper.h:90
double height
Definition LabelLayouterHelper.h:77