1 /* 2 * Copyright (c) 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_DATA_H 17 #define FONT_DATA_H 18 19 #include <cfloat> 20 #include <cstdint> 21 #include <freetype/freetype.h> 22 #include <memory> 23 #include <shared_mutex> 24 25 #include <base/containers/string_view.h> 26 #include <font/intf_font.h> 27 28 #include "font_defs.h" 29 30 namespace { 31 constexpr float POINT_SIZE = 72.f; 32 } 33 34 FONT_BEGIN_NAMESPACE() 35 struct UiText; 36 struct RenderVertexLayout0; 37 38 class FaceData; 39 40 class FontData final : public std::enable_shared_from_this<FontData> { 41 public: 42 // interface API 43 static std::unique_ptr<FontData> CreateFromFaceData(const std::weak_ptr<FaceData>& face, bool sdf); 44 45 FontData(const std::weak_ptr<FaceData>& face, FT_Size size, bool sdf); 46 ~FontData(); 47 48 FontSize GetSize(); 49 50 FontMetrics GetMetrics(); 51 GlyphMetrics GetGlyphMetrics(uint32_t glyphIndex); 52 GlyphInfo GetGlyphInfo(uint32_t glyphIndex); 53 BASE_NS::vector<GlyphContour> GetGlyphContours(uint32_t glyphIndex); 54 55 // measure string dimension as if text would have been drawn by DrawString 56 BASE_NS::Math::Vec2 MeasureString(BASE_NS::string_view); 57 58 private: 59 friend class FaceData; 60 61 std::weak_ptr<FaceData> faceData_; // weak reference to face which owns this data 62 FT_Size sizeData_; // scaling size used by this instance 63 bool sdfData_; // sdf usage by this instance 64 65 std::shared_mutex mutex_; // mutex for glyph cache access 66 FontDefs::GlyphCache glyphCache_; 67 68 const FontDefs::Glyph* GetOrCreateCachedGlyph(uint32_t glyphIndex); 69 const FontDefs::Glyph* UpdateGlyph(uint32_t glyphIndex); 70 }; 71 72 FONT_END_NAMESPACE() 73 #endif // FONT_DATA_H 74