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 const std::string SIMPLIFIED_CHINESE = "zh-hans";
33 const std::string TRADITIONAL_CHINESE = "zh-hant";
34 const std::string ENGLISH = "en-latn";
35 const unsigned int LANGUAGE_SC = 2052;
36 const unsigned int LANGUAGE_TC = 1028;
37 const unsigned int LANGUAGE_EN = 1033;
38 const unsigned int 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     enum SystemFontType : int32_t {
72         ALL = 1 << 0,
73         GENERIC = 1 << 1,
74         STYLISH = 1 << 2,
75         INSTALLED = 1 << 3,
76     };
77 
78     struct FontDescriptor {
79         std::string path;
80         std::string postScriptName;
81         std::string fullName;
82         std::string fontFamily;
83         std::string fontSubfamily;
84         std::string requestedFullname;
85         unsigned int postScriptNameLid = 0;
86         unsigned int fullNameLid = 0;
87         unsigned int fontFamilyLid = 0;
88         unsigned int fontSubfamilyLid = 0;
89         unsigned int requestedLid = 0;
90         int weight = 0;
91         int width = 0;
92         int italic = 0;
93         bool monoSpace = false;
94         bool symbolic = false;
95     };
96 
97     FontParser();
98     std::vector<FontDescriptor> GetVisibilityFonts(const std::string &locale = SIMPLIFIED_CHINESE);
99     std::unique_ptr<FontDescriptor> GetVisibilityFontByName(const std::string& fontName,
100         const std::string locale = SIMPLIFIED_CHINESE);
101 
102     std::vector<std::shared_ptr<FontDescriptor>> GetSystemFonts(const std::string locale = ENGLISH);
103     std::vector<std::shared_ptr<FontDescriptor>> ParserFontDescriptorsFromPath(
104         const std::string& path, const std::string& locale = ENGLISH);
105     std::vector<std::shared_ptr<FontDescriptor>> CreateFontDescriptors(
106         const std::vector<std::shared_ptr<Drawing::Typeface>>& typefaces,
107         const std::string& locale = ENGLISH);
108 
109 private:
110     static void GetStringFromNameId(NameId nameId, unsigned int languageId, const std::string& nameString,
111         FontDescriptor& fontDescriptor);
112     static void ProcessCmapTable(const struct CmapTables* cmapTable, FontDescriptor& fontDescriptor);
113     int ProcessNameTable(const struct NameTable* nameTable, FontDescriptor& fontDescriptor) const;
114     static void ProcessPostTable(const struct PostTable* postTable, FontDescriptor& fontDescriptor);
115     int ParseCmapTable(std::shared_ptr<Drawing::Typeface> typeface, FontDescriptor& fontDescriptor);
116     int ParseNameTable(std::shared_ptr<Drawing::Typeface> typeface, FontDescriptor& fontDescriptor);
117     int ParsePostTable(std::shared_ptr<Drawing::Typeface> typeface, FontDescriptor& fontDescriptor);
118     int ParseTable(std::shared_ptr<Drawing::Typeface> typeface, FontDescriptor& fontDescriptor);
119     int SetFontDescriptor(const unsigned int languageId);
120     std::unique_ptr<FontParser::FontDescriptor> ParseFontDescriptor(const std::string& fontName,
121         const unsigned int languageId);
122     static void SetNameString(FontParser::FontDescriptor& fontDescriptor, std::string& field, unsigned int& fieldLid,
123         unsigned int languageId, const std::string& nameString);
GetLanguageId(const std::string & locale)124     int GetLanguageId(const std::string& locale)
125     {
126         std::string localeLower = locale;
127         transform(localeLower.begin(), localeLower.end(), localeLower.begin(), tolower);
128         if (localeLower.empty()) {
129             return LANGUAGE_SC;
130         } else if (localeLower.find(TRADITIONAL_CHINESE) == 0) {
131             return LANGUAGE_TC;
132         } else if (localeLower.find(ENGLISH) == 0) {
133             return LANGUAGE_EN;
134         } else {
135             return LANGUAGE_SC;
136         }
137     }
138 #ifdef BUILD_NON_SDK_VER
139     static std::string ConvertToString(const std::string& src, const std::string& srcType,
140         const std::string& targetType);
141 #endif
142 
143     const char* data_;
144     unsigned int length_;
145     std::vector<std::string> fontSet_;
146     std::vector<FontDescriptor> visibilityFonts_;
147 };
148 } // namespace TextEngine
149 } // namespace Rosen
150 } // namespace OHOS
151 
152 #endif // ROSEN_MODULES_TEXGINE_SRC_FONT_PARSER_H
153