int main (int argc, char **argv)
{
	RsvgDimensionData dimensions;
	RsvgHandle *logo_handle;
	cairo_surface_t *surface;
	GError *error = NULL;
	cairo_t *cr;
	cairo_status_t status;
	char *input, *size, *output;
	GString *layer;
	char *layer_name;
	int h, w;

	g_type_init ();

	input = argv[1];
	size = argv[2];
	layer = g_string_new (argv[3]);
	g_string_ascii_down (layer);
	g_string_prepend_c (layer, '#');
	output = argv[4];

	if (sscanf (size, "%dx%d", &w, &h) != 2) {
		g_warning ("Couldn't parse size '%s'", size);
		return 1;
	}

	logo_handle = rsvg_handle_new_from_file (input, &error);
	if (!logo_handle) {
		g_warning ("Couldn't open '%s': %s", input, error->message);
		g_error_free (error);
		return 1;
	}

	surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, w, h);
	cr = cairo_create (surface);

	rsvg_handle_get_dimensions (logo_handle, &dimensions);
	cairo_scale (cr,
		     (double) w / dimensions.width,
		     (double) h / dimensions.height);

	layer_name = g_string_free (layer, FALSE);
	rsvg_handle_render_cairo_sub (logo_handle, cr, "#background");
//	rsvg_handle_render_cairo_sub (logo_handle, cr, "#base");
	rsvg_handle_render_cairo_sub (logo_handle, cr, layer_name);

	status = cairo_surface_write_to_png (surface, output);
	if (status != CAIRO_STATUS_SUCCESS) {
		g_warning ("Couldn't write output '%s': %s", output, cairo_status_to_string (status));
		return 1;
	}

	g_free (layer_name);
	cairo_destroy (cr);

	return 0;
}
Example #2
0
BitmapPtr SVG::internalRenderElement(const SVGElementPtr& pElement, 
        const glm::vec2& renderSize, const glm::vec2& size)
{
    glm::vec2 pos = pElement->getPos();
    glm::vec2 scale(renderSize.x/size.x, renderSize.y/size.y);
    IntPoint boundingBox = IntPoint(renderSize) + 
            IntPoint(int(scale.x+0.5), int(scale.y+0.5));
    BitmapPtr pBmp(new Bitmap(boundingBox, B8G8R8A8));
    FilterFill<Pixel32>(Pixel32(0,0,0,0)).applyInPlace(pBmp);

    cairo_surface_t* pSurface;
    cairo_t* pCairo;
    pSurface = cairo_image_surface_create_for_data(pBmp->getPixels(), 
            CAIRO_FORMAT_ARGB32, boundingBox.x, boundingBox.y, 
            pBmp->getStride());
    pCairo = cairo_create(pSurface);
    cairo_scale(pCairo, scale.x, scale.y);
    cairo_translate(pCairo, -pos.x, -pos.y);
    rsvg_handle_render_cairo_sub(m_pRSVG, pCairo, pElement->getUnescapedID().c_str()); 

    FilterUnmultiplyAlpha().applyInPlace(pBmp);

    cairo_surface_destroy(pSurface);
    cairo_destroy(pCairo);
   
    if (!BitmapLoader::get()->isBlueFirst()) {
        FilterFlipRGB().applyInPlace(pBmp);
    }

    return pBmp;
}
Example #3
0
static void process_rsvg(struct neg_loop_vars *loop, const char *fname)
{
    int i, j;
    struct neg_rsvg *rsvg;

    rsvg = neg_rsvg_open(fname);

    // {{{ TODO shoudl go into neg_set_defaults() or some such
    if (!loop->conf->out.width)
        loop->conf->out.width = rsvg->size.width;
    if (!loop->conf->out.height)
        loop->conf->out.height = rsvg->size.height;
    // }}}

    printf("input  %u x %u (%s)\n", rsvg->size.width, rsvg->size.height,
           fname);
    printf("output %u x %u (%s)\n", (unsigned)loop->conf->out.width,
           (unsigned)loop->conf->out.height, loop->conf->out.name);

    if (! loop->ctx)
        loop->ctx = loop->rndr->init(loop->conf);

    for (i = rsvg->layer_count-1; i>=0; i--) {
        struct neg_layer *lyr = &rsvg->layers[i];
        cairo_t *c;

        if (lyr->flags & NEG_LAYER_HIDDEN)
            continue;

        c = loop->rndr->slide_start(loop->ctx);

        printf("  * %s : ", lyr->name);

        if (!c)
            errx(1, "Could not create cairo");

        cairo_scale(c, loop->conf->out.width/rsvg->size.width,
                    loop->conf->out.height/rsvg->size.height);

        for(j=0; j<lyr->order->count; j++) {
            unsigned order = lyr->order->array[j];
            struct neg_layer *ord = &rsvg->layers[order];
            char id[strlen(ord->id)+2];

            printf("%s, ", ord->name);

            id[0] = '#';
            strcpy(id+1, ord->id);

            rsvg_handle_render_cairo_sub(rsvg->handle, c, id);
        }

        printf("\n");

        if (! loop->rndr->slide_end(loop->ctx))
            errx(1, "error writing out %s slide", loop->rndr->name);
    }
    neg_rsvg_close(rsvg);
}
Example #4
0
File: rsvg.c Project: chagge/libsvg
/**
 * rsvg_handle_get_pixbuf_sub:
 * @handle: An #RsvgHandle
 * @id: The id of an element inside the SVG, or %NULL to render the whole SVG. For
 * example, if you have a layer called "layer1" that you wish to render, pass 
 * "##layer1" as the id.
 *
 * Returns the pixbuf loaded by #handle.  The pixbuf returned will be reffed, so
 * the caller of this function must assume that ref.  If insufficient data has
 * been read to create the pixbuf, or an error occurred in loading, then %NULL
 * will be returned.  Note that the pixbuf may not be complete until
 * @rsvg_handle_close has been called.
 *
 * Returns: the pixbuf loaded by #handle, or %NULL.
 *
 * Since: 2.14
 **/
GdkPixbuf *
rsvg_handle_get_pixbuf_sub (RsvgHandle * handle, const char *id)
{
    RsvgDimensionData dimensions;
    GdkPixbuf *output = NULL;
    guint8 *pixels;
    cairo_surface_t *surface;
    cairo_t *cr;
    int rowstride;

    g_return_val_if_fail (handle != NULL, NULL);

    if (!handle->priv->finished)
        return NULL;

    rsvg_handle_get_dimensions (handle, &dimensions);
    if (!(dimensions.width && dimensions.height))
        return NULL;

    rowstride = dimensions.width * 4;

    pixels = g_try_malloc0 (dimensions.width * dimensions.height * 4UL);
    if (!pixels)
        return NULL;

    surface = cairo_image_surface_create_for_data (pixels,
                                                   CAIRO_FORMAT_ARGB32,
                                                   dimensions.width, dimensions.height, rowstride);
    cr = cairo_create (surface);
    cairo_surface_destroy (surface);

    if (rsvg_handle_render_cairo_sub (handle, cr, id)) {
        rsvg_cairo_to_pixbuf (pixels, rowstride, dimensions.height);

        output = gdk_pixbuf_new_from_data (pixels,
                                           GDK_COLORSPACE_RGB,
                                           TRUE,
                                           8,
                                           dimensions.width,
                                           dimensions.height,
                                           rowstride,
                                           (GdkPixbufDestroyNotify) rsvg_pixmap_destroy, NULL);
	} else {
        g_free (pixels);
        output = NULL;
	}

    cairo_destroy (cr);

    return output;
}
Example #5
0
static void
ar_card_theme_kde_paint_card (ArCardTheme *card_theme,
                              cairo_t *cr,
                              int card_id)
{
  ArCardThemePreimage *preimage_card_theme = (ArCardThemePreimage *) card_theme;
  ArCardThemeKDE *theme = (ArCardThemeKDE *) card_theme;
  ArSvg *svg = preimage_card_theme->cards_svg;
  char node[32];
  cairo_rectangle_t *card_extents;
  cairo_matrix_t matrix;
  cairo_font_options_t *font_options;

  if (G_UNLIKELY (card_id == AR_CARD_SLOT)) {
    ar_svg_render_cairo (preimage_card_theme->slot_preimage,
                                 cr,
                                 preimage_card_theme->card_size.width,
                                 preimage_card_theme->card_size.height);
    return;
  }

  if (theme->legacy)
    ar_card_get_legacy_node_by_id_snprintf (node, sizeof (node), card_id);
  else
    ar_card_get_node_by_id_snprintf (node, sizeof (node), card_id);

  card_extents = ar_card_theme_kde_get_card_extents (theme, card_id, node);
  if (!card_extents)
    return;

  cairo_save (cr);

  font_options = ar_svg_get_font_options (svg);
  if (font_options) {
    cairo_set_antialias (cr, cairo_font_options_get_antialias (font_options));
    cairo_set_font_options (cr, font_options);
  }

  cairo_matrix_init_identity (&matrix);
  cairo_matrix_scale (&matrix,
                      preimage_card_theme->card_size.width / card_extents->width,
                      preimage_card_theme->card_size.height / card_extents->height);
  cairo_matrix_translate (&matrix, -card_extents->x, -card_extents->y);

  cairo_set_matrix (cr, &matrix);

  rsvg_handle_render_cairo_sub (RSVG_HANDLE (svg), cr, node);

  cairo_restore (cr);
}
Example #6
0
static VALUE
rb_rsvg_handle_render_cairo(int argc, VALUE *argv, VALUE self)
{
    VALUE cr, id;
    rb_scan_args(argc, argv, "11", &cr, &id);

    if (NIL_P(id)) {
        rsvg_handle_render_cairo( _SELF(self), RVAL2CRCONTEXT(cr));
    } else {
        rsvg_handle_render_cairo_sub( _SELF(self), RVAL2CRCONTEXT(cr),
                                      (const char *)RVAL2CSTR(id));
    }

    return Qnil;
}
Example #7
0
cv::Mat JobFileSvg::loadSlice(int layer){
	cv::Mat ret;

	//clear cairo context
	m_cairoMutex.lock();
	cairo_set_source_rgb (m_pCairo, 0, 0, 0);
	cairo_paint (m_pCairo);

	std::ostringstream id;
	id << "#layer" << layer;//filter with group name
	printf("(JobFile) Extract layer %s from svg\n",id.str().c_str() );
	rsvg_handle_render_cairo_sub(m_pRsvgHandle, m_pCairo, id.str().c_str() ); 

	ret = cv::Mat(
			cairo_image_surface_get_height(m_pSurface),
			cairo_image_surface_get_width(m_pSurface),
			CV_8UC4,
			(void*) cairo_image_surface_get_data(m_pSurface)
			);

	m_cairoMutex.unlock();

	//convert to grayscale image
	//ret.convertTo( ret, CV_8UC1 );
	//convert to grayscale image with alpha channel
	//ret.convertTo( ret, CV_8UC2 );

	/* Remove interpolation B9Creator beta software approach to
	 * get black-white-image (see crushbitmap.cpp )
	 * if(c.red()<32 && c.blue()<32 && c.green()<32) return false;
	 */
	cv::threshold(ret, ret, 31, 255, cv::THRESH_BINARY);

	//use red values as mask (assume white/black image)
#ifdef FLIP_COLORS
	int from_to[] = { 0,0,  1,1,  2,2,  2,3 };
#else
	int from_to[] = { 0,0,  1,1,  2,2,  0,3 };
#endif
	cv::mixChannels( &ret, 1, &ret, 1, from_to, 4 );

	return ret;
}
Example #8
0
static cairo_rectangle_t *
ar_card_theme_kde_get_card_extents (ArCardThemeKDE *theme,
                                    int card_id,
                                    const char *node)
{
  ArSvg *svg;
  cairo_rectangle_t *card_extents;
  cairo_rectangle_t rect;
  cairo_surface_t *surface;
  cairo_t *cr;

  card_extents = &theme->card_extents[card_id];
  /* Is it initalised yet? */
  if (card_extents->width != 0. && card_extents->height != 0.)
    return card_extents;

  svg = ((ArCardThemePreimage *) theme)->cards_svg;

  surface = cairo_recording_surface_create (CAIRO_CONTENT_ALPHA, NULL);
  cr = cairo_create (surface);
  cairo_set_tolerance (cr, 1.);
  ar_profilestart ("getting ink extents for node %s", node);
  rsvg_handle_render_cairo_sub (RSVG_HANDLE (svg), cr, node);
  ar_profileend ("getting ink extents for node %s", node);
  cairo_destroy (cr);

  cairo_recording_surface_ink_extents (surface, &rect.x, &rect.y, &rect.width, &rect.height);
  cairo_surface_destroy (surface);

  ar_debug_print (AR_DEBUG_CARD_THEME,
                      "card %s %.3f x%.3f at (%.3f | %.3f)\n",
                      node,
                      card_extents->width, card_extents->height,
                      card_extents->x, card_extents->y);

  *card_extents = rect;

  /* Sanity check; necessary? */
  if (rect.width == 0. || rect.height == 0.)
    return NULL;

  return card_extents;
}
Example #9
0
/**
 * rsvg_handle_get_pixbuf_sub:
 * @handle: An #RsvgHandle
 * @id: The id of an element inside the SVG, or %NULL to render the whole SVG. For
 * example, if you have a layer called "layer1" that you wish to render, pass 
 * "##layer1" as the id.
 *
 * Returns the pixbuf loaded by #handle.  The pixbuf returned will be reffed, so
 * the caller of this function must assume that ref.  If insufficient data has
 * been read to create the pixbuf, or an error occurred in loading, then %NULL
 * will be returned.  Note that the pixbuf may not be complete until
 * @rsvg_handle_close has been called.
 *
 * Returns: the pixbuf loaded by #handle, or %NULL.
 *
 * Since: 2.14
 **/
