[cairo-commit] cairo-demo/cairo_snippets COPYING, NONE, 1.1 ChangeLog, NONE, 1.1 Makefile, NONE, 1.1 README, NONE, 1.1 bezier_spline.cairo, NONE, 1.1 cairo_snippets_gtk.c, NONE, 1.1 cairo_snippets_png.c, NONE, 1.1 caps.cairo, NONE, 1.1 caps_and_joins.cairo, NONE, 1.1 fill_and_stroke.cairo, NONE, 1.1 fill_and_stroke2.cairo, NONE, 1.1 gradient.cairo, NONE, 1.1 image.cairo, NONE, 1.1 joins.cairo, NONE, 1.1 libsvg.cairo, NONE, 1.1 path.cairo, NONE, 1.1 png_io.c, NONE, 1.1 png_io.h, NONE, 1.1 prepare_snippets.c, NONE, 1.1 snippets.h, NONE, 1.1 text.cairo, NONE, 1.1 text_centering.cairo, NONE, 1.1 xxx_clipping.cairo, NONE, 1.1 xxx_dash.cairo, NONE, 1.1 xxx_image_clipping.cairo, NONE, 1.1 xxx_long_lines.cairo, NONE, 1.1 xxx_multi_segment_caps.cairo, NONE, 1.1 xxx_self_intersect.cairo, NONE, 1.1

OEyvind Kolaas commit at pdx.freedesktop.org
Sun May 16 12:09:52 PDT 2004


Committed by: pippin

Update of /cvs/cairo/cairo-demo/cairo_snippets
In directory pdx:/tmp/cvs-serv528

Added Files:
	COPYING ChangeLog Makefile README bezier_spline.cairo 
	cairo_snippets_gtk.c cairo_snippets_png.c caps.cairo 
	caps_and_joins.cairo fill_and_stroke.cairo 
	fill_and_stroke2.cairo gradient.cairo image.cairo joins.cairo 
	libsvg.cairo path.cairo png_io.c png_io.h prepare_snippets.c 
	snippets.h text.cairo text_centering.cairo xxx_clipping.cairo 
	xxx_dash.cairo xxx_image_clipping.cairo xxx_long_lines.cairo 
	xxx_multi_segment_caps.cairo xxx_self_intersect.cairo 
Log Message:
initial import of cairo_snippets


--- NEW FILE: COPYING ---
cairo_snippets is released as PUBLIC DOMAIN,
since it's main purpose is to illustrate usage of the cairo rendering API.

--- NEW FILE: ChangeLog ---
2004-05-16 OEyvind Kolaas <pippin at freedesktop.org> 

	Initial import

--- NEW FILE: Makefile ---
CFLAGS = `pkg-config --cflags cairo libpng libsvg-cairo`
LIBS   = `pkg-config --libs   cairo libpng libsvg-cairo`

CFLAGS += `pkg-config --cflags gtkcairo`
LIBS   += `pkg-config --libs gtkcairo`

all: cairo_snippets_gtk cairo_snippets_png

snippets.c: prepare_snippets *.cairo
	./prepare_snippets *.cairo

clean:
	rm -f prepare_snippets cairo_snippets_png cairo_snippets_gtk snippets.html snippets.c *.png *.o *~

cairo_snippets_png: cairo_snippets_png.o snippets.o png_io.o
	gcc $< snippets.o png_io.o -o $@ $(LIBS)

cairo_snippets_gtk: cairo_snippets_gtk.o snippets.o png_io.o
	gcc $< snippets.o png_io.o -o $@ $(LIBS)

scp: pngs
	scp *.png freedesktop.org:public_html/cairo/cairo_snippets/
	scp snippets.html freedesktop.org:public_html/cairo/cairo_snippets/index.html

pngs: cairo_snippets_png
	./cairo_snippets_png
epss: pngs
	for a in *.png; do \
	   pngtopnm $$a | pnmtops -noturn -equalpixels > `echo $$a | sed s/png/eps/`;\
	done

--- NEW FILE: README ---
cairo_snippets is a collection of code for testing, and demonstrating
the cairo rendering API.

