summaryrefslogtreecommitdiff
path: root/src/mbgl/text/placement.cpp
blob: c927fdd78219c21d4206644cf4d549d9893ee654 (plain)
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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
#include <mbgl/text/placement.hpp>

#include <mbgl/layout/symbol_layout.hpp>
#include <mbgl/renderer/bucket.hpp>
#include <mbgl/renderer/buckets/symbol_bucket.hpp>
#include <mbgl/renderer/render_layer.hpp>
#include <mbgl/renderer/render_tile.hpp>
#include <mbgl/renderer/update_parameters.hpp>
#include <mbgl/tile/geometry_tile.hpp>
#include <mbgl/util/math.hpp>
#include <utility>

namespace mbgl {

OpacityState::OpacityState(bool placed_, bool skipFade)
    : opacity((skipFade && placed_) ? 1 : 0)
    , placed(placed_)
{
}

OpacityState::OpacityState(const OpacityState& prevState, float increment, bool placed_) :
    opacity(::fmax(0, ::fmin(1, prevState.opacity + (prevState.placed ? increment : -increment)))),
    placed(placed_) {}

bool OpacityState::isHidden() const {
    return opacity == 0 && !placed;
}

JointOpacityState::JointOpacityState(bool placedText, bool placedIcon, bool skipFade) :
    icon(OpacityState(placedIcon, skipFade)),
    text(OpacityState(placedText, skipFade)) {}

JointOpacityState::JointOpacityState(const JointOpacityState& prevOpacityState, float increment, bool placedText, bool placedIcon) :
    icon(OpacityState(prevOpacityState.icon, increment, placedIcon)),
    text(OpacityState(prevOpacityState.text, increment, placedText)) {}

bool JointOpacityState::isHidden() const {
    return icon.isHidden() && text.isHidden();
}
    
const CollisionGroups::CollisionGroup& CollisionGroups::get(const std::string& sourceID) {
    // The predicate/groupID mechanism allows for arbitrary grouping,
    // but the current interface defines one source == one group when
    // crossSourceCollisions == true.
    if (!crossSourceCollisions) {
        if (collisionGroups.find(sourceID) == collisionGroups.end()) {
            uint16_t nextGroupID = ++maxGroupID;
            collisionGroups.emplace(sourceID, CollisionGroup(
                nextGroupID,
                optional<Predicate>([nextGroupID](const IndexedSubfeature& feature) -> bool {
                    return feature.collisionGroupId == nextGroupID;
                })
            ));
        }
        return collisionGroups[sourceID];
    } else {
        static CollisionGroup nullGroup{0, nullopt};
        return nullGroup;
    }
}

// PlacementController implemenation

PlacementController::PlacementController() : placement(makeMutable<Placement>()) {}

void PlacementController::setPlacement(Immutable<Placement> placement_) {
    placement = std::move(placement_);
    stale = false;
}

bool PlacementController::placementIsRecent(TimePoint now, const float zoom, optional<Duration> maximumDuration) const {
    if (!placement->transitionsEnabled()) return false;

    auto updatePeriod = placement->getUpdatePeriod(zoom);

    if (maximumDuration) {
        updatePeriod = std::min(*maximumDuration, updatePeriod);
    }

    return placement->getCommitTime() + updatePeriod > now;
}

bool PlacementController::hasTransitions(TimePoint now) const {
    if (!placement->transitionsEnabled()) return false;

    if (stale) return true;

    return placement->hasTransitions(now);
}

// Placement implementation

Placement::Placement(std::shared_ptr<const UpdateParameters> updateParameters_,
                     optional<Immutable<Placement>> prevPlacement_)
    : updateParameters(std::move(updateParameters_)),
      collisionIndex(updateParameters->transformState, updateParameters->mode),
      mapMode(updateParameters->mode),
      transitionOptions(updateParameters->transitionOptions),
      commitTime(updateParameters->timePoint),
      placementZoom(updateParameters->transformState.getZoom()),
      collisionGroups(updateParameters->crossSourceCollisions),
      prevPlacement(std::move(prevPlacement_)),
      showCollisionBoxes(updateParameters->debugOptions & MapDebugOptions::Collision) {
    assert(prevPlacement || mapMode != MapMode::Continuous);
    if (prevPlacement) {
        prevPlacement->get()->prevPlacement = nullopt; // Only hold on to one placement back
    }
}

Placement::Placement() : collisionIndex({}, MapMode::Static), collisionGroups(true) {}

void Placement::placeLayer(const RenderLayer& layer) {
    std::set<uint32_t> seenCrossTileIDs;
    for (const auto& item : layer.getPlacementData()) {
        Bucket& bucket = item.bucket;
        BucketPlacementParameters params{item.tile, layer.baseImpl->source, item.featureIndex};
        bucket.place(*this, params, seenCrossTileIDs);
    }
}

namespace {
Point<float> calculateVariableLayoutOffset(style::SymbolAnchorType anchor,
                                           float width,
                                           float height,
                                           std::array<float, 2> offset,
                                           float textBoxScale,
                                           bool rotateWithMap,
                                           bool pitchWithMap,
                                           float bearing) {
    AnchorAlignment alignment = AnchorAlignment::getAnchorAlignment(anchor);
    float shiftX = -(alignment.horizontalAlign - 0.5f) * width;
    float shiftY = -(alignment.verticalAlign - 0.5f) * height;
    auto variableOffset = SymbolLayout::evaluateVariableOffset(anchor, offset);
    Point<float> shift{shiftX + variableOffset[0] * textBoxScale, shiftY + variableOffset[1] * textBoxScale};
    if (rotateWithMap) {
        shift = util::rotate(shift, pitchWithMap ? bearing : -bearing);
    }
    return shift;
}
} // namespace

void Placement::placeBucket(const SymbolBucket& bucket,
                            const BucketPlacementParameters& params,
                            std::set<uint32_t>& seenCrossTileIDs) {
    assert(updateParameters);
    const auto& layout = *bucket.layout;
    const auto& renderTile = params.tile;
    const auto& state = collisionIndex.getTransformState();
    const float pixelsToTileUnits = renderTile.id.pixelsToTileUnits(1, placementZoom);
    const OverscaledTileID& overscaledID = renderTile.getOverscaledTileID();
    const float scale = std::pow(2, placementZoom - overscaledID.overscaledZ);
    const float pixelRatio = (util::tileSize * overscaledID.overscaleFactor()) / util::EXTENT;

    const bool rotateTextWithMap = layout.get<style::TextRotationAlignment>() == style::AlignmentType::Map;
    const bool pitchTextWithMap = layout.get<style::TextPitchAlignment>() == style::AlignmentType::Map;

    const bool rotateIconWithMap = layout.get<style::IconRotationAlignment>() == style::AlignmentType::Map;
    const bool pitchIconWithMap = layout.get<style::IconPitchAlignment>() == style::AlignmentType::Map;

    const mat4& posMatrix = renderTile.matrix;

    mat4 textLabelPlaneMatrix =
        getLabelPlaneMatrix(posMatrix, pitchTextWithMap, rotateTextWithMap, state, pixelsToTileUnits);

    mat4 iconLabelPlaneMatrix;
    if (rotateTextWithMap == rotateIconWithMap && pitchTextWithMap == pitchIconWithMap) {
        iconLabelPlaneMatrix = textLabelPlaneMatrix;
    } else {
        iconLabelPlaneMatrix =
            getLabelPlaneMatrix(posMatrix, pitchIconWithMap, rotateIconWithMap, state, pixelsToTileUnits);
    }

    const auto& collisionGroup = collisionGroups.get(params.sourceId);
    auto partiallyEvaluatedTextSize = bucket.textSizeBinder->evaluateForZoom(placementZoom);
    auto partiallyEvaluatedIconSize = bucket.iconSizeBinder->evaluateForZoom(placementZoom);

    optional<CollisionBoundaries> tileBorders;
    optional<CollisionBoundaries> avoidEdges;
    if (mapMode == MapMode::Tile) tileBorders = collisionIndex.projectTileBoundaries(posMatrix);

    if (tileBorders && (layout.get<style::SymbolAvoidEdges>() ||
                        layout.get<style::SymbolPlacement>() == style::SymbolPlacementType::Line)) {
        avoidEdges = tileBorders;
    }

    const bool textAllowOverlap = layout.get<style::TextAllowOverlap>();
    const bool iconAllowOverlap = layout.get<style::IconAllowOverlap>();
    // This logic is similar to the "defaultOpacityState" logic below in updateBucketOpacities
    // If we know a symbol is always supposed to show, force it to be marked visible even if
    // it wasn't placed into the collision index (because some or all of it was outside the range
    // of the collision grid).
    // There is a subtle edge case here we're accepting:
    //  Symbol A has text-allow-overlap: true, icon-allow-overlap: true, icon-optional: false
    //  A's icon is outside the grid, so doesn't get placed
    //  A's text would be inside grid, but doesn't get placed because of icon-optional: false
    //  We still show A because of the allow-overlap settings.
    //  Symbol B has allow-overlap: false, and gets placed where A's text would be
    //  On panning in, there is a short period when Symbol B and Symbol A will overlap
    //  This is the reverse of our normal policy of "fade in on pan", but should look like any other
    //  collision and hopefully not be too noticeable.
    // See https://github.com/mapbox/mapbox-gl-native/issues/12683
    const bool alwaysShowText = textAllowOverlap && (iconAllowOverlap || !(bucket.hasIconData() || bucket.hasSdfIconData()) || layout.get<style::IconOptional>());
    const bool alwaysShowIcon = iconAllowOverlap && (textAllowOverlap || !bucket.hasTextData() || layout.get<style::TextOptional>());
    std::vector<style::TextVariableAnchorType> variableTextAnchors = layout.get<style::TextVariableAnchor>();

    const bool hasIconTextFit = layout.get<style::IconTextFit>() != style::IconTextFitType::None;

    const bool zOrderByViewportY = layout.get<style::SymbolZOrder>() == style::SymbolZOrderType::ViewportY;
    std::vector<ProjectedCollisionBox> textBoxes;
    std::vector<ProjectedCollisionBox> iconBoxes;

    auto placeSymbol = [&](const SymbolInstance& symbolInstance) {
        if (symbolInstance.crossTileID == SymbolInstance::invalidCrossTileID() ||
            seenCrossTileIDs.count(symbolInstance.crossTileID) != 0u)
            return;

        if (renderTile.holdForFade()) {
            // Mark all symbols from this tile as "not placed", but don't add to seenCrossTileIDs, because we don't
            // know yet if we have a duplicate in a parent tile that _should_ be placed.
            placements.emplace(symbolInstance.crossTileID, JointPlacement(false, false, false));
            return;
        }
        textBoxes.clear();
        iconBoxes.clear();

        bool placeText = false;
        bool placeIcon = false;
        bool offscreen = true;
        std::pair<bool, bool> placed{ false, false };
        std::pair<bool, bool> placedVerticalText{ false, false };
        std::pair<bool, bool> placedVerticalIcon{ false, false };
        Point<float> shift{0.0f, 0.0f};
        optional<size_t> horizontalTextIndex = symbolInstance.getDefaultHorizontalPlacedTextIndex();
        if (horizontalTextIndex) {
            const PlacedSymbol& placedSymbol = bucket.text.placedSymbols.at(*horizontalTextIndex);
            const float fontSize = evaluateSizeForFeature(partiallyEvaluatedTextSize, placedSymbol);

            const auto updatePreviousOrientationIfNotPlaced = [&](bool isPlaced) {
                if (bucket.allowVerticalPlacement && !isPlaced && getPrevPlacement()) {
                    auto prevOrientation = getPrevPlacement()->placedOrientations.find(symbolInstance.crossTileID);
                    if (prevOrientation != getPrevPlacement()->placedOrientations.end()) {
                        placedOrientations[symbolInstance.crossTileID] = prevOrientation->second;
                    }
                }
            };

            const auto placeTextForPlacementModes = [&](auto& placeHorizontalFn, auto& placeVerticalFn) {
                if (bucket.allowVerticalPlacement && symbolInstance.writingModes & WritingModeType::Vertical) {
                    assert(!bucket.placementModes.empty());
                    for (auto& placementMode : bucket.placementModes) {
                        if (placementMode == style::TextWritingModeType::Vertical) {
                            placedVerticalText = placed = placeVerticalFn();
                        } else {
                            placed = placeHorizontalFn();
                        }

                        if (placed.first) {
                            break;
                        }
                    }
                } else {
                    placed = placeHorizontalFn();
                }
            };

            // Line or point label placement
            if (variableTextAnchors.empty()) {
                const auto placeFeature = [&](const CollisionFeature& collisionFeature,
                                              style::TextWritingModeType orientation) {
                    textBoxes.clear();
                    auto placedFeature = collisionIndex.placeFeature(collisionFeature,
                                                                     {},
                                                                     posMatrix,
                                                                     textLabelPlaneMatrix,
                                                                     pixelRatio,
                                                                     placedSymbol,
                                                                     scale,
                                                                     fontSize,
                                                                     textAllowOverlap,
                                                                     pitchTextWithMap,
                                                                     showCollisionBoxes,
                                                                     avoidEdges,
                                                                     collisionGroup.second,
                                                                     textBoxes);
                    if (placedFeature.first) {
                        placedOrientations.emplace(symbolInstance.crossTileID, orientation);
                    }
                    return placedFeature;
                };

                const auto placeHorizontal = [&] {
                    return placeFeature(symbolInstance.textCollisionFeature, style::TextWritingModeType::Horizontal);
                };

                const auto placeVertical = [&] {
                    if (bucket.allowVerticalPlacement && symbolInstance.verticalTextCollisionFeature) {
                        return placeFeature(*symbolInstance.verticalTextCollisionFeature,
                                            style::TextWritingModeType::Vertical);
                    }
                    return std::pair<bool, bool>{false, false};
                };

                placeTextForPlacementModes(placeHorizontal, placeVertical);
                updatePreviousOrientationIfNotPlaced(placed.first);

                placeText = placed.first;
                offscreen &= placed.second;
            } else if (!symbolInstance.textCollisionFeature.alongLine &&
                       !symbolInstance.textCollisionFeature.boxes.empty()) {
                // If this symbol was in the last placement, shift the previously used
                // anchor to the front of the anchor list, only if the previous anchor
                // is still in the anchor list.
                if (getPrevPlacement()) {
                    auto prevOffset = getPrevPlacement()->variableOffsets.find(symbolInstance.crossTileID);
                    if (prevOffset != getPrevPlacement()->variableOffsets.end()) {
                        const auto prevAnchor = prevOffset->second.anchor;
                        auto found = std::find(variableTextAnchors.begin(), variableTextAnchors.end(), prevAnchor);
                        if (found != variableTextAnchors.begin() && found != variableTextAnchors.end()) {
                            std::vector<style::TextVariableAnchorType> filtered;
                            filtered.reserve(variableTextAnchors.size());
                            filtered.push_back(prevAnchor);
                            for (auto anchor : variableTextAnchors) {
                                if (anchor != prevAnchor) {
                                    filtered.push_back(anchor);
                                }
                            }
                            variableTextAnchors = std::move(filtered);
                        }
                    }
                }

                const bool doVariableIconPlacement =
                    hasIconTextFit && !iconAllowOverlap && symbolInstance.placedIconIndex;
                const auto placeFeatureForVariableAnchors = [&](const CollisionFeature& textCollisionFeature,
                                                                style::TextWritingModeType orientation,
                                                                const CollisionFeature& iconCollisionFeature) {
                    const CollisionBox& textBox = textCollisionFeature.boxes[0];
                    const float width = textBox.x2 - textBox.x1;
                    const float height = textBox.y2 - textBox.y1;
                    const float textBoxScale = symbolInstance.textBoxScale;
                    std::pair<bool, bool> placedFeature = {false, false};
                    const size_t anchorsSize = variableTextAnchors.size();
                    const size_t placementAttempts = textAllowOverlap ? anchorsSize * 2 : anchorsSize;
                    for (size_t i = 0u; i < placementAttempts; ++i) {
                        auto anchor = variableTextAnchors[i % anchorsSize];
                        const bool isFirstAnchor = (anchor == variableTextAnchors.front());
                        const bool allowOverlap = (i >= anchorsSize);
                        shift = calculateVariableLayoutOffset(anchor,
                                                              width,
                                                              height,
                                                              symbolInstance.variableTextOffset,
                                                              textBoxScale,
                                                              rotateTextWithMap,
                                                              pitchTextWithMap,
                                                              state.getBearing());
                        textBoxes.clear();

                        if (mapMode == MapMode::Tile && !isFirstAnchor &&
                            collisionIndex.intercectsTileEdges(symbolInstance.textCollisionFeature.boxes.front(),
                                                               shift,
                                                               posMatrix,
                                                               pixelRatio,
                                                               *tileBorders)) {
                            continue;
                        }

                        placedFeature = collisionIndex.placeFeature(textCollisionFeature,
                                                                    shift,
                                                                    posMatrix,
                                                                    mat4(),
                                                                    pixelRatio,
                                                                    placedSymbol,
                                                                    scale,
                                                                    fontSize,
                                                                    allowOverlap,
                                                                    pitchTextWithMap,
                                                                    showCollisionBoxes,
                                                                    avoidEdges,
                                                                    collisionGroup.second,
                                                                    textBoxes);

                        if (doVariableIconPlacement) {
                            auto placedIconFeature =
                                collisionIndex.placeFeature(iconCollisionFeature,
                                                            shift,
                                                            posMatrix,
                                                            iconLabelPlaneMatrix,
                                                            pixelRatio,
                                                            placedSymbol,
                                                            scale,
                                                            fontSize,
                                                            iconAllowOverlap,
                                                            pitchTextWithMap, // TODO: shall it be pitchIconWithMap?
                                                            showCollisionBoxes,
                                                            avoidEdges,
                                                            collisionGroup.second,
                                                            iconBoxes);
                            iconBoxes.clear();
                            if (!placedIconFeature.first) continue;
                        }

                        if (placedFeature.first) {
                            assert(symbolInstance.crossTileID != 0u);
                            optional<style::TextVariableAnchorType> prevAnchor;

                            // If this label was placed in the previous placement, record the anchor position
                            // to allow us to animate the transition
                            if (getPrevPlacement()) {
                                auto prevOffset = getPrevPlacement()->variableOffsets.find(symbolInstance.crossTileID);
                                auto prevPlacements = getPrevPlacement()->placements.find(symbolInstance.crossTileID);
                                if (prevOffset != getPrevPlacement()->variableOffsets.end() &&
                                    prevPlacements != getPrevPlacement()->placements.end() &&
                                    prevPlacements->second.text) {
                                    // TODO: The prevAnchor seems to be unused, needs to be fixed.
                                    prevAnchor = prevOffset->second.anchor;
                                }
                            }

                            variableOffsets.insert(std::make_pair(symbolInstance.crossTileID, VariableOffset{
                                symbolInstance.variableTextOffset,
                                width,
                                height,
                                anchor,
                                textBoxScale,
                                prevAnchor
                            }));

                            if (bucket.allowVerticalPlacement) {
                                placedOrientations.emplace(symbolInstance.crossTileID, orientation);
                            }
                            break;
                        }
                    }

                    return placedFeature;
                };

                const auto placeHorizontal = [&] {
                    return placeFeatureForVariableAnchors(symbolInstance.textCollisionFeature,
                                                          style::TextWritingModeType::Horizontal,
                                                          symbolInstance.iconCollisionFeature);
                };

                const auto placeVertical = [&] {
                    if (bucket.allowVerticalPlacement && !placed.first && symbolInstance.verticalTextCollisionFeature) {
                        return placeFeatureForVariableAnchors(*symbolInstance.verticalTextCollisionFeature,
                                                              style::TextWritingModeType::Vertical,
                                                              symbolInstance.verticalIconCollisionFeature
                                                                  ? *symbolInstance.verticalIconCollisionFeature
                                                                  : symbolInstance.iconCollisionFeature);
                    }
                    return std::pair<bool, bool>{false, false};
                };

                placeTextForPlacementModes(placeHorizontal, placeVertical);

                placeText = placed.first;
                offscreen &= placed.second;

                updatePreviousOrientationIfNotPlaced(placed.first);

                // If we didn't get placed, we still need to copy our position from the last placement for
                // fade animations
                if (!placeText && getPrevPlacement()) {
                    auto prevOffset = getPrevPlacement()->variableOffsets.find(symbolInstance.crossTileID);
                    if (prevOffset != getPrevPlacement()->variableOffsets.end()) {
                        variableOffsets[symbolInstance.crossTileID] = prevOffset->second;
                    }
                }
            }
        }

        if (symbolInstance.placedIconIndex) {
            if (!hasIconTextFit || !placeText || variableTextAnchors.empty()) {
                shift = {0.0f, 0.0f};
            }

            const auto& iconBuffer = symbolInstance.hasSdfIcon() ? bucket.sdfIcon : bucket.icon;
            const PlacedSymbol& placedSymbol = iconBuffer.placedSymbols.at(*symbolInstance.placedIconIndex);
            const float fontSize = evaluateSizeForFeature(partiallyEvaluatedIconSize, placedSymbol);
            const auto& placeIconFeature = [&](const CollisionFeature& collisionFeature) {
                return collisionIndex.placeFeature(collisionFeature,
                                                   shift,
                                                   posMatrix,
                                                   iconLabelPlaneMatrix,
                                                   pixelRatio,
                                                   placedSymbol,
                                                   scale,
                                                   fontSize,
                                                   iconAllowOverlap,
                                                   pitchTextWithMap,
                                                   showCollisionBoxes,
                                                   avoidEdges,
                                                   collisionGroup.second,
                                                   iconBoxes);
            };

            std::pair<bool, bool> placedIcon = {false, false};
            if (placedVerticalText.first && symbolInstance.verticalIconCollisionFeature) {
                placedIcon = placedVerticalIcon = placeIconFeature(*symbolInstance.verticalIconCollisionFeature);
            } else {
                placedIcon = placeIconFeature(symbolInstance.iconCollisionFeature);
            }
            placeIcon = placedIcon.first;
            offscreen &= placedIcon.second;
        }

        const bool iconWithoutText = !symbolInstance.hasText() || layout.get<style::TextOptional>();
        const bool textWithoutIcon = !symbolInstance.hasIcon() || layout.get<style::IconOptional>();

        // combine placements for icon and text
        if (!iconWithoutText && !textWithoutIcon) {
            placeText = placeIcon = placeText && placeIcon;
        } else if (!textWithoutIcon) {
            placeText = placeText && placeIcon;
        } else if (!iconWithoutText) {
            placeIcon = placeText && placeIcon;
        }

        if (placeText) {
            if (placedVerticalText.first && symbolInstance.verticalTextCollisionFeature) {
                collisionIndex.insertFeature(*symbolInstance.verticalTextCollisionFeature, textBoxes, layout.get<style::TextIgnorePlacement>(), bucket.bucketInstanceId, collisionGroup.first);
            } else {
                collisionIndex.insertFeature(symbolInstance.textCollisionFeature, textBoxes, layout.get<style::TextIgnorePlacement>(), bucket.bucketInstanceId, collisionGroup.first);
            }
        }

        if (placeIcon) {
            if (placedVerticalIcon.first && symbolInstance.verticalIconCollisionFeature) {
                collisionIndex.insertFeature(*symbolInstance.verticalIconCollisionFeature, iconBoxes, layout.get<style::IconIgnorePlacement>(), bucket.bucketInstanceId, collisionGroup.first);
            } else {
                collisionIndex.insertFeature(symbolInstance.iconCollisionFeature, iconBoxes, layout.get<style::IconIgnorePlacement>(), bucket.bucketInstanceId, collisionGroup.first);
            }
        }

        const bool hasIconCollisionCircleData = bucket.hasIconCollisionCircleData();
        const bool hasTextCollisionCircleData = bucket.hasTextCollisionCircleData();

        if (hasIconCollisionCircleData && symbolInstance.iconCollisionFeature.alongLine && !iconBoxes.empty()) {
            collisionCircles[&symbolInstance.iconCollisionFeature] = iconBoxes;
        }
        if (hasTextCollisionCircleData && symbolInstance.textCollisionFeature.alongLine && !textBoxes.empty()) {
            collisionCircles[&symbolInstance.textCollisionFeature] = textBoxes;
        }

        assert(symbolInstance.crossTileID != 0);

        if (placements.find(symbolInstance.crossTileID) != placements.end()) {
            // If there's a previous placement with this ID, it comes from a tile that's fading out
            // Erase it so that the placement result from the non-fading tile supersedes it
            placements.erase(symbolInstance.crossTileID);
        }
        
        placements.emplace(symbolInstance.crossTileID, JointPlacement(placeText || alwaysShowText, placeIcon || alwaysShowIcon, offscreen || bucket.justReloaded));
        seenCrossTileIDs.insert(symbolInstance.crossTileID);
    };

    if (zOrderByViewportY) {
        const auto& sortedSymbols = bucket.getSortedSymbols(state.getBearing());
        // Place in the reverse order than draw i.e., starting from the foreground elements.
        for (auto it = sortedSymbols.rbegin(); it != sortedSymbols.rend(); ++it) {
            placeSymbol(*it);
        }
    } else if (mapMode == MapMode::Tile && !avoidEdges) {
        // In this case we first try to place symbols, which intersects the tile borders, so that
        // those symbols will remain even if each tile is handled independently.
        std::vector<std::reference_wrapper<const SymbolInstance>> symbolInstances(bucket.symbolInstances.begin(),
                                                                                  bucket.symbolInstances.end());
        optional<style::TextVariableAnchorType> variableAnchor;
        if (!variableTextAnchors.empty()) variableAnchor = variableTextAnchors.front();

        std::unordered_map<uint32_t, bool> locationCache;
        locationCache.reserve(symbolInstances.size());

        // Keeps the data necessary to find a feature location according to a tile.
        struct NeighborTileData {
            NeighborTileData(const CollisionIndex& collisionIndex, UnwrappedTileID id_, Point<float> shift_)
                : id(id_), shift(shift_), matrix() {
                collisionIndex.getTransformState().matrixFor(matrix, id);
                matrix::multiply(matrix, collisionIndex.getTransformState().getProjectionMatrix(), matrix);
                borders = collisionIndex.projectTileBoundaries(matrix);
            }

            UnwrappedTileID id;
            Point<float> shift;
            mat4 matrix;
            CollisionBoundaries borders;
        };

        uint8_t z = renderTile.id.canonical.z;
        uint32_t x = renderTile.id.canonical.x;
        uint32_t y = renderTile.id.canonical.y;
        const std::array<NeighborTileData, 4> neightbors{{
            {collisionIndex, UnwrappedTileID(z, x, y - 1), {0.0f, util::EXTENT}},  // top
            {collisionIndex, UnwrappedTileID(z, x, y + 1), {0.0f, -util::EXTENT}}, // bottom
            {collisionIndex, UnwrappedTileID(z, x - 1, y), {util::EXTENT, 0.0f}},  // left
            {collisionIndex, UnwrappedTileID(z, x + 1, y), {-util::EXTENT, 0.0f}}  // right
        }};

        auto intercectsTileEdges = [&](const CollisionBox& collisionBox, const SymbolInstance& symbol) -> bool {
            auto it = locationCache.find(symbol.crossTileID);
            if (it != locationCache.end()) return it->second;

            Point<float> offset{};
            if (variableAnchor) {
                const CollisionBox& textBox = symbol.textCollisionFeature.boxes.front();
                float width = textBox.x2 - textBox.x1;
                float height = textBox.y2 - textBox.y1;
                offset = calculateVariableLayoutOffset(*variableAnchor,
                                                       width,
                                                       height,
                                                       symbol.variableTextOffset,
                                                       symbol.textBoxScale,
                                                       rotateTextWithMap,
                                                       pitchTextWithMap,
                                                       state.getBearing());
            }

            bool intercects =
                collisionIndex.intercectsTileEdges(collisionBox, offset, renderTile.matrix, pixelRatio, *tileBorders);
            if (!intercects) {
                // Check if this symbol intersects the neighbor tile borders.
                // If so, it also shall be placed with priority (location = FeatureLocation::IntersectsTileBorder).
                for (const auto& neighbor : neightbors) {
                    intercects = collisionIndex.intercectsTileEdges(
                        collisionBox, offset + neighbor.shift, neighbor.matrix, pixelRatio, neighbor.borders);
                    if (intercects) break;
                }
            }

            locationCache.insert(std::make_pair(symbol.crossTileID, intercects));
            return intercects;
        };

        std::stable_sort(symbolInstances.begin(),
                         symbolInstances.end(),
                         [&intercectsTileEdges](const SymbolInstance& a, const SymbolInstance& b) {
                             assert(!a.textCollisionFeature.alongLine);
                             assert(!b.textCollisionFeature.alongLine);
                             if (a.textCollisionFeature.boxes.empty() || b.textCollisionFeature.boxes.empty())
                                 return false;

                             auto intercectsA = intercectsTileEdges(a.textCollisionFeature.boxes.front(), a);
                             auto intercectsB = intercectsTileEdges(b.textCollisionFeature.boxes.front(), b);
                             if (intercectsA) {
                                 if (!intercectsB) return true;
                                 // Both symbols are inrecepting the tile borders, we need a universal cross-tile rule
                                 // to define which of them shall be placed first - use anchor `y` point.
                                 return a.anchor.point.y < b.anchor.point.y;
                             }
                             return false;
                         });

        for (const SymbolInstance& symbol : symbolInstances) {
            placeSymbol(symbol);
        }

    } else {
        for (const SymbolInstance& symbol : bucket.symbolInstances) {
            placeSymbol(symbol);
        }
    }

    bucket.justReloaded = false;

    // As long as this placement lives, we have to hold onto this bucket's
    // matching FeatureIndex/data for querying purposes
    retainedQueryData.emplace(std::piecewise_construct,
                                std::forward_as_tuple(bucket.bucketInstanceId),
                                std::forward_as_tuple(bucket.bucketInstanceId, params.featureIndex, overscaledID));
}

void Placement::commit() {
    if (!getPrevPlacement()) {
        assert(mapMode != MapMode::Continuous);
        fadeStartTime = commitTime;
        for (auto& jointPlacement : placements) {
            opacities.emplace(
                jointPlacement.first,
                JointOpacityState(
                    jointPlacement.second.text, jointPlacement.second.icon, jointPlacement.second.skipFade));
        }
        return;
    }

    bool placementChanged = false;

    prevZoomAdjustment = getPrevPlacement()->zoomAdjustment(placementZoom);
    float increment = getPrevPlacement()->symbolFadeChange(commitTime);

    // add the opacities from the current placement, and copy their current values from the previous placement
    for (auto& jointPlacement : placements) {
        auto prevOpacity = getPrevPlacement()->opacities.find(jointPlacement.first);
        if (prevOpacity != getPrevPlacement()->opacities.end()) {
            opacities.emplace(jointPlacement.first, JointOpacityState(prevOpacity->second, increment, jointPlacement.second.text, jointPlacement.second.icon));
            placementChanged = placementChanged ||
                jointPlacement.second.icon != prevOpacity->second.icon.placed ||
                jointPlacement.second.text != prevOpacity->second.text.placed;
        } else {
            opacities.emplace(jointPlacement.first, JointOpacityState(jointPlacement.second.text, jointPlacement.second.icon, jointPlacement.second.skipFade));
            placementChanged = placementChanged || jointPlacement.second.icon || jointPlacement.second.text;
        }
    }

    // copy and update values from the previous placement that aren't in the current placement but haven't finished fading
    for (auto& prevOpacity : getPrevPlacement()->opacities) {
        if (opacities.find(prevOpacity.first) == opacities.end()) {
            JointOpacityState jointOpacity(prevOpacity.second, increment, false, false);
            if (!jointOpacity.isHidden()) {
                opacities.emplace(prevOpacity.first, jointOpacity);
                placementChanged = placementChanged || prevOpacity.second.icon.placed || prevOpacity.second.text.placed;
            }
        }
    }

    for (auto& prevOffset : getPrevPlacement()->variableOffsets) {
        const uint32_t crossTileID = prevOffset.first;
        auto foundOffset = variableOffsets.find(crossTileID);
        auto foundOpacity = opacities.find(crossTileID);
        if (foundOffset == variableOffsets.end() && foundOpacity != opacities.end() && !foundOpacity->second.isHidden()) {
            variableOffsets[prevOffset.first] = prevOffset.second;
        }
    }

    for (auto& prevOrientation : getPrevPlacement()->placedOrientations) {
        const uint32_t crossTileID = prevOrientation.first;
        auto foundOrientation = placedOrientations.find(crossTileID);
        auto foundOpacity = opacities.find(crossTileID);
        if (foundOrientation == placedOrientations.end() && foundOpacity != opacities.end() && !foundOpacity->second.isHidden()) {
            placedOrientations[prevOrientation.first] = prevOrientation.second;
        }
    }

    fadeStartTime = placementChanged ? commitTime : getPrevPlacement()->fadeStartTime;
}

void Placement::updateLayerBuckets(const RenderLayer& layer, const TransformState& state, bool updateOpacities) const {
    std::set<uint32_t> seenCrossTileIDs;
    for (const auto& item : layer.getPlacementData()) {
        item.bucket.get().updateVertices(*this, updateOpacities, state, item.tile, seenCrossTileIDs);
    }
}

namespace {
Point<float> calculateVariableRenderShift(style::SymbolAnchorType anchor, float width, float height, std::array<float, 2> textOffset, float textBoxScale, float renderTextSize) {
    AnchorAlignment alignment = AnchorAlignment::getAnchorAlignment(anchor);
    float shiftX = -(alignment.horizontalAlign - 0.5f) * width;
    float shiftY = -(alignment.verticalAlign - 0.5f) * height;
    auto variablOffset = SymbolLayout::evaluateVariableOffset(anchor, textOffset);
    return { (shiftX / textBoxScale + variablOffset[0]) * renderTextSize,
             (shiftY / textBoxScale + variablOffset[1]) * renderTextSize };
}
} // namespace

bool Placement::updateBucketDynamicVertices(SymbolBucket& bucket, const TransformState& state, const RenderTile& tile) const {
    using namespace style;
    const auto& layout = *bucket.layout;
    const bool alongLine = layout.get<SymbolPlacement>() != SymbolPlacementType::Point;
    const bool hasVariableAnchors = !layout.get<TextVariableAnchor>().empty() && bucket.hasTextData();
    const bool updateTextFitIcon = layout.get<IconTextFit>() != IconTextFitType::None && (bucket.allowVerticalPlacement || hasVariableAnchors) && (bucket.hasIconData() || bucket.hasSdfIconData());
    bool result = false;

    if (alongLine) {
        if (layout.get<IconRotationAlignment>() == AlignmentType::Map) {
            const bool pitchWithMap = layout.get<style::IconPitchAlignment>() == style::AlignmentType::Map;
            const bool keepUpright = layout.get<style::IconKeepUpright>();
            if (bucket.hasSdfIconData()) {
                reprojectLineLabels(bucket.sdfIcon.dynamicVertices, bucket.sdfIcon.placedSymbols,
                                    tile.matrix, pitchWithMap, true /*rotateWithMap*/, keepUpright,
                                    tile, *bucket.iconSizeBinder, state);
                result = true;
            }
            if (bucket.hasIconData()) {
                reprojectLineLabels(bucket.icon.dynamicVertices, bucket.icon.placedSymbols,
                                    tile.matrix, pitchWithMap, true /*rotateWithMap*/, keepUpright,
                                    tile, *bucket.iconSizeBinder, state);
                result = true;
            }
        }

        if (bucket.hasTextData() && layout.get<TextRotationAlignment>() == AlignmentType::Map) {
            const bool pitchWithMap = layout.get<style::TextPitchAlignment>() == style::AlignmentType::Map;
            const bool keepUpright = layout.get<style::TextKeepUpright>();
            reprojectLineLabels(bucket.text.dynamicVertices, bucket.text.placedSymbols,
                tile.matrix, pitchWithMap, true /*rotateWithMap*/, keepUpright,
                tile, *bucket.textSizeBinder, state);
            result = true;
        }
    } else if (hasVariableAnchors) {
        bucket.text.dynamicVertices.clear();
        bucket.hasVariablePlacement = false;

        const auto partiallyEvaluatedSize = bucket.textSizeBinder->evaluateForZoom(state.getZoom());
        const float tileScale = std::pow(2, state.getZoom() - tile.getOverscaledTileID().overscaledZ);
        const bool rotateWithMap = layout.get<TextRotationAlignment>() == AlignmentType::Map;
        const bool pitchWithMap = layout.get<TextPitchAlignment>() == AlignmentType::Map;
        const float pixelsToTileUnits = tile.id.pixelsToTileUnits(1.0, state.getZoom());
        const auto labelPlaneMatrix = getLabelPlaneMatrix(tile.matrix, pitchWithMap, rotateWithMap, state, pixelsToTileUnits);
        std::unordered_map<std::size_t, std::pair<std::size_t, Point<float>>> placedTextShifts;

        for (std::size_t i = 0; i < bucket.text.placedSymbols.size(); ++i) {
            const PlacedSymbol& symbol = bucket.text.placedSymbols[i];
            optional<VariableOffset> variableOffset;
            const bool skipOrientation = bucket.allowVerticalPlacement && !symbol.placedOrientation;
            if (!symbol.hidden && symbol.crossTileID != 0u && !skipOrientation) {
                auto it = variableOffsets.find(symbol.crossTileID);
                if (it != variableOffsets.end()) {
                    bucket.hasVariablePlacement = true;
                    variableOffset = it->second;
                }
            }

            if (!variableOffset) {
                // These symbols are from a justification that is not being used, or a label that wasn't placed
                // so we don't need to do the extra math to figure out what incremental shift to apply.
                hideGlyphs(symbol.glyphOffsets.size(), bucket.text.dynamicVertices);
            } else {
                const Point<float> tileAnchor = symbol.anchorPoint;
                const auto projectedAnchor = project(tileAnchor, pitchWithMap ? tile.matrix : labelPlaneMatrix);
                const float perspectiveRatio = 0.5f + 0.5f * (state.getCameraToCenterDistance() / projectedAnchor.second);
                float renderTextSize = evaluateSizeForFeature(partiallyEvaluatedSize, symbol) * perspectiveRatio / util::ONE_EM;
                if (pitchWithMap) {
                    // Go from size in pixels to equivalent size in tile units
                    renderTextSize *= bucket.tilePixelRatio / tileScale;
                }

                auto shift = calculateVariableRenderShift(
                        (*variableOffset).anchor,
                        (*variableOffset).width,
                        (*variableOffset).height,
                        (*variableOffset).offset,
                        (*variableOffset).textBoxScale,
                        renderTextSize);

                // Usual case is that we take the projected anchor and add the pixel-based shift
                // calculated above. In the (somewhat weird) case of pitch-aligned text, we add an equivalent
                // tile-unit based shift to the anchor before projecting to the label plane.
                Point<float> shiftedAnchor;
                if (pitchWithMap) {
                    shiftedAnchor = project(Point<float>(tileAnchor.x + shift.x, tileAnchor.y + shift.y),
                                            labelPlaneMatrix).first;
                } else if (rotateWithMap) {
                    auto rotated = util::rotate(shift, -state.getPitch());
                    shiftedAnchor = Point<float>(projectedAnchor.first.x + rotated.x,
                                                projectedAnchor.first.y + rotated.y);
                } else {
                    shiftedAnchor = Point<float>(projectedAnchor.first.x + shift.x,
                                                 projectedAnchor.first.y + shift.y);
                }

                if (updateTextFitIcon && symbol.placedIconIndex) {
                    placedTextShifts.emplace(*symbol.placedIconIndex,
                                             std::pair<std::size_t, Point<float>>{i, shiftedAnchor});
                }

                for (std::size_t j = 0; j < symbol.glyphOffsets.size(); ++j) {
                    addDynamicAttributes(shiftedAnchor, symbol.angle, bucket.text.dynamicVertices);
                }
            }
        }

        if (updateTextFitIcon && bucket.hasVariablePlacement) {
            auto updateIcon = [&](SymbolBucket::Buffer& iconBuffer) {
                iconBuffer.dynamicVertices.clear();
                for (std::size_t i = 0; i < iconBuffer.placedSymbols.size(); ++i) {
                    const PlacedSymbol& placedIcon = iconBuffer.placedSymbols[i];
                    if (placedIcon.hidden || (!placedIcon.placedOrientation && bucket.allowVerticalPlacement)) {
                        hideGlyphs(placedIcon.glyphOffsets.size(), iconBuffer.dynamicVertices);
                    } else {
                        const auto& pair = placedTextShifts.find(i);
                        if (pair == placedTextShifts.end()) {
                            hideGlyphs(placedIcon.glyphOffsets.size(), iconBuffer.dynamicVertices);
                        } else {
                            for (std::size_t j = 0; j < placedIcon.glyphOffsets.size(); ++j) {
                                addDynamicAttributes(pair->second.second, placedIcon.angle, iconBuffer.dynamicVertices);
                            }
                        }
                    }
                }
            };
            updateIcon(bucket.icon);
            updateIcon(bucket.sdfIcon);
        }

        result = true;
    } else if (bucket.allowVerticalPlacement && bucket.hasTextData()) {
        const auto updateDynamicVertices = [](SymbolBucket::Buffer& buffer) {
            buffer.dynamicVertices.clear();
            for (const PlacedSymbol& symbol : buffer.placedSymbols) {
                if (symbol.hidden || !symbol.placedOrientation) {
                    hideGlyphs(symbol.glyphOffsets.size(), buffer.dynamicVertices);
                } else {
                    for (std::size_t j = 0; j < symbol.glyphOffsets.size(); ++j) {
                        addDynamicAttributes(symbol.anchorPoint, symbol.angle, buffer.dynamicVertices);
                    }
                }
            }
        };

        updateDynamicVertices(bucket.text);
        // When text box is rotated, icon-text-fit icon must be rotated as well.
        if (updateTextFitIcon) {
            updateDynamicVertices(bucket.icon);
            updateDynamicVertices(bucket.sdfIcon);
        }

        result = true;
    }

    return result;
}

void Placement::updateBucketOpacities(SymbolBucket& bucket,
                                      const TransformState& state,
                                      std::set<uint32_t>& seenCrossTileIDs) const {
    if (bucket.hasTextData()) bucket.text.opacityVertices.clear();
    if (bucket.hasIconData()) bucket.icon.opacityVertices.clear();
    if (bucket.hasSdfIconData()) bucket.sdfIcon.opacityVertices.clear();
    if (bucket.hasIconCollisionBoxData()) bucket.iconCollisionBox->dynamicVertices.clear();
    if (bucket.hasIconCollisionCircleData()) bucket.iconCollisionCircle->dynamicVertices.clear();
    if (bucket.hasTextCollisionBoxData()) bucket.textCollisionBox->dynamicVertices.clear();
    if (bucket.hasTextCollisionCircleData()) bucket.textCollisionCircle->dynamicVertices.clear();

    const JointOpacityState duplicateOpacityState(false, false, true);

    const bool textAllowOverlap = bucket.layout->get<style::TextAllowOverlap>();
    const bool iconAllowOverlap = bucket.layout->get<style::IconAllowOverlap>();
    const bool variablePlacement = !bucket.layout->get<style::TextVariableAnchor>().empty();
    const bool rotateWithMap = bucket.layout->get<style::TextRotationAlignment>() == style::AlignmentType::Map;
    const bool pitchWithMap = bucket.layout->get<style::TextPitchAlignment>() == style::AlignmentType::Map;
    const bool hasIconTextFit = bucket.layout->get<style::IconTextFit>() != style::IconTextFitType::None;

    // If allow-overlap is true, we can show symbols before placement runs on them
    // But we have to wait for placement if we potentially depend on a paired icon/text
    // with allow-overlap: false.
    // See https://github.com/mapbox/mapbox-gl-native/issues/12483
    const JointOpacityState defaultOpacityState(
            textAllowOverlap && (iconAllowOverlap || !(bucket.hasIconData() || bucket.hasSdfIconData()) || bucket.layout->get<style::IconOptional>()),
            iconAllowOverlap && (textAllowOverlap || !bucket.hasTextData() || bucket.layout->get<style::TextOptional>()),
            true);

    for (SymbolInstance& symbolInstance : bucket.symbolInstances) {
        bool isDuplicate = seenCrossTileIDs.count(symbolInstance.crossTileID) > 0;

        auto it = opacities.find(symbolInstance.crossTileID);
        auto opacityState = defaultOpacityState;
        if (isDuplicate) {
            opacityState = duplicateOpacityState;
        } else if (it != opacities.end()) {
            opacityState = it->second;
        }

        seenCrossTileIDs.insert(symbolInstance.crossTileID);

        if (symbolInstance.hasText()) {
            size_t textOpacityVerticesSize = 0u;
            const auto& opacityVertex = SymbolSDFTextProgram::opacityVertex(opacityState.text.placed, opacityState.text.opacity);
            if (symbolInstance.placedRightTextIndex) {
                textOpacityVerticesSize += symbolInstance.rightJustifiedGlyphQuadsSize * 4;
                PlacedSymbol& placed = bucket.text.placedSymbols[*symbolInstance.placedRightTextIndex];
                placed.hidden = opacityState.isHidden();
            }
            if (symbolInstance.placedCenterTextIndex && !symbolInstance.singleLine) {
                textOpacityVerticesSize += symbolInstance.centerJustifiedGlyphQuadsSize * 4;
                PlacedSymbol& placed = bucket.text.placedSymbols[*symbolInstance.placedCenterTextIndex];
                placed.hidden = opacityState.isHidden();
            }
            if (symbolInstance.placedLeftTextIndex && !symbolInstance.singleLine) {
                textOpacityVerticesSize += symbolInstance.leftJustifiedGlyphQuadsSize * 4;
                PlacedSymbol& placed = bucket.text.placedSymbols[*symbolInstance.placedLeftTextIndex];
                placed.hidden = opacityState.isHidden();
            }
            if (symbolInstance.placedVerticalTextIndex) {
                textOpacityVerticesSize += symbolInstance.verticalGlyphQuadsSize * 4;
                bucket.text.placedSymbols[*symbolInstance.placedVerticalTextIndex].hidden = opacityState.isHidden();
            }

            bucket.text.opacityVertices.extend(textOpacityVerticesSize, opacityVertex);

            style::TextWritingModeType previousOrientation = style::TextWritingModeType::Horizontal;
            if (bucket.allowVerticalPlacement) {
                auto prevOrientation = placedOrientations.find(symbolInstance.crossTileID);
                if (prevOrientation != placedOrientations.end()) {
                    previousOrientation = prevOrientation->second;
                    markUsedOrientation(bucket, prevOrientation->second, symbolInstance);
                }
            }

            auto prevOffset = variableOffsets.find(symbolInstance.crossTileID);
            if (prevOffset != variableOffsets.end()) {
                markUsedJustification(bucket, prevOffset->second.anchor, symbolInstance, previousOrientation);
            }
        }
        if (symbolInstance.hasIcon()) {
            size_t iconOpacityVerticesSize = 0u;
            const auto& opacityVertex =
                SymbolIconProgram::opacityVertex(opacityState.icon.placed, opacityState.icon.opacity);
            auto& iconBuffer = symbolInstance.hasSdfIcon() ? bucket.sdfIcon : bucket.icon;
            
            if (symbolInstance.placedIconIndex) {
                iconOpacityVerticesSize += symbolInstance.iconQuadsSize * 4;
                iconBuffer.placedSymbols[*symbolInstance.placedIconIndex].hidden = opacityState.isHidden();
            }

            if (symbolInstance.placedVerticalIconIndex) {
                iconOpacityVerticesSize += symbolInstance.iconQuadsSize * 4;
                iconBuffer.placedSymbols[*symbolInstance.placedVerticalIconIndex].hidden = opacityState.isHidden();
            }

            iconBuffer.opacityVertices.extend(iconOpacityVerticesSize, opacityVertex);
        }

        auto updateIconCollisionBox = [&](const auto& feature, const bool placed, const Point<float>& shift) {
            if (feature.alongLine) {
                return;
            }
            const auto& dynamicVertex = CollisionBoxProgram::dynamicVertex(placed, false, shift);
            bucket.iconCollisionBox->dynamicVertices.extend(feature.boxes.size() * 4, dynamicVertex);
        };

        auto updateTextCollisionBox =
            [this, &bucket, &symbolInstance, &state, variablePlacement, rotateWithMap, pitchWithMap](
                const auto& feature, const bool placed) {
                Point<float> shift{0.0f, 0.0f};
                if (feature.alongLine) {
                    return shift;
                }
                bool used = true;
                if (variablePlacement) {
                    auto foundOffset = variableOffsets.find(symbolInstance.crossTileID);
                    if (foundOffset != variableOffsets.end()) {
                        const VariableOffset& variableOffset = foundOffset->second;
                        // This will show either the currently placed position or the last
                        // successfully placed position (so you can visualize what collision
                        // just made the symbol disappear, and the most likely place for the
                        // symbol to come back)
                        shift = calculateVariableLayoutOffset(variableOffset.anchor,
                                                              variableOffset.width,
                                                              variableOffset.height,
                                                              variableOffset.offset,
                                                              variableOffset.textBoxScale,
                                                              rotateWithMap,
                                                              pitchWithMap,
                                                              state.getBearing());
                    } else {
                        // No offset -> this symbol hasn't been placed since coming on-screen
                        // No single box is particularly meaningful and all of them would be too noisy
                        // Use the center box just to show something's there, but mark it "not used"
                        used = false;
                    }
                }
                const auto& dynamicVertex = CollisionBoxProgram::dynamicVertex(placed, !used, shift);
                bucket.textCollisionBox->dynamicVertices.extend(feature.boxes.size() * 4, dynamicVertex);
                return shift;
            };

        auto updateCollisionCircles = [&](const auto& feature, const bool placed, bool isText) {
            if (!feature.alongLine) {
                return;
            }
            auto circles = collisionCircles.find(&feature);
            if (circles != collisionCircles.end()) {
                for (const auto& circle : circles->second) {
                    const auto& dynamicVertex = CollisionBoxProgram::dynamicVertex(placed, !circle.isCircle(), {});
                    isText ? bucket.textCollisionCircle->dynamicVertices.extend(4, dynamicVertex):
                             bucket.iconCollisionCircle->dynamicVertices.extend(4, dynamicVertex);
                }
            } else {
                // This feature was not placed, because it was not loaded or from a fading tile. Apply default values.
                static const auto dynamicVertex = CollisionBoxProgram::dynamicVertex(placed, false /*not used*/, {});
                isText ? bucket.textCollisionCircle->dynamicVertices.extend(4 * feature.boxes.size(), dynamicVertex):
                         bucket.iconCollisionCircle->dynamicVertices.extend(4 * feature.boxes.size(), dynamicVertex);
            }
        };
        Point<float> textShift{0.0f, 0.0f};
        Point<float> verticalTextShift{0.0f, 0.0f};
        if (bucket.hasTextCollisionBoxData()) {
            textShift = updateTextCollisionBox(symbolInstance.textCollisionFeature, opacityState.text.placed);
            if (bucket.allowVerticalPlacement && symbolInstance.verticalTextCollisionFeature) {
                verticalTextShift = updateTextCollisionBox(*symbolInstance.verticalTextCollisionFeature, opacityState.text.placed);
            }
        }
        if (bucket.hasIconCollisionBoxData()) {
            updateIconCollisionBox(symbolInstance.iconCollisionFeature, opacityState.icon.placed, hasIconTextFit ? textShift : Point<float>{0.0f, 0.0f});
            if (bucket.allowVerticalPlacement && symbolInstance.verticalIconCollisionFeature) {
                updateIconCollisionBox(*symbolInstance.verticalIconCollisionFeature, opacityState.text.placed, hasIconTextFit ? verticalTextShift : Point<float>{0.0f, 0.0f});
            }
        }

        if (bucket.hasIconCollisionCircleData()) {
            updateCollisionCircles(symbolInstance.iconCollisionFeature, opacityState.icon.placed, false);
        }
        if (bucket.hasTextCollisionCircleData()) {
            updateCollisionCircles(symbolInstance.textCollisionFeature, opacityState.text.placed, true);
        }
    }

    bucket.sortFeatures(state.getBearing());
    auto retainedData = retainedQueryData.find(bucket.bucketInstanceId);
    if (retainedData != retainedQueryData.end()) {
        retainedData->second.featureSortOrder = bucket.featureSortOrder;
    }
}

namespace {
optional<size_t> justificationToIndex(style::TextJustifyType justify, const SymbolInstance& symbolInstance, style::TextWritingModeType orientation) {
    // Vertical symbol has just one justification, style::TextJustifyType::Left.
    if (orientation == style::TextWritingModeType::Vertical) {
        return symbolInstance.placedVerticalTextIndex;
    }

    switch(justify) {
        case style::TextJustifyType::Right: return symbolInstance.placedRightTextIndex;
        case style::TextJustifyType::Center: return symbolInstance.placedCenterTextIndex;
        case style::TextJustifyType::Left: return symbolInstance.placedLeftTextIndex;
        case style::TextJustifyType::Auto: break;
    }
    assert(false);
    return nullopt;
}

const style::TextJustifyType justifyTypes[] = {
    style::TextJustifyType::Right, style::TextJustifyType::Center, style::TextJustifyType::Left};

}  // namespace

void Placement::markUsedJustification(SymbolBucket& bucket,
                                      style::TextVariableAnchorType placedAnchor,
                                      const SymbolInstance& symbolInstance,
                                      style::TextWritingModeType orientation) const {
    style::TextJustifyType anchorJustify = getAnchorJustification(placedAnchor);
    assert(anchorJustify != style::TextJustifyType::Auto);
    const optional<size_t>& autoIndex = justificationToIndex(anchorJustify, symbolInstance, orientation);

    for (auto& justify : justifyTypes) {
        const optional<size_t> index = justificationToIndex(justify, symbolInstance, orientation);
        if (index) {
            assert(bucket.text.placedSymbols.size() > *index);
            if (autoIndex && *index != *autoIndex) {
                // There are multiple justifications and this one isn't it: shift offscreen
                bucket.text.placedSymbols.at(*index).crossTileID = 0u;
            } else {
                // Either this is the chosen justification or the justification is hardwired: use this one
                bucket.text.placedSymbols.at(*index).crossTileID = symbolInstance.crossTileID;
            }
        }
    }
}

void Placement::markUsedOrientation(SymbolBucket& bucket,
                                    style::TextWritingModeType orientation,
                                    const SymbolInstance& symbolInstance) const {
    auto horizontal = orientation == style::TextWritingModeType::Horizontal ?
                                     optional<style::TextWritingModeType>(orientation) : nullopt;
    auto vertical = orientation == style::TextWritingModeType::Vertical ?
                                     optional<style::TextWritingModeType>(orientation) : nullopt;

    if (symbolInstance.placedRightTextIndex) {
        bucket.text.placedSymbols.at(*symbolInstance.placedRightTextIndex).placedOrientation = horizontal;
    }

    if (symbolInstance.placedCenterTextIndex && !symbolInstance.singleLine) {
        bucket.text.placedSymbols.at(*symbolInstance.placedCenterTextIndex).placedOrientation = horizontal;
    }

    if (symbolInstance.placedLeftTextIndex && !symbolInstance.singleLine) {
        bucket.text.placedSymbols.at(*symbolInstance.placedLeftTextIndex).placedOrientation = horizontal;
    }

    if (symbolInstance.placedVerticalTextIndex) {
        bucket.text.placedSymbols.at(*symbolInstance.placedVerticalTextIndex).placedOrientation = vertical;
    }

    auto& iconBuffer = symbolInstance.hasSdfIcon() ? bucket.sdfIcon : bucket.icon;
    if (symbolInstance.placedIconIndex) {
        iconBuffer.placedSymbols.at(*symbolInstance.placedIconIndex).placedOrientation = horizontal;
    }

    if (symbolInstance.placedVerticalIconIndex) {
        iconBuffer.placedSymbols.at(*symbolInstance.placedVerticalIconIndex).placedOrientation = vertical;
    }
}

float Placement::symbolFadeChange(TimePoint now) const {
    if (transitionsEnabled() &&
        transitionOptions.duration.value_or(util::DEFAULT_TRANSITION_DURATION) > Milliseconds(0)) {
        return std::chrono::duration<float>(now - commitTime) / transitionOptions.duration.value_or(util::DEFAULT_TRANSITION_DURATION) + prevZoomAdjustment;
    }
    return 1.0;
}

float Placement::zoomAdjustment(const float zoom) const {
    // When zooming out labels can overlap each other quickly. This
    // adjustment is used to reduce the fade duration for symbols while zooming out quickly.
    // It is also used to reduce the interval between placement calculations. Reducing the
    // interval between placements means collisions are discovered and eliminated sooner.
    return std::max(0.0, (placementZoom - zoom) / 1.5);
}

Duration Placement::getUpdatePeriod(const float zoom) const {
    // Even if transitionOptions.duration is set to a value < 300ms, we still wait for this default transition duration
    // before attempting another placement operation.
    const auto fadeDuration = std::max(util::DEFAULT_TRANSITION_DURATION,
                                       transitionOptions.duration.value_or(util::DEFAULT_TRANSITION_DURATION));
    return std::chrono::duration_cast<Duration>(fadeDuration * (1.0 - zoomAdjustment(zoom)));
}

bool Placement::transitionsEnabled() const {
    return mapMode == MapMode::Continuous && transitionOptions.enablePlacementTransitions;
}

bool Placement::hasTransitions(TimePoint now) const {
    assert(transitionsEnabled());
    return std::chrono::duration<float>(now - fadeStartTime) <
           transitionOptions.duration.value_or(util::DEFAULT_TRANSITION_DURATION);
}

const CollisionIndex& Placement::getCollisionIndex() const {
    return collisionIndex;
}
    
const RetainedQueryData& Placement::getQueryData(uint32_t bucketInstanceId) const {
    auto it = retainedQueryData.find(bucketInstanceId);
    if (it == retainedQueryData.end()) {
        throw std::runtime_error("Placement::getQueryData with unrecognized bucketInstanceId");
    }
    return it->second;
}

} // namespace mbgl