1 /*
2  * Copyright (c) 2023-2024 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 "text/font.h"
17 
18 #include "impl_factory.h"
19 #include "impl_interface/font_impl.h"
20 #include "text/font_mgr.h"
21 #include "utils/log.h"
22 
23 namespace OHOS {
24 namespace Rosen {
25 namespace Drawing {
Font()26 Font::Font() : fontImpl_(ImplFactory::CreateFontImpl()) {}
27 
Font(std::shared_ptr<Typeface> typeface,scalar size,scalar scaleX,scalar skewX)28 Font::Font(std::shared_ptr<Typeface> typeface, scalar size, scalar scaleX, scalar skewX)
29     : fontImpl_(ImplFactory::CreateFontImpl(typeface, size, scaleX, skewX)) {}
30 
Font(const Font & font)31 Font::Font(const Font& font) : fontImpl_(ImplFactory::CreateFontImpl(font)) {}
32 
SetEdging(FontEdging edging)33 void Font::SetEdging(FontEdging edging)
34 {
35     fontImpl_->SetEdging(edging);
36 }
37 
SetBaselineSnap(bool baselineSnap)38 void Font::SetBaselineSnap(bool baselineSnap)
39 {
40     fontImpl_->SetBaselineSnap(baselineSnap);
41 }
42 
SetForceAutoHinting(bool isForceAutoHinting)43 void Font::SetForceAutoHinting(bool isForceAutoHinting)
44 {
45     fontImpl_->SetForceAutoHinting(isForceAutoHinting);
46 }
47 
SetSubpixel(bool isSubpixel)48 void Font::SetSubpixel(bool isSubpixel)
49 {
50     fontImpl_->SetSubpixel(isSubpixel);
51 }
52 
SetHinting(FontHinting hintingLevel)53 void Font::SetHinting(FontHinting hintingLevel)
54 {
55     fontImpl_->SetHinting(hintingLevel);
56 }
57 
SetEmbeddedBitmaps(bool embeddedBitmaps)58 void Font::SetEmbeddedBitmaps(bool embeddedBitmaps)
59 {
60     fontImpl_->SetEmbeddedBitmaps(embeddedBitmaps);
61 }
62 
SetTypeface(std::shared_ptr<Typeface> typeface)63 void Font::SetTypeface(std::shared_ptr<Typeface> typeface)
64 {
65     fontImpl_->SetTypeface(typeface);
66 }
67 
SetSize(scalar textSize)68 void Font::SetSize(scalar textSize)
69 {
70     fontImpl_->SetSize(textSize);
71 }
72 
SetEmbolden(bool isEmbolden)73 void Font::SetEmbolden(bool isEmbolden)
74 {
75     fontImpl_->SetEmbolden(isEmbolden);
76 }
77 
SetScaleX(scalar scaleX)78 void Font::SetScaleX(scalar scaleX)
79 {
80     fontImpl_->SetScaleX(scaleX);
81 }
82 
SetSkewX(scalar skewX)83 void Font::SetSkewX(scalar skewX)
84 {
85     fontImpl_->SetSkewX(skewX);
86 }
87 
SetLinearMetrics(bool isLinearMetrics)88 void Font::SetLinearMetrics(bool isLinearMetrics)
89 {
90     fontImpl_->SetLinearMetrics(isLinearMetrics);
91 }
92 
GetMetrics(FontMetrics * metrics) const93 scalar Font::GetMetrics(FontMetrics* metrics) const
94 {
95     return fontImpl_->GetMetrics(metrics);
96 }
97 
GetWidths(const uint16_t glyphs[],int count,scalar widths[]) const98 void Font::GetWidths(const uint16_t glyphs[], int count, scalar widths[]) const
99 {
100     fontImpl_->GetWidths(glyphs, count, widths);
101 }
102 
GetWidths(const uint16_t glyphs[],int count,scalar widths[],Rect bounds[]) const103 void Font::GetWidths(const uint16_t glyphs[], int count, scalar widths[], Rect bounds[]) const
104 {
105     fontImpl_->GetWidths(glyphs, count, widths, bounds);
106 }
107 
GetSize() const108 scalar Font::GetSize() const
109 {
110     return fontImpl_->GetSize();
111 }
112 
GetTypeface() const113 std::shared_ptr<Typeface> Font::GetTypeface() const
114 {
115     return fontImpl_->GetTypeface();
116 }
117 
GetEdging() const118 FontEdging Font::GetEdging() const
119 {
120     return fontImpl_->GetEdging();
121 }
122 
GetHinting() const123 FontHinting Font::GetHinting() const
124 {
125     return fontImpl_->GetHinting();
126 }
127 
IsEmbeddedBitmaps() const128 bool Font::IsEmbeddedBitmaps() const
129 {
130     return fontImpl_->IsEmbeddedBitmaps();
131 }
132 
GetScaleX() const133 scalar Font::GetScaleX() const
134 {
135     return fontImpl_->GetScaleX();
136 }
137 
GetSkewX() const138 scalar Font::GetSkewX() const
139 {
140     return fontImpl_->GetSkewX();
141 }
142 
IsBaselineSnap() const143 bool Font::IsBaselineSnap() const
144 {
145     return fontImpl_->IsBaselineSnap();
146 }
147 
IsForceAutoHinting() const148 bool Font::IsForceAutoHinting() const
149 {
150     return fontImpl_->IsForceAutoHinting();
151 }
152 
IsSubpixel() const153 bool Font::IsSubpixel() const
154 {
155     return fontImpl_->IsSubpixel();
156 }
157 
IsLinearMetrics() const158 bool Font::IsLinearMetrics() const
159 {
160     return fontImpl_->IsLinearMetrics();
161 }
162 
IsEmbolden() const163 bool Font::IsEmbolden() const
164 {
165     return fontImpl_->IsEmbolden();
166 }
167 
UnicharToGlyph(int32_t uni) const168 uint16_t Font::UnicharToGlyph(int32_t uni) const
169 {
170     return fontImpl_->UnicharToGlyph(uni);
171 }
172 
TextToGlyphs(const void * text,size_t byteLength,TextEncoding encoding,uint16_t glyphs[],int maxGlyphCount) const173 int Font::TextToGlyphs(const void* text, size_t byteLength, TextEncoding encoding,
174     uint16_t glyphs[], int maxGlyphCount) const
175 {
176     return fontImpl_->TextToGlyphs(text, byteLength, encoding, glyphs, maxGlyphCount);
177 }
178 
MeasureText(const void * text,size_t byteLength,TextEncoding encoding,Rect * bounds) const179 scalar Font::MeasureText(const void* text, size_t byteLength, TextEncoding encoding, Rect* bounds) const
180 {
181     return fontImpl_->MeasureText(text, byteLength, encoding, bounds);
182 }
183 
MeasureSingleCharacter(int32_t unicode) const184 scalar Font::MeasureSingleCharacter(int32_t unicode) const
185 {
186     scalar textWidth = 0.0f;
187     uint16_t glyph = UnicharToGlyph(unicode);
188     if (glyph != 0) {
189         textWidth = MeasureText(&glyph, sizeof(uint16_t), TextEncoding::GLYPH_ID);
190     } else {
191         std::shared_ptr<Font> fallbackFont = GetFallbackFont(unicode);
192         if (fallbackFont) {
193             uint16_t fallbackGlyph = fallbackFont->UnicharToGlyph(unicode);
194             textWidth = fallbackFont->MeasureText(&fallbackGlyph, sizeof(uint16_t), TextEncoding::GLYPH_ID);
195         }
196     }
197     return textWidth;
198 }
199 
GetFallbackFont(int32_t unicode) const200 std::shared_ptr<Font> Font::GetFallbackFont(int32_t unicode) const
201 {
202     std::shared_ptr<FontMgr> fontMgr = FontMgr::CreateDefaultFontMgr();
203     if (fontMgr == nullptr) {
204         LOGE("Font::GetFallbackFont, default fontMgr is nullptr.");
205         return nullptr;
206     }
207     std::shared_ptr<Typeface> currentTypeface = GetTypeface();
208     std::shared_ptr<Typeface> fallbackTypeface = nullptr;
209     if (currentTypeface) {
210         fallbackTypeface = std::shared_ptr<Typeface>(fontMgr->MatchFamilyStyleCharacter(nullptr,
211             currentTypeface->GetFontStyle(), nullptr, 0, unicode < 0 ? 0xFFFD : unicode));
212     } else {
213         std::shared_ptr<Typeface> defaultTypeface = Typeface::MakeDefault();
214         fallbackTypeface = std::shared_ptr<Typeface>(fontMgr->MatchFamilyStyleCharacter(nullptr, defaultTypeface
215             ? defaultTypeface->GetFontStyle() : FontStyle(), nullptr, 0, unicode < 0 ? 0xFFFD : unicode));
216     }
217     if (fallbackTypeface == nullptr) {
218         LOGE("Font::GetFallbackFont, fallback typeface is nullptr.");
219         return nullptr;
220     }
221     std::shared_ptr<Font> fallbackFont = std::make_shared<Font>(*this);
222     fallbackFont->SetTypeface(fallbackTypeface);
223     return fallbackFont;
224 }
225 
CountText(const void * text,size_t byteLength,TextEncoding encoding) const226 int Font::CountText(const void* text, size_t byteLength, TextEncoding encoding) const
227 {
228     return fontImpl_->CountText(text, byteLength, encoding);
229 }
230 } // namespace Drawing
231 } // namespace Rosen
232 } // namespace OHOS
233