mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
This is 2nd attempt of 41e02e96131c1ec66d013e4615348be013518dc4 The difference is adding clearElementsEithLock to Font class which is necessary to delete Fonts object outside of minikin. This method should be removed once http://b/28119474 is fixed. Here is original commit message of reverted change. This lays the groundwork for making SparseBitSet serializable. FontFamily.addFont is only used when the FontFamily is constructed. Thus, instead of calling FontFamily.addFont multiple time, passes Font list to the constructor. By this change, FontFamily can be immutable now. By making FontFamily immutable, We can create FontFamily with pre-calculated SparseBitSet. Bug: 34042446 Bug: 28119474 Bug: 34378805 Test: minikin_tests has passed Change-Id: Ice433931196f5ae79a1a7ee0c98020f914aeb5f2
103 lines
3.8 KiB
C++
103 lines
3.8 KiB
C++
/*
|
|
* 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.
|
|
*/
|
|
|
|
#define LOG_TAG "Minikin"
|
|
|
|
#include <libxml/tree.h>
|
|
#include <unistd.h>
|
|
|
|
#include <log/log.h>
|
|
|
|
#include "FontLanguage.h"
|
|
#include "MinikinFontForTest.h"
|
|
#include <minikin/FontCollection.h>
|
|
#include <minikin/FontFamily.h>
|
|
|
|
namespace minikin {
|
|
|
|
FontCollection* getFontCollection(const char* fontDir, const char* fontXml) {
|
|
xmlDoc* doc = xmlReadFile(fontXml, NULL, 0);
|
|
xmlNode* familySet = xmlDocGetRootElement(doc);
|
|
|
|
std::vector<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 = VARIANT_DEFAULT;
|
|
if (variantXmlch) {
|
|
if (xmlStrcmp(variantXmlch, (const xmlChar*)"elegant") == 0) {
|
|
variant = VARIANT_ELEGANT;
|
|
} else if (xmlStrcmp(variantXmlch, (const xmlChar*)"compact") == 0) {
|
|
variant = VARIANT_COMPACT;
|
|
}
|
|
}
|
|
|
|
std::vector<Font> fonts;
|
|
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* index = xmlGetProp(familyNode, (const xmlChar*)"index");
|
|
|
|
xmlChar* fontFileName = xmlNodeListGetString(doc, fontNode->xmlChildrenNode, 1);
|
|
std::string fontPath = fontDir + std::string((const char*)fontFileName);
|
|
xmlFree(fontFileName);
|
|
|
|
if (access(fontPath.c_str(), R_OK) != 0) {
|
|
ALOGW("%s is not found.", fontPath.c_str());
|
|
continue;
|
|
}
|
|
|
|
if (index == nullptr) {
|
|
MinikinAutoUnref<MinikinFontForTest>
|
|
minikinFont(new MinikinFontForTest(fontPath));
|
|
fonts.push_back(Font(minikinFont.get(), FontStyle(weight, italic)));
|
|
} else {
|
|
MinikinAutoUnref<MinikinFontForTest>
|
|
minikinFont(new MinikinFontForTest(fontPath, atoi((const char*)index)));
|
|
fonts.push_back(Font(minikinFont.get(), FontStyle(weight, italic)));
|
|
}
|
|
}
|
|
|
|
xmlChar* lang = xmlGetProp(familyNode, (const xmlChar*)"lang");
|
|
FontFamily* family;
|
|
if (lang == nullptr) {
|
|
family = new FontFamily(variant, std::move(fonts));
|
|
} else {
|
|
uint32_t langId = FontStyle::registerLanguageList(
|
|
std::string((const char*)lang, xmlStrlen(lang)));
|
|
family = new FontFamily(langId, variant, std::move(fonts));
|
|
}
|
|
families.push_back(family);
|
|
}
|
|
xmlFreeDoc(doc);
|
|
|
|
FontCollection* collection = new FontCollection(families);
|
|
for (size_t i = 0; i < families.size(); ++i) {
|
|
families[i]->Unref();
|
|
}
|
|
return collection;
|
|
}
|
|
|
|
} // namespace minikin
|