-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathLabelDrawing.cpp
More file actions
593 lines (499 loc) · 17 KB
/
LabelDrawing.cpp
File metadata and controls
593 lines (499 loc) · 17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
/**************************************************************************************
* File name: LabelDrawing.cpp
*
* Project: MapWindow Open Source (MapWinGis ActiveX control)
* Description: Implementation of CLabelDrawer
*
**************************************************************************************
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at http://www.mozilla.org/mpl/
* See the License for the specific language governing rights and limitations
* under the License.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
**************************************************************************************
* Contributor(s):
* (Open source contributors should list themselves and their modifications here). */
#include "stdafx.h"
#include "LabelDrawing.h"
#include <map>
#include <set>
#include "Labels.h"
#include "LabelCategory.h"
#include "Shapefile.h"
#include "macros.h"
#include "TableHelper.h"
#include "GdiPlusLabelDrawer.h"
#include "GdiLabelDrawer.h"
#include "LabelDrawingHelper.h"
#include "LabelsHelper.h"
#include "AngleHelper.h"
using namespace Gdiplus;
// *********************************************************************
// InitSettings()
// *********************************************************************
void CLabelDrawer::InitSettings(LabelSettings& settings, ILabels* labels, IShapefile* sf)
{
labels->get_NumCategories(&settings.numCategories);
labels->get_RemoveDuplicates(&settings.removeDuplicates);
labels->get_CollisionBuffer(&settings.buffer);
labels->get_AvoidCollisions(&settings.avoidCollisions);
settings.scaleFactor = GetScaleFactor(labels);
settings.autoOffset = GetAutoOffset(labels, sf);
settings.numLabels = LabelsHelper::GetCount(labels);
CLabels* lbs = ((CLabels*)labels);
settings.hasRotation = lbs->HasRotation();
settings.useVariableFontSize = lbs->RecalculateFontSize();
}
// *********************************************************************
// DrawLabels()
// *********************************************************************
void CLabelDrawer::DrawLabels(ILabels* labels)
{
if (!CheckVisibility(labels)) {
return;
}
// ------------------------------------------------
// accessing data
// ------------------------------------------------
CLabels* lbs = static_cast<CLabels*>(labels);
vector<vector<CLabelInfo*>*>* labelData = lbs->get_LabelData();
IShapefile* sf = lbs->get_ParentShapefile();
std::vector<ShapeRecord*>* shapeData = NULL;
if (sf) {
shapeData = ((CShapefile*)sf)->get_ShapeVector();
}
// ---------------------------------
// settings, filter and sorting
// ---------------------------------
LabelSettings settings;
InitSettings(settings, labels, sf);
vector<bool> visibilityMask(settings.numLabels, false);
GetVisibilityMask(labels, sf, shapeData, visibilityMask);
// sort them if sort field is specified
vector<long>* indices = NULL;
if (sf) {
((CShapefile*)sf)->GetSorting(&indices);
}
if (indices && indices->size() != settings.numLabels) {
indices = NULL;
}
// ---------------------------------
// preparing to draw
// ---------------------------------
GdiLabelDrawer gdi;
GdiPlusLabelDrawer gdiPlus;
CRect rect(0, 0, 0, 0);
std::set<CStringW> uniqueValues;
bool useGdiPlus = GetUseGdiPlus(labels);
if (useGdiPlus) {
gdiPlus.InitGraphics(_graphics, labels);
}
else {
gdi.InitDc(_graphics);
}
// ---------------------------------------------------------------
// drawing categories - we'll start from the categories
// with the higher priority, therefore reverse order
// ---------------------------------------------------------------
for (int categoryIndex = settings.numCategories - 1; categoryIndex >= -1; categoryIndex--)
{
CLabelOptions* options = GetCategoryOptions(labels, categoryIndex);
if (!options || !options->visible) continue;
// we create separate pens/brushes for each drawing method
if (useGdiPlus) {
gdiPlus.InitFromCategory(options, settings.hasRotation);
}
else {
gdi.InitFromCategory(options);
}
if (!settings.useVariableFontSize)
{
if (useGdiPlus) {
gdiPlus.SelectFont(options, options->fontSize, settings.scaleFactor);
}
else {
gdi.SelectFont(options, options->fontSize, settings.scaleFactor);
}
}
// ---------------------------------------------------------------
// drawing labels within category
// ---------------------------------------------------------------
for (long k = 0; k < settings.numLabels; k++)
{
long i = indices ? (*indices)[k] : k;
if (!visibilityMask[i]) {
continue;
}
vector<CLabelInfo*>* parts = (*labelData)[i];
for (int j = 0; j < (int)parts->size(); j++)
{
CLabelInfo* lbl = (*parts)[j];
if (lbl->x < _extents->left) continue;
if (lbl->x > _extents->right) continue;
if (lbl->y < _extents->bottom) continue;
if (lbl->y > _extents->top) continue;
if (lbl->text.GetLength() <= 0) continue;
if ((lbl->category == categoryIndex) || (categoryIndex == -1 && (lbl->category < 0 || lbl->category >= settings.numCategories))) {}
else continue; /* Wrong category */
// blocking the labels with the text already displayed
if (settings.removeDuplicates)
{
if (uniqueValues.find(lbl->text) != uniqueValues.end())
continue;
else
uniqueValues.insert(lbl->text);
}
// choosing appropriate font
if (settings.useVariableFontSize)
{
if (useGdiPlus) {
gdiPlus.SelectFont(options, lbl, settings.scaleFactor);
}
else {
gdi.SelectFont(options, lbl, settings.scaleFactor);
}
}
// measuring label
if (useGdiPlus) {
gdiPlus.MeasureString(lbl, rect);
}
else {
gdi.MeasureString(lbl, rect);
}
// rotation angle
double angle, angleRad;
LabelDrawingHelper::CalcRotation(lbl, _mapRotation, angle);
angleRad = AngleHelper::ToRad(angle);
// calculating screen rectangle
double piX, piY;
// Fix for MWGIS-79:
int shapeSize = 0;
if (sf)
shapeSize = (*shapeData)[i]->size;
CalcScreenRectangle(options, lbl, settings.autoOffset, shapeSize, rect, piX, piY);
// do we have overlaps?
if (!TryAvoidCollisions(lbl, settings.avoidCollisions, rect, piX, piY, settings.buffer, angleRad)) {
continue;
}
// actual drawing
if (useGdiPlus) {
gdiPlus.DrawLabel(options, rect, piX, piY, angle);
}
else {
gdi.DrawLabel(options, lbl, rect, angleRad, piX, piY);
}
}
} // label
if (useGdiPlus) {
gdiPlus.ReleaseForCategory(settings.useVariableFontSize);
}
else {
gdi.ReleaseForCategory(settings.useVariableFontSize);
}
} // category
// restoring rendering options
if (useGdiPlus) {
gdiPlus.RestoreGraphics();
}
else {
gdi.ReleaseDc();
}
}
// *********************************************************************
// GetCategoryOptions()
// *********************************************************************
CLabelOptions* CLabelDrawer::GetCategoryOptions(ILabels* labels, int categoryIndex)
{
if (categoryIndex != -1)
{
CComPtr<ILabelCategory> category = NULL;
labels->get_Category(categoryIndex, &category);
if (!category) {
return NULL;
}
CLabelCategory* ct = (CLabelCategory*)&(*category);
return reinterpret_cast<CLabelOptions*>(ct->get_LabelOptions());
}
else
{
// standard settings
CLabels* lbs = static_cast<CLabels*>(labels);
return lbs->get_LabelOptions();
}
}
// *********************************************************************
// CalcScreenRectangle()
// *********************************************************************
void CLabelDrawer::CalcScreenRectangle(CLabelOptions* options, CLabelInfo* lbl, bool autoOffset, int shapeSize, CRect& rect, double& piX, double& piY)
{
int offset = autoOffset ? shapeSize / 2 + 2 : 0;
// laCenter alignment isn't allowed for auto mode
tkLabelAlignment align = (autoOffset && options->alignment == laCenter) ? laCenterRight : options->alignment;
LabelDrawingHelper::AlignRectangle(rect, options->alignment);
double lblX = lbl->x + lbl->offsetX, lblY = lbl->y + lbl->offsetY;
if (_spatiallyReferenced)
{
this->ProjectionToPixel(lblX, lblY, piX, piY);
}
else
{
// no calculations for screen referenced labels
piX = lblX;
piY = lblY;
}
// MWGIS-229; erroneous code removed from here, handling rounded rectangles.
// Rounded and Pointed rectangles are now properly handled in GDI DrawFrame
// and GDI+ DrawLabelFrame methods (see https://mapwindow.atlassian.net/browse/MWGIS-229)
// adding padding
if (options->frameVisible)
{
rect.left -= (LONG)ceil(double(options->framePaddingX / 2.0));
rect.right += (LONG)ceil(double(options->framePaddingX / 2.0));
rect.top -= (LONG)ceil(double(options->framePaddingY / 2.0));
rect.bottom += (LONG)ceil(double(options->framePaddingY / 2.0));
offset += options->framePaddingX;
}
if (autoOffset)
{
LabelDrawingHelper::UpdateAutoOffset(rect, align, offset);
}
rect.left += (LONG) options->offsetX;
rect.right += (LONG) options->offsetX;
rect.top += (LONG) options->offsetY;
rect.bottom += (LONG) options->offsetY;
}
// *********************************************************************
// TryAvoidCollisions()
// *********************************************************************
bool CLabelDrawer::TryAvoidCollisions(CLabelInfo* lbl, VARIANT_BOOL avoidCollisions, CRect& rect, double piX, double piY, long buffer, double angleRad)
{
if (angleRad != 0.0)
{
CRotatedRectangle* rectNew = new CRotatedRectangle(rect);
double angle1 = 2 * pi_ - angleRad;
for (int i = 0; i < 4; i++)
{
long x = rectNew->points[i].x;
long y = rectNew->points[i].y;
rectNew->points[i].x = LONG(x * cos(angle1) + y * sin(angle1) + piX);
rectNew->points[i].y = LONG(-x * sin(angle1) + y * cos(angle1) + piY);
}
if (avoidCollisions && _collisionList != NULL)
{
if (_collisionList->HaveCollision(*rectNew))
{
delete rectNew;
return false;
}
}
// memory must be cleared at this point
lbl->rotatedFrame = rectNew;
lbl->isDrawn = VARIANT_TRUE;
if (_collisionList != NULL)
{
_collisionList->AddRotatedRectangle(rectNew, buffer, buffer);
}
}
else
{
CRect* rectNew = new CRect(rect);
rectNew->left += (LONG)piX;
rectNew->right += (LONG)piX;
rectNew->bottom += (LONG)piY;
rectNew->top += (LONG)piY;
if (avoidCollisions && _collisionList != NULL)
{
if (_collisionList->HaveCollision(*rectNew))
{
delete rectNew;
return false;
}
}
// memory must be cleared at this point
lbl->horizontalFrame = rectNew;
lbl->isDrawn = VARIANT_TRUE;
if (_collisionList != NULL)
{
_collisionList->AddRectangle(rectNew, buffer, buffer);
}
}
return true;
}
// *********************************************************************
// GetExpressionFilter()
// *********************************************************************
bool CLabelDrawer::GetExpressionFilter(ILabels* labels, IShapefile* sf, vector<long>& filter)
{
CComBSTR expr;
labels->get_VisibilityExpression(&expr);
CStringW err;
if (SysStringLen(expr) > 0)
{
if (sf)
{
CComPtr<ITable> tbl = NULL;
sf->get_Table(&tbl);
USES_CONVERSION;
if (TableHelper::Cast(tbl)->QueryCore(OLE2CW(expr), filter, err))
{
return true;
}
}
}
return false;
}
// *********************************************************************
// GetVisibilityMask()
// *********************************************************************
void CLabelDrawer::GetVisibilityMask(ILabels* labels, IShapefile* sf, std::vector<ShapeRecord*>* shapeData, std::vector<bool>& visibilityMask)
{
vector<long> filter;
bool hasFilter = GetExpressionFilter(labels, sf, filter);
long numLabels;
labels->get_Count(&numLabels);
VARIANT_BOOL synchronized;
labels->get_Synchronized(&synchronized);
if (synchronized && sf)
{
long minSize;
labels->get_MinDrawingSize(&minSize);
long size = hasFilter ? (long)filter.size() : numLabels;
for (long i = 0; i < size; i++)
{
long index = hasFilter ? filter[i] : i;
visibilityMask[index] = (*shapeData)[index]->wasRendered() && (*shapeData)[index]->size >= minSize;
}
}
else
{
// for images all the labels will do
for (int i = 0; i < numLabels; i++)
{
visibilityMask[i] = true;
}
}
}
// *********************************************************************
// GetScaleFactor()
// *********************************************************************
double CLabelDrawer::GetScaleFactor(ILabels* labels)
{
VARIANT_BOOL bScale;
double scaleFactor = 1.0;
labels->get_ScaleLabels(&bScale);
if (bScale && _spatiallyReferenced)
{
double scale = _currentScale;
if (scale == 0) {
return scaleFactor;
}
double basicScale;
labels->get_BasicScale(&basicScale);
if (basicScale == 0.0)
{
basicScale = scale;
labels->put_BasicScale(scale);
}
scaleFactor = basicScale / scale;
}
return scaleFactor;
}
// *********************************************************************
// CheckVisibility()
// *********************************************************************
bool CLabelDrawer::CheckVisibility(ILabels* labels)
{
if (m_globalSettings.forceHideLabels) {
return false;
}
if (LabelsHelper::GetCount(labels) == 0) {
return false;
}
VARIANT_BOOL visible;
labels->get_Visible(&visible);
if (!visible) return false;
if (!CheckDynamicVisibility(labels)) {
return false;
}
return true;
}
// *********************************************************************
// CheckDynamicVisibility()
// *********************************************************************
bool CLabelDrawer::CheckDynamicVisibility(ILabels* labels)
{
VARIANT_BOOL dynVisibility;
labels->get_DynamicVisibility(&dynVisibility);
if (dynVisibility)
{
double minScale, maxScale;
labels->get_MinVisibleScale(&minScale);
labels->get_MaxVisibleScale(&maxScale);
long minZoom, maxZoom;
labels->get_MinVisibleZoom(&minZoom);
labels->get_MaxVisibleZoom(&maxZoom);
if (_currentScale < minScale || _currentScale > maxScale ||
_currentZoom < minZoom || _currentZoom > maxZoom) {
return false;
}
}
return true;
}
// *********************************************************************
// GetUseGdiPlus()
// *********************************************************************
bool CLabelDrawer::GetUseGdiPlus(ILabels* labels)
{
VARIANT_BOOL useGdiPlus = VARIANT_FALSE;
labels->get_UseGdiPlus(&useGdiPlus);
CLabelOptions* options = ((CLabels*)labels)->get_LabelOptions();
if (useGdiPlus && options->frameVisible) {
// GDI generally looks better, however it can't be used without frame / background
// since we are using transparent GDI+ buffer (GDI doesn't have alpha blending)
useGdiPlus = VARIANT_FALSE;
}
if (!useGdiPlus)
{
// when there is some form of alpha blending is used,
// we have to stick with GDI+
if ((options->fontGradientMode != gmNone || options->fontTransparency != 255) ||
((options->frameGradientMode != gmNone || options->frameTransparency != 255) && options->frameVisible))
{
useGdiPlus = VARIANT_TRUE;
}
}
if (_printing) {
useGdiPlus = VARIANT_TRUE;
}
return useGdiPlus ? true: false;
}
// *********************************************************************
// GetAutoOffset()
// *********************************************************************
bool CLabelDrawer::GetAutoOffset(ILabels* labels, IShapefile* sf)
{
// do we need to enable auto offset? shapefile type must be point and labels must be synchronized
VARIANT_BOOL autoOffset;
labels->get_AutoOffset(&autoOffset);
if (autoOffset && sf != NULL)
{
VARIANT_BOOL synchronized;
labels->get_Synchronized(&synchronized);
ShpfileType shpType;
sf->get_ShapefileType2D(&shpType);
autoOffset = shpType == SHP_POINT || shpType == SHP_MULTIPOINT ? synchronized : VARIANT_FALSE;
}
else
{
autoOffset = VARIANT_FALSE;
}
return autoOffset ? true : false;
}