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

#include <boost/algorithm/string.hpp>

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

void TaggedString::trim() {
    auto whiteSpace = boost::algorithm::is_any_of(u" \t\n\v\f\r");
    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<uint8_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 = std::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);
}

} // namespace mbgl