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 glib.c.functions; 37 private import gobject.ObjectG; 38 private import rsvg.c.functions; 39 public import rsvg.c.types; 40 41 42 /** 43 * Lets you load SVG data and render it. 44 */ 45 public class Handle : ObjectG 46 { 47 /** the main Gtk struct */ 48 protected RsvgHandle* rsvgHandle; 49 50 /** Get the main Gtk struct */ 51 public RsvgHandle* getHandleStruct(bool transferOwnership = false) 52 { 53 if (transferOwnership) 54 ownedRef = false; 55 return rsvgHandle; 56 } 57 58 /** the main Gtk struct as a void* */ 59 protected override void* getStruct() 60 { 61 return cast(void*)rsvgHandle; 62 } 63 64 /** 65 * Sets our main struct and passes it to the parent class. 66 */ 67 public this (RsvgHandle* rsvgHandle, bool ownedRef = false) 68 { 69 this.rsvgHandle = rsvgHandle; 70 super(cast(GObject*)rsvgHandle, ownedRef); 71 } 72 73 74 /** */ 75 public static GType getType() 76 { 77 return rsvg_handle_get_type(); 78 } 79 80 /** 81 * Returns a new rsvg handle. Must be freed with @g_object_unref. This 82 * handle can be used to load an image. 83 * 84 * The preferred way of loading SVG data into the returned #RsvgHandle is with 85 * rsvg_handle_read_stream_sync(). 86 * 87 * The deprecated way of loading SVG data is with rsvg_handle_write() and 88 * rsvg_handle_close(); note that these require buffering the entire file 89 * internally, and for this reason it is better to use the stream functions: 90 * rsvg_handle_new_from_stream_sync(), rsvg_handle_read_stream_sync(), or 91 * rsvg_handle_new_from_gfile_sync(). 92 * 93 * After loading the #RsvgHandle with data, you can render it using Cairo or get 94 * a GdkPixbuf from it. When finished, free the handle with g_object_unref(). No 95 * more than one image can be loaded with one handle. 96 * 97 * Note that this function creates an #RsvgHandle with no flags set. If you 98 * require any of #RsvgHandleFlags to be set, use any of 99 * rsvg_handle_new_with_flags(), rsvg_handle_new_from_stream_sync(), or 100 * rsvg_handle_new_from_gfile_sync(). 101 * 102 * Returns: A new #RsvgHandle with no flags set. 103 * 104 * Throws: ConstructionException GTK+ fails to create the object. 105 */ 106 public this() 107 { 108 auto __p = rsvg_handle_new(); 109 110 if(__p is null) 111 { 112 throw new ConstructionException("null returned by new"); 113 } 114 115 this(cast(RsvgHandle*) __p, true); 116 } 117 118 /** 119 * Loads the SVG specified by @data. Note that this function creates an 120 * #RsvgHandle without a base URL, and without any #RsvgHandleFlags. If you 121 * need these, use rsvg_handle_new_from_stream_sync() instead by creating 122 * a #GMemoryInputStream from your data. 123 * 124 * Params: 125 * data = The SVG data 126 * 127 * Returns: A #RsvgHandle or %NULL if an error occurs. 128 * 129 * Since: 2.14 130 * 131 * Throws: GException on failure. 132 * Throws: ConstructionException GTK+ fails to create the object. 133 */ 134 public this(ubyte[] data) 135 { 136 GError* err = null; 137 138 auto __p = rsvg_handle_new_from_data(data.ptr, cast(size_t)data.length, &err); 139 140 if (err !is null) 141 { 142 throw new GException( new ErrorG(err) ); 143 } 144 145 if(__p is null) 146 { 147 throw new ConstructionException("null returned by new_from_data"); 148 } 149 150 this(cast(RsvgHandle*) __p, true); 151 } 152 153 /** 154 * Loads the SVG specified by @file_name. Note that this function, like 155 * rsvg_handle_new(), does not specify any loading flags for the resulting 156 * handle. If you require the use of #RsvgHandleFlags, use 157 * rsvg_handle_new_from_gfile_sync(). 158 * 159 * Params: 160 * filename = The file name to load, or a URI. 161 * 162 * Returns: A #RsvgHandle or %NULL if an error occurs. 163 * 164 * Since: 2.14 165 * 166 * Throws: GException on failure. 167 * Throws: ConstructionException GTK+ fails to create the object. 168 */ 169 public this(string filename) 170 { 171 GError* err = null; 172 173 auto __p = rsvg_handle_new_from_file(Str.toStringz(filename), &err); 174 175 if (err !is null) 176 { 177 throw new GException( new ErrorG(err) ); 178 } 179 180 if(__p is null) 181 { 182 throw new ConstructionException("null returned by new_from_file"); 183 } 184 185 this(cast(RsvgHandle*) __p, true); 186 } 187 188 /** 189 * Creates a new #RsvgHandle for @file. 190 * 191 * This function sets the "base file" of the handle to be @file itself, so SVG 192 * elements like <literal><image></literal> which reference external 193 * resources will be resolved relative to the location of @file. 194 * 195 * If @cancellable is not %NULL, then the operation can be cancelled by 196 * triggering the cancellable object from another thread. If the 197 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be 198 * returned in @error. 199 * 200 * Params: 201 * file = a #GFile 202 * flags = flags from #RsvgHandleFlags 203 * cancellable = a #GCancellable, or %NULL 204 * 205 * Returns: a new #RsvgHandle on success, or %NULL with @error filled in 206 * 207 * Since: 2.32 208 * 209 * Throws: GException on failure. 210 * Throws: ConstructionException GTK+ fails to create the object. 211 */ 212 public this(FileIF file, RsvgHandleFlags flags, Cancellable cancellable) 213 { 214 GError* err = null; 215 216 auto __p = rsvg_handle_new_from_gfile_sync((file is null) ? null : file.getFileStruct(), flags, (cancellable is null) ? null : cancellable.getCancellableStruct(), &err); 217 218 if (err !is null) 219 { 220 throw new GException( new ErrorG(err) ); 221 } 222 223 if(__p is null) 224 { 225 throw new ConstructionException("null returned by new_from_gfile_sync"); 226 } 227 228 this(cast(RsvgHandle*) __p, true); 229 } 230 231 /** 232 * Creates a new #RsvgHandle for @stream. 233 * 234 * This function sets the "base file" of the handle to be @base_file if 235 * provided. SVG elements like <literal><image></literal> which reference 236 * external resources will be resolved relative to the location of @base_file. 237 * 238 * If @cancellable is not %NULL, then the operation can be cancelled by 239 * triggering the cancellable object from another thread. If the 240 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be 241 * returned in @error. 242 * 243 * Params: 244 * inputStream = a #GInputStream 245 * baseFile = a #GFile, or %NULL 246 * flags = flags from #RsvgHandleFlags 247 * cancellable = a #GCancellable, or %NULL 248 * 249 * Returns: a new #RsvgHandle on success, or %NULL with @error filled in 250 * 251 * Since: 2.32 252 * 253 * Throws: GException on failure. 254 * Throws: ConstructionException GTK+ fails to create the object. 255 */ 256 public this(InputStream inputStream, FileIF baseFile, RsvgHandleFlags flags, Cancellable cancellable) 257 { 258 GError* err = null; 259 260 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); 261 262 if (err !is null) 263 { 264 throw new GException( new ErrorG(err) ); 265 } 266 267 if(__p is null) 268 { 269 throw new ConstructionException("null returned by new_from_stream_sync"); 270 } 271 272 this(cast(RsvgHandle*) __p, true); 273 } 274 275 /** 276 * Creates a new #RsvgHandle with flags @flags. After calling this function, 277 * you can feed the resulting handle with SVG data by using 278 * rsvg_handle_read_stream_sync(). 279 * 280 * Params: 281 * flags = flags from #RsvgHandleFlags 282 * 283 * Returns: a new #RsvgHandle 284 * 285 * Since: 2.36 286 * 287 * Throws: ConstructionException GTK+ fails to create the object. 288 */ 289 public this(RsvgHandleFlags flags) 290 { 291 auto __p = rsvg_handle_new_with_flags(flags); 292 293 if(__p is null) 294 { 295 throw new ConstructionException("null returned by new_with_flags"); 296 } 297 298 this(cast(RsvgHandle*) __p, true); 299 } 300 301 /** 302 * Closes @handle, to indicate that loading the image is complete. This will 303 * return %TRUE if the loader closed successfully and the SVG data was parsed 304 * correctly. Note that @handle isn't freed until @g_object_unref is called. 305 * 306 * Deprecated: Use rsvg_handle_read_stream_sync() or the constructor 307 * functions rsvg_handle_new_from_gfile_sync() or 308 * rsvg_handle_new_from_stream_sync(). See the deprecation notes for 309 * rsvg_handle_write() for more information. 310 * 311 * Returns: %TRUE on success, or %FALSE on error. 312 * 313 * Throws: GException on failure. 314 */ 315 public bool close() 316 { 317 GError* err = null; 318 319 auto __p = rsvg_handle_close(rsvgHandle, &err) != 0; 320 321 if (err !is null) 322 { 323 throw new GException( new ErrorG(err) ); 324 } 325 326 return __p; 327 } 328 329 /** 330 * Gets the base uri for this #RsvgHandle. 331 * 332 * Returns: the base uri, possibly null 333 * 334 * Since: 2.8 335 */ 336 public string getBaseUri() 337 { 338 return Str.toString(rsvg_handle_get_base_uri(rsvgHandle)); 339 } 340 341 /** 342 * Get the SVG's size. Do not call from within the size_func callback, because 343 * an infinite loop will occur. 344 * 345 * This function depends on the #RsvgHandle's DPI to compute dimensions in 346 * pixels, so you should call rsvg_handle_set_dpi() beforehand. 347 * 348 * Params: 349 * dimensionData = A place to store the SVG's size 350 * 351 * Since: 2.14 352 */ 353 public void getDimensions(out RsvgDimensionData dimensionData) 354 { 355 rsvg_handle_get_dimensions(rsvgHandle, &dimensionData); 356 } 357 358 /** 359 * Get the size of a subelement of the SVG file. Do not call from within the 360 * size_func callback, because an infinite loop will occur. 361 * 362 * This function depends on the #RsvgHandle's DPI to compute dimensions in 363 * pixels, so you should call rsvg_handle_set_dpi() beforehand. 364 * 365 * Element IDs should look like an URL fragment identifier; for example, pass 366 * "##foo" (hash <literal>foo</literal>) to get the geometry of the element that 367 * has an <literal>id="foo"</literal> attribute. 368 * 369 * Deprecated: Use rsvg_handle_get_geometry_for_layer() instead. 370 * 371 * Params: 372 * dimensionData = A place to store the SVG's size 373 * id = An element's id within the SVG, starting with "##" (a single 374 * hash character), for example, "##layer1". This notation corresponds to a 375 * URL's fragment ID. Alternatively, pass %NULL to use the whole SVG. 376 * 377 * Since: 2.22 378 */ 379 public bool getDimensionsSub(out RsvgDimensionData dimensionData, string id) 380 { 381 return rsvg_handle_get_dimensions_sub(rsvgHandle, &dimensionData, Str.toStringz(id)) != 0; 382 } 383 384 /** 385 * Computes the ink rectangle and logical rectangle of a single SVG element. 386 * 387 * While `rsvg_handle_get_geometry_for_layer` computes the geometry of an SVG element subtree with 388 * its transformation matrix, this other function will compute the element's geometry 389 * as if it were being rendered under an identity transformation by itself. That is, 390 * the resulting geometry is as if the element got extracted by itself from the SVG. 391 * 392 * This function is the counterpart to `rsvg_handle_render_element`. 393 * 394 * Element IDs should look like an URL fragment identifier; for example, pass 395 * "##foo" (hash <literal>foo</literal>) to get the geometry of the element that 396 * has an <literal>id="foo"</literal> attribute. 397 * 398 * The "ink rectangle" is the bounding box that would be painted 399 * for fully- stroked and filled elements. 400 * 401 * The "logical rectangle" just takes into account the unstroked 402 * paths and text outlines. 403 * 404 * Note that these bounds are not minimum bounds; for example, 405 * clipping paths are not taken into account. 406 * 407 * You can pass #NULL for the @id if you want to measure all 408 * the elements in the SVG, i.e. to measure everything from the 409 * root element. 410 * 411 * This operation is not constant-time, as it involves going through all 412 * the child elements. 413 * 414 * API ordering: This function must be called on a fully-loaded @handle. See 415 * the section <ulink url="#API-ordering">API ordering</ulink> for details. 416 * 417 * Panics: this function will panic if the @handle is not fully-loaded. 418 * 419 * Params: 420 * id = An element's id within the SVG, starting with "##" (a single 421 * hash character), for example, "##layer1". This notation corresponds to a 422 * URL's fragment ID. Alternatively, pass %NULL to compute the geometry for the 423 * whole SVG. 424 * outInkRect = Place to store the ink rectangle of the element. 425 * outLogicalRect = Place to store the logical rectangle of the element. 426 * 427 * Since: 2.46 428 * 429 * Throws: GException on failure. 430 */ 431 public bool getGeometryForElement(string id, out RsvgRectangle outInkRect, out RsvgRectangle outLogicalRect) 432 { 433 GError* err = null; 434 435 auto __p = rsvg_handle_get_geometry_for_element(rsvgHandle, Str.toStringz(id), &outInkRect, &outLogicalRect, &err) != 0; 436 437 if (err !is null) 438 { 439 throw new GException( new ErrorG(err) ); 440 } 441 442 return __p; 443 } 444 445 /** 446 * Computes the ink rectangle and logical rectangle of an SVG element, or the 447 * whole SVG, as if the whole SVG were rendered to a specific viewport. 448 * 449 * Element IDs should look like an URL fragment identifier; for example, pass 450 * "##foo" (hash <literal>foo</literal>) to get the geometry of the element that 451 * has an <literal>id="foo"</literal> attribute. 452 * 453 * The "ink rectangle" is the bounding box that would be painted 454 * for fully- stroked and filled elements. 455 * 456 * The "logical rectangle" just takes into account the unstroked 457 * paths and text outlines. 458 * 459 * Note that these bounds are not minimum bounds; for example, 460 * clipping paths are not taken into account. 461 * 462 * You can pass #NULL for the @id if you want to measure all 463 * the elements in the SVG, i.e. to measure everything from the 464 * root element. 465 * 466 * This operation is not constant-time, as it involves going through all 467 * the child elements. 468 * 469 * API ordering: This function must be called on a fully-loaded @handle. See 470 * the section <ulink url="#API-ordering">API ordering</ulink> for details. 471 * 472 * Panics: this function will panic if the @handle is not fully-loaded. 473 * 474 * Params: 475 * id = An element's id within the SVG, starting with "##" (a single 476 * hash character), for example, "##layer1". This notation corresponds to a 477 * URL's fragment ID. Alternatively, pass %NULL to compute the geometry for the 478 * whole SVG. 479 * viewport = Viewport size at which the whole SVG would be fitted. 480 * outInkRect = Place to store the ink rectangle of the element. 481 * outLogicalRect = Place to store the logical rectangle of the element. 482 * 483 * Since: 2.46 484 * 485 * Throws: GException on failure. 486 */ 487 public bool getGeometryForLayer(string id, RsvgRectangle* viewport, out RsvgRectangle outInkRect, out RsvgRectangle outLogicalRect) 488 { 489 GError* err = null; 490 491 auto __p = rsvg_handle_get_geometry_for_layer(rsvgHandle, Str.toStringz(id), viewport, &outInkRect, &outLogicalRect, &err) != 0; 492 493 if (err !is null) 494 { 495 throw new GException( new ErrorG(err) ); 496 } 497 498 return __p; 499 } 500 501 /** 502 * Queries the <literal>width</literal>, <literal>height</literal>, and 503 * <literal>viewBox</literal> attributes in an SVG document. 504 * 505 * If you are calling this function to compute a scaling factor to render the SVG, 506 * consider simply using rsvg_handle_render_document() instead; it will do the 507 * scaling computations automatically. 508 * 509 * As an example, the following SVG element has a <literal>width</literal> of 100 pixels and a <literal>height</literal> of 400 pixels, but no <literal>viewBox</literal>: 510 * 511 * |[ 512 * <svg xmlns="http://www.w3.org/2000/svg" width="100" height="400"> 513 * ]| 514 * 515 * Conversely, the following element has a <literal>viewBox</literal>, but no <literal>width</literal> or <literal>height</literal>: 516 * 517 * |[ 518 * <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 400"> 519 * ]| 520 * 521 * Note that the #RsvgLength return values have #RsvgUnits in them; you should 522 * not assume that they are always in pixels. For example, the following SVG element 523 * will return a width value whose <literal>units</literal> field is RSVG_UNIT_MM. 524 * 525 * |[ 526 * <svg xmlns="http://www.w3.org/2000/svg" width="210mm" height="297mm"> 527 * ]| 528 * 529 * API ordering: This function must be called on a fully-loaded @handle. See 530 * the section <ulink url="#API-ordering">API ordering</ulink> for details. 531 * 532 * Panics: this function will panic if the @handle is not fully-loaded. 533 * 534 * Params: 535 * outHasWidth = Will be set to #TRUE if the toplevel SVG has a <literal>width</literal> attribute 536 * outWidth = Will be set to the value of the <literal>width</literal> attribute in the toplevel SVG 537 * outHasHeight = Will be set to #TRUE if the toplevel SVG has a <literal>height</literal> attribute 538 * outHeight = Will be set to the value of the <literal>height</literal> attribute in the toplevel SVG 539 * outHasViewbox = Will be set to #TRUE if the toplevel SVG has a <literal>viewBox</literal> attribute 540 * outViewbox = Will be set to the value of the <literal>viewBox</literal> attribute in the toplevel SVG 541 * 542 * Since: 2.46 543 */ 544 public void getIntrinsicDimensions(out bool outHasWidth, out RsvgLength outWidth, out bool outHasHeight, out RsvgLength outHeight, out bool outHasViewbox, out RsvgRectangle outViewbox) 545 { 546 int outoutHasWidth; 547 int outoutHasHeight; 548 int outoutHasViewbox; 549 550 rsvg_handle_get_intrinsic_dimensions(rsvgHandle, &outoutHasWidth, &outWidth, &outoutHasHeight, &outHeight, &outoutHasViewbox, &outViewbox); 551 552 outHasWidth = (outoutHasWidth == 1); 553 outHasHeight = (outoutHasHeight == 1); 554 outHasViewbox = (outoutHasViewbox == 1); 555 } 556 557 /** 558 * Returns the pixbuf loaded by @handle. The pixbuf returned will be reffed, so 559 * the caller of this function must assume that ref. If insufficient data has 560 * been read to create the pixbuf, or an error occurred in loading, then %NULL 561 * will be returned. Note that the pixbuf may not be complete until 562 * @rsvg_handle_close has been called. 563 * 564 * This function depends on the #RsvgHandle's DPI to compute dimensions in 565 * pixels, so you should call rsvg_handle_set_dpi() beforehand. 566 * 567 * Returns: the pixbuf loaded by @handle, or %NULL. 568 */ 569 public Pixbuf getPixbuf() 570 { 571 auto __p = rsvg_handle_get_pixbuf(rsvgHandle); 572 573 if(__p is null) 574 { 575 return null; 576 } 577 578 return ObjectG.getDObject!(Pixbuf)(cast(GdkPixbuf*) __p, true); 579 } 580 581 /** 582 * Creates a #GdkPixbuf the same size as the entire SVG loaded into @handle, but 583 * only renders the sub-element that has the specified @id (and all its 584 * sub-sub-elements recursively). If @id is #NULL, this function renders the 585 * whole SVG. 586 * 587 * This function depends on the #RsvgHandle's DPI to compute dimensions in 588 * pixels, so you should call rsvg_handle_set_dpi() beforehand. 589 * 590 * If you need to render an image which is only big enough to fit a particular 591 * sub-element of the SVG, consider using rsvg_handle_render_cairo_sub(), upon a 592 * surface that is just the size returned by rsvg_handle_get_dimensions_sub(). 593 * You will need to offset the rendering by the amount returned in 594 * rsvg_handle_get_position_sub(). 595 * 596 * Element IDs should look like an URL fragment identifier; for example, pass 597 * "##foo" (hash <literal>foo</literal>) to get the geometry of the element that 598 * has an <literal>id="foo"</literal> attribute. 599 * 600 * Params: 601 * id = An element's id within the SVG, starting with "##" (a single 602 * hash character), for example, "##layer1". This notation corresponds to a 603 * URL's fragment ID. Alternatively, pass %NULL to use the whole SVG. 604 * 605 * Returns: a pixbuf, or %NULL if an error occurs 606 * during rendering. 607 * 608 * Since: 2.14 609 */ 610 public Pixbuf getPixbufSub(string id) 611 { 612 auto __p = rsvg_handle_get_pixbuf_sub(rsvgHandle, Str.toStringz(id)); 613 614 if(__p is null) 615 { 616 return null; 617 } 618 619 return ObjectG.getDObject!(Pixbuf)(cast(GdkPixbuf*) __p, true); 620 } 621 622 /** 623 * Get the position of a subelement of the SVG file. Do not call from within 624 * the size_func callback, because an infinite loop will occur. 625 * 626 * This function depends on the #RsvgHandle's DPI to compute dimensions in 627 * pixels, so you should call rsvg_handle_set_dpi() beforehand. 628 * 629 * Element IDs should look like an URL fragment identifier; for example, pass 630 * "##foo" (hash <literal>foo</literal>) to get the geometry of the element that 631 * has an <literal>id="foo"</literal> attribute. 632 * 633 * Deprecated: Use rsvg_handle_get_geometry_for_layer() instead. 634 * 635 * Params: 636 * positionData = A place to store the SVG fragment's position. 637 * id = An element's id within the SVG, starting with "##" (a single 638 * hash character), for example, "##layer1". This notation corresponds to a 639 * URL's fragment ID. Alternatively, pass %NULL to use the whole SVG. 640 * 641 * Since: 2.22 642 */ 643 public bool getPositionSub(out RsvgPositionData positionData, string id) 644 { 645 return rsvg_handle_get_position_sub(rsvgHandle, &positionData, Str.toStringz(id)) != 0; 646 } 647 648 /** 649 * Checks whether the element @id exists in the SVG document. 650 * 651 * Element IDs should look like an URL fragment identifier; for example, pass 652 * "##foo" (hash <literal>foo</literal>) to get the geometry of the element that 653 * has an <literal>id="foo"</literal> attribute. 654 * 655 * Params: 656 * id = An element's id within the SVG, starting with "##" (a single hash 657 * character), for example, "##layer1". This notation corresponds to a URL's 658 * fragment ID. 659 * 660 * Returns: %TRUE if @id exists in the SVG document, %FALSE otherwise. 661 * 662 * Since: 2.22 663 */ 664 public bool hasSub(string id) 665 { 666 return rsvg_handle_has_sub(rsvgHandle, Str.toStringz(id)) != 0; 667 } 668 669 /** 670 * Do not call this function. This is intended for librsvg's internal 671 * test suite only. 672 * 673 * Params: 674 * testing = Whether to enable testing mode 675 */ 676 public void internalSetTesting(bool testing) 677 { 678 rsvg_handle_internal_set_testing(rsvgHandle, testing); 679 } 680 681 /** 682 * Reads @stream and writes the data from it to @handle. 683 * 684 * Before calling this function, you may need to call rsvg_handle_set_base_uri() 685 * or rsvg_handle_set_base_gfile() to set the "base file" for resolving 686 * references to external resources. SVG elements like 687 * <literal><image></literal> which reference external resources will be 688 * resolved relative to the location you specify with those functions. 689 * 690 * If @cancellable is not %NULL, then the operation can be cancelled by 691 * triggering the cancellable object from another thread. If the 692 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be 693 * returned. 694 * 695 * Params: 696 * stream = a #GInputStream 697 * cancellable = a #GCancellable, or %NULL 698 * 699 * Returns: %TRUE if reading @stream succeeded, or %FALSE otherwise 700 * with @error filled in 701 * 702 * Since: 2.32 703 * 704 * Throws: GException on failure. 705 */ 706 public bool readStreamSync(InputStream stream, Cancellable cancellable) 707 { 708 GError* err = null; 709 710 auto __p = rsvg_handle_read_stream_sync(rsvgHandle, (stream is null) ? null : stream.getInputStreamStruct(), (cancellable is null) ? null : cancellable.getCancellableStruct(), &err) != 0; 711 712 if (err !is null) 713 { 714 throw new GException( new ErrorG(err) ); 715 } 716 717 return __p; 718 } 719 720 /** 721 * Draws a loaded SVG handle to a Cairo context. Drawing will occur with 722 * respect to the @cr's current transformation: for example, if the @cr has a 723 * rotated current transformation matrix, the whole SVG will be rotated in the 724 * rendered version. 725 * 726 * This function depends on the #RsvgHandle's DPI to compute dimensions in 727 * pixels, so you should call rsvg_handle_set_dpi() beforehand. 728 * 729 * Note that @cr must be a Cairo context that is not in an error state, that is, 730 * cairo_status() must return #CAIRO_STATUS_SUCCESS for it. Cairo can set a 731 * context to be in an error state in various situations, for example, if it was 732 * passed an invalid matrix or if it was created for an invalid surface. 733 * 734 * Params: 735 * cr = A Cairo context 736 * 737 * Returns: %TRUE if drawing succeeded; %FALSE otherwise. 738 * 739 * Since: 2.14 740 */ 741 public bool renderCairo(Context cr) 742 { 743 return rsvg_handle_render_cairo(rsvgHandle, (cr is null) ? null : cr.getContextStruct()) != 0; 744 } 745 746 /** 747 * Draws a subset of a loaded SVG handle to a Cairo context. Drawing will occur with 748 * respect to the @cr's current transformation: for example, if the @cr has a 749 * rotated current transformation matrix, the whole SVG will be rotated in the 750 * rendered version. 751 * 752 * This function depends on the #RsvgHandle's DPI to compute dimensions in 753 * pixels, so you should call rsvg_handle_set_dpi() beforehand. 754 * 755 * Note that @cr must be a Cairo context that is not in an error state, that is, 756 * cairo_status() must return #CAIRO_STATUS_SUCCESS for it. Cairo can set a 757 * context to be in an error state in various situations, for example, if it was 758 * passed an invalid matrix or if it was created for an invalid surface. 759 * 760 * Element IDs should look like an URL fragment identifier; for example, pass 761 * "##foo" (hash <literal>foo</literal>) to get the geometry of the element that 762 * has an <literal>id="foo"</literal> attribute. 763 * 764 * Params: 765 * cr = A Cairo context 766 * id = An element's id within the SVG, starting with "##" (a single 767 * hash character), for example, "##layer1". This notation corresponds to a 768 * URL's fragment ID. Alternatively, pass %NULL to render the whole SVG. 769 * 770 * Returns: %TRUE if drawing succeeded; %FALSE otherwise. 771 * 772 * Since: 2.14 773 */ 774 public bool renderCairoSub(Context cr, string id) 775 { 776 return rsvg_handle_render_cairo_sub(rsvgHandle, (cr is null) ? null : cr.getContextStruct(), Str.toStringz(id)) != 0; 777 } 778 779 /** 780 * Renders the whole SVG document fitted to a viewport. 781 * 782 * The @viewport gives the position and size at which the whole SVG 783 * document will be rendered. 784 * 785 * The @cr must be in a #CAIRO_STATUS_SUCCESS state, or this function will not 786 * render anything, and instead will return an error. 787 * 788 * API ordering: This function must be called on a fully-loaded @handle. See 789 * the section <ulink url="#API-ordering">API ordering</ulink> for details. 790 * 791 * Panics: this function will panic if the @handle is not fully-loaded. 792 * 793 * Params: 794 * cr = A Cairo context 795 * viewport = Viewport size at which the whole SVG would be fitted. 796 * 797 * Since: 2.46 798 * 799 * Throws: GException on failure. 800 */ 801 public bool renderDocument(Context cr, RsvgRectangle* viewport) 802 { 803 GError* err = null; 804 805 auto __p = rsvg_handle_render_document(rsvgHandle, (cr is null) ? null : cr.getContextStruct(), viewport, &err) != 0; 806 807 if (err !is null) 808 { 809 throw new GException( new ErrorG(err) ); 810 } 811 812 return __p; 813 } 814 815 /** 816 * Renders a single SVG element to a given viewport 817 * 818 * This function can be used to extract individual element subtrees and render them, 819 * scaled to a given @element_viewport. This is useful for applications which have 820 * reusable objects in an SVG and want to render them individually; for example, an 821 * SVG full of icons that are meant to be be rendered independently of each other. 822 * 823 * Element IDs should look like an URL fragment identifier; for example, pass 824 * "##foo" (hash <literal>foo</literal>) to get the geometry of the element that 825 * has an <literal>id="foo"</literal> attribute. 826 * 827 * You can pass #NULL for the @id if you want to render all 828 * the elements in the SVG, i.e. to render everything from the 829 * root element. 830 * 831 * The `element_viewport` gives the position and size at which the named element will 832 * be rendered. FIXME: mention proportional scaling. 833 * 834 * API ordering: This function must be called on a fully-loaded @handle. See 835 * the section <ulink url="#API-ordering">API ordering</ulink> for details. 836 * 837 * Panics: this function will panic if the @handle is not fully-loaded. 838 * 839 * Params: 840 * cr = A Cairo context 841 * id = An element's id within the SVG, starting with "##" (a single 842 * hash character), for example, "##layer1". This notation corresponds to a 843 * URL's fragment ID. Alternatively, pass %NULL to render the whole SVG document tree. 844 * elementViewport = Viewport size in which to fit the element 845 * 846 * Since: 2.46 847 * 848 * Throws: GException on failure. 849 */ 850 public bool renderElement(Context cr, string id, RsvgRectangle* elementViewport) 851 { 852 GError* err = null; 853 854 auto __p = rsvg_handle_render_element(rsvgHandle, (cr is null) ? null : cr.getContextStruct(), Str.toStringz(id), elementViewport, &err) != 0; 855 856 if (err !is null) 857 { 858 throw new GException( new ErrorG(err) ); 859 } 860 861 return __p; 862 } 863 864 /** 865 * Renders a single SVG element in the same place as for a whole SVG document. 866 * 867 * This is equivalent to rsvg_handle_render_document(), but it renders only a 868 * single element and its children, as if they composed an individual layer in 869 * the SVG. The element is rendered with the same transformation matrix as it 870 * has within the whole SVG document. Applications can use this to re-render a 871 * single element and repaint it on top of a previously-rendered document, for 872 * example. 873 * 874 * Element IDs should look like an URL fragment identifier; for example, pass 875 * "##foo" (hash <literal>foo</literal>) to get the geometry of the element that 876 * has an <literal>id="foo"</literal> attribute. 877 * 878 * You can pass #NULL for the @id if you want to render all 879 * the elements in the SVG, i.e. to render everything from the 880 * root element. 881 * 882 * API ordering: This function must be called on a fully-loaded @handle. See 883 * the section <ulink url="#API-ordering">API ordering</ulink> for details. 884 * 885 * Panics: this function will panic if the @handle is not fully-loaded. 886 * 887 * Params: 888 * cr = A Cairo context 889 * id = An element's id within the SVG, starting with "##" (a single 890 * hash character), for example, "##layer1". This notation corresponds to a 891 * URL's fragment ID. Alternatively, pass %NULL to render the whole SVG document tree. 892 * viewport = Viewport size at which the whole SVG would be fitted. 893 * 894 * Since: 2.46 895 * 896 * Throws: GException on failure. 897 */ 898 public bool renderLayer(Context cr, string id, RsvgRectangle* viewport) 899 { 900 GError* err = null; 901 902 auto __p = rsvg_handle_render_layer(rsvgHandle, (cr is null) ? null : cr.getContextStruct(), Str.toStringz(id), viewport, &err) != 0; 903 904 if (err !is null) 905 { 906 throw new GException( new ErrorG(err) ); 907 } 908 909 return __p; 910 } 911 912 /** 913 * Set the base URI for @handle from @file. 914 * 915 * Note: This function may only be called before rsvg_handle_write() or 916 * rsvg_handle_read_stream_sync() have been called. 917 * 918 * Params: 919 * baseFile = a #GFile 920 * 921 * Since: 2.32 922 */ 923 public void setBaseGfile(FileIF baseFile) 924 { 925 rsvg_handle_set_base_gfile(rsvgHandle, (baseFile is null) ? null : baseFile.getFileStruct()); 926 } 927 928 /** 929 * Set the base URI for this SVG. 930 * 931 * Note: This function may only be called before rsvg_handle_write() or 932 * rsvg_handle_read_stream_sync() have been called. 933 * 934 * Params: 935 * baseUri = The base uri 936 * 937 * Since: 2.9 938 */ 939 public void setBaseUri(string baseUri) 940 { 941 rsvg_handle_set_base_uri(rsvgHandle, Str.toStringz(baseUri)); 942 } 943 944 /** 945 * Sets the DPI at which the @handle will be rendered. Common values are 946 * 75, 90, and 300 DPI. 947 * 948 * Passing a number <= 0 to @dpi will reset the DPI to whatever the default 949 * value happens to be, but since rsvg_set_default_dpi() is deprecated, please 950 * do not pass values <= 0 to this function. 951 * 952 * Params: 953 * dpi = Dots Per Inch (i.e. as Pixels Per Inch) 954 * 955 * Since: 2.8 956 */ 957 public void setDpi(double dpi) 958 { 959 rsvg_handle_set_dpi(rsvgHandle, dpi); 960 } 961 962 /** 963 * Sets the DPI at which the @handle will be rendered. Common values are 964 * 75, 90, and 300 DPI. 965 * 966 * Passing a number <= 0 to @dpi will reset the DPI to whatever the default 967 * value happens to be, but since rsvg_set_default_dpi_x_y() is deprecated, 968 * please do not pass values <= 0 to this function. 969 * 970 * Params: 971 * dpiX = Dots Per Inch (i.e. Pixels Per Inch) 972 * dpiY = Dots Per Inch (i.e. Pixels Per Inch) 973 * 974 * Since: 2.8 975 */ 976 public void setDpiXY(double dpiX, double dpiY) 977 { 978 rsvg_handle_set_dpi_x_y(rsvgHandle, dpiX, dpiY); 979 } 980 981 /** 982 * Sets a CSS stylesheet to use for an SVG document. 983 * 984 * The @css_len argument is mandatory; this function will not compute the length 985 * of the @css string. This is because a provided stylesheet, which the calling 986 * program could read from a file, can have nul characters in it. 987 * 988 * During the CSS cascade, the specified stylesheet will be used with a "User" 989 * <ulink 990 * url="https://drafts.csswg.org/css-cascade-3/#cascading-origins">origin</ulink>. 991 * 992 * Note that `@import` rules will not be resolved, except for `data:` URLs. 993 * 994 * Params: 995 * css = String with CSS data; must be valid UTF-8. 996 * 997 * Since: 2.48 998 * 999 * Throws: GException on failure. 1000 */ 1001 public bool setStylesheet(ubyte[] css) 1002 { 1003 GError* err = null; 1004 1005 auto __p = rsvg_handle_set_stylesheet(rsvgHandle, css.ptr, cast(size_t)css.length, &err) != 0; 1006 1007 if (err !is null) 1008 { 1009 throw new GException( new ErrorG(err) ); 1010 } 1011 1012 return __p; 1013 } 1014 1015 /** 1016 * Loads the next @count bytes of the image. 1017 * 1018 * Before calling this function for the first time, you may need to call 1019 * rsvg_handle_set_base_uri() or rsvg_handle_set_base_gfile() to set the "base 1020 * file" for resolving references to external resources. SVG elements like 1021 * <literal><image></literal> which reference external resources will be 1022 * resolved relative to the location you specify with those functions. 1023 * 1024 * Deprecated: Use rsvg_handle_read_stream_sync() or the constructor 1025 * functions rsvg_handle_new_from_gfile_sync() or 1026 * rsvg_handle_new_from_stream_sync(). This function is deprecated because it 1027 * will accumulate data from the @buf in memory until rsvg_handle_close() gets 1028 * called. To avoid a big temporary buffer, use the suggested functions, which 1029 * take a #GFile or a #GInputStream and do not require a temporary buffer. 1030 * 1031 * Params: 1032 * buf = pointer to svg data 1033 * 1034 * Returns: %TRUE on success, or %FALSE on error. 1035 * 1036 * Throws: GException on failure. 1037 */ 1038 public bool write(char[] buf) 1039 { 1040 GError* err = null; 1041 1042 auto __p = rsvg_handle_write(rsvgHandle, buf.ptr, cast(size_t)buf.length, &err) != 0; 1043 1044 if (err !is null) 1045 { 1046 throw new GException( new ErrorG(err) ); 1047 } 1048 1049 return __p; 1050 } 1051 1052 /** 1053 * 1054 * 1055 * Deprecated: No-op. This function should not be called from normal programs. 1056 * 1057 * Since: 2.36 1058 */ 1059 public static void cleanup() 1060 { 1061 rsvg_cleanup(); 1062 } 1063 1064 /** 1065 * Do not use this function. Create an #RsvgHandle and call 1066 * rsvg_handle_set_dpi() on it instead. 1067 * 1068 * Deprecated: This function used to set a global default DPI. However, 1069 * it only worked if it was called before any #RsvgHandle objects had been 1070 * created; it would not work after that. To avoid global mutable state, please 1071 * use rsvg_handle_set_dpi() instead. 1072 * 1073 * Params: 1074 * dpi = Dots Per Inch (aka Pixels Per Inch) 1075 * 1076 * Since: 2.8 1077 */ 1078 public static void setDefaultDpi(double dpi) 1079 { 1080 rsvg_set_default_dpi(dpi); 1081 } 1082 1083 /** 1084 * Do not use this function. Create an #RsvgHandle and call 1085 * rsvg_handle_set_dpi_x_y() on it instead. 1086 * 1087 * Deprecated: This function used to set a global default DPI. However, 1088 * it only worked if it was called before any #RsvgHandle objects had been 1089 * created; it would not work after that. To avoid global mutable state, please 1090 * use rsvg_handle_set_dpi() instead. 1091 * 1092 * Params: 1093 * dpiX = Dots Per Inch (aka Pixels Per Inch) 1094 * dpiY = Dots Per Inch (aka Pixels Per Inch) 1095 * 1096 * Since: 2.8 1097 */ 1098 public static void setDefaultDpiXY(double dpiX, double dpiY) 1099 { 1100 rsvg_set_default_dpi_x_y(dpiX, dpiY); 1101 } 1102 }