1 /* 2 * Copyright (c) 2023-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 FONT_H 17 #define FONT_H 18 19 #include <memory> 20 #include <cstdint> 21 22 #include "impl_interface/font_impl.h" 23 #include "text/font_metrics.h" 24 #include "text/font_types.h" 25 #include "text/typeface.h" 26 #include "utils/rect.h" 27 #include "utils/scalar.h" 28 29 namespace OHOS { 30 namespace Rosen { 31 namespace Drawing { 32 class DRAWING_API Font { 33 public: 34 Font(); 35 Font(std::shared_ptr<Typeface> typeface, scalar size, scalar scaleX, scalar skewX); 36 Font(const Font& font); 37 virtual ~Font() = default; 38 39 /** 40 * @brief Set font edge pixels pattern. 41 * @param edging Edge pixels pattern. 42 */ 43 void SetEdging(FontEdging edging); 44 45 /** 46 * @brief Requests that baselines be snapped to pixels when the current 47 * transformation matrix is axis aligned. 48 * @param baselineSnap Setting for baseline snapping to pixels. 49 */ 50 void SetBaselineSnap(bool baselineSnap); 51 52 /** 53 * @brief Set whether the font outline is automatically adjusted. 54 * @param isForceAutoHinting Indicate whether the font outline is automatically adjusted. 55 */ 56 void SetForceAutoHinting(bool isForceAutoHinting); 57 58 /** 59 * @brief Set glyphs are drawn at sub-pixel offsets. 60 * @param isSubpixel Glyphs should be drawn at sub-pixel. 61 */ 62 void SetSubpixel(bool isSubpixel); 63 64 /** 65 * @brief Set font hinting pattern. 66 * @param hintingLevel Font hinting level. 67 */ 68 void SetHinting(FontHinting hintingLevel); 69 70 /** 71 * @brief Set font bitmaps mode. 72 * @param embeddedBitmaps Font bitmaps mode. 73 */ 74 void SetEmbeddedBitmaps(bool embeddedBitmaps); 75 76 /** 77 * @brief Set Typeface to font. 78 * @param typeface A shared point to typeface. 79 */ 80 void SetTypeface(std::shared_ptr<Typeface> typeface); 81 82 /** 83 * @brief Set text size. 84 * @param textSize Text size. 85 */ 86 void SetSize(scalar textSize); 87 88 /** 89 * @brief Set to increase stroke width when creating glyph bitmaps to approximate a bold typeface. 90 * @param isEmbolden Should increase stroke width. 91 */ 92 void SetEmbolden(bool isEmbolden); 93 94 /** 95 * @brief Set text scale on x-axis. 96 * @param scaleX Text horizontal scale. 97 */ 98 void SetScaleX(scalar scaleX); 99 100 /** 101 * @brief Set text skew on x-axis. 102 * @param skewX Additional shear on x-axis relative to y-axis. 103 */ 104 void SetSkewX(scalar skewX); 105 106 /** 107 * @brief Set Font and glyph metrics should ignore hinting and rounding. 108 * @param isLinearMetrics Should ignore hinting and rounding. 109 */ 110 void SetLinearMetrics(bool isLinearMetrics); 111 112 /** 113 * @brief Get fontMetrics associated with typeface. 114 * @param metrics The fontMetrics value returned to the caller. 115 * @return Recommended spacing between lines. 116 */ 117 scalar GetMetrics(FontMetrics* metrics) const; 118 119 /** 120 * @brief Retrieves the advance and bounds for each glyph in glyphs. 121 * @param glyphs Array of glyph indices to be measured 122 * @param count Number of glyphs 123 * @param widths Text advances for each glyph returned to the caller. 124 */ 125 void GetWidths(const uint16_t glyphs[], int count, scalar widths[]) const; 126 127 /** 128 * @brief Retrieves the advance and bounds for each glyph in glyphs. 129 * @param glyphs Array of glyph indices to be measured 130 * @param count Number of glyphs 131 * @param widths Text advances for each glyph returned to the caller. 132 * @param bounds Bounds for each glyph relative to (0, 0) returned to the caller. 133 */ 134 void GetWidths(const uint16_t glyphs[], int count, scalar widths[], Rect bounds[]) const; 135 136 /** 137 * @brief Returns text size in points. 138 * @return The size of text. 139 */ 140 scalar GetSize() const; 141 142 /** 143 * @brief Returns Typeface if set, or nullptr. 144 * @return Typeface if previously set, nullptr otherwise. 145 */ 146 std::shared_ptr<Typeface> GetTypeface() const; 147 148 /** 149 * @brief Get font edge pixels pattern. 150 * @return Edge pixels pattern. 151 */ 152 FontEdging GetEdging() const; 153 154 /** 155 * @brief Get font hinting pattern. 156 * @return Font hinting level. 157 */ 158 FontHinting GetHinting() const; 159 160 /** 161 * @brief Returns text scale on x-axis. 162 * @return Text horizontal scale. 163 */ 164 scalar GetScaleX() const; 165 166 /** 167 * @brief Returns text skew on x-axis. 168 * @return Additional shear on x-axis relative to y-axis. 169 */ 170 scalar GetSkewX() const; 171 172 /** 173 * @brief Returns true if baselines may be snapped to pixels. 174 * @return True if baselines will be snapped to pixel positions. 175 */ 176 bool IsBaselineSnap() const; 177 178 /** 179 * @brief Returns font bitmaps mode. 180 * @return Font bitmaps mode. 181 */ 182 bool IsEmbeddedBitmaps() const; 183 184 /** 185 * @brief Returns true if bold is approximated by increasing the stroke width. 186 * @return True if bold is approximated through stroke width. 187 */ 188 bool IsEmbolden() const; 189 190 /** 191 * @brief Returns true if the font outline is automatically adjusted. 192 * @return True if the font outline adjusts automatically 193 */ 194 bool IsForceAutoHinting() const; 195 196 /** 197 * @brief Returns true if glyphs may be drawn at sub-pixel offsets. 198 * @return True if glyphs may be drawn at sub-pixel offsets. 199 */ 200 bool IsSubpixel() const; 201 202 /** 203 * @brief Returns true if font and glyph metrics are requested to be linearly scalable. 204 * @return True if font and glyph metrics are requested to be linearly scalable. 205 */ 206 bool IsLinearMetrics() const; 207 208 /** 209 * @brief Returns glyph index for Unicode character. 210 * @param uni Unicode character. 211 * @return Glyph index. 212 */ 213 uint16_t UnicharToGlyph(int32_t uni) const; 214 215 /** 216 * @brief Converts text into glyph indices. 217 * @param text Character storage encoded with TextEncoding. 218 * @param byteLength Length of character storage in bytes. 219 * @param glyphs Storage for glyph indices; may be nullptr. 220 * @param maxGlyphCount Storage capacity. 221 * @return Number of glyphs represented by text of length byteLength. 222 */ 223 int TextToGlyphs(const void* text, size_t byteLength, TextEncoding encoding, 224 uint16_t glyphs[], int maxGlyphCount) const; 225 226 /** 227 * @brief Measure the width of text. 228 * @param text Character storage encoded with TextEncoding 229 * @param byteLength Length of character storage in bytes 230 * @param encoding Text encoding. 231 * @param bounds Bounding box relative to (0, 0) 232 * @return The width of text. 233 */ 234 scalar MeasureText(const void* text, size_t byteLength, TextEncoding encoding, Rect* bounds = nullptr) const; 235 236 /** 237 * @brief Measure the width of a single character. 238 * @param unicode unicode encoding of a single character. 239 * @return The width of a single character. 240 */ 241 scalar MeasureSingleCharacter(int32_t unicode) const; 242 243 /** 244 * @brief Gets a font where you can draw a single character. 245 * @param unicode unicode encoding of a single character. 246 * @return A pointer to font, if nullptr is returned, get failed. 247 */ 248 std::shared_ptr<Font> GetFallbackFont(int32_t unicode) const; 249 250 int CountText(const void* text, size_t byteLength, TextEncoding encoding) const; 251 252 template<typename T> GetImpl()253 T* GetImpl() const 254 { 255 return fontImpl_->DowncastingTo<T>(); 256 } 257 258 private: 259 std::shared_ptr<FontImpl> fontImpl_; 260 }; 261 } // namespace Drawing 262 } // namespace Rosen 263 } // namespace OHOS 264 #endif