• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 GRAPHIC_LITE_UI_FONT_HEADER_H
17 #define GRAPHIC_LITE_UI_FONT_HEADER_H
18 
19 #include <string>
20 #include "common/text.h"
21 namespace OHOS {
22 #pragma pack(1)
23 
24 enum UITextWildcardStaticType : uint8_t { TEXT_STATIC_STR1 = 0, TEXT_STATIC_STR2, TEXT_STATIC_STR3 };
25 
26 struct UITextLanguageTextParam {
27     uint16_t textId;
28     uint8_t fontId;
29     uint8_t align : 2;  // UITextLanguageAlignment
30     uint8_t direct : 1; // UITextLanguageDirect
31     uint8_t shaping : 1;
32     uint8_t wildCard1 : 1;
33     uint8_t wildCard2 : 1;
34     uint8_t reserve : 2;
35     uint32_t strOff1;
36     uint16_t strLen1;
37     uint16_t strLen2;
38     uint16_t strLen3;
39     uint32_t codePointsOff;
40     uint16_t codePointsLen;
41 }; // UITextLanguageTextParam
42 
43 // bin header
44 constexpr uint8_t FONT_VERSION_OFFSET = 0;
45 constexpr uint8_t FONT_VERSION_LEN = 10;
46 constexpr uint8_t FONT_MAGIC_NUM_OFFSET = FONT_VERSION_OFFSET + FONT_VERSION_LEN;
47 constexpr uint8_t FONT_MAGIC_NUM_LEN = 6;
48 constexpr uint8_t FONT_NUMBER_OFFSET = FONT_MAGIC_NUM_OFFSET + FONT_MAGIC_NUM_LEN;
49 constexpr uint8_t FONT_NUMBER_LEN = 2;
50 constexpr uint8_t BIN_HEAD_RES_OFFSET = FONT_NUMBER_OFFSET + FONT_NUMBER_LEN;
51 constexpr uint8_t BIN_HEAD_RES_LEN = 2;
52 constexpr uint8_t FONT_DATA_OFFSET = BIN_HEAD_RES_OFFSET + BIN_HEAD_RES_LEN;
53 constexpr uint8_t BIN_HEAD_TOTAL_LEN = FONT_VERSION_LEN + FONT_MAGIC_NUM_LEN + FONT_NUMBER_LEN + BIN_HEAD_RES_LEN;
54 
55 // font header
56 constexpr uint8_t FONT_ID_OFFSET = 0;
57 constexpr uint8_t FONT_ID_LEN = 1;
58 constexpr uint8_t GLYPH_NUM_OFFSET = FONT_ID_OFFSET + FONT_ID_LEN;
59 constexpr uint8_t GLYPH_NUM_LEN = 2;
60 constexpr uint8_t FONT_HEIGHT_OFFSET = GLYPH_NUM_OFFSET + GLYPH_NUM_LEN;
61 constexpr uint8_t FONT_HEIGHT_LEN = 2;
62 constexpr uint8_t FONT_ASCEND_OFFSET = FONT_HEIGHT_OFFSET + FONT_HEIGHT_LEN;
63 constexpr uint8_t FONT_ASCEND_LEN = 2;
64 constexpr uint8_t FONT_DESCEND_OFFSET = FONT_ASCEND_OFFSET + FONT_ASCEND_LEN;
65 constexpr uint8_t FONT_DESCEND_LEN = 2;
66 constexpr uint8_t FONT_INDEX_OFF_OFFSET = FONT_DESCEND_OFFSET + FONT_DESCEND_LEN;
67 constexpr uint8_t FONT_INDEX_OFF_LEN = 4;
68 constexpr uint8_t FONT_INDEX_LEN_OFFSET = FONT_INDEX_OFF_OFFSET + FONT_INDEX_OFF_LEN;
69 constexpr uint8_t FONT_INDEX_LEN_LEN = 4;
70 constexpr uint8_t GLYPH_OFF_OFFSET = FONT_INDEX_LEN_OFFSET + FONT_INDEX_LEN_LEN;
71 constexpr uint8_t GLYPH_OFF_LEN = 4;
72 constexpr uint8_t GLYPH_LEN_OFFSET = GLYPH_OFF_OFFSET + GLYPH_OFF_LEN;
73 constexpr uint8_t GLYPH_LEN_LEN = 4;
74 constexpr uint8_t FONT_HEAD_RES_OFFSET = GLYPH_LEN_OFFSET + GLYPH_LEN_LEN;
75 constexpr uint8_t FONT_HEAD_RES_LEN = 2;
76 constexpr uint8_t FONT_HEAD_TOTAL_LEN = FONT_ID_LEN + GLYPH_NUM_LEN + FONT_HEIGHT_LEN + FONT_ASCEND_LEN +
77                                         FONT_DESCEND_LEN + GLYPH_OFF_LEN + GLYPH_LEN_LEN + FONT_HEAD_RES_LEN;
78 
79 // glyph header, internal using, not write to bin file
80 constexpr uint8_t FONT_G_HEIGHT_OFFSET = 0;
81 constexpr uint8_t FONT_G_HEIGHT_LEN = 2;
82 constexpr uint8_t GLYPH_G_NUM_OFFSET = FONT_G_HEIGHT_OFFSET + FONT_G_HEIGHT_LEN;
83 constexpr uint8_t GLYPH_G_NUM_LEN = 2;
84 constexpr uint8_t GLYPH_G_ASCEND_OFFSET = GLYPH_G_NUM_OFFSET + GLYPH_G_NUM_LEN;
85 constexpr uint8_t GLYPH_G_ASCEND_LEN = 2;
86 constexpr uint8_t GLYPH_G_DESCEND_OFFSET = GLYPH_G_ASCEND_OFFSET + GLYPH_G_ASCEND_LEN;
87 constexpr uint8_t GLYPH_G_DESCEND_LEN = 2;
88 constexpr uint8_t GLYPH_G_INDEX_OFF_OFFSET = GLYPH_G_DESCEND_OFFSET + GLYPH_G_DESCEND_LEN;
89 constexpr uint8_t GLYPH_G_INDEX_OFF_LEN = 4;
90 constexpr uint8_t GLYPH_G_INDEX_LEN_OFFSET = GLYPH_G_INDEX_OFF_OFFSET + GLYPH_G_INDEX_OFF_LEN;
91 constexpr uint8_t GLYPH_G_INDEX_LEN_LEN = 4;
92 constexpr uint8_t GLYPH_G_OFF_OFFSET = GLYPH_G_INDEX_LEN_OFFSET + GLYPH_G_INDEX_LEN_LEN;
93 constexpr uint8_t GLYPH_G_OFF_LEN = 4;
94 constexpr uint8_t GLYPH_G_LEN_OFFSET = GLYPH_G_OFF_OFFSET + GLYPH_G_OFF_LEN;
95 constexpr uint8_t GLYPH_G_LEN_LEN = 4;
96 constexpr uint8_t FONT_G_HEAD_TOTAL_LEN =
97     FONT_G_HEIGHT_LEN + GLYPH_G_NUM_LEN + GLYPH_G_ASCEND_LEN + GLYPH_G_DESCEND_LEN + GLYPH_G_OFF_LEN + GLYPH_G_LEN_LEN;
98 
99 // glyph node
100 constexpr uint8_t GLYPH_UNICODE_OFFSET = GLYPH_G_NUM_OFFSET + GLYPH_G_NUM_LEN;
101 constexpr uint8_t GLYPH_UNICODE_LEN = 4;
102 constexpr uint8_t GLYPH_LEFT_OFFSET = GLYPH_UNICODE_OFFSET + GLYPH_UNICODE_LEN;
103 constexpr uint8_t GLYPH_LEFT_LEN = 2;
104 constexpr uint8_t GLYPH_TOP_OFFSET = GLYPH_LEFT_OFFSET + GLYPH_LEFT_LEN;
105 constexpr uint8_t GLYPH_TOP_LEN = 2;
106 constexpr uint8_t GLYPH_ADVANCE_OFFSET = GLYPH_TOP_OFFSET + GLYPH_TOP_LEN;
107 constexpr uint8_t GLYPH_ADVANCE_LEN = 2;
108 constexpr uint8_t GLYPH_COLS_OFFSET = GLYPH_ADVANCE_OFFSET + GLYPH_ADVANCE_LEN;
109 constexpr uint8_t GLYPH_COLS_LEN = 2;
110 constexpr uint8_t GLYPH_ROWS_OFFSET = GLYPH_COLS_OFFSET + GLYPH_COLS_LEN;
111 constexpr uint8_t GLYPH_ROWS_LEN = 2;
112 constexpr uint8_t GLYPH_DATA_OFF_OFFSET = GLYPH_ROWS_OFFSET + GLYPH_ROWS_LEN;
113 constexpr uint8_t GLYPH_DATA_OFF_LEN = 4;
114 constexpr uint8_t GLYPH_KERN_OFF_OFFSET = GLYPH_DATA_OFF_OFFSET + GLYPH_DATA_OFF_LEN;
115 constexpr uint8_t GLYPH_KERN_OFF_LEN = 4;
116 constexpr uint8_t GLYPH_KERN_SIZE_OFFSET = GLYPH_KERN_OFF_OFFSET + GLYPH_KERN_OFF_LEN;
117 constexpr uint8_t GLYPH_KERN_SIZE_LEN = 2;
118 constexpr uint8_t GLYPH_HEAD_RES_OFFSET = GLYPH_KERN_SIZE_OFFSET + GLYPH_KERN_SIZE_LEN;
119 constexpr uint8_t GLYPH_HEAD_RES_LEN = 2;
120 constexpr uint8_t GLYPH_HEAD_TOTAL_LEN = GLYPH_UNICODE_LEN + GLYPH_LEFT_LEN + GLYPH_TOP_LEN + GLYPH_ADVANCE_LEN +
121                                          GLYPH_COLS_LEN + GLYPH_ROWS_LEN + GLYPH_DATA_OFF_LEN + GLYPH_KERN_OFF_LEN +
122                                          GLYPH_KERN_SIZE_LEN + GLYPH_HEAD_RES_LEN;
123 
124 // language texts table header
125 constexpr uint8_t LANG_TEXTS_PARAM_OFF = 0;
126 constexpr uint8_t LANG_TEXTS_PARAM_LEN = 4;
127 constexpr uint8_t LANG_TEXTS_UTF8P_OFF = LANG_TEXTS_PARAM_OFF + LANG_TEXTS_PARAM_LEN;
128 constexpr uint8_t LANG_TEXTS_UTF8P_LEN = 4;
129 constexpr uint8_t LANG_TEXTS_UTF8_OFF = LANG_TEXTS_UTF8P_OFF + LANG_TEXTS_UTF8P_LEN;
130 constexpr uint8_t LANG_TEXTS_UTF8_LEN = 4;
131 constexpr uint8_t LANG_TEXTS_TOTAL_LEN = LANG_TEXTS_PARAM_LEN + LANG_TEXTS_UTF8P_LEN + LANG_TEXTS_UTF8_LEN;
132 
133 // language texts utf-8 param header
134 constexpr uint8_t LANG_UTF8_PARAM_TOTAL_LEN = 8; // 4B offset + 4B len
135 
136 // language bin header
137 constexpr uint8_t LANG_OFFSET_OFF = 0;
138 constexpr uint8_t LANG_OFFSET_LEN = 4;
139 constexpr uint8_t LANG_SIZE_OFF = LANG_OFFSET_OFF + LANG_OFFSET_LEN;
140 constexpr uint8_t LANG_SIZE_LEN = 4;
141 constexpr uint8_t LANG_HEAD_TOTAL_LEN = LANG_OFFSET_LEN + LANG_SIZE_LEN;
142 
143 // dynamic font
144 constexpr uint32_t GLYPH_HEADER_LENGTH = 0xD7C00;                  // 860 K, almost 33870 glyph header
145 constexpr uint32_t FONT_HEADER_LENGTH = FONT_HEAD_TOTAL_LEN * 150; // 150 font, almost 3K
146 constexpr uint32_t DYNAMIC_FONT_HEADER_TOTAL_LENGTH = GLYPH_HEADER_LENGTH + FONT_HEADER_LENGTH;
147 
148 // static font psram
149 constexpr uint32_t UTF8_PARAM_LENGTH = LANG_UTF8_PARAM_TOTAL_LEN * 100;          // 100 lang, almost 1K
150 constexpr uint32_t STATIC_PARAM_LENGTH = sizeof(UITextLanguageTextParam) * 3218; // 3218 texts, 44K
151 constexpr uint32_t STATIC_UTF8_LENGTH = 0xC800;                                  // 50 * 1024 = 50K
152 constexpr uint32_t STATIC_FONT_HEADER_LENGTH = FONT_HEAD_TOTAL_LEN * 150;        // 150 font, almost 3K
153 constexpr uint32_t STATIC_GLYPH_HEADER_LENGTH = 0x15C00;                         // 87K, almost 26 * 3426 glyph header
154 constexpr uint32_t STATIC_FONT_HEADER_TOTAL_LENGTH = UTF8_PARAM_LENGTH + STATIC_PARAM_LENGTH + STATIC_UTF8_LENGTH +
155                                                      STATIC_FONT_HEADER_LENGTH + STATIC_GLYPH_HEADER_LENGTH;
156 
157 constexpr uint8_t MAX_CACHE_CHAR_NUMBER = 50;        // cache 50 letters
158 constexpr uint32_t DYNAMIC_ONE_CHAR_LENGTH = 0x5000; // 20 K means w200:h200:bpp4, 200 x 200 / 2
159 
160 // 50 * 20K = 1000K
161 constexpr uint32_t DYNAMIC_TEXT_CACHE_LENGTH = MAX_CACHE_CHAR_NUMBER * DYNAMIC_ONE_CHAR_LENGTH;
162 constexpr uint32_t MIN_FONT_PSRAM_LENGTH = 0x380000;
163 
164 // text shaping psram
165 constexpr uint32_t SHAPING_TTF_TABLE_LENGTH         = 0x4B000; // 307K
166 constexpr uint32_t SHAPING_WORD_DICT_LENGTH         = 0xD000; // 53K
167 constexpr uint32_t SHAPING_CACHE_LENGTH             = 0x50000; // 328K
168 constexpr uint32_t MIN_SHAPING_PSRAM_LENGTH =
169     SHAPING_TTF_TABLE_LENGTH + SHAPING_WORD_DICT_LENGTH + SHAPING_CACHE_LENGTH + HARFBUZ_CACHE_LENGTH;
170 
171 // other definition
172 constexpr uint8_t FONT_DPI = 100;
173 constexpr uint8_t FONT_PIXEL_IN_POINT = 64;
174 constexpr uint8_t BPP_BIT_1 = 1;
175 constexpr uint8_t BPP_MASK_1 = 7;
176 constexpr uint8_t BPP_SHIFT_1 = 3;
177 constexpr uint8_t BPP_MASK_NUM_1 = 0x80;
178 constexpr uint8_t BPP_BIT_2 = 2;
179 constexpr uint8_t BPP_MASK_2 = 3;
180 constexpr uint8_t BPP_SHIFT_2 = 2;
181 constexpr uint8_t BPP_MASK_NUM_2 = 0xC0;
182 constexpr uint8_t BPP_BIT_4 = 4;
183 constexpr uint8_t BPP_MASK_4 = 1;
184 constexpr uint8_t BPP_SHIFT_4 = 1;
185 constexpr uint8_t BPP_MASK_NUM_4 = 0xF0;
186 constexpr uint8_t BPP_BIT_8 = 8;
187 constexpr uint32_t DEFAULT_FONT_SIZE = 10;
188 constexpr uint32_t MAX_CHAR_MB_LEN = 16;
189 constexpr uint32_t MAX_CHAR_NUM = 512;
190 constexpr uint8_t BITS_PER_BYTE = 8;
191 constexpr uint8_t BITS_MASK_IN_BYTE = 7;
192 constexpr uint32_t MAX_MONO_ADVANCE = 128;
193 constexpr uint8_t BIT0 = 0;
194 constexpr uint8_t BIT1 = 1;
195 constexpr uint8_t BIT2 = 2;
196 constexpr uint8_t BIT3 = 3;
197 constexpr uint8_t BIT4 = 4;
198 constexpr uint8_t BIT5 = 5;
199 constexpr uint8_t BIT6 = 6;
200 constexpr uint8_t BIT7 = 7;
201 constexpr uint8_t BIT8 = 8;
202 constexpr uint8_t BIT12 = 12;
203 constexpr uint8_t BIT18 = 18;
204 constexpr uint8_t GLYPH_KERN_ITEM_LEN = 2;
205 constexpr uint8_t UTF_TO_UNICODE_MASK1 = 0x80;
206 constexpr uint8_t UTF_TO_UNICODE_MASK2 = 0xC0;
207 constexpr uint8_t UTF_TO_UNICODE_MASK3 = 0xE0;
208 constexpr uint8_t UTF_TO_UNICODE_MASK4 = 0xF0;
209 constexpr uint8_t UTF_TO_UNICODE_MASK5 = 0xF8;
210 constexpr uint8_t UTF_TO_UNICODE_MASK6 = 0x3F;
211 constexpr uint8_t UTF_TO_UNICODE_MASK7 = 0x1F;
212 constexpr uint8_t UTF_TO_UNICODE_MASK8 = 0x07;
213 constexpr uint8_t UTF_TO_UNICODE_MASK9 = 0x0F;
214 constexpr uint8_t MAX_FILE_NAME_LEN = 255;
215 constexpr uint8_t GPU_FONT_ALIGN_BIT = 16;
216 constexpr uint8_t INVALID_UCHAR_ID = 0xFF;
217 constexpr uint16_t INVALID_USHORT_ID = 0xFFFF;
218 constexpr uint32_t INVALID_UINT_ID = 0xFFFFFFFF;
219 constexpr int8_t INVALID_RET_VALUE = -1;
220 constexpr int8_t RET_VALUE_OK = 0;
221 constexpr const char* FONT_MAGIC_NUMBER = const_cast<char *>("OHOS.");
222 
223 /**
224  * @brief struct BinHeader for font
225  * refer to ui_font.h
226  */
227 struct BinHeader {
228     char fontVersion[FONT_VERSION_LEN]; // FONT_VERSION_OFFSET
229     char fontMagic[FONT_MAGIC_NUM_LEN]; // FONT_MAGIC_NUM_OFFSET
230     uint16_t fontNum;                   // FONT_NUMBER_OFFSET
231     uint16_t reserve;                   // BIN_HEAD_RES_OFFSET
232 };
233 
234 /**
235  * @brief struct FontHeader
236  * refer to ui_font.h
237  */
238 struct FontHeader {
239     uint8_t fontId;      // FONT_ID_OFFSET
240     uint16_t glyphNum;   // GLYPH_NUM_OFFSET
241     uint16_t fontHeight; // FONT_HEIGHT_OFFSET
242     int16_t ascender;    // FONT_ASCEND_LEN
243     int16_t descender;   // FONT_DESCEND_LEN
244     uint32_t indexOffset;
245     uint32_t indexLen;
246     uint32_t glyphOffset; // GLYPH_G_OFF_OFFSET
247     uint32_t glyphLen;    // GLYPH_OFF_OFFSET
248     uint16_t reserve;     // GLYPH_LEN_OFFSET
249 };
250 
251 /**
252  * @brief struct GlyphHeader for font, font tool using, not for bin file
253  * refer to ui_font.h
254  */
255 struct GlyphHeader {
256     uint16_t fontHeight; // FONT_G_HEIGHT_OFFSET
257     uint16_t glyphNum;   // GLYPH_G_NUM_OFFSET
258     int16_t ascender;    // GLYPH_G_ASCEND_LEN
259     int16_t descender;   // GLYPH_G_DESCEND_LEN
260     uint32_t indexOffset;
261     uint32_t indexLen;
262     uint32_t glyphOffset; // GLYPH_G_OFF_OFFSET
263     uint32_t glyphLen;    // GLYPH_G_LEN_OFFSET
264 };
265 
266 /**
267  * @brief struct GlyphNode for font
268  * refer to ui_font.h
269  */
270 struct GlyphNode {
271     uint32_t unicode;  // GLYPH_UNICODE_OFFSET
272     int16_t left;      // GLYPH_LEFT_OFFSET
273     int16_t top;       // GLYPH_TOP_OFFSET
274     uint16_t advance;  // GLYPH_ADVANCE_OFFSET
275     uint16_t cols;     // GLYPH_COLS_OFFSET
276     uint16_t rows;     // GLYPH_ROWS_OFFSET
277     uint32_t dataOff;  // GLYPH_DATA_OFF_OFFSET
278     uint32_t kernOff;  // GLYPH_KERN_OFF_OFFSET
279     uint16_t kernSize; // GLYPH_KERN_SIZE_OFFSET
280     uint8_t fontId;    // GLYPH_FONT_ID
281     uint8_t dataFlag;  // GLYPH_DATA_HEAD_FLAG
282 };
283 
284 /**
285  * @brief struct LangBinHeader for font
286  * refer to ui_font.h
287  */
288 struct LangBinHeader {
289     char fontVersion[FONT_VERSION_LEN]; // FONT_VERSION_OFFSET
290     char fontMagic[FONT_MAGIC_NUM_LEN]; // FONT_MAGIC_NUM_OFFSET
291 };
292 
293 /**
294  * @brief struct LangFontBinHeader for font
295  * refer to ui_font.h
296  */
297 struct LangFontBinHeader {
298     uint32_t offset; // LANG_OFFSET_LEN
299     uint32_t size;   // LANG_SIZE_LEN
300 };
301 
302 /**
303  * @brief struct LangTexsTableHeader for font
304  * refer to ui_font.h
305  */
306 struct LangTexsTableHeader {
307     uint32_t textParamStart;
308     uint32_t textParamLen; // LANG_TEXTS_PARAM_LEN
309     uint32_t textUtf8ParamStart;
310     uint32_t textUtf8ParamLen; // LANG_TEXTS_UTF8P_LEN
311     uint32_t textUtf8Start;
312     uint32_t textUtf8Len; // LANG_TEXTS_UTF8_LEN
313     uint32_t textUtf8MaxItemLen;
314     uint32_t textCodepointsStart;
315     uint32_t textCodepointsMaxItemLen;
316     uint32_t textCodepointsTotalLen;
317 };
318 
319 /**
320  * @brief struct LangTexsUtf8Param for font
321  * refer to ui_font.h
322  */
323 struct LangTexsUtf8Param {
324     uint32_t offset;
325     uint32_t len;
326     uint32_t codePointOffset;
327     uint32_t codePointLen;
328 };
329 
330 /**
331  * @brief struct KernNode for kerning of font
332  * refer to ui_font.h
333  */
334 struct KernNode {
335     uint32_t unicode;  // GLYPH_UNICODE_LEN
336     int16_t kernValue; // GLYPH_KERN_SIZE_LEN
337     uint8_t found;     // found the kerning or not
338 };
339 
340 /**
341  * @brief struct TtfHeader for header of ttftab.bin
342  * refer to ui_font.h
343  */
344 struct TtfHeader {
345     uint8_t ttfId;
346     uint32_t offset;
347     uint32_t len;
348 };
349 
350 struct UITextLanguageFontParam {
351     uint8_t size; // Invalid value for vector font
352     uint8_t fontWeight;
353     uint8_t shaping;
354     uint8_t ttfId; // auto create during registration for vector font
355     const char* ttfName;
356 }; // UITextLanguageFontParam
357 
358 struct TextIsoHeader {
359     uint16_t paramLen;
360     uint16_t strLen;
361 };
362 
363 struct TextIsoParam {
364     uint16_t offset;
365     uint16_t len;
366 };
367 
368 struct LangTextParam {
369     uint8_t fontDirect; // refer to UITextLanguageDirect
370     uint8_t fontSize;
371     const char* familyName; // such as "Roboto-Medium"
372     const char* langName;   // such as "ar", "zh"
373 };
374 
375 struct FileCommonHeader {
376     const char magicWord[5]; // 5:file identifier's length is 5
377     uint32_t fileLength;
378 };
379 #pragma pack()
380 } // namespace OHOS
381 #endif /* UI_FONT_HEADER_H */