Minsky: 3.17.0
SVGItem.cc
Go to the documentation of this file.
1 /*
2  @copyright Steve Keen 2012
3  @author Russell Standish
4  This file is part of Minsky.
5 
6  Minsky is free software: you can redistribute it and/or modify it
7  under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Minsky is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with Minsky. If not, see <http://www.gnu.org/licenses/>.
18 */
19 // Implementations of canvas items representing operations and variables.
20 
21 /*
22  @copyright Steve Keen 2014
23  @author Russell Standish
24  This file is part of Minsky.
25 
26  Minsky is free software: you can redistribute it and/or modify it
27  under the terms of the GNU General Public License as published by
28  the Free Software Foundation, either version 3 of the License, or
29  (at your option) any later version.
30 
31  Minsky is distributed in the hope that it will be useful,
32  but WITHOUT ANY WARRANTY; without even the implied warranty of
33  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34  GNU General Public License for more details.
35 
36  You should have received a copy of the GNU General Public License
37  along with Minsky. If not, see <http://www.gnu.org/licenses/>.
38 */
39 #include "SVGItem.h"
40 #include "SVGItem.rcd"
41 #include "cairo_base.h"
42 #include "minsky_epilogue.h"
43 #include <librsvg-2.0/librsvg/rsvg.h>
44 
45 // if not #ifdef protected, you get a deprecated warning, which is
46 // made fatal by -Werror
47 #ifndef RSVG_CAIRO_H
48 #include <librsvg-2.0/librsvg/rsvg-cairo.h>
49 #endif
50 
51 #include <stdexcept>
52 using namespace std;
53 
54 namespace minsky
55 {
56 
57  void SVGRenderer::setResource(const std::string& resource)
58  {
59  if (svg) g_object_unref(svg);
60  GError* err=nullptr;
61  svg=rsvg_handle_new_from_file(resource.c_str(),&err);
62  if (err)
63  {
64  std::string msg="SVGRenderer failed to initialise: ";
65  msg+=err->message;
66  g_error_free(err);
67  throw runtime_error(msg);
68  }
69 #ifdef MXE // MXE doesn't currently have a Rust compiler, so librsvg can be no later than 2.40.21
70  RsvgDimensionData dims;
71  rsvg_handle_get_dimensions(svg, &dims);
72  m_width=dims.width;
73  m_height=dims.height;
74 #endif
75  }
76 
77 
78  SVGRenderer::~SVGRenderer()
79  {
80  if (svg)
81  g_object_unref(svg);
82  }
83 
84  void SVGRenderer::render(cairo_t* cairo, double width, double height) const
85  {
86  if (svg)
87 #ifdef MXE // MXE doesn't currently have a Rust compiler, so librsvg can be no later than 2.40.21
88  cairo_scale(cairo,width/m_width, height/m_height);
89  rsvg_handle_render_cairo(svg,cairo);
90 #else
91  {
92  GError* err=nullptr;
93  const RsvgRectangle rect{0,0,width,height};
94  rsvg_handle_render_document(svg,cairo,&rect,&err);
95  }
96 #endif
97  }
98 
99 }
STL namespace.
CLASSDESC_ACCESS_EXPLICIT_INSTANTIATION(minsky::SVGRenderer)