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 <log/log.h> 21 #include <unistd.h> 22 23 #include "minikin/FontCollection.h" 24 #include "minikin/FontFamily.h" 25 #include "minikin/LocaleList.h" 26 27 #include "FontTestUtils.h" 28 #include "FreeTypeMinikinFontForTest.h" 29 #include "LocaleListCache.h" 30 #include "MinikinInternal.h" 31 32 namespace minikin { 33 34 namespace { 35 std::string xmlTrim(const std::string& in) { 36 if (in.empty()) { 37 return in; 38 } 39 const char XML_SPACES[] = "\u0020\u000D\u000A\u0009"; 40 const size_t start = in.find_first_not_of(XML_SPACES); // inclusive 41 const size_t end = in.find_last_not_of(XML_SPACES); // inclusive 42 MINIKIN_ASSERT(start != std::string::npos, "Not a valid file name \"%s\"", in.c_str()); 43 MINIKIN_ASSERT(end != std::string::npos, "Not a valid file name \"%s\"", in.c_str()); 44 return in.substr(start, end - start + 1 /* +1 since end is inclusive */); 45 } 46 47 } // namespace 48 49 std::vector<std::shared_ptr<FontFamily>> getFontFamilies(const std::string& fontDir, 50 const std::string& xmlPath) { 51 xmlDoc* doc = xmlReadFile(xmlPath.c_str(), NULL, 0); 52 xmlNode* familySet = xmlDocGetRootElement(doc); 53 54 std::vector<std::shared_ptr<FontFamily>> families; 55 for (xmlNode* familyNode = familySet->children; familyNode; familyNode = familyNode->next) { 56 if (xmlStrcmp(familyNode->name, (const xmlChar*)"family") != 0) { 57 continue; 58 } 59 60 xmlChar* variantXmlch = xmlGetProp(familyNode, (const xmlChar*)"variant"); 61 FontFamily::Variant variant = FontFamily::Variant::DEFAULT; 62 if (variantXmlch) { 63 if (xmlStrcmp(variantXmlch, (const xmlChar*)"elegant") == 0) { 64 variant = FontFamily::Variant::ELEGANT; 65 } else if (xmlStrcmp(variantXmlch, (const xmlChar*)"compact") == 0) { 66 variant = FontFamily::Variant::COMPACT; 67 } 68 } 69 70 std::vector<Font> fonts; 71 for (xmlNode* fontNode = familyNode->children; fontNode; fontNode = fontNode->next) { 72 if (xmlStrcmp(fontNode->name, (const xmlChar*)"font") != 0) { 73 continue; 74 } 75 76 uint16_t weight = atoi((const char*)(xmlGetProp(fontNode, (const xmlChar*)"weight"))); 77 FontStyle::Slant italic = static_cast<FontStyle::Slant>( 78 xmlStrcmp(xmlGetProp(fontNode, (const xmlChar*)"style"), 79 (const xmlChar*)"italic") == 0); 80 xmlChar* index = xmlGetProp(familyNode, (const xmlChar*)"index"); 81 82 xmlChar* fontFileName = xmlNodeListGetString(doc, fontNode->xmlChildrenNode, 1); 83 const std::string fontPath = xmlTrim(fontDir + std::string((const char*)fontFileName)); 84 xmlFree(fontFileName); 85 86 // TODO: Support font variation axis. 87 88 if (access(fontPath.c_str(), R_OK) != 0) { 89 ALOGW("%s is not found.", fontPath.c_str()); 90 continue; 91 } 92 93 FontStyle style(weight, italic); 94 if (index == nullptr) { 95 std::shared_ptr<MinikinFont> minikinFont = 96 std::make_shared<FreeTypeMinikinFontForTest>(fontPath); 97 fonts.push_back(Font::Builder(minikinFont).setStyle(style).build()); 98 } else { 99 std::shared_ptr<MinikinFont> minikinFont = 100 std::make_shared<FreeTypeMinikinFontForTest>(fontPath, 101 atoi((const char*)index)); 102 fonts.push_back(Font::Builder(minikinFont).setStyle(style).build()); 103 } 104 } 105 106 xmlChar* lang = xmlGetProp(familyNode, (const xmlChar*)"lang"); 107 std::shared_ptr<FontFamily> family; 108 if (lang == nullptr) { 109 family = std::make_shared<FontFamily>(variant, std::move(fonts)); 110 } else { 111 uint32_t langId = registerLocaleList(std::string((const char*)lang, xmlStrlen(lang))); 112 family = std::make_shared<FontFamily>(langId, variant, std::move(fonts)); 113 } 114 families.push_back(family); 115 } 116 xmlFreeDoc(doc); 117 return families; 118 } 119 120 std::shared_ptr<FontCollection> buildFontCollection(const std::string& filePath) { 121 return std::make_shared<FontCollection>(buildFontFamily(filePath)); 122 } 123 124 std::shared_ptr<FontFamily> buildFontFamily(const std::string& filePath) { 125 auto font = std::make_shared<FreeTypeMinikinFontForTest>(getTestFontPath(filePath)); 126 std::vector<Font> fonts; 127 fonts.push_back(Font::Builder(font).build()); 128 return std::make_shared<FontFamily>(std::move(fonts)); 129 } 130 131 std::shared_ptr<FontFamily> buildFontFamily(const std::string& filePath, const std::string& lang) { 132 auto font = std::make_shared<FreeTypeMinikinFontForTest>(getTestFontPath(filePath)); 133 std::vector<Font> fonts; 134 fonts.push_back(Font::Builder(font).build()); 135 return std::make_shared<FontFamily>(LocaleListCache::getId(lang), FontFamily::Variant::DEFAULT, 136 std::move(fonts)); 137 } 138 139 } // namespace minikin 140