1 // Copyright (c) 2023 Huawei Device Co., Ltd. All rights reserved
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifdef ENABLE_TEXT_ENHANCE
6 #include "SkFontStyleSet_ohos.h"
7
8 /*! Constructor
9 * \param fontConfig the pointer of FontConfig_OHOS
10 * \param index the index of the font style set
11 * \param isFallback true - the font style is from fallback family
12 * \n false - the font style is from generic family
13 */
SkFontStyleSet_OHOS(const std::shared_ptr<FontConfig_OHOS> & fontConfig,int index,bool isFallback)14 SkFontStyleSet_OHOS::SkFontStyleSet_OHOS(const std::shared_ptr<FontConfig_OHOS>& fontConfig,
15 int index, bool isFallback)
16 : fontConfig_(fontConfig), styleIndex(index), isFallback(isFallback)
17 {
18 if (fontConfig) {
19 tpCount = fontConfig_->getTypefaceCount(styleIndex, isFallback);
20 }
21 }
22
23 /*! To get the count of typeface
24 * \return The count of typeface in this font style set
25 */
count()26 int SkFontStyleSet_OHOS::count()
27 {
28 return tpCount;
29 }
30
31 /*! To get the font style for the specified typeface
32 * \param the index of a typeface
33 * \param[out] style the style value returned to the caller
34 * \param[out] the style name returned to the caller
35 */
getStyle(int index,SkFontStyle * style,SkString * styleName)36 void SkFontStyleSet_OHOS::getStyle(int index, SkFontStyle* style, SkString* styleName)
37 {
38 if (index < 0 || index >= this->count() || fontConfig_ == nullptr) {
39 return;
40 }
41
42 sk_sp<SkTypeface> typeface = fontConfig_->getTypeface(styleIndex, index, isFallback);
43 if (typeface == nullptr) {
44 return;
45 }
46
47 if (style) {
48 *style = typeface->fontStyle();
49 }
50 if (styleName) {
51 const char* names[] = {
52 "invisible",
53 "thin",
54 "extralight",
55 "light",
56 "normal",
57 "medium",
58 "semibold",
59 "bold",
60 "extrabold",
61 "black",
62 "extrablack"
63 };
64 // the value of font weight is between 0 ~ 1000 (refer to SkFontStyle::Weight)
65 // the weight is divided by 100 to get the matched name
66 unsigned int i = typeface->fontStyle().weight() / 100;
67 if (i < sizeof(names) / sizeof(char*)) {
68 styleName->set(names[i]);
69 } else {
70 styleName->reset();
71 }
72 }
73 }
74
75 /*! To create a typeface
76 * \param index the index of the typeface in this font style set
77 * \return The object of a typeface, if successful
78 * \n Return null, if the 'index' is out of range
79 * \note The caller must call unref() on the returned object if it's not null
80 */
createTypeface(int index)81 sk_sp<SkTypeface> SkFontStyleSet_OHOS::createTypeface(int index)
82 {
83 if (index < 0 || index >= this->count()) {
84 return nullptr;
85 }
86 if (fontConfig_) {
87 return fontConfig_->getTypeface(styleIndex, index, isFallback);
88 }
89 return nullptr;
90 }
91
92 /*! To get the closest matching typeface
93 * \param pattern the style value to be matching
94 * \return the object of a typeface which is the closest matching to 'pattern'
95 * \note The caller must call unref() on the returned object
96 */
matchStyle(const SkFontStyle & pattern)97 sk_sp<SkTypeface> SkFontStyleSet_OHOS::matchStyle(const SkFontStyle& pattern)
98 {
99 if (fontConfig_) {
100 return fontConfig_->getTypeface(styleIndex, pattern, isFallback);
101 }
102 return nullptr;
103 }
104
105 #endif // ENABLE_TEXT_ENHANCE