xref: /aosp_15_r20/external/skia/modules/skottie/utils/PreshapeTool.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2024 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "include/core/SkData.h"
9 #include "include/core/SkFontMgr.h"  // IWYU pragma: keep
10 #include "include/core/SkGraphics.h"
11 #include "include/core/SkRefCnt.h"
12 #include "include/core/SkStream.h"
13 #include "include/private/base/SkDebug.h"
14 #include "include/private/base/SkFeatures.h"
15 #include "modules/skottie/utils/TextPreshape.h"
16 #include "modules/skresources/include/SkResources.h"
17 #include "modules/skshaper/utils/FactoryHelpers.h"
18 #include "tools/flags/CommandLineFlags.h"
19 
20 #if defined(SK_BUILD_FOR_MAC) && defined(SK_FONTMGR_CORETEXT_AVAILABLE)
21 #include "include/ports/SkFontMgr_mac_ct.h"
22 #elif defined(SK_BUILD_FOR_ANDROID) && defined(SK_FONTMGR_ANDROID_AVAILABLE)
23 #include "include/ports/SkFontMgr_android.h"
24 #include "include/ports/SkFontScanner_FreeType.h"
25 #elif defined(SK_BUILD_FOR_UNIX) && defined(SK_FONTMGR_FONTCONFIG_AVAILABLE) && defined(SK_TYPEFACE_FACTORY_FREETYPE)
26 #include "include/ports/SkFontMgr_fontconfig.h"
27 #include "include/ports/SkFontScanner_FreeType.h"
28 #else
29 #include "include/ports/SkFontMgr_empty.h"
30 #endif
31 
32 static DEFINE_string2(input , i, nullptr, "Input .json file.");
33 static DEFINE_string2(output, o, nullptr, "Output .json file.");
34 
main(int argc,char ** argv)35  int main(int argc, char** argv) {
36     CommandLineFlags::Parse(argc, argv);
37     SkGraphics::Init();
38 
39     if (FLAGS_input.isEmpty() || FLAGS_output.isEmpty()) {
40         SkDebugf("Missing required 'input' and 'output' args.\n");
41         return 1;
42     }
43 
44     const auto data = SkData::MakeFromFileName(FLAGS_input[0]);
45     if (!data) {
46         SkDebugf("Could not read file: %s\n", FLAGS_input[0]);
47         return 1;
48     }
49 
50     SkFILEWStream out(FLAGS_output[0]);
51     if (!out.isValid()) {
52         SkDebugf("Could not write file: %s\n", FLAGS_output[0]);
53         return 1;
54     }
55 
56 #if defined(SK_BUILD_FOR_MAC) && defined(SK_FONTMGR_CORETEXT_AVAILABLE)
57     sk_sp<SkFontMgr> fontMgr = SkFontMgr_New_CoreText(nullptr);
58 #elif defined(SK_BUILD_FOR_ANDROID) && defined(SK_FONTMGR_ANDROID_AVAILABLE) && defined(SK_TYPEFACE_FACTORY_FREETYPE)
59     sk_sp<SkFontMgr> fontMgr = SkFontMgr_New_Android(nullptr, SkFontScanner_Make_FreeType());
60 #elif defined(SK_BUILD_FOR_UNIX) && defined(SK_FONTMGR_FONTCONFIG_AVAILABLE) && defined(SK_TYPEFACE_FACTORY_FREETYPE)
61     sk_sp<SkFontMgr> fontMgr = SkFontMgr_New_FontConfig(nullptr, SkFontScanner_Make_FreeType());
62 #else
63     sk_sp<SkFontMgr> fontMgr = SkFontMgr_New_Custom_Empty();
64 #endif
65 
66     if (!skottie_utils::Preshape(data, &out, fontMgr, SkShapers::BestAvailable(), nullptr)) {
67         SkDebugf("Could not preshape: %s\n", FLAGS_input[0]);
68         return -1;
69     }
70 
71     return 0;
72  }
73