Krita Source Code Documentation
Loading...
Searching...
No Matches
SvgSavingContext Class Reference

Context for saving svg files. More...

#include <SvgSavingContext.h>

+ Inheritance diagram for SvgSavingContext:

Public Member Functions

QString createFileName (const QString &extension)
 Create a filename suitable for saving external data.
 
QString createUID (const QString &base)
 Create a unique id from the specified base text.
 
QString getID (const KoShape *obj)
 Returns the unique id for the given shape.
 
bool isSavingInlineImages () const
 Returns if image should be saved inline.
 
 Private (QIODevice *_mainDevice, QIODevice *_styleDevice)
 
QString saveImage (const QImage &image)
 Saves given image and returns the href used.
 
void setStrippedTextMode (bool value)
 
KoXmlWritershapeWriter ()
 Provides access to the shape writer.
 
bool strippedTextMode () const
 
KoXmlWriterstyleWriter ()
 Provides access to the style writer.
 
 SvgSavingContext (QIODevice &outputDevice, bool saveInlineImages=true)
 Creates a new svg saving context on the specified output device.
 
 SvgSavingContext (QIODevice &shapesDevice, QIODevice &styleDevice, bool saveInlineImages=true)
 
QTransform userSpaceTransform () const
 Returns the transformation used to transform into user space.
 
 ~Private ()
 
virtual ~SvgSavingContext ()
 Virtual destructor.
 

Public Attributes

QIODevice * mainDevice
 
bool saveInlineImages
 
QBuffer shapeBuffer
 
QHash< const KoShape *, QString > shapeIds
 
QScopedPointer< KoXmlWritershapeWriter
 
bool strippedTextMode = false
 
QBuffer styleBuffer
 
QIODevice * styleDevice
 
QScopedPointer< KoXmlWriterstyleWriter
 
QHash< QString, int > uniqueNames
 
QTransform userSpaceMatrix
 

Private Attributes

Private *const d
 
- Private Attributes inherited from Private
KisCanvas2canvas
 
int displayedFrame
 
int intendedFrame
 

Additional Inherited Members

- Private Member Functions inherited from Private
 Private (KisCanvas2 *c)
 

Detailed Description

Context for saving svg files.

Definition at line 25 of file SvgSavingContext.cpp.

Constructor & Destructor Documentation

◆ ~Private()

SvgSavingContext::~Private ( )
inline

Definition at line 43 of file SvgSavingContext.cpp.

44 {
45 }

◆ SvgSavingContext() [1/2]

SvgSavingContext::SvgSavingContext ( QIODevice & outputDevice,
bool saveInlineImages = true )
explicit

Creates a new svg saving context on the specified output device.

Definition at line 61 of file SvgSavingContext.cpp.

62 : d(new Private(&outputDevice, 0))
63{
64 d->saveInlineImages = saveInlineImages;
65}
Private *const d

References d, and saveInlineImages.

◆ SvgSavingContext() [2/2]

SvgSavingContext::SvgSavingContext ( QIODevice & shapesDevice,
QIODevice & styleDevice,
bool saveInlineImages = true )
explicit

Definition at line 67 of file SvgSavingContext.cpp.

68 : d(new Private(&shapesDevice, &styleDevice))
69{
70 d->saveInlineImages = saveInlineImages;
71}

References d, and saveInlineImages.

◆ ~SvgSavingContext()

SvgSavingContext::~SvgSavingContext ( )
virtual

Virtual destructor.

Definition at line 73 of file SvgSavingContext.cpp.

74{
75 d->styleWriter->endElement();
76
77 if (d->styleDevice) {
78 d->styleDevice->write(d->styleBuffer.data());
79 } else {
80 d->mainDevice->write(d->styleBuffer.data());
81 d->mainDevice->write("\n");
82 }
83
84 d->mainDevice->write(d->shapeBuffer.data());
85
86 delete d;
87}

References d.

Member Function Documentation

