| 1 |
/* |
| 2 |
* Copyright 2015 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/svg/SkSVGCanvas.h" |
| 9 |
|
| 10 |
#include "include/core/SkCanvas.h" |
| 11 |
#include "include/core/SkRect.h" |
| 12 |
#include "include/core/SkRefCnt.h" |
| 13 |
#include "include/private/base/SkTo.h" |
| 14 |
#include "src/svg/SkSVGDevice.h" |
| 15 |
#include "src/xml/SkXMLWriter.h" |
| 16 |
|
| 17 |
#include <utility> |
| 18 |
|
| 19 |
#if !defined(SK_DISABLE_LEGACY_SVG_FACTORIES) |
| 20 |
#if defined(SK_CODEC_ENCODES_PNG_WITH_RUST) |
| 21 |
#include "include/encode/SkPngRustEncoder.h" |
| 22 |
#else |
| 23 |
#include "include/encode/SkPngEncoder.h" |
| 24 |
#endif // SK_CODEC_ENCODES_PNG_WITH_RUST |
| 25 |
#endif // !defined(SK_DISABLE_LEGACY_SVG_FACTORIES) |
| 26 |
|
| 27 |
std::unique_ptr<SkCanvas> SkSVGCanvas::Make(const SkRect& bounds, SkWStream* writer, |
| 28 |
Options opts) { |
| 29 |
#if !defined(SK_DISABLE_LEGACY_SVG_FACTORIES) |
| 30 |
if (!opts.pngEncoder) { |
| 31 |
opts.pngEncoder = [](SkWStream* dst, const SkPixmap& src) { |
| 32 |
#if defined(SK_CODEC_ENCODES_PNG_WITH_RUST) |
| 33 |
return SkPngRustEncoder::Encode(dst, src, {}); |
| 34 |
#else |
| 35 |
return SkPngEncoder::Encode(dst, src, {}); |
| 36 |
#endif // defined(SK_CODEC_ENCODES_PNG_WITH_RUST) |
| 37 |
}; |
| 38 |
} |
| 39 |
#else |
| 40 |
if (!opts.pngEncoder) { |
| 41 |
SK_ABORT("Must set a PNG encoder to make SVG canvas"); |
| 42 |
} |
| 43 |
#endif // !defined(SK_DISABLE_LEGACY_SVG_FACTORIES) |
| 44 |
|
| 45 |
// TODO: pass full bounds to the device |
| 46 |
const auto size = bounds.roundOut().size(); |
| 47 |
const auto xml_flags = (opts.flags & kNoPrettyXML_Flag) |
| 48 |
? SkToU32(SkXMLStreamWriter::kNoPretty_Flag) |
| 49 |
: 0; |
| 50 |
|
| 51 |
auto svgDevice = SkSVGDevice::Make(size, |
| 52 |
std::make_unique<SkXMLStreamWriter>(writer, xml_flags), |
| 53 |
opts); |
| 54 |
|
| 55 |
return svgDevice ? std::make_unique<SkCanvas>(std::move(svgDevice)) |
| 56 |
: nullptr; |
| 57 |
} |
| 58 |
|
| 59 |
#if !defined(SK_DISABLE_LEGACY_SVG_FACTORIES) |
| 60 |
std::unique_ptr<SkCanvas> SkSVGCanvas::Make(const SkRect& bounds, |
| 61 |
SkWStream* stream, |
| 62 |
uint32_t flags) { |
| 63 |
Options opts; |
| 64 |
opts.flags = static_cast<Flags>(flags); |
| 65 |
return Make(bounds, stream, opts); |
| 66 |
} |
| 67 |
#endif |