summaryrefslogtreecommitdiff
path: root/src/mbgl/text/tagged_string.cpp
blob: 3755ad3a284193ea10af169a88cc1377c942f32f (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
#include <mbgl/text/tagged_string.hpp>
#include <mbgl/math/minmax.hpp>
#include <mbgl/util/i18n.hpp>

namespace mbgl {
    
void TaggedString::addSection(const std::u16string& sectionText, double scale, FontStack fontStack, const optional<FormattedSectionID>& id) {
    styledText.first += sectionText;
    sections.emplace_back(scale, fontStack, id);
    styledText.second.resize(styledText.first.size(), sections.size() - 1);
}

void TaggedString::trim() {
    std::size_t beginningWhitespace = styledText.first.find_first_not_of(u" \t\n\v\f\r");
    if (beginningWhitespace == std::u16string::npos) {
        // Entirely whitespace
        styledText.first.clear();
        styledText.second.clear();
    } else {
        std::size_t trailingWhitespace = styledText.first.find_last_not_of(u" \t\n\v\f\r") + 1;

        styledText.first = styledText.first.substr(beginningWhitespace, trailingWhitespace - beginningWhitespace);
        styledText.second = std::vector<std::size_t>(styledText.second.begin() + beginningWhitespace, styledText.second.begin() + trailingWhitespace);
    }
}

double TaggedString::getMaxScale() const {
    double maxScale = 0.0;
    for (std::size_t i = 0; i < styledText.first.length(); i++) {
        maxScale = util::max(maxScale, getSection(i).scale);
    }
    return maxScale;
}
    
void TaggedString::verticalizePunctuation() {
    // Relies on verticalization changing characters in place so that style indices don't need updating
    styledText.first = util::i18n::verticalizePunctuation(styledText.first);
}

bool TaggedString::hasMultipleUniqueSections() const noexcept {
    if (sections.size() < 2) {
        return false;
    }

    const auto& id = sections.at(0).id;
    for (std::size_t i = 1; i < sections.size(); ++i) {
        if (id != sections.at(i).id) {
            return true;
        }
    }

    return false;
}

} // namespace mbgl