| 1 | package PDF::Make::Builder::Shape::Pie; | ||||||||||
| 2 | 53 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 53 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 53 | ▶ | use Object::Proto; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 53 | ▶ | use POSIX qw(floor); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 53 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 53 | Object::Proto::define('PDF::Make::Builder::Shape::Pie', | |||||||||
| 9 | 'fill_colour:Str:default(#000)', | ||||||||||
| 10 | 'x:Num:required', | ||||||||||
| 11 | 'y:Num:required', | ||||||||||
| 12 | 'r:Num:required', | ||||||||||
| 13 | 'rx:Num:required', | ||||||||||
| 14 | 'ry:Num:required', | ||||||||||
| 15 | ); | ||||||||||
| 16 | 53 | Object::Proto::import_accessors('PDF::Make::Builder::Shape::Pie'); | |||||||||
| 17 | } | ||||||||||
| 18 | |||||||||||
| 19 | 53 | ▶ | use constant PI => 3.14159265358979323846; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | |||||||||||
| 21 | 1 | ▶ | sub add { | ||||||||
|
Subroutine
add: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 22 | 1 | my ($self, $builder) = @_; | |||||||||
| 23 | 1 | my $page = $builder->page; | |||||||||
| 24 | 1 | my $canvas = $page->canvas; | |||||||||
| 25 | 1 | my $font = $builder->font; | |||||||||
| 26 | |||||||||||
| 27 | 1 | my $cx = $self->x; | |||||||||
| 28 | 1 | my $cy = $self->y; | |||||||||
| 29 | 1 | my $radius = $self->r; | |||||||||
| 30 | 1 | my $start_deg = $self->rx; | |||||||||
| 31 | 1 | my $end_deg = $self->ry; | |||||||||
| 32 | 1 | my ($cr, $cg, $cb) = $font->hex_to_rgb(fill_colour $self); | |||||||||
| 33 | |||||||||||
| 34 | 1 | my $start = $start_deg * PI / 180; | |||||||||
| 35 | 1 | my $end = $end_deg * PI / 180; | |||||||||
| 36 | |||||||||||
| 37 | 1 | $canvas->q->rg($cr, $cg, $cb); | |||||||||
| 38 | |||||||||||
| 39 | # Move to center, line to arc start | ||||||||||
| 40 | 1 | $canvas->m($cx, $cy); | |||||||||
| 41 | 1 | $canvas->l($cx + $radius * cos($start), $cy + $radius * sin($start)); | |||||||||
| 42 | |||||||||||
| 43 | # Approximate arc with Bezier segments (max 90 degrees each) | ||||||||||
| 44 | 1 | my $angle = $end - $start; | |||||||||
| 45 | 1 | ▶ | $angle += 2 * PI if $angle < 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | 1 | my $segments = int($angle / (PI / 2)) + 1; | |||||||||
| 47 | 1 | my $seg_angle = $angle / $segments; | |||||||||
| 48 | |||||||||||
| 49 | 1 | my $a = $start; | |||||||||
| 50 | 1 | for (1 .. $segments) { | |||||||||
| 51 | 2 | my $a2 = $a + $seg_angle; | |||||||||
| 52 | 2 | my $alpha = 4.0 / 3.0 * (1 - cos($seg_angle / 2)) / sin($seg_angle / 2); | |||||||||
| 53 | |||||||||||
| 54 | 2 | my $x1 = $cx + $radius * cos($a); | |||||||||
| 55 | 2 | my $y1 = $cy + $radius * sin($a); | |||||||||
| 56 | 2 | my $x4 = $cx + $radius * cos($a2); | |||||||||
| 57 | 2 | my $y4 = $cy + $radius * sin($a2); | |||||||||
| 58 | |||||||||||
| 59 | 2 | my $cpx1 = $x1 - $alpha * $radius * sin($a); | |||||||||
| 60 | 2 | my $cpy1 = $y1 + $alpha * $radius * cos($a); | |||||||||
| 61 | 2 | my $cpx2 = $x4 + $alpha * $radius * sin($a2); | |||||||||
| 62 | 2 | my $cpy2 = $y4 - $alpha * $radius * cos($a2); | |||||||||
| 63 | |||||||||||
| 64 | 2 | $canvas->c($cpx1, $cpy1, $cpx2, $cpy2, $x4, $y4); | |||||||||
| 65 | 2 | $a = $a2; | |||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | 1 | $canvas->h->f->Q; | |||||||||
| 69 | |||||||||||
| 70 | 1 | return $self; | |||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | 53 | 1; | |||||||||
| 74 | |||||||||||
| 75 | __END__ | ||||||||||