Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
finish off examples
  • Loading branch information
dwarring committed May 31, 2017
1 parent 3e4f120 commit 675af7e
Show file tree
Hide file tree
Showing 11 changed files with 273 additions and 32 deletions.
1 change: 1 addition & 0 deletions Readme.md
Expand Up @@ -16,3 +16,4 @@ Synopsis
}


Please see the `examples` folder.
4 changes: 3 additions & 1 deletion examples/README.md
@@ -1,2 +1,4 @@
Examples mostly taken from https://cairographics.org/samples
`multi-page-pdf.pl` demonstrates the creation of a two page PDF.

For other examples, please see https://cairographics.org/samples
for expected output and equivalent C programs.
26 changes: 26 additions & 0 deletions examples/multi-page-pdf.pl
@@ -0,0 +1,26 @@
use v6;
use Cairo;

given Cairo::Surface::PDF.create("multi-page-pdf.pdf", 256, 256) {
for 1..2 -> $page {
given Cairo::Context.new($_) {

.select_font_face("courier", Cairo::FontSlant::FONT_SLANT_ITALIC, Cairo::FontWeight::FONT_WEIGHT_BOLD);
.set_font_size(10);
.save;
do {
.rgb(0, 0.7, 0.9);
.rectangle(10, 10, 50, 50);
.fill :preserve; .rgb(1, 1, 1);
.stroke;
};
.restore;
.move_to(10, 10);
.show_text("Page $page/2");
}
.show_page;

};
.finish;
}

23 changes: 23 additions & 0 deletions examples/multi-segment-caps.pl
@@ -0,0 +1,23 @@
use v6;
use Cairo;

given Cairo::Image.create(Cairo::FORMAT_ARGB32, 256, 256) {
given Cairo::Context.new($_) {

.move_to(50.0, 75.0);
.line_to(200.0, 75.0);

.move_to(50.0, 125.0);
.line_to(200.0, 125.0);

.move_to(50.0, 175.0);
.line_to(200.0, 175.0);

.line_width = 30.0;
.line_cap = Cairo::LineCap::LINE_CAP_ROUND;
.stroke;

};
.write_png("multi-segment-caps.png");
}

32 changes: 32 additions & 0 deletions examples/rounded-rectangle.pl
@@ -0,0 +1,32 @@
use v6;
use Cairo;

given Cairo::Image.create(Cairo::FORMAT_ARGB32, 256, 256) {
given Cairo::Context.new($_) {

constant x = 25.6; # parameters like cairo_rectangle
constant y = 25.6;
constant width = 204.8;
constant height = 204.8;
constant aspect = 1.0; # aspect ratio
constant corner_radius = height / 10.0; # and corner curvature radius
constant radius = corner_radius / aspect;
constant degrees = pi / 180.0;

.sub_path;
.arc(x + width - radius, y + radius, radius, -90 * degrees, 0 * degrees);
.arc(x + width - radius, y + height - radius, radius, 0 * degrees, 90 * degrees);
.arc(x + radius, y + height - radius, radius, 90 * degrees, 180 * degrees);
.arc(x + radius, y + radius, radius, 180 * degrees, 270 * degrees);
.close_path;

.rgb(0.5, 0.5, 1);
.fill(:preserve);
.rgba(0.5, 0, 0, 0.5);
.line_width = 10;
.stroke;

};
.write_png("rounded-rectangle.png");
}

29 changes: 29 additions & 0 deletions examples/set-line-cap.pl
@@ -0,0 +1,29 @@
use v6;
use Cairo;

given Cairo::Image.create(Cairo::FORMAT_ARGB32, 256, 256) {
given Cairo::Context.new($_) {

.line_width = 30.0;
.line_cap = Cairo::LineCap::LINE_CAP_BUTT; # default
.move_to(64.0, 50.0); .line_to(64.0, 200.0);
.stroke;
.line_cap = Cairo::LineCap::LINE_CAP_ROUND;
.move_to(128.0, 50.0); .line_to(128.0, 200.0);
.stroke;
.line_cap = Cairo::LineCap::LINE_CAP_SQUARE;
.move_to(192.0, 50.0); .line_to(192.0, 200.0);
.stroke;

# draw helping lines */
.rgb(1, 0.2, 0.2);
.line_width = 2.56;
.move_to(64.0, 50.0); .line_to(64.0, 200.0);
.move_to(128.0, 50.0); .line_to(128.0, 200.0);
.move_to(192.0, 50.0); .line_to(192.0, 200.0);
.stroke;

};
.write_png("set-line-cap.png");
}

29 changes: 29 additions & 0 deletions examples/set-line-join.pl
@@ -0,0 +1,29 @@
use v6;
use Cairo;

given Cairo::Image.create(Cairo::FORMAT_ARGB32, 256, 256) {
given Cairo::Context.new($_) {

.line_width = 40.96;
.move_to(76.8, 84.48);
.line_to(51.2, -51.2, :relative);
.line_to(51.2, 51.2, :relative);
.line_join = Cairo::LineJoin::LINE_JOIN_MITER; # default
.stroke;

.move_to(76.8, 161.28);
.line_to(51.2, -51.2, :relative);
.line_to(51.2, 51.2, :relative);
.line_join = Cairo::LineJoin::LINE_JOIN_BEVEL;
.stroke;

.move_to(76.8, 238.08);
.line_to(51.2, -51.2, :relative);
.line_to(51.2, 51.2, :relative);
.line_join = Cairo::LineJoin::LINE_JOIN_ROUND;
.stroke;

};
.write_png("set-line-join.png");
}

33 changes: 33 additions & 0 deletions examples/text-align-center.pl
@@ -0,0 +1,33 @@
use v6;
use Cairo;

given Cairo::Image.create(Cairo::FORMAT_ARGB32, 256, 256) {
given Cairo::Context.new($_) {

.select_font_face("Sans",
Cairo::FontSlant::FONT_SLANT_NORMAL,
Cairo::FontWeight::FONT_WEIGHT_BOLD);
.set_font_size(52.0);

my \text = "cairo";
my Cairo::cairo_text_extents_t \extents = .text_extents(text);
my \x = 128.0-(extents.width/2 + extents.x_bearing);
my \y = 128.0-(extents.height/2 + extents.y_bearing);

.move_to(x, y);
.show_text(text);

# draw helping lines
.rgba(1, 0.2, 0.2, 0.6);
.line_width = 6.0;
.arc(x, y, 10.0, 0, 2*pi);
.fill;
.move_to(128.0, 0);
.line_to(0, 256, :relative);
.move_to(0, 128.0);
.line_to(256, 0, :relative);
.stroke;
};
.write_png("text-align-center.png");
}

33 changes: 33 additions & 0 deletions examples/text-extents.pl
@@ -0,0 +1,33 @@
use v6;
use Cairo;

given Cairo::Image.create(Cairo::FORMAT_ARGB32, 256, 256) {
given Cairo::Context.new($_) {

.select_font_face("Sans",
Cairo::FontSlant::FONT_SLANT_NORMAL,
Cairo::FontWeight::FONT_WEIGHT_NORMAL);
.set_font_size(100.0);

my \text = "cairo";
my Cairo::cairo_text_extents_t \extents = .text_extents(text);
constant x=25.0;
constant y=150.0;

.move_to(x,y);
.show_text(text);

# draw helping lines
.rgba(1, 0.2, 0.2, 0.6);
.line_width = 6.0;
.arc(x, y, 10.0, 0, 2*pi);
.fill;
.move_to(x,y);
.line_to(0, -extents.height, :relative);
.line_to(extents.width, 0, :relative);
.line_to(extents.x_bearing, -extents.y_bearing, :relative);
.stroke;
};
.write_png("text-extents.png");
}

33 changes: 33 additions & 0 deletions examples/text.pl
@@ -0,0 +1,33 @@
use v6;
use Cairo;

given Cairo::Image.create(Cairo::FORMAT_ARGB32, 256, 256) {
given Cairo::Context.new($_) {

.select_font_face("Sans",
Cairo::FontSlant::FONT_SLANT_NORMAL,
Cairo::FontWeight::FONT_WEIGHT_BOLD);
.set_font_size(90.0);

.move_to(10.0, 135.0);
.show_text("Hello");

.move_to(70.0, 165.0);
.text_path("void");
.rgb(0.5, 0.5, 1);
.fill(:preserve);
.rgb(0, 0, 0);
.line_width = 2.56;
.stroke;

# draw helping lines
.rgba(1, 0.2, 0.2, 0.6);
.arc(10.0, 135.0, 5.12, 0, 2*pi);
.close_path;
.arc(70.0, 165.0, 5.12, 0, 2*pi);
.fill;

};
.write_png("text.png");
}

62 changes: 31 additions & 31 deletions lib/Cairo.pm6
Expand Up @@ -17,6 +17,27 @@ our class cairo_surface_t is repr('CPointer') { }

our class cairo_pattern_t is repr('CPointer') { }

our class cairo_rectangle_t is repr('CPointer') { }

our class cairo_path_t is repr('CPointer') { }

our class cairo_text_extents_t is repr('CStruct') {
has num64 $.x_bearing;
has num64 $.y_bearing;
has num64 $.width;
has num64 $.height;
has num64 $.x_advance;
has num64 $.y_advance;
}

our class cairo_font_extents_t is repr('CStruct') {
has num64 $.ascent;
has num64 $.descent;
has num64 $.height;
has num64 $.max_x_advance;
has num64 $.max_y_advance;
}

our class cairo_matrix_t is repr('CStruct') {
has num64 $.xx; has num64 $.yx;
has num64 $.xy; has num64 $.yy;
Expand All @@ -30,37 +51,16 @@ our class cairo_matrix_t is repr('CStruct') {
is native($cairolib)
{*}

multi method init(:$identity!) {
multi method init(:$identity! where .so) {
cairo_matrix_init_identity(self);
return self;
}

multi method init(Num(Cool) $sx, Num(Cool) $sy, :$scale!) {
multi method init(Num(Cool) $sx, Num(Cool) $sy, :$scale where .so) {
cairo_matrix_init_scale(self, $sx, $sy);
return self;
}

};

our class cairo_rectangle_t is repr('CPointer') { }

our class cairo_path_t is repr('CPointer') { }

our class cairo_text_extents_t is repr('CStruct') {
has num64 $.x-bearing;
has num64 $.y-bearing;
has num64 $.width;
has num64 $.height;
has num64 $.x-advance;
has num64 $.y-advance;
}

our class cairo_font_extents_t is repr('CStruct') {
has num64 $.ascent;
has num64 $.descent;
has num64 $.height;
has num64 $.max-x-advance;
has num64 $.max-y-advance;
}

our class Surface { ... }
Expand Down Expand Up @@ -841,7 +841,7 @@ class Context {
multi method copy_path() {
cairo_copy_path($!context);
}
multi method copy_path(:$flat!) {
multi method copy_path(:$flat! where .so) {
cairo_copy_path_flat($!context);
}
method append_path($path) {
Expand Down Expand Up @@ -901,16 +901,16 @@ class Context {
multi method stroke {
cairo_stroke($!context)
}
multi method fill(:$preserve!) {
multi method fill(:$preserve! where .so) {
cairo_fill_preserve($!context);
}
multi method stroke(:$preserve!) {
multi method stroke(:$preserve! where .so) {
cairo_stroke_preserve($!context);
}
multi method clip {
cairo_clip($!context);
}
multi method clip(:$preserve!) {
multi method clip(:$preserve! where .so) {
cairo_clip_preserve($!context);
}

Expand All @@ -932,21 +932,21 @@ class Context {
cairo_line_to($!context, $x.Num, $y.Num);
}

multi method move_to(Cool $x, Cool $y, :$relative!) {
multi method move_to(Cool $x, Cool $y, :$relative! where .so) {
cairo_rel_move_to($!context, $x.Num, $y.Num);
}
multi method line_to(Cool $x, Cool $y, :$relative!) {
multi method line_to(Cool $x, Cool $y, :$relative! where .so) {
cairo_rel_line_to($!context, $x.Num, $y.Num);
}

multi method curve_to(Cool $x1, Cool $y1, Cool $x2, Cool $y2, Cool $x3, Cool $y3) {
cairo_curve_to($!context, $x1.Num, $y1.Num, $x2.Num, $y2.Num, $x3.Num, $y3.Num);
}

multi method arc(Cool $xc, Cool $yc, Cool $radius, Cool $angle1, Cool $angle2, :$negative!) {
multi method arc(Cool $xc, Cool $yc, Cool $radius, Cool $angle1, Cool $angle2, :$negative! where .so) {
cairo_arc_negative($!context, $xc.Num, $yc.Num, $radius.Num, $angle1.Num, $angle2.Num);
}
multi method arc(num $xc, num $yc, num $radius, num $angle1, num $angle2, :$negative!) {
multi method arc(num $xc, num $yc, num $radius, num $angle1, num $angle2, :$negative! where .so) {
cairo_arc_negative($!context, $xc, $yc, $radius, $angle1, $angle2);
}

Expand Down

0 comments on commit 675af7e

Please sign in to comment.