FontTestUtils.cpp revision 5e995fb850c2b32631914c3815dfb421855fba9b
1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <libxml/tree.h>
18
19#include <minikin/FontCollection.h>
20#include <minikin/FontFamily.h>
21
22#include <cutils/log.h>
23
24#include "FontLanguage.h"
25#include "MinikinFontForTest.h"
26
27std::unique_ptr<android::FontCollection> getFontCollection(
28         const char* fontDir, const char* fontXml) {
29    xmlDoc* doc = xmlReadFile(fontXml, NULL, 0);
30    xmlNode* familySet = xmlDocGetRootElement(doc);
31
32    std::vector<android::FontFamily*> families;
33    for (xmlNode* familyNode = familySet->children; familyNode; familyNode = familyNode->next) {
34        if (xmlStrcmp(familyNode->name, (const xmlChar*)"family") != 0) {
35            continue;
36        }
37
38        xmlChar* variantXmlch = xmlGetProp(familyNode, (const xmlChar*)"variant");
39        int variant = android::VARIANT_DEFAULT;
40        if (variantXmlch) {
41            if (xmlStrcmp(variantXmlch, (const xmlChar*)"elegant") == 0) {
42                variant = android::VARIANT_ELEGANT;
43            } else if (xmlStrcmp(variantXmlch, (const xmlChar*)"compact") == 0) {
44                variant = android::VARIANT_COMPACT;
45            }
46        }
47
48        xmlChar* lang = xmlGetProp(familyNode, (const xmlChar*)"lang");
49        uint32_t langId = android::FontStyle::registerLanguageList(
50                std::string((const char*)lang, xmlStrlen(lang)));
51
52        android::FontFamily* family = new android::FontFamily(langId, variant);
53
54        for (xmlNode* fontNode = familyNode->children; fontNode; fontNode = fontNode->next) {
55            if (xmlStrcmp(fontNode->name, (const xmlChar*)"font") != 0) {
56                continue;
57            }
58
59            int weight = atoi((const char*)(xmlGetProp(fontNode, (const xmlChar*)"weight"))) / 100;
60            bool italic = xmlStrcmp(
61                    xmlGetProp(fontNode, (const xmlChar*)"style"), (const xmlChar*)"italic") == 0;
62
63            xmlChar* fontFileName = xmlNodeListGetString(doc, fontNode->xmlChildrenNode, 1);
64            std::string fontPath = fontDir + std::string((const char*)fontFileName);
65            xmlFree(fontFileName);
66
67            LOG_ALWAYS_FATAL_IF(access(fontPath.c_str(), R_OK) != 0,
68                    "%s is not found", fontPath.c_str());
69
70            family->addFont(new MinikinFontForTest(fontPath), android::FontStyle(weight, italic));
71        }
72        families.push_back(family);
73    }
74    xmlFreeDoc(doc);
75
76    std::unique_ptr<android::FontCollection> r(new android::FontCollection(families));
77    for (size_t i = 0; i < families.size(); ++i) {
78        families[i]->Unref();
79    }
80    return r;
81}
82