1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include "font_collection.h" 17 #include "texgine/font_providers.h" 18 #include "texgine/system_font_provider.h" 19 20 namespace OHOS { 21 namespace Rosen { 22 namespace TextEngine { Create()23std::shared_ptr<FontProviders> FontProviders::Create() noexcept(true) 24 { 25 return std::make_shared<FontProviders>(); 26 } 27 SystemFontOnly()28std::shared_ptr<FontProviders> FontProviders::SystemFontOnly() noexcept(true) 29 { 30 auto fps = FontProviders::Create(); 31 fps->AppendFontProvider(SystemFontProvider::GetInstance()); 32 return fps; 33 } 34 AppendFontProvider(const std::shared_ptr<IFontProvider> & provider)35void FontProviders::AppendFontProvider(const std::shared_ptr<IFontProvider> &provider) noexcept(true) 36 { 37 if (provider == nullptr) { 38 return; 39 } 40 41 for (const auto &p : providers_) { 42 if (p == provider) { 43 return; 44 } 45 } 46 47 providers_.push_back(provider); 48 } 49 DisableFallback()50void FontProviders::DisableFallback() 51 { 52 enableFallback_ = false; 53 } 54 GenerateFontCollection(const std::vector<std::string> & families) const55std::shared_ptr<FontCollection> FontProviders::GenerateFontCollection( 56 const std::vector<std::string> &families) const noexcept(true) 57 { 58 std::vector<std::shared_ptr<VariantFontStyleSet>> sets; 59 for (const auto &familyName : families) { 60 for (const auto &provider : providers_) { 61 auto it = fontStyleSetCache_.find(familyName); 62 if (it == fontStyleSetCache_.end()) { 63 auto set = provider->MatchFamily(familyName); 64 if (set != nullptr) { 65 fontStyleSetCache_[familyName] = set; 66 it = fontStyleSetCache_.find(familyName); 67 } 68 } 69 70 if (it != fontStyleSetCache_.end()) { 71 sets.push_back(it->second); 72 break; 73 } 74 } 75 } 76 77 auto collection = std::make_shared<FontCollection>(std::move(sets)); 78 if (!enableFallback_) { 79 collection->DisableFallback(); 80 } 81 return collection; 82 } 83 } // namespace TextEngine 84 } // namespace Rosen 85 } // namespace OHOS 86