• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 ROSEN_MODULES_TEXGINE_SRC_FONT_PARSER_H
17 #define ROSEN_MODULES_TEXGINE_SRC_FONT_PARSER_H
18 
19 #include <hb.h>
20 #include <string>
21 #include <vector>
22 
23 #include "opentype_parser/cmap_table_parser.h"
24 #include "opentype_parser/name_table_parser.h"
25 #include "opentype_parser/post_table_parser.h"
26 
27 #include "texgine_typeface.h"
28 
29 namespace OHOS {
30 namespace Rosen {
31 namespace TextEngine {
32 #define SIMPLIFIED_CHINESE "zh-hans"
33 #define TRADITIONAL_CHINESE "zh-hant"
34 #define ENGLISH "en-latn"
35 #define LANGUAGE_SC 2052
36 #define LANGUAGE_TC 1028
37 #define LANGUAGE_EN 1033
38 #define LANGUAGE_DEFAULT LANGUAGE_SC
39 
40 class FontParser {
41 public:
42     enum class PlatformId {
43         UNITE_CODE = 0,
44         MACINTOSH = 1,
45         ISO = 2,
46         WINDOWS = 3,
47         CUSTOM = 4,
48     };
49 
50     enum class EncodingIdWin {
51         SYMBOL = 0,
52         UNICODE_BMP = 1,
53     };
54 
55     enum class Version {
56         SYMBOL = 0,
57         UNICODE_BMP = 1,
58     };
59 
60     enum class NameId {
61         COPYRIGHT = 0,
62         FONT_FAMILY = 1,
63         FONT_SUBFAMILY = 2,
64         UNIQUE_FONT_ID = 3,
65         FULL_NAME = 4,
66         VERSION_STRING = 5,
67         POSTSCRIPT_NAME = 6,
68         TRADEMARK = 7,
69     };
70 
71     struct FontDescriptor {
72         FontDescriptor();
73         std::string path;
74         std::string postScriptName;
75         std::string fullName;
76         std::string fontFamily;
77         std::string fontSubfamily;
78         unsigned int postScriptNameLid;
79         unsigned int fullNameLid;
80         unsigned int fontFamilyLid;
81         unsigned int fontSubfamilyLid;
82         unsigned int requestedLid;
83         int weight;
84         int width;
85         int italic;
86         bool monoSpace;
87         bool symbolic;
88     };
89 
90     FontParser();
91     std::vector<FontDescriptor> GetVisibilityFonts(const std::string locale = SIMPLIFIED_CHINESE);
92     std::unique_ptr<FontDescriptor> GetVisibilityFontByName(const std::string& fontName,
93         const std::string locale = SIMPLIFIED_CHINESE);
94 
95 private:
96     static void GetStringFromNameId(NameId nameId, unsigned int languageId, const std::string& nameString,
97         FontDescriptor& fontDescriptor);
98     static void ProcessCmapTable(const struct CmapTables* cmapTable, FontDescriptor& fontDescriptor);
99     int ProcessNameTable(const struct NameTable* nameTable, FontDescriptor& fontDescriptor) const;
100     static void ProcessPostTable(const struct PostTable* postTable, FontDescriptor& fontDescriptor);
101     int ParseCmapTable(std::shared_ptr<TexgineTypeface> typeface, FontDescriptor& fontDescriptor);
102     int ParseNameTable(std::shared_ptr<TexgineTypeface> typeface, FontDescriptor& fontDescriptor);
103     int ParsePostTable(std::shared_ptr<TexgineTypeface> typeface, FontDescriptor& fontDescriptor);
104     int ParseTable(std::shared_ptr<TexgineTypeface> typeface, FontDescriptor& fontDescriptor);
105     int SetFontDescriptor(const unsigned int languageId);
106     std::unique_ptr<FontParser::FontDescriptor> ParseFontDescriptor(const std::string& fontName,
107         const unsigned int languageId);
108     static void SetNameString(FontParser::FontDescriptor& fontDescriptor, std::string& field, unsigned int& fieldLid,
109         unsigned int languageId, const std::string& nameString);
GetLanguageId(const std::string & locale)110     int GetLanguageId(const std::string& locale)
111     {
112         std::string localeLower = locale;
113         transform(localeLower.begin(), localeLower.end(), localeLower.begin(), tolower);
114         if (localeLower.empty()) {
115             return LANGUAGE_SC;
116         } else if (localeLower.find(TRADITIONAL_CHINESE) == 0) {
117             return LANGUAGE_TC;
118         } else if (localeLower.find(ENGLISH) == 0) {
119             return LANGUAGE_EN;
120         } else {
121             return LANGUAGE_SC;
122         }
123     }
124 #ifdef BUILD_NON_SDK_VER
125     static std::string ToUtf8(const std::string& gbkStr);
126 #endif
127 
128     const char* data_;
129     unsigned int length_;
130     std::vector<std::string> fontSet_;
131     std::vector<FontDescriptor> visibilityFonts_;
132 };
133 } // namespace TextEngine
134 } // namespace Rosen
135 } // namespace OHOS
136 
137 #endif // ROSEN_MODULES_TEXGINE_SRC_FONT_PARSER_H
138