FontTestUtils.cpp revision 1c2bd209d11e59ea3a31d49ec4e97725fd711bea
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 "MinikinFontForTest.h"
23
24const char kFontDir[] = "/system/fonts/";
25const char kFontXml[] = "/system/etc/fonts.xml";
26
27std::unique_ptr<android::FontCollection> getFontCollection() {
28    xmlDoc* doc = xmlReadFile(kFontXml, 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
49        android::FontFamily* family = new android::FontFamily(
50                android::FontLanguage((const char*)lang, xmlStrlen(lang)), variant);
51
52        for (xmlNode* fontNode = familyNode->children; fontNode; fontNode = fontNode->next) {
53            if (xmlStrcmp(fontNode->name, (const xmlChar*)"font") != 0) {
54                continue;
55            }
56
57            int weight = atoi((const char*)(xmlGetProp(fontNode, (const xmlChar*)"weight"))) / 100;
58            bool italic = xmlStrcmp(
59                    xmlGetProp(fontNode, (const xmlChar*)"style"), (const xmlChar*)"italic") == 0;
60
61            xmlChar* fontFileName = xmlNodeListGetString(doc, fontNode->xmlChildrenNode, 1);
62            std::string fontPath = kFontDir + std::string((const char*)fontFileName);
63            xmlFree(fontFileName);
64
65            if (access(fontPath.c_str(), R_OK) != 0) {
66                // Skip not accessible fonts.
67                continue;
68            }
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