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 #include "hm_symbol_run_fuzzer.h"
17 #include <cstddef>
18 #include "get_object.h"
19 #include "symbol_engine/hm_symbol_run.h"
20 
21 namespace OHOS {
22 namespace Rosen {
23 namespace SPText {
DrawSymbolFuzzTest(const uint8_t * data,size_t size)24 bool DrawSymbolFuzzTest(const uint8_t* data, size_t size)
25 {
26     if (data == nullptr) {
27         return false;
28     }
29     // initialize
30     g_data = data;
31     g_size = size;
32     g_pos = 0;
33 
34     std::shared_ptr<RSCanvas> rsCanvas = std::make_shared<RSCanvas>();
35     const char* str1 = "A";
36     Drawing::Font font;
37     auto textblob1 = Drawing::TextBlob::MakeFromText(str1, strlen(str1), font, Drawing::TextEncoding::UTF8);
38 
39     float offsetX = SPText::GetObject<float>();
40     float offsety = SPText::GetObject<float>();
41     RSPoint paint = {offsetX, offsety};
42     RSEffectStrategy effectStrategy = SPText::GetObject<RSEffectStrategy>();
43     HMSymbolTxt symbolText;
44     symbolText.SetSymbolEffect(effectStrategy);
45 
46     HMSymbolRun hmSymbolRun = HMSymbolRun();
47     hmSymbolRun.DrawSymbol(rsCanvas.get(), textblob1.get(), paint, symbolText);
48 
49     // test the TextBlob is nullptr
50     textblob1 = nullptr;
51     hmSymbolRun.DrawSymbol(rsCanvas.get(), textblob1.get(), paint, symbolText);
52 
53     // test the multiple glyphs
54     const char* str2 = "Test multiple glyphs";
55     auto textblob2 = Drawing::TextBlob::MakeFromText(str2, strlen(str2), font, Drawing::TextEncoding::UTF8);
56     hmSymbolRun.DrawSymbol(rsCanvas.get(), textblob2.get(), paint, symbolText);
57     return true;
58 }
59 } // namespace SPText
60 } // namespace Rosen
61 } // namespace OHOS
62 
63 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)64 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
65 {
66     /* Run your code on data */
67     OHOS::Rosen::SPText::DrawSymbolFuzzTest(data, size);
68     return 0;
69 }
70