◆ createFileName()

QString SvgSavingContext::createFileName ( const QString & extension)

Create a filename suitable for saving external data.

Definition at line 159 of file SvgSavingContext.cpp.

160{
161 QFile *file = qobject_cast<QFile*>(d->mainDevice);
162 if (!file)
163 return QString();
164
165 QFileInfo fi(file->fileName());
166 QString path = fi.absolutePath();
167 QString dstBaseFilename = fi.completeBaseName();
168
169 // create a filename for the image file at the destination directory
170 QString fname = dstBaseFilename + '_' + createUID("file");
171
172 // check if file exists already
173 int i = 0;
174 QString counter;
175 // change filename as long as the filename already exists
176 while (QFile(path + fname + counter + extension).exists()) {
177 counter = QString("_%1").arg(++i);
178 }
179
180 return fname + counter + extension;
181}
QString createUID(const QString &base)
Create a unique id from the specified base text.

References createUID(), and d.

◆ createUID()

QString SvgSavingContext::createUID ( const QString & base)

Create a unique id from the specified base text.

Definition at line 99 of file SvgSavingContext.cpp.

100{
101 QString idBase = base.isEmpty() ? "defitem" : base;
102 int counter = d->uniqueNames.value(idBase);
103 QString res;
104 do {
105 res = idBase + QString::number(counter);
106 counter++;
107 } while (d->uniqueNames.contains(res));
108
109 d->uniqueNames.insert(idBase, counter);
110 d->uniqueNames.insert(res, 1);
111 return res;
112}

References d.

◆ getID()

QString SvgSavingContext::getID ( const KoShape * obj)

Returns the unique id for the given shape.

Definition at line 114 of file SvgSavingContext.cpp.

115{
116 QString id;
117 // do we have already an id for this object ?
118 if (d->shapeIds.contains(obj)) {
119 // use existing id
120 id = d->shapeIds[obj];
121 } else {
122 // initialize from object name
123 id = obj->name();
124 // if object name is not empty and was not used already
125 // we can use it as is
126 if (!id.isEmpty() && !d->uniqueNames.contains(id)) {
127 // add to unique names so it does not get reused
128 d->uniqueNames.insert(id, 1);
129 } else {
130 if (id.isEmpty()) {
131 // differentiate a little between shape types
132 if (dynamic_cast<const KoShapeGroup*>(obj))
133 id = "group";
134 else if (dynamic_cast<const KoShapeLayer*>(obj))
135 id = "layer";
136 else
137 id = "shape";
138 }
139 // create a completely new id based on object name
140 // or a generic name
141 id = createUID(id);
142 }
143 // record id for this shape
144 d->shapeIds.insert(obj, id);
145 }
146 return id;
147}
QString name() const
Definition KoShape.cpp:1150

References createUID(), d, and KoShape::name().

◆ isSavingInlineImages()

bool SvgSavingContext::isSavingInlineImages ( ) const

Returns if image should be saved inline.

Definition at line 154 of file SvgSavingContext.cpp.

155{
156 return d->saveInlineImages;
157}

References d.

◆ Private()

SvgSavingContext::Private ( QIODevice * _mainDevice,
QIODevice * _styleDevice )
inline

Definition at line 28 of file SvgSavingContext.cpp.

29 : mainDevice(_mainDevice)
30 , styleDevice(_styleDevice)
31 , styleWriter(0)
32 , shapeWriter(0)
33 , saveInlineImages(true)
34 {
35 styleWriter.reset(new KoXmlWriter(&styleBuffer, 1));
36 styleWriter->startElement("defs");
37 shapeWriter.reset(new KoXmlWriter(&shapeBuffer, 1));
38
39 const qreal scaleToUserSpace = SvgUtil::toUserSpace(1.0);
40 userSpaceMatrix.scale(scaleToUserSpace, scaleToUserSpace);
41 }
QScopedPointer< KoXmlWriter > shapeWriter
QScopedPointer< KoXmlWriter > styleWriter
static double toUserSpace(double value)
Definition SvgUtil.cpp:34

