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