api.c 16.5 KB
Newer Older
1 2 3 4 5 6 7
/* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* vim: set ts=4 nowrap ai expandtab sw=4: */

#include "config.h"

#include <stdio.h>
#include <glib.h>
8 9

#define RSVG_DISABLE_DEPRECATION_WARNINGS /* so we can test deprecated API */
10
#include "librsvg/rsvg.h"
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
#include "test-utils.h"

/*
rsvg_handle_get_base_uri
rsvg_handle_set_base_uri
rsvg_handle_set_size_callback
rsvg_handle_internal_set_testing
rsvg_handle_set_base_gfile
rsvg_handle_get_title
rsvg_handle_get_desc
rsvg_handle_get_metadata
*/

static void
handle_has_gtype (void)
{
    RsvgHandle *handle;

    handle = rsvg_handle_new();
    g_assert (G_OBJECT_TYPE (handle) == rsvg_handle_get_type ());
    g_object_unref (handle);
}

34
static char *
35
get_test_filename (const char *basename) {
36 37
    return g_build_filename (test_utils_get_test_data_path (),
                             "api",
38
                             basename,
39 40 41
                             NULL);
}

42 43
#define EXAMPLE_WIDTH 100
#define EXAMPLE_HEIGHT 400
44

45 46 47
#define XZOOM 2
#define YZOOM 3

48 49 50
#define MAX_WIDTH 10
#define MAX_HEIGHT 40

51 52 53
#define MAX_ZOOMED_WIDTH 20
#define MAX_ZOOMED_HEIGHT 120

54 55
#define EXAMPLE_ONE_ID "#one"
#define EXAMPLE_TWO_ID "#two"
56
#define EXAMPLE_NONEXISTENT_ID "#nonexistent"
57 58 59

#define EXAMPLE_ONE_X 0
#define EXAMPLE_ONE_Y 0
60 61
#define EXAMPLE_ONE_W 100
#define EXAMPLE_ONE_H 200
62 63

#define EXAMPLE_TWO_X 0
64 65 66
#define EXAMPLE_TWO_Y 200
#define EXAMPLE_TWO_W 100
#define EXAMPLE_TWO_H 200
67

68 69
static GdkPixbuf *
pixbuf_from_file (const char *filename, GError **error)
70
{
71 72
    return rsvg_pixbuf_from_file (filename, error);
}
73

74 75 76 77
static GdkPixbuf *
pixbuf_from_file_at_zoom (const char *filename, GError **error)
{
    return rsvg_pixbuf_from_file_at_zoom (filename, (double) XZOOM, (double) YZOOM, error);
78 79
}

80 81
static GdkPixbuf *
pixbuf_from_file_at_size (const char *filename, GError **error)
82
{
83 84
    return rsvg_pixbuf_from_file_at_size (filename, EXAMPLE_WIDTH * XZOOM, EXAMPLE_HEIGHT * YZOOM, error);
}
85

86 87 88 89 90 91
static GdkPixbuf *
pixbuf_from_file_at_max_size (const char *filename, GError **error)
{
    return rsvg_pixbuf_from_file_at_max_size (filename, MAX_WIDTH, MAX_HEIGHT, error);
}

92 93 94 95 96 97 98 99 100
static GdkPixbuf *
pixbuf_from_file_at_zoom_with_max (const char *filename, GError **error)
{
    return rsvg_pixbuf_from_file_at_zoom_with_max (filename,
                                                   XZOOM, YZOOM,
                                                   MAX_ZOOMED_WIDTH, MAX_ZOOMED_HEIGHT,
                                                   error);
}

101
typedef GdkPixbuf *(* PixbufCreateFn) (const char *filename, GError **error);
102

103 104 105 106 107 108 109 110
typedef struct {
    const char *test_name;
    PixbufCreateFn pixbuf_create_fn;
    int expected_width;
    int expected_height;
} PixbufTest;

