#include <stdlib.h>
#include <cairo.h>
#include <librsvg/rsvg.h>
#include <librsvg/rsvg-cairo.h>
#include <glib.h>

int main(int argc, char *argv[]) {
  RsvgHandle *handle;
  GError *error = NULL;
  RsvgDimensionData dim;
  RsvgPositionData pos;
  char *node;
  clock_t t0, t1, t2, t3, t4;

  g_type_init ();

  if (argc < 2) {
    g_printerr ("Usage: %s FILE [ID]\n", argv[0]);
    exit (1);
  }

  t0 = clock ();
  handle = rsvg_handle_new_from_file (argv[1], &error);
  if (!handle) {
    g_printerr ("Error: %s\n", error->message);
    exit (1);
  }
    
  t1 = clock();
  rsvg_handle_get_dimensions (handle, &dim);
  g_print ("RSVG width=%d height=%d\n", dim.width, dim.height);

  if (argc > 2)
    node = argv[2];
  else
    node = NULL;

  t2 = clock ();
  if (!rsvg_handle_get_dimensions_sub (handle, &dim, node)) {
    g_printerr ("Failed to get dimension\n");
    exit(1);
  }

  t3 = clock ();
  if (!rsvg_handle_get_position_sub (handle, &pos, node)) {
    g_printerr ("Failed to get position\n");
    exit(1);
  }

  g_print ("Node %s width=%d heigh=%d at x=%d y=%d\n", node ? node : "<root>", dim.width, dim.height, pos.x, pos.y);

  t4 = clock ();
  g_print ("new-from-file         took %.3fs\n", (t1 - t0) * 1.0 / CLOCKS_PER_SEC);
  g_print ("handle-get-dimensions took %.3fs\n", (t2 - t1) * 1.0 / CLOCKS_PER_SEC);
  g_print ("get-position-sub      took %.3fs\n", (t3 - t2) * 1.0 / CLOCKS_PER_SEC);
  g_print ("get-dimensions-sub    took %.3fs\n", (t4 - t3) * 1.0 / CLOCKS_PER_SEC);

  return 0;
}