• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef API_FONT_IFONT_H
17 #define API_FONT_IFONT_H
18 
19 #include <cstdint>
20 
21 #include <base/containers/array_view.h>
22 #include <base/containers/refcnt_ptr.h>
23 #include <base/math/vector.h>
24 #include <core/plugin/intf_interface.h>
25 #include <font/namespace.h>
26 #include <render/resource_handle.h>
27 #include <base/containers/vector.h>
28 
29 FONT_BEGIN_NAMESPACE()
30 using FontSize = float;
31 
32 enum class FontMethod { RASTER = 0, SDF = 1, TEXT3D = 2 };
33 
34 constexpr FontSize DEFAULT_FONT_PT_SIZE = 12.f;
35 constexpr uint16_t DEFAULT_XDPI = 72u;
36 constexpr uint16_t DEFAULT_YDPI = 72u;
37 
38 struct GlyphInfo {
39     RENDER_NS::RenderHandleReference atlas;
40     BASE_NS::Math::Vec2 tl;
41     BASE_NS::Math::Vec2 br;
42 };
43 
44 struct GlyphMetrics {
45     float advance;
46     // bounds
47     float left;
48     float top;
49     float right;
50     float bottom;
51     float leftBearing;
52     float topBearing;
53 };
54 
55 struct FontMetrics {
56     float ascent;
57     float descent;
58     /** Line height, distance between two consecutive baselines, to get
59      * the global font height, compute: descent - ascent.
60      */
61     float height;
62     /** Line Gap, difference between line height and sum of ascender and descender */
63     float leading;
64     float x_height;
65 };
66 
67 struct GlyphContour {
68     BASE_NS::vector<BASE_NS::Math::Vec2> points;
69 };
70 
71 /** Fonts interface.
72  *
73  */
74 class IFont : public CORE_NS::IInterface {
75 public:
76     static constexpr BASE_NS::Uid UID { "138e9acd-3ee1-4b75-a169-c7724f63b061" };
77     using Ptr = BASE_NS::refcnt_ptr<IFont>;
78 
79     /** Set current font size in point units. */
80     virtual void SetSize(FontSize) = 0;
81     /** Get current font size in point units. */
82     virtual FontSize GetSize() = 0;
83 
84     /** Set current font method. */
85     virtual void SetMethod(FontMethod) = 0;
86     /** Get current font method. */
87     virtual FontMethod GetMethod() = 0;
88 
89     /** Set current DPI for direct calls to font measure and draw api. */
90     virtual void SetDpi(uint16_t x, uint16_t y) = 0;
91     /** Get current DPI of this font.*/
92     virtual void GetDpi(uint16_t& x, uint16_t& y) = 0;
93 
94     /** Get metrics of the font. */
95     virtual FontMetrics GetMetrics() = 0;
96     /** Get metrics of the glyph at given index. */
97     virtual GlyphMetrics GetGlyphMetrics(uint32_t glyphIndex) = 0;
98     virtual GlyphInfo GetGlyphInfo(uint32_t glyphIndex) = 0;
99     virtual BASE_NS::vector<GlyphContour> GetGlyphContours(uint32_t glyphIndex) = 0;
100 
101     /** Convert character code to glyph index.
102      * @return glyph's index or 0 if requested glyph is not found.
103      */
104     virtual uint32_t GetGlyphIndex(uint32_t codepoint) = 0;
105 
106     /** This function returns dimensions of UTF-8 string by positioning glyphs based on their advances and kerning (if
107      * kerning is supported by font.
108      */
109     virtual BASE_NS::Math::Vec2 MeasureString(const BASE_NS::string_view) = 0;
110 
111     virtual BASE_NS::array_view<uint8_t> GetFontData() = 0;
112 
113 protected:
114     IFont() = default;
115     virtual ~IFont() = default;
116     IFont(const IFont&) = delete;
117     IFont& operator=(const IFont&) = delete;
118 };
119 FONT_END_NAMESPACE()
120 #endif // API_FONT_IFONT_H
121