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