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 gtkc.rsvg; 38 public import gtkc.rsvgtypes; 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 * dataLen = The length of @data, in bytes 115 * 116 * Returns: A #RsvgHandle or %NULL if an error occurs. 117 * 118 * Since: 2.14 119 * 120 * Throws: GException on failure. 121 * Throws: ConstructionException GTK+ fails to create the object. 122 */ 123 public this(ubyte[] data) 124 { 125 GError* err = null; 126 127 auto p = rsvg_handle_new_from_data(data.ptr, cast(size_t)data.length, &err); 128 129 if (err !is null) 130 { 131 throw new GException( new ErrorG(err) ); 132 } 133 134 if(p is null) 135 { 136 throw new ConstructionException("null returned by new_from_data"); 137 } 138 139 this(cast(RsvgHandle*) p, true); 140 } 141 142 /** 143 * Loads the SVG specified by @file_name. 144 * 145 * Params: 146 * fileName = The file name to load. If built with gnome-vfs, can be a URI. 147 * 148 * Returns: A #RsvgHandle or %NULL if an error occurs. 149 * 150 * Since: 2.14 151 * 152 * Throws: GException on failure. 153 * Throws: ConstructionException GTK+ fails to create the object. 154 */ 155 public this(string fileName) 156 { 157 GError* err = null; 158 159 auto p = rsvg_handle_new_from_file(Str.toStringz(fileName), &err); 160 161 if (err !is null) 162 { 163 throw new GException( new ErrorG(err) ); 164 } 165 166 if(p is null) 167 { 168 throw new ConstructionException("null returned by new_from_file"); 169 } 170 171 this(cast(RsvgHandle*) p, true); 172 } 173 174 /** 175 * Creates a new #RsvgHandle for @file. 176 * 177 * If @cancellable is not %NULL, then the operation can be cancelled by 178 * triggering the cancellable object from another thread. If the 179 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be 180 * returned. 181 * 182 * Params: 183 * file = a #GFile 184 * flags = flags from #RsvgHandleFlags 185 * cancellable = a #GCancellable, or %NULL 186 * 187 * Returns: a new #RsvgHandle on success, or %NULL with @error filled in 188 * 189 * Since: 2.32 190 * 191 * Throws: GException on failure. 192 * Throws: ConstructionException GTK+ fails to create the object. 193 */ 194 public this(FileIF file, RsvgHandleFlags flags, Cancellable cancellable) 195 { 196 GError* err = null; 197 198 auto p = rsvg_handle_new_from_gfile_sync((file is null) ? null : file.getFileStruct(), flags, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err); 199 200 if (err !is null) 201 { 202 throw new GException( new ErrorG(err) ); 203 } 204 205 if(p is null) 206 { 207 throw new ConstructionException("null returned by new_from_gfile_sync"); 208 } 209 210 this(cast(RsvgHandle*) p, true); 211 } 212 213 /** 214 * Creates a new #RsvgHandle for @stream. 215 * 216 * If @cancellable is not %NULL, then the operation can be cancelled by 217 * triggering the cancellable object from another thread. If the 218 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be 219 * returned. 220 * 221 * Params: 222 * inputStream = a #GInputStream 223 * baseFile = a #GFile, or %NULL 224 * flags = flags from #RsvgHandleFlags 225 * cancellable = a #GCancellable, or %NULL 226 * 227 * Returns: a new #RsvgHandle on success, or %NULL with @error filled in 228 * 229 * Since: 2.32 230 * 231 * Throws: GException on failure. 232 * Throws: ConstructionException GTK+ fails to create the object. 233 */ 234 public this(InputStream inputStream, FileIF baseFile, RsvgHandleFlags flags, Cancellable cancellable) 235 { 236 GError* err = null; 237 238 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); 239 240 if (err !is null) 241 { 242 throw new GException( new ErrorG(err) ); 243 } 244 245 if(p is null) 246 { 247 throw new ConstructionException("null returned by new_from_stream_sync"); 248 } 249 250 this(cast(RsvgHandle*) p, true); 251 } 252 253 /** 254 * Creates a new #RsvgHandle with flags @flags. 255 * 256 * Params: 257 * flags = flags from #RsvgHandleFlags 258 * 259 * Returns: a new #RsvgHandle 260 * 261 * Since: 2.36 262 * 263 * Throws: ConstructionException GTK+ fails to create the object. 264 */ 265 public this(RsvgHandleFlags flags) 266 { 267 auto p = rsvg_handle_new_with_flags(flags); 268 269 if(p is null) 270 { 271 throw new ConstructionException("null returned by new_with_flags"); 272 } 273 274 this(cast(RsvgHandle*) p, true); 275 } 276 277 /** 278 * Closes @handle, to indicate that loading the image is complete. This will 279 * return %TRUE if the loader closed successfully. Note that @handle isn't 280 * freed until @g_object_unref is called. 281 * 282 * Returns: %TRUE on success, or %FALSE on error 283 * 284 * Throws: GException on failure. 285 */ 286 public bool close() 287 { 288 GError* err = null; 289 290 auto p = rsvg_handle_close(rsvgHandle, &err) != 0; 291 292 if (err !is null) 293 { 294 throw new GException( new ErrorG(err) ); 295 } 296 297 return p; 298 } 299 300 /** 301 * Gets the base uri for this #RsvgHandle. 302 * 303 * Returns: the base uri, possibly null 304 * 305 * Since: 2.8 306 */ 307 public string getBaseUri() 308 { 309 return Str.toString(rsvg_handle_get_base_uri(rsvgHandle)); 310 } 311 312 /** 313 * Get the SVG's size. Do not call from within the size_func callback, because an infinite loop will occur. 314 * 315 * Params: 316 * dimensionData = A place to store the SVG's size 317 * 318 * Since: 2.14 319 */ 320 public void getDimensions(out RsvgDimensionData dimensionData) 321 { 322 rsvg_handle_get_dimensions(rsvgHandle, &dimensionData); 323 } 324 325 /** 326 * 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. 327 * 328 * Params: 329 * dimensionData = A place to store the SVG's size 330 * id = An element's id within the SVG, or %NULL to get 331 * the dimension of the whole SVG. For example, if you have a layer 332 * called "layer1" for that you want to get the dimension, pass 333 * "#layer1" as the id. 334 * 335 * Since: 2.22 336 */ 337 public bool getDimensionsSub(out RsvgDimensionData dimensionData, string id) 338 { 339 return rsvg_handle_get_dimensions_sub(rsvgHandle, &dimensionData, Str.toStringz(id)) != 0; 340 } 341 342 /** 343 * Returns the pixbuf loaded by @handle. The pixbuf returned will be reffed, so 344 * the caller of this function must assume that ref. If insufficient data has 345 * been read to create the pixbuf, or an error occurred in loading, then %NULL 346 * will be returned. Note that the pixbuf may not be complete until 347 * @rsvg_handle_close has been called. 348 * 349 * Returns: the pixbuf loaded by @handle, or %NULL. 350 */ 351 public Pixbuf getPixbuf() 352 { 353 auto p = rsvg_handle_get_pixbuf(rsvgHandle); 354 355 if(p is null) 356 { 357 return null; 358 } 359 360 return ObjectG.getDObject!(Pixbuf)(cast(GdkPixbuf*) p, true); 361 } 362 363 /** 364 * Returns the pixbuf loaded by @handle. The pixbuf returned will be reffed, so 365 * the caller of this function must assume that ref. If insufficient data has 366 * been read to create the pixbuf, or an error occurred in loading, then %NULL 367 * will be returned. Note that the pixbuf may not be complete until 368 * @rsvg_handle_close has been called. 369 * 370 * Params: 371 * id = The id of an element inside the SVG, or %NULL to 372 * render the whole SVG. For example, if you have a layer called 373 * "layer1" that you wish to render, pass "##layer1" as the id. 374 * 375 * Returns: the pixbuf loaded by @handle, or %NULL. 376 * 377 * Since: 2.14 378 */ 379 public Pixbuf getPixbufSub(string id) 380 { 381 auto p = rsvg_handle_get_pixbuf_sub(rsvgHandle, Str.toStringz(id)); 382 383 if(p is null) 384 { 385 return null; 386 } 387 388 return ObjectG.getDObject!(Pixbuf)(cast(GdkPixbuf*) p, true); 389 } 390 391 /** 392 * Get the position of a subelement of the SVG file. Do not call from within 393 * the size_func callback, because an infinite loop will occur. 394 * 395 * Params: 396 * positionData = A place to store the SVG fragment's position. 397 * id = An element's id within the SVG. 398 * For example, if you have a layer called "layer1" for that you want to get 399 * the position, pass "##layer1" as the id. 400 * 401 * Since: 2.22 402 */ 403 public bool getPositionSub(out RsvgPositionData positionData, string id) 404 { 405 return rsvg_handle_get_position_sub(rsvgHandle, &positionData, Str.toStringz(id)) != 0; 406 } 407 408 /** 409 * Checks whether the element @id exists in the SVG document. 410 * 411 * Params: 412 * id = an element's id within the SVG 413 * 414 * Returns: %TRUE if @id exists in the SVG document 415 * 416 * Since: 2.22 417 */ 418 public bool hasSub(string id) 419 { 420 return rsvg_handle_has_sub(rsvgHandle, Str.toStringz(id)) != 0; 421 } 422 423 /** 424 * Reads @stream and writes the data from it to @handle. 425 * 426 * If @cancellable is not %NULL, then the operation can be cancelled by 427 * triggering the cancellable object from another thread. If the 428 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be 429 * returned. 430 * 431 * Params: 432 * stream = a #GInputStream 433 * cancellable = a #GCancellable, or %NULL 434 * 435 * Returns: %TRUE if reading @stream succeeded, or %FALSE otherwise 436 * with @error filled in 437 * 438 * Since: 2.32 439 * 440 * Throws: GException on failure. 441 */ 442 public bool readStreamSync(InputStream stream, Cancellable cancellable) 443 { 444 GError* err = null; 445 446 auto p = rsvg_handle_read_stream_sync(rsvgHandle, (stream is null) ? null : stream.getInputStreamStruct(), (cancellable is null) ? null : cancellable.getCancellableStruct(), &err) != 0; 447 448 if (err !is null) 449 { 450 throw new GException( new ErrorG(err) ); 451 } 452 453 return p; 454 } 455 456 /** 457 * Draws a SVG to a Cairo surface 458 * 459 * Params: 460 * cr = A Cairo renderer 461 * 462 * Returns: %TRUE if drawing succeeded. 463 * 464 * Since: 2.14 465 */ 466 public bool renderCairo(Context cr) 467 { 468 return rsvg_handle_render_cairo(rsvgHandle, (cr is null) ? null : cr.getContextStruct()) != 0; 469 } 470 471 /** 472 * Draws a subset of a SVG to a Cairo surface 473 * 474 * Params: 475 * cr = A Cairo renderer 476 * id = An element's id within the SVG, or %NULL to render 477 * the whole SVG. For example, if you have a layer called "layer1" 478 * that you wish to render, pass "##layer1" as the id. 479 * 480 * Returns: %TRUE if drawing succeeded. 481 * 482 * Since: 2.14 483 */ 484 public bool renderCairoSub(Context cr, string id) 485 { 486 return rsvg_handle_render_cairo_sub(rsvgHandle, (cr is null) ? null : cr.getContextStruct(), Str.toStringz(id)) != 0; 487 } 488 489 /** 490 * Set the base URI for @handle from @file. 491 * Note: This function may only be called before rsvg_handle_write() 492 * or rsvg_handle_read_stream_sync() has been called. 493 * 494 * Params: 495 * baseFile = a #GFile 496 * 497 * Since: 2.32 498 */ 499 public void setBaseGfile(FileIF baseFile) 500 { 501 rsvg_handle_set_base_gfile(rsvgHandle, (baseFile is null) ? null : baseFile.getFileStruct()); 502 } 503 504 /** 505 * Set the base URI for this SVG. This can only be called before rsvg_handle_write() 506 * has been called. 507 * 508 * Params: 509 * baseUri = The base uri 510 * 511 * Since: 2.9 512 */ 513 public void setBaseUri(string baseUri) 514 { 515 rsvg_handle_set_base_uri(rsvgHandle, Str.toStringz(baseUri)); 516 } 517 518 /** 519 * Sets the DPI for the outgoing pixbuf. Common values are 520 * 75, 90, and 300 DPI. Passing a number <= 0 to @dpi will 521 * reset the DPI to whatever the default value happens to be. 522 * 523 * Params: 524 * dpi = Dots Per Inch (aka Pixels Per Inch) 525 * 526 * Since: 2.8 527 */ 528 public void setDpi(double dpi) 529 { 530 rsvg_handle_set_dpi(rsvgHandle, dpi); 531 } 532 533 /** 534 * Sets the DPI for the outgoing pixbuf. Common values are 535 * 75, 90, and 300 DPI. Passing a number <= 0 to #dpi_x or @dpi_y will 536 * reset the DPI to whatever the default value happens to be. 537 * 538 * Params: 539 * dpiX = Dots Per Inch (aka Pixels Per Inch) 540 * dpiY = Dots Per Inch (aka Pixels Per Inch) 541 * 542 * Since: 2.8 543 */ 544 public void setDpiXY(double dpiX, double dpiY) 545 { 546 rsvg_handle_set_dpi_x_y(rsvgHandle, dpiX, dpiY); 547 } 548 549 /** 550 * Loads the next @count bytes of the image. This will return %TRUE if the data 551 * was loaded successful, and %FALSE if an error occurred. In the latter case, 552 * the loader will be closed, and will not accept further writes. If %FALSE is 553 * returned, @error will be set to an error from the #RsvgError domain. Errors 554 * from #GIOErrorEnum are also possible. 555 * 556 * Params: 557 * buf = pointer to svg data 558 * count = length of the @buf buffer in bytes 559 * 560 * Returns: %TRUE on success, or %FALSE on error 561 * 562 * Throws: GException on failure. 563 */ 564 public bool write(char[] buf) 565 { 566 GError* err = null; 567 568 auto p = rsvg_handle_write(rsvgHandle, buf.ptr, cast(size_t)buf.length, &err) != 0; 569 570 if (err !is null) 571 { 572 throw new GException( new ErrorG(err) ); 573 } 574 575 return p; 576 } 577 }