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 #ifndef USE_GRAPHIC_TEXT_GINE
18 #include "rosen_text/ui/font_collection.h"
19 #else
20 #include "rosen_text/font_collection.h"
21 
22 #ifndef USE_TEXGINE
23 #ifdef USE_SKIA_TXT
24 #include "adapter/skia_txt/font_collection.h"
25 #else
26 #include "adapter/txt/font_collection.h"
27 #endif
28 #else
29 #include "texgine/font_collection.h"
30 #endif
31 
32 #endif
33 #include "utils/object_mgr.h"
34 
35 using namespace OHOS::Rosen::Drawing;
36 
37 static std::shared_ptr<ObjectMgr> g_objectMgr = ObjectMgr::GetInstance();
38 
39 template<typename T1, typename T2>
ConvertToFontCollection(T2 * ptr)40 inline T1* ConvertToFontCollection(T2* ptr)
41 {
42     return reinterpret_cast<T1*>(ptr);
43 }
44 
OH_Drawing_CreateFontCollection(void)45 OH_Drawing_FontCollection* OH_Drawing_CreateFontCollection(void)
46 {
47 #ifndef USE_GRAPHIC_TEXT_GINE
48     OH_Drawing_FontCollection* fc = (OH_Drawing_FontCollection*)new (std::nothrow) rosen::FontCollection;
49     if (fc == nullptr) {
50         return nullptr;
51     }
52 #else
53 #ifndef USE_TEXGINE
54     OH_Drawing_FontCollection* fc =
55         (OH_Drawing_FontCollection*)new (std::nothrow) OHOS::Rosen::AdapterTxt::FontCollection;
56     if (fc == nullptr) {
57         return nullptr;
58     }
59 #else
60     OH_Drawing_FontCollection* fc =
61         (OH_Drawing_FontCollection*)new (std::nothrow) OHOS::Rosen::AdapterTextEngine::FontCollection;
62     if (fc == nullptr) {
63         return nullptr;
64     }
65 #endif
66 #endif
67     g_objectMgr->AddObject(fc);
68     return fc;
69 }
70 
OH_Drawing_CreateSharedFontCollection(void)71 OH_Drawing_FontCollection* OH_Drawing_CreateSharedFontCollection(void)
72 {
73 #ifndef USE_GRAPHIC_TEXT_GINE
74     auto fc = std::make_shared<rosen::FontCollection>();
75 #else
76 #ifndef USE_TEXGINE
77     auto fc = std::make_shared<OHOS::Rosen::AdapterTxt::FontCollection>();
78 #else
79     auto fc = std::make_shared<OHOS::Rosen::AdapterTextEngine::FontCollection>();
80 #endif
81 #endif
82     OH_Drawing_FontCollection* pointer = reinterpret_cast<OH_Drawing_FontCollection*>(fc.get());
83     FontCollectionMgr::GetInstance().Insert(pointer, fc);
84     return pointer;
85 }
86 
OH_Drawing_DestroyFontCollection(OH_Drawing_FontCollection * fontCollection)87 void OH_Drawing_DestroyFontCollection(OH_Drawing_FontCollection* fontCollection)
88 {
89     if (!fontCollection) {
90         return;
91     }
92 
93     if (FontCollectionMgr::GetInstance().Remove(fontCollection)) {
94         return;
95     }
96     if (!g_objectMgr->RemoveObject(fontCollection)) {
97         return;
98     }
99 
100 #ifndef USE_GRAPHIC_TEXT_GINE
101     delete ConvertToFontCollection<rosen::FontCollection>(fontCollection);
102 #else
103 #ifndef USE_TEXGINE
104     delete ConvertToFontCollection<OHOS::Rosen::AdapterTxt::FontCollection>(fontCollection);
105 #else
106     delete ConvertToFontCollection<OHOS::Rosen::AdapterTextEngine::FontCollection>(fontCollection);
107 #endif
108 #endif
109 }
110 
OH_Drawing_DisableFontCollectionFallback(OH_Drawing_FontCollection * fontCollection)111 void OH_Drawing_DisableFontCollectionFallback(OH_Drawing_FontCollection* fontCollection)
112 {
113     if (!fontCollection) {
114         return;
115     }
116     ConvertToFontCollection<OHOS::Rosen::AdapterTxt::FontCollection>(fontCollection)->DisableFallback();
117 }
118 
OH_Drawing_DisableFontCollectionSystemFont(OH_Drawing_FontCollection * fontCollection)119 void OH_Drawing_DisableFontCollectionSystemFont(OH_Drawing_FontCollection* fontCollection)
120 {
121     if (fontCollection == nullptr) {
122         return;
123     }
124 #ifndef USE_GRAPHIC_TEXT_GINE
125     ConvertToFontCollection<rosen::FontCollection>(fontCollection)->DisableSystemFont();
126 #else
127 #ifndef USE_TEXGINE
128     ConvertToFontCollection<OHOS::Rosen::AdapterTxt::FontCollection>(fontCollection)->DisableSystemFont();
129 #else
130     ConvertToFontCollection<OHOS::Rosen::AdapterTextEngine::FontCollection>(fontCollection)->DisableSystemFont();
131 #endif
132 #endif
133 }
134 
OH_Drawing_ClearFontCaches(OH_Drawing_FontCollection * fontCollection)135 void OH_Drawing_ClearFontCaches(OH_Drawing_FontCollection* fontCollection)
136 {
137     if (!fontCollection) {
138         return;
139     }
140 
141     if (FontCollectionMgr::GetInstance().Find(fontCollection)) {
142         ConvertToFontCollection<OHOS::Rosen::AdapterTxt::FontCollection>(fontCollection)->ClearCaches();
143         return;
144     }
145 
146     if (g_objectMgr->HasObject(fontCollection)) {
147         ConvertToFontCollection<OHOS::Rosen::AdapterTxt::FontCollection>(fontCollection)->ClearCaches();
148         return;
149     }
150     return;
151 }
152 
OH_Drawing_GetFontCollectionGlobalInstance(void)153 OH_Drawing_FontCollection* OH_Drawing_GetFontCollectionGlobalInstance(void)
154 {
155     std::shared_ptr<OHOS::Rosen::FontCollection> fc = OHOS::Rosen::FontCollection::Create();
156     OH_Drawing_FontCollection* pointer = reinterpret_cast<OH_Drawing_FontCollection*>(fc.get());
157     if (!FontCollectionMgr::GetInstance().Find(pointer)) {
158         FontCollectionMgr::GetInstance().Insert(pointer, fc);
159     }
160     return pointer;
161 }