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 28 FONT_BEGIN_NAMESPACE() 29 using FontSize = float; 30 31 constexpr FontSize DEFAULT_FONT_PT_SIZE = 12.f; 32 constexpr uint16_t DEFAULT_XDPI = 72u; 33 constexpr uint16_t DEFAULT_YDPI = 72u; 34 35 struct GlyphInfo { 36 RENDER_NS::RenderHandleReference atlas; 37 BASE_NS::Math::Vec2 tl; 38 BASE_NS::Math::Vec2 br; 39 }; 40 41 struct GlyphMetrics { 42 float advance; 43 // bounds 44 float left; 45 float top; 46 float right; 47 float bottom; 48 float leftBearing; 49 float topBearing; 50 }; 51 52 struct FontMetrics { 53 float ascent; 54 float descent; 55 /** Line height, distance between two consecutive baselines, to get 56 * the global font height, compute: descent - ascent. 57 */ 58 float height; 59 /** Line Gap, difference between line height and sum of ascender and descender */ 60 float leading; 61 float xHeight; 62 }; 63 64 // clang-format off 65 66 union FontStyle { 67 uint64_t bits; 68 struct { 69 Weight Weight : 10; 70 uint64_t Slant : 7; 71 uint64_t BackSlant : 1; 72 SlantType SlantType : 2; 73 Width Width : 11; 74 uint64_t OpticalSize : 8; 75 uint64_t Pad : 25; 76 }; 77 operator =(uint64_t _bits)78 FontStyle& operator=(uint64_t _bits) 79 { 80 bits = _bits; 81 return *this; 82 } operator uint64_t() const83 inline constexpr operator uint64_t() const 84 { 85 return bits; 86 } operator ==(const FontStyle& style) const87 inline constexpr bool operator==(const FontStyle& style) const 88 { 89 return bits == uint64_t(style); 90 } 91 }; 92 93 static constexpr FontStyle Regular = { 0x73E800190 }; 94 static constexpr FontStyle Bold = { 0x73E8002BC }; 95 static constexpr FontStyle Italic = { 0x73E800190 | (1 << 18) }; 96 static constexpr FontStyle Oblique = { 0x73E800190 | (1 << 19) }; 97 // clang-format on 98 99 /** Fonts interface. 100 * 101 */ 102 class IFont : public CORE_NS::IInterface { 103 public: 104 static constexpr BASE_NS::Uid UID { "138e9acd-3ee1-4b75-a169-c7724f63b061" }; 105 using Ptr = BASE_NS::refcnt_ptr<IFont>; 106 107 /** Set current font size in point units. */ 108 virtual void SetSize(FontSize) = 0; 109 /** Get current font size in point units. */ 110 virtual FontSize GetSize() = 0; 111 112 /** Set current DPI for direct calls to font measure and draw api. */ 113 virtual void SetDpi(uint16_t x, uint16_t y) = 0; 114 /** Get current DPI of this font.*/ 115 virtual void GetDpi(uint16_t& x, uint16_t& y) = 0; 116 117 /** Get metrics of the font. */ 118 virtual FontMetrics GetMetrics() = 0; 119 /** Get metrics of the glyph at given index. */ 120 virtual GlyphMetrics GetGlyphMetrics(uint32_t glyphIndex) = 0; 121 virtual GlyphInfo GetGlyphInfo(uint32_t glyphIndex) = 0; 122 123 /** Convert character code to glyph index. 124 * @return glyph's index or 0 if requested glyph is not found. 125 */ 126 virtual uint32_t GetGlyphIndex(uint32_t codepoint) = 0; 127 128 /** This function returns dimensions of UTF-8 string by positioning glyphs based on their advances and kerning (if 129 * kerning is supported by font. 130 */ 131 virtual BASE_NS::Math::Vec2 MeasureString(const BASE_NS::string_view) = 0; 132 133 virtual BASE_NS::array_view<uint8_t> GetFontData() = 0; 134 135 protected: 136 IFont() = default; 137 virtual ~IFont() = default; 138 IFont(const IFont&) = delete; 139 IFont& operator=(const IFont&) = delete; 140 }; 141 FONT_END_NAMESPACE() 142 #endif // API_FONT_IFONT_H 143