static const PixbufTest pixbuf_tests[] = {
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
    {
        "/api/pixbuf_from_file",
        pixbuf_from_file,
        EXAMPLE_WIDTH,
        EXAMPLE_HEIGHT
    },
    {
        "/api/pixbuf_from_file_at_zoom",
        pixbuf_from_file_at_zoom,
        EXAMPLE_WIDTH * XZOOM,
        EXAMPLE_HEIGHT * YZOOM
    },
    {
        "/api/pixbuf_from_file_at_size",
        pixbuf_from_file_at_size,
        EXAMPLE_WIDTH * XZOOM,
        EXAMPLE_HEIGHT * YZOOM
    },
    {
        "/api/pixbuf_from_file_at_max_size",
        pixbuf_from_file_at_max_size,
        MAX_WIDTH,
        MAX_HEIGHT
    },
    {
        "/api/pixbuf_from_file_at_zoom_with_max",
        pixbuf_from_file_at_zoom_with_max,
        MAX_ZOOMED_WIDTH,
        MAX_ZOOMED_HEIGHT
    },
141
};
142

143
static void
144 145 146
test_pixbuf (gconstpointer data) {
    const PixbufTest *test = data;

147
    char *filename = get_test_filename ("example.svg");
148
    GError *error = NULL;
149 150 151

    GdkPixbuf *pixbuf = test->pixbuf_create_fn (filename, &error);

152 153 154 155
    g_free (filename);

    g_assert (pixbuf != NULL);
    g_assert (error == NULL);
156 157
    g_assert_cmpint (gdk_pixbuf_get_width (pixbuf), ==, test->expected_width);
    g_assert_cmpint (gdk_pixbuf_get_height (pixbuf), ==, test->expected_height);
158 159 160 161

    g_object_unref (pixbuf);
}

162 163 164 165 166 167 168 169 170
static void
noops (void)
{
    /* Just to test that these functions are present in the binary, I guess */
    rsvg_init ();
    rsvg_term ();
    g_assert (rsvg_cleanup != NULL); /* shouldn't call this one! */
}

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
static void
set_dpi (void)
{
    char *filename = get_test_filename ("dpi.svg");
    GError *error = NULL;
    RsvgHandle *handle;

    /* Get dimensions at default DPI */

    RsvgDimensionData dim_100_dpi;
    RsvgDimensionData dim_200_300_dpi;

    /* Set 100 DPI */

    handle = rsvg_handle_new_from_file (filename, &error);
    g_assert (handle != NULL);
    g_assert (error == NULL);

    rsvg_handle_set_dpi (handle, 100.0);
    rsvg_handle_get_dimensions (handle, &dim_100_dpi);
    g_assert_cmpint (dim_100_dpi.width,  ==, 100);
    g_assert_cmpint (dim_100_dpi.height, ==, 400);
    g_object_unref (handle);

    /* Set 200x300 DPI */

    handle = rsvg_handle_new_from_file (filename, &error);
    g_assert (handle != NULL);
    g_assert (error == NULL);

    rsvg_handle_set_dpi_x_y (handle, 200.0, 300.0);
    rsvg_handle_get_dimensions (handle, &dim_200_300_dpi);
    g_object_unref (handle);

    /* Check! */

    g_assert_cmpint (dim_100_dpi.width * 2,  ==, dim_200_300_dpi.width);
    g_assert_cmpint (dim_100_dpi.height * 3, ==, dim_200_300_dpi.height);
}

211 212 213 214 215 216
static void
error_quark (void)
{
    g_assert_cmpint (rsvg_error_quark(), !=, 0);
}

217 218 219 220 221 222 223 224 225 226 227 228 229 230
static void
auto_generated (void)
{
    GTypeQuery q;

    g_type_query (RSVG_TYPE_ERROR, &q);
    g_assert (G_TYPE_IS_ENUM (q.type));
    g_assert_cmpstr (q.type_name, ==, "RsvgError");

    g_type_query (RSVG_TYPE_HANDLE_FLAGS, &q);
    g_assert (G_TYPE_IS_FLAGS (q.type));
    g_assert_cmpstr (q.type_name, ==, "RsvgHandleFlags");
}

231 232 233 234 235 236 237 238 239 240 241 242 243
static void
handle_write_close_free (void)
{
    char *filename = get_test_filename ("dpi.svg");
    char *data;
    gsize length;
    gsize i;
    GError *error = NULL;

    g_assert (g_file_get_contents (filename, &data, &length, &error));
    g_assert (data != NULL);
    g_assert (error == NULL);

244
    RsvgHandle *handle = rsvg_handle_new_with_flags (RSVG_HANDLE_FLAGS_NONE);
245 246 247 248 249 250 251 252 253 254 255 256 257

    for (i = 0; i < length; i++) {
        g_assert (rsvg_handle_write (handle, (guchar *) &data[i], 1, &error));
        g_assert (error == NULL);
    }

    g_assert (rsvg_handle_close (handle, &error));
    g_assert (error == NULL);

    rsvg_handle_free (handle);
    g_free (data);
}

