FontTestUtils.cpp 3.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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.
 */

17
#define LOG_TAG "Minikin"
18

19 20
#include <libxml/tree.h>
#include <unistd.h>
21

22
#include <android/log.h>
23 24

#include "FontLanguage.h"
25
#include "MinikinFontForTest.h"
26 27
#include <minikin/FontCollection.h>
#include <minikin/FontFamily.h>
28

S
Seigo Nonaka 已提交
29 30 31
namespace minikin {

FontCollection* getFontCollection(const char* fontDir, const char* fontXml) {
32
    xmlDoc* doc = xmlReadFile(fontXml, NULL, 0);
33 34
    xmlNode* familySet = xmlDocGetRootElement(doc);

S
Seigo Nonaka 已提交
35
    std::vector<FontFamily*> families;
36 37 38 39 40 41
    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");
S
Seigo Nonaka 已提交
42
        int variant = VARIANT_DEFAULT;
43 44
        if (variantXmlch) {
            if (xmlStrcmp(variantXmlch, (const xmlChar*)"elegant") == 0) {
S
Seigo Nonaka 已提交
45
                variant = VARIANT_ELEGANT;
46
            } else if (xmlStrcmp(variantXmlch, (const xmlChar*)"compact") == 0) {
S
Seigo Nonaka 已提交
47
                variant = VARIANT_COMPACT;
48 49 50 51
            }
        }

        xmlChar* lang = xmlGetProp(familyNode, (const xmlChar*)"lang");
S
Seigo Nonaka 已提交
52 53 54 55 56 57 58 59
        FontFamily* family;
        if (lang == nullptr) {
            family = new FontFamily(variant);
        } else {
            uint32_t langId = FontStyle::registerLanguageList(
                    std::string((const char*)lang, xmlStrlen(lang)));
            family = new FontFamily(langId, variant);
        }
60 61 62 63 64 65 66 67 68

        for (xmlNode* fontNode = familyNode->children; fontNode; fontNode = fontNode->next) {
            if (xmlStrcmp(fontNode->name, (const xmlChar*)"font") != 0) {
                continue;
            }

            int weight = atoi((const char*)(xmlGetProp(fontNode, (const xmlChar*)"weight"))) / 100;
            bool italic = xmlStrcmp(
                    xmlGetProp(fontNode, (const xmlChar*)"style"), (const xmlChar*)"italic") == 0;
S
Seigo Nonaka 已提交
69
            xmlChar* index = xmlGetProp(familyNode, (const xmlChar*)"index");
70 71

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

S
Seigo Nonaka 已提交
75 76 77 78
            if (access(fontPath.c_str(), R_OK) != 0) {
                ALOGW("%s is not found.", fontPath.c_str());
                continue;
            }
S
Seigo Nonaka 已提交
79

S
Seigo Nonaka 已提交
80 81
            if (index == nullptr) {
                MinikinAutoUnref<MinikinFontForTest>
82
                        minikinFont(new MinikinFontForTest(fontPath));
S
Seigo Nonaka 已提交
83 84 85
                family->addFont(minikinFont.get(), FontStyle(weight, italic));
            } else {
                MinikinAutoUnref<MinikinFontForTest>
86
                        minikinFont(new MinikinFontForTest(fontPath, atoi((const char*)index)));
S
Seigo Nonaka 已提交
87 88
                family->addFont(minikinFont.get(), FontStyle(weight, italic));
            }
89 90 91 92 93
        }
        families.push_back(family);
    }
    xmlFreeDoc(doc);

S
Seigo Nonaka 已提交
94
    FontCollection* collection = new FontCollection(families);
95 96 97
    for (size_t i = 0; i < families.size(); ++i) {
        families[i]->Unref();
    }
98
    return collection;
99
}
S
Seigo Nonaka 已提交
100 101

}  // namespace minikin