cairo-utils.cpp revision cf99b5253f47e8fa5ba3c655712fc15031d1d91a
/*
* Helper functions to use cairo with inkscape
*
* Copyright (C) 2007 bulia byak
* Copyright (C) 2008 Johan Engelen
*
* Released under GNU GPL
*
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "display/cairo-utils.h"
#include <stdexcept>
#include "color.h"
#include "style.h"
#include "helper/geom-curves.h"
namespace {
/**
* Key for cairo_surface_t to keep track of current color interpolation value
* Only the address of the structure is used, it is never initialized. See:
* http://www.cairographics.org/manual/cairo-Types.html#cairo-user-data-key-t
*/
} // namespace
namespace Inkscape {
CairoGroup::~CairoGroup() {
if (pushed) {
}
}
void CairoGroup::push() {
pushed = true;
}
pushed = true;
}
if (pushed) {
pushed = false;
return ret;
} else {
}
}
if (pushed) {
pushed = false;
return retmm;
} else {
}
}
void CairoGroup::pop_to_source() {
if (pushed) {
pushed = false;
}
}
{}
{
}
{
}
{
}
{
return ret;
}
} // namespace Inkscape
/*
* Can be called recursively.
* If optimize_stroke == false, the view Rect is not used.
*/
static void
feed_curve_to_cairo(cairo_t *cr, Geom::Curve const &c, Geom::Affine const & trans, Geom::Rect view, bool optimize_stroke)
{
if( is_straight_curve(c) )
{
if (!optimize_stroke) {
} else {
} else {
}
}
}
else if(Geom::QuadraticBezier const *quadratic_bezier = dynamic_cast<Geom::QuadraticBezier const*>(&c)) {
if (!optimize_stroke) {
} else {
} else {
}
}
}
//points[0] *= trans; // don't do this one here for fun: it is only needed for optimized strokes
if (!optimize_stroke) {
cairo_curve_to(cr, points[1][0], points[1][1], points[2][0], points[2][1], points[3][0], points[3][1]);
} else {
cairo_curve_to(cr, points[1][0], points[1][1], points[2][0], points[2][1], points[3][0], points[3][1]);
} else {
}
}
}
// else if(Geom::SVGEllipticalArc const *svg_elliptical_arc = dynamic_cast<Geom::SVGEllipticalArc *>(c)) {
// //TODO: get at the innards and spit them out to cairo
// }
else {
//this case handles sbasis as well as all other curve types
//recurse to convert the new path resulting from the sbasis to svgd
}
}
}
/** Feeds path-creating calls to the cairo context translating them from the Path */
static void
{
return;
feed_curve_to_cairo(ct, *cit, Geom::identity(), Geom::Rect(), false); // optimize_stroke is false, so the view rect is not used
}
}
}
/** Feeds path-creating calls to the cairo context translating them from the Path, with the given transform and shift */
static void
feed_path_to_cairo (cairo_t *ct, Geom::Path const &path, Geom::Affine trans, Geom::OptRect area, bool optimize_stroke, double stroke_width)
{
if (!area)
return;
return;
// Transform all coordinates to coords within "area"
// Pass transformation to feed_curve, so that we don't need to create a whole new path.
}
if (!optimize_stroke) {
} else {
/* We cannot use cairo_close_path(ct) here because some parts of the path may have been
clipped and not drawn (maybe the before last segment was outside view area), which
would result in closing the "subpath" after the last interruption, not the entire path.
However, according to cairo documentation:
The behavior of cairo_close_path() is distinct from simply calling cairo_line_to() with the equivalent coordinate
in the case of stroking. When a closed sub-path is stroked, there are no caps on the ends of the sub-path. Instead,
there is a line join connecting the final and initial segments of the sub-path.
The correct fix will be possible when cairo introduces methods for moving without
see bug 168129
*/
}
}
}
/** Feeds path-creating calls to the cairo context translating them from the PathVector, with the given transform and shift
* One must have done cairo_new_path(ct); before calling this function. */
void
feed_pathvector_to_cairo (cairo_t *ct, Geom::PathVector const &pathv, Geom::Affine trans, Geom::OptRect area, bool optimize_stroke, double stroke_width)
{
if (!area)
return;
return;
}
}
/** Feeds path-creating calls to the cairo context translating them from the PathVector
* One must have done cairo_new_path(ct); before calling this function. */
void
{
return;
}
}
} else {
return SP_CSS_COLOR_INTERPOLATION_AUTO;
}
}
/** Set the color_interpolation_value for a Cairo surface.
* Transform the surface between sRGB and linearRGB if necessary. */
void
if( ci_in == SP_CSS_COLOR_INTERPOLATION_SRGB &&
}
if( ci_in == SP_CSS_COLOR_INTERPOLATION_LINEARRGB &&
}
}
}
void
}
void
{
cairo_set_source_rgba(ct, SP_RGBA32_R_F(rgba), SP_RGBA32_G_F(rgba), SP_RGBA32_B_F(rgba), SP_RGBA32_A_F(rgba));
}
void
{
}
{
}
{
}
void
{
ink_matrix_to_cairo(cm, m);
}
void
{
ink_matrix_to_cairo(cm, m);
}
void
{
}
{
int w = gdk_pixbuf_get_width(pb);
int h = gdk_pixbuf_get_height(pb);
return pbs;
}
/**
* Cleanup function for GdkPixbuf.
* This function should be passed as the GdkPixbufDestroyNotify parameter
* to gdk_pixbuf_new_from_data when creating a GdkPixbuf backed by
* a Cairo surface.
*/
{
}
/**
* Create an exact copy of a surface.
* Creates a surface that has the same type, content type, dimensions and contents
* as the specified surface.
*/
{
if (cairo_surface_get_type(s) == CAIRO_SURFACE_TYPE_IMAGE) {
// use memory copy instead of using a Cairo context
int stride = cairo_image_surface_get_stride(s);
int h = cairo_image_surface_get_height(s);
} else {
// generic implementation
cairo_set_source_surface(ct, s, 0, 0);
}
return ns;
}
/**
* Create a surface that differs only in pixel content.
* Creates a surface that has the same type, content type and dimensions
* as the specified surface. Pixel contents are not copied.
*/
{
return ns;
}
{
return ns;
}
/**
* Extract the alpha channel into a new surface.
* Creates a surface with a content type of CAIRO_CONTENT_ALPHA that contains
* the alpha values of pixels from @a s.
*/
{
cairo_set_source_surface(ct, s, 0, 0);
return alpha;
}
{
} else {
}
return out;
}
void
{
{
// use memory copy instead of using a Cairo context
int h = cairo_image_surface_get_height(src);
} else {
// generic implementation
}
}
int
{
// For now only image surface is handled.
// Later add others, e.g. cairo-gl
return cairo_image_surface_get_width(surface);
}
int
{
return cairo_image_surface_get_height(surface);
}
static int ink_cairo_surface_average_color_internal(cairo_surface_t *surface, double &rf, double &gf, double &bf, double &af)
{
/* TODO convert this to OpenMP somehow */
for (int x = 0; x < width; ++x) {
EXTRACT_ARGB32(px, a,r,g,b)
rf += r / 255.0;
gf += g / 255.0;
bf += b / 255.0;
af += a / 255.0;
}
}
}
{
ASSEMBLE_ARGB32(px, a,r,g,b);
return px;
}
void ink_cairo_surface_average_color(cairo_surface_t *surface, double &r, double &g, double &b, double &a)
{
r /= a;
g /= a;
b /= a;
a /= count;
}
void ink_cairo_surface_average_color_premul(cairo_surface_t *surface, double &r, double &g, double &b, double &a)
{
r /= count;
g /= count;
b /= count;
a /= count;
}
void srgb_to_linear( double* c ) {
if( *c < 0.04045 ) {
*c /= 12.92;
} else {
}
}
*c = unpremul_alpha( *c, a );
double cc = *c/255.0;
if( cc < 0.04045 ) {
cc /= 12.92;
} else {
}
cc *= 255.0;
*c = (int)cc;
*c = premul_alpha( *c, a );
}
*c = unpremul_alpha( *c, a );
double cc = *c/255.0;
if( cc < 0.0031308 ) {
cc *= 12.92;
} else {
}
cc *= 255.0;
*c = (int)cc;
*c = premul_alpha( *c, a );
}
{
/* TODO convert this to OpenMP somehow */
for (int x = 0; x < width; ++x) {
if( a != 0 ) {
srgb_to_linear( &r, a );
srgb_to_linear( &g, a );
srgb_to_linear( &b, a );
}
ASSEMBLE_ARGB32(px2, a,r,g,b);
}
}
}
{
/* TODO convert this to OpenMP somehow */
for (int x = 0; x < width; ++x) {
if( a != 0 ) {
linear_to_srgb( &r, a );
linear_to_srgb( &g, a );
linear_to_srgb( &b, a );
}
ASSEMBLE_ARGB32(px2, a,r,g,b);
}
}
}
{
int const w = 6;
int const h = 6;
cairo_rectangle(ct, 0, 0, w, h);
cairo_rectangle(ct, w, h, w, h);
cairo_fill(ct);
return p;
}
/* The following two functions use "from" instead of "to", because when you write:
val1 = argb32_from_pixbuf(val1);
the name of the format is closer to the value in that format. */
{
guint32 o = 0;
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
#else
guint32 a = (c & 0x000000ff);
#endif
if (a != 0) {
// extract color components
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
guint32 r = (c & 0x000000ff);
#else
#endif
// premultiply
r = premul_alpha(r, a);
b = premul_alpha(b, a);
g = premul_alpha(g, a);
// combine into output
o = (a << 24) | (r << 16) | (g << 8) | (b);
}
return o;
}
{
if (a == 0) return 0;
// extract color components
guint32 b = (c & 0x000000ff);
// unpremultiply; adding a/2 gives correct rounding
// (taken from Cairo sources)
r = (r * 255 + a/2) / a;
b = (b * 255 + a/2) / a;
g = (g * 255 + a/2) / a;
// combine into output
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
#else
#endif
return o;
}
/**
* Convert pixel data from GdkPixbuf format to ARGB.
* This will convert pixel data from GdkPixbuf format to Cairo's native pixel format.
* This involves premultiplying alpha and shuffling around the channels.
* Pixbuf data must have an alpha channel, otherwise the results are undefined
* (usually a segfault).
*/
void
{
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
++px;
}
}
}
/**
* Convert pixel data from ARGB to GdkPixbuf format.
* This will convert pixel data from GdkPixbuf format to Cairo's native pixel format.
* This involves premultiplying alpha and shuffling around the channels.
*/
void
{
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
++px;
}
}
}
/**
* Converts GdkPixbuf's data to premultiplied ARGB.
* This function will convert a GdkPixbuf in place into Cairo's native pixel format.
* Note that this is a hack intended to save memory. When the pixbuf is in Cairo's format,
* using it with GTK will result in corrupted drawings.
*/
void
{
}
/**
* Converts GdkPixbuf's data back to its native format.
* Once this is done, the pixbuf can be used with GTK again.
*/
void
{
}
{
guint32 r, g, b, a;
a = (in & 0x000000ff);
ASSEMBLE_ARGB32(px, a, r, g, b)
return px;
}
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :