Calendar-Plugin-Renderer

 view release on metacpan or  search on metacpan

lib/Calendar/Plugin/Renderer/SVG.pm  view on Meta::CPAN


sub process {
    my ($self) = @_;

    my $start_index    = $self->start_index;
    my $max_month_days = $self->days;
    my $days_box       = $self->days_box;

    my $row = 1;
    my $i   = 0;
    while ($i < $start_index) {
        $days_box->[$row][$i]->text->value(' ');
        $i++;
    }

    my $d = 1;
    while ($i <= 6) {
        $days_box->[$row][$i]->text->value($d);
        $i++;
        $d++;
    }

    $row++;
    my $k = 0;
    while ($d <= $max_month_days) {
        $days_box->[$row][$k]->text->value($d);
        if ($k == 6) {
            $row++;
            $k = 0;
        }
        else {
            $k++;
        }
        $d++;
    }

    $self->_row($row);
}

sub as_string {
    my ($self) = @_;

    my $p_height = sprintf("%d%s", $self->page->height, $self->page->height_unit);
    my $p_width  = sprintf("%d%s", $self->page->width,  $self->page->width_unit);
    my $view_box = sprintf("0 0 %d %d", $self->page->width, $self->page->height);

    my $svg = SVG->new(
        height   => $p_height,
        width    => $p_width,
        viewBox  => $view_box,
        -svg_version => '1.1',
        -pubid       => '-//W3C//DTD SVG 1.1//EN',
        -sysid       => 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd');
    my $calendar = $svg->group(id => 'calendar', label => "Calendar");

    my $month_label = $self->month_label;
    $calendar->text('id'    => "month",
                    'fill'  => 'blue',
                    'x'     => $month_label->x,
                    'y'     => $month_label->y,
                    'style' => $month_label->style)->cdata($month_label->text);

    my $year_label = $self->year_label;
    $calendar->text('id'    => "year",
                    'fill'  => 'blue',
                    'x'     => $year_label->x,
                    'y'     => $year_label->y,
                    'style' => $year_label->style)->cdata($year_label->text);

    my $boundary_box = $self->boundary_box;
    $calendar->rect('id'     => 'bounding_box',
                    'height' => $boundary_box->height - $self->adjust_height,
                    'width'  => $boundary_box->width - 14,
                    'x'      => $boundary_box->x + 7 + 7,
                    'y'      => $boundary_box->y,
                    'style'  => 'fill:none; stroke: blue; stroke-width: 0.5;');

    my $wdays = $self->wdays;
    foreach (0..6) {
        my $day = $calendar->tag('g',
                                 'id'           => "row0_col$_",
                                 'text-anchor'  => 'middle',
                                 'fill'         => 'none',
                                 'stroke'       => 'blue',
                                 'stroke-width' => '0.5');
        next unless defined $wdays->[$_];

        $day->rect('id'     => "box_row0_col$_",
                   'x'      => $wdays->[$_]->x,
                   'y'      => $wdays->[$_]->y,
                   'height' => $wdays->[$_]->height,
                   'width'  => $wdays->[$_]->width);
        $day->text('id'          => "text_row0_col$_",
                   'x'           => $wdays->[$_]->text->x,
                   'y'           => $wdays->[$_]->text->y,
                   'length'      => $wdays->[$_]->text->length,
                   'adjust'      => $wdays->[$_]->text->adjust,
                   'font-size'   => $wdays->[$_]->text->font_size,
                   'text-anchor' => 'middle',
                   'stroke'      => 'red')
            ->cdata($wdays->[$_]->text->value);
    }

    my $row      = $self->_row;
    my $days_box = $self->days_box;
    foreach my $r (1..$row) {
        foreach my $c (0..6) {
            my $g_id = sprintf("row%d_col%d"     , $r, $c);
            my $r_id = sprintf("box_row%d_col%d" , $r, $c);
            my $t_id = sprintf("text_row%d_col%d", $r, $c);
            next unless defined $days_box->[$r]->[$c];

            my $d = $calendar->tag('g',
                                   'id'           => "$g_id",
                                   'fill'         => 'none',
                                   'stroke'       => 'blue',
                                   'stroke-width' => '0.5');
            $d->rect('id'     => "$r_id",
                     'x'      => $days_box->[$r]->[$c]->x,
                     'y'      => $days_box->[$r]->[$c]->y,
                     'height' => $days_box->[$r]->[$c]->height,
                     'width'  => $days_box->[$r]->[$c]->width,
                     'fill'         => 'none',
                     'stroke'       => 'blue',
                     'stroke-width' => '0.5');

            my $text = ' ';
            if (defined $days_box->[$r]->[$c]->text
                && defined $days_box->[$r]->[$c]->text->value) {
                $text = $days_box->[$r]->[$c]->text->value;
            }

            $d->text('id'     => "$t_id",
                     'x'      => $days_box->[$r]->[$c]->text->x + 1,
                     'y'      => $days_box->[$r]->[$c]->text->y + 5,
                     'length' => $days_box->[$r]->[$c]->text->length,
                     'adjust' => 'spacing',
                     'font-size'    => $days_box->[$r]->[$c]->text->font_size,
                     'stroke'       => 'green',
                     'text-anchor'  => 'right',
                     'fill'         => 'silver',
                     'fill-opacity' => '50%')
                ->cdata($text);
        }
    }

    return $svg->to_xml;
}

=head1 AUTHOR

Mohammad S Anwar, C<< <mohammad.anwar at yahoo.com> >>

=head1 REPOSITORY

L<https://github.com/manwar/Calendar-Plugin-Renderer>

=head1 BUGS

Please report any bugs / feature requests to C<bug-calendar-plugin-renderer at rt.cpan.org>,
or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Calendar-Plugin-Renderer>.
I will be notified, and then you'll automatically be notified of progress on your
bug as I make changes.

=head1 SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc Calendar::Plugin::Renderer::SVG

You can also look for information at:

=over 4

=item * RT: CPAN's request tracker

L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Calendar-Plugin-Renderer>

=item * AnnoCPAN: Annotated CPAN documentation

L<http://annocpan.org/dist/Calendar-Plugin-Renderer>

=item * CPAN Ratings

L<http://cpanratings.perl.org/d/Calendar-Plugin-Renderer>

=item * Search CPAN

L<http://search.cpan.org/dist/Calendar-Plugin-Renderer/>

=back

=head1 LICENSE AND COPYRIGHT

Copyright (C) 2015 - 2016 Mohammad S Anwar.

This program  is  free software; you can redistribute it and / or modify it under
the  terms  of the the Artistic License (2.0). You may obtain a  copy of the full
license at:

L<http://www.perlfoundation.org/artistic_license_2_0>

Any  use,  modification, and distribution of the Standard or Modified Versions is



( run in 3.133 seconds using v1.01-cache-2.11-cpan-7fcb06a456a )