prepare_snippets - is a program used during the build process, it creates
    the snippets.c file, to create a new front end for cairo_snippets, look
    at the data and functions exported by snippets.c in snippets.h , by default
    during the build process all .cairo files in the current dir is included in
    the snippet collection.

cairo_snippets_png - processes all contained snippets, writing a png file
    for each, in addition to a snippets.html index which simultanously displays
    all snippets contained.

cairo_snippets_gtk - a gui displaying a list of snippets, the source for the
    currently selected snippet, and the rendered output, it uses GtkCairo,
    and thus utilitizes the Render backend by default, if cairo and GtkCairo is
    compiled with support for glitz, setting the enviroment variable GTKCAIRO_GL=1
    will use the glitz backend instead

*.cairo - are the actual snippets, adding a new snippet requires you to add the
    snippet, and recompile, the Makefile should figure out that things have
    changed.




--- NEW FILE: bezier_spline.cairo ---
double x0=0.1, y0=0.5,
       x1=0.4, y1=0.9,
       x2=0.6, y2=0.1,
       x3=0.9, y3=0.5;
cairo_move_to (cr, x0, y0);
cairo_curve_to (cr, x1, y1, x2, y2, x3, y3);
cairo_stroke (cr);

cairo_set_rgb_color (cr, 1,0.2,0.2);
cairo_set_alpha (cr, 0.6);
cairo_set_line_width (cr, 0.03);
cairo_move_to (cr,x0,y0); cairo_line_to (cr,x1,y1);
cairo_move_to (cr,x2,y2); cairo_line_to (cr,x3,y3);
cairo_stroke (cr);

--- NEW FILE: cairo_snippets_gtk.c ---
(This appears to be a binary file; contents omitted.)

--- NEW FILE: cairo_snippets_png.c ---
(This appears to be a binary file; contents omitted.)

--- NEW FILE: caps.cairo ---
cairo_set_line_width (cr, 0.12);
cairo_set_line_cap  (cr, CAIRO_LINE_CAP_BUTT); /* default */
cairo_move_to (cr, 0.25, 0.2); cairo_line_to (cr, 0.25, 0.8);
cairo_stroke (cr);
cairo_set_line_cap  (cr, CAIRO_LINE_CAP_ROUND);
cairo_move_to (cr, 0.5, 0.2); cairo_line_to (cr, 0.5, 0.8);
cairo_stroke (cr);
cairo_set_line_cap  (cr, CAIRO_LINE_CAP_SQUARE);
cairo_move_to (cr, 0.75, 0.2); cairo_line_to (cr, 0.75, 0.8);
cairo_stroke (cr);
cairo_move_to (cr, 0.25, 0.2); cairo_line_to (cr, 0.25, 0.8);
cairo_move_to (cr, 0.5, 0.2);  cairo_line_to (cr, 0.5, 0.8);
cairo_move_to (cr, 0.75, 0.2); cairo_line_to (cr, 0.75, 0.8);
cairo_set_rgb_color (cr, 1,1,1);
cairo_set_line_width (cr, 0.01);
cairo_stroke (cr);

--- NEW FILE: caps_and_joins.cairo ---
cairo_set_line_width (cr, 0.12);
cairo_move_to (cr, 0.3, 0.3);
cairo_rel_line_to (cr, 0.2, -0.2);
cairo_rel_line_to (cr, 0.2, 0.2);
cairo_set_line_cap  (cr, CAIRO_LINE_CAP_BUTT);
cairo_set_line_join (cr, CAIRO_LINE_JOIN_BEVEL);
cairo_stroke (cr);
cairo_move_to (cr, 0.3, 0.6);
cairo_rel_line_to (cr, 0.2, -0.2);
cairo_rel_line_to (cr, 0.2, 0.2);
cairo_set_line_cap  (cr, CAIRO_LINE_CAP_SQUARE);
cairo_set_line_join (cr, CAIRO_LINE_JOIN_MITER);
cairo_stroke (cr);
cairo_move_to (cr, 0.3, 0.9);
cairo_rel_line_to (cr, 0.2, -0.2);
cairo_rel_line_to (cr, 0.2, 0.2);
cairo_set_line_cap  (cr, CAIRO_LINE_CAP_ROUND);
cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
cairo_stroke (cr);

