Flutter Engine
The Flutter Engine
svg_renderer.cpp
Go to the documentation of this file.
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
21
22#if defined(SK_FONTMGR_FONTCONFIG_AVAILABLE)
24#endif
25
26#if defined(SK_FONTMGR_CORETEXT_AVAILABLE)
28#endif
29
30#include <cstdio>
31
32constexpr size_t kWidth = 450;
33constexpr size_t kHeight = 120;
34
35// The text in this SVG should be properly shaped if the V and A characters overlap slightly.
36const 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
45int main(int argc, char** argv) {
46 if (argc != 2) {
47 printf("Usage: %s <name.png>", argv[0]);
48 return 1;
49 }
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.
58#if defined(SK_FONTMGR_FONTCONFIG_AVAILABLE)
60#elif defined(SK_FONTMGR_CORETEXT_AVAILABLE)
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
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}
SK_API sk_sp< SkFontMgr > SkFontMgr_New_FontConfig(FcConfig *fc)
SK_API sk_sp< SkFontMgr > SkFontMgr_New_CoreText(CTFontCollectionRef)
VkSurfaceKHR surface
Definition: main.cc:49
sk_sp< SkFontMgr > fontMgr
Definition: examples.cpp:32
char ** argv
Definition: library.h:9
SK_API bool Encode(SkWStream *dst, const SkPixmap &src, const Options &options)
std::string printf(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: SkSLString.cpp:83
sk_sp< Factory > BestAvailable()
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
DlVertices::Builder Builder
static SkImageInfo MakeN32Premul(int width, int height)
static constexpr SkSize Make(SkScalar w, SkScalar h)
Definition: SkSize.h:56
constexpr size_t kHeight
int main(int argc, char **argv)
constexpr size_t kWidth
const char * svg