GSoC/2022/StatusReports/ReinoldRojas

From KDE Community Wiki
< GSoC‎ | 2022‎ | StatusReports

Export an Image to SVG

SVG is a common file format that Krita is able to read but not fully save as. Currently Krita only supports exporting a single vector layer as SVG. SVG 1.1 and SVG 2 both offer the same functionality for embedded raster images. Considering that svg 2 is not recommended by the World Wide Web Consortium and Krita also only currently supports importing of svg 1.1 so that will be the version we support for exporting as well. This project proposes the ability to export as SVG 1.1 with support for both raster and vector layers.

Mentors

Wolthera van Hövell

Halla Rempt

Links

Bug Report

Merge Request

Blog

  1. Update 1
  2. Update 2
  3. Update 3
  4. Update 4

Goals

  • Krita should be able to export the current file as group SVG
  • Raster layers should be exportable to SVG and saved as embedded PNG’s in the SVG file
  • Vector layers should be saved as a group of strokes
  • A kra-file with layers saved as SVG, should be later be opened by Krita as a document with layers

Work Report

Adding an SVG option

Krita has a class KisMimeDatabase that stores all available file formats Krita supports. Adding a new option to this database is fairly easy as there are plenty of examples in the KisMimeDatabase.cpp.

But just because its in the database doesn’t mean its a valid file format that Krita recognizes. All file formats need a corresponding impex plugin in plugins/impex/$FILE_FORMAT and to be included in plugins/impex/CMakeLists.txt. We can see in plugins/impex/svg/CMakeLists.txt that there is already definitions for svg import so now we just need to figure out the export.

Mostly this is adding kritaimpex to the target libraries and krita_svg.desktop to the ${XDG_APPS_INSTALL_DIR}. We also add a `.desktop` file for Linux desktops so they can understand that Krita can now read svg files.

Boiler plate functionality for svg output

Now that we’ve added all these files to the CMakeLists we actually need to implement some of that code that Krita is looking for. Not everything, but just enough so Krita is happy and we can start testing and iterating. Outside the basic constructor/destructor we can see in other plugins that we need to implement KisImportExportErrorCode convert() and initializeCapabilities(). convert() is what actually converts between Krita’s .kra file and SVG, while initializeCapabilities() tells Krita what features SVG supports.

The convert and initializeCapabilities functions we add some basic stubs for so Krita finds what it expects. Finally, we also make and add plugins/impex/svg/krita_svg_export.json to the plugin ExportFactory.

Pivoting to use a SaveVisitor

Originally I had thought to reuse the svgWriter as I iterated over the different layers in a file. However, using this method would mean I would have to re-implement a way to traverse and account for all different layers and layer configuration. Krita already had the necessary code to do so by implementing the node_visitor class for the svg (created as `KisScalableVectorGraphicsSaveVisitor`). This takes a saveContext that will save the layer information passed from the visit functions in the saveVisitor.

Post GSoC

Its been great working on this project for Krita during these few months. I learned a lot more about Krita and about c++ than when working on smaller bugfixes. After GSoC officially ends I plan to continue on my project as there is still work left to do. Speciically:

  1. Finish transitioning the functionality in svgWriter to the KisScalableVectorGraphicsSaveVisitor
  2. Create an implement a loadVisitor and loadContext to open SVG files
  3. Implement saving rastor images as bitmaps in an svg file