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