| 1 |
/* |
| 2 |
* Copyright 2026 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 "tools/DeserialProcsUtils.h" |
| 9 |
|
| 10 |
#include "include/codec/SkCodec.h" |
| 11 |
#include "include/core/SkFontMgr.h" |
| 12 |
#include "include/core/SkImage.h" |
| 13 |
#include "include/core/SkStream.h" |
| 14 |
#include "tools/fonts/FontToolUtils.h" |
| 15 |
|
| 16 |
#if defined(SK_CODEC_DECODES_PNG_WITH_RUST) |
| 17 |
#include "include/codec/SkPngRustDecoder.h" |
| 18 |
#else |
| 19 |
#include "include/codec/SkPngDecoder.h" |
| 20 |
#endif |
| 21 |
|
| 22 |
namespace ToolUtils { |
| 23 |
|
| 24 |
SkDeserialProcs get_default_skp_deserial_procs() { |
| 25 |
SkDeserialProcs procs; |
| 26 |
procs.fImageDataProc = |
| 27 |
[](sk_sp<SkData> data, std::optional<SkAlphaType> at, void*) -> sk_sp<SkImage> { |
| 28 |
#if defined(SK_CODEC_DECODES_PNG_WITH_RUST) |
| 29 |
std::unique_ptr<SkStream> stream = SkMemoryStream::Make(data); |
| 30 |
auto codec = SkPngRustDecoder::Decode(std::move(stream), nullptr, nullptr); |
| 31 |
#else |
| 32 |
auto codec = SkPngDecoder::Decode(data, nullptr, nullptr); |
| 33 |
#endif |
| 34 |
if (!codec) { |
| 35 |
SkDebugf("Invalid png data detected\n"); |
| 36 |
return nullptr; |
| 37 |
} |
| 38 |
if (auto lazyImage = SkCodecs::DeferredImage(std::move(codec), at)) { |
| 39 |
return lazyImage->makeRasterImage(/*GrDirectContext=*/nullptr); |
| 40 |
} |
| 41 |
return nullptr; |
| 42 |
}; |
| 43 |
|
| 44 |
// SKPs may have typefaces encoded in them (e.g. with FreeType). We can try falling back |
| 45 |
// to the Test FontMgr (possibly a native one) if we have do not have FreeType built-in. |
| 46 |
procs.fTypefaceStreamProc = [](SkStream& stream, void*) -> sk_sp<SkTypeface> { |
| 47 |
return SkTypeface::MakeDeserialize(&stream, ToolUtils::TestFontMgr()); |
| 48 |
}; |
| 49 |
return procs; |
| 50 |
} |
| 51 |
|
| 52 |
} // namespace ToolUtils |
| 53 |
|