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 #include <unordered_map> 22 23 #include "impl_interface/font_impl.h" 24 #include "text/font_metrics.h" 25 #include "text/font_types.h" 26 #include "text/typeface.h" 27 #include "utils/rect.h" 28 #include "utils/scalar.h" 29 #include "draw/brush.h" 30 #include "draw/pen.h" 31 32 namespace OHOS { 33 namespace Rosen { 34 namespace Drawing { 35 using DrawingFontFeatures = std::vector<std::unordered_map<std::string, double>>; 36 class DRAWING_API Font { 37 public: 38 Font(); 39 Font(std::shared_ptr<Typeface> typeface, scalar size, scalar scaleX, scalar skewX); 40 Font(const Font& font); 41 virtual ~Font() = default; 42 43 /** 44 * @brief Set font edge pixels pattern. 45 * @param edging Edge pixels pattern. 46 */ 47 void SetEdging(FontEdging edging); 48 49 /** 50 * @brief Requests that baselines be snapped to pixels when the current 51 * transformation matrix is axis aligned. 52 * @param baselineSnap Setting for baseline snapping to pixels. 53 */ 54 void SetBaselineSnap(bool baselineSnap); 55 56 /** 57 * @brief Set whether the font outline is automatically adjusted. 58 * @param isForceAutoHinting Indicate whether the font outline is automatically adjusted. 59 */ 60 void SetForceAutoHinting(bool isForceAutoHinting); 61 62 /** 63 * @brief Set glyphs are drawn at sub-pixel offsets. 64 * @param isSubpixel Glyphs should be drawn at sub-pixel. 65 */ 66 void SetSubpixel(bool isSubpixel); 67 68 /** 69 * @brief Set font hinting pattern. 70 * @param hintingLevel Font hinting level. 71 */ 72 void SetHinting(FontHinting hintingLevel); 73 74 /** 75 * @brief Set font bitmaps mode. 76 * @param embeddedBitmaps Font bitmaps mode. 77 */ 78 void SetEmbeddedBitmaps(bool embeddedBitmaps); 79 80 /** 81 * @brief Set Typeface to font. 82 * @param typeface A shared point to typeface. 83 */ 84 void SetTypeface(std::shared_ptr<Typeface> typeface); 85 86 /** 87 * @brief Set text size. 88 * @param textSize Text size. 89 */ 90 void SetSize(scalar textSize); 91 92 /** 93 * @brief Set to increase stroke width when creating glyph bitmaps to approximate a bold typeface. 94 * @param isEmbolden Should increase stroke width. 95 */ 96 void SetEmbolden(bool isEmbolden); 97 98 /** 99 * @brief Set text scale on x-axis. 100 * @param scaleX Text horizontal scale. 101 */ 102 void SetScaleX(scalar scaleX); 103 104 /** 105 * @brief Set text skew on x-axis. 106 * @param skewX Additional shear on x-axis relative to y-axis. 107 */ 108 void SetSkewX(scalar skewX); 109 110 /** 111 * @brief Set Font and glyph metrics should ignore hinting and rounding. 112 * @param isLinearMetrics Should ignore hinting and rounding. 113 */ 114 void SetLinearMetrics(bool isLinearMetrics); 115 116 /** 117 * @brief Get fontMetrics associated with typeface. 118 * @param metrics The fontMetrics value returned to the caller. 119 * @return Recommended spacing between lines. 120 */ 121 scalar GetMetrics(FontMetrics* metrics) const; 122 123 /** 124 * @brief Retrieves the advance and bounds for each glyph in glyphs. 125 * @param glyphs Array of glyph indices to be measured 126 * @param count Number of glyphs 127 * @param widths Text advances for each glyph returned to the caller. 128 */ 129 void GetWidths(const uint16_t glyphs[], int count, scalar widths[]) const; 130 131 /** 132 * @brief Retrieves the advance and bounds for each glyph in glyphs. 133 * @param glyphs Array of glyph indices to be measured 134 * @param count Number of glyphs 135 * @param widths Text advances for each glyph returned to the caller. 136 * @param bounds Bounds for each glyph relative to (0, 0) returned to the caller. 137 */ 138 void GetWidths(const uint16_t glyphs[], int count, scalar widths[], Rect bounds[]) const; 139 140 /** 141 * @brief Returns text size in points. 142 * @return The size of text. 143 */ 144 scalar GetSize() const; 145 146 /** 147 * @brief Returns Typeface if set, or nullptr. 148 * @return Typeface if previously set, nullptr otherwise. 149 */ 150 std::shared_ptr<Typeface> GetTypeface() const; 151 152 /** 153 * @brief Get font edge pixels pattern. 154 * @return Edge pixels pattern. 155 */ 156 FontEdging GetEdging() const; 157 158 /** 159 * @brief Get font hinting pattern. 160 * @return Font hinting level. 161 */ 162 FontHinting GetHinting() const; 163 164 /** 165 * @brief Returns text scale on x-axis. 166 * @return Text horizontal scale. 167 */ 168 scalar GetScaleX() const; 169 170 /** 171 * @brief Returns text skew on x-axis. 172 * @return Additional shear on x-axis relative to y-axis. 173 */ 174 scalar GetSkewX() const; 175 176 /** 177 * @brief Returns true if baselines may be snapped to pixels. 178 * @return True if baselines will be snapped to pixel positions. 179 */ 180 bool IsBaselineSnap() const; 181 182 /** 183 * @brief Returns font bitmaps mode. 184 * @return Font bitmaps mode. 185 */ 186 bool IsEmbeddedBitmaps() const; 187 188 /** 189 * @brief Returns true if bold is approximated by increasing the stroke width. 190 * @return True if bold is approximated through stroke width. 191 */ 192 bool IsEmbolden() const; 193 194 /** 195 * @brief Returns true if the font outline is automatically adjusted. 196 * @return True if the font outline adjusts automatically 197 */ 198 bool IsForceAutoHinting() const; 199 200 /** 201 * @brief Returns true if glyphs may be drawn at sub-pixel offsets. 202 * @return True if glyphs may be drawn at sub-pixel offsets. 203 */ 204 bool IsSubpixel() const; 205 206 /** 207 * @brief Returns true if font and glyph metrics are requested to be linearly scalable. 208 * @return True if font and glyph metrics are requested to be linearly scalable. 209 */ 210 bool IsLinearMetrics() const; 211 212 /** 213 * @brief Returns glyph index for Unicode character. 214 * @param uni Unicode character. 215 * @return Glyph index. 216 */ 217 uint16_t UnicharToGlyph(int32_t uni) const; 218 219 /** 220 * @brief Returns glyph index for Unicode character with DrawingFontFeatures. 221 * @param uni Unicode character. 222 * @param fontFeatures Font Features. 223 * @return Glyph index. 224 */ 225 uint16_t UnicharToGlyphWithFeatures(const char* uni, 226 std::shared_ptr<Drawing::DrawingFontFeatures> fontFeatures) const; 227 228 /** 229 * @brief Converts text into glyph indices. 230 * @param text Character storage encoded with TextEncoding. 231 * @param byteLength Length of character storage in bytes. 232 * @param glyphs Storage for glyph indices; may be nullptr. 233 * @param maxGlyphCount Storage capacity. 234 * @return Number of glyphs represented by text of length byteLength. 235 */ 236 int TextToGlyphs(const void* text, size_t byteLength, TextEncoding encoding, 237 uint16_t glyphs[], int maxGlyphCount) const; 238 239 /** 240 * @brief Measure the width of text. 241 * @param text Character storage encoded with TextEncoding 242 * @param byteLength Length of character storage in bytes 243 * @param encoding Text encoding. 244 * @param bounds Bounding box relative to (0, 0) 245 * @return The width of text. 246 */ 247 scalar MeasureText(const void* text, size_t byteLength, TextEncoding encoding, Rect* bounds = nullptr) const; 248 249 /** 250 * @brief Measures the width of text with brush or pen, brush and pen are both not nullptr. 251 * @param text Character storage encoded with TextEncoding 252 * @param byteLength Length of character storage in bytes 253 * @param encoding Text encoding. 254 * @param bounds Bounding box relative to (0, 0) 255 * @param Brush Brush to apply transparency, filtering, and so on. 256 * @param Pen Pen to apply transparency, filtering, and so on. 257 * @return The width of text. 258 */ 259 scalar MeasureText(const void* text, size_t byteLength, TextEncoding encoding, Rect* bounds, const Brush* brush, 260 const Pen* pen) const; 261 262 /** 263 * @brief Retrieves the positions for each glyph, beginning at the specified origin, 264 * brush and pen are both not nullptr. 265 * @param glyphs Indicates the array of glyph indices to be measured. 266 * @param count Indicates the number of glyphs. 267 * @param widths Indicates the text advances for each glyph returned to the caller. 268 * @param bounds Indicates the text bounding box for each glyph returned to caller. 269 * @param brush Brush to apply transparency, filtering, and so on. 270 * @param pen Pen to apply transparency, filtering, and so on. 271 */ 272 void GetWidthsBounds( 273 const uint16_t glyphs[], int count, float widths[], Rect bounds[], const Brush* brush, const Pen* pen) const; 274 275 /** 276 * @brief Retrieves the positions for each glyph, beginning at the specified origin. 277 * @param glyphs Indicates the array of glyph indices to be measured. 278 * @param count Indicates the number of glyphs. 279 * @param origin Indicates the location of the first glyph. 280 * @param points Indicates the relative position for each glyph returned to tha caller. 281 */ 282 void GetPos(const uint16_t glyphs[], int count, Point points[], Point origin) const; 283 284 /** 285 * @brief Returns the recommended spacing between lines. 286 * @return Spacing between lines. 287 */ 288 float GetSpacing() const; 289 290 /** 291 * @brief Measure the width of a single character. 292 * @param unicode unicode encoding of a single character. 293 * @return The width of a single character. 294 */ 295 scalar MeasureSingleCharacter(int32_t unicode) const; 296 297 /** 298 * @brief Measure the width of a single character. 299 * @param unicode unicode encoding of a single character. 300 * @param unicodeId unicode encoding id of a single character. 301 * @param fontFeatures font features to be applied. 302 * @return The width of a single character, in px. 303 */ 304 scalar MeasureSingleCharacterWithFeatures(const char* unicode, int32_t unicodeId, 305 std::shared_ptr<Drawing::DrawingFontFeatures> fontFeatures) const; 306 307 /** 308 * @brief Gets a font where you can draw a single character. 309 * @param unicode unicode encoding of a single character. 310 * @return A pointer to font, if nullptr is returned, get failed. 311 */ 312 std::shared_ptr<Font> GetFallbackFont(int32_t unicode) const; 313 314 int CountText(const void* text, size_t byteLength, TextEncoding encoding) const; 315 316 /** 317 * @brief Gets the path of specified glyph. 318 * @param glyph The glyph index. 319 * @param path The pointer of path object. 320 * @return True if success, false if no path found. 321 */ 322 bool GetPathForGlyph(uint16_t glyph, Path* path) const; 323 324 /** 325 * @brief Get the text outline path. 326 * @param text Indicates the character storage encoded with text encoding. 327 * @param byteLength Indicates the text length in bytes. 328 * @param encoding Indicates the text encoding. 329 * @param x Indicates x coordinates of the text. 330 * @param y Indicates y coordinates of the text. 331 * @param path The pointer of path object. 332 */ 333 void GetTextPath(const void* text, size_t byteLength, TextEncoding encoding, float x, float y, Path* path) const; 334 335 /** 336 * @brief Sets whether to follow the theme font. If the value is true, the theme font is used when typeface is not set. 337 * @param followed Indicates whether to follow the theme font. 338 */ 339 void SetThemeFontFollowed(bool followed); 340 341 /** 342 * @brief Gets whether to follow the theme font. 343 * @return True if follow the theme font. 344 */ 345 bool IsThemeFontFollowed() const; 346 347 template<typename T> GetImpl()348 T* GetImpl() const 349 { 350 return fontImpl_->DowncastingTo<T>(); 351 } 352 353 private: 354 bool themeFontFollowed_ = false; // Only effective for ndk interface and ArkTS interface. 355 std::shared_ptr<FontImpl> fontImpl_; 356 }; 357 } // namespace Drawing 358 } // namespace Rosen 359 } // namespace OHOS 360 #endif