Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
finish off examples
- Loading branch information
Showing
11 changed files
with
273 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,3 +16,4 @@ Synopsis | |
} | ||
|
||
|
||
Please see the `examples` folder. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters