• 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 <string>
20 #include <vector>
21 
22 #include "opentype_parser/cmap_table_parser.h"
23 #include "opentype_parser/name_table_parser.h"
24 #include "opentype_parser/post_table_parser.h"
25 
26 #include "text/typeface.h"
27 
28 namespace OHOS {
29 namespace Rosen {
30 namespace TextEngine {
31 const std::string SIMPLIFIED_CHINESE = "zh-hans";
32 const std::string TRADITIONAL_CHINESE = "zh-hant";
33 const std::string ENGLISH = "en-latn";
34 const unsigned int LANGUAGE_SC = 2052;
35 const unsigned int LANGUAGE_TC = 1028;
36 const unsigned int LANGUAGE_EN = 1033;
37 const unsigned int LANGUAGE_DEFAULT = LANGUAGE_SC;
38 
39 class FontParser {
40 public:
41     enum class PlatformId {
42         UNITE_CODE = 0,
43         MACINTOSH = 1,
44         ISO = 2,
45         WINDOWS = 3,
46         CUSTOM = 4,
47     };
48 
49     enum class EncodingIdWin {
50         SYMBOL = 0,
51         UNICODE_BMP = 1,
52     };
53 
54     enum class Version {
55         SYMBOL = 0,
56         UNICODE_BMP = 1,
57     };
58 
59     enum class NameId {
60         COPYRIGHT = 0,
61         FONT_FAMILY = 1,
62         FONT_SUBFAMILY = 2,
63         UNIQUE_FONT_ID = 3,
64         FULL_NAME = 4,
65         VERSION_STRING = 5,
66         POSTSCRIPT_NAME = 6,
67         TRADEMARK = 7,
68     };
69 
70     enum SystemFontType : int32_t {
71         ALL = 1 << 0,
72         GENERIC = 1 << 1,
73         STYLISH = 1 << 2,
74         INSTALLED = 1 << 3,
75         CUSTOMIZED = 1 << 4
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) const;
105     std::shared_ptr<FontDescriptor> ParserFontDescriptorFromPath(
106         const std::string& path, size_t index, const std::string& locale = ENGLISH) const;
107     std::shared_ptr<FontDescriptor> CreateFontDescriptor(
108         const std::shared_ptr<Drawing::Typeface>& typefaces, unsigned int languageId) const;
109     std::vector<std::shared_ptr<FontDescriptor>> CreateFontDescriptors(
110         const std::vector<std::shared_ptr<Drawing::Typeface>>& typefaces, const std::string& locale = ENGLISH) const;
111 
112 private:
113     static void GetStringFromNameId(NameId nameId, unsigned int languageId, const std::string& nameString,
114         FontDescriptor& fontDescriptor);
115     static void ProcessTable(const CmapTables* cmapTable, FontDescriptor& fontDescriptor);
116     static void ProcessTable(const NameTable* nameTable, FontDescriptor& fontDescriptor);
117     static void ProcessTable(const PostTable* postTable, FontDescriptor& fontDescriptor);
118     template<typename T>
119     static bool ParseOneTable(std::shared_ptr<Drawing::Typeface> typeface, FontParser::FontDescriptor& fontDescriptor);
120     template<typename Tuple, size_t... Is>
121     bool ParseAllTables(
122         std::shared_ptr<Drawing::Typeface> typeface, FontDescriptor& fontDescriptor, std::index_sequence<Is...>) const;
123     bool ParseTable(std::shared_ptr<Drawing::Typeface> typeface, FontDescriptor& fontDescriptor) const;
124     bool SetFontDescriptor(const unsigned int languageId);
125     std::unique_ptr<FontParser::FontDescriptor> ParseFontDescriptor(
126         const std::string& fontName, const unsigned int languageId);
127     static bool CheckFullNameParamInvalid(FontParser::FontDescriptor& fontDescriptor, unsigned int languageId,
128         const std::string& nameString);
129     static void SetNameString(FontParser::FontDescriptor& fontDescriptor, std::string& field, unsigned int& fieldLid,
130         unsigned int languageId, const std::string& nameString);
GetLanguageId(const std::string & locale)131     int GetLanguageId(const std::string& locale) const
132     {
133         std::string localeLower = locale;
134         transform(localeLower.begin(), localeLower.end(), localeLower.begin(), tolower);
135         if (localeLower.empty()) {
136             return LANGUAGE_SC;
137         } else if (localeLower.find(TRADITIONAL_CHINESE) == 0) {
138             return LANGUAGE_TC;
139         } else if (localeLower.find(ENGLISH) == 0) {
140             return LANGUAGE_EN;
141         } else {
142             return LANGUAGE_SC;
143         }
144     }
145 #ifdef BUILD_NON_SDK_VER
146     static std::string ConvertToString(const std::string& src, const std::string& srcType,
147         const std::string& targetType);
148 #endif
149 
150     const char* data_;
151     unsigned int length_;
152     std::vector<std::string> fontSet_;
153     std::vector<FontDescriptor> visibilityFonts_;
154 };
155 } // namespace TextEngine
156 } // namespace Rosen
157 } // namespace OHOS
158 
159 #endif // ROSEN_MODULES_TEXGINE_SRC_FONT_PARSER_H
160