258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
static void
handle_new_from_file (void)
{
    char *filename = get_test_filename ("dpi.svg");
    char *uri = g_strconcat ("file://", filename, NULL);

    RsvgHandle *handle;
    GError *error = NULL;

    /* rsvg_handle_new_from_file() can take both filenames and URIs */

    handle = rsvg_handle_new_from_file (filename, &error);
    g_assert (handle != NULL);
    g_assert (error == NULL);
    g_object_unref (handle);

    handle = rsvg_handle_new_from_file (uri, &error);
    g_assert (handle != NULL);
    g_assert (error == NULL);
    g_object_unref (handle);

    g_free (filename);
    g_free (uri);
}

283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
static void
handle_new_from_data (void)
{
    char *filename = get_test_filename ("dpi.svg");
    char *data;
    gsize length;
    GError *error = NULL;

    g_assert (g_file_get_contents (filename, &data, &length, &error));
    g_assert (data != NULL);
    g_assert (error == NULL);

    RsvgHandle *handle = rsvg_handle_new_from_data ((guint8 *) data, length, &error);
    g_assert (handle != NULL);
    g_assert (error == NULL);

    g_object_unref (handle);
    g_free (data);
}

303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
static void
handle_new_from_gfile_sync (void)
{
    char *filename = get_test_filename ("dpi.svg");
    GError *error = NULL;
    GFile *file = g_file_new_for_path (filename);
    g_assert (file != NULL);

    g_free (filename);

    RsvgHandle *handle = rsvg_handle_new_from_gfile_sync (file,
                                                          RSVG_HANDLE_FLAGS_NONE,
                                                          NULL,
                                                          &error);

    g_assert (handle != NULL);
    g_assert (error == NULL);

    g_object_unref (handle);
    g_object_unref (file);
}
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352

static void
handle_new_from_stream_sync (void)
{
    char *filename = get_test_filename ("dpi.svg");
    GError *error = NULL;
    GFile *file = g_file_new_for_path (filename);
    g_assert (file != NULL);

    g_free (filename);

    GFileInputStream *stream = g_file_read (file, NULL, &error);
    g_assert (stream != NULL);
    g_assert (error == NULL);

    RsvgHandle *handle = rsvg_handle_new_from_stream_sync (G_INPUT_STREAM (stream),
                                                           file,
                                                           RSVG_HANDLE_FLAGS_NONE,
                                                           NULL,
                                                           &error);

    g_assert (handle != NULL);
    g_assert (error == NULL);

    g_object_unref (handle);
    g_object_unref (file);
    g_object_unref (stream);
}

353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
static void
handle_read_stream_sync (void)
{
    char *filename = get_test_filename ("dpi.svg");
    GError *error = NULL;
    GFile *file = g_file_new_for_path (filename);
    g_assert (file != NULL);

    g_free (filename);

    GFileInputStream *stream = g_file_read (file, NULL, &error);
    g_assert (stream != NULL);
    g_assert (error == NULL);

    RsvgHandle *handle = rsvg_handle_new ();

    g_assert (rsvg_handle_read_stream_sync (handle, G_INPUT_STREAM (stream), NULL, &error));
    g_assert (error == NULL);

    g_object_unref (handle);
    g_object_unref (file);
    g_object_unref (stream);
}

377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
static void
handle_has_sub (void)
{
    char *filename = get_test_filename ("example.svg");
    GError *error = NULL;

    RsvgHandle *handle = rsvg_handle_new_from_file (filename, &error);
    g_free (filename);

    g_assert (handle != NULL);
    g_assert (error == NULL);

    g_assert (rsvg_handle_has_sub (handle, EXAMPLE_ONE_ID));
    g_assert (rsvg_handle_has_sub (handle, EXAMPLE_TWO_ID));
    g_assert (!rsvg_handle_has_sub (handle, "#foo"));
}

