1 package Maasha::BGB::Draw;
3 # Copyright (C) 2009 Martin A. Hansen.
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 # http://www.gnu.org/copyleft/gpl.html
22 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
25 # Routines for creating Biopieces Browser graphics using Cairo and Pango.
28 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
38 use vars qw( @ISA @EXPORT );
40 @ISA = qw( Exporter );
43 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
48 # Martin A. Hansen, November 2009
50 # Given a list of features add these to
51 # a Cairo::Context object.
53 my ( $cr, # Cairo::Context object
54 $features, # List of features
61 foreach $feature ( @{ $features } )
63 $cr->set_source_rgb( @{ $feature->{ 'color' } } );
65 if ( $feature->{ 'type' } eq 'line' )
67 $cr->set_line_width( $feature->{ 'line_width' } );
68 $cr->move_to( $feature->{ 'x1' }, $feature->{ 'y1' } );
69 $cr->line_to( $feature->{ 'x2' }, $feature->{ 'y2' } );
71 elsif ( $feature->{ 'type' } eq 'rect' )
76 $feature->{ 'x2' } - $feature->{ 'x1' },
77 $feature->{ 'y2' } - $feature->{ 'y1' },
82 elsif ( $feature->{ 'type' } eq 'text' )
84 $cr->move_to( $feature->{ 'x1' }, $feature->{ 'y1' } );
85 $cr->set_font_size( $feature->{ 'font_size' } );
86 $cr->show_text( $feature->{ 'txt' } );
96 # Martin A. Hansen, November 2009.
98 # Given a color number, pick that color from
99 # the color palette and return.
101 my ( $i, # color number
106 my ( $palette, $color );
139 $color = $palette->[ $i ];
141 map { $_ /= 255 } @{ $color };
149 # Martin A. Hansen, October 2009.
151 # Prints a Cairo::Surface object to a PNG file.
153 my ( $surface, # Cairo::Surface object
154 $file, # path to PNG file
159 $surface->write_to_png( $file );
165 # Martin A. Hansen, December 2009.
167 # Extract a PNG stream from a Cairo::Surface object
168 # and convert it to base64 before returning it.
170 my ( $surface, # Cairo::Surface object
175 my ( $png_data, $callback, $base64 );
179 $callback = sub { $png_data .= $_[ 1 ] };
181 $surface->write_to_png_stream( $callback );
183 $base64 = MIME::Base64::encode_base64( $png_data );
189 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<