• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define LOG_TAG "Minikin"
18 
19 #include <libxml/tree.h>
20 #include <unistd.h>
21 
22 #include <log/log.h>
23 
24 #include "FontLanguage.h"
25 #include "MinikinFontForTest.h"
26 #include <minikin/FontCollection.h>
27 #include <minikin/FontFamily.h>
28 
29 namespace minikin {
30 
getFontFamilies(const char * fontDir,const char * fontXml)31 std::vector<std::shared_ptr<FontFamily>> getFontFamilies(const char* fontDir, const char* fontXml) {
32     xmlDoc* doc = xmlReadFile(fontXml, NULL, 0);
33     xmlNode* familySet = xmlDocGetRootElement(doc);
34 
35     std::vector<std::shared_ptr<FontFamily>> families;
36     for (xmlNode* familyNode = familySet->children; familyNode; familyNode = familyNode->next) {
37         if (xmlStrcmp(familyNode->name, (const xmlChar*)"family") != 0) {
38             continue;
39         }
40 
41         xmlChar* variantXmlch = xmlGetProp(familyNode, (const xmlChar*)"variant");
42         int variant = VARIANT_DEFAULT;
43         if (variantXmlch) {
44             if (xmlStrcmp(variantXmlch, (const xmlChar*)"elegant") == 0) {
45                 variant = VARIANT_ELEGANT;
46             } else if (xmlStrcmp(variantXmlch, (const xmlChar*)"compact") == 0) {
47                 variant = VARIANT_COMPACT;
48             }
49         }
50 
51         std::vector<Font> fonts;
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             xmlChar* index = xmlGetProp(familyNode, (const xmlChar*)"index");
61 
62             xmlChar* fontFileName = xmlNodeListGetString(doc, fontNode->xmlChildrenNode, 1);
63             std::string fontPath = fontDir + std::string((const char*)fontFileName);
64             xmlFree(fontFileName);
65 
66             if (access(fontPath.c_str(), R_OK) != 0) {
67                 ALOGW("%s is not found.", fontPath.c_str());
68                 continue;
69             }
70 
71             if (index == nullptr) {
72                 std::shared_ptr<MinikinFont> minikinFont =
73                         std::make_shared<MinikinFontForTest>(fontPath);
74                 fonts.push_back(Font(minikinFont, FontStyle(weight, italic)));
75             } else {
76                 std::shared_ptr<MinikinFont> minikinFont =
77                         std::make_shared<MinikinFontForTest>(fontPath, atoi((const char*)index));
78                 fonts.push_back(Font(minikinFont, FontStyle(weight, italic)));
79             }
80         }
81 
82         xmlChar* lang = xmlGetProp(familyNode, (const xmlChar*)"lang");
83         std::shared_ptr<FontFamily> family;
84         if (lang == nullptr) {
85             family = std::make_shared<FontFamily>(variant, std::move(fonts));
86         } else {
87             uint32_t langId = FontStyle::registerLanguageList(
88                     std::string((const char*)lang, xmlStrlen(lang)));
89             family = std::make_shared<FontFamily>(langId, variant, std::move(fonts));
90         }
91         families.push_back(family);
92     }
93     xmlFreeDoc(doc);
94     return families;
95 }
getFontCollection(const char * fontDir,const char * fontXml)96 std::shared_ptr<FontCollection> getFontCollection(const char* fontDir, const char* fontXml) {
97     return std::make_shared<FontCollection>(getFontFamilies(fontDir, fontXml));
98 }
99 
100 }  // namespace minikin
101