All pastes #925906 Raw Edit

Anonymous

public text v1 · immutable
·published 2008-03-03 11:59 UTC
rendered paste body
#include <clutter/clutter.h>
#include <clutter-cairo.h>
#include <cairo.h>
#include <librsvg/rsvg.h>
#include <librsvg/rsvg-cairo.h>

static
void get_sizes(gint *cx, gint *cy, gint *sizes[2])
{
  g_print("sizes[0] = 0x%x\n", (int)(sizes[0]));
  if (sizes[0])
    (*(sizes[0])) = (*cx) = 300;
  g_print("sizes[1] = 0x%x\n", (int)(sizes[1]));
  if (sizes[1])
    (*(sizes[1])) = (*cy) = 200;
}

static RsvgHandle *
load_svg_file(char *fname, gint *cx, gint *cy)
{
  gsize file_length = 0;
  char *the_file = NULL;

  if (g_file_get_contents(fname, &the_file, &file_length, NULL)) {
    RsvgHandle *svg = rsvg_handle_new();
    gint *sizes[2] = {cx, cy};

    rsvg_handle_set_size_callback(svg, (RsvgSizeFunc)get_sizes, sizes, NULL);

    if (rsvg_handle_write(svg, (const guchar *)the_file, file_length, NULL)) {
      g_print("rsvg_handle_write of %d bytes has succeeded\n", file_length);
      return svg;
    }

    rsvg_handle_close(svg, NULL);
  }

  return NULL;
}

int
main(int argc, char **argv)
{
  ClutterActor *stage = NULL;
  ClutterActor *cairo = NULL;
  cairo_t *cr;
  RsvgHandle *svg = NULL;
  gint cx_svg = 0, cy_svg = 0;

  clutter_init(&argc, &argv);

  stage = clutter_stage_get_default();

  clutter_container_add(CLUTTER_CONTAINER(stage),
    cairo = g_object_new(CLUTTER_TYPE_CAIRO, "visible", TRUE, "x", 10, "y", 10, "surface-width", 300, "surface-height", 200, NULL), NULL);

  cr = clutter_cairo_create(CLUTTER_CAIRO(cairo));
  cairo_new_path(cr);
  cairo_move_to(cr, 10, 10);
  cairo_line_to(cr, 280, 10);
  cairo_line_to(cr, 280, 180);
  cairo_line_to(cr, 10, 180);
  cairo_close_path(cr);
  cairo_stroke(cr);

  g_print("&cx_svg = 0x%x and &cy_svg = 0x%x\n", (int)(&cx_svg), (int)(&cy_svg));

  svg = load_svg_file("hour-hand.svg", &cx_svg, &cy_svg);
  g_print("svg size is [%dx%d]\n", cx_svg, cy_svg);
  rsvg_handle_render_cairo(svg, cr);
  rsvg_handle_close(svg, NULL);
  cairo_destroy(cr);

  g_object_set(G_OBJECT(stage), "visible", TRUE, NULL);
  clutter_main();

  return 0;
}