1 /*
2  * Copyright (c) 2020-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 #ifndef UI_FONT_ALLOCATOR_H
17 #define UI_FONT_ALLOCATOR_H
18 
19 #include <stdint.h>
20 
21 #include "font/ui_font_header.h"
22 #include "gfx_utils/graphic_buffer.h"
23 
24 namespace OHOS {
25 class UIFontAllocator {
26     struct UI_STRUCT_ALIGN Chunk {
27         uint32_t next;
28         uint32_t prev;
29         bool used;
30         uint32_t reserve;
31     };
32 
33 public:
34     UIFontAllocator();
35 
36     ~UIFontAllocator();
37 
38     void SetRamAddr(uint8_t* ram, uint32_t size);
39 
40     void SetMinChunkSize(uint32_t size);
41 
42     uint32_t GetSize(void* addr);
43 
44     /**
45      * @brief allocate memory
46      *
47      * @param size
48      * @return memory address
49      */
50     void* Allocate(uint32_t size);
51 
52     void Free(void* addr);
53 
54     static BufferInfo GetCacheBuffer(uint16_t fontId,
55                                      uint32_t unicode,
56                                      ColorMode mode,
57                                      GlyphNode& glyphNode,
58                                      bool hasMetric,
59                                      TextStyle textStyle);
60 
61     static void RearrangeBitmap(BufferInfo& bufInfo, uint32_t fileSz, bool hasMetric);
62 
63 private:
64     void CombineFree(Chunk* cache);
65 
66     uint8_t* ram_;
67     uint32_t ramSize_;
68     uint32_t freeSize_;
69     uint32_t minSize_;
70     struct Chunk* end_;
71     struct Chunk* free_;
72 };
73 } // namespace OHOS
74 #endif /* UI_FONT_ALLOCATOR_H */
75