1 /* 2 * Copyright (c) 2023 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 enum class FontEdging { 33 ALIAS, 34 ANTI_ALIAS, 35 SUBPIXEL_ANTI_ALIAS, 36 }; 37 38 class DRAWING_API Font { 39 public: 40 Font(); 41 Font(std::shared_ptr<Typeface> typeface, scalar size, scalar scaleX, scalar skewX); 42 virtual ~Font() = default; 43 44 /* 45 * @brief Set font edge pixels pattern. 46 * @param edging Edge pixels pattern. 47 */ 48 void SetEdging(FontEdging edging); 49 50 /* 51 * @brief Set glyphs are drawn at sub-pixel offsets. 52 * @param isSubpixel Glyphs should be drawn at sub-pixel. 53 */ 54 void SetSubpixel(bool isSubpixel); 55 56 /* 57 * @brief Set font hinting pattern. 58 * @param hintingLevel Font hinting level. 59 */ 60 void SetHinting(FontHinting hintingLevel); 61 62 /* 63 * @brief Set Typeface to font. 64 * @param typeface A shared point to typeface. 65 */ 66 void SetTypeface(std::shared_ptr<Typeface> typeface); 67 68 /* 69 * @brief Set text size. 70 * @param textSize Text size. 71 */ 72 void SetSize(scalar textSize); 73 74 /* 75 * @brief Set to increase stroke width when creating glyph bitmaps to approximate a bold typeface. 76 * @param isEmbolden Should increase stroke width. 77 */ 78 void SetEmbolden(bool isEmbolden); 79 80 /* 81 * @brief Set text scale on x-axis. 82 * @param scaleX Text horizontal scale. 83 */ 84 void SetScaleX(scalar scaleX); 85 86 /* 87 * @brief Set text skew on x-axis. 88 * @param skewX Additional shear on x-axis relative to y-axis. 89 */ 90 void SetSkewX(scalar skewX); 91 92 /* 93 * @brief Set Font and glyph metrics should ignore hinting and rounding. 94 * @param isLinearMetrics Should ignore hinting and rounding. 95 */ 96 void SetLinearMetrics(bool isLinearMetrics); 97 98 /* 99 * @brief Get fontMetrics associated with typeface. 100 * @param metrics The fontMetrics value returned to the caller. 101 * @return Recommended spacing between lines. 102 */ 103 scalar GetMetrics(FontMetrics* metrics) const; 104 105 /* 106 * @brief Retrieves the advance and bounds for each glyph in glyphs. 107 * @param glyphs Array of glyph indices to be measured 108 * @param count Number of glyphs 109 * @param widths Text advances for each glyph returned to the caller. 110 */ 111 void GetWidths(const uint16_t glyphs[], int count, scalar widths[]) const; 112 113 /* 114 * @brief Retrieves the advance and bounds for each glyph in glyphs. 115 * @param glyphs Array of glyph indices to be measured 116 * @param count Number of glyphs 117 * @param widths Text advances for each glyph returned to the caller. 118 * @param bounds Bounds for each glyph relative to (0, 0) returned to the caller. 119 */ 120 void GetWidths(const uint16_t glyphs[], int count, scalar widths[], Rect bounds[]) const; 121 122 /* 123 * @brief Returns text size in points. 124 * @return The size of text. 125 */ 126 scalar GetSize() const; 127 128 /* 129 * @brief Returns Typeface if set, or nullptr. 130 * @return Typeface if previously set, nullptr otherwise. 131 */ 132 std::shared_ptr<Typeface> GetTypeface(); 133 134 /* 135 * @brief Measure the width of text. 136 * @param text Character storage encoded with TextEncoding 137 * @param byteLength Length of character storage in bytes 138 * @param encoding Text encoding. 139 * @return The width of text. 140 */ 141 scalar MeasureText(const void* text, size_t byteLength, TextEncoding encoding); 142 143 template<typename T> GetImpl()144 T* GetImpl() const 145 { 146 return fontImpl_->DowncastingTo<T>(); 147 } 148 149 private: 150 std::shared_ptr<FontImpl> fontImpl_; 151 }; 152 } // namespace Drawing 153 } // namespace Rosen 154 } // namespace OHOS 155 #endif