References SvgUtil::toUserSpace().

◆ saveImage()

QString SvgSavingContext::saveImage ( const QImage & image)

Saves given image and returns the href used.

Definition at line 183 of file SvgSavingContext.cpp.

184{
185 if (isSavingInlineImages()) {
186 QBuffer buffer;
187 buffer.open(QIODevice::WriteOnly);
188 if (image.save(&buffer, "PNG")) {
189 const QString header("data:image/x-png;base64,");
190 return header + buffer.data().toBase64();
191 }
192 } else {
193 // write to a temp file first
194 QTemporaryFile imgFile;
195 if (image.save(&imgFile, "PNG")) {
196 QString dstFilename = createFileName(".png");
197 if (QFile::copy(imgFile.fileName(), dstFilename)) {
198 return dstFilename;
199 }
200 else {
201 QFile f(imgFile.fileName());
202 f.remove();
203 }
204 }
205 }
206
207 return QString();
208}
bool isSavingInlineImages() const
Returns if image should be saved inline.
QString createFileName(const QString &extension)
Create a filename suitable for saving external data.

References createFileName(), and isSavingInlineImages().

◆ setStrippedTextMode()

void SvgSavingContext::setStrippedTextMode ( bool value)

Definition at line 210 of file SvgSavingContext.cpp.

211{
212 d->strippedTextMode = value;
213}
float value(const T *src, size_t ch)

References d, and value().

◆ shapeWriter()

KoXmlWriter & SvgSavingContext::shapeWriter ( )

Provides access to the shape writer.

◆ strippedTextMode()

bool SvgSavingContext::strippedTextMode ( ) const

◆ styleWriter()

KoXmlWriter & SvgSavingContext::styleWriter ( )

Provides access to the style writer.

◆ userSpaceTransform()

QTransform SvgSavingContext::userSpaceTransform ( ) const

Returns the transformation used to transform into user space.

Definition at line 149 of file SvgSavingContext.cpp.

150{
151 return d->userSpaceMatrix;
152}

References d.

Member Data Documentation

◆ d

Private* const SvgSavingContext::d
private

Definition at line 64 of file SvgSavingContext.h.

◆ mainDevice

QIODevice* SvgSavingContext::mainDevice

Definition at line 47 of file SvgSavingContext.cpp.

◆ saveInlineImages

bool SvgSavingContext::saveInlineImages

Definition at line 57 of file SvgSavingContext.cpp.

◆ shapeBuffer

QBuffer SvgSavingContext::shapeBuffer

Definition at line 50 of file SvgSavingContext.cpp.

◆ shapeIds

QHash<const KoShape*, QString> SvgSavingContext::shapeIds

Definition at line 55 of file SvgSavingContext.cpp.

◆ shapeWriter

KoXmlWriter & SvgSavingContext::shapeWriter

Definition at line 52 of file SvgSavingContext.cpp.

◆ strippedTextMode

bool SvgSavingContext::strippedTextMode = false

Definition at line 58 of file SvgSavingContext.cpp.

◆ styleBuffer

QBuffer SvgSavingContext::styleBuffer

Definition at line 49 of file SvgSavingContext.cpp.

◆ styleDevice

QIODevice* SvgSavingContext::styleDevice

Definition at line 48 of file SvgSavingContext.cpp.

◆ styleWriter

KoXmlWriter & SvgSavingContext::styleWriter

Definition at line 51 of file SvgSavingContext.cpp.

◆ uniqueNames

QHash<QString, int> SvgSavingContext::uniqueNames

Definition at line 54 of file SvgSavingContext.cpp.

◆ userSpaceMatrix

QTransform SvgSavingContext::userSpaceMatrix

Definition at line 56 of file SvgSavingContext.cpp.


The documentation for this class was generated from the following files: