Flutter Engine
The Flutter Engine
Classes | Functions
SkSVGDevice.cpp File Reference
#include "src/svg/SkSVGDevice.h"
#include "include/core/SkBitmap.h"
#include "include/core/SkBlendMode.h"
#include "include/core/SkClipOp.h"
#include "include/core/SkColor.h"
#include "include/core/SkColorFilter.h"
#include "include/core/SkData.h"
#include "include/core/SkDataTable.h"
#include "include/core/SkFont.h"
#include "include/core/SkFontStyle.h"
#include "include/core/SkImage.h"
#include "include/core/SkImageInfo.h"
#include "include/core/SkMatrix.h"
#include "include/core/SkPaint.h"
#include "include/core/SkPath.h"
#include "include/core/SkPathBuilder.h"
#include "include/core/SkPathEffect.h"
#include "include/core/SkPathTypes.h"
#include "include/core/SkPathUtils.h"
#include "include/core/SkPoint.h"
#include "include/core/SkRRect.h"
#include "include/core/SkRect.h"
#include "include/core/SkScalar.h"
#include "include/core/SkShader.h"
#include "include/core/SkSize.h"
#include "include/core/SkSpan.h"
#include "include/core/SkStream.h"
#include "include/core/SkString.h"
#include "include/core/SkSurfaceProps.h"
#include "include/core/SkTileMode.h"
#include "include/core/SkTypeface.h"
#include "include/encode/SkPngEncoder.h"
#include "include/private/base/SkDebug.h"
#include "include/private/base/SkNoncopyable.h"
#include "include/private/base/SkTPin.h"
#include "include/private/base/SkTemplates.h"
#include "include/private/base/SkTo.h"
#include "include/svg/SkSVGCanvas.h"
#include "src/base/SkBase64.h"
#include "src/base/SkTLazy.h"
#include "src/core/SkAnnotationKeys.h"
#include "src/core/SkClipStack.h"
#include "src/core/SkDevice.h"
#include "src/core/SkFontPriv.h"
#include "src/core/SkTHash.h"
#include "src/image/SkImage_Base.h"
#include "src/shaders/SkColorShader.h"
#include "src/shaders/SkShaderBase.h"
#include "src/text/GlyphRun.h"
#include "src/xml/SkXMLWriter.h"
#include <cstring>
#include <memory>
#include <string>
#include <utility>

Go to the source code of this file.

Classes

class  SkSVGDevice::ResourceBucket
 
struct  SkSVGDevice::MxCp
 
class  SkSVGDevice::AutoElement
 
class  SVGTextBuilder
 

Functions

static bool is_png (const void *bytes, size_t length)
 
static bool is_jpeg (const void *bytes, size_t length)
 
sk_sp< SkDataAsDataUri (SkImage *image)
 
static sk_sp< SkDataencode (const SkBitmap &src)
 

Function Documentation

◆ AsDataUri()

sk_sp< SkData > AsDataUri ( SkImage image)

Definition at line 517 of file SkSVGDevice.cpp.

517 {
518 static constexpr char jpgDataPrefix[] = "data:image/jpeg;base64,";
519 static constexpr char pngDataPrefix[] = "data:image/png;base64,";
520
522
523 const char* selectedPrefix = pngDataPrefix;
524 size_t selectedPrefixLength = sizeof(pngDataPrefix);
525
526 sk_sp<SkData> imageData = image->refEncodedData();
527 if (imageData) { // Already encoded as something
528 if (is_jpeg(imageData->data(), imageData->size())) {
529 selectedPrefix = jpgDataPrefix;
530 selectedPrefixLength = sizeof(jpgDataPrefix);
531 } else if (!is_png(imageData->data(), imageData->size())) {
532 // re-encode the image as a PNG.
533 // GrDirectContext is nullptr because we shouldn't have any texture-based images
534 // passed in.
535 imageData = SkPngEncoder::Encode(nullptr, image, {});
536 if (!imageData) {
537 return nullptr;
538 }
539 }
540 // else, it's already encoded as a PNG - we don't need to do anything.
541 } else {
542 // It was not encoded as something, so we need to encode it as a PNG.
543 imageData = SkPngEncoder::Encode(nullptr, image, {});
544 if (!imageData) {
545 return nullptr;
546 }
547 }
548
549 size_t b64Size = SkBase64::EncodedSize(imageData->size());
550 sk_sp<SkData> dataUri = SkData::MakeUninitialized(selectedPrefixLength + b64Size);
551 char* dest = (char*)dataUri->writable_data();
552 memcpy(dest, selectedPrefix, selectedPrefixLength);
553 SkBase64::Encode(imageData->data(), imageData->size(), dest + selectedPrefixLength - 1);
554 dest[dataUri->size() - 1] = 0;
555 return dataUri;
556}
#define SkASSERT(cond)
Definition: SkAssert.h:116
static bool is_png(const void *bytes, size_t length)
static bool is_jpeg(const void *bytes, size_t length)
static sk_sp< SkData > MakeUninitialized(size_t length)
Definition: SkData.cpp:116
const void * data() const
Definition: SkData.h:37
void * writable_data()
Definition: SkData.h:52
size_t size() const
Definition: SkData.h:30
virtual bool isTextureBacked() const =0
sk_sp< SkData > refEncodedData() const
Definition: SkImage.cpp:214
SK_API bool Encode(SkWStream *dst, const SkPixmap &src, const Options &options)
sk_sp< const SkImage > image
Definition: SkRecords.h:269
dest
Definition: zip.py:79
static size_t EncodedSize(size_t srcDataLength)
Definition: SkBase64.h:40
static size_t Encode(const void *src, size_t length, void *dst, const char *encode=nullptr)
Definition: SkBase64.cpp:113

◆ encode()

static sk_sp< SkData > encode ( const SkBitmap src)
static

Definition at line 988 of file SkSVGDevice.cpp.

988 {
990 return SkPngEncoder::Encode(&buf, src.pixmap(), {}) ? buf.detachAsData() : nullptr;
991}
sk_sp< SkData > detachAsData()
Definition: SkStream.cpp:707

◆ is_jpeg()

static bool is_jpeg ( const void *  bytes,
size_t  length 
)
static

Definition at line 509 of file SkSVGDevice.cpp.

509 {
510 static constexpr uint8_t jpegSig[] = {0xFF, 0xD8, 0xFF};
511 return length >= sizeof(jpegSig) && !memcmp(bytes, jpegSig, sizeof(jpegSig));
512}
size_t length

◆ is_png()

static bool is_png ( const void *  bytes,
size_t  length 
)
static

Definition at line 504 of file SkSVGDevice.cpp.

504 {
505 static constexpr uint8_t pngSig[] = {0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A};
506 return length >= sizeof(pngSig) && !memcmp(bytes, pngSig, sizeof(pngSig));
507}