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