GRAPHICS: Initial code for SVG rendering

This commit is contained in:
Eugene Sandulenko 2021-03-29 02:03:17 +02:00
parent a5f6866d49
commit 40018d0882
6 changed files with 4650 additions and 0 deletions

View File

@ -36,6 +36,7 @@ MODULE_OBJS := \
scaler/normal.o \
sjis.o \
surface.o \
svg.o \
transform_struct.o \
transform_tools.o \
transparent_surface.o \

View File

@ -0,0 +1,18 @@
Copyright (c) 2013-14 Mikko Mononen memon@inside.org
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.

3018
graphics/nanosvg/nanosvg.h Normal file

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

103
graphics/svg.cpp Normal file
View File

@ -0,0 +1,103 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "common/scummsys.h"
#include "common/stream.h"
#include "common/textconsole.h"
#include "graphics/managed_surface.h"
#include "graphics/pixelformat.h"
#include "graphics/svg.h"
#define NANOSVG_IMPLEMENTATION
#include "graphics/nanosvg/nanosvg.h"
#define NANOSVGRAST_IMPLEMENTATION
#include "graphics/nanosvg/nanosvgrast.h"
namespace Graphics {
SVGBitmap::SVGBitmap(Common::SeekableReadStream *in) {
int32 size = in->size();
char *data = (char *)malloc(size + 1);
in->read(data, size);
data[size] = '\0';
_svg = nsvgParse(data, "px", 96);
if (_svg == NULL)
error("Cannot parse SVG image");
_rasterizer = NULL;
_cachedW = _cachedH = 0;
_cache = NULL;
_render = NULL;
_pixelformat = new Graphics::PixelFormat(4, 8, 8, 8, 8, 24, 16, 8, 0);
}
SVGBitmap::~SVGBitmap() {
if (_rasterizer)
nsvgDeleteRasterizer(_rasterizer);
nsvgDelete(_svg);
if (_cache)
free(_cache);
delete _render;
}
void SVGBitmap::render(Graphics::Surface &target, int dw, int dh) {
if (_rasterizer == NULL)
_rasterizer = nsvgCreateRasterizer();
if (_cachedW != dw || _cachedH != dh) {
if (_cache)
free(_cache);
_cache = (byte *)malloc(dw * dh * 4);
nsvgRasterize(_rasterizer, _svg, 0, 0, 1, _cache, dw, dh, dw * 4);
_cachedW = dw;
_cachedH = dh;
if (_render)
delete _render;
Graphics::Surface tmp;;
tmp.init(dw, dh, dw * 4, _cache, *_pixelformat);
_render = new ManagedSurface(dw, dh, *_pixelformat);
_render->clear(_pixelformat->ARGBToColor(255, 255, 0, 255));
_render->blitFrom(tmp);
tmp.free();
}
target.copyFrom(_render->rawSurface());
}
} // end of namespace Graphics

58
graphics/svg.h Normal file
View File

@ -0,0 +1,58 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef GRAPHICS_SVG_H
#define GRAPHICS_SVG_H
namespace Common {
class SeekableReadStream;
}
struct NSVGimage;
struct NSVGrasterizer;
namespace Graphics {
class ManagedSurface;
struct Surface;
struct PixelFormat;
class SVGBitmap {
public:
SVGBitmap(Common::SeekableReadStream *in);
~SVGBitmap();
void render(Graphics::Surface &target, int dw, int dh);
private:
NSVGimage *_svg;
NSVGrasterizer *_rasterizer;
Graphics::ManagedSurface *_render;
int _cachedW, _cachedH;
byte *_cache;
Graphics::PixelFormat *_pixelformat;
};
} // end of namespace Graphics
#endif // GRAPHICS_SVG_H