• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 Huawei Device Co., Ltd.. All rights reserved.
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 #include "text_font_utils.h"
16 
17 namespace OHOS {
18 namespace Rosen {
19 namespace SPText {
20 namespace skt = skia::textlayout;
21 static const int WEIGHT_BASE = 100;
22 
GetTxtFontWeight(int fontWeight)23 FontWeight TextFontUtils::GetTxtFontWeight(int fontWeight)
24 {
25     constexpr int minWeight = static_cast<int>(FontWeight::W100);
26     constexpr int maxWeight = static_cast<int>(FontWeight::W900);
27 
28     int weight = std::clamp((fontWeight - WEIGHT_BASE) / WEIGHT_BASE, minWeight, maxWeight);
29     return static_cast<FontWeight>(weight);
30 }
31 
GetTxtFontStyle(RSFontStyle::Slant slant)32 FontStyle TextFontUtils::GetTxtFontStyle(RSFontStyle::Slant slant)
33 {
34     return slant == RSFontStyle::Slant::UPRIGHT_SLANT ? FontStyle::NORMAL : FontStyle::ITALIC;
35 }
36 
GetSkiaFontWeight(FontWeight spFontWeight)37 RSFontStyle::Weight TextFontUtils::GetSkiaFontWeight(FontWeight spFontWeight)
38 {
39     constexpr int minWeight = static_cast<int>(FontWeight::W100);
40     constexpr int maxWeight = static_cast<int>(FontWeight::W900);
41 
42     int fontWeight = static_cast<int>(spFontWeight);
43     fontWeight = std::clamp(fontWeight, minWeight, maxWeight) + 1;
44 
45     return static_cast<RSFontStyle::Weight>(fontWeight * WEIGHT_BASE);
46 }
47 
GetSkiaFontSlant(FontStyle spSlant)48 RSFontStyle::Slant TextFontUtils::GetSkiaFontSlant(FontStyle spSlant)
49 {
50     return spSlant == FontStyle::NORMAL ? RSFontStyle::UPRIGHT_SLANT : RSFontStyle::ITALIC_SLANT;
51 }
52 
GetSkiaFontWidth(FontWidth spFontWidth)53 RSFontStyle::Width TextFontUtils::GetSkiaFontWidth(FontWidth spFontWidth)
54 {
55     return RSFontStyle::Width(static_cast<int>(spFontWidth));
56 }
57 
GetSkiaFontfamilies(const std::vector<std::string> & spFontFamiles)58 std::vector<SkString> TextFontUtils::GetSkiaFontfamilies(const std::vector<std::string>& spFontFamiles)
59 {
60     std::vector<SkString> skiaFontFamiles;
61     std::transform(spFontFamiles.begin(), spFontFamiles.end(), std::back_inserter(skiaFontFamiles),
62         [](const std::string& f) { return SkString(f.c_str()); });
63     return skiaFontFamiles;
64 }
65 
MakeFontArguments(skt::TextStyle & skStyle,const FontVariations & fontVariations)66 void TextFontUtils::MakeFontArguments(skt::TextStyle& skStyle, const FontVariations& fontVariations)
67 {
68     constexpr size_t axisLen = 4;
69 
70     std::vector<SkFontArguments::VariationPosition::Coordinate> coordinates;
71     for (const auto& [axis, value] : fontVariations.GetAxisValues()) {
72         if (axis.length() == axisLen) {
73             coordinates.push_back({
74                 SkSetFourByteTag(axis[0], axis[1], axis[2], axis[3]),
75                 value,
76             });
77         }
78     }
79     SkFontArguments::VariationPosition position = { coordinates.data(), static_cast<int>(coordinates.size()) };
80 
81     SkFontArguments arguments;
82     arguments.setVariationDesignPosition(position);
83     skStyle.setFontArguments(arguments);
84 }
85 
MakeTextShadow(const TextShadow & txtShadow)86 skt::TextShadow TextFontUtils::MakeTextShadow(const TextShadow& txtShadow)
87 {
88     skt::TextShadow shadow;
89     shadow.fOffset = txtShadow.offset;
90     shadow.fBlurSigma = txtShadow.blurSigma;
91     shadow.fColor = txtShadow.color;
92     return shadow;
93 }
94 
ConvertToRSFontSlant(FontStyle fontStyle)95 RSFontStyle::Slant TextFontUtils::ConvertToRSFontSlant(FontStyle fontStyle)
96 {
97     RSFontStyle::Slant slant;
98     switch (fontStyle) {
99         case FontStyle::NORMAL:
100             slant = RSFontStyle::Slant::UPRIGHT_SLANT;
101             break;
102         case FontStyle::ITALIC:
103             slant = RSFontStyle::Slant::ITALIC_SLANT;
104             break;
105         case FontStyle::OBLIQUE:
106             slant = RSFontStyle::Slant::OBLIQUE_SLANT;
107             break;
108         default:
109             slant = RSFontStyle::Slant::UPRIGHT_SLANT;
110     }
111     return slant;
112 }
113 
ConvertToSkFontWeight(FontWeight fontWeight)114 int TextFontUtils::ConvertToSkFontWeight(FontWeight fontWeight)
115 {
116     return static_cast<int>(fontWeight) * WEIGHT_BASE + WEIGHT_BASE;
117 }
118 
MakeFontStyle(FontWeight fontWeight,FontWidth fontWidth,FontStyle fontStyle)119 RSFontStyle TextFontUtils::MakeFontStyle(FontWeight fontWeight, FontWidth fontWidth, FontStyle fontStyle)
120 {
121     auto weight = TextFontUtils::ConvertToSkFontWeight(fontWeight);
122     auto width = static_cast<RSFontStyle::Width>(fontWidth);
123     auto slant = TextFontUtils::ConvertToRSFontSlant(fontStyle);
124     return RSFontStyle(weight, width, slant);
125 }
126 
127 }  // namespace SPText
128 }  // namespace Rosen
129 }  // namespace OHOS