1 /* Copyright (C) 2005 The cairomm Development Team
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Library General Public
5 * License as published by the Free Software Foundation; either
6 * version 2 of the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Library General Public License for more details.
13 * You should have received a copy of the GNU Library General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 #include <cairomm/surface.h>
20 #include <cairomm/private.h>
25 Surface::Surface(cairo_surface_t* cobject, bool has_reference)
31 m_cobject = cairo_surface_reference(cobject);
37 cairo_surface_destroy(m_cobject);
40 void Surface::finish()
42 cairo_surface_finish(m_cobject);
43 check_object_status_and_throw_exception(*this);
46 void Surface::get_font_options(FontOptions& options) const
48 cairo_font_options_t* cfontoptions = cairo_font_options_create();
49 cairo_surface_get_font_options(m_cobject, cfontoptions);
50 options = FontOptions(cfontoptions);
51 cairo_font_options_destroy(cfontoptions);
52 check_object_status_and_throw_exception(*this);
57 cairo_surface_flush(m_cobject);
58 check_object_status_and_throw_exception(*this);
61 void Surface::mark_dirty()
63 cairo_surface_mark_dirty(m_cobject);
64 check_object_status_and_throw_exception(*this);
67 void Surface::mark_dirty(int x, int y, int width, int height)
69 cairo_surface_mark_dirty_rectangle(m_cobject, x, y, width, height);
70 check_object_status_and_throw_exception(*this);
73 void Surface::set_device_offset(double x_offset, double y_offset)
75 cairo_surface_set_device_offset(m_cobject, x_offset, y_offset);
76 check_object_status_and_throw_exception(*this);
79 void Surface::get_device_offset(double& x_offset, double& y_offset) const
81 cairo_surface_get_device_offset(const_cast<cobject*>(m_cobject), &x_offset, &y_offset);
84 void Surface::set_fallback_resolution(double x_pixels_per_inch, double y_pixels_per_inch)
86 cairo_surface_set_fallback_resolution(m_cobject, x_pixels_per_inch, y_pixels_per_inch);
87 check_object_status_and_throw_exception(*this);
90 SurfaceType Surface::get_type() const
92 cairo_surface_type_t surface_type = cairo_surface_get_type(m_cobject);
93 check_object_status_and_throw_exception(*this);
94 return static_cast<SurfaceType>(surface_type);
97 #ifdef CAIRO_HAS_PNG_FUNCTIONS
98 void Surface::write_to_png(const std::string& filename)
100 ErrorStatus status = cairo_surface_write_to_png(m_cobject, filename.c_str());
101 check_status_and_throw_exception(status);
104 void Surface::write_to_png(cairo_write_func_t write_func, void *closure)
106 ErrorStatus status = cairo_surface_write_to_png_stream(m_cobject, write_func, closure);
107 check_status_and_throw_exception(status);
111 void Surface::reference() const
113 cairo_surface_reference(m_cobject);
116 void Surface::unreference() const
118 cairo_surface_destroy(m_cobject);
121 RefPtr<Surface> Surface::create(const RefPtr<Surface> other, Content content, int width, int height)
123 cairo_surface_t* cobject = cairo_surface_create_similar(other->m_cobject, (cairo_content_t)content, width, height);
124 check_status_and_throw_exception(cairo_surface_status(cobject));
125 return RefPtr<Surface>(new Surface(cobject, true /* has reference */));
130 ImageSurface::ImageSurface(cairo_surface_t* cobject, bool has_reference)
131 : Surface(cobject, has_reference)
134 ImageSurface::~ImageSurface()
136 // surface is destroyed in base class
139 RefPtr<ImageSurface> ImageSurface::create(Format format, int width, int height)
141 cairo_surface_t* cobject = cairo_image_surface_create((cairo_format_t)format, width, height);
142 check_status_and_throw_exception(cairo_surface_status(cobject));
143 return RefPtr<ImageSurface>(new ImageSurface(cobject, true /* has reference */));
146 RefPtr<ImageSurface> ImageSurface::create(unsigned char* data, Format format, int width, int height, int stride)
148 cairo_surface_t* cobject = cairo_image_surface_create_for_data(data, (cairo_format_t)format, width, height, stride);
149 check_status_and_throw_exception(cairo_surface_status(cobject));
150 return RefPtr<ImageSurface>(new ImageSurface(cobject, true /* has reference */));
153 #ifdef CAIRO_HAS_PNG_FUNCTIONS
155 RefPtr<ImageSurface> ImageSurface::create_from_png(std::string filename)
157 cairo_surface_t* cobject = cairo_image_surface_create_from_png(filename.c_str());
158 check_status_and_throw_exception(cairo_surface_status(cobject));
159 return RefPtr<ImageSurface>(new ImageSurface(cobject, true /* has reference */));
162 RefPtr<ImageSurface> ImageSurface::create_from_png(cairo_read_func_t read_func, void *closure)
164 cairo_surface_t* cobject = cairo_image_surface_create_from_png_stream(read_func, closure);
165 check_status_and_throw_exception(cairo_surface_status(cobject));
166 return RefPtr<ImageSurface>(new ImageSurface(cobject, true /* has reference */));
169 #endif // CAIRO_HAS_PNG_FUNCTIONS
171 int ImageSurface::get_width() const
173 const int result = cairo_image_surface_get_width(m_cobject);
174 check_object_status_and_throw_exception(*this);
178 int ImageSurface::get_height() const
180 const int result = cairo_image_surface_get_height(m_cobject);
181 check_object_status_and_throw_exception(*this);
185 unsigned char* ImageSurface::get_data()
187 return cairo_image_surface_get_data(m_cobject);
190 const unsigned char* ImageSurface::get_data() const
192 return cairo_image_surface_get_data(m_cobject);
195 Format ImageSurface::get_format() const
197 return static_cast<Format>(cairo_image_surface_get_format(m_cobject));
200 int ImageSurface::get_stride() const
202 return cairo_image_surface_get_stride(m_cobject);
206 /*******************************************************************************
207 * THE FOLLOWING SURFACE TYPES ARE EXPERIMENTAL AND NOT FULLY SUPPORTED
208 ******************************************************************************/
210 #ifdef CAIRO_HAS_PDF_SURFACE
212 PdfSurface::PdfSurface(cairo_surface_t* cobject, bool has_reference) :
213 Surface(cobject, has_reference)
216 PdfSurface::~PdfSurface()
218 // surface is destroyed in base class
221 RefPtr<PdfSurface> PdfSurface::create(std::string filename, double width_in_points, double height_in_points)
223 cairo_surface_t* cobject = cairo_pdf_surface_create(filename.c_str(), width_in_points, height_in_points);
224 check_status_and_throw_exception(cairo_surface_status(cobject));
225 return RefPtr<PdfSurface>(new PdfSurface(cobject, true /* has reference */));
228 RefPtr<PdfSurface> PdfSurface::create(cairo_write_func_t write_func, void *closure, double width_in_points, double height_in_points)
230 cairo_surface_t* cobject = cairo_pdf_surface_create_for_stream(write_func, closure, width_in_points, height_in_points);
231 check_status_and_throw_exception(cairo_surface_status(cobject));
232 return RefPtr<PdfSurface>(new PdfSurface(cobject, true /* has reference */));
235 void PdfSurface::set_size(double width_in_points, double height_in_points)
237 cairo_pdf_surface_set_size(m_cobject, width_in_points, height_in_points);
238 check_object_status_and_throw_exception(*this);
241 #endif // CAIRO_HAS_PDF_SURFACE
246 #ifdef CAIRO_HAS_PS_SURFACE
248 PsSurface::PsSurface(cairo_surface_t* cobject, bool has_reference) :
249 Surface(cobject, has_reference)
252 PsSurface::~PsSurface()
254 // surface is destroyed in base class
257 RefPtr<PsSurface> PsSurface::create(std::string filename, double width_in_points, double height_in_points)
259 cairo_surface_t* cobject = cairo_ps_surface_create(filename.c_str(), width_in_points, height_in_points);
260 check_status_and_throw_exception(cairo_surface_status(cobject));
261 return RefPtr<PsSurface>(new PsSurface(cobject, true /* has reference */));
264 RefPtr<PsSurface> PsSurface::create(cairo_write_func_t write_func, void *closure, double width_in_points, double height_in_points)
266 cairo_surface_t* cobject = cairo_ps_surface_create_for_stream(write_func, closure, width_in_points, height_in_points);
267 check_status_and_throw_exception(cairo_surface_status(cobject));
268 return RefPtr<PsSurface>(new PsSurface(cobject, true /* has reference */));
271 void PsSurface::set_size(double width_in_points, double height_in_points)
273 cairo_ps_surface_set_size(m_cobject, width_in_points, height_in_points);
274 check_object_status_and_throw_exception(*this);
278 void PsSurface::dsc_comment(std::string comment)
280 cairo_ps_surface_dsc_comment(m_cobject, comment.c_str());
281 check_object_status_and_throw_exception(*this);
284 void PsSurface::dsc_begin_setup()
286 cairo_ps_surface_dsc_begin_setup(m_cobject);
287 check_object_status_and_throw_exception(*this);
290 void PsSurface::dsc_begin_page_setup()
292 cairo_ps_surface_dsc_begin_page_setup(m_cobject);
293 check_object_status_and_throw_exception(*this);
297 #endif // CAIRO_HAS_PS_SURFACE
302 #ifdef CAIRO_HAS_SVG_SURFACE
304 SvgSurface::SvgSurface(cairo_surface_t* cobject, bool has_reference) :
305 Surface(cobject, has_reference)
308 SvgSurface::~SvgSurface()
310 // surface is destroyed in base class
313 RefPtr<SvgSurface> SvgSurface::create(std::string filename, double width_in_points, double height_in_points)
315 cairo_surface_t* cobject = cairo_svg_surface_create(filename.c_str(), width_in_points, height_in_points);
316 check_status_and_throw_exception(cairo_surface_status(cobject));
317 return RefPtr<SvgSurface>(new SvgSurface(cobject, true /* has reference */));
320 RefPtr<SvgSurface> SvgSurface::create(cairo_write_func_t write_func, void *closure, double width_in_points, double height_in_points)
322 cairo_surface_t* cobject = cairo_svg_surface_create_for_stream(write_func, closure, width_in_points, height_in_points);
323 check_status_and_throw_exception(cairo_surface_status(cobject));
324 return RefPtr<SvgSurface>(new SvgSurface(cobject, true /* has reference */));
327 void SvgSurface::restrict_to_version(SvgVersion version)
329 cairo_svg_surface_restrict_to_version(m_cobject, static_cast<cairo_svg_version_t>(version));
330 check_object_status_and_throw_exception(*this);
333 const std::vector<SvgVersion> SvgSurface::get_versions()
335 cairo_svg_version_t const *versions;
337 cairo_svg_get_versions(&versions, &num_versions);
339 // Just copy the version array out into a std::vector. This is a rarely used
340 // function and the array of versions is going to be very small, so there's no
341 // real performance hit.
342 std::vector<SvgVersion> vec;
343 for (int i = 0; i < num_versions; ++i)
345 vec.push_back(static_cast<SvgVersion>(versions[i]));
350 std::string SvgSurface::version_to_string(SvgVersion version)
352 return std::string(cairo_svg_version_to_string(static_cast<cairo_svg_version_t>(version)));
355 #endif // CAIRO_HAS_SVG_SURFACE
360 #ifdef CAIRO_HAS_GLITZ_SURFACE
362 GlitzSurface::GlitzSurface(cairo_surface_t* cobject, bool has_reference)
363 : Surface(cobject, has_reference)
366 GlitzSurface::~GlitzSurface()
368 // surface is destroyed in base class
371 RefPtr<GlitzSurface> GlitzSurface::create(glitz_surface_t *surface)
373 cairo_surface_t* cobject = cairo_glitz_surface_create(surface);
374 check_status_and_throw_exception(cairo_surface_status(cobject));
375 return RefPtr<GlitzSurface>(new GlitzSurface(cobject, true /* has reference */));
378 #endif // CAIRO_HAS_GLITZ_SURFACE