static void
handle_get_pixbuf (void)
{
    char *filename = get_test_filename ("example.svg");
    GError *error = NULL;

    RsvgHandle *handle = rsvg_handle_new_from_file (filename, &error);
    g_free (filename);

    g_assert (handle != NULL);
    g_assert (error == NULL);

    GdkPixbuf *pixbuf = rsvg_handle_get_pixbuf (handle);
    g_assert (pixbuf != NULL);

    g_assert_cmpint (gdk_pixbuf_get_width (pixbuf), ==, EXAMPLE_WIDTH);
    g_assert_cmpint (gdk_pixbuf_get_height (pixbuf), ==, EXAMPLE_HEIGHT);

    g_object_unref (pixbuf);
    g_object_unref (handle);
}

416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
static void
handle_get_pixbuf_sub (void)
{
    char *filename = get_test_filename ("example.svg");
    GError *error = NULL;

    RsvgHandle *handle = rsvg_handle_new_from_file (filename, &error);
    g_free (filename);

    g_assert (handle != NULL);
    g_assert (error == NULL);

    GdkPixbuf *pixbuf = rsvg_handle_get_pixbuf_sub (handle, EXAMPLE_ONE_ID);
    g_assert (pixbuf != NULL);

    /* Note that rsvg_handle_get_pixbuf_sub() creates a surface the size of the
     * whole SVG, not just the size of the sub-element.
     */
    g_assert_cmpint (gdk_pixbuf_get_width (pixbuf), ==, EXAMPLE_WIDTH);
    g_assert_cmpint (gdk_pixbuf_get_height (pixbuf), ==, EXAMPLE_HEIGHT);

    g_object_unref (pixbuf);
    g_object_unref (handle);
}

441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
static void
dimensions_and_position (void)
{
    char *filename = get_test_filename ("example.svg");
    GError *error = NULL;

    RsvgHandle *handle = rsvg_handle_new_from_file (filename, &error);
    g_free (filename);

    g_assert (handle != NULL);
    g_assert (error == NULL);

    RsvgDimensionData dim;

    g_assert (rsvg_handle_get_dimensions_sub (handle, &dim, EXAMPLE_TWO_ID));
    g_assert_cmpint (dim.width,  ==, EXAMPLE_TWO_W);
    g_assert_cmpint (dim.height, ==, EXAMPLE_TWO_H);

    RsvgPositionData pos;
    g_assert (rsvg_handle_get_position_sub (handle, &pos, EXAMPLE_TWO_ID));
    g_assert_cmpint (pos.x, ==, EXAMPLE_TWO_X);
    g_assert_cmpint (pos.y, ==, EXAMPLE_TWO_Y);

464 465 466
    g_assert (!rsvg_handle_get_position_sub (handle, &pos, EXAMPLE_NONEXISTENT_ID));
    g_assert (!rsvg_handle_get_dimensions_sub (handle, &dim, EXAMPLE_NONEXISTENT_ID));

467 468 469
    g_object_unref (handle);
}

470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
static void
detects_cairo_context_in_error (void)
{
    if (g_test_subprocess ()) {
        char *filename = get_test_filename ("example.svg");
        GError *error = NULL;

        RsvgHandle *handle = rsvg_handle_new_from_file (filename, &error);
        g_assert (handle != NULL);
        g_assert (error == NULL);

        /* this is wrong; it is to simulate creating a surface and a cairo_t in error */
        cairo_surface_t *surf = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, -1, -1);
        cairo_t *cr = cairo_create (surf);

        /* rsvg_handle_render_cairo() should return FALSE when it gets a cr in an error state */
        g_assert (!rsvg_handle_render_cairo (handle, cr));

        return;
    }

    g_test_trap_subprocess (NULL, 0, 0);
    g_test_trap_assert_failed ();
    g_test_trap_assert_stderr ("*WARNING*cannot render on a cairo_t with a failure status*");
}

496 497 498 499 500 501 502 503 504 505 506
static gboolean
matrixes_are_equal (cairo_matrix_t *a, cairo_matrix_t *b)
{
    return (a->xx == b->xx &&
            a->yx == b->yx &&
            a->xy == b->xy &&
            a->yy == b->yy &&
            a->x0 == b->x0 &&
            a->y0 == b->y0);
}

