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