1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module rsvg.Handle;
26 
27 private import cairo.Context;
28 private import gdkpixbuf.Pixbuf;
29 private import gio.Cancellable;
30 private import gio.FileIF;
31 private import gio.InputStream;
32 private import glib.ConstructionException;
33 private import glib.ErrorG;
34 private import glib.GException;
35 private import glib.Str;
36 private import gobject.ObjectG;
37 private import rsvg.c.functions;
38 public  import rsvg.c.types;
39 
40 
41 /**
42  * The #RsvgHandle is an object representing the parsed form of a SVG
43  */
44 public class Handle : ObjectG
45 {
46 	/** the main Gtk struct */
47 	protected RsvgHandle* rsvgHandle;
48 
49 	/** Get the main Gtk struct */
50 	public RsvgHandle* getHandleStruct(bool transferOwnership = false)
51 	{
52 		if (transferOwnership)
53 			ownedRef = false;
54 		return rsvgHandle;
55 	}
56 
57 	/** the main Gtk struct as a void* */
58 	protected override void* getStruct()
59 	{
60 		return cast(void*)rsvgHandle;
61 	}
62 
63 	protected override void setStruct(GObject* obj)
64 	{
65 		rsvgHandle = cast(RsvgHandle*)obj;
66 		super.setStruct(obj);
67 	}
68 
69 	/**
70 	 * Sets our main struct and passes it to the parent class.
71 	 */
72 	public this (RsvgHandle* rsvgHandle, bool ownedRef = false)
73 	{
74 		this.rsvgHandle = rsvgHandle;
75 		super(cast(GObject*)rsvgHandle, ownedRef);
76 	}
77 
78 
79 	/** */
80 	public static GType getType()
81 	{
82 		return rsvg_handle_get_type();
83 	}
84 
85 	/**
86 	 * Returns a new rsvg handle.  Must be freed with @g_object_unref.  This
87 	 * handle can be used for dynamically loading an image.  You need to feed it
88 	 * data using @rsvg_handle_write, then call @rsvg_handle_close when done.
89 	 * Afterwords, you can render it using Cairo or get a GdkPixbuf from it. When
90 	 * finished, free with g_object_unref(). No more than one image can be loaded
91 	 * with one handle.
92 	 *
93 	 * Returns: A new #RsvgHandle
94 	 *
95 	 * Throws: ConstructionException GTK+ fails to create the object.
96 	 */
97 	public this()
98 	{
99 		auto p = rsvg_handle_new();
100 
101 		if(p is null)
102 		{
103 			throw new ConstructionException("null returned by new");
104 		}
105 
106 		this(cast(RsvgHandle*) p, true);
107 	}
108 
109 	/**
110 	 * Loads the SVG specified by @data.
111 	 *
112 	 * Params:
113 	 *     data = The SVG data
114 	 *
115 	 * Returns: A #RsvgHandle or %NULL if an error occurs.
116 	 *
117 	 * Since: 2.14
118 	 *
119 	 * Throws: GException on failure.
120 	 * Throws: ConstructionException GTK+ fails to create the object.
121 	 */
122 	public this(ubyte[] data)
123 	{
124 		GError* err = null;
125 
126 		auto p = rsvg_handle_new_from_data(data.ptr, cast(size_t)data.length, &err);
127 
128 		if (err !is null)
129 		{
130 			throw new GException( new ErrorG(err) );
131 		}
132 
133 		if(p is null)
134 		{
135 			throw new ConstructionException("null returned by new_from_data");
136 		}
137 
138 		this(cast(RsvgHandle*) p, true);
139 	}
140 
141 	/**
142 	 * Loads the SVG specified by @file_name.
143 	 *
144 	 * Params:
145 	 *     fileName = The file name to load. If built with gnome-vfs, can be a URI.
146 	 *
147 	 * Returns: A #RsvgHandle or %NULL if an error occurs.
148 	 *
149 	 * Since: 2.14
150 	 *
151 	 * Throws: GException on failure.
152 	 * Throws: ConstructionException GTK+ fails to create the object.
153 	 */
154 	public this(string fileName)
155 	{
156 		GError* err = null;
157 
158 		auto p = rsvg_handle_new_from_file(Str.toStringz(fileName), &err);
159 
160 		if (err !is null)
161 		{
162 			throw new GException( new ErrorG(err) );
163 		}
164 
165 		if(p is null)
166 		{
167 			throw new ConstructionException("null returned by new_from_file");
168 		}
169 
170 		this(cast(RsvgHandle*) p, true);
171 	}
172 
173 	/**
174 	 * Creates a new #RsvgHandle for @file.
175 	 *
176 	 * If @cancellable is not %NULL, then the operation can be cancelled by
177 	 * triggering the cancellable object from another thread. If the
178 	 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
179 	 * returned.
180 	 *
181 	 * Params:
182 	 *     file = a #GFile
183 	 *     flags = flags from #RsvgHandleFlags
184 	 *     cancellable = a #GCancellable, or %NULL
185 	 *
186 	 * Returns: a new #RsvgHandle on success, or %NULL with @error filled in
187 	 *
188 	 * Since: 2.32
189 	 *
190 	 * Throws: GException on failure.
191 	 * Throws: ConstructionException GTK+ fails to create the object.
192 	 */
193 	public this(FileIF file, RsvgHandleFlags flags, Cancellable cancellable)
194 	{
195 		GError* err = null;
196 
197 		auto p = rsvg_handle_new_from_gfile_sync((file is null) ? null : file.getFileStruct(), flags, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err);
198 
199 		if (err !is null)
200 		{
201 			throw new GException( new ErrorG(err) );
202 		}
203 
204 		if(p is null)
205 		{
206 			throw new ConstructionException("null returned by new_from_gfile_sync");
207 		}
208 
209 		this(cast(RsvgHandle*) p, true);
210 	}
211 
212 	/**
213 	 * Creates a new #RsvgHandle for @stream.
214 	 *
215 	 * If @cancellable is not %NULL, then the operation can be cancelled by
216 	 * triggering the cancellable object from another thread. If the
217 	 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
218 	 * returned.
219 	 *
220 	 * Params:
221 	 *     inputStream = a #GInputStream
222 	 *     baseFile = a #GFile, or %NULL
223 	 *     flags = flags from #RsvgHandleFlags
224 	 *     cancellable = a #GCancellable, or %NULL
225 	 *
226 	 * Returns: a new #RsvgHandle on success, or %NULL with @error filled in
227 	 *
228 	 * Since: 2.32
229 	 *
230 	 * Throws: GException on failure.
231 	 * Throws: ConstructionException GTK+ fails to create the object.
232 	 */
233 	public this(InputStream inputStream, FileIF baseFile, RsvgHandleFlags flags, Cancellable cancellable)
234 	{
235 		GError* err = null;
236 
237 		auto p = rsvg_handle_new_from_stream_sync((inputStream is null) ? null : inputStream.getInputStreamStruct(), (baseFile is null) ? null : baseFile.getFileStruct(), flags, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err);
238 
239 		if (err !is null)
240 		{
241 			throw new GException( new ErrorG(err) );
242 		}
243 
244 		if(p is null)
245 		{
246 			throw new ConstructionException("null returned by new_from_stream_sync");
247 		}
248 
249 		this(cast(RsvgHandle*) p, true);
250 	}
251 
252 	/**
253 	 * Creates a new #RsvgHandle with flags @flags.
254 	 *
255 	 * Params:
256 	 *     flags = flags from #RsvgHandleFlags
257 	 *
258 	 * Returns: a new #RsvgHandle
259 	 *
260 	 * Since: 2.36
261 	 *
262 	 * Throws: ConstructionException GTK+ fails to create the object.
263 	 */
264 	public this(RsvgHandleFlags flags)
265 	{
266 		auto p = rsvg_handle_new_with_flags(flags);
267 
268 		if(p is null)
269 		{
270 			throw new ConstructionException("null returned by new_with_flags");
271 		}
272 
273 		this(cast(RsvgHandle*) p, true);
274 	}
275 
276 	/**
277 	 * Closes @handle, to indicate that loading the image is complete.  This will
278 	 * return %TRUE if the loader closed successfully.  Note that @handle isn't
279 	 * freed until @g_object_unref is called.
280 	 *
281 	 * Returns: %TRUE on success, or %FALSE on error
282 	 *
283 	 * Throws: GException on failure.
284 	 */
285 	public bool close()
286 	{
287 		GError* err = null;
288 
289 		auto p = rsvg_handle_close(rsvgHandle, &err) != 0;
290 
291 		if (err !is null)
292 		{
293 			throw new GException( new ErrorG(err) );
294 		}
295 
296 		return p;
297 	}
298 
299 	/**
300 	 * Gets the base uri for this #RsvgHandle.
301 	 *
302 	 * Returns: the base uri, possibly null
303 	 *
304 	 * Since: 2.8
305 	 */
306 	public string getBaseUri()
307 	{
308 		return Str.toString(rsvg_handle_get_base_uri(rsvgHandle));
309 	}
310 
311 	/**
312 	 * Get the SVG's size. Do not call from within the size_func callback, because an infinite loop will occur.
313 	 *
314 	 * Params:
315 	 *     dimensionData = A place to store the SVG's size
316 	 *
317 	 * Since: 2.14
318 	 */
319 	public void getDimensions(out RsvgDimensionData dimensionData)
320 	{
321 		rsvg_handle_get_dimensions(rsvgHandle, &dimensionData);
322 	}
323 
324 	/**
325 	 * Get the size of a subelement of the SVG file. Do not call from within the size_func callback, because an infinite loop will occur.
326 	 *
327 	 * Params:
328 	 *     dimensionData = A place to store the SVG's size
329 	 *     id = An element's id within the SVG, or %NULL to get
330 	 *         the dimension of the whole SVG.  For example, if you have a layer
331 	 *         called "layer1" for that you want to get the dimension, pass
332 	 *         "#layer1" as the id.
333 	 *
334 	 * Since: 2.22
335 	 */
336 	public bool getDimensionsSub(out RsvgDimensionData dimensionData, string id)
337 	{
338 		return rsvg_handle_get_dimensions_sub(rsvgHandle, &dimensionData, Str.toStringz(id)) != 0;
339 	}
340 
341 	/**
342 	 * Returns the pixbuf loaded by @handle.  The pixbuf returned will be reffed, so
343 	 * the caller of this function must assume that ref.  If insufficient data has
344 	 * been read to create the pixbuf, or an error occurred in loading, then %NULL
345 	 * will be returned.  Note that the pixbuf may not be complete until
346 	 * @rsvg_handle_close has been called.
347 	 *
348 	 * Returns: the pixbuf loaded by @handle, or %NULL.
349 	 */
350 	public Pixbuf getPixbuf()
351 	{
352 		auto p = rsvg_handle_get_pixbuf(rsvgHandle);
353 
354 		if(p is null)
355 		{
356 			return null;
357 		}
358 
359 		return ObjectG.getDObject!(Pixbuf)(cast(GdkPixbuf*) p, true);
360 	}
361 
362 	/**
363 	 * Returns the pixbuf loaded by @handle.  The pixbuf returned will be reffed, so
364 	 * the caller of this function must assume that ref.  If insufficient data has
365 	 * been read to create the pixbuf, or an error occurred in loading, then %NULL
366 	 * will be returned.  Note that the pixbuf may not be complete until
367 	 * @rsvg_handle_close has been called.
368 	 *
369 	 * Params:
370 	 *     id = The id of an element inside the SVG, or %NULL to
371 	 *         render the whole SVG. For example, if you have a layer called
372 	 *         "layer1" that you wish to render, pass "##layer1" as the id.
373 	 *
374 	 * Returns: the pixbuf loaded by @handle, or %NULL.
375 	 *
376 	 * Since: 2.14
377 	 */
378 	public Pixbuf getPixbufSub(string id)
379 	{
380 		auto p = rsvg_handle_get_pixbuf_sub(rsvgHandle, Str.toStringz(id));
381 
382 		if(p is null)
383 		{
384 			return null;
385 		}
386 
387 		return ObjectG.getDObject!(Pixbuf)(cast(GdkPixbuf*) p, true);
388 	}
389 
390 	/**
391 	 * Get the position of a subelement of the SVG file. Do not call from within
392 	 * the size_func callback, because an infinite loop will occur.
393 	 *
394 	 * Params:
395 	 *     positionData = A place to store the SVG fragment's position.
396 	 *     id = An element's id within the SVG.
397 	 *         For example, if you have a layer called "layer1" for that you want to get
398 	 *         the position, pass "##layer1" as the id.
399 	 *
400 	 * Since: 2.22
401 	 */
402 	public bool getPositionSub(out RsvgPositionData positionData, string id)
403 	{
404 		return rsvg_handle_get_position_sub(rsvgHandle, &positionData, Str.toStringz(id)) != 0;
405 	}
406 
407 	/**
408 	 * Checks whether the element @id exists in the SVG document.
409 	 *
410 	 * Params:
411 	 *     id = an element's id within the SVG
412 	 *
413 	 * Returns: %TRUE if @id exists in the SVG document
414 	 *
415 	 * Since: 2.22
416 	 */
417 	public bool hasSub(string id)
418 	{
419 		return rsvg_handle_has_sub(rsvgHandle, Str.toStringz(id)) != 0;
420 	}
421 
422 	/** */
423 	public void internalSetTesting(bool testing)
424 	{
425 		rsvg_handle_internal_set_testing(rsvgHandle, testing);
426 	}
427 
428 	/**
429 	 * Reads @stream and writes the data from it to @handle.
430 	 *
431 	 * If @cancellable is not %NULL, then the operation can be cancelled by
432 	 * triggering the cancellable object from another thread. If the
433 	 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
434 	 * returned.
435 	 *
436 	 * Params:
437 	 *     stream = a #GInputStream
438 	 *     cancellable = a #GCancellable, or %NULL
439 	 *
440 	 * Returns: %TRUE if reading @stream succeeded, or %FALSE otherwise
441 	 *     with @error filled in
442 	 *
443 	 * Since: 2.32
444 	 *
445 	 * Throws: GException on failure.
446 	 */
447 	public bool readStreamSync(InputStream stream, Cancellable cancellable)
448 	{
449 		GError* err = null;
450 
451 		auto p = rsvg_handle_read_stream_sync(rsvgHandle, (stream is null) ? null : stream.getInputStreamStruct(), (cancellable is null) ? null : cancellable.getCancellableStruct(), &err) != 0;
452 
453 		if (err !is null)
454 		{
455 			throw new GException( new ErrorG(err) );
456 		}
457 
458 		return p;
459 	}
460 
461 	/**
462 	 * Draws a SVG to a Cairo surface
463 	 *
464 	 * Params:
465 	 *     cr = A Cairo renderer
466 	 *
467 	 * Returns: %TRUE if drawing succeeded.
468 	 *
469 	 * Since: 2.14
470 	 */
471 	public bool renderCairo(Context cr)
472 	{
473 		return rsvg_handle_render_cairo(rsvgHandle, (cr is null) ? null : cr.getContextStruct()) != 0;
474 	}
475 
476 	/**
477 	 * Draws a subset of a SVG to a Cairo surface
478 	 *
479 	 * Params:
480 	 *     cr = A Cairo renderer
481 	 *     id = An element's id within the SVG, or %NULL to render
482 	 *         the whole SVG. For example, if you have a layer called "layer1"
483 	 *         that you wish to render, pass "##layer1" as the id.
484 	 *
485 	 * Returns: %TRUE if drawing succeeded.
486 	 *
487 	 * Since: 2.14
488 	 */
489 	public bool renderCairoSub(Context cr, string id)
490 	{
491 		return rsvg_handle_render_cairo_sub(rsvgHandle, (cr is null) ? null : cr.getContextStruct(), Str.toStringz(id)) != 0;
492 	}
493 
494 	/**
495 	 * Set the base URI for @handle from @file.
496 	 * Note: This function may only be called before rsvg_handle_write()
497 	 * or rsvg_handle_read_stream_sync() has been called.
498 	 *
499 	 * Params:
500 	 *     baseFile = a #GFile
501 	 *
502 	 * Since: 2.32
503 	 */
504 	public void setBaseGfile(FileIF baseFile)
505 	{
506 		rsvg_handle_set_base_gfile(rsvgHandle, (baseFile is null) ? null : baseFile.getFileStruct());
507 	}
508 
509 	/**
510 	 * Set the base URI for this SVG. This can only be called before rsvg_handle_write()
511 	 * has been called.
512 	 *
513 	 * Params:
514 	 *     baseUri = The base uri
515 	 *
516 	 * Since: 2.9
517 	 */
518 	public void setBaseUri(string baseUri)
519 	{
520 		rsvg_handle_set_base_uri(rsvgHandle, Str.toStringz(baseUri));
521 	}
522 
523 	/**
524 	 * Sets the DPI for the outgoing pixbuf. Common values are
525 	 * 75, 90, and 300 DPI. Passing a number <= 0 to @dpi will
526 	 * reset the DPI to whatever the default value happens to be.
527 	 *
528 	 * Params:
529 	 *     dpi = Dots Per Inch (aka Pixels Per Inch)
530 	 *
531 	 * Since: 2.8
532 	 */
533 	public void setDpi(double dpi)
534 	{
535 		rsvg_handle_set_dpi(rsvgHandle, dpi);
536 	}
537 
538 	/**
539 	 * Sets the DPI for the outgoing pixbuf. Common values are
540 	 * 75, 90, and 300 DPI. Passing a number <= 0 to #dpi_x or @dpi_y will
541 	 * reset the DPI to whatever the default value happens to be.
542 	 *
543 	 * Params:
544 	 *     dpiX = Dots Per Inch (aka Pixels Per Inch)
545 	 *     dpiY = Dots Per Inch (aka Pixels Per Inch)
546 	 *
547 	 * Since: 2.8
548 	 */
549 	public void setDpiXY(double dpiX, double dpiY)
550 	{
551 		rsvg_handle_set_dpi_x_y(rsvgHandle, dpiX, dpiY);
552 	}
553 
554 	/**
555 	 * Loads the next @count bytes of the image.  This will return %TRUE if the data
556 	 * was loaded successful, and %FALSE if an error occurred.  In the latter case,
557 	 * the loader will be closed, and will not accept further writes. If %FALSE is
558 	 * returned, @error will be set to an error from the #RsvgError domain. Errors
559 	 * from #GIOErrorEnum are also possible.
560 	 *
561 	 * Params:
562 	 *     buf = pointer to svg data
563 	 *
564 	 * Returns: %TRUE on success, or %FALSE on error
565 	 *
566 	 * Throws: GException on failure.
567 	 */
568 	public bool write(char[] buf)
569 	{
570 		GError* err = null;
571 
572 		auto p = rsvg_handle_write(rsvgHandle, buf.ptr, cast(size_t)buf.length, &err) != 0;
573 
574 		if (err !is null)
575 		{
576 			throw new GException( new ErrorG(err) );
577 		}
578 
579 		return p;
580 	}
581 
582 	/**
583 	 * Sets the DPI for the all future outgoing pixbufs. Common values are
584 	 * 75, 90, and 300 DPI. Passing a number <= 0 to @dpi will
585 	 * reset the DPI to whatever the default value happens to be.
586 	 *
587 	 * Params:
588 	 *     dpi = Dots Per Inch (aka Pixels Per Inch)
589 	 *
590 	 * Since: 2.8
591 	 */
592 	public static void setDefaultDpi(double dpi)
593 	{
594 		rsvg_set_default_dpi(dpi);
595 	}
596 
597 	/**
598 	 * Sets the DPI for the all future outgoing pixbufs. Common values are
599 	 * 75, 90, and 300 DPI. Passing a number <= 0 to @dpi will
600 	 * reset the DPI to whatever the default value happens to be.
601 	 *
602 	 * Params:
603 	 *     dpiX = Dots Per Inch (aka Pixels Per Inch)
604 	 *     dpiY = Dots Per Inch (aka Pixels Per Inch)
605 	 *
606 	 * Since: 2.8
607 	 */
608 	public static void setDefaultDpiXY(double dpiX, double dpiY)
609 	{
610 		rsvg_set_default_dpi_x_y(dpiX, dpiY);
611 	}
612 }