• 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 #include "skia_font_style_set.h"
17 
18 #include "include/core/SkString.h"
19 #include "include/core/SkTypeface.h"
20 #include "include/core/SkFontMgr.h"
21 
22 #include "impl_interface/typeface_impl.h"
23 #include "skia_adapter/skia_convert_utils.h"
24 #include "skia_adapter/skia_typeface.h"
25 #include "utils/log.h"
26 
27 namespace OHOS {
28 namespace Rosen {
29 namespace Drawing {
SkiaFontStyleSet(sk_sp<SkFontStyleSet> skFontStyleSet)30 SkiaFontStyleSet::SkiaFontStyleSet(sk_sp<SkFontStyleSet> skFontStyleSet) : skFontStyleSet_(skFontStyleSet) {}
31 
CreateTypeface(int index)32 Typeface* SkiaFontStyleSet::CreateTypeface(int index)
33 {
34     if (!skFontStyleSet_) {
35         LOGD("SkiaFontStyleSet::CreateTypeface, skFontStyleSet_ nullptr");
36         return nullptr;
37     }
38     SkTypeface* skTypeface = skFontStyleSet_->createTypeface(index);
39     if (!skTypeface) {
40         return nullptr;
41     }
42     std::shared_ptr<TypefaceImpl> typefaceImpl = std::make_shared<SkiaTypeface>(sk_sp(skTypeface));
43     return new Typeface(typefaceImpl);
44 }
45 
GetStyle(int32_t index,FontStyle * fontStyle,std::string * styleName)46 void SkiaFontStyleSet::GetStyle(int32_t index, FontStyle* fontStyle, std::string* styleName)
47 {
48     if (!skFontStyleSet_) {
49         LOGD("SkiaFontStyleSet::GetStyle, skFontStyleSet_ nullptr");
50         return;
51     }
52     SkFontStyle skFontStyle;
53     if (fontStyle) {
54         SkiaConvertUtils::DrawingFontStyleCastToSkFontStyle(*fontStyle, skFontStyle);
55     }
56     SkString skStyleName;
57     if (styleName) {
58         SkiaConvertUtils::StdStringCastToSkString(*styleName, skStyleName);
59     }
60     skFontStyleSet_->getStyle(index, fontStyle ? &skFontStyle : nullptr, styleName ? &skStyleName : nullptr);
61     if (fontStyle) {
62         SkiaConvertUtils::SkFontStyleCastToDrawingFontStyle(skFontStyle, *fontStyle);
63     }
64     if (styleName) {
65         SkiaConvertUtils::SkStringCastToStdString(skStyleName, *styleName);
66     }
67 }
68 
MatchStyle(const FontStyle & pattern)69 Typeface* SkiaFontStyleSet::MatchStyle(const FontStyle& pattern)
70 {
71     if (!skFontStyleSet_) {
72         LOGD("SkiaFontStyleSet::MatchStyle, skFontStyleSet_ nullptr");
73         return nullptr;
74     }
75     SkFontStyle skFontStyle;
76     SkiaConvertUtils::DrawingFontStyleCastToSkFontStyle(pattern, skFontStyle);
77     SkTypeface* skTypeface = skFontStyleSet_->matchStyle(skFontStyle);
78     if (!skTypeface) {
79         return nullptr;
80     }
81     std::shared_ptr<TypefaceImpl> typefaceImpl = std::make_shared<SkiaTypeface>(sk_sp(skTypeface));
82     return new Typeface(typefaceImpl);
83 }
84 
Count()85 int SkiaFontStyleSet::Count()
86 {
87     if (!skFontStyleSet_) {
88         LOGD("SkiaFontStyleSet::Count, skFontStyleSet_ nullptr");
89         return 0;
90     }
91     return skFontStyleSet_->count();
92 }
93 } // namespace Drawing
94 } // namespace Rosen
95 } // namespace OHOS