• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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> MakeFromStream(std::unique_ptr<MemoryStream> memoryStream,
61         const FontArguments& fontArguments);
62     static std::shared_ptr<Typeface> MakeFromName(const char familyName[], FontStyle fontStyle);
63     static std::vector<std::shared_ptr<Typeface>> GetSystemFonts();
64 
65     static sk_sp<SkData> SerializeTypeface(SkTypeface* typeface, void* ctx);
66     static sk_sp<SkTypeface> DeserializeTypeface(const void* data, size_t length, void* ctx);
67     std::shared_ptr<Data> Serialize() const override;
68     static std::shared_ptr<Typeface> Deserialize(const void* data, size_t size);
69     /** return stored hash, calculates the hash if no value is stored */
70     uint32_t GetHash() const override;
71     /** store hash, storing a zero causes recalculating a hash when asked */
72     void SetHash(uint32_t hash) override;
73 
74 private:
75     SkiaTypeface() = default;
76 
77     sk_sp<SkTypeface> skTypeface_;
78 };
79 } // namespace Drawing
80 } // namespace Rosen
81 } // namespace OHOS
82 #endif