--- NEW FILE: fill_and_stroke.cairo ---
cairo_move_to (cr, 0.5, 0.1);
cairo_line_to (cr, 0.9, 0.9);
cairo_rel_line_to (cr, -0.4, 0.0);
cairo_curve_to (cr, 0.2, 0.9, 0.2, 0.5, 0.5, 0.5);

cairo_save (cr);
    cairo_set_rgb_color (cr, 0, 0, 1);
    cairo_fill (cr);
cairo_restore (cr);

cairo_close_path (cr);
cairo_stroke (cr);

--- NEW FILE: fill_and_stroke2.cairo ---
cairo_move_to (cr, 0.5, 0.1);
cairo_line_to (cr, 0.9, 0.9);
cairo_rel_line_to (cr, -0.4, 0.0);
cairo_curve_to (cr, 0.2, 0.9, 0.2, 0.5, 0.5, 0.5);
cairo_close_path (cr);

cairo_move_to (cr, 0.25, 0.1);
cairo_rel_line_to (cr, 0.2, 0.2);
cairo_rel_line_to (cr, -0.2, 0.2);
cairo_rel_line_to (cr, -0.2, -0.2);
cairo_close_path (cr);


cairo_save (cr);
    cairo_set_rgb_color (cr, 0, 0, 1);
    cairo_fill (cr);
cairo_restore (cr);

cairo_stroke (cr);

--- NEW FILE: gradient.cairo ---
cairo_pattern_t *pat;
pat = cairo_pattern_create_linear (0.0, 0.0,  0.0, 1.0);
cairo_pattern_add_color_stop (pat, 1, 0, 0, 0, 1);
cairo_pattern_add_color_stop (pat, 0, 1, 1, 1, 1);
cairo_rectangle (cr, 0,0,1,1);
cairo_set_pattern (cr, pat);
cairo_fill (cr);

cairo_pattern_destroy (pat);
pat = cairo_pattern_create_radial (0.45, 0.4, 0.1,
                                   0.4,  0.4, 0.5);
cairo_pattern_add_color_stop (pat, 0, 1, 1, 1, 1);
cairo_pattern_add_color_stop (pat, 1, 0, 0, 0, 1);
cairo_set_pattern (cr, pat);
cairo_arc (cr, 0.5, 0.5, 0.3, 0, 2 * M_PI);
cairo_fill (cr);
cairo_pattern_destroy (pat);



--- NEW FILE: image.cairo ---
int              w,h,stride;
char            *buffer;
cairo_surface_t *image;

buffer = read_png_argb32 (
              "data/romedalen.png", &w,&h, &stride);
image  = cairo_surface_create_for_image (
              buffer, CAIRO_FORMAT_ARGB32, w,h, stride);

cairo_translate (cr, 0.5, 0.5);
cairo_rotate (cr, 45* M_PI/180);
cairo_scale  (cr, 1.0/w, 1.0/h);
cairo_translate (cr, -0.5*w, -0.5*h);

cairo_move_to (cr, 0,0);
cairo_show_surface (cr, image, w, h);
cairo_surface_destroy (image);
free (buffer);

--- NEW FILE: joins.cairo ---
cairo_set_line_width (cr, 0.12);
cairo_move_to (cr, 0.3, 0.3);
cairo_rel_line_to (cr, 0.2, -0.2);
cairo_rel_line_to (cr, 0.2, 0.2);
cairo_set_line_join (cr, CAIRO_LINE_JOIN_MITER); /* default */
cairo_stroke (cr);

cairo_move_to (cr, 0.3, 0.6);
cairo_rel_line_to (cr, 0.2, -0.2);
cairo_rel_line_to (cr, 0.2, 0.2);
cairo_set_line_join (cr, CAIRO_LINE_JOIN_BEVEL);
cairo_stroke (cr);