GdkPixbuf *
rsvg_handle_get_pixbuf_sub (RsvgHandle * handle, const char *id)
{
    RsvgDimensionData dimensions;
    GdkPixbuf *output = NULL;
    cairo_surface_t *surface;
    cairo_t *cr;

    g_return_val_if_fail (handle != NULL, NULL);

    if (!handle->priv->finished)
        return NULL;

    rsvg_handle_get_dimensions (handle, &dimensions);
    if (!(dimensions.width && dimensions.height))
        return NULL;

    surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
                                          dimensions.width, dimensions.height);
    if (cairo_surface_status (surface) != CAIRO_STATUS_SUCCESS) {
        cairo_surface_destroy (surface);
        return NULL;
    }

    cr = cairo_create (surface);

    if (!rsvg_handle_render_cairo_sub (handle, cr, id)) {
        cairo_destroy (cr);
        cairo_surface_destroy (surface);
        return NULL;
    }

    cairo_destroy (cr);

    output = rsvg_cairo_surface_to_pixbuf (surface);
    cairo_surface_destroy (surface);

    return output;
}
/**
 * rsvg_handle_render_cairo:
 * @handle: A RsvgHandle
 * @cr: A Cairo renderer
 *
 * Draws a SVG to a Cairo surface
 *
 * Returns: %TRUE if drawing succeeded.
 * Since: 2.14
 */
gboolean
rsvg_handle_render_cairo (RsvgHandle * handle, cairo_t * cr)
{
    return rsvg_handle_render_cairo_sub (handle, cr, NULL);
}
/*
 * Draws global image with fill color onto a pixmap with the given
 * resolution and returns it.
 *
 */
xcb_pixmap_t draw_image(uint32_t *resolution) {
    xcb_pixmap_t bg_pixmap = XCB_NONE;

    RsvgDimensionData svg_dimensions;
    rsvg_handle_get_dimensions(svg, &svg_dimensions);
    int indicator_x_physical = ceil(scaling_factor() * svg_dimensions.width);
    int indicator_y_physical = ceil(scaling_factor() * svg_dimensions.height);

    if (!vistype)
        vistype = get_root_visual_type(screen);
    bg_pixmap = create_bg_pixmap(conn, screen, resolution, color);
    /* Initialize cairo: Create one in-memory surface to render the unlock
     * indicator on, create one XCB surface to actually draw (one or more,
     * depending on the amount of screens) unlock indicators on. */
    cairo_surface_t *output = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, indicator_x_physical, indicator_y_physical);
    cairo_t *ctx = cairo_create(output);

    cairo_surface_t *xcb_output = cairo_xcb_surface_create(conn, bg_pixmap, vistype, resolution[0], resolution[1]);
    cairo_t *xcb_ctx = cairo_create(xcb_output);

    if (img) {
        if (!tile) {
            cairo_set_source_surface(xcb_ctx, img, 0, 0);
            cairo_paint(xcb_ctx);
        } else {
            /* create a pattern and fill a rectangle as big as the screen */
            cairo_pattern_t *pattern;
            pattern = cairo_pattern_create_for_surface(img);
            cairo_set_source(xcb_ctx, pattern);
            cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
            cairo_rectangle(xcb_ctx, 0, 0, resolution[0], resolution[1]);
            cairo_fill(xcb_ctx);
            cairo_pattern_destroy(pattern);
        }
    } else {
        char strgroups[3][3] = {{color[0], color[1], '\0'},
                                {color[2], color[3], '\0'},
                                {color[4], color[5], '\0'}};
        uint32_t rgb16[3] = {(strtol(strgroups[0], NULL, 16)),
                             (strtol(strgroups[1], NULL, 16)),
                             (strtol(strgroups[2], NULL, 16))};
        cairo_set_source_rgb(xcb_ctx, rgb16[0] / 255.0, rgb16[1] / 255.0, rgb16[2] / 255.0);
        cairo_rectangle(xcb_ctx, 0, 0, resolution[0], resolution[1]);
        cairo_fill(xcb_ctx);
    }

    if (unlock_state >= STATE_KEY_PRESSED && unlock_indicator) {
        cairo_scale(ctx, scaling_factor(), scaling_factor());

        rsvg_handle_render_cairo_sub(svg, ctx, "#bg");


        /* Use the appropriate color for the different PAM states
         * (currently verifying, wrong password, or default) */
        if (!(unlock_state == STATE_KEY_ACTIVE ||
            unlock_state == STATE_BACKSPACE_ACTIVE) ||
            !remove_background) {

            switch (pam_state) {
                case STATE_PAM_VERIFY:
                    rsvg_handle_render_cairo_sub(svg, ctx, "#verify");
                    break;
                case STATE_PAM_WRONG:
                    rsvg_handle_render_cairo_sub(svg, ctx, "#fail");
                    break;
                default:
                    rsvg_handle_render_cairo_sub(svg, ctx, "#idle");
                    break;
            }
        }

        /* After the user pressed any valid key or the backspace key, we
         * highlight a random part of the unlock indicator to confirm this
         * keypress. */
        if (unlock_state == STATE_KEY_ACTIVE ||
            unlock_state == STATE_BACKSPACE_ACTIVE) {

            if(++current_frame >= anim_layer_count)
                current_frame = 0;

            if (unlock_state == STATE_KEY_ACTIVE) {
                if(!sequential_animation)
                    current_frame = rand() % anim_layer_count;
                char anim_id[9];
                snprintf(anim_id, sizeof(anim_id), "#anim%02d", current_frame);
                rsvg_handle_render_cairo_sub(svg, ctx, anim_id);
            } else {
                rsvg_handle_render_cairo_sub(svg, ctx, "#backspace");
            }

        }

        rsvg_handle_render_cairo_sub(svg, ctx, "#fg");
    }

    if (xr_screens > 0) {
        /* Composite the unlock indicator in the middle of each screen. */
        for (int screen = 0; screen < xr_screens; screen++) {
            int x = (xr_resolutions[screen].x + ((xr_resolutions[screen].width / 2) - (indicator_x_physical / 2)));
            int y = (xr_resolutions[screen].y + ((xr_resolutions[screen].height / 2) - (indicator_y_physical / 2)));
            cairo_set_source_surface(xcb_ctx, output, x, y);
            cairo_rectangle(xcb_ctx, x, y, indicator_x_physical, indicator_y_physical);
            cairo_fill(xcb_ctx);
        }
    } else {
        /* We have no information about the screen sizes/positions, so we just
         * place the unlock indicator in the middle of the X root window and
         * hope for the best. */
        int x = (last_resolution[0] / 2) - (indicator_x_physical / 2);
        int y = (last_resolution[1] / 2) - (indicator_y_physical / 2);
        cairo_set_source_surface(xcb_ctx, output, x, y);
        cairo_rectangle(xcb_ctx, x, y, indicator_x_physical, indicator_y_physical);
        cairo_fill(xcb_ctx);
    }

    cairo_surface_destroy(xcb_output);
    cairo_surface_destroy(output);
    cairo_destroy(ctx);
    cairo_destroy(xcb_ctx);
    return bg_pixmap;
}