1// Copyright (c) 2023 Huawei Device Co., Ltd. All rights reserved
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef FONTINFO_OHOS_H
6#define FONTINFO_OHOS_H
7
8#include "securec.h"
9
10#include "SkFixed.h"
11#include "SkFontDescriptor.h"
12#include "SkFontHost_FreeType_common.h"
13
14/*!
15 * \brief To manage the font information
16 */
17struct FontInfo {
18public:
19    /*! Constructor
20     *
21     */
22    FontInfo() : familyName(""), fname(""), index(0),
23        style(SkFontStyle::Normal()), isFixedWidth(false), stream(nullptr)
24    {
25        memset_s(&axisSet, sizeof(AxisSet), 0, sizeof(AxisSet));
26    }
27    /*! Copy Constructor
28     * \param font an object of FontInfo
29     */
30    explicit FontInfo(const FontInfo& font)
31        : familyName(font.familyName), fname(font.fname), index(font.index),
32          style(font.style), isFixedWidth(font.isFixedWidth), stream(nullptr)
33    {
34        axisSet.axis = font.axisSet.axis;
35        axisSet.range = font.axisSet.range;
36        if (font.stream) {
37            stream = font.stream->duplicate();
38        }
39    }
40
41    /*! Move Constructor
42     * \param font an object of FontInfo
43     */
44    explicit FontInfo(FontInfo&& font)
45        : familyName(std::move(font.familyName)), fname(std::move(font.fname)), index(font.index),
46          style(font.style), isFixedWidth(font.isFixedWidth), stream(nullptr)
47    {
48        axisSet.axis = std::move(font.axisSet.axis);
49        axisSet.range = std::move(font.axisSet.range);
50        if (font.stream) {
51            stream = std::move(font.stream);
52        }
53    }
54
55    /*! Constructor
56     * \param fname the fullname of font file
57     * \param index the index of the typeface in the font file
58     */
59    FontInfo(const char* fname, int index)
60        : familyName(""), fname(""), index(index),
61          style(SkFontStyle::Normal()), isFixedWidth(false), stream(nullptr)
62    {
63        if (fname) {
64            this->fname.set(fname);
65        }
66        memset_s(&axisSet, sizeof(axisSet), 0, sizeof(axisSet));
67    }
68
69    /*! Destructor
70     *
71     */
72    virtual ~FontInfo() = default;
73
74    /*! Copy assignment operator
75     * \param font an object of FontInfo
76     */
77    FontInfo& operator = (const FontInfo& font)
78    {
79        if (this == &font) {
80            return *this;
81        }
82        familyName = font.familyName;
83        fname = font.fname;
84        index = font.index;
85        style = font.style;
86        isFixedWidth = font.isFixedWidth;
87        axisSet.axis = font.axisSet.axis;
88        axisSet.range = font.axisSet.range;
89        if (font.stream) {
90            stream = font.stream->duplicate();
91        }
92        return *this;
93    }
94
95    /*! The move assignment operator
96     * \param font an object of FontInfo
97     */
98    FontInfo& operator = (FontInfo&& font)
99    {
100        if (this == &font) {
101            return *this;
102        }
103        familyName = std::move(font.familyName);
104        fname = std::move(font.fname);
105        index = font.index;
106        style = font.style;
107        isFixedWidth = font.isFixedWidth;
108        axisSet.axis = std::move(font.axisSet.axis);
109        axisSet.range = std::move(font.axisSet.range);
110        if (font.stream) {
111            stream = std::move(font.stream);
112        }
113        return *this;
114    }
115
116    /*! To set axis values
117     * \param count the count of axis
118     * \param axis an array of SkFixed value
119     * \param range an array of AxisDefinition
120     */
121    void setAxisSet(int count, const SkFixed* axis,
122        const SkTypeface_FreeType::Scanner::AxisDefinition* range)
123    {
124        axisSet.axis.clear();
125        axisSet.range.clear();
126        for (int i = 0; i < count; i++) {
127            axisSet.axis.emplace_back(axis[i]);
128            axisSet.range.emplace_back(range[i]);
129        }
130    }
131
132    SkFontStyle computeFontStyle()
133    {
134        int weight = style.weight();
135        int width = style.width();
136        auto slant = style.slant();
137        for (size_t i = 0; i < axisSet.axis.size(); i++) {
138            auto value = SkFixedToScalar(axisSet.axis[i]);
139            auto tag = axisSet.range[i].fTag;
140            if (tag == SkSetFourByteTag('w', 'g', 'h', 't')) {
141                weight = SkScalarFloorToInt(value);
142            } else if (tag == SkSetFourByteTag('w', 'd', 't', 'h')) {
143                width = SkScalarFloorToInt(value);
144            }
145        }
146        return SkFontStyle(weight, width, slant);
147    }
148
149    SkString familyName;  // the real family name of the font
150    SkString fname; // the full name of font file
151    int index; // the index of the font in a ttc font
152    SkFontStyle style; // the font style
153    bool isFixedWidth; // the flag to indicate if the font has fixed width or not
154    /*!
155     * \brief To manage the axis values for variable font
156     */
157    struct AxisSet {
158        std::vector<SkFixed> axis;  // the axis values
159        std::vector<SkTypeface_FreeType::Scanner::AxisDefinition> range; // the axis ranges
160    } axisSet; // the axis values for a variable font
161    std::unique_ptr<SkStreamAsset> stream; // the data stream of font file
162};
163
164#endif /* FONTINFO_OHOS_H */
165