cairo_move_to (cr, 0.3, 0.9);
cairo_rel_line_to (cr, 0.2, -0.2);
cairo_rel_line_to (cr, 0.2, 0.2);
cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
cairo_stroke (cr);

--- NEW FILE: libsvg.cairo ---
svg_cairo_t *svgc;
int          width,
             height;

svg_cairo_create (&svgc);

svg_cairo_parse (svgc, "data/home.svg");
svg_cairo_get_size (svgc, &width, &height);
cairo_scale (cr, 1.0/width, 1.0/height);
svg_cairo_render (svgc, cr);

svg_cairo_destroy (svgc);

--- NEW FILE: path.cairo ---
cairo_move_to (cr, 0.5, 0.1);
cairo_line_to (cr, 0.9, 0.9);
cairo_rel_line_to (cr, -0.4, 0.0);
cairo_curve_to (cr, 0.2, 0.9, 0.2, 0.5, 0.5, 0.5);

cairo_stroke (cr);

--- NEW FILE: png_io.c ---
/* png input and output functions, originally coded by Carl Worth, based
 * on the sample code in libpng */

#include <stdio.h>
#include <png.h>
#include <stdlib.h>

#include "png_io.h"

static void
unpremultiply_data (png_structp   png,
                    png_row_infop row_info,
                    png_bytep     data)
{
    int i;

    for (i = 0; i < row_info->rowbytes; i += 4) {
        unsigned char *b = &data[i];
        unsigned char alpha = b[3];
        unsigned long pixel;
        unsigned long *p;
        if (alpha == 0)
            pixel = 0;
        else
            pixel = ((((b[0] * 255) / alpha) << 0) |
                     (((b[1] * 255) / alpha) << 8) |
                     (((b[2] * 255) / alpha) << 16) |
                     (alpha << 24));
        p = (unsigned long *) b;
        *p = pixel;
    }
}

static void
premultiply_data (png_structp   png,
                  png_row_infop row_info,
                  png_bytep     data)
{
    int i;

    for (i = 0; i < row_info->rowbytes; i += 4) {
	unsigned char  *base = &data[i];
	unsigned char  blue = base[0];
	unsigned char  green = base[1];
	unsigned char  red = base[2];
	unsigned char  alpha = base[3];
	unsigned long	p;

	red = (unsigned) red * (unsigned) alpha / 255;
	green = (unsigned) green * (unsigned) alpha / 255;
	blue = (unsigned) blue * (unsigned) alpha / 255;
	p = (alpha << 24) | (red << 16) | (green << 8) | (blue << 0);
	memcpy (base, &p, sizeof (unsigned long));
    }
}

void
write_png_argb32 (char       *buffer,
                  const char *filename,
		          int         width,
                  int         height,
                  int         stride)
{
    FILE* f;
    png_struct* png;
    png_info* info;
    png_byte** rows;
    int       i;
    png_color_16 white;

    f = fopen (filename, "w");
    rows = malloc (height*sizeof (png_byte*));

    for (i=0;i<height;i++) {
	rows[i]=buffer+i*stride;
    }

    png = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
    info = png_create_info_struct (png);

    png_init_io (png, f);
    png_set_IHDR (png, info,
		  width, height, 8,
		  PNG_COLOR_TYPE_RGB_ALPHA, 
		  PNG_INTERLACE_NONE,
		  PNG_COMPRESSION_TYPE_DEFAULT,
		  PNG_FILTER_TYPE_DEFAULT);

    white.red=0xff;
    white.blue=0xff;
    white.green=0xff;
    png_set_bKGD (png, info, &white);

    png_set_write_user_transform_fn (png, unpremultiply_data);
    png_set_bgr (png);

    png_write_info (png, info);
    png_write_image (png, rows);
    png_write_end (png, info);

    png_destroy_write_struct (&png, &info);

    free (rows);
    fclose (f);
}

char *
read_png_argb32 (const char *filename,
		         int        *widthp,
                 int        *heightp,
                 int        *stridep)
{
    FILE    *f;
    char    *buffer;
    png_structp png;
    png_infop info;
    png_bytepp rows;
    int i;
    png_uint_32 width, height;
    png_uint_32	stride;
    int depth, color, interlace;
    
    png = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
    if (png == NULL)
	return NULL;
    info = png_create_info_struct (png);
    if (info == NULL)
    {
	png_destroy_read_struct (&png, NULL, NULL);
	return NULL;
    }
    if (setjmp (png->jmpbuf))
    {
	png_destroy_read_struct (&png, &info, NULL);
	return NULL;
    }
    f = fopen (filename, "rb");
    if (f == NULL)
    {
	png_destroy_read_struct (&png, &info, NULL);
	return NULL;
    }
    png_init_io (png, f);
    png_read_info (png, info);
    png_get_IHDR (png, info, &width, &height, &depth, &color, &interlace,
		  NULL, NULL);

    if (color == PNG_COLOR_TYPE_PALETTE && depth <= 8)
	png_set_expand (png);

    if (color == PNG_COLOR_TYPE_GRAY && depth < 8)
	png_set_expand (png);

    if (png_get_valid (png, info, PNG_INFO_tRNS))
	png_set_expand (png);

    if (depth == 16)
	png_set_strip_16 (png);

    if (depth < 8)
	png_set_packing (png);

    if (color == PNG_COLOR_TYPE_GRAY || color == PNG_COLOR_TYPE_GRAY_ALPHA)
	png_set_gray_to_rgb (png);

    if (interlace != PNG_INTERLACE_NONE)
	png_set_interlace_handling (png);

    png_set_bgr (png);
    png_set_filler (png, 255, PNG_FILLER_AFTER);

    png_set_read_user_transform_fn (png, premultiply_data);

    png_read_update_info (png, info);

    stride = width * 4;
    buffer = malloc (stride * height);
    
    rows = malloc (sizeof (png_bytep) * height);

    for (i = 0; i < height; i++)
	rows[i] = (png_bytep) (buffer + i * stride);
    
    png_read_image (png, rows);
    png_read_end (png, info);

    free (rows);
    fclose (f);
    png_destroy_read_struct (&png, &info, NULL);

    *widthp = (int) width;
    *heightp = (int) height;
    *stridep = (int) stride;
    
    return buffer;
}

		    

--- NEW FILE: png_io.h ---
#ifndef PNG_IO_H
#define PNG_IO_H

void
write_png_argb32 (char       *buffer,
                  const char *filename,
		          int         width,
                  int         height,
                  int         stride);

char *
read_png_argb32 (const char *filename,
		         int        *width,
                 int        *height,
                 int        *stride);

#endif

--- NEW FILE: prepare_snippets.c ---
(This appears to be a binary file; contents omitted.)

--- NEW FILE: snippets.h ---
(This appears to be a binary file; contents omitted.)

--- NEW FILE: text.cairo ---
cairo_select_font (cr, "Sans", CAIRO_FONT_SLANT_NORMAL,
                               CAIRO_FONT_WEIGHT_BOLD);
cairo_scale_font (cr, 0.35);

cairo_move_to (cr, 0.04, 0.53);
cairo_show_text (cr, "Hello");

cairo_move_to (cr, 0.27, 0.65);
cairo_text_path (cr, "void");
cairo_save (cr);
    cairo_set_rgb_color (cr, 0.5,0.5,1);
    cairo_fill (cr);
cairo_restore (cr);
cairo_set_line_width (cr, 0.01);
cairo_stroke (cr);

--- NEW FILE: text_centering.cairo ---
const char *string = "cairo";
cairo_text_extents_t extents;
double x,y;

cairo_select_font (cr, "Sans",
    CAIRO_FONT_SLANT_NORMAL,
    CAIRO_FONT_WEIGHT_NORMAL);
cairo_scale_font (cr, 0.4);
cairo_text_extents (cr, string, &extents);
x = 0.5-(extents.width/2 +extents.x_bearing);
y = 0.5-(extents.height/2+extents.y_bearing);
cairo_move_to (cr, x, y);
cairo_show_text (cr, string);

--- NEW FILE: xxx_clipping.cairo ---
cairo_arc (cr, 0.5, 0.5, 0.3, 0, 2 * M_PI);
cairo_clip (cr);
cairo_new_path (cr);
cairo_rectangle (cr, 0, 0, 1, 1);
cairo_fill (cr);
cairo_set_rgb_color (cr, 0, 1, 0);
cairo_move_to (cr, 0, 0);
cairo_line_to (cr, 1, 1);
cairo_move_to (cr, 1, 0);
cairo_line_to (cr, 0, 1);
cairo_stroke (cr);

--- NEW FILE: xxx_dash.cairo ---
/* BUG: spline is not stroked, is it a bug that
   a negative offset for cairo_set_dash, causes
   no dashing to occur?
*/

double dashes[4]={0.20,  /* ink */
                  0.05,  /* skip */
                  0.05,  /* ink */
                  0.05   /* skip*/ 
                 };

cairo_move_to (cr, 0.5, 0.1);
cairo_line_to (cr, 0.9, 0.9);
cairo_rel_line_to (cr, -0.4, 0.0);
cairo_curve_to (cr, 0.2, 0.9, 0.2, 0.5, 0.5, 0.5);

cairo_set_dash (cr, dashes, 4, -0.2);

cairo_stroke (cr);

--- NEW FILE: xxx_image_clipping.cairo ---
int              w,h,stride;
char            *buffer;
cairo_surface_t *image;

buffer = read_png_argb32 (
              "data/romedalen.png", &w,&h, &stride);
image  = cairo_surface_create_for_image (
              buffer, CAIRO_FORMAT_ARGB32, w,h, stride);

cairo_arc (cr, 0.5, 0.5, 0.3, 0, 2*M_PI);
cairo_clip (cr);
cairo_new_path (cr);

cairo_scale (cr, 1.0/w, 1.0/h);

cairo_move_to (cr, 0, 0);
cairo_show_surface (cr, image, w, h);
cairo_rectangle (cr, 0,0,w,h);
cairo_fill (cr);

cairo_surface_destroy (image);
free (buffer);

--- NEW FILE: xxx_long_lines.cairo ---
cairo_move_to (cr, 0.1, -50);
cairo_line_to (cr, 0.1,  50);
cairo_set_rgb_color (cr, 1, 0 ,0);
cairo_stroke (cr);

cairo_move_to (cr, 0.2, -60);
cairo_line_to (cr, 0.2,  60);
cairo_set_rgb_color (cr, 1, 1 ,0);
cairo_stroke (cr);

cairo_move_to (cr, 0.3, -70);
cairo_line_to (cr, 0.3,  70);
cairo_set_rgb_color (cr, 0, 1 ,0);
cairo_stroke (cr);

cairo_move_to (cr, 0.4, -80);
cairo_line_to (cr, 0.4,  80);
cairo_set_rgb_color (cr, 0, 0 ,1);
cairo_stroke (cr);

--- NEW FILE: xxx_multi_segment_caps.cairo ---
/* BUG: Caps are added only to the last subpath in a complex path */

cairo_move_to (cr, 0.2, 0.3);
cairo_line_to (cr, 0.8, 0.3);

cairo_move_to (cr, 0.2, 0.5);
cairo_line_to (cr, 0.8, 0.5);

cairo_move_to (cr, 0.2, 0.7);
cairo_line_to (cr, 0.8, 0.7);

cairo_set_line_width (cr, 0.12);
cairo_set_line_cap  (cr, CAIRO_LINE_CAP_ROUND);
cairo_stroke (cr);

--- NEW FILE: xxx_self_intersect.cairo ---
/* is the minimally different shade of the right part of the bar
   the self intersect bug described in BUGS? */

cairo_move_to (cr, 0.3, 0.3);
cairo_line_to (cr, 0.7, 0.3);

cairo_line_to (cr, 0.5, 0.3);
cairo_line_to (cr, 0.5, 0.7);

cairo_set_line_width (cr, 0.22);
cairo_set_line_cap  (cr, CAIRO_LINE_CAP_ROUND);
cairo_set_line_join  (cr, CAIRO_LINE_JOIN_ROUND);
cairo_stroke (cr);





More information about the cairo-commit mailing list