507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
static void
can_draw_to_non_image_surface (void)
{
    cairo_rectangle_t rect;
    cairo_surface_t *surface;
    cairo_t *cr;

    char *filename = get_test_filename ("example.svg");
    GError *error = NULL;

    RsvgHandle *handle = rsvg_handle_new_from_file (filename, &error);
    g_assert (handle != NULL);
    g_assert (error == NULL);

    rect.x = 0.0;
    rect.y = 0.0;
    rect.width = 100.0;
    rect.height = 100.0;

    /* We create a surface that is not a Cairo image surface,
     * so we can test that in fact we can render to non-image surfaces.
     */
    surface = cairo_recording_surface_create (CAIRO_CONTENT_COLOR_ALPHA, &rect);
    cr = cairo_create (surface);

532 533 534 535 536
    cairo_translate (cr, 42.0, 42.0);

    cairo_matrix_t original_affine;
    cairo_get_matrix (cr, &original_affine);

537 538
    g_assert (rsvg_handle_render_cairo (handle, cr));

539 540 541 542 543
    cairo_matrix_t new_affine;
    cairo_get_matrix (cr, &new_affine);

    g_assert (matrixes_are_equal (&original_affine, &new_affine));

544 545 546 547 548
    g_object_unref (handle);

    cairo_destroy (cr);
}

549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
/* Test that we preserve the affine transformation in the cr during a call
 * to rsvg_handle_render_cairo_sub().
 */
static void
render_cairo_sub (void)
{
    char *filename = get_test_filename ("334-element-positions.svg");
    GError *error = NULL;

    RsvgHandle *handle = rsvg_handle_new_from_file (filename, &error);
    g_assert (handle != NULL);
    g_assert (error == NULL);

    cairo_surface_t *surf = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 200, 200);
    cairo_t *cr = cairo_create (surf);

    cairo_translate (cr, 42.0, 42.0);

    cairo_matrix_t original_affine;
    cairo_get_matrix (cr, &original_affine);

    g_assert (rsvg_handle_render_cairo_sub (handle, cr, "#button5-leader"));

    cairo_matrix_t new_affine;
    cairo_get_matrix (cr, &new_affine);

    g_assert (matrixes_are_equal (&original_affine, &new_affine));

    g_object_unref (handle);
    cairo_destroy (cr);
}

581 582 583
int
main (int argc, char **argv)
{
584 585
    int i;

586 587
    g_test_init (&argc, &argv, NULL);

588 589 590 591
    for (i = 0; i < G_N_ELEMENTS (pixbuf_tests); i++) {
        g_test_add_data_func (pixbuf_tests[i].test_name, &pixbuf_tests[i], test_pixbuf);
    }

592
    g_test_add_func ("/api/handle_has_gtype", handle_has_gtype);
593
    g_test_add_func ("/api/noops", noops);
594
    g_test_add_func ("/api/set_dpi", set_dpi);
595
    g_test_add_func ("/api/error_quark", error_quark);
596
    g_test_add_func ("/api/auto_generated", auto_generated);
597
    g_test_add_func ("/api/handle_write_close_free", handle_write_close_free);
598
    g_test_add_func ("/api/handle_new_from_file", handle_new_from_file);
599
    g_test_add_func ("/api/handle_new_from_data", handle_new_from_data);
600
    g_test_add_func ("/api/handle_new_from_gfile_sync", handle_new_from_gfile_sync);
601
    g_test_add_func ("/api/handle_new_from_stream_sync", handle_new_from_stream_sync);
602
    g_test_add_func ("/api/handle_read_stream_sync", handle_read_stream_sync);
603 604
    g_test_add_func ("/api/handle_has_sub", handle_has_sub);
    g_test_add_func ("/api/handle_get_pixbuf", handle_get_pixbuf);
605
    g_test_add_func ("/api/handle_get_pixbuf_sub", handle_get_pixbuf_sub);
606
    g_test_add_func ("/api/dimensions_and_position", dimensions_and_position);
607
    g_test_add_func ("/api/detects_cairo_context_in_error", detects_cairo_context_in_error);
608
    g_test_add_func ("/api/can_draw_to_non_image_surface", can_draw_to_non_image_surface);
609
    g_test_add_func ("/api/render_cairo_sub", render_cairo_sub);
610 611 612

    return g_test_run ();
}