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 SKIA_TYPEFACE_H 17 #define SKIA_TYPEFACE_H 18 19 #include "include/core/SkData.h" 20 #include "include/core/SkTypeface.h" 21 22 #include "impl_interface/typeface_impl.h" 23 #include "text/typeface.h" 24 #include "utils/memory_stream.h" 25 26 namespace OHOS { 27 namespace Rosen { 28 namespace Drawing { 29 class SkiaTypeface : public TypefaceImpl { 30 public: 31 static inline constexpr AdapterType TYPE = AdapterType::SKIA_ADAPTER; 32 33 explicit SkiaTypeface(sk_sp<SkTypeface> skTypeface); 34 ~SkiaTypeface() override = default; 35 GetType()36 AdapterType GetType() const override 37 { 38 return AdapterType::SKIA_ADAPTER; 39 } 40 41 sk_sp<SkTypeface> GetTypeface() const; 42 43 std::string GetFamilyName() const override; 44 FontStyle GetFontStyle() const override; 45 size_t GetTableSize(uint32_t tag) const override; 46 size_t GetTableData(uint32_t tag, size_t offset, size_t length, void* data) const override; 47 bool GetItalic() const override; 48 uint32_t GetUniqueID() const override; 49 int32_t GetUnitsPerEm() const override; 50 std::shared_ptr<Typeface> MakeClone(const FontArguments& args) const override; 51 bool IsCustomTypeface() const override; 52 sk_sp<SkTypeface> GetSkTypeface(); 53 54 static std::shared_ptr<Typeface> MakeDefault(); 55 static std::shared_ptr<Typeface> MakeFromFile(const char path[], int index); 56 static std::shared_ptr<Typeface> MakeFromFile(const char path[], const FontArguments& fontArguments); 57 static std::shared_ptr<Typeface> MakeFromStream(std::unique_ptr<MemoryStream> memoryStream, int32_t index); 58 static std::shared_ptr<Typeface> MakeFromName(const char familyName[], FontStyle fontStyle); 59 60 static sk_sp<SkData> SerializeTypeface(SkTypeface* typeface, void* ctx); 61 static sk_sp<SkTypeface> DeserializeTypeface(const void* data, size_t length, void* ctx); 62 std::shared_ptr<Data> Serialize() const override; 63 static std::shared_ptr<Typeface> Deserialize(const void* data, size_t size); 64 /** return stored hash, calculates the hash if no value is stored */ 65 uint32_t GetHash() const override; 66 /** store hash, storing a zero causes recalculating a hash when asked */ 67 void SetHash(uint32_t hash) override; 68 69 private: 70 SkiaTypeface() = default; 71 72 sk_sp<SkTypeface> skTypeface_; 73 }; 74 } // namespace Drawing 75 } // namespace Rosen 76 } // namespace OHOS 77 #endif