Added svg pixel size dialog (#1911)

This commit is contained in:
Gaspar Capello 2018-11-26 10:55:19 -03:00 committed by David Capello
parent ba0a16cf34
commit c1b61fedbf
5 changed files with 103 additions and 8 deletions

View File

@ -295,6 +295,10 @@
<option id="show_alert" type="bool" default="true" />
<option id="quality" type="double" default="1.0" migrate="JPEG.Quality" />
</section>
<section id="svg">
<option id="show_alert" type="bool" default="true" />
<option id="pixel_scale" type="int" default="1" />
</section>
<section id="webp">
<option id="show_alert" type="bool" default="true" />
<option id="loop" type="bool" default="true" />

View File

@ -1046,6 +1046,7 @@ overwrite_files_on_export_alert = Show warning when overwriting files on File >
overwrite_files_on_export_sprite_sheet_alert = Show warning when overwriting files on Export Sprite Sheet
gif_options_alert = Show GIF options when saving .gif files
jpeg_options_alert = Show JPEG options when saving .jpeg files
svg_options_alert = Show SVG options when saving .svg files
advanced_mode_alert = Show alert when we enter to Advanced Mode
invalid_fg_bg_color_alert = Show alert when drawing with index out of palette bounds
run_script_alert = Show alert when we try to run a script
@ -1234,6 +1235,10 @@ percentage = Percentage:
interpolation = Interpolation:
method = Method:
[svg_options]
title = SVG Options
pixel_scale = Pixel Scale:
[tab_popup_menu]
close = &Close

View File

@ -362,7 +362,9 @@
<check id="gif_options_alert" text="@.gif_options_alert"
pref="gif.show_alert" />
<check id="jpeg_options_alert" text="@.jpeg_options_alert"
pref="jpeg.show_alert" />
pref="jpeg.show_alert" />
<check id="svg_options_alert" text="@.svg_options_alert"
pref="svg.show_alert" />
<check id="advanced_mode_alert" text="@.advanced_mode_alert"
pref="advanced_mode.show_alert" />
<check id="invalid_fg_bg_color_alert" text="@.invalid_fg_bg_color_alert"

View File

@ -0,0 +1,21 @@
<!-- Aseprite -->
<!-- Copyright (C) 2018 by Igara Studio S.A. -->
<gui>
<window id="svg_options" text="@.title">
<grid columns="2">
<label text="@.pixel_scale" />
<expr id="pxsc" magnet="true" cell_align="horizontal"/>
<separator horizontal="true" cell_hspan="2" />
<hbox cell_hspan="2">
<check text="@general.dont_show" id="dont_show" tooltip="@general.dont_show_tooltip" />
<boxfiller />
</hbox>
<box horizontal="true" homogeneous="true" cell_hspan="2" cell_align="right">
<button text="@general.ok" closewindow="true" id="ok" magnet="true" minwidth="60" />
<button text="@general.cancel" closewindow="true" />
</box>
</grid>
</window>
</gui>

View File

@ -9,19 +9,32 @@
#include "config.h"
#endif
#include "app/console.h"
#include "app/context.h"
#include "app/doc.h"
#include "app/file/file.h"
#include "app/file/file_format.h"
#include "app/file/format_options.h"
#include "app/pref/preferences.h"
#include "base/cfile.h"
#include "base/file_handle.h"
#include "doc/doc.h"
#include "ui/window.h"
#include "svg_options.xml.h"
namespace app {
using namespace base;
class SvgFormat : public FileFormat {
// Data for SVG files
class SvgOptions : public FormatOptions {
public:
SvgOptions() : pixelScale(1) { }
int pixelScale;
};
const char* onGetName() const override {
return "svg";
}
@ -40,14 +53,17 @@ class SvgFormat : public FileFormat {
FILE_SUPPORT_RGB |
FILE_SUPPORT_RGBA |
FILE_SUPPORT_GRAY |
FILE_SUPPORT_GRAYA |
FILE_SUPPORT_INDEXED |
FILE_SUPPORT_SEQUENCES;
FILE_SUPPORT_SEQUENCES |
FILE_SUPPORT_GET_FORMAT_OPTIONS;
}
bool onLoad(FileOp* fop) override;
#ifdef ENABLE_SAVE
bool onSave(FileOp* fop) override;
#endif
base::SharedPtr<FormatOptions> onGetFormatOptions(FileOp* fop) override;
};
FileFormat* CreateSvgFormat()
@ -63,17 +79,20 @@ bool SvgFormat::onSave(FileOp* fop)
{
const Image* image = fop->sequenceImage();
int x, y, c, r, g, b, a, alpha;
const base::SharedPtr<SvgOptions> svg_options = fop->formatOptions();
const int pixelScaleValue = MID(0, svg_options->pixelScale, 10000);
FileHandle handle(open_file_with_exception_sync_on_close(fop->filename(), "wb"));
FILE* f = handle.get();
auto printcol = [f](int x, int y, int r, int g, int b, int a) {
fprintf(f, "<rect x=\"%d\" y=\"%d\" width=\"1\" height=\"1\" fill=\"#%02X%02X%02X\" ", x, y, r, g, b);
auto printcol = [f](int x, int y,int r, int g, int b, int a, int pxScale) {
fprintf(f, "<rect x=\"%d\" y=\"%d\" width=\"%d\" height=\"%d\" fill=\"#%02X%02X%02X\" ",
x*pxScale, y*pxScale, pxScale, pxScale, r, g, b);
if (a != 255)
fprintf(f, "opacity=\"%f\" ", (float)a / 255.0);
fprintf(f, "/>\n");
};
fprintf(f, "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
fprintf(f, "<svg version=\"1.1\" width=\"%d\" height=\"%d\" xmlns=\"http://www.w3.org/2000/svg\">\n",
image->width(), image->height());
image->width()*pixelScaleValue, image->height()*pixelScaleValue);
switch (image->pixelFormat()) {
@ -83,7 +102,7 @@ bool SvgFormat::onSave(FileOp* fop)
c = get_pixel_fast<RgbTraits>(image, x, y);
alpha = rgba_geta(c);
if (alpha != 0x00)
printcol(x, y, rgba_getr(c), rgba_getg(c), rgba_getb(c), alpha);
printcol(x, y, rgba_getr(c), rgba_getg(c), rgba_getb(c), alpha, pixelScaleValue);
}
fop->setProgress((float)y / (float)(image->height()));
}
@ -96,7 +115,7 @@ bool SvgFormat::onSave(FileOp* fop)
auto v = graya_getv(c);
alpha = graya_geta(c);
if (alpha != 0x00)
printcol(x, y, v, v, v, alpha);
printcol(x, y, v, v, v, alpha, pixelScaleValue);
}
fop->setProgress((float)y / (float)(image->height()));
}
@ -124,7 +143,8 @@ bool SvgFormat::onSave(FileOp* fop)
printcol(x, y, image_palette[c][0] & 0xff,
image_palette[c][1] & 0xff,
image_palette[c][2] & 0xff,
image_palette[c][3] & 0xff);
image_palette[c][3] & 0xff,
pixelScaleValue);
}
fop->setProgress((float)y / (float)(image->height()));
}
@ -142,4 +162,47 @@ bool SvgFormat::onSave(FileOp* fop)
}
#endif
// Shows the SVG configuration dialog.
base::SharedPtr<FormatOptions> SvgFormat::onGetFormatOptions(FileOp* fop)
{
base::SharedPtr<SvgOptions> svg_options;
if (fop->document()->getFormatOptions())
svg_options = base::SharedPtr<SvgOptions>(fop->document()->getFormatOptions());
if (!svg_options)
svg_options.reset(new SvgOptions);
#ifdef ENABLE_UI
if (fop->context() && fop->context()->isUIAvailable()) {
try {
auto& pref = Preferences::instance();
if (pref.isSet(pref.svg.pixelScale))
svg_options->pixelScale = pref.svg.pixelScale();
if (pref.svg.showAlert()) {
app::gen::SvgOptions win;
win.pxsc()->setTextf("%d", svg_options->pixelScale);
win.openWindowInForeground();
if (win.closer() == win.ok()) {
pref.svg.pixelScale((int)win.pxsc()->textInt());
pref.svg.showAlert(!win.dontShow()->isSelected());
svg_options->pixelScale = pref.svg.pixelScale();
}
else {
svg_options.reset(nullptr);
}
}
}
catch (std::exception& e) {
Console::showException(e);
return base::SharedPtr<SvgOptions>(nullptr);
}
}
#endif
return svg_options;
}
} // namespace app