Browse Source

add rsvg support

master
Alexandros Theodotou 3 years ago
parent
commit
284a88ba80
Signed by: alex
GPG Key ID: 022EAE42313D70F3
  1. 6
      README.md
  2. 61
      inc/ztoolkit/rsvg.h
  3. 1
      inc/ztoolkit/ztk.h
  4. 7
      meson.build
  5. 6
      meson_options.txt
  6. 1
      src/meson.build
  7. 109
      src/rsvg.c
  8. 10
      src/ztk_color.c
  9. 10
      tests/meson.build
  10. 39
      tests/rsvg.c
  11. 71
      tests/test.svg

6
README.md

@ -16,12 +16,14 @@ This is a work in progress. @@ -16,12 +16,14 @@ This is a work in progress.
Dependencies
------------
Cairo and X11
Required: Cairo and X11
Optional: Librsvg (for SVG support)
Building
--------
meson build
meson build -Denable_rsvg=true
ninja -C build
ninja -C build install

61
inc/ztoolkit/rsvg.h

@ -0,0 +1,61 @@ @@ -0,0 +1,61 @@
/*
* Copyright (C) 2020 Alexandros Theodotou <alex at zrythm dot org>
*
* This file is part of ZToolkit
*
* ZToolkit is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* ZToolkit 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU General Affero Public License
* along with ZToolkit. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef __ZTOOLKIT_RSVG_H__
#define __ZTOOLKIT_RSVG_H__
#include <cairo.h>
typedef void ZtkRsvgHandle;
/**
* Loads an SVG from an absolute path.
*
* @return An rsvg handle, or NULL if failed.
*/
ZtkRsvgHandle *
ztk_rsvg_load_svg (
const char * abs_path);
/**
* Gets the width of the svg.
*/
int
ztk_rsvg_get_width (
ZtkRsvgHandle * handle);
/**
* Gets the height of the svg.
*/
int
ztk_rsvg_get_height (
ZtkRsvgHandle * handle);
/**
* Draws the SVG on the current cairo context.
*
* @return 0 if successful, non-zero if failed.
*/
int
ztk_rsvg_draw (
ZtkRsvgHandle * handle,
cairo_t * cr,
ZtkRect * rect);
#endif // header guard

1
inc/ztoolkit/ztk.h

@ -22,6 +22,7 @@ @@ -22,6 +22,7 @@
#include "ztoolkit/log.h"
#include "ztoolkit/types.h"
#include "ztoolkit/rsvg.h"
#include "ztoolkit/ztk_app.h"
#include "ztoolkit/ztk_color.h"
#include "ztoolkit/ztk_drawing_area.h"

7
meson.build

@ -76,14 +76,15 @@ endif @@ -76,14 +76,15 @@ endif
# create config.h
config_h = configure_file (
output: 'config.h',
output: 'ztoolkit_config.h',
configuration: cdata,
)
# include dirs
inc_dirs = [
include_directories ('inc'),
include_directories ('pugl'),
include_directories('.'),
include_directories('inc'),
include_directories('pugl'),
]
# cflags

6
meson_options.txt

@ -27,3 +27,9 @@ option ( @@ -27,3 +27,9 @@ option (
value: false,
description: '''Enable SVG support through librsvg.
This requires the whole glib stack.''')
option (
'strict_flags',
type: 'boolean',
value: false,
description: 'Strict compilation flags')

1
src/meson.build

@ -17,6 +17,7 @@ @@ -17,6 +17,7 @@
ztoolkit_srcs = files([
'log.c',
'rsvg.c',
'ztk_app.c',
'ztk_color.c',
'ztk_drawing_area.c',

109
src/rsvg.c

@ -0,0 +1,109 @@ @@ -0,0 +1,109 @@
/*
* Copyright (C) 2020 Alexandros Theodotou <alex at zrythm dot org>
*
* This file is part of ZToolkit
*
* ZToolkit is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* ZToolkit 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU General Affero Public License
* along with ZToolkit. If not, see <https://www.gnu.org/licenses/>.
*/
#include "ztoolkit_config.h"
#ifdef HAVE_RSVG
#include <ztoolkit/ztk.h>
#include <librsvg/rsvg.h>
/**
* Loads an SVG from an absolute path.
*
* @return An rsvg handle, or NULL if failed.
*/
ZtkRsvgHandle *
ztk_rsvg_load_svg (
const char * abs_path)
{
GFile * file =
g_file_new_for_path (abs_path);
GError * err = NULL;
RsvgHandle * handle =
rsvg_handle_new_from_gfile_sync (
file, RSVG_HANDLE_FLAGS_NONE, NULL, &err);
if (err)
{
ztk_error (
"An error occurred parsing the SVG file at "
"%s: %s", abs_path);
g_object_unref (file);
return NULL;
}
g_object_unref (file);
/* common values are 75, 90, 300 */
rsvg_handle_set_dpi (handle, 300);
return (ZtkRsvgHandle *) handle;
}
/**
* Gets the width of the svg.
*/
int
ztk_rsvg_get_width (
ZtkRsvgHandle * handle)
{
RsvgDimensionData dim;
rsvg_handle_get_dimensions (
(RsvgHandle *) handle, &dim);
return dim.width;
}
/**
* Gets the height of the svg.
*/
int
ztk_rsvg_get_height (
ZtkRsvgHandle * handle)
{
RsvgDimensionData dim;
rsvg_handle_get_dimensions (
(RsvgHandle *) handle, &dim);
return dim.height;
}
/**
* Draws the SVG on the current cairo context.
*
* @return 0 if successful, non-zero if failed.
*/
int
ztk_rsvg_draw (
ZtkRsvgHandle * handle,
cairo_t * cr,
ZtkRect * rect)
{
RsvgRectangle viewport = {
rect->x, rect->y, rect->width, rect->height };
GError * err = NULL;
rsvg_handle_render_document (
(RsvgHandle *) handle, cr, &viewport, &err);
if (err)
{
return -1;
}
return 0;
}
#endif // HAVE_RSVG

10
src/ztk_color.c

@ -1,20 +1,20 @@ @@ -1,20 +1,20 @@
/*
* Copyright (C) 2019 Alexandros Theodotou <alex at zrythm dot org>
* Copyright (C) 2019-2020 Alexandros Theodotou <alex at zrythm dot org>
*
* This file is part of ZPlugins
* This file is part of ZToolkit
*
* ZPlugins is free software: you can redistribute it and/or modify
* ZToolkit is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* ZPlugins is distributed in the hope that it will be useful,
* ZToolkit 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU General Affero Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
* along with ZToolkit. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdio.h>

10
tests/meson.build

@ -29,3 +29,13 @@ e = executable ( @@ -29,3 +29,13 @@ e = executable (
dependencies: deps,
)
test ('color_test', e)
e = executable (
'rsvg', 'rsvg.c',
include_directories: inc_dirs,
link_with: ztoolkit_lib,
dependencies: deps,
)
test ('rsvg_test', e,
args: join_paths(
meson.current_source_dir(), 'test.svg'))

39
tests/rsvg.c

@ -0,0 +1,39 @@ @@ -0,0 +1,39 @@
/*
* Copyright (C) 2020 Alexandros Theodotou <alex at zrythm dot org>
*
* This file is part of ZToolkit
*
* ZToolkit is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ZToolkit 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with ZToolkit. If not, see <https://www.gnu.org/licenses/>.
*/
#include "helper.h"
#include <ztoolkit/ztk.h>
int main (
int argc, const char* argv[])
{
ZtkRsvgHandle * handle =
ztk_rsvg_load_svg (argv[1]);
ztk_assert (handle);
int width =
ztk_rsvg_get_width (handle);
ztk_assert (width == 32);
int height =
ztk_rsvg_get_height (handle);
ztk_assert (height == 32);
return 0;
}

71
tests/test.svg

@ -0,0 +1,71 @@ @@ -0,0 +1,71 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
width="32"
height="32"
viewBox="0 0 8.4666666 8.4666666"
version="1.1"
id="svg1923"
inkscape:version="0.92.4 (5da689c313, 2019-01-14)"
sodipodi:docname="test.svg">
<defs
id="defs1917" />
<sodipodi:namedview
id="base"
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1.0"
inkscape:pageopacity="0.0"
inkscape:pageshadow="2"
inkscape:zoom="15.839192"
inkscape:cx="13.04635"
inkscape:cy="12.983332"
inkscape:document-units="mm"
inkscape:current-layer="layer1"
showgrid="false"
units="px"
inkscape:window-width="1920"
inkscape:window-height="1025"
inkscape:window-x="0"
inkscape:window-y="27"
inkscape:window-maximized="1" />
<metadata
id="metadata1920">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
<dc:title></dc:title>
</cc:Work>
</rdf:RDF>
</metadata>
<g
inkscape:label="Layer 1"
inkscape:groupmode="layer"
id="layer1"
transform="translate(0,-288.53333)">
<text
inkscape:export-ydpi="96"
inkscape:export-xdpi="96"
id="text2281"
y="293.84329"
x="0.50109881"
style="font-style:normal;font-variant:normal;font-weight:bold;font-stretch:normal;font-size:2.68319941px;line-height:1.25;font-family:Cantarell;-inkscape-font-specification:'Cantarell Bold';letter-spacing:0px;word-spacing:0px;display:inline;fill:#cfcfcd;fill-opacity:1;stroke:none;stroke-width:0.06707997"
xml:space="preserve"><tspan
style="font-style:normal;font-variant:normal;font-weight:bold;font-stretch:normal;font-size:3.10266089px;font-family:Cantarell;-inkscape-font-specification:'Cantarell Bold';fill:#cfcfcd;fill-opacity:1;stroke-width:0.06707997"
y="293.84329"
x="0.50109881"
id="tspan2279"
sodipodi:role="line">TEST</tspan></text>
</g>
</svg>

After

Width:  |  Height:  |  Size: 2.5 KiB

Loading…
Cancel
Save