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