18bf80f4bSopenharmony_ci/*
28bf80f4bSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
38bf80f4bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
48bf80f4bSopenharmony_ci * you may not use this file except in compliance with the License.
58bf80f4bSopenharmony_ci * You may obtain a copy of the License at
68bf80f4bSopenharmony_ci *
78bf80f4bSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
88bf80f4bSopenharmony_ci *
98bf80f4bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
108bf80f4bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
118bf80f4bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
128bf80f4bSopenharmony_ci * See the License for the specific language governing permissions and
138bf80f4bSopenharmony_ci * limitations under the License.
148bf80f4bSopenharmony_ci */
158bf80f4bSopenharmony_ci
168bf80f4bSopenharmony_ci#ifndef API_FONT_IFONT_H
178bf80f4bSopenharmony_ci#define API_FONT_IFONT_H
188bf80f4bSopenharmony_ci
198bf80f4bSopenharmony_ci#include <cstdint>
208bf80f4bSopenharmony_ci
218bf80f4bSopenharmony_ci#include <base/containers/array_view.h>
228bf80f4bSopenharmony_ci#include <base/containers/refcnt_ptr.h>
238bf80f4bSopenharmony_ci#include <base/math/vector.h>
248bf80f4bSopenharmony_ci#include <core/plugin/intf_interface.h>
258bf80f4bSopenharmony_ci#include <font/namespace.h>
268bf80f4bSopenharmony_ci#include <render/resource_handle.h>
278bf80f4bSopenharmony_ci
288bf80f4bSopenharmony_ciFONT_BEGIN_NAMESPACE()
298bf80f4bSopenharmony_ciusing FontSize = float;
308bf80f4bSopenharmony_ci
318bf80f4bSopenharmony_ciconstexpr FontSize DEFAULT_FONT_PT_SIZE = 12.f;
328bf80f4bSopenharmony_ciconstexpr uint16_t DEFAULT_XDPI = 72u;
338bf80f4bSopenharmony_ciconstexpr uint16_t DEFAULT_YDPI = 72u;
348bf80f4bSopenharmony_ci
358bf80f4bSopenharmony_cistruct GlyphInfo {
368bf80f4bSopenharmony_ci    RENDER_NS::RenderHandleReference atlas;
378bf80f4bSopenharmony_ci    BASE_NS::Math::Vec2 tl;
388bf80f4bSopenharmony_ci    BASE_NS::Math::Vec2 br;
398bf80f4bSopenharmony_ci};
408bf80f4bSopenharmony_ci
418bf80f4bSopenharmony_cistruct GlyphMetrics {
428bf80f4bSopenharmony_ci    float advance;
438bf80f4bSopenharmony_ci    // bounds
448bf80f4bSopenharmony_ci    float left;
458bf80f4bSopenharmony_ci    float top;
468bf80f4bSopenharmony_ci    float right;
478bf80f4bSopenharmony_ci    float bottom;
488bf80f4bSopenharmony_ci    float leftBearing;
498bf80f4bSopenharmony_ci    float topBearing;
508bf80f4bSopenharmony_ci};
518bf80f4bSopenharmony_ci
528bf80f4bSopenharmony_cistruct FontMetrics {
538bf80f4bSopenharmony_ci    float ascent;
548bf80f4bSopenharmony_ci    float descent;
558bf80f4bSopenharmony_ci    /** Line height, distance between two consecutive baselines, to get
568bf80f4bSopenharmony_ci     * the global font height, compute: descent - ascent.
578bf80f4bSopenharmony_ci     */
588bf80f4bSopenharmony_ci    float height;
598bf80f4bSopenharmony_ci    /** Line Gap, difference between line height and sum of ascender and descender */
608bf80f4bSopenharmony_ci    float leading;
618bf80f4bSopenharmony_ci    float xHeight;
628bf80f4bSopenharmony_ci};
638bf80f4bSopenharmony_ci
648bf80f4bSopenharmony_ci// clang-format off
658bf80f4bSopenharmony_ci
668bf80f4bSopenharmony_ciunion FontStyle {
678bf80f4bSopenharmony_ci    uint64_t bits;
688bf80f4bSopenharmony_ci    struct {
698bf80f4bSopenharmony_ci        Weight      Weight      : 10;
708bf80f4bSopenharmony_ci        uint64_t    Slant       : 7;
718bf80f4bSopenharmony_ci        uint64_t    BackSlant   : 1;
728bf80f4bSopenharmony_ci        SlantType   SlantType   : 2;
738bf80f4bSopenharmony_ci        Width       Width       : 11;
748bf80f4bSopenharmony_ci        uint64_t    OpticalSize : 8;
758bf80f4bSopenharmony_ci        uint64_t    Pad         : 25;
768bf80f4bSopenharmony_ci    };
778bf80f4bSopenharmony_ci
788bf80f4bSopenharmony_ci    FontStyle& operator=(uint64_t _bits)
798bf80f4bSopenharmony_ci    {
808bf80f4bSopenharmony_ci        bits = _bits;
818bf80f4bSopenharmony_ci        return *this;
828bf80f4bSopenharmony_ci    }
838bf80f4bSopenharmony_ci    inline constexpr operator uint64_t() const
848bf80f4bSopenharmony_ci    {
858bf80f4bSopenharmony_ci        return bits;
868bf80f4bSopenharmony_ci    }
878bf80f4bSopenharmony_ci    inline constexpr bool operator==(const FontStyle& style) const
888bf80f4bSopenharmony_ci    {
898bf80f4bSopenharmony_ci        return bits == uint64_t(style);
908bf80f4bSopenharmony_ci    }
918bf80f4bSopenharmony_ci};
928bf80f4bSopenharmony_ci
938bf80f4bSopenharmony_cistatic constexpr FontStyle Regular  = { 0x73E800190 };
948bf80f4bSopenharmony_cistatic constexpr FontStyle Bold     = { 0x73E8002BC };
958bf80f4bSopenharmony_cistatic constexpr FontStyle Italic   = { 0x73E800190 | (1 << 18) };
968bf80f4bSopenharmony_cistatic constexpr FontStyle Oblique  = { 0x73E800190 | (1 << 19) };
978bf80f4bSopenharmony_ci// clang-format on
988bf80f4bSopenharmony_ci
998bf80f4bSopenharmony_ci/** Fonts interface.
1008bf80f4bSopenharmony_ci *
1018bf80f4bSopenharmony_ci */
1028bf80f4bSopenharmony_ciclass IFont : public CORE_NS::IInterface {
1038bf80f4bSopenharmony_cipublic:
1048bf80f4bSopenharmony_ci    static constexpr BASE_NS::Uid UID { "138e9acd-3ee1-4b75-a169-c7724f63b061" };
1058bf80f4bSopenharmony_ci    using Ptr = BASE_NS::refcnt_ptr<IFont>;
1068bf80f4bSopenharmony_ci
1078bf80f4bSopenharmony_ci    /** Set current font size in point units. */
1088bf80f4bSopenharmony_ci    virtual void SetSize(FontSize) = 0;
1098bf80f4bSopenharmony_ci    /** Get current font size in point units. */
1108bf80f4bSopenharmony_ci    virtual FontSize GetSize() = 0;
1118bf80f4bSopenharmony_ci
1128bf80f4bSopenharmony_ci    /** Set current DPI for direct calls to font measure and draw api. */
1138bf80f4bSopenharmony_ci    virtual void SetDpi(uint16_t x, uint16_t y) = 0;
1148bf80f4bSopenharmony_ci    /** Get current DPI of this font.*/
1158bf80f4bSopenharmony_ci    virtual void GetDpi(uint16_t& x, uint16_t& y) = 0;
1168bf80f4bSopenharmony_ci
1178bf80f4bSopenharmony_ci    /** Get metrics of the font. */
1188bf80f4bSopenharmony_ci    virtual FontMetrics GetMetrics() = 0;
1198bf80f4bSopenharmony_ci    /** Get metrics of the glyph at given index. */
1208bf80f4bSopenharmony_ci    virtual GlyphMetrics GetGlyphMetrics(uint32_t glyphIndex) = 0;
1218bf80f4bSopenharmony_ci    virtual GlyphInfo GetGlyphInfo(uint32_t glyphIndex) = 0;
1228bf80f4bSopenharmony_ci
1238bf80f4bSopenharmony_ci    /** Convert character code to glyph index.
1248bf80f4bSopenharmony_ci     * @return glyph's index or 0 if requested glyph is not found.
1258bf80f4bSopenharmony_ci     */
1268bf80f4bSopenharmony_ci    virtual uint32_t GetGlyphIndex(uint32_t codepoint) = 0;
1278bf80f4bSopenharmony_ci
1288bf80f4bSopenharmony_ci    /** This function returns dimensions of UTF-8 string by positioning glyphs based on their advances and kerning (if
1298bf80f4bSopenharmony_ci     * kerning is supported by font.
1308bf80f4bSopenharmony_ci     */
1318bf80f4bSopenharmony_ci    virtual BASE_NS::Math::Vec2 MeasureString(const BASE_NS::string_view) = 0;
1328bf80f4bSopenharmony_ci
1338bf80f4bSopenharmony_ci    virtual BASE_NS::array_view<uint8_t> GetFontData() = 0;
1348bf80f4bSopenharmony_ci
1358bf80f4bSopenharmony_ciprotected:
1368bf80f4bSopenharmony_ci    IFont() = default;
1378bf80f4bSopenharmony_ci    virtual ~IFont() = default;
1388bf80f4bSopenharmony_ci    IFont(const IFont&) = delete;
1398bf80f4bSopenharmony_ci    IFont& operator=(const IFont&) = delete;
1408bf80f4bSopenharmony_ci};
1418bf80f4bSopenharmony_ciFONT_END_NAMESPACE()
1428bf80f4bSopenharmony_ci#endif // API_FONT_IFONT_H
143