1 /*
2 * Copyright (c) 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 "font.h"
17
18 #include <cfloat>
19
20 #include "face_data.h"
21 #include "font_buffer.h"
22 #include "font_data.h"
23
24 using namespace BASE_NS;
25
FONT_BEGIN_NAMESPACE()26 FONT_BEGIN_NAMESPACE()
27 Font::Font(std::shared_ptr<FaceData>&& faceData) : faceData_(faceData) {}
28
SetSize(FontSize sizeInPt)29 void Font::SetSize(FontSize sizeInPt)
30 {
31 this->fontSize_ = sizeInPt;
32 this->data_ = nullptr;
33 }
34
GetSize()35 FontSize Font::GetSize()
36 {
37 return fontSize_;
38 }
39
SetMethod(FontMethod method)40 void Font::SetMethod(FontMethod method)
41 {
42 this->fontMethod_ = method;
43 this->data_ = nullptr;
44 }
45
GetMethod()46 FontMethod Font::GetMethod()
47 {
48 return fontMethod_;
49 }
50
SetDpi(uint16_t x,uint16_t y)51 void Font::SetDpi(uint16_t x, uint16_t y)
52 {
53 xDpi_ = x;
54 yDpi_ = y;
55 this->data_ = nullptr;
56 }
GetDpi(uint16_t & x,uint16_t & y)57 void Font::GetDpi(uint16_t& x, uint16_t& y)
58 {
59 x = xDpi_;
60 y = yDpi_;
61 }
62
GetData()63 FontData* Font::GetData()
64 {
65 if (!data_) {
66 data_ = faceData_->CreateFontData(fontSize_, xDpi_, yDpi_, fontMethod_ == FontMethod::SDF ? true : false);
67 }
68 return data_;
69 }
70
GetFontData()71 BASE_NS::array_view<uint8_t> Font::GetFontData()
72 {
73 return faceData_->fontBuffer_->bytes_;
74 }
75
DrawGlyphs(BASE_NS::array_view<const GlyphInfo> glyphs,const FontDefs::RenderData & renderData)76 void Font::DrawGlyphs(BASE_NS::array_view<const GlyphInfo> glyphs, const FontDefs::RenderData& renderData)
77 {
78 }
79
DrawString(const BASE_NS::string_view string,const FontDefs::RenderData & renderData)80 void Font::DrawString(const BASE_NS::string_view string, const FontDefs::RenderData& renderData)
81 {
82 }
83
MeasureString(const BASE_NS::string_view string)84 BASE_NS::Math::Vec2 Font::MeasureString(const BASE_NS::string_view string)
85 {
86 if (auto data = GetData()) {
87 return data->MeasureString(string);
88 }
89 return {};
90 }
91
GetMetrics()92 FontMetrics Font::GetMetrics()
93 {
94 if (auto data = GetData()) {
95 return data->GetMetrics();
96 }
97 return {};
98 }
99
GetGlyphMetrics(uint32_t glyphIndex)100 GlyphMetrics Font::GetGlyphMetrics(uint32_t glyphIndex)
101 {
102 if (auto data = GetData()) {
103 return data->GetGlyphMetrics(glyphIndex);
104 }
105 return {};
106 }
107
GetGlyphContours(uint32_t glyphIndex)108 BASE_NS::vector<GlyphContour> Font::GetGlyphContours(uint32_t glyphIndex)
109 {
110 if (auto data = GetData()) {
111 return data->GetGlyphContours(glyphIndex);
112 }
113 return {};
114 }
115
GetGlyphInfo(uint32_t glyphIndex)116 GlyphInfo Font::GetGlyphInfo(uint32_t glyphIndex)
117 {
118 if (auto data = GetData()) {
119 return data->GetGlyphInfo(glyphIndex);
120 }
121 return {};
122 }
123
GetGlyphIndex(uint32_t code)124 uint32_t Font::GetGlyphIndex(uint32_t code)
125 {
126 return faceData_->GetGlyphIndex(code);
127 }
128
GetInterface(const BASE_NS::Uid & uid) const129 const CORE_NS::IInterface* Font::GetInterface(const BASE_NS::Uid& uid) const
130 {
131 if (uid == CORE_NS::IInterface::UID) {
132 return this;
133 }
134 if (uid == IFont::UID) {
135 return this;
136 }
137 return nullptr;
138 }
139
GetInterface(const BASE_NS::Uid & uid)140 CORE_NS::IInterface* Font::GetInterface(const BASE_NS::Uid& uid)
141 {
142 if (uid == CORE_NS::IInterface::UID) {
143 return this;
144 }
145 if (uid == IFont::UID) {
146 return this;
147 }
148 return nullptr;
149 }
150
Ref()151 void Font::Ref()
152 {
153 refcnt_.fetch_add(1, std::memory_order_relaxed);
154 }
155
Unref()156 void Font::Unref()
157 {
158 if (refcnt_.fetch_sub(1, std::memory_order_release) == 1) {
159 std::atomic_thread_fence(std::memory_order_acquire);
160 delete this;
161 }
162 }
163
164 FONT_END_NAMESPACE()
165