summaryrefslogtreecommitdiff
path: root/tests/util/FontTestUtils.cpp
blob: 0d563b8f4ab4d209d178ef39880e9676f8e0fe27 (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
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "Minikin"

#include "FontTestUtils.h"

#include <libxml/parser.h>
#include <log/log.h>
#include <unistd.h>

#include "FreeTypeMinikinFontForTest.h"
#include "LocaleListCache.h"
#include "MinikinInternal.h"
#include "minikin/FontCollection.h"
#include "minikin/FontFamily.h"
#include "minikin/LocaleList.h"

namespace minikin {

namespace {
std::string xmlTrim(const std::string& in) {
    if (in.empty()) {
        return in;
    }
    const char XML_SPACES[] = "\u0020\u000D\u000A\u0009";
    const size_t start = in.find_first_not_of(XML_SPACES);  // inclusive
    const size_t end = in.find_last_not_of(XML_SPACES);     // inclusive
    MINIKIN_ASSERT(start != std::string::npos, "Not a valid file name \"%s\"", in.c_str());
    MINIKIN_ASSERT(end != std::string::npos, "Not a valid file name \"%s\"", in.c_str());
    return in.substr(start, end - start + 1 /* +1 since end is inclusive */);
}

}  // namespace

std::vector<std::shared_ptr<FontFamily>> getFontFamilies(const std::string& fontDir,
                                                         const std::string& xmlPath) {
    xmlDoc* doc = xmlReadFile(xmlPath.c_str(), NULL, 0);
    xmlNode* familySet = xmlDocGetRootElement(doc);

    std::vector<std::shared_ptr<FontFamily>> families;
    for (xmlNode* familyNode = familySet->children; familyNode; familyNode = familyNode->next) {
        if (xmlStrcmp(familyNode->name, (const xmlChar*)"family") != 0) {
            continue;
        }

        xmlChar* variantXmlch = xmlGetProp(familyNode, (const xmlChar*)"variant");
        FamilyVariant variant = FamilyVariant::DEFAULT;
        if (variantXmlch) {
            if (xmlStrcmp(variantXmlch, (const xmlChar*)"elegant") == 0) {
                variant = FamilyVariant::ELEGANT;
            } else if (xmlStrcmp(variantXmlch, (const xmlChar*)"compact") == 0) {
                variant = FamilyVariant::COMPACT;
            }
        }

        std::vector<std::shared_ptr<Font>> fonts;
        for (xmlNode* fontNode = familyNode->children; fontNode; fontNode = fontNode->next) {
            if (xmlStrcmp(fontNode->name, (const xmlChar*)"font") != 0) {
                continue;
            }

            uint16_t weight = atoi((const char*)(xmlGetProp(fontNode, (const xmlChar*)"weight")));
            FontStyle::Slant italic = static_cast<FontStyle::Slant>(
                    xmlStrcmp(xmlGetProp(fontNode, (const xmlChar*)"style"),
                              (const xmlChar*)"italic") == 0);
            xmlChar* index = xmlGetProp(familyNode, (const xmlChar*)"index");

            xmlChar* fontFileName = xmlNodeListGetString(doc, fontNode->xmlChildrenNode, 1);
            const std::string fontPath = xmlTrim(fontDir + std::string((const char*)fontFileName));
            xmlFree(fontFileName);

            // TODO: Support font variation axis.

            if (access(fontPath.c_str(), R_OK) != 0) {
                ALOGW("%s is not found.", fontPath.c_str());
                continue;
            }

            FontStyle style(weight, italic);
            if (index == nullptr) {
                std::shared_ptr<MinikinFont> minikinFont =
                        std::make_shared<FreeTypeMinikinFontForTest>(fontPath);
                fonts.push_back(Font::Builder(minikinFont).setStyle(style).build());
            } else {
                std::shared_ptr<MinikinFont> minikinFont =
                        std::make_shared<FreeTypeMinikinFontForTest>(fontPath,
                                                                     atoi((const char*)index));
                fonts.push_back(Font::Builder(minikinFont).setStyle(style).build());
            }
        }

        xmlChar* lang = xmlGetProp(familyNode, (const xmlChar*)"lang");
        std::shared_ptr<FontFamily> family;
        if (lang == nullptr) {
            family = FontFamily::create(variant, std::move(fonts));
        } else {
            uint32_t langId = registerLocaleList(std::string((const char*)lang, xmlStrlen(lang)));
            family = FontFamily::create(langId, variant, std::move(fonts),
                                        false /* isCustomFallback */, false /* isdefaultFallback */,
                                        VariationFamilyType::None);
        }
        families.push_back(family);
    }
    xmlFreeDoc(doc);
    return families;
}

std::shared_ptr<FontCollection> buildFontCollection(const std::string& filePath) {
    return FontCollection::create(buildFontFamily(filePath));
}

std::shared_ptr<FontFamily> buildFontFamily(const std::string& filePath) {
    auto font = std::make_shared<FreeTypeMinikinFontForTest>(getTestFontPath(filePath));
    std::vector<std::shared_ptr<Font>> fonts;
    fonts.push_back(Font::Builder(font).build());
    return FontFamily::create(std::move(fonts));
}

std::shared_ptr<FontFamily> buildFontFamily(const std::string& filePath, const std::string& lang,
                                            bool isCustomFallback) {
    auto font = std::make_shared<FreeTypeMinikinFontForTest>(getTestFontPath(filePath));
    std::vector<std::shared_ptr<Font>> fonts;
    fonts.push_back(Font::Builder(font).build());
    return FontFamily::create(LocaleListCache::getId(lang), FamilyVariant::DEFAULT,
                              std::move(fonts), isCustomFallback, false /* isDefaultFallback */,
                              VariationFamilyType::None);
}

}  // namespace minikin