FontTestUtils.cpp revision 3dd8757fcf48976295bac566277b6da1046e8362
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#include "MinikinFontForTest.h"
24
25std::unique_ptr<android::FontCollection> getFontCollection(
26         const char* fontDir, const char* fontXml) {
27    xmlDoc* doc = xmlReadFile(fontXml, NULL, 0);
28    xmlNode* familySet = xmlDocGetRootElement(doc);
29
30    std::vector<android::FontFamily*> families;
31    for (xmlNode* familyNode = familySet->children; familyNode; familyNode = familyNode->next) {
32        if (xmlStrcmp(familyNode->name, (const xmlChar*)"family") != 0) {
33            continue;
34        }
35
36        xmlChar* variantXmlch = xmlGetProp(familyNode, (const xmlChar*)"variant");
37        int variant = android::VARIANT_DEFAULT;
38        if (variantXmlch) {
39            if (xmlStrcmp(variantXmlch, (const xmlChar*)"elegant") == 0) {
40                variant = android::VARIANT_ELEGANT;
41            } else if (xmlStrcmp(variantXmlch, (const xmlChar*)"compact") == 0) {
42                variant = android::VARIANT_COMPACT;
43            }
44        }
45
46        xmlChar* lang = xmlGetProp(familyNode, (const xmlChar*)"lang");
47
48        android::FontFamily* family = new android::FontFamily(
49                android::FontLanguage((const char*)lang, xmlStrlen(lang)), variant);
50
51        for (xmlNode* fontNode = familyNode->children; fontNode; fontNode = fontNode->next) {
52            if (xmlStrcmp(fontNode->name, (const xmlChar*)"font") != 0) {
53                continue;
54            }
55
56            int weight = atoi((const char*)(xmlGetProp(fontNode, (const xmlChar*)"weight"))) / 100;
57            bool italic = xmlStrcmp(
58                    xmlGetProp(fontNode, (const xmlChar*)"style"), (const xmlChar*)"italic") == 0;
59
60            xmlChar* fontFileName = xmlNodeListGetString(doc, fontNode->xmlChildrenNode, 1);
61            std::string fontPath = fontDir + std::string((const char*)fontFileName);
62            xmlFree(fontFileName);
63
64            LOG_ALWAYS_FATAL_IF(access(fontPath.c_str(), R_OK) != 0,
65                    "%s is not found", fontPath.c_str());
66
67            family->addFont(new MinikinFontForTest(fontPath), android::FontStyle(weight, italic));
68        }
69        families.push_back(family);
70    }
71    xmlFreeDoc(doc);
72
73    std::unique_ptr<android::FontCollection> r(new android::FontCollection(families));
74    for (size_t i = 0; i < families.size(); ++i) {
75        families[i]->Unref();
76    }
77    return r;
78}
79