1/*
2 * Copyright (c) 2022 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 GLYPHS_FILE_H
17#define GLYPHS_FILE_H
18
19#include "font/ui_font_header.h"
20#include "gfx_utils/graphic_buffer.h"
21
22namespace OHOS {
23class GlyphsFile {
24public:
25    GlyphsFile();
26
27    GlyphsFile(const GlyphsFile&) = delete;
28
29    GlyphsFile& operator=(const GlyphsFile&) = delete;
30
31    GlyphsFile(const GlyphsFile&&) = delete;
32
33    GlyphsFile& operator=(const GlyphsFile&&) = delete;
34
35    ~GlyphsFile();
36
37    int8_t GetFontVersion(const char* fontName, char* version, uint8_t len);
38
39    int16_t GetFontHeight(uint16_t fontId);
40
41    const FontHeader* GetFontHeader(uint16_t fontId);
42
43    int8_t GetBitmap(GlyphNode& node, BufferInfo& bufInfo);
44
45    int8_t SetFile(const char* fontName, int32_t fp, uint32_t start);
46
47    int8_t GetNodeFromFile(uint32_t unicode, uint16_t fontId, GlyphNode& node);
48
49    bool IsSameFile(const char* fontName);
50
51private:
52    static constexpr uint8_t FONT_NAME_LEN_MAX = 64;
53    static constexpr uint8_t RADIX_TREE_BITS = 4;
54    static constexpr uint8_t RADIX_SHIFT_START = 32 - RADIX_TREE_BITS;
55    static constexpr uint32_t RADIX_TREE_SLOT_NUM = 1 << RADIX_TREE_BITS;
56    static constexpr uint32_t RADIX_TREE_MASK = RADIX_TREE_SLOT_NUM - 1;
57
58    struct IndexNode {
59        uint16_t stubs[RADIX_TREE_SLOT_NUM];
60    };
61
62    struct GlyphInfo {
63        uint16_t fontId;
64        uint32_t glyphNodeSectionStart;
65        uint32_t bitMapSectionStart;
66        uint32_t fontIndexSectionStart;
67        uint8_t* indexCache;
68        FontHeader* fontHeader;
69    };
70
71    int8_t CacheInit();
72    int8_t GetGlyphInfo(uint16_t fontId, GlyphInfo& glyphInfo);
73    void SetFontName(const char* fontName);
74
75    BinHeader binHeader_;
76    uint32_t start_;
77    uint32_t fontHeaderSectionStart_;
78    uint32_t fontIndexSectionStart_;
79    uint32_t glyphNodeSectionStart_;
80    uint32_t bitMapSectionStart_;
81
82    FontHeader* fontHeaderCache_;
83    uint8_t* indexCache_;
84    char* fontName_;
85    int32_t fp_;
86    bool isFileSet_;
87    uint8_t fontNum_;
88};
89} // namespace OHOS
90#endif /* GLYPHS_FILE_H */
91