• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2024 Google LLC
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/SkAlphaType.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkFontMgr.h"
12 #include "include/core/SkFontStyle.h"
13 #include "include/core/SkImageInfo.h"
14 #include "include/core/SkPixmap.h"
15 #include "include/core/SkRefCnt.h"
16 #include "include/core/SkStream.h"
17 #include "include/core/SkSurface.h"
18 #include "include/encode/SkPngEncoder.h"
19 #include "modules/svg/include/SkSVGDOM.h"
20 #include "modules/skshaper/utils/FactoryHelpers.h"
21 
22 #if defined(SK_FONTMGR_FONTCONFIG_AVAILABLE)
23 #include "include/ports/SkFontMgr_fontconfig.h"
24 #endif
25 
26 #if defined(SK_FONTMGR_CORETEXT_AVAILABLE)
27 #include "include/ports/SkFontMgr_mac_ct.h"
28 #endif
29 
30 #include <cstdio>
31 
32 constexpr size_t kWidth =  450;
33 constexpr size_t kHeight = 120;
34 
35 // The text in this SVG should be properly shaped if the V and A characters overlap slightly.
36 const char* svg = "<svg viewBox=\"0 0 150 40\" xmlns=\"http://www.w3.org/2000/svg\">"
37   "<style>"
38     "text {"
39       "font: 13px sans-serif;"
40     "}"
41   "</style>"
42   "<text x=\"10\" y=\"30\">VAVAVAVA</text>"
43 "</svg>";
44 
main(int argc,char ** argv)45 int main(int argc, char** argv) {
46     if (argc != 2) {
47         printf("Usage: %s <name.png>", argv[0]);
48         return 1;
49     }
50     SkFILEWStream output(argv[1]);
51     if (!output.isValid()) {
52         printf("Cannot open output file %s\n", argv[1]);
53         return 1;
54     }
55 
56     // If necessary, clients should use a font manager that would load fonts from the system.
57     sk_sp<SkFontMgr> fontMgr;
58 #if defined(SK_FONTMGR_FONTCONFIG_AVAILABLE)
59     fontMgr = SkFontMgr_New_FontConfig(nullptr);
60 #elif defined(SK_FONTMGR_CORETEXT_AVAILABLE)
61     fontMgr = SkFontMgr_New_CoreText(nullptr);
62 #endif
63     if (!fontMgr) {
64         printf("No Font Manager configured\n");
65         return 1;
66     }
67 
68     SkMemoryStream svgStream(svg, std::strlen(svg));
69 
70     auto svg_dom = SkSVGDOM::Builder()
71                            .setFontManager(fontMgr)
72                            .setTextShapingFactory(SkShapers::BestAvailable())
73                            .make(svgStream);
74 
75     if (!svg_dom) {
76         printf("Could not parse compiled-in svg\n");
77         return 1;
78     }
79 
80     auto surface = SkSurfaces::Raster(SkImageInfo::MakeN32Premul(kWidth, kHeight));
81 
82     svg_dom->setContainerSize(SkSize::Make(kWidth, kHeight));
83     svg_dom->render(surface->getCanvas());
84 
85     SkPixmap pixmap;
86     surface->peekPixels(&pixmap);
87 
88     if (!SkPngEncoder::Encode(&output, pixmap, {})) {
89         printf("PNG encoding failed.\n");
90         return 1;
91     }
92     printf("Success!\n");
93     return 0;
94 }
95