package Maasha::KISS::Draw;

# Copyright (C) 2009 Martin A. Hansen.

# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

# http://www.gnu.org/copyleft/gpl.html


# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


# Routines for creating KISS graphics using Cairo and Pango.


# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


use warnings;
use strict;
use Data::Dumper;
use Cairo;
use Pango;

use vars qw( @ISA @EXPORT );

@ISA = qw( Exporter );


# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


sub draw_feature
{
    # Given a list of features add these to
    # a Cairo::Context object.

    my ( $cr,         # Cairo::Context object
         $features,   # List of features
       ) = @_;

    # Returns nothing.

    my ( $feature );

    foreach $feature ( @{ $features } )
    {
        $cr->set_source_rgb( @{ $feature->{ 'color' } } );

        if ( $feature->{ 'type' } eq 'line' )
        {
            $cr->set_line_width( $feature->{ 'line_width' } );
            $cr->move_to( $feature->{ 'x1' }, $feature->{ 'y1' } );
            $cr->line_to( $feature->{ 'x2' }, $feature->{ 'y2' } );
        }
        elsif ( $feature->{ 'type' } eq 'rect' )
        {
            $cr->rectangle(
                $feature->{ 'x1' },
                $feature->{ 'y1' },
                $feature->{ 'x2' } - $feature->{ 'x1' },
                $feature->{ 'y2' } - $feature->{ 'y1' },
            );

            $cr->fill;
        }
        elsif ( $feature->{ 'type' } eq 'text' )
        {
            $cr->move_to( $feature->{ 'x1' }, $feature->{ 'y1' } );
            $cr->set_font_size( $feature->{ 'font_size' } );
            $cr->show_text( $feature->{ 'txt' } );
        }

        $cr->stroke;
    }
}


sub file_png
{
    # Martin A. Hansen, October 2009.

    # Prints a Cairo::Surface object to a PNG file.

    my ( $surface,   # Cairo::Surface object
         $file,      # path to PNG file
       ) = @_;

    # Returns nothing

    $surface->write_to_png( $file );
}


sub hdump
{
    my ( $foo ) = @_;

    my ( @html );

    @html = "Content-Type: text/html; charset=ISO-8859-1\n\n";

    push @html, "<pre>\n";
    push @html, Dumper( $foo );
    push @html, "</pre>\n";

    return wantarray ? @html : \@html;
}


# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

1;


