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     std::string GetFontPath() const override;
45     FontStyle GetFontStyle() const override;
46     size_t GetTableSize(uint32_t tag) const override;
47     size_t GetTableData(uint32_t tag, size_t offset, size_t length, void* data) const override;
48     bool GetItalic() const override;
49     uint32_t GetUniqueID() const override;
50     int32_t GetUnitsPerEm() const override;
51     std::shared_ptr<Typeface> MakeClone(const FontArguments& args) const override;
52     bool IsCustomTypeface() const override;
53     bool IsThemeTypeface() const override;
54     sk_sp<SkTypeface> GetSkTypeface();
55 
56     static std::shared_ptr<Typeface> MakeDefault();
57     static std::shared_ptr<Typeface> MakeFromFile(const char path[], int index);
58     static std::shared_ptr<Typeface> MakeFromFile(const char path[], const FontArguments& fontArguments);
59     static std::shared_ptr<Typeface> MakeFromStream(std::unique_ptr<MemoryStream> memoryStream, int32_t index);
60     static std::shared_ptr<Typeface> MakeFromName(const char familyName[], FontStyle fontStyle);
61     static std::vector<std::shared_ptr<Typeface>> GetSystemFonts();
62 
63     static sk_sp<SkData> SerializeTypeface(SkTypeface* typeface, void* ctx);
64     static sk_sp<SkTypeface> DeserializeTypeface(const void* data, size_t length, void* ctx);
65     std::shared_ptr<Data> Serialize() const override;
66     static std::shared_ptr<Typeface> Deserialize(const void* data, size_t size);
67     /** return stored hash, calculates the hash if no value is stored */
68     uint32_t GetHash() const override;
69     /** store hash, storing a zero causes recalculating a hash when asked */
70     void SetHash(uint32_t hash) override;
71 
72 private:
73     SkiaTypeface() = default;
74 
75     sk_sp<SkTypeface> skTypeface_;
76 };
77 } // namespace Drawing
78 } // namespace Rosen
79 } // namespace OHOS
80 #endif