1 /*
2  * Copyright (c) 2024 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 #ifndef FONT_BUFFER_H
17 #define FONT_BUFFER_H
18 
19 #include <memory>
20 #include <shared_mutex>
21 
22 #include <base/containers/array_view.h>
23 #include <base/containers/refcnt_ptr.h>
24 #include <base/containers/vector.h>
25 #include <font/namespace.h>
26 
27 FONT_BEGIN_NAMESPACE()
28 class FaceData;
29 class FontManager;
30 
31 class FontBuffer final : public std::enable_shared_from_this<FontBuffer> {
32 public:
33     FontBuffer(FontManager* fontMgr, uint64_t hash, BASE_NS::array_view<const uint8_t> bytes);
34     ~FontBuffer();
35 
36     std::shared_ptr<FaceData> CreateFace(long index);
37 
38     void Gc();
39 
40 private:
41     friend class FaceData;
42     friend class Font;
43 
44     uint64_t hash_;                  // path hash as map key in font mgr
45     BASE_NS::vector<uint8_t> bytes_; // font file contents
46 
47     std::shared_mutex mutex_;
48     // faces created from this font file. each Font instance references a FaceData instance and multiple Font instances
49     // can reference the same FaceData. here we hold weak references so that FaceData is released when the last Font
50     // using it is destroyed.
51     BASE_NS::vector<std::weak_ptr<FaceData>> faces_;
52 
53     BASE_NS::refcnt_ptr<FontManager> fontManager;
54 };
55 FONT_END_NAMESPACE()
56 
57 #endif // FONT_BUFFER_H
58