Re: outputting pdf with Cairo 0.3



On Fri, 10 Feb 2006 12:26:48 -0500
zentara <zentara zentara net> wrote:

Hi,

Does anyone have an example of outputting pdf
from Cairo? It says in the docs it supports pdf
output, but I can only find png output.

Thanks

I figured it out.
Well having to figure these things out, is good for me. ;-)

First you need the latest Cairo built with --enable-pdf

Then here is a modified version of the Cairo-0.3 example
script, that outputs pdf instead of png.

#!/usr/bin/perl
use strict;
use warnings;
use Cairo;

use constant 
{
        IMG_WIDTH => 640,
        IMG_HEIGHT => 480,
        M_PI => 3.14159265,
};

die "pdf backend not supported" unless (Cairo::HAS_PDF_SURFACE);

my $pdf_out = "$0.pdf";

my $surf = Cairo::PdfSurface->create ($pdf_out, IMG_WIDTH, IMG_HEIGHT);

my $cr = Cairo::Context->create ($surf);

$cr->rectangle (0, 0, IMG_WIDTH, IMG_HEIGHT);
$cr->set_source_rgba (1, 1, 1, 0.5);
$cr->fill;

# black
$cr->save;
$cr->set_source_rgba (0, 0, 0, 0.5);
$cr->translate (IMG_WIDTH / 2, IMG_HEIGHT - (IMG_HEIGHT / 4));
do_star ();
$cr->restore;

# red
$cr->save;
$cr->set_source_rgba (1, 0, 0, 0.5);
$cr->translate (IMG_WIDTH / 2, IMG_HEIGHT / 4);
do_star ();
$cr->restore;

# green
$cr->save;
$cr->set_source_rgba (0, 1, 0, 0.5);
$cr->translate (IMG_WIDTH / 4, IMG_HEIGHT / 2);
do_star ();
$cr->restore;

# blue
$cr->save;
$cr->set_source_rgba (0, 0, 1, 0.5);
$cr->translate (IMG_WIDTH - (IMG_WIDTH / 4), IMG_HEIGHT / 2);
do_star ();
$cr->restore;

$cr->show_page;
###############################################################
sub do_star
{
        my $mx = IMG_WIDTH / 3.0;
        my $count = 100;
        foreach (0..$count-1)
        {
                $cr->new_path;
                $cr->move_to (0, 0);
                $cr->rel_line_to (-$mx, 0);
                $cr->stroke;
                $cr->rotate ((M_PI * 2) / $count);
        }
}
__END__




-- 
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]