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
26 FONT_BEGIN_NAMESPACE()
Font(std::shared_ptr<FaceData>&& faceData)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
SetDpi(uint16_t x, uint16_t y)40 void Font::SetDpi(uint16_t x, uint16_t y)
41 {
42 xDpi_ = x;
43 yDpi_ = y;
44 this->data_ = nullptr;
45 }
GetDpi(uint16_t& x, uint16_t& y)46 void Font::GetDpi(uint16_t& x, uint16_t& y)
47 {
48 x = xDpi_;
49 y = yDpi_;
50 }
51
GetData()52 FontData* Font::GetData()
53 {
54 if (!data_) {
55 data_ = faceData_->CreateFontData(fontSize_, xDpi_, yDpi_);
56 }
57 return data_;
58 }
59
GetFontData()60 BASE_NS::array_view<uint8_t> Font::GetFontData()
61 {
62 return faceData_->fontBuffer_->bytes_;
63 }
64
DrawGlyphs(BASE_NS::array_view<const GlyphInfo> glyphs, const FontDefs::RenderData& renderData)65 void Font::DrawGlyphs(BASE_NS::array_view<const GlyphInfo> glyphs, const FontDefs::RenderData& renderData)
66 {
67 GetData()->DrawGlyphs(glyphs, renderData);
68 }
69
DrawString(const BASE_NS::string_view string, const FontDefs::RenderData& renderData)70 void Font::DrawString(const BASE_NS::string_view string, const FontDefs::RenderData& renderData)
71 {
72 GetData()->DrawString(string, renderData);
73 }
74
MeasureString(const BASE_NS::string_view string)75 BASE_NS::Math::Vec2 Font::MeasureString(const BASE_NS::string_view string)
76 {
77 return GetData()->MeasureString(string);
78 }
79
GetMetrics()80 FontMetrics Font::GetMetrics()
81 {
82 return GetData()->GetMetrics();
83 }
84
GetGlyphMetrics(uint32_t glyphIndex)85 GlyphMetrics Font::GetGlyphMetrics(uint32_t glyphIndex)
86 {
87 return GetData()->GetGlyphMetrics(glyphIndex);
88 }
89
GetGlyphInfo(uint32_t glyphIndex)90 GlyphInfo Font::GetGlyphInfo(uint32_t glyphIndex)
91 {
92 return GetData()->GetGlyphInfo(glyphIndex);
93 }
94
GetGlyphIndex(uint32_t code)95 uint32_t Font::GetGlyphIndex(uint32_t code)
96 {
97 return faceData_->GetGlyphIndex(code);
98 }
99
GetInterface(const BASE_NS::Uid& uid) const100 const CORE_NS::IInterface* Font::GetInterface(const BASE_NS::Uid& uid) const
101 {
102 if (uid == CORE_NS::IInterface::UID) {
103 return this;
104 }
105 if (uid == IFont::UID) {
106 return this;
107 }
108 return nullptr;
109 }
110
GetInterface(const BASE_NS::Uid& uid)111 CORE_NS::IInterface* Font::GetInterface(const BASE_NS::Uid& uid)
112 {
113 if (uid == CORE_NS::IInterface::UID) {
114 return this;
115 }
116 if (uid == IFont::UID) {
117 return this;
118 }
119 return nullptr;
120 }
121
Ref()122 void Font::Ref()
123 {
124 refcnt_.fetch_add(1, std::memory_order_relaxed);
125 }
126
Unref()127 void Font::Unref()
128 {
129 if (refcnt_.fetch_sub(1, std::memory_order_release) == 1) {
130 std::atomic_thread_fence(std::memory_order_acquire);
131 delete this;
132 }
133 }
134
135 FONT_END_NAMESPACE()
136