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 <map>
17
18 #include "font_styles.h"
19 #include "texgine_exception.h"
20 #include "texgine/utils/exlog.h"
21
22 namespace OHOS {
23 namespace Rosen {
24 namespace TextEngine {
25 #define MULTIPLE 100
26 #define DIFFERENCE 1
27
28 namespace {
ToSlant(const FontStyle style)29 FontStyles::Slant ToSlant(const FontStyle style)
30 {
31 std::map<FontStyle, FontStyles::Slant> switchMap = {
32 {FontStyle::NORMAL, FontStyles::Slant::UPRIGHT},
33 {FontStyle::ITALIC, FontStyles::Slant::ITALIC}
34 };
35 return switchMap[style];
36 }
37
ToTexgineSlant(const FontStyles::Slant slant)38 TexgineFontStyle::Slant ToTexgineSlant(const FontStyles::Slant slant)
39 {
40 std::map<FontStyles::Slant, TexgineFontStyle::Slant> switchMap = {
41 {FontStyles::Slant::UPRIGHT, TexgineFontStyle::Slant::K_UPRIGHT_SLANT},
42 {FontStyles::Slant::ITALIC, TexgineFontStyle::Slant::K_ITALIC_SLANT},
43 {FontStyles::Slant::OBLIQUE, TexgineFontStyle::Slant::K_OBLIQUE_SLANT},
44 };
45 return switchMap[slant];
46 }
47 } // namespace
48
FontStyles(FontWeight weight,FontStyle style)49 FontStyles::FontStyles(FontWeight weight, FontStyle style)
50 {
51 if (!(0 <= static_cast<int>(weight) && weight < FontWeight::MAX)) {
52 LOGEX_FUNC_LINE(ERROR) << "FontWeight Error!";
53 throw TEXGINE_EXCEPTION(INVALID_ARGUMENT);
54 }
55
56 if (!(0 <= static_cast<int>(style) && style < FontStyle::MAX)) {
57 LOGEX_FUNC_LINE(ERROR) << "FontStyle Error!";
58 throw TEXGINE_EXCEPTION(INVALID_ARGUMENT);
59 }
60
61 slant_ = ToSlant(style);
62 weight_ = static_cast<Weight>(static_cast<int>(weight) + DIFFERENCE);
63 }
64
FontStyles(FontStyles::Weight weight,FontStyles::Width width,FontStyles::Slant slant)65 FontStyles::FontStyles(FontStyles::Weight weight, FontStyles::Width width, FontStyles::Slant slant)
66 {
67 if (!(0 <= static_cast<int>(weight) && weight < Weight::MAX)) {
68 LOGEX_FUNC_LINE(ERROR) << "Weight Error!";
69 throw TEXGINE_EXCEPTION(INVALID_ARGUMENT);
70 }
71
72 if (!(0 <= static_cast<int>(width) && width < Width::MAX)) {
73 LOGEX_FUNC_LINE(ERROR) << "Width Error!";
74 throw TEXGINE_EXCEPTION(INVALID_ARGUMENT);
75 }
76
77 if (!(0 <= static_cast<int>(slant) && slant < FontStyles::Slant::MAX)) {
78 LOGEX_FUNC_LINE(ERROR) << "Slant Error!";
79 throw TEXGINE_EXCEPTION(INVALID_ARGUMENT);
80 }
81
82 weight_ = weight;
83 width_ = width;
84 slant_ = slant;
85 }
86
ToTexgineFontStyle() const87 TexgineFontStyle FontStyles::ToTexgineFontStyle() const
88 {
89 return TexgineFontStyle(static_cast<int>(weight_) * MULTIPLE,
90 static_cast<int>(width_) + DIFFERENCE, ToTexgineSlant(slant_));
91 }
92
operator ==(const FontStyles & rhs) const93 bool FontStyles::operator==(const FontStyles &rhs) const
94 {
95 return weight_ == rhs.weight_ && width_ == rhs.width_ && slant_ == rhs.slant_;
96 }
97
operator !=(const FontStyles & rhs) const98 bool FontStyles::operator!=(const FontStyles &rhs) const
99 {
100 return !(*this == rhs);
101 }
102
operator <(const FontStyles & rhs) const103 bool FontStyles::operator<(const FontStyles& rhs) const
104 {
105 if (weight_ != rhs.weight_) {
106 return weight_ < rhs.weight_;
107 }
108
109 if (width_ != rhs.width_) {
110 return width_ < rhs.width_;
111 }
112 return slant_ < rhs.slant_;
113 }
114 } // namespace TextEngine
115 } // namespace Rosen
116 } // namespace OHOS
117