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 * Lets you load SVG data and render it. 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 320 * size_func callback, because an infinite loop will occur. 321 * 322 * Params: 323 * dimensionData = A place to store the SVG's size 324 * id = An element's id within the SVG, starting with "##", for 325 * example, "##layer1"; or %NULL to use the whole SVG. 326 * 327 * Since: 2.22 328 */ 329 public bool getDimensionsSub(out RsvgDimensionData dimensionData, string id) 330 { 331 return rsvg_handle_get_dimensions_sub(rsvgHandle, &dimensionData, Str.toStringz(id)) != 0; 332 } 333 334 /** 335 * Returns the pixbuf loaded by @handle. The pixbuf returned will be reffed, so 336 * the caller of this function must assume that ref. If insufficient data has 337 * been read to create the pixbuf, or an error occurred in loading, then %NULL 338 * will be returned. Note that the pixbuf may not be complete until 339 * @rsvg_handle_close has been called. 340 * 341 * Returns: the pixbuf loaded by @handle, or %NULL. 342 */ 343 public Pixbuf getPixbuf() 344 { 345 auto p = rsvg_handle_get_pixbuf(rsvgHandle); 346 347 if(p is null) 348 { 349 return null; 350 } 351 352 return ObjectG.getDObject!(Pixbuf)(cast(GdkPixbuf*) p, true); 353 } 354 355 /** 356 * Creates a #GdkPixbuf the same size as the entire SVG loaded into @handle, but 357 * only renders the sub-element that has the specified @id (and all its 358 * sub-sub-elements recursively). If @id is #NULL, this function renders the 359 * whole SVG. 360 * 361 * If you need to render an image which is only big enough to fit a particular 362 * sub-element of the SVG, consider using rsvg_handle_render_cairo_sub(), upon a 363 * surface that is just the size returned by rsvg_handle_get_dimensions_sub(). 364 * You will need to offset the rendering by the amount returned in 365 * rsvg_handle_get_position_sub(). 366 * 367 * Params: 368 * id = An element's id within the SVG, starting with "##", for 369 * example, "##layer1"; or %NULL to use the whole SVG. 370 * 371 * Returns: a pixbuf, or %NULL if an error occurs 372 * during rendering. 373 * 374 * Since: 2.14 375 */ 376 public Pixbuf getPixbufSub(string id) 377 { 378 auto p = rsvg_handle_get_pixbuf_sub(rsvgHandle, Str.toStringz(id)); 379 380 if(p is null) 381 { 382 return null; 383 } 384 385 return ObjectG.getDObject!(Pixbuf)(cast(GdkPixbuf*) p, true); 386 } 387 388 /** 389 * Get the position of a subelement of the SVG file. Do not call from within 390 * the size_func callback, because an infinite loop will occur. 391 * 392 * Params: 393 * positionData = A place to store the SVG fragment's position. 394 * id = An element's id within the SVG, starting with "##", for 395 * example, "##layer1"; or %NULL to use the whole SVG. 396 * 397 * Since: 2.22 398 */ 399 public bool getPositionSub(out RsvgPositionData positionData, string id) 400 { 401 return rsvg_handle_get_position_sub(rsvgHandle, &positionData, Str.toStringz(id)) != 0; 402 } 403 404 /** 405 * Checks whether the element @id exists in the SVG document. 406 * 407 * Params: 408 * id = an element's id within the SVG, starting with "##", for example, "##layer1". 409 * 410 * Returns: %TRUE if @id exists in the SVG document 411 * 412 * Since: 2.22 413 */ 414 public bool hasSub(string id) 415 { 416 return rsvg_handle_has_sub(rsvgHandle, Str.toStringz(id)) != 0; 417 } 418 419 /** */ 420 public void internalSetTesting(bool testing) 421 { 422 rsvg_handle_internal_set_testing(rsvgHandle, testing); 423 } 424 425 /** 426 * Reads @stream and writes the data from it to @handle. 427 * 428 * If @cancellable is not %NULL, then the operation can be cancelled by 429 * triggering the cancellable object from another thread. If the 430 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be 431 * returned. 432 * 433 * Params: 434 * stream = a #GInputStream 435 * cancellable = a #GCancellable, or %NULL 436 * 437 * Returns: %TRUE if reading @stream succeeded, or %FALSE otherwise 438 * with @error filled in 439 * 440 * Since: 2.32 441 * 442 * Throws: GException on failure. 443 */ 444 public bool readStreamSync(InputStream stream, Cancellable cancellable) 445 { 446 GError* err = null; 447 448 auto p = rsvg_handle_read_stream_sync(rsvgHandle, (stream is null) ? null : stream.getInputStreamStruct(), (cancellable is null) ? null : cancellable.getCancellableStruct(), &err) != 0; 449 450 if (err !is null) 451 { 452 throw new GException( new ErrorG(err) ); 453 } 454 455 return p; 456 } 457 458 /** 459 * Draws a loaded SVG handle to a Cairo context. Drawing will occur with 460 * respect to the @cr's current transformation: for example, if the @cr has a 461 * rotated current transformation matrix, the whole SVG will be rotated in the 462 * rendered version. 463 * 464 * Params: 465 * cr = A Cairo renderer 466 * 467 * Returns: %TRUE if drawing succeeded; %FALSE otherwise. 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 loaded SVG handle to a Cairo context. Drawing will occur with 478 * respect to the @cr's current transformation: for example, if the @cr has a 479 * rotated current transformation matrix, the whole SVG will be rotated in the 480 * rendered version. 481 * 482 * Params: 483 * cr = A Cairo renderer 484 * id = An element's id within the SVG, or %NULL to render 485 * the whole SVG. For example, if you have a layer called "layer1" 486 * that you wish to render, pass "##layer1" as the id. 487 * 488 * Returns: %TRUE if drawing succeeded; %FALSE otherwise. 489 * 490 * Since: 2.14 491 */ 492 public bool renderCairoSub(Context cr, string id) 493 { 494 return rsvg_handle_render_cairo_sub(rsvgHandle, (cr is null) ? null : cr.getContextStruct(), Str.toStringz(id)) != 0; 495 } 496 497 /** 498 * Set the base URI for @handle from @file. 499 * Note: This function may only be called before rsvg_handle_write() 500 * or rsvg_handle_read_stream_sync() has been called. 501 * 502 * Params: 503 * baseFile = a #GFile 504 * 505 * Since: 2.32 506 */ 507 public void setBaseGfile(FileIF baseFile) 508 { 509 rsvg_handle_set_base_gfile(rsvgHandle, (baseFile is null) ? null : baseFile.getFileStruct()); 510 } 511 512 /** 513 * Set the base URI for this SVG. This can only be called before rsvg_handle_write() 514 * has been called. 515 * 516 * Params: 517 * baseUri = The base uri 518 * 519 * Since: 2.9 520 */ 521 public void setBaseUri(string baseUri) 522 { 523 rsvg_handle_set_base_uri(rsvgHandle, Str.toStringz(baseUri)); 524 } 525 526 /** 527 * Sets the DPI for the outgoing pixbuf. Common values are 528 * 75, 90, and 300 DPI. Passing a number <= 0 to @dpi will 529 * reset the DPI to whatever the default value happens to be. 530 * 531 * Params: 532 * dpi = Dots Per Inch (aka Pixels Per Inch) 533 * 534 * Since: 2.8 535 */ 536 public void setDpi(double dpi) 537 { 538 rsvg_handle_set_dpi(rsvgHandle, dpi); 539 } 540 541 /** 542 * Sets the DPI for the outgoing pixbuf. Common values are 543 * 75, 90, and 300 DPI. Passing a number <= 0 to #dpi_x or @dpi_y will 544 * reset the DPI to whatever the default value happens to be. 545 * 546 * Params: 547 * dpiX = Dots Per Inch (aka Pixels Per Inch) 548 * dpiY = Dots Per Inch (aka Pixels Per Inch) 549 * 550 * Since: 2.8 551 */ 552 public void setDpiXY(double dpiX, double dpiY) 553 { 554 rsvg_handle_set_dpi_x_y(rsvgHandle, dpiX, dpiY); 555 } 556 557 /** 558 * Loads the next @count bytes of the image. This will return %TRUE if the data 559 * was loaded successful, and %FALSE if an error occurred. In the latter case, 560 * the loader will be closed, and will not accept further writes. If %FALSE is 561 * returned, @error will be set to an error from the #RsvgError domain. Errors 562 * from #GIOErrorEnum are also possible. 563 * 564 * Params: 565 * buf = pointer to svg data 566 * 567 * Returns: %TRUE on success, or %FALSE on error 568 * 569 * Throws: GException on failure. 570 */ 571 public bool write(char[] buf) 572 { 573 GError* err = null; 574 575 auto p = rsvg_handle_write(rsvgHandle, buf.ptr, cast(size_t)buf.length, &err) != 0; 576 577 if (err !is null) 578 { 579 throw new GException( new ErrorG(err) ); 580 } 581 582 return p; 583 } 584 585 /** 586 * This function should not be called from normal programs. 587 * See xmlCleanupParser() for more information. 588 * 589 * Since: 2.36 590 */ 591 public static void cleanup() 592 { 593 rsvg_cleanup(); 594 } 595 596 /** 597 * Do not use this function. Create an #RsvgHandle and call 598 * rsvg_handle_set_dpi() on it instead. 599 * 600 * Deprecated: This function used to set a global default DPI. However, 601 * it only worked if it was called before any #RsvgHandle objects had been 602 * created; it would not work after that. To avoid global mutable state, please 603 * use rsvg_handle_set_dpi() instead. 604 * 605 * Params: 606 * dpi = Dots Per Inch (aka Pixels Per Inch) 607 * 608 * Since: 2.8 609 */ 610 public static void setDefaultDpi(double dpi) 611 { 612 rsvg_set_default_dpi(dpi); 613 } 614 615 /** 616 * Do not use this function. Create an #RsvgHandle and call 617 * rsvg_handle_set_dpi_x_y() on it instead. 618 * 619 * Deprecated: This function used to set a global default DPI. However, 620 * it only worked if it was called before any #RsvgHandle objects had been 621 * created; it would not work after that. To avoid global mutable state, please 622 * use rsvg_handle_set_dpi() instead. 623 * 624 * Params: 625 * dpiX = Dots Per Inch (aka Pixels Per Inch) 626 * dpiY = Dots Per Inch (aka Pixels Per Inch) 627 * 628 * Since: 2.8 629 */ 630 public static void setDefaultDpiXY(double dpiX, double dpiY) 631 { 632 rsvg_set_default_dpi_x_y(dpiX, dpiY); 633 } 634 }