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