1 // Copyright (c) 2021 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 #include "SkTypeface_ohos.h"
6
7 #include "securec.h"
8
9 #include "SkFontDescriptor.h"
10 #include "SkFontHost_FreeType_common.h"
11 #include "SkTArray.h"
12
13 /*! Constructor
14 * \param familyName the specified family name for the typeface
15 * \param info the font information for the typeface
16 */
SkTypeface_OHOS(const SkString & familyName,FontInfo & info)17 SkTypeface_OHOS::SkTypeface_OHOS(const SkString& familyName, FontInfo& info)
18 : SkTypeface_FreeType(info.style, info.isFixedWidth),
19 specifiedName(familyName)
20 {
21 fontInfo = std::make_unique<FontInfo>(std::move(info));
22 }
23
24 /*! Constructor
25 * \param info the font information for the typeface
26 */
SkTypeface_OHOS(FontInfo & info)27 SkTypeface_OHOS::SkTypeface_OHOS(FontInfo& info)
28 : SkTypeface_FreeType(info.style, info.isFixedWidth)
29 {
30 specifiedName.reset();
31 fontInfo = std::make_unique<FontInfo>(std::move(info));
32 }
33
34 /*! To get stream of the typeface
35 * \param[out] ttcIndex the index of the typeface in a ttc file returned to the caller
36 * \return The stream object of the typeface
37 */
onOpenStream(int * ttcIndex) const38 std::unique_ptr<SkStreamAsset> SkTypeface_OHOS::onOpenStream(int* ttcIndex) const
39 {
40 if (fontInfo) {
41 if (ttcIndex) {
42 *ttcIndex = fontInfo->index;
43 }
44 if (fontInfo->stream == nullptr) {
45 fontInfo->stream = SkStream::MakeFromFile(fontInfo->fname.c_str());
46 }
47 if (fontInfo->stream) {
48 return fontInfo->stream->duplicate();
49 }
50 }
51 return nullptr;
52 }
53
54 /*! To make font data from the typeface
55 * \return The object of SkFontData
56 */
onMakeFontData() const57 std::unique_ptr<SkFontData> SkTypeface_OHOS::onMakeFontData() const
58 {
59 if (fontInfo == nullptr) {
60 return nullptr;
61 }
62
63 if (fontInfo->stream.get() == nullptr) {
64 fontInfo->stream = SkStream::MakeFromFile(fontInfo->fname.c_str());
65 }
66 if (fontInfo->stream.get() == nullptr) {
67 return nullptr;
68 }
69 return std::make_unique<SkFontData>(fontInfo->stream->duplicate(), fontInfo->index,
70 fontInfo->axisSet.axis.data(), fontInfo->axisSet.axis.size());
71 }
72
73 /*! To get the font descriptor of the typeface
74 * \param[out] descriptor the font descriptor returned to the caller
75 * \param[out] isLocal the false to the caller
76 */
onGetFontDescriptor(SkFontDescriptor * descriptor,bool * isLocal) const77 void SkTypeface_OHOS::onGetFontDescriptor(SkFontDescriptor* descriptor, bool* isLocal) const
78 {
79 if (isLocal) {
80 *isLocal = false;
81 }
82 if (descriptor) {
83 SkString familyName;
84 onGetFamilyName(&familyName);
85 descriptor->setFamilyName(familyName.c_str());
86 descriptor->setStyle(this->fontStyle());
87 }
88 }
89
90 /*! To get the family name of the typeface
91 * \param[out] familyName the family name returned to the caller
92 */
onGetFamilyName(SkString * familyName) const93 void SkTypeface_OHOS::onGetFamilyName(SkString* familyName) const
94 {
95 if (familyName == nullptr) {
96 return;
97 }
98 if (specifiedName.size() > 0) {
99 *familyName = specifiedName;
100 } else {
101 if (fontInfo) {
102 *familyName = fontInfo->familyName;
103 }
104 }
105 }
106
107 /*! To clone a typeface from this typeface
108 * \param args the specified font arguments from which the new typeface is created
109 * \return The object of a new typeface
110 * \note The caller must call unref() on the returned object
111 */
onMakeClone(const SkFontArguments & args) const112 sk_sp<SkTypeface> SkTypeface_OHOS::onMakeClone(const SkFontArguments& args) const
113 {
114 FontInfo info(*(fontInfo.get()));
115 info.index = args.getCollectionIndex();
116 unsigned int count = args.getVariationDesignPosition().coordinateCount;
117 if (count > 0 && count == fontInfo->axisSet.range.size()) {
118 SkFontArguments::VariationPosition position = args.getVariationDesignPosition();
119 SkTypeface_FreeType::Scanner::AxisDefinitions axisDefs;
120 for (unsigned int i = 0; i < count; i++) {
121 axisDefs.push_back(fontInfo->axisSet.range[i]);
122 }
123 SkFixed axisValues[count];
124 memset_s(axisValues, sizeof(axisValues), 0, sizeof(axisValues));
125 SkTypeface_FreeType::Scanner::computeAxisValues(axisDefs, position,
126 axisValues, fontInfo->familyName);
127 info.axisSet.axis.clear();
128 for (unsigned int i = 0; i < count; i++) {
129 info.axisSet.axis.emplace_back(axisValues[i]);
130 }
131 }
132 return sk_make_sp<SkTypeface_OHOS>(specifiedName, info);
133 }
134
135 /*! To get the font information of the typeface
136 * \return The object of FontInfo
137 */
getFontInfo() const138 const FontInfo* SkTypeface_OHOS::getFontInfo() const
139 {
140 return fontInfo.get();
141 }
142