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