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

#include <libxml/tree.h>

#include <minikin/FontCollection.h>
#include <minikin/FontFamily.h>

22
#include <cutils/log.h>
23 24
#include "MinikinFontForTest.h"

25 26 27
std::unique_ptr<android::FontCollection> getFontCollection(
         const char* fontDir, const char* fontXml) {
    xmlDoc* doc = xmlReadFile(fontXml, NULL, 0);
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
    xmlNode* familySet = xmlDocGetRootElement(doc);

    std::vector<android::FontFamily*> families;
    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");
        int variant = android::VARIANT_DEFAULT;
        if (variantXmlch) {
            if (xmlStrcmp(variantXmlch, (const xmlChar*)"elegant") == 0) {
                variant = android::VARIANT_ELEGANT;
            } else if (xmlStrcmp(variantXmlch, (const xmlChar*)"compact") == 0) {
                variant = android::VARIANT_COMPACT;
            }
        }

        xmlChar* lang = xmlGetProp(familyNode, (const xmlChar*)"lang");

48 49
        android::FontFamily* family = new android::FontFamily(
                android::FontLanguage((const char*)lang, xmlStrlen(lang)), variant);
50 51 52 53 54 55 56 57 58 59 60

        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;

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

64 65
            LOG_ALWAYS_FATAL_IF(access(fontPath.c_str(), R_OK) != 0,
                    "%s is not found", fontPath.c_str());
66 67 68 69 70 71 72 73 74 75 76 77 78

            family->addFont(new MinikinFontForTest(fontPath), android::FontStyle(weight, italic));
        }
        families.push_back(family);
    }
    xmlFreeDoc(doc);

    std::unique_ptr<android::FontCollection> r(new android::FontCollection(families));
    for (size_t i = 0; i < families.size(); ++i) {
        families[i]->Unref();
    }
    return r;
}