• 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 UTIL__FONT_H
17 #define UTIL__FONT_H
18 
19 #include <atomic>
20 #include <memory>
21 #include <base/containers/vector.h>
22 
23 #include <font/intf_font_manager.h>
24 
25 #include "font_defs.h"
26 
27 FONT_BEGIN_NAMESPACE()
28 class FaceData;
29 class FontData;
30 
31 class Font final : public IFont {
32 public:
33     // interface API
34     explicit Font(std::shared_ptr<FaceData>&& faceData);
35     ~Font() = default;
36 
37     // IInterface
38     const CORE_NS::IInterface* GetInterface(const BASE_NS::Uid& uid) const override;
39     CORE_NS::IInterface* GetInterface(const BASE_NS::Uid& uid) override;
40     void Ref() override;
41     void Unref() override;
42 
43     // IFont
44     // set current size in Points for all consequent font operations
45     void SetSize(FontSize sizeInPt) override;
46     // get current font size in Points
47     FontSize GetSize() override;
48     /** Set current font method */
49     void SetMethod(FontMethod) override;
50     /** Get current font method */
51     FontMethod GetMethod() override;
52 
53     void SetDpi(uint16_t x, uint16_t y) override;
54     void GetDpi(uint16_t& x, uint16_t& y) override;
55 
56     BASE_NS::array_view<uint8_t> GetFontData() override;
57 
58     FontMetrics GetMetrics() override;
59     GlyphMetrics GetGlyphMetrics(uint32_t glyphIndex) override;
60     GlyphInfo GetGlyphInfo(uint32_t glyphIndex) override;
61     BASE_NS::vector<GlyphContour> GetGlyphContours(uint32_t glyphIndex) override;
62 
63     uint32_t GetGlyphIndex(uint32_t codepoint) override;
64 
65     // measure string dimension as if text would have been drawn by DrawString
66     BASE_NS::Math::Vec2 MeasureString(const BASE_NS::string_view) override;
67 
68     // implementation specific API
69     void DrawGlyphs(BASE_NS::array_view<const GlyphInfo>, const FontDefs::RenderData&);
70 
71     // This function draws UTF-8 string by positioning corresponding glyphs
72     // based on their advances and kerning (if kerning is supported by font).
73     // NOTE: this function does not perform any font shaping nor guarantees
74     // accurate glyphs positioning.
75     void DrawString(const BASE_NS::string_view, const FontDefs::RenderData&);
76 
77 private:
78     std::atomic_uint32_t refcnt_ { 0 };
79 
80     std::shared_ptr<FaceData> faceData_;
81     FontData* data_ { nullptr }; // FontData is owned by FaceData
82     FontMethod fontMethod_ { FontMethod::RASTER };
83     FontSize fontSize_ { DEFAULT_FONT_PT_SIZE }; // in PT
84     uint16_t xDpi_ = DEFAULT_XDPI;
85     uint16_t yDpi_ = DEFAULT_YDPI;
86 
87     FontData* GetData();
88 };
89 FONT_END_NAMESPACE()
90 #endif
91