1 /* 2 * Copyright (c) 2021 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 "drawing_font_collection.h" 17 18 #include "rosen_text/font_collection.h" 19 20 #ifdef USE_SKIA_TXT 21 #include "skia_txt/font_collection.h" 22 #else 23 #include "txt/font_collection.h" 24 #endif 25 26 #include "utils/object_mgr.h" 27 28 using namespace OHOS::Rosen::Drawing; 29 30 static std::shared_ptr<ObjectMgr> g_objectMgr = ObjectMgr::GetInstance(); 31 32 template<typename T1, typename T2> ConvertToFontCollection(T2 * ptr)33inline T1* ConvertToFontCollection(T2* ptr) 34 { 35 return reinterpret_cast<T1*>(ptr); 36 } 37 OH_Drawing_CreateFontCollection(void)38OH_Drawing_FontCollection* OH_Drawing_CreateFontCollection(void) 39 { 40 OH_Drawing_FontCollection* fc = 41 (OH_Drawing_FontCollection*)new (std::nothrow) OHOS::Rosen::AdapterTxt::FontCollection; 42 if (fc == nullptr) { 43 return nullptr; 44 } 45 g_objectMgr->AddObject(fc); 46 return fc; 47 } 48 OH_Drawing_CreateSharedFontCollection(void)49OH_Drawing_FontCollection* OH_Drawing_CreateSharedFontCollection(void) 50 { 51 auto fc = std::make_shared<OHOS::Rosen::AdapterTxt::FontCollection>(); 52 OH_Drawing_FontCollection* pointer = reinterpret_cast<OH_Drawing_FontCollection*>(fc.get()); 53 FontCollectionMgr::GetInstance().Insert(pointer, fc); 54 return pointer; 55 } 56 OH_Drawing_DestroyFontCollection(OH_Drawing_FontCollection * fontCollection)57void OH_Drawing_DestroyFontCollection(OH_Drawing_FontCollection* fontCollection) 58 { 59 if (!fontCollection) { 60 return; 61 } 62 63 if (FontCollectionMgr::GetInstance().Remove(fontCollection)) { 64 return; 65 } 66 if (!g_objectMgr->RemoveObject(fontCollection)) { 67 return; 68 } 69 70 delete ConvertToFontCollection<OHOS::Rosen::AdapterTxt::FontCollection>(fontCollection); 71 } 72 OH_Drawing_DisableFontCollectionFallback(OH_Drawing_FontCollection * fontCollection)73void OH_Drawing_DisableFontCollectionFallback(OH_Drawing_FontCollection* fontCollection) 74 { 75 if (!fontCollection) { 76 return; 77 } 78 ConvertToFontCollection<OHOS::Rosen::AdapterTxt::FontCollection>(fontCollection)->DisableFallback(); 79 } 80 OH_Drawing_DisableFontCollectionSystemFont(OH_Drawing_FontCollection * fontCollection)81void OH_Drawing_DisableFontCollectionSystemFont(OH_Drawing_FontCollection* fontCollection) 82 { 83 if (fontCollection == nullptr) { 84 return; 85 } 86 ConvertToFontCollection<OHOS::Rosen::AdapterTxt::FontCollection>(fontCollection)->DisableSystemFont(); 87 } 88 OH_Drawing_ClearFontCaches(OH_Drawing_FontCollection * fontCollection)89void OH_Drawing_ClearFontCaches(OH_Drawing_FontCollection* fontCollection) 90 { 91 if (!fontCollection) { 92 return; 93 } 94 95 if (FontCollectionMgr::GetInstance().Find(fontCollection)) { 96 ConvertToFontCollection<OHOS::Rosen::AdapterTxt::FontCollection>(fontCollection)->ClearCaches(); 97 return; 98 } 99 100 if (g_objectMgr->HasObject(fontCollection)) { 101 ConvertToFontCollection<OHOS::Rosen::AdapterTxt::FontCollection>(fontCollection)->ClearCaches(); 102 return; 103 } 104 return; 105 } 106 OH_Drawing_GetFontCollectionGlobalInstance(void)107OH_Drawing_FontCollection* OH_Drawing_GetFontCollectionGlobalInstance(void) 108 { 109 std::shared_ptr<OHOS::Rosen::FontCollection> fc = OHOS::Rosen::FontCollection::Create(); 110 OH_Drawing_FontCollection* pointer = reinterpret_cast<OH_Drawing_FontCollection*>(fc.get()); 111 if (!FontCollectionMgr::GetInstance().Find(pointer)) { 112 FontCollectionMgr::GetInstance().Insert(pointer, fc); 113 } 114 return pointer; 115 }