GD-2.46/ 0000755 0001750 0001750 00000000000 11557316041 011320 5 ustar lstein lstein GD-2.46/Makefile.PL 0000644 0001750 0001750 00000026756 11523332336 013310 0 ustar lstein lstein use ExtUtils::MakeMaker qw(prompt WriteMakefile);
use Config;
use strict;
require 5.6.0;
unless (@ARGV) {
warn < PATHS: CHECK AND ADJUST <=====
my (@INC,@LIBPATH,@LIBS);
my $AUTOCONFIG = 0; # global set by try_to_autoconfigure() below
my ($options,$lib_gd_path,$lib_ft_path,$lib_png_path,$lib_jpeg_path,$lib_xpm_path,$lib_zlib_path,$force);
use Getopt::Long;
GetOptions("ignore_missing_gd" => \$force);
unless (try_to_autoconfigure(\$options,\$lib_gd_path,\@INC,\@LIBPATH,\@LIBS) || $force) {
die < \$options,
"lib_gd_path=s" => \$lib_gd_path,
"lib_ft_path=s" => \$lib_ft_path,
"lib_png_path=s" => \$lib_png_path,
"lib_jpeg_path=s" => \$lib_jpeg_path,
"lib_xpm_path=s" => \$lib_xpm_path,
"lib_zlib_path=s" => \$lib_zlib_path,
);
unless ($result) {
print STDERR <.config.cache";
print F $DEFINES,"\n";
close F;
WriteMakefile(
'NAME' => 'GD',
'VERSION_FROM' => 'GD.pm',
'PREREQ_PM' => {
'Math::Trig' => 0,
},
'PM' => { 'GD.pm' => '$(INST_LIBDIR)/GD.pm',
'GD/Polyline.pm' => '$(INST_LIBDIR)/GD/Polyline.pm',
'GD/Polygon.pm' => '$(INST_LIBDIR)/GD/Polygon.pm',
'GD/Simple.pm' => '$(INST_LIBDIR)/GD/Simple.pm',
'GD/Image.pm' => '$(INST_LIBDIR)/GD/Image.pm',
'GD/Group.pm' => '$(INST_LIBDIR)/GD/Group.pm',
'qd.pl' => '$(INST_LIBDIR)/qd.pl'},
'dist' => {'COMPRESS'=>'gzip -9f', 'SUFFIX' => 'gz',
'ZIP'=>'/usr/bin/zip','ZIPFLAGS'=>'-rl'},
'LIBS' => [join(' ',$ENV{'GD_LIBS'},@LIBPATH,@LIBS)],
'INC' => join(' ',$ENV{'GD_INC'},@INC),
'PL_FILES' => {'bdf_scripts/bdf2gdfont.PLS' => 'bdf_scripts/bdf2gdfont.pl',
'GD/Image.pm.PLS' => 'GD/Image.pm',
},
'CCFLAGS' => '-Wformat=0',
'EXE_FILES' => ['bdf_scripts/bdf2gdfont.pl'],
'AUTHOR' => 'Lincoln Stein (lstein@cshl.org)',
'ABSTRACT' => 'Interface to Gd Graphics Library',
$CAPI ? ('CAPI' => 'TRUE') : (),
'DEFINE' => $DEFINES,
);
exit 0;
sub try_to_autoconfigure {
my ($options,$lib_gd_path,$INC,$LIBPATH,$LIBS) = @_;
my $config = `gdlib-config --all`;
return unless $config;
$AUTOCONFIG++;
my ($version) = $config =~ /^GD library\s+(\S+)/m;
warn "Configuring for libgd version $version.\n";
my ($cflags) = $config =~ /^cflags:\s+(.+)/m;
my ($ldflags) = $config =~ /^ldflags:\s+(.+)/m;
my ($libs) = $config =~ /^libs:\s+(.+)/m;
my ($libdir) = $config =~ /^libdir:\s+(.+)/m;
my ($features) = $config =~ /^features:\s+(.+)/m;
my ($includedir) = $config =~ /^includedir:\s+(.+)/m;
@$INC = map {s/^-I// && "-I$_"} split /\s+/,$cflags;
@$LIBPATH = map {s/^-L// && "-L$_"} split /\s+/,$ldflags;
@$LIBS = split /\s+/,$libs;
push @$LIBS,"-lgd";
push @$LIBPATH,"-L$libdir";
($$lib_gd_path = $libdir) =~ s!/[^/]+$!!;
$$options = $features;
my ($minor) = $version =~ /^2\.\d+\.(\d+)$/;
$$options .= " GD_UNCLOSEDPOLY GD_ANIMGIF GD_FTCIRCLE VERSION_33" if defined($minor) && $minor >= 33;
my @correct_inc = map {s/^-I// && $_} split /\s+/,$cflags;
check_for_stray_headers(@correct_inc);
return 1;
}
sub check_for_stray_headers {
my @official_libraries = @_;
my %official_libraries = map {$_=>1} @official_libraries;
print STDERR "Checking for stray libgd header files...";
my $cc = $Config{'cc'};
my $data = `$cc -print-search-dirs`;
$data ||= '';
my ($libraries) = $data =~ /^libraries: =(.+)/m;
my @cc_libs = split /:/,$libraries;
foreach (@cc_libs) { s/\/$//; }
my %libraries = map {$_=>1} (@cc_libs,'/include','/usr/include',
'/usr/local/include','/opt/include',
'/usr/X11R6/include');
my $problems;
foreach (keys %libraries) {
s/\blib\b/include/;
next if $official_libraries{$_};
next unless -e "$_/gd.h";
warn "\n\n** WARNING: found gd.h header file in ${_}gd.h, but it is expected at $official_libraries[0]/gd.h. This may cause compile errors! **\n";
$problems++;
}
print STDERR $problems ? "** Possible problems found **\n" : "none found.\n";
print STDERR "\n";
}
sub MY::postamble {
my $postamble = <<'END';
html: GD.pm
pod2html --outfile=GD.html GD.pm
END
$postamble;
}
package MY;
sub processPL {
my $inherited = shift->SUPER::processPL(@_);
$inherited =~ s/(:: GD\/Image.pm.PLS)/$1 .config.cache/;
$inherited;
}
GD-2.46/qd.pl 0000644 0001750 0001750 00000100052 07403777174 012274 0 ustar lstein lstein #!/usr/local/bin/perl
# $Id: qd.pl,v 1.1.1.1 2001-12-06 23:25:48 lstein Exp $
# This is a package of routines that let you create Macintosh
# PICT files from within perl. It implements a subset of Quickdraw
# drawing commands, primarily those related to line drawing, rectangles,
# ovals, polygons, and text. Flagrantly absent are: regions and the
# snazzy color transfer modes. Regions are absent because they were
# more trouble than I had time for, and the transfer modes because I
# never use them. (The latter shouldn't be too hard to add.) Also
# missing are the pixmap commands. If you want to do pixmaps, you
# should be using the ppm utilities.
# A QUICK TUTORIAL ON QUICKDRAW
#
# Quickdraw is not Postscript. You cannot write routines in it or get
# (any useful) information out of it. Quickdraw pictures are a series of
# drawing commands, concatenated together in a binary format.
#
# A Macintosh picture consists of a header describing the size of the
# picture and its bounding rectangle, followed by a series of drawing
# commands, followed by a termination code. This perl library is
# modeled closely on the way that you would draw a picture on the Mac.
# First you open the picture with the &qd'OpenPicture() command. This
# initializes some data structures. Then you call a series of drawing
# subroutines, such as &qd'TextFont(), &qd'MoveTo(), &qd'DrawString().
# These routines append their data to the growing (but still private)
# picture. You then close the picture with &qd'ClosePicture. This
# returns a scalar variable containing the binary picture data.
# RECTANGLES
#
# To open a picture you need to define a rectangle that will serve as
# its frame and will define its drawing area. The rectangle is (of
# course) a binary structure. The following utilities allow you to
# create and manipulate rectangles:
#
# &qd'SetRect(*myRect,left,top,right,bottom); # Set the sides of $myRect
# &qd'OffsetRect(*myRect,deltaH,deltaV); # Shift the rectangle as indicated
# &qd'InsetRect(*myRect,deltaH,deltaV); # Shrink rectangle by size indicated
# OPENING A PICTURE
#
# Pass a previously-defined rectangle to the routine OpenPicture. Only one picture
# may be open at a time. The rectangle defines the drawing area in pixels.
# A printer page is 8.5 x 11 inches, at 72 pixels per inch = 612 x 792 pixels.
#
# &qd'OpenPicture($myRect);
#
# You will next very likely want to set the clipping rectangle to the same rectangle
# you used to open the picture with. Clipping rectangles limit quickdraw's drawing
# to the area within the rectangle. Even if you don't use clipping, however, it's a
# good idea to define the rectangle because some drawing programs behave eratically
# when displaying unclipped pictures.
#
# You then issue drawing commands. When you're done you can get the picture data with
# something like $pictData = &qd'ClosePicture;
#
# SETTING THE FOREGROUND AND BACKGROUND COLORS
#
# The foreground color is the color of the ink when a "frame" or "paint" command
# is given. The background color is the color of the erased area when an "erase"
# command is given. The defaults are black and white. The colors can be changed
# in either of two ways:
#
# 1. The "old" 8-color system: black, white, red, green, blue, cyan, magenta, yellow
# Call the routines &qd'FgColor() and &qd'BgColor() with one of the constants
# $qd'REDCOLOR,$qd'GREENCOLOR, etc. This gives you a limited number of highly
# satured colors.
#
# 2. The new 24-bit color system. Call the routines &qd'RGBForeColor() and
# &qd'RGBBackColor(), passing the routines the red, green and blue components
# of the color. These components are two-byte unsigned integers, so you can choose
# any value between 0x000 and 0xFFFF. Higher is darker, so:
# (0x0000,0x0000,0x0000) = BLACK
# (0xFFFFF,0xFFFF,0xFFFF) = WHITE
# (0xFFFFF,0x0000,0x0000) = PURE RED
# etc.
# SETTING THE PATTERN
#
# Like colors, the drawing commands use the current pattern, a 32 row x 32 column
# bit array that defines the pattern of the "ink".
# The default pattern is $qd'BLACK, which is solid black. The only
# other pattern I've defined is $qd'GRAY, which is a 50% checkerboard. You
# might want to define others.
#
# The current pattern is set using &qd'PenPat($myPattern).
# LINE DRAWING
#
# Quickdraw has the concept of the "current point" of the pen. Generally
# you move the pen to a point and then start drawing. The next time you draw,
# the pen will be wherever the last drawing command left it. In addition, the
# pen has a width, a pattern and a color. In the below descriptions,
# h=horizontal, v=vertical
#
# &qd'MoveTo(h,v) # Move to indicated coordinates (0,0 is upper left of picture)
# &qd'LineTo(h,v) # Draw from current position to indicated position
# &qd'Line(dh,dv) # Draw a line dh pixels horizontally, dv pixels vertically,
# starting at current position
# &qd'PenSize(h,v) # Set the size of the pen to h pixels wide, v pixels high
# PEN SCALING
#
# The original quickdraw was incapable of drawing at higher than the screen resolution,
# so even if the PenSize is set to (1,1) the lines will appear chunky when printed out
# on the laserwriter (which has four times the resolution of the screen). Call
# &qd'Scale(1,4) to fix this problem by shrinking the pen down to a quarter of its
# (1,1) size.
#
# &qd'Scale(numerator,denominator) # Scale the pen by the fraction numerator/denominator
# TEXT
#
# &qd'TextFont(fontCode) # Set the current font to indicated code. Currently
# defined fonts are $qd'TIMES, $qd'NEWCENTURYSCHOOLBK,
# $qd'SYMBOL, $qd'HELVETICA, and $qd'COURIER.
#
# &qd'TextSize(size) # Set the current font size (in points). 12 point is typical
#
# &qd'TextFace(attributes) # Set one or more font style attributes. Currently defined
# are $qd'PLAIN, $qd'BOLD, $qd'ITALIC, $qd'UNDERLINE, and
# can be used in combination:
# &qd'TextFace($qd'BOLD + $qd'ITALIC);
#
# &qd'DrawString(string) # Draw the indicated text. It will be drawn from the
# current pen location. Word wrap is NOT supported.
# Rotated text is NOT supported.
#
# &qd'TextWidth(string) # This will return an approximate width for the string
# when it is printed in the current size, font and face.
# Unfortunately, since perl has no access to the Macintosh
# font description tables, the number returned by this
# routine will be wildly inaccurate at best.
# However, if you have X11R5 bdf fonts installed, we look
# in the directory $qd'X11FONTS in order to find a bdf metrics
# font to use. This will give you extremely accurate measurements.
# Please set this variable to whatever is correct for your local
# system. To add more fonts, put them in your bdf font directory
# and update the %qd'font_metric_files array at the bottom of this
# file. It maps a key consisting of the Quickdraw font number,
# font size, and font style (0 for plain, 1 for italic, 2 for bold,
# 3 for both) to the appropriate bdf file.
# RECTANGLES
#
# Draw rectangles using the routines:
# &qd'FrameRect($myRect); # Draw wire-frame rectangle
# &qd'PaintRect($myRect); # Fill rectangle with current foreground
# color and pattern
# &qd'EraseRect($myRect); # Erase the rectangle (fill with bg color)
# &qd'InvertRect($myRect); # Invert black and white in rectangle
# OVALS
#
# Draw ovals using the routines:
# &qd'FrameOval($myRect); # Draw wire-frame oval
# &qd'PaintOval($myRect); # Fill oval with current foreground
# color and pattern
# &qd'EraseOval($myRect); # Erase the oval (fill with bg color)
# &qd'InvertOval($myRect); # Invert black and white in oval
# &qd'FillOval($myRect,$pat); # Fill with specified pattern
#
# ROUND RECTANGLES
#
# Draw round-cornered rectangles with these routines. They each take an oval radius
# to determine the amount of curvature. Values of 10-20 are typical.
# &qd'FrameRoundRect($myRect,$ovalWidth,$ovalHeight); # wire-frame outline
# &qd'PaintRoundRect($myRect,$ovalWidth,$ovalHeight); # fill with current foreground
# &qd'EraseRoundRect($myRect,$ovalWidth,$ovalHeight); # erase
# &qd'InvertRoundRect($myRect,$ovalWidth,$ovalHeight);# invert
# &qd'FillRoundRect($myRect,$ovalWidth,$ovalHeight,$pat); # fill with specified pattern
# ARCS
# Draw an arc subtending the specified rectangle. Angles are in degrees and
# start pointing northward and get larger clockwise:
# e.g. PaintArc($r,45,90) gives you a pie wedge from 2 o'clock to 5 o'clock
# &qd'FrameArc($rect,$startAngle,$arcAngle); # wire-frame the arc
# &qd'PaintArc($rect,$startAngle,$arcAngle); # fill with current foreground
# &qd'EraseArc($rect,$startAngle,$arcAngle); # erase arc
# &qd'InvertArc($rect,$startAngle,$arcAngle); # flip white and black
# &qd'FillArc($rect,,$startAngle,$arcAngle,$pat); # fill with specified pattern
# POLYGONS
# Calling OpenPoly returns the name of a variable in which a growing
# polygon structure will be stored. Once a polygon is opened, all drawing
# commands cease to have an effect on the picture. Instead, all MoveTo,
# LineTo and Line commands accumulate polygon vertices into the data structure.
# Call ClosePoly to stop recording drawing commands. The polygon can now
# be moved, scaled, drawn, filled and erased as many times as wished. Call
# KillPoly to release the memory taken up by the polygon
# $polygon = &qd'OpenPoly; # begin recording drawing commands
# &qd'ClosePoly($polygon); # stop recording drawing commands
# &qd'FramePoly($polygon); # wire-frame the polygon
# &qd'PaintPoly($polygon); # fill with current foreground
# &qd'ErasePoly($polygon); # erase polygon
# &qd'FillPoly($polygon,$pat); # fill polygon with pattern
# &qd'OffsetPoly($polygon,$dh,$dv); # translate poly by dh horizontally, dv vertically
# &qd'MapPoly($polygon,$srcRect,$destRect); # map polygon from coordinate system defined by
# source rectangle to that defined by destination
# rectangle (moving or resizing it as needed)
# PRINTING OUT THE PICTURE IN A FORM THAT THE MACINTOSH CAN READ
#
# The Mac expects its picture files to begin with 512 bytes of "application specific"
# data. By default the picture data that you get will be proceeded by 512 bytes of
# 0's. If you want something else, or if you just want the picture data, set the
# package variable $qd'PICTHEADER to whatever you desire before calling ClosePicture.
# In order for the picture data to be readable on the Macintosh, the file type must
# be set to 'PICT'. A number of UNIX utilities, including mcvert and BinHex allow
# you to do this. Or you can use the picttoppm utility (part of the netppm suite of
# graphics tools) to translate the file into any format you desire.
# A WORKING EXAMPLE
# require "qd.pl";
# &qd'SetRect(*myRect,0,0,500,500); # Define a 500 pixel square
# &qd'OpenPicture($myRect); # Begin defining the picture
# &qd'ClipRect($myRect); # Always a good idea
# &qd'MoveTo(5,5); # Move the pen to a starting point
# &qd'LineTo(400,400); # A diagonal line
# &qd'TextFont($qd'COURIER); # Set the font
# &qd'MoveTo(50,20); # Move the pen to a new starting point
# &qd'DrawString("Hello there!"); # Friendly greeting
# &qd'SetRect(*myRect,80,80,250,250); # New rectangle
# &qd'RGBForeColor(0x0000,0x0000,0xFFFF); # Set the color to blue
# &qd'PaintRect($myRect); # Fill rectangle with that color
# $data = &qd'ClosePicture; # Close picture and retrieve data
# # Pipe through binhex, setting the creator type to JVWR for JPEG Viewer
# # Note: BinHex is available at
# open (BINHEX "| BinHex -t PICT -c JVWR -n 'An Example'");
# print BINHEX $data;
# close BINHEX;
# # Turn it into a GIF file, using the ppm utilities
# open (GIF, "| picttoppm | ppmtogif -transparent white");
# print GIF $data;
# close GIF;
# MISCELLANEOUS NOTES
# NOTE: For some reason the various FILL routines don't work as
# advertised. They are simulated by a PnPat followed by a paint
# --------------------------------------------------------------------
# Quickdraw-like functions -- now using PICT2
# --------------------------------------------------------------------
{
package qd;
# Directory to look in to find font metric definitions -- change this
# for your installation
$X11FONTS = '/usr/local/X11R5/X11/fonts/bdf';
# Apple quickdraw constants
$TIMES = 20;
$HELVETICA = 21;
$COURIER = 22;
$SYMBOL = 23;
$NEWCENTURYSCHOOLBK = 34;
$PLAIN = 0;
$BOLD = 1;
$ITALIC = 2;
$UNDERLINE = 4;
# Some minimal patterns -- define your own if you like
$GRAY = pack ('n4',0xAA55,0xAA55,0xAA55,0xAA55);
$DKGRAY = pack ('n4',0xDD77,0xDD77,0xDD77,0xDD77);
$LTGRAY = pack ('n4',0x8822,0x8822,0x8822,0x8822);
$WHITE = pack('n4',0x0000,0x0000,0x0000,0x0000);
$BLACK = pack ('n4',0xFFFF,0xFFFF,0xFFFF,0xFFFF);
# absolute colors to be used with FgColor/BgColor
# (for better control, use RGBFgColor/RGBBgColor)
$BLACKCOLOR = 33;
$WHITECOLOR = 30;
$REDCOLOR = 209;
$GREENCOLOR = 329;
$BLUECOLOR = 389;
$CYANCOLOR = 269;
$MAGENTACOLOR = 149;
$YELLOWCOLOR = 89;
# This defines the header used at the beginning of PICT files:
$PICTHEADER = "\0" x 512;
# These are phoney font metrics which we use when no font metrics files are
# around to help us out.
$fudgefactor = 0.55;
$ITALICEXTRA = 0.05;
$BOLDEXTRA = 0.08;
# Initial starting values
$textFont = $HELVETICA;
$textSize = 12;
$textFace = $PLAIN;
$rgbfgcolor = pack('n*',0xFFFF,0xFFFF,0xFFFF);
$rgbbgcolor = pack('n*',0,0,0);
$fgcolor = $BLACKCOLOR;
$bgcolor = $WHITECOLOR;
$polySave = undef;
$_PnPattern = $BLACK;
$_polyName = "polygon000";
sub OpenPicture { # begin a picture
local($rect) = @_;
$currH = $currV = 0; # current pen position
$pict = $PICTHEADER; # the header
$pict .= pack('n',0); # size int (placeholder)
$pict .= $rect; # pict frame
$pict .= pack('n',0x0011); # Type 2 picture
$pict .= pack('n',0x02FF); # version number
$pict .= pack('nC24',0x0C00,0); # reserved header opcode + 24 bytes of reserved data
# initialize the font and size
&TextFont($textFont);
&TextSize($textSize);
&TextFace($textFace);
}
sub ClosePicture { # close pict and return it
$pict .= pack ('n',0x00FF); # end of pict code
substr($pict,512,2) = pack('n',length($pict) - 512); # fill in length
return $pict;
}
sub ClipRect {
local($rect) = @_;
$pict .= pack('nn',0x0001,0x0A) . $rect;
}
sub PenPat {
local($newpat) = @_;
return unless $newpat ne $_PnPattern;
$_PnPattern = $newpat;
$pict .= pack('n',0x0009) . $_PnPattern;
}
sub RGBForeColor {
local($rgb) = pack('n3',@_);
return unless $rgb ne $rgbfgcolor;
$rgbfgcolor = $rgb;
$pict .= pack('n',0x001A) . $rgbfgcolor;
}
sub RGBBackColor {
local($rgb) = pack('n3',@_);
return unless $rgb ne $rgbbgcolor;
$rgbbgcolor = $rgb;
$pict .= pack('n',0x001B) . $rgbbgcolor;
}
sub FgColor {
local($color) = @_;
return unless $color != $fgcolor;
$fgcolor = $color;
$pict .= pack('nL',0x000E,$color);
}
sub BgColor {
local($color) = @_;
return unless $color != $bgcolor;
$bgcolor = $color;
$pict .= pack('nL',0x000F,$color);
}
sub TextFont {
local($font) = @_;
$textFont = $font;
$pict .= pack('nn',0x0003,$font);
}
sub TextSize {
local($size) = @_;
$textSize = $size;
$pict .= pack('nn',0x000D,$size);
}
sub PenSize {
local($h,$v) = @_;
$pict .= pack('nnn',0x0007,$v,$h);
}
sub TextFace {
return if $textFace == @_[0];
$textFace = @_[0];
$pict .= pack ('nCC',0x0004,$textFace,0); # (zero added to pad to word)
}
sub DrawString {
local($text) = @_;
$text .= "\0" x ((length($text) + 1) % 2); # pad text to an odd length
$pict .= pack('nnnC',0x0028,$currV,$currH,length($text)) . $text;
}
# RECTANGLE MANIPULATION ROUTINES. Note that
# the rectangles are passed by NAME rather than by value,
# in accordance with the MacOS way of doing things.
sub SetRect {
local(*r,$h1,$v1,$h2,$v2) = @_;
$r = pack ('n4',$v1,$h1,$v2,$h2);
}
sub OffsetRect {
local(*r,$x,$y) = @_;
local($v1,$h1,$v2,$h2) = unpack('n4',$r);
$h1 += $x; $h2 += $x;
$v1 += $y; $v2 += $y;
$r = pack ('n4',$v1,$h1,$v2,$h2);
}
sub InsetRect {
local(*r,$x,$y) = @_;
local($v1,$h1,$v2,$h2) = unpack('n4',$r);
$h1 -= int($x/2); $h2 -= int($x/2);
$v1 -= int($y/2); $v2 -= int($y/2);
$r = pack ('n4',$v1,$h1,$v2,$h2);
}
# A few utility routine to translate between perl
# arrays and rectangles.
# four-element perl array to quickdraw rect structure
sub a2r {
local($top,$left,$bottom,$right) = @_;
return pack('n4',$top,$left,$bottom,$right);
}
# rectangle to four-element perl array
sub r2a {
local($rect) = @_;
return unpack('n4',$rect);
}
# associative array in which the keys are 'top','left','bottom','right'
# to quickdraw rect structure
sub aa2r {
local(%r) = @_;
return pack('n4',$r{'top'},$r{'left'},$r{'bottom'},$r{'right'});
}
# quickdraw rect structure to associative array
sub r2aa {
local($r) = @_;
local(%r);
($r{'top'},$r{'left'},$r{'bottom'},$r{'right'}) = unpack('n4',$r);
return %r;
}
# LINE DRAWING ROUTINES
sub MoveTo {
($currH,$currV) = @_;
}
sub Move {
local($dh,$dv) = @_;
$currH += $dh;
$currV += $dv;
}
sub LineTo {
local($h,$v) = @_;
# Special handling for polygons
if (defined(@polySave)) {
&_addVertex(*polySave,$h,$v)
} else {
$pict .= pack('nn4',0x0020,$currV,$currH,$v,$h);
}
($currH,$currV) = ($h,$v);
}
sub Line {
local($dh,$dv) = @_;
# Special handling for polygons
if (defined(@polySave)) {
&_addVertex(*polySave,$h,$v);
} else {
$pict .= pack('nn4',0x0020,$currV,$currH,$currV+$dv,$currH+$dh);
}
($currH,$currV) = ($currH+$dh,$currV+$dv);
}
sub Scale { #use picComment to set laserwriter line scaling
local($numerator,$denominator)= @_;
$pict .= pack('nnnn2',0x00A1,182,4,$numerator,$denominator);
}
# Rectangles
sub FrameRect {
local($rect) = @_;
$pict .= pack('n',0x0030) . $rect;
}
sub PaintRect {
local($rect) = @_;
$pict .= pack('n',0x0031) . $rect;
}
sub EraseRect {
local($rect) = @_;
$pict .= pack('n',0x0032) . $rect;
}
sub InvertRect {
local($rect) = @_;
$pict .= pack('n',0x0033) . $rect;
}
sub FillRect {
local($rect,$pattern) = @_;
local($oldpat) = $_PnPattern;
&PenPat($pattern);
&PaintRect($rect);
&PenPat($oldpat);
}
# Ovals
sub FrameOval {
local($rect) = @_;
$pict .= pack('n',0x0050) . $rect;
}
sub PaintOval {
local($rect) = @_;
$pict .= pack('n',0x0051) . $rect;
}
sub EraseOval {
local($rect) = @_;
$pict .= pack('n',0x0052) . $rect;
}
sub InvertOval {
local($rect) = @_;
$pict .= pack('n',0x0053) . $rect;
}
sub FillOval {
local($rect,$pattern) = @_;
local($oldpat) = $_PnPattern;
&PenPat($pattern);
&PaintOval($rect);
&PenPat($oldpat);
}
# Arcs
sub FrameArc {
local($rect,$startAngle,$arcAngle) = @_;
$pict .= pack('n',0x0060) . $rect;
$pict .= pack('nn',$startAngle,$arcAngle);
}
sub PaintArc {
local($rect,$startAngle,$arcAngle) = @_;
$pict .= pack('n',0x0061) . $rect;
$pict .= pack('nn',$startAngle,$arcAngle);
}
sub EraseArc {
local($rect,$startAngle,$arcAngle) = @_;
$pict .= pack('n',0x0062) . $rect;
$pict .= pack('nn',$startAngle,$arcAngle);
}
sub InvertArc {
local($rect,$startAngle,$arcAngle) = @_;
$pict .= pack('n',0x0063) . $rect;
$pict .= pack('nn',$startAngle,$arcAngle);
}
sub FillArc {
local($rect,$startAngle,$arcAngle,$pattern) = @_;
local($oldpat) = $_PnPattern;
&PenPat($pattern);
&PaintArc($rect,$startAngle,$arcAngle);
&PenPat($oldpat);
}
# Round rects
sub FrameRoundRect {
local($rect,$ovalWidth,$ovalHeight) = @_;
unless ($_roundRectCurvature eq "$ovalWidth $ovalHeight") {
$pict .= pack('nn2',0x000B,$ovalHeight,$ovalWidth);
$_roundRectCurvature = "$ovalWidth $ovalHeight";
}
$pict .= pack('n',0x0040) . $rect;
}
sub PaintRoundRect {
local($rect,$ovalWidth,$ovalHeight) = @_;
unless ($_roundRectCurvature eq "$ovalWidth $ovalHeight") {
$pict .= pack('nn2',0x000B,$ovalHeight,$ovalWidth);
$_roundRectCurvature = "$ovalWidth $ovalHeight";
}
$pict .= pack('n',0x0041) . $rect;
}
sub EraseRoundRect {
local($rect,$ovalWidth,$ovalHeight) = @_;
unless ($_roundRectCurvature eq "$ovalWidth $ovalHeight") {
$pict .= pack('nn2',0x000B,$ovalHeight,$ovalWidth);
$_roundRectCurvature = "$ovalWidth $ovalHeight";
}
$pict .= pack('n',0x0042) . $rect;
}
sub InvertRoundRect {
local($rect,$ovalWidth,$ovalHeight) = @_;
unless ($_roundRectCurvature eq "$ovalWidth $ovalHeight") {
$pict .= pack('nn2',0x000B,$ovalHeight,$ovalWidth);
$_roundRectCurvature = "$ovalWidth $ovalHeight";
}
$pict .= pack('n',0x0043) . $rect;
}
sub FillRoundRect {
local($rect,$ovalWidth,$ovalHeight,$pattern) = @_;
local($oldpat) = $_PnPattern;
&PenPat($pattern);
&PaintRoundRect($rect,$ovalWidth,$ovalHeight);
&PenPat($oldpat);
}
# Polygons -- you are only allowed to create one polygon at a time.
# You will be returned a "handle" which contains the growing polygon
# structure. The "handle" is actually the NAME of the scalar
sub OpenPoly {
$_polyName++;
undef $polySave; # close one if it was already defined
*polySave = $_polyName;
@polySave = (10,0,0,0,0); # initialize it to empty size and rectangle
return $_polyName;
}
sub ClosePoly {
*polySave = 'scratch';
undef @polySave;
}
# Kill the poly -- really a no-op in perl
sub KillPoly {
local(*poly) = @_;
undef @poly;
}
# Polygon drawing
sub FramePoly {
local(*poly) = @_;
return unless @poly;
$pict .= pack('n*',0x0070,@poly);
}
sub PaintPoly {
local(*poly) = @_;
return unless @poly;
$pict .= pack('n*',0x0071,@poly);
}
sub ErasePoly {
local(*poly) = @_;
return unless @poly;
$pict .= pack('n*',0x0072,@poly);
}
sub InvertPoly {
local(*poly) = @_;
return unless @poly;
$pict .= pack('n*',0x0073,@poly);
}
sub FillPoly {
local(*poly,$pattern) = @_;
return unless @poly;
local($oldpat) = $_PnPattern;
&PenPat($pattern);
&PaintPoly(*poly);
&PenPat($oldpat);
}
sub OffsetPoly {
local(*poly,$dh,$dv) = @_;
return unless @poly;
local($size,@vertices) = @poly;
local($i);
for ($i=0;$i<@vertices;$i+=2) {
$vertices[$i] += $dv;
$vertices[$i+1] += $dh;
}
@poly = ($size,@vertices);
}
sub MapPoly {
local(*poly,$srcRect,$destRect) = @_;
return unless @poly;
local($size,@vertices) = @poly;
local(@src) = unpack('n4',$srcRect);
local(@dest) = unpack('n4',$destRect);
local($factorV) = ($dest[2]-$dest[0])/($src[2]-$src[0]);
local($factorH) = ($dest[3]-$dest[1])/($src[3]-$src[1]);
for ($i=0;$i<@vertices;$i+=2) {
$vertices[$i] = int($dest[0] + ($vertices[$i] - $src[0]) * $factorV);
$vertices[$i+1] = int($dest[1] + ($vertices[$i+1] - $src[1]) * $factorH);
}
@poly = ($size,@vertices);
}
# A utility routine to add a vertex to the growing polygon structure
# We need to grow both the size of the polygon and increase the bounding
# rectangle. A special case occurs when we add the first vertex:
# we store both the current position
sub _addVertex {
local(*polygon,$h,$v) = @_;
local($size,$top,$left,$bottom,$right,@vertices) = @polygon;
# Special case for empty vertices -- add the current point
unless (@vertices) {
push(@vertices,$currV,$currH);
$size += 4;
$top = $bottom = $currV;
$left = $right = $currH;
}
# IM V1 implies that all vertices are stored relative to
# the first point -- I don't know if this is really the case
push (@vertices,$v,$h);
$size += 4;
$top = $v if $v < $top;
$bottom = $v if $v > $bottom;
$left = $h if $h < $left;
$right = $h if $h > $right;
@polygon=($size,$top,$left,$bottom,$right,@vertices);
}
# We try to get the metrics from an X11 bdf font file, if possible.
sub TextWidth {
local($text) = @_;
# See if we can derive the character widths from a metrics file
local($face) = 0xFB & $textFace; # underlining don't count
local($metric_name) = &_getFontMetrics($textFont,$textSize,$face);
if ($metric_name && (*metrics = $metric_name) && defined(%metrics)) {
local($length);
foreach (split('',$text)) {
$length += $metrics{ord($_)};
}
return $length;
} else { # we get here if we don't have any metrics - make it up
local($extra);
$extra += $ITALICEXTRA if vec($textFace,$ITALIC,1);
$extra += $BOLDEXTRA if vec($textFace,$BOLD,1);
return length($text) * $textSize * ($fudgefactor+$extra);
}
}
# Utility routine to read text widths out of bdf files. We create a metrics
# array on the fly. The names of the metrics files are stored in an array
# called _metricsArrays. We return the name of the array, or undef if inapplicable.
sub _getFontMetrics {
local($font,$size,$face) = @_;
local($key) = "$font $size $face";
return $_metricsArrays{$key} if $_metricsArrays{$key};
# If we get here, we don't have a metrics array to return. See if we can
# construct one from a bdf file.
# Don't bother unless this font is defined.
return undef unless $font_metric_files{$key};
# Don't bother if we tried before and failed
return undef if $_failed_metric{$key};
# Try to open up the bdf file. Remember if we fail
unless (open(BDF,"$font_metric_files{$key}")) {
$_failed_metric_files{$key}++;
return undef;
}
# Wow! We're golden. Create a new metrics array
$next_metric++; # bump up the name
local(*metrics) = $next_metric; local($char);
while () {
next unless /^STARTCHAR/../^ENDCHAR/;
if (/^ENCODING\s+(\d+)/) { $char = $1; }
elsif (/^DWIDTH\s+(\d+)/) { $metrics{$char}=$1; }
}
close(BDF);
# Remember the name of the metrics array and return it
return $_metricsArrays{$key} = $next_metric;
}
# Ugly stuff that I want to hide at the bottom
# For the purposes of mapping from quickdraw fonts to X11fonts, we define
# the following dictionary:
%font_metric_files = (
"22 8 1","$X11FONTS/courB08.bdf",
"22 10 1","$X11FONTS/courB10.bdf",
"22 12 1","$X11FONTS/courB12.bdf",
"22 14 1","$X11FONTS/courB14.bdf",
"22 18 1","$X11FONTS/courB18.bdf",
"22 24 1","$X11FONTS/courB24.bdf",
"22 8 2","$X11FONTS/courO08.bdf",
"22 10 2","$X11FONTS/courO10.bdf",
"22 12 2","$X11FONTS/courO12.bdf",
"22 14 2","$X11FONTS/courO14.bdf",
"22 18 2","$X11FONTS/courO18.bdf",
"22 24 2","$X11FONTS/courO24.bdf",
"22 8 0","$X11FONTS/courR08.bdf",
"22 10 0","$X11FONTS/courR10.bdf",
"22 12 0","$X11FONTS/courR12.bdf",
"22 14 0","$X11FONTS/courR14.bdf",
"22 18 0","$X11FONTS/courR18.bdf",
"22 24 0","$X11FONTS/courR24.bdf",
"21 8 1","$X11FONTS/helvB08.bdf",
"21 10 1","$X11FONTS/helvB10.bdf",
"21 12 1","$X11FONTS/helvB12.bdf",
"21 14 1","$X11FONTS/helvB14.bdf",
"21 18 1","$X11FONTS/helvB18.bdf",
"21 24 1","$X11FONTS/helvB24.bdf",
"21 8 2","$X11FONTS/helvO08.bdf",
"21 10 2","$X11FONTS/helvO10.bdf",
"21 12 2","$X11FONTS/helvO12.bdf",
"21 14 2","$X11FONTS/helvO14.bdf",
"21 18 2","$X11FONTS/helvO18.bdf",
"21 24 2","$X11FONTS/helvO24.bdf",
"21 8 0","$X11FONTS/helvR08.bdf",
"21 10 0","$X11FONTS/helvR10.bdf",
"21 12 0","$X11FONTS/helvR12.bdf",
"21 14 0","$X11FONTS/helvR14.bdf",
"21 18 0","$X11FONTS/helvR18.bdf",
"21 24 0","$X11FONTS/helvR24.bdf",
"20 8 1","$X11FONTS/timB08.bdf",
"20 10 1","$X11FONTS/timB10.bdf",
"20 12 1","$X11FONTS/timB12.bdf",
"20 14 1","$X11FONTS/timB14.bdf",
"20 18 1","$X11FONTS/timB18.bdf",
"20 24 1","$X11FONTS/timB24.bdf",
"20 8 3","$X11FONTS/timBI08.bdf",
"20 10 3","$X11FONTS/timBI10.bdf",
"20 12 3","$X11FONTS/timBI12.bdf",
"20 14 3","$X11FONTS/timBI14.bdf",
"20 18 3","$X11FONTS/timBI18.bdf",
"20 24 3","$X11FONTS/timBI24.bdf",
"20 8 2","$X11FONTS/timI08.bdf",
"20 10 2","$X11FONTS/timI10.bdf",
"20 12 2","$X11FONTS/timI12.bdf",
"20 14 2","$X11FONTS/timI14.bdf",
"20 18 2","$X11FONTS/timI18.bdf",
"20 24 2","$X11FONTS/timI24.bdf",
"20 8 0","$X11FONTS/timR08.bdf",
"20 10 0","$X11FONTS/timR10.bdf",
"20 12 0","$X11FONTS/timR12.bdf",
"20 14 0","$X11FONTS/timR14.bdf",
"20 18 0","$X11FONTS/timR18.bdf",
"20 24 0","$X11FONTS/timR24.bdf",
"34 8 1","$X11FONTS/ncenB08.bdf",
"34 10 1","$X11FONTS/ncenB10.bdf",
"34 12 1","$X11FONTS/ncenB12.bdf",
"34 14 1","$X11FONTS/ncenB14.bdf",
"34 18 1","$X11FONTS/ncenB18.bdf",
"34 24 1","$X11FONTS/ncenB24.bdf",
"34 8 3","$X11FONTS/ncenBI08.bdf",
"34 10 3","$X11FONTS/ncenBI10.bdf",
"34 12 3","$X11FONTS/ncenBI12.bdf",
"34 14 3","$X11FONTS/ncenBI14.bdf",
"34 18 3","$X11FONTS/ncenBI18.bdf",
"34 24 3","$X11FONTS/ncenBI24.bdf",
"34 8 2","$X11FONTS/ncenI08.bdf",
"34 10 2","$X11FONTS/ncenI10.bdf",
"34 12 2","$X11FONTS/ncenI12.bdf",
"34 14 2","$X11FONTS/ncenI14.bdf",
"34 18 2","$X11FONTS/ncenI18.bdf",
"34 24 2","$X11FONTS/ncenI24.bdf",
"34 8 0","$X11FONTS/ncenR08.bdf",
"34 10 0","$X11FONTS/ncenR10.bdf",
"34 12 0","$X11FONTS/ncenR12.bdf",
"34 14 0","$X11FONTS/ncenR14.bdf",
"34 18 0","$X11FONTS/ncenR18.bdf",
"34 24 0","$X11FONTS/ncenR24.bdf"
);
$next_metric = "metrics0000"; # name of our metrics arrays - dynamically allocated
1;
} #end of package qd
GD-2.46/typemap 0000644 0001750 0001750 00000000102 07403777174 012730 0 ustar lstein lstein TYPEMAP
GD::Image T_PTROBJ
GD::Font T_PTROBJ
GD::Polygon T_PTROBJ
GD-2.46/META.yml 0000644 0001750 0001750 00000001037 11557316041 012572 0 ustar lstein lstein --- #YAML:1.0
name: GD
version: 2.46
abstract: Interface to Gd Graphics Library
author:
- Lincoln Stein (lstein@cshl.org)
license: unknown
distribution_type: module
configure_requires:
ExtUtils::MakeMaker: 0
build_requires:
ExtUtils::MakeMaker: 0
requires:
Math::Trig: 0
no_index:
directory:
- t
- inc
generated_by: ExtUtils::MakeMaker version 6.55_02
meta-spec:
url: http://module-build.sourceforge.net/META-spec-v1.4.html
version: 1.4
GD-2.46/GD/ 0000755 0001750 0001750 00000000000 11557316041 011612 5 ustar lstein lstein GD-2.46/GD/Simple.pm 0000644 0001750 0001750 00000106560 11523333544 013411 0 ustar lstein lstein package GD::Simple;
=head1 NAME
GD::Simple - Simplified interface to GD library
=head1 SYNOPSIS
use GD::Simple;
# create a new image
$img = GD::Simple->new(400,250);
# draw a red rectangle with blue borders
$img->bgcolor('red');
$img->fgcolor('blue');
$img->rectangle(10,10,50,50);
# draw an empty rectangle with green borders
$img->bgcolor(undef);
$img->fgcolor('green');
$img->rectangle(30,30,100,100);
# move to (80,80) and draw a green line to (100,190)
$img->moveTo(80,80);
$img->lineTo(100,190);
# draw a solid orange ellipse
$img->moveTo(110,100);
$img->bgcolor('orange');
$img->fgcolor('orange');
$img->ellipse(40,40);
# draw a black filled arc
$img->moveTo(150,150);
$img->fgcolor('black');
$img->arc(50,50,0,100,gdNoFill|gdEdged);
# draw a string at (10,180) using the default
# built-in font
$img->moveTo(10,180);
$img->string('This is very simple');
# draw a string at (280,210) using 20 point
# times italic, angled upward 90 degrees
$img->moveTo(280,210);
$img->font('Times:italic');
$img->fontsize(20);
$img->angle(-90);
$img->string('This is very fancy');
# some turtle graphics
$img->moveTo(300,100);
$img->penSize(3,3);
$img->angle(0);
$img->line(20); # 20 pixels going to the right
$img->turn(30); # set turning angle to 30 degrees
$img->line(20); # 20 pixel line
$img->line(20);
$img->line(20);
$img->turn(-90); # set turning angle to -90 degrees
$img->line(50); # 50 pixel line
# draw a cyan polygon edged in blue
my $poly = new GD::Polygon;
$poly->addPt(150,100);
$poly->addPt(199,199);
$poly->addPt(100,199);
$img->bgcolor('cyan');
$img->fgcolor('blue');
$img->penSize(1,1);
$img->polygon($poly);
# convert into png data
print $img->png;
=head1 DESCRIPTION
GD::Simple is a subclass of the GD library that shortens many of the
long GD method calls by storing information about the pen color, size
and position in the GD object itself. It also adds a small number of
"turtle graphics" style calls for those who prefer to work in polar
coordinates. In addition, the library allows you to use symbolic
names for colors, such as "chartreuse", and will manage the colors for
you.
=head2 The Pen
GD::Simple maintains a "pen" whose settings are used for line- and
shape-drawing operations. The pen has the following properties:
=over 4
=item fgcolor
The pen foreground color is the color of lines and the borders of
filled and unfilled shapes.
=item bgcolor
The pen background color is the color of the contents of filled
shapes.
=item pensize
The pen size is the width of the pen. Larger sizes draw thicker
lines.
=item position
The pen position is its current position on the canvas in (X,Y)
coordinates.
=item angle
When drawing in turtle mode, the pen angle determines the current
direction of lines of relative length.
=item turn
When drawing in turtle mode, the turn determines the clockwise or
counterclockwise angle that the pen will turn before drawing the next
line.
=item font
The font to use when drawing text. Both built-in bitmapped fonts and
TrueType fonts are supported.
=item fontsize
The size of the font to use when drawing with TrueType fonts.
=back
One sets the position and properties of the pen and then draws. As
the drawing progresses, the position of the pen is updated.
=head2 Methods
GD::Simple introduces a number of new methods, a few of which have the
same name as GD::Image methods, and hence change their behavior. In
addition to these new methods, GD::Simple objects support all of the
GD::Image methods. If you make a method call that isn't directly
supported by GD::Simple, it refers the request to the underlying
GD::Image object. Hence one can load a JPEG image into GD::Simple and
declare it to be TrueColor by using this call, which is effectively
inherited from GD::Image:
my $img = GD::Simple->newFromJpeg('./myimage.jpg',1);
The rest of this section describes GD::Simple-specific methods.
=cut
use strict;
use GD;
use GD::Group;
use Math::Trig;
use Carp 'croak';
our @ISA = 'Exporter';
our @EXPORT = @GD::EXPORT;
our @EXPORT_OK = @GD::EXPORT_OK;
our $AUTOLOAD;
my %COLORS;
my $IMAGECLASS = 'GD::Image';
my $TRANSPARENT;
sub AUTOLOAD {
my $self = shift;
my($pack,$func_name) = $AUTOLOAD=~/(.+)::([^:]+)$/;
return if $func_name eq 'DESTROY';
if (ref $self && exists $self->{gd}) {
$self->{gd}->$func_name(@_);
} else {
my @result = $IMAGECLASS->$func_name(@_);
if (UNIVERSAL::isa($result[0],'GD::Image')) {
return $self->new($result[0]);
} else {
return @result;
}
}
}
=over 4
=item $img = GD::Simple->new($x,$y [,$truecolor])
=item $img = GD::Simple->new($gd)
Create a new GD::Simple object. There are two forms of new(). In the
first form, pass the width and height of the desired canvas, and
optionally a boolean flag to request a truecolor image. In the second
form, pass a previously-created GD::Image object.
=cut
# dual-purpose code - beware
sub new {
my $pack = shift;
unshift @_,(100,100) if @_ == 0;
if (@_ >= 2) { # traditional GD::Image->new() call
my $gd = $IMAGECLASS->new(@_);
my $self = $pack->new($gd);
$self->clear;
return $self;
}
if (@_ == 1) { # initialize from existing image
my $gd = shift;
my $self = bless {
gd => $gd,
xy => [0,0],
font => gdSmallFont,
fontsize => 9,
turningangle => 0,
angle => 0,
pensize => 1,
},$pack;
$self->{bgcolor} = $self->translate_color(255,255,255);
$self->{fgcolor} = $self->translate_color(0,0,0);
return $self;
}
}
=item GD::Simple->class('GD');
=item GD::Simple->class('GD::SVG');
Select whether new() should use GD or GD::SVG internally. Call
GD::Simple->class('GD::SVG') before calling new() if you wish to
generate SVG images.
If future GD subclasses are created, this method will subport them.
=cut
sub class {
my $pack = shift;
if (@_) {
$IMAGECLASS = shift;
eval "require $IMAGECLASS; 1" or die $@;
$IMAGECLASS = "$IMAGECLASS\:\:Image"
if $IMAGECLASS eq 'GD::SVG';
}
$IMAGECLASS;
}
=item $img->moveTo($x,$y)
This call changes the position of the pen without drawing. It moves
the pen to position ($x,$y) on the drawing canvas.
=cut
sub moveTo {
my $self = shift;
croak 'Usage GD::Simple->moveTo($x,$y)' unless @_ == 2;
my ($x,$y) = @_;
$self->{xy} = [$x,$y];
}
=item $img->move($dx,$dy)
=item $img->move($dr)
This call changes the position of the pen without drawing. When called
with two arguments it moves the pen $dx pixels to the right and $dy
pixels downward. When called with one argument it moves the pen $dr
pixels along the vector described by the current pen angle.
=cut
sub move {
my $self = shift;
if (@_ == 1) { # polar coordinates -- this is r
$self->{angle} += $self->{turningangle};
my $angle = deg2rad($self->{angle});
$self->{xy}[0] += $_[0] * cos($angle);
$self->{xy}[1] += $_[0] * sin($angle);
}
elsif (@_ == 2) { # cartesian coordinates
$self->{xy}[0] += $_[0];
$self->{xy}[1] += $_[1];
} else {
croak 'Usage GD::Simple->move($dx,$dy) or move($r)';
}
}
=item $img->lineTo($x,$y)
The lineTo() call simultaneously draws and moves the pen. It draws a
line from the current pen position to the position defined by ($x,$y)
using the current pen size and color. After drawing, the position of
the pen is updated to the new position.
=cut
sub lineTo {
my $self = shift;
croak 'Usage GD::Simple->lineTo($x,$y)' unless @_ == 2;
$self->gd->line($self->curPos,@_,$self->fgcolor);
$self->moveTo(@_);
}
=item $img->line($x1,$y1,$x2,$y2 [,$color])
=item $img->line($dx,$dy)
=item $img->line($dr)
The line() call simultaneously draws and moves the pen. When called
with two arguments it draws a line from the current position of the
pen to the position $dx pixels to the right and $dy pixels down. When
called with one argument, it draws a line $dr pixels long along the
angle defined by the current pen angle.
When called with four or five arguments, line() behaves like
GD::Image->line().
=cut
sub line {
my $self = shift;
if (@_ >= 4) {
my ($x1,$x2,$y1,$y2,$color) = @_;
$color ||= $self->fgcolor;
return $self->gd->line($x1,$x2,$y1,$y2,$color);
}
croak 'Usage GD::Simple->line($dx,$dy) or line($r) or line($x1,$y1,$x2,$y2 [,$color])' unless @_ >= 1;
my @curPos = $self->curPos;
$self->move(@_);
my @newPos = $self->curPos;
return $self->gd->line(@curPos,@newPos,$self->fgcolor);
}
=item $img->clear
This method clears the canvas by painting over it with the current
background color.
=cut
sub clear {
my $self = shift;
$self->gd->filledRectangle(0,0,$self->getBounds,$self->bgcolor);
}
=item $img->rectangle($x1,$y1,$x2,$y2)
This method draws the rectangle defined by corners ($x1,$y1),
($x2,$y2). The rectangle's edges are drawn in the foreground color and
its contents are filled with the background color. To draw a solid
rectangle set bgcolor equal to fgcolor. To draw an unfilled rectangle
(transparent inside), set bgcolor to undef.
=cut
sub rectangle {
my $self = shift;
return $self->gd->rectangle(@_) if @_ == 5;
croak 'Usage GD::Simple->rectangle($x1,$y1,$x2,$y2)' unless @_ == 4;
my $gd = $self->gd;
my ($bg,$fg) = ($self->bgcolor,$self->fgcolor);
$gd->filledRectangle(@_,$bg) if defined $bg;
$gd->rectangle(@_,$fg) if defined $fg && (!defined $bg || $bg != $fg);
}
=item $img->ellipse($width,$height)
This method draws the ellipse centered at the current location with
width $width and height $height. The ellipse's border is drawn in the
foreground color and its contents are filled with the background
color. To draw a solid ellipse set bgcolor equal to fgcolor. To draw
an unfilled ellipse (transparent inside), set bgcolor to undef.
=cut
sub ellipse {
my $self = shift;
return $self->gd->ellipse(@_) if @_ == 5;
croak 'Usage GD::Simple->ellipse($width,$height)' unless @_ == 2;
my $gd = $self->gd;
my ($bg,$fg) = ($self->bgcolor,$self->fgcolor);
$gd->filledEllipse($self->curPos,@_,$bg) if defined $bg;
$gd->ellipse($self->curPos,@_,$fg) if defined $fg && (!defined $bg || $bg != $fg);
}
=item $img->arc($cx,$cy,$width,$height,$start,$end [,$style])
This method draws filled and unfilled arcs. See L for a
description of the arguments. To draw a solid arc (such as a pie
wedge) set bgcolor equal to fgcolor. To draw an unfilled arc, set
bgcolor to undef.
=cut
sub arc {
my $self = shift;
return $self->gd->arc(@_) if @_ == 7;
croak 'Usage GD::Simple->arc($width,$height,$start,$end,$style)' unless @_ >= 4;
my ($width,$height,$start,$end,$style) = @_;
my $gd = $self->gd;
my ($bg,$fg) = ($self->bgcolor,$self->fgcolor);
my ($cx,$cy) = $self->curPos;
if ($bg) {
my @args = ($cx,$cy,$width,$height,$start,$end,$bg);
push @args,$style if defined $style;
$gd->filledArc(@args);
} else {
my @args = ($cx,$cy,$width,$height,$start,$end,$fg);
$gd->arc(@args);
}
}
=item $img->polygon($poly)
This method draws filled and unfilled polygon using the current
settings of fgcolor for the polygon border and bgcolor for the polygon
fill color. See L for a description of creating polygons. To draw
a solid polygon set bgcolor equal to fgcolor. To draw an unfilled
polygon, set bgcolor to undef.
=cut
sub polygon {
my $self = shift;
croak 'Usage GD::Simple->polygon($poly)' unless @_ == 1;
my $gd = $self->gd;
my ($bg,$fg) = ($self->bgcolor,$self->fgcolor);
$gd->filledPolygon(@_,$bg) if defined $bg;
$gd->openPolygon(@_,$fg) if defined $fg && (!defined $bg || $bg != $fg);
}
=item $img->polyline($poly)
This method draws polygons without closing the first and last vertices
(similar to GD::Image->unclosedPolygon()). It uses the fgcolor to draw
the line.
=cut
sub polyline {
my $self = shift;
croak 'Usage GD::Simple->polyline($poly)' unless @_ == 1;
my $gd = $self->gd;
my $fg = $self->fgcolor;
$gd->unclosedPolygon(@_,$fg);
}
=item $img->string($string)
This method draws the indicated string starting at the current
position of the pen. The pen is moved to the end of the drawn string.
Depending on the font selected with the font() method, this will use
either a bitmapped GD font or a TrueType font. The angle of the pen
will be consulted when drawing the text. For TrueType fonts, any angle
is accepted. For GD bitmapped fonts, the angle can be either 0 (draw
horizontal) or -90 (draw upwards).
For consistency between the TrueType and GD font behavior, the string
is always drawn so that the current position of the pen corresponds to
the bottom left of the first character of the text. This is different
from the GD behavior, in which the first character of bitmapped fonts
hangs down from the pen point.
This method returns a polygon indicating the bounding box of the
rendered text. If an error occurred (such as invalid font
specification) it returns undef and an error message in $@.
=cut
sub string {
my $self = shift;
return $self->gd->string(@_) if @_ == 5;
my $string = shift;
my $font = $self->font;
my @bounds;
if (ref $font && $font->isa('GD::Font')) {
my ($x,$y) = $self->curPos;
if ($self->angle == -90) {
$x -= $font->height;
$y -= $font->width;
$self->gd->stringUp($font,$x,$y,$string,$self->fgcolor);
$self->{xy}[1] -= length($string) * $font->width;
@bounds = ( ($self->{xy}[0],$y), ($x,$y), ($x,$self->{xy}[1]-$font->width), ($self->{xy}[0],$self->{xy}[1]-$font->width) );
} else {
$y -= $font->height;
$self->gd->string($font,$x,$y,$string,$self->fgcolor);
$self->{xy}[0] += length($string) * $font->width;
@bounds = ( ($x,$self->{xy}[1]), ($self->{xy}[0],$self->{xy}[1]), ($self->{xy}[0],$y), ($x,$y) );
}
}
else {
$self->useFontConfig(1);
@bounds = $self->stringFT($self->fgcolor,$font,
$self->fontsize,-deg2rad($self->angle), # -pi * $self->angle/180,
$self->curPos,$string);
return unless @bounds;
my ($delta_x,$delta_y) = $self->_string_width(@bounds);
$self->{xy}[0] += $delta_x;
$self->{xy}[1] += $delta_y;
}
my $poly = GD::Polygon->new;
while (@bounds) {
$poly->addPt(splice(@bounds,0,2));
}
return $poly;
}
=item $metrics = $img->fontMetrics
=item ($metrics,$width,$height) = GD::Simple->fontMetrics($font,$fontsize,$string)
This method returns information about the current font, most commonly
a TrueType font. It can be invoked as an instance method (on a
previously-created GD::Simple object) or as a class method (on the
'GD::Simple' class).
When called as an instance method, fontMetrics() takes no arguments
and returns a single hash reference containing the metrics that
describe the currently selected font and size. The hash reference
contains the following information:
xheight the base height of the font from the bottom to the top of
a lowercase 'm'
ascent the length of the upper stem of the lowercase 'd'
descent the length of the lower step of the lowercase 'j'
lineheight the distance from the bottom of the 'j' to the top of
the 'd'
leading the distance between two adjacent lines
=cut
# return %$fontmetrics
# keys: 'ascent', 'descent', 'lineheight', 'xheight', 'leading'
sub fontMetrics {
my $self = shift;
unless (ref $self) { #class invocation -- create a scratch
$self = $self->new;
$self->font(shift) if defined $_[0];
$self->fontsize(shift) if defined $_[0];
}
my $font = $self->font;
my $metrics;
if (ref $font && $font->isa('GD::Font')) {
my $height = $font->height;
$metrics = {ascent => 0,
descent => 0,
lineheight => $height,
xheight => $height,
leading => 0};
}
else {
$self->useFontConfig(1);
my @mbounds = GD::Image->stringFT($self->fgcolor,$font,
$self->fontsize,0,
0,0,'m');
my $xheight = $mbounds[3]-$mbounds[5];
my @jbounds = GD::Image->stringFT($self->fgcolor,$font,
$self->fontsize,0,
0,0,'j');
my $ascent = $mbounds[7]-$jbounds[7];
my $descent = $jbounds[3]-$mbounds[3];
my @mmbounds = GD::Image->stringFT($self->fgcolor,$font,
$self->fontsize,0,
0,0,"m\nm");
my $twolines = $mmbounds[3]-$mmbounds[5];
my $lineheight = $twolines - 2*$xheight;
my $leading = $lineheight - $ascent - $descent;
$metrics = {ascent => $ascent,
descent => $descent,
lineheight => $lineheight,
xheight => $xheight,
leading => $leading};
}
if ((my $string = shift) && wantarray) {
my ($width,$height) = $self->stringBounds($string);
return ($metrics,abs($width),abs($height));
}
return $metrics;
}
=item ($delta_x,$delta_y)= $img->stringBounds($string)
This method indicates the X and Y offsets (which may be negative) that
will occur when the given string is drawn using the current font,
fontsize and angle. When the string is drawn horizontally, it gives
the width and height of the string's bounding box.
=cut
sub stringBounds {
my $self = shift;
my $string = shift;
my $font = $self->font;
if (ref $font && $font->isa('GD::Font')) {
if ($self->angle == -90) {
return ($font->height,-length($string) * $font->width);
} else {
return (length($string) * $font->width,$font->height);
}
}
else {
$self->useFontConfig(1);
my @bounds = GD::Image->stringFT($self->fgcolor,$font,
$self->fontsize,-deg2rad($self->angle),
$self->curPos,$string);
return $self->_string_width(@bounds);
}
}
=item $delta_x = $img->stringWidth($string)
This method indicates the width of the string given the current font,
fontsize and angle. It is the same as ($img->stringBounds($string))[0]
=cut
sub stringWidth {
return ((shift->stringBounds(@_))[0]);
}
sub _string_width {
my $self = shift;
my @bounds = @_;
my $delta_x = abs($bounds[2]-$bounds[0]);
my $delta_y = abs($bounds[5]-$bounds[3]);
my $angle = $self->angle % 360;
if ($angle >= 0 && $angle < 90) {
return ($delta_x,$delta_y);
} elsif ($angle >= 90 && $angle < 180) {
return (-$delta_x,$delta_y);
} elsif ($angle >= 180 && $angle < 270) {
return (-$delta_x,-$delta_y);
} elsif ($angle >= 270 && $angle < 360) {
return ($delta_x,-$delta_y);
}
}
=item ($x,$y) = $img->curPos
Return the current position of the pen. Set the current position
using moveTo().
=cut
sub curPos { @{shift->{xy}}; }
=item $font = $img->font([$newfont] [,$newsize])
Get or set the current font. Fonts can be GD::Font objects, TrueType
font file paths, or fontconfig font patterns like "Times:italic" (see
L). The latter feature requires that you have the
fontconfig library installed and are using libgd version 2.0.33 or
higher.
As a shortcut, you may pass two arguments to set the font and the
fontsize simultaneously. The fontsize is only valid when drawing with
TrueType fonts.
=cut
sub font {
my $self = shift;
$self->{font} = shift if @_;
$self->{fontsize} = shift if @_;
$self->{font};
}
=item $size = $img->fontsize([$newfontsize])
Get or set the current font size. This is only valid for TrueType
fonts.
=cut
sub fontsize {
my $self = shift;
$self->{fontsize} = shift if @_;
$self->{fontsize};
}
=item $size = $img->penSize([$newpensize])
Get or set the current pen width for use during line drawing
operations.
=cut
sub penSize {
my $self = shift;
if (@_) {
$self->{pensize} = shift;
$self->gd->setThickness($self->{pensize});
}
$self->{pensize};
}
=item $angle = $img->angle([$newangle])
Set the current angle for use when calling line() or move() with a
single argument.
Here is an example of using turn() and angle() together to draw an
octagon. The first line drawn is the downward-slanting top right
edge. The last line drawn is the horizontal top of the octagon.
$img->moveTo(200,50);
$img->angle(0);
$img->turn(360/8);
for (1..8) { $img->line(50) }
=cut
sub angle {
my $self = shift;
$self->{angle} = shift if @_;
$self->{angle};
}
=item $angle = $img->turn([$newangle])
Get or set the current angle to turn prior to drawing lines. This
value is only used when calling line() or move() with a single
argument. The turning angle will be applied to each call to line() or
move() just before the actual drawing occurs.
Angles are in degrees. Positive values turn the angle clockwise.
=cut
# degrees, not radians
sub turn {
my $self = shift;
$self->{turningangle} = shift if @_;
$self->{turningangle};
}
=item $color = $img->fgcolor([$newcolor])
Get or set the pen's foreground color. The current pen color can be
set by (1) using an (r,g,b) triple; (2) using a previously-allocated
color from the GD palette; or (3) by using a symbolic color name such
as "chartreuse." The list of color names can be obtained using
color_names(). The special color name 'transparent' will create a
completely transparent color.
=cut
sub fgcolor {
my $self = shift;
$self->{fgcolor} = $self->translate_color(@_) if @_;
$self->{fgcolor};
}
=item $color = $img->bgcolor([$newcolor])
Get or set the pen's background color. The current pen color can be
set by (1) using an (r,g,b) triple; (2) using a previously-allocated
color from the GD palette; or (3) by using a symbolic color name such
as "chartreuse." The list of color names can be obtained using
color_names(). The special color name 'transparent' will create a
completely transparent color.
=cut
sub bgcolor {
my $self = shift;
$self->{bgcolor} = $self->translate_color(@_) if @_;
$self->{bgcolor};
}
=item $index = $img->translate_color(@args)
Translates a color into a GD palette or TrueColor index. You may pass
either an (r,g,b) triple or a symbolic color name. If you pass a
previously-allocated index, the method will return it unchanged.
=cut
sub translate_color {
my $self = shift;
return unless defined $_[0];
my ($r,$g,$b);
if (@_ == 1 && $_[0] =~ /^-?\d+/) { # previously allocated index
return $_[0];
}
elsif (@_ == 3) { # (rgb triplet)
($r,$g,$b) = @_;
}
elsif (lc $_[0] eq 'transparent') {
return $TRANSPARENT ||= $self->alphaColor('white',127);
}
else {
$self->read_color_table unless %COLORS;
die "unknown color" unless exists $COLORS{lc $_[0]};
($r,$g,$b) = @{$COLORS{lc $_[0]}};
}
return $self->colorResolve($r,$g,$b);
}
sub transparent {
my $self = shift;
my $index = $self->translate_color(@_);
$self->gd->transparent($index);
}
=item $index = $img->alphaColor(@args,$alpha)
Creates an alpha color. You may pass either an (r,g,b) triple or a
symbolic color name, followed by an integer indicating its
opacity. The opacity value ranges from 0 (fully opaque) to 127 (fully
transparent).
=cut
sub alphaColor {
my $self = shift;
return unless defined $_[0];
my ($r,$g,$b,$a);
if (@_ == 4) { # (rgb triplet)
($r,$g,$b,$a) = @_;
} else {
$self->read_color_table unless %COLORS;
die "unknown color" unless exists $COLORS{lc $_[0]};
($r,$g,$b) = @{$COLORS{lc $_[0]}};
$a = $_[1];
}
return $self->colorAllocateAlpha($r,$g,$b,$a);
}
=item @names = GD::Simple->color_names
=item $translate_table = GD::Simple->color_names
Called in a list context, color_names() returns the list of symbolic
color names recognized by this module. Called in a scalar context,
the method returns a hash reference in which the keys are the color
names and the values are array references containing [r,g,b] triples.
=cut
sub color_names {
my $self = shift;
$self->read_color_table unless %COLORS;
return wantarray ? sort keys %COLORS : \%COLORS;
}
=item $gd = $img->gd
Return the internal GD::Image object. Usually you will not need to
call this since all GD methods are automatically referred to this object.
=cut
sub gd { shift->{gd} }
sub read_color_table {
my $class = shift;
while () {
chomp;
last if /^__END__/;
my ($name,$r,$g,$b) = split /\s+/;
$COLORS{$name} = [hex $r,hex $g,hex $b];
}
}
sub setBrush {
my $self = shift;
my $brush = shift;
if ($brush->isa('GD::Simple')) {
$self->gd->setBrush($brush->gd);
} else {
$self->gd->setBrush($brush);
}
}
=item ($red,$green,$blue) = GD::Simple->HSVtoRGB($hue,$saturation,$value)
Convert a Hue/Saturation/Value (HSV) color into an RGB triple. The
hue, saturation and value are integers from 0 to 255.
=cut
sub HSVtoRGB {
my $self = shift;
@_ == 3 or croak "Usage: GD::Simple->HSVtoRGB(\$hue,\$saturation,\$value)";
my ($h,$s,$v)=@_;
my ($r,$g,$b,$i,$f,$p,$q,$t);
if( $s == 0 ) {
## achromatic (grey)
return ($v,$v,$v);
}
$h %= 255;
$s /= 255; ## scale saturation from 0.0-1.0
$h /= 255; ## scale hue from 0 to 1.0
$h *= 360; ## and now scale it to 0 to 360
$h /= 60; ## sector 0 to 5
$i = $h % 6;
$f = $h - $i; ## factorial part of h
$p = $v * ( 1 - $s );
$q = $v * ( 1 - $s * $f );
$t = $v * ( 1 - $s * ( 1 - $f ) );
if($i<1) {
$r = $v;
$g = $t;
$b = $p;
} elsif($i<2){
$r = $q;
$g = $v;
$b = $p;
} elsif($i<3){
$r = $p;
$g = $v;
$b = $t;
} elsif($i<4){
$r = $p;
$g = $q;
$b = $v;
} elsif($i<5){
$r = $t;
$g = $p;
$b = $v;
} else {
$r = $v;
$g = $p;
$b = $q;
}
return (int($r+0.5),int($g+0.5),int($b+0.5));
}
=item ($hue,$saturation,$value) = GD::Simple->RGBtoHSV($hue,$saturation,$value)
Convert a Red/Green/Blue (RGB) value into a Hue/Saturation/Value (HSV)
triple. The hue, saturation and value are integers from 0 to 255.
=back
=cut
sub RGBtoHSV {
my $self = shift;
my ($r, $g ,$bl) = @_;
my ($min,undef,$max) = sort {$a<=>$b} ($r,$g,$bl);
return (0,0,0) unless $max > 0;
my $v = $max;
my $s = 255 * ($max - $min)/$max;
my $h;
my $range = $max - $min;
if ($range == 0) { # all colors are equal, so monochrome
return (0,0,$max);
}
if ($max == $r) {
$h = 60 * ($g-$bl)/$range;
}
elsif ($max == $g) {
$h = 60 * ($bl-$r)/$range + 120;
}
else {
$h = 60 * ($r-$g)/$range + 240;
}
$h = int($h*255/360 + 0.5);
return ($h, $s, $v);
}
sub newGroup {
my $self = shift;
return $self->GD::newGroup(@_);
}
1;
__DATA__
white FF FF FF
black 00 00 00
aliceblue F0 F8 FF
antiquewhite FA EB D7
aqua 00 FF FF
aquamarine 7F FF D4
azure F0 FF FF
beige F5 F5 DC
bisque FF E4 C4
blanchedalmond FF EB CD
blue 00 00 FF
blueviolet 8A 2B E2
brown A5 2A 2A
burlywood DE B8 87
cadetblue 5F 9E A0
chartreuse 7F FF 00
chocolate D2 69 1E
coral FF 7F 50
cornflowerblue 64 95 ED
cornsilk FF F8 DC
crimson DC 14 3C
cyan 00 FF FF
darkblue 00 00 8B
darkcyan 00 8B 8B
darkgoldenrod B8 86 0B
darkgray A9 A9 A9
darkgreen 00 64 00
darkkhaki BD B7 6B
darkmagenta 8B 00 8B
darkolivegreen 55 6B 2F
darkorange FF 8C 00
darkorchid 99 32 CC
darkred 8B 00 00
darksalmon E9 96 7A
darkseagreen 8F BC 8F
darkslateblue 48 3D 8B
darkslategray 2F 4F 4F
darkturquoise 00 CE D1
darkviolet 94 00 D3
deeppink FF 14 100
deepskyblue 00 BF FF
dimgray 69 69 69
dodgerblue 1E 90 FF
firebrick B2 22 22
floralwhite FF FA F0
forestgreen 22 8B 22
fuchsia FF 00 FF
gainsboro DC DC DC
ghostwhite F8 F8 FF
gold FF D7 00
goldenrod DA A5 20
gray 80 80 80
green 00 80 00
greenyellow AD FF 2F
honeydew F0 FF F0
hotpink FF 69 B4
indianred CD 5C 5C
indigo 4B 00 82
ivory FF FF F0
khaki F0 E6 8C
lavender E6 E6 FA
lavenderblush FF F0 F5
lawngreen 7C FC 00
lemonchiffon FF FA CD
lightblue AD D8 E6
lightcoral F0 80 80
lightcyan E0 FF FF
lightgoldenrodyellow FA FA D2
lightgreen 90 EE 90
lightgrey D3 D3 D3
lightpink FF B6 C1
lightsalmon FF A0 7A
lightseagreen 20 B2 AA
lightskyblue 87 CE FA
lightslategray 77 88 99
lightsteelblue B0 C4 DE
lightyellow FF FF E0
lime 00 FF 00
limegreen 32 CD 32
linen FA F0 E6
magenta FF 00 FF
maroon 80 00 00
mediumaquamarine 66 CD AA
mediumblue 00 00 CD
mediumorchid BA 55 D3
mediumpurple 100 70 DB
mediumseagreen 3C B3 71
mediumslateblue 7B 68 EE
mediumspringgreen 00 FA 9A
mediumturquoise 48 D1 CC
mediumvioletred C7 15 85
midnightblue 19 19 70
mintcream F5 FF FA
mistyrose FF E4 E1
moccasin FF E4 B5
navajowhite FF DE AD
navy 00 00 80
oldlace FD F5 E6
olive 80 80 00
olivedrab 6B 8E 23
orange FF A5 00
orangered FF 45 00
orchid DA 70 D6
palegoldenrod EE E8 AA
palegreen 98 FB 98
paleturquoise AF EE EE
palevioletred DB 70 100
papayawhip FF EF D5
peachpuff FF DA B9
peru CD 85 3F
pink FF C0 CB
plum DD A0 DD
powderblue B0 E0 E6
purple 80 00 80
red FF 00 00
rosybrown BC 8F 8F
royalblue 41 69 E1
saddlebrown 8B 45 13
salmon FA 80 72
sandybrown F4 A4 60
seagreen 2E 8B 57
seashell FF F5 EE
sienna A0 52 2D
silver C0 C0 C0
skyblue 87 CE EB
slateblue 6A 5A CD
slategray 70 80 90
snow FF FA FA
springgreen 00 FF 7F
steelblue 46 82 B4
tan D2 B4 8C
teal 00 80 80
thistle D8 BF D8
tomato FF 63 47
turquoise 40 E0 D0
violet EE 82 EE
wheat F5 DE B3
whitesmoke F5 F5 F5
yellow FF FF 00
yellowgreen 9A CD 32
gradient1 00 ff 00
gradient2 0a ff 00
gradient3 14 ff 00
gradient4 1e ff 00
gradient5 28 ff 00
gradient6 32 ff 00
gradient7 3d ff 00
gradient8 47 ff 00
gradient9 51 ff 00
gradient10 5b ff 00
gradient11 65 ff 00
gradient12 70 ff 00
gradient13 7a ff 00
gradient14 84 ff 00
gradient15 8e ff 00
gradient16 99 ff 00
gradient17 a3 ff 00
gradient18 ad ff 00
gradient19 b7 ff 00
gradient20 c1 ff 00
gradient21 cc ff 00
gradient22 d6 ff 00
gradient23 e0 ff 00
gradient24 ea ff 00
gradient25 f4 ff 00
gradient26 ff ff 00
gradient27 ff f4 00
gradient28 ff ea 00
gradient29 ff e0 00
gradient30 ff d6 00
gradient31 ff cc 00
gradient32 ff c1 00
gradient33 ff b7 00
gradient34 ff ad 00
gradient35 ff a3 00
gradient36 ff 99 00
gradient37 ff 8e 00
gradient38 ff 84 00
gradient39 ff 7a 00
gradient40 ff 70 00
gradient41 ff 65 00
gradient42 ff 5b 00
gradient43 ff 51 00
gradient44 ff 47 00
gradient45 ff 3d 00
gradient46 ff 32 00
gradient47 ff 28 00
gradient48 ff 1e 00
gradient49 ff 14 00
gradient50 ff 0a 00
__END__
=head1 COLORS
This script will create an image showing all the symbolic colors.
#!/usr/bin/perl
use strict;
use GD::Simple;
my @color_names = GD::Simple->color_names;
my $cols = int(sqrt(@color_names));
my $rows = int(@color_names/$cols)+1;
my $cell_width = 100;
my $cell_height = 50;
my $legend_height = 16;
my $width = $cols * $cell_width;
my $height = $rows * $cell_height;
my $img = GD::Simple->new($width,$height);
$img->font(gdSmallFont);
for (my $c=0; $c<$cols; $c++) {
for (my $r=0; $r<$rows; $r++) {
my $color = $color_names[$c*$rows + $r] or next;
my @topleft = ($c*$cell_width,$r*$cell_height);
my @botright = ($topleft[0]+$cell_width,$topleft[1]+$cell_height-$legend_height);
$img->bgcolor($color);
$img->fgcolor($color);
$img->rectangle(@topleft,@botright);
$img->moveTo($topleft[0]+2,$botright[1]+$legend_height-2);
$img->fgcolor('black');
$img->string($color);
}
}
print $img->png;
=head1 AUTHOR
The GD::Simple module is copyright 2004, Lincoln D. Stein. It is
distributed under the same terms as Perl itself. See the "Artistic
License" in the Perl source code distribution for licensing terms.
The latest versions of GD.pm are available at
http://stein.cshl.org/WWW/software/GD
=head1 SEE ALSO
L,
L,
L,
L
=cut
GD-2.46/GD/Image.pm.PLS 0000644 0001750 0001750 00000012656 11065554364 013647 0 ustar lstein lstein #!perl
use Config;
use File::Basename qw(&basename &dirname);
use Cwd;
my $DEFINES = '';
my $VERSION = '';
if (open F,".config.cache") {
chomp($DEFINES = );
close F;
}
$origdir = cwd;
chdir dirname($0);
$file = basename($0, '.PL','.PLS');
open OUT,">$file" or die "Can't create $file: $!";
print "Extracting $file (with variable substitutions)\n";
print OUT <<"!GROK!THIS!";
# DO NOT EDIT! THIS FILE IS AUTOGENERATED BY $0
!GROK!THIS!
print OUT << '!NO!SUBS!';
package GD::Image;
use strict;
use GD;
use Symbol 'gensym','qualify_to_ref';
use vars '$VERSION';
$VERSION = '2.38';
=head1 NAME
GD::Image - Image class for the GD image library
=head1 SYNOPSIS
See L
=head1 DESCRIPTION
See L
=head1 AUTHOR
The GD.pm interface is copyright 1995-2005, Lincoln D. Stein. It is
distributed under the same terms as Perl itself. See the "Artistic
License" in the Perl source code distribution for licensing terms.
The latest versions of GD.pm are available on CPAN:
http://www.cpan.org
=head1 SEE ALSO
L
L,
L,
L,
L
=cut
# Copyright 1995 Lincoln D. Stein. See accompanying README file for
# usage information
*stringTTF = \&GD::Image::stringFT;
sub _make_filehandle {
shift; # get rid of class
no strict 'refs';
my $thing = shift;
return $thing if defined(fileno $thing);
# otherwise try qualifying it into caller's package
my $fh;
{
local $^W = 0; # to avoid uninitialized variable warning from Symbol.pm
$fh = qualify_to_ref($thing,caller(2));
}
return $fh if defined(fileno $fh);
# otherwise treat it as a file to open
$fh = gensym;
open($fh,$thing) || return;
return $fh;
}
sub new {
my $pack = shift;
if (@_ == 1) {
if (my $type = _image_type($_[0])) {
my $method = "newFrom${type}Data";
return unless $pack->can($method);
return $pack->$method($_[0]);
}
return unless my $fh = $pack->_make_filehandle($_[0]);
my $magic;
return unless read($fh,$magic,4);
return unless my $type = _image_type($magic);
seek($fh,0,0);
my $method = "newFrom${type}";
return $pack->$method($fh);
}
return $pack->_new(@_);
}
sub newTrueColor {
my $pack = shift;
return $pack->_new(@_, 1);
}
sub newPalette {
my $pack = shift;
return $pack->_new(@_, 0);
}
sub newFromGd {
croak("Usage: newFromGd(class,filehandle)") unless @_==2;
my($class,$f) = @_;
my $fh = $class->_make_filehandle($f);
binmode($fh);
$class->_newFromGd($fh);
}
sub newFromGd2 {
croak("Usage: newFromGd2(class,filehandle)") unless @_==2;
my($class,$f) = @_;
my $fh = $class->_make_filehandle($f);
binmode($fh);
$class->_newFromGd2($fh);
}
sub newFromGd2Part {
croak("Usage: newFromGd2(class,filehandle,srcX,srcY,width,height)") unless @_==6;
my($class,$f) = splice(@_,0,2);
my $fh = $class->_make_filehandle($f);
binmode($fh);
$class->_newFromGd2Part($fh,@_);
}
sub ellipse ($$$$$) {
my ($self,$cx,$cy,$width,$height,$color) = @_;
$self->arc($cx,$cy,$width,$height,0,360,$color);
}
# draws closed polygon with the specified color
sub polygon {
my $self = shift;
my($p,$c) = @_;
$self->openPolygon($p, $c);
$self->line( @{$p->{'points'}->[0]},
@{$p->{'points'}->[$p->{'length'}-1]}, $c);
}
sub width {
my $self = shift;
my @bounds = $self->getBounds;
$bounds[0];
}
sub height {
my $self = shift;
my @bounds = $self->getBounds;
$bounds[1];
}
sub _image_type {
my $data = shift;
my $magic = substr($data,0,4);
return 'Png' if $magic eq "\x89PNG";
return 'Jpeg' if $magic eq "\377\330\377\340";
return 'Jpeg' if $magic eq "\377\330\377\341";
return 'Jpeg' if $magic eq "\377\330\377\355";
return 'Jpeg' if $magic eq "\377\330\377\356";
return 'Gif' if $magic eq "GIF8";
return 'Gd2' if $magic eq "gd2\000";
return 'Xpm' if substr($data,0,9) eq "/* XPM */";
return;
}
sub clone {
croak("Usage: clone(\$image)") unless @_ == 1;
my $self = shift;
my ($x,$y) = $self->getBounds;
my $new = $self->new($x,$y);
return unless $new;
$new->copy($self,0,0,0,0,$x,$y);
return $new;
}
!NO!SUBS!
if ($DEFINES =~ /HAVE_PNG/) {
print OUT <<'!NO!SUBS!'
sub newFromPng {
croak("Usage: newFromPng(class,filehandle,[truecolor])") unless @_>=2;
my($class) = shift;
my($f) = shift;
my $fh = $class->_make_filehandle($f);
binmode($fh);
$class->_newFromPng($fh,@_);
}
!NO!SUBS!
}
if ($DEFINES =~ /HAVE_JPEG/) {
print OUT <<'!NO!SUBS!'
sub newFromJpeg {
croak("Usage: newFromJpeg(class,filehandle,[truecolor])") unless @_>=2;
my($class) = shift;
my($f) = shift;
my $fh = $class->_make_filehandle($f);
binmode($fh);
$class->_newFromJpeg($fh,@_);
}
!NO!SUBS!
}
if ($DEFINES =~ /HAVE_GIF/) {
print OUT <<'!NO!SUBS!'
sub newFromGif {
croak("Usage: newFromGif(class,filehandle,[truecolor])") unless @_>=2;
my($class) = shift;
my($f) = shift;
my $fh = $class->_make_filehandle($f);
binmode($fh);
$class->_newFromGif($fh,@_);
}
!NO!SUBS!
}
if ($DEFINES =~ /HAVE_DBM/) {
print OUT <<'!NO!SUBS!'
sub newFromXbm {
croak("Usage: newFromXbm(class,filehandle)") unless @_==2;
my($class,$f) = @_;
my $fh = $class->_make_filehandle($f);
binmode($fh);
$class->_newFromXbm($fh);
}
!NO!SUBS!
}
print OUT <<'!NO!SUBS!';
# Autoload methods go after __END__, and are processed by the autosplit program.
1;
__END__
!NO!SUBS!
close OUT or die "Can't close $file: $!";
chdir $origdir;
GD-2.46/GD/Polyline.pm 0000644 0001750 0001750 00000053723 10535374276 013766 0 ustar lstein lstein ############################################################################
#
# Polyline.pm
#
# Author: Dan Harasty
# Email: harasty@cpan.org
# Version: 0.2
# Date: 2002/08/06
#
# For usage documentation: see POD at end of file
#
# For changes: see "Changes" file included with distribution
#
use strict;
package GD::Polyline;
############################################################################
#
# GD::Polyline
#
############################################################################
#
# What's this? A class with nothing but a $VERSION and and @ISA?
# Below, this module overrides and adds several modules to
# the parent class, GD::Polygon. Those updated/new methods
# act on polygons and polylines, and sometimes those behaviours
# vary slightly based on whether the object is a polygon or polyine.
#
use vars qw($VERSION @ISA);
$VERSION = "0.2";
@ISA = qw(GD::Polygon);
package GD::Polygon;
############################################################################
#
# new methods on GD::Polygon
#
############################################################################
use GD;
use Carp 'croak','carp';
use vars qw($bezSegs $csr);
$bezSegs = 20; # number of bezier segs -- number of segments in each portion of the spline produces by toSpline()
$csr = 1/3; # control seg ratio -- the one possibly user-tunable parameter in the addControlPoints() algorithm
sub rotate {
my ($self, $angle, $cx, $cy) = @_;
$self->offset(-$cx,-$cy) if $cx or $cy;
$self->transform(cos($angle),sin($angle),-sin($angle),cos($angle),$cx,$cy);
}
sub centroid {
my ($self, $scale) = @_;
my ($cx,$cy);
$scale = 1 unless defined $scale;
map {$cx += $_->[0]; $cy += $_->[1]} $self->vertices();
$cx *= $scale / $self->length();
$cy *= $scale / $self->length();
return ($cx, $cy);
}
sub segLength {
my $self = shift;
my @points = $self->vertices();
my ($p1, $p2, @segLengths);
$p1 = shift @points;
# put the first vertex on the end to "close" a polygon, but not a polyline
push @points, $p1 unless $self->isa('GD::Polyline');
while ($p2 = shift @points) {
push @segLengths, _len($p1, $p2);
$p1 = $p2;
}
return @segLengths if wantarray;
my $sum;
map {$sum += $_} @segLengths;
return $sum;
}
sub segAngle {
my $self = shift;
my @points = $self->vertices();
my ($p1, $p2, @segAngles);
$p1 = shift @points;
# put the first vertex on the end to "close" a polygon, but not a polyline
push @points, $p1 unless $self->isa('GD::Polyline');
while ($p2 = shift @points) {
push @segAngles, _angle_reduce2(_angle($p1, $p2));
$p1 = $p2;
}
return @segAngles;
}
sub vertexAngle {
my $self = shift;
my @points = $self->vertices();
my ($p1, $p2, $p3, @vertexAngle);
$p1 = $points[$#points]; # last vertex
$p2 = shift @points; # current point -- the first vertex
# put the first vertex on the end to "close" a polygon, but not a polyline
push @points, $p2 unless $self->isa('GD::Polyline');
while ($p3 = shift @points) {
push @vertexAngle, _angle_reduce2(_angle($p1, $p2, $p3));
($p1, $p2) = ($p2, $p3);
}
$vertexAngle[0] = undef if defined $vertexAngle[0] and $self->isa("GD::Polyline");
return @vertexAngle if wantarray;
}
sub toSpline {
my $self = shift;
my @points = $self->vertices();
# put the first vertex on the end to "close" a polygon, but not a polyline
push @points, [$self->getPt(0)] unless $self->isa('GD::Polyline');
unless (@points > 1 and @points % 3 == 1) {
carp "Attempt to call toSpline() with invalid set of control points";
return undef;
}
my ($ap1, $dp1, $dp2, $ap2); # ap = anchor point, dp = director point
$ap1 = shift @points;
my $bez = new ref($self);
$bez->addPt(@$ap1);
while (@points) {
($dp1, $dp2, $ap2) = splice(@points, 0, 3);
for (1..$bezSegs) {
my ($t0, $t1, $c1, $c2, $c3, $c4, $x, $y);
$t1 = $_/$bezSegs;
$t0 = (1 - $t1);
# possible optimization:
# these coefficient could be calculated just once and
# cached in an array for a given value of $bezSegs
$c1 = $t0 * $t0 * $t0;
$c2 = 3 * $t0 * $t0 * $t1;
$c3 = 3 * $t0 * $t1 * $t1;
$c4 = $t1 * $t1 * $t1;
$x = $c1 * $ap1->[0] + $c2 * $dp1->[0] + $c3 * $dp2->[0] + $c4 * $ap2->[0];
$y = $c1 * $ap1->[1] + $c2 * $dp1->[1] + $c3 * $dp2->[1] + $c4 * $ap2->[1];
$bez->addPt($x, $y);
}
$ap1 = $ap2;
}
# remove the last anchor point if this is a polygon -- since it will autoclose without it
$bez->deletePt($bez->length()-1) unless $self->isa('GD::Polyline');
return $bez;
}
sub addControlPoints {
my $self = shift;
my @points = $self->vertices();
unless (@points > 1) {
carp "Attempt to call addControlPoints() with too few vertices in polyline";
return undef;
}
my $points = scalar(@points);
my @segAngles = $self->segAngle();
my @segLengths = $self->segLength();
my ($prevLen, $nextLen, $prevAngle, $thisAngle, $nextAngle);
my ($controlSeg, $pt, $ptX, $ptY, @controlSegs);
# this loop goes about creating polylines -- here called control segments --
# that hold the control points for the final set of control points
# each control segment has three points, and these are colinear
# the first and last will ultimately be "director points", and
# the middle point will ultimately be an "anchor point"
for my $i (0..$#points) {
$controlSeg = new GD::Polyline;
$pt = $points[$i];
($ptX, $ptY) = @$pt;
if ($self->isa('GD::Polyline') and ($i == 0 or $i == $#points)) {
$controlSeg->addPt($ptX, $ptY); # director point
$controlSeg->addPt($ptX, $ptY); # anchor point
$controlSeg->addPt($ptX, $ptY); # director point
next;
}
$prevLen = $segLengths[$i-1];
$nextLen = $segLengths[$i];
$prevAngle = $segAngles[$i-1];
$nextAngle = $segAngles[$i];
# make a control segment with control points (director points)
# before and after the point from the polyline (anchor point)
$controlSeg->addPt($ptX - $csr * $prevLen, $ptY); # director point
$controlSeg->addPt($ptX , $ptY); # anchor point
$controlSeg->addPt($ptX + $csr * $nextLen, $ptY); # director point
# note that:
# - the line is parallel to the x-axis, as the points have a common $ptY
# - the points are thus clearly colinear
# - the director point is a distance away from the anchor point in proportion to the length of the segment it faces
# now, we must come up with a reasonable angle for the control seg
# first, "unwrap" $nextAngle w.r.t. $prevAngle
$nextAngle -= 2*pi() until $nextAngle < $prevAngle + pi();
$nextAngle += 2*pi() until $nextAngle > $prevAngle - pi();
# next, use seg lengths as an inverse weighted average
# to "tip" the control segment toward the *shorter* segment
$thisAngle = ($nextAngle * $prevLen + $prevAngle * $nextLen) / ($prevLen + $nextLen);
# rotate the control segment to $thisAngle about it's anchor point
$controlSeg->rotate($thisAngle, $ptX, $ptY);
} continue {
# save the control segment for later
push @controlSegs, $controlSeg;
}
# post process
my $controlPoly = new ref($self);
# collect all the control segments' points in to a single control poly
foreach my $cs (@controlSegs) {
foreach my $pt ($cs->vertices()) {
$controlPoly->addPt(@$pt);
}
}
# final clean up based on poly type
if ($controlPoly->isa('GD::Polyline')) {
# remove the first and last control point
# since they are director points ...
$controlPoly->deletePt(0);
$controlPoly->deletePt($controlPoly->length()-1);
} else {
# move the first control point to the last control point
# since it is supposed to end with two director points ...
$controlPoly->addPt($controlPoly->getPt(0));
$controlPoly->deletePt(0);
}
return $controlPoly;
}
# The following helper functions are for internal
# use of this module. Input arguments of "points"
# refer to an array ref of two numbers, [$x, $y]
# as is used internally in the GD::Polygon
#
# _len()
# Find the length of a segment, passing in two points.
# Internal function; NOT a class or object method.
#
sub _len {
# my ($p1, $p2) = @_;
# return sqrt(($p2->[0]-$p1->[0])**2 + ($p2->[1]-$p1->[1])**2);
my $pt = _subtract(@_);
return sqrt($pt->[0] ** 2 + $pt->[1] **2);
}
use Math::Trig;
# _angle()
# Find the angle of... well, depends on the number of arguments:
# - one point: the angle from x-axis to the point (origin is the center)
# - two points: the angle of the vector defined from point1 to point2
# - three points:
# Internal function; NOT a class or object method.
#
sub _angle {
my ($p1, $p2, $p3) = @_;
my $angle = undef;
if (@_ == 1) {
return atan2($p1->[1], $p1->[0]);
}
if (@_ == 2) {
return _angle(_subtract($p1, $p2));
}
if (@_ == 3) {
return _angle(_subtract($p2, $p3)) - _angle(_subtract($p2, $p1));
}
}
# _subtract()
# Find the difference of two points; returns a point.
# Internal function; NOT a class or object method.
#
sub _subtract {
my ($p1, $p2) = @_;
# print(_print_point($p2), "-", _print_point($p1), "\n");
return [$p2->[0]-$p1->[0], $p2->[1]-$p1->[1]];
}
# _print_point()
# Returns a string suitable for displaying the value of a point.
# Internal function; NOT a class or object method.
#
sub _print_point {
my ($p1) = @_;
return "[" . join(", ", @$p1) . "]";
}
# _angle_reduce1()
# "unwraps" angle to interval -pi < angle <= +pi
# Internal function; NOT a class or object method.
#
sub _angle_reduce1 {
my ($angle) = @_;
$angle += 2 * pi() while $angle <= -pi();
$angle -= 2 * pi() while $angle > pi();
return $angle;
}
# _angle_reduce2()
# "unwraps" angle to interval 0 <= angle < 2 * pi
# Internal function; NOT a class or object method.
#
sub _angle_reduce2 {
my ($angle) = @_;
$angle += 2 * pi() while $angle < 0;
$angle -= 2 * pi() while $angle >= 2 * pi();
return $angle;
}
############################################################################
#
# new methods on GD::Image
#
############################################################################
sub GD::Image::polyline {
my $self = shift; # the GD::Image
my $p = shift; # the GD::Polyline (or GD::Polygon)
my $c = shift; # the color
my @points = $p->vertices();
my $p1 = shift @points;
my $p2;
while ($p2 = shift @points) {
$self->line(@$p1, @$p2, $c);
$p1 = $p2;
}
}
sub GD::Image::polydraw {
my $self = shift; # the GD::Image
my $p = shift; # the GD::Polyline or GD::Polygon
my $c = shift; # the color
return $self->polyline($p, $c) if $p->isa('GD::Polyline');
return $self->polygon($p, $c);
}
1;
__END__
=pod
=head1 NAME
GD::Polyline - Polyline object and Polygon utilities (including splines) for use with GD
=head1 SYNOPSIS
use GD;
use GD::Polyline;
# create an image
$image = new GD::Image (500,300);
$white = $image->colorAllocate(255,255,255);
$black = $image->colorAllocate( 0, 0, 0);
$red = $image->colorAllocate(255, 0, 0);
# create a new polyline
$polyline = new GD::Polyline;
# add some points
$polyline->addPt( 0, 0);
$polyline->addPt( 0,100);
$polyline->addPt( 50,125);
$polyline->addPt(100, 0);
# polylines can use polygon methods (and vice versa)
$polyline->offset(200,100);
# rotate 60 degrees, about the centroid
$polyline->rotate(3.14159/3, $polyline->centroid());
# scale about the centroid
$polyline->scale(1.5, 2, $polyline->centroid());
# draw the polyline
$image->polydraw($polyline,$black);
# create a spline, which is also a polyine
$spline = $polyline->addControlPoints->toSpline;
$image->polydraw($spline,$red);
# output the png
binmode STDOUT;
print $image->png;
=head1 DESCRIPTION
B extends the GD module by allowing you to create polylines. Think
of a polyline as "an open polygon", that is, the last vertex is not connected
to the first vertex (unless you expressly add the same value as both points).
For the remainder of this doc, "polyline" will refer to a GD::Polyline,
"polygon" will refer to a GD::Polygon that is not a polyline, and
"polything" and "$poly" may be either.
The big feature added to GD by this module is the means
to create splines, which are approximations to curves.
=head1 The Polyline Object
GD::Polyline defines the following class:
=over 5
=item C
A polyline object, used for storing lists of vertices prior to
rendering a polyline into an image.
=item C
Cnew> I
Create an empty polyline with no vertices.
$polyline = new GD::Polyline;
$polyline->addPt( 0, 0);
$polyline->addPt( 0,100);
$polyline->addPt( 50,100);
$polyline->addPt(100, 0);
$image->polydraw($polyline,$black);
In fact GD::Polyline is a subclass of GD::Polygon,
so all polygon methods (such as B and B)
may be used on polylines.
Some new methods have thus been added to GD::Polygon (such as B)
and a few updated/modified/enhanced (such as B) I.
See section "New or Updated GD::Polygon Methods" for more info.
=back
Note that this module is very "young" and should be
considered subject to change in future releases, and/or
possibly folded in to the existing polygon object and/or GD module.
=head1 Updated Polygon Methods
The following methods (defined in GD.pm) are OVERRIDDEN if you use this module.
All effort has been made to provide 100% backward compatibility, but if you
can confirm that has not been achieved, please consider that a bug and let the
the author of Polyline.pm know.
=over 5
=item C
C<$poly-Escale($sx, $sy, $cx, $cy)> I
" . genHTMLTable([$summary_table], 1));
}
if (1) {
$image = NewImage();
$offset = 50;
for $poly (new GD::Polygon, new GD::Polyline) {
$poly->addPt( 0, 0);
$poly->addPt( 0,100);
$poly->addPt( 50,125);
$poly->addPt(100, 0);
$poly->offset(50 + $offset,80);
$offset += 200;
# draw the original poly
$image->polydraw($poly,$green);
# create and draw the control line for the spline
$ctrlline = $poly->addControlPoints();
$image->polydraw($ctrlline,$cyan);
# create and draw the spline itself
$spline = $ctrlline->toSpline();
$image->polydraw($spline,$black);
}
SampleImage($image, "polyline-spline.png", "Spline", "Splines fit to vertices of polygon and polyline. $splinekey");
}
if (1) {
$image = NewImage();
$triangle = new GD::Polygon;
$triangle->addPt( 0, 0);
$triangle->addPt(-19, 95);
$triangle->addPt( 19, 95);
$triangle->offset(250,50);
foreach (1..9) {
$image->polydraw($triangle,gdBrushed);
$triangle->rotate($TWO_PI / 9, 250, 150);
}
SampleImage($image, "polyline-star9.png", "Nine Pointed Star", "A triangle, rotated about a point other than the origin. Demonstration of \$poly->rotate() and \$poly->offset()");
}
if (1) {
$image = NewImage();
$cloverControl = new GD::Polyline;
$cloverControl->addPt(45,45);
$cloverControl->addPt(10,10);
$cloverControl->addPt(90,10);
$cloverControl->addPt(55,45);
$cloverControl->addPt(90,10);
$cloverControl->addPt(90,90);
$cloverControl->addPt(55,55);
$cloverControl->addPt(90,90);
$cloverControl->addPt(10,90);
$cloverControl->addPt(45,55);
$cloverControl->addPt(10,90);
$cloverControl->addPt(10,10);
$cloverControl->addPt(45,45);
$clover = $cloverControl->toSpline();
# note that the three following transformations
# could have been called on $cloverControl, instead,
# followed by the above call
$clover->offset($clover->centroid(-1));
$clover->scale(3, 3);
$clover->offset(250, 150);
$image->filledPolygon($clover,$green);
SampleImage($image, "polyline-clover.png", "Clover", "Sample image generated by GD::Polygon");
}
if (1) {
$image = NewImage();
$polyline = new GD::Polyline;
for (0..15) {
$polyline->addPt(30 * $_ + 10, rand(90) + 5);
}
$image->polyline($polyline,$green);
$ctrlline = $polyline->addControlPoints();
$ctrlline->offset(0,100);
$image->polyline($ctrlline,$cyan);
$spline = $ctrlline->toSpline();
$spline->offset(0,100);
$image->polyline($spline,$black);
SampleImage($image, "polyline-zigzag.png", "Zigzag", "Spline fit to random function. $splinekey");
}
if (1) {
$image = NewImage();
$ring_network = new GD::Polygon;
$num_nodes = 10;
$randfactor = 80;
for (1..$num_nodes) {
$x = 250 + 150 * cos($TWO_PI * $_/$num_nodes);
$y = 150 + 100 * sin($TWO_PI * $_/$num_nodes);
$x += rand($randfactor)-$randfactor/2;
$y += rand($randfactor)-$randfactor/2;
$ring_network->addPt($x, $y);
}
$image->setBrush($brush2);
$image->polyline($ring_network->addControlPoints->toSpline,gdBrushed);
$ring_node = new GD::Polygon;
$ring_node->addPt( 0, 0);
$ring_node->addPt(10, 0);
$ring_node->addPt(10,10);
$ring_node->addPt( 0,10);
for $ring_vertex ($ring_network->vertices()) {
$ring_node->offset($ring_node->centroid(-1));
$ring_node->offset(@$ring_vertex);
$image->filledPolygon($ring_node,$grey);
}
SampleImage($image, "polyline-ring-network.png", "Ring Network", "Closed spline fit to nodes at somewhat random positions.");
}
WriteToFile("polyline-example.html", theHTML());
print "\n";
print "open 'polyline-example.html' in your favorite browser that supports PNG.\n";
print "\n";
print "done! " . localtime() . "\n";
##########################
#
# helper functions
#
sub NewImage {
$image = new GD::Image (500,300);
$white = $image->colorAllocate(255,255,255);
$black = $image->colorAllocate( 0, 0, 0);
$grey = $image->colorAllocate(128,128,128);
$red = $image->colorAllocate(255, 0, 0);
$orange = $image->colorAllocate(255,196, 0);
$green = $image->colorAllocate( 0,255, 0);
$blue = $image->colorAllocate( 0, 0,255);
$cyan = $image->colorAllocate( 0,255,255);
$purple = $image->colorAllocate(206, 0,165);
$brush_width = 2;
$brush_color = [255,128,0];
$brush = new GD::Image($brush_width,$brush_width);
$brush->transparent($brush->colorAllocate(255,255,255));
$brush->filledRectangle(0,0,$brush_width,$brush_width,$brush->colorAllocate(@$brush_color));
$brush1 = $brush;
$brush_width = 3;
$brush_color = [206,0,165];
$brush = new GD::Image($brush_width,$brush_width);
$brush->transparent($brush->colorAllocate(255,255,255));
$brush->filledRectangle(0,0,$brush_width,$brush_width,$brush->colorAllocate(@$brush_color));
$brush2 = $brush;
$image->setBrush($brush1);
$image;
}
my $html;
sub SampleImage {
my $image = shift;
my $file = shift;
my $title = shift;
my $text = shift;
WriteToBinaryFile($file, $image->png());
$html .= " \n";
$html .= "$title - $file \n";
$html .= "
$text
\n";
}
sub theHTML {
$html;
}
sub WriteToFile {
my $file = shift || return 0;
my $contents = shift || "";
open (NEWFILE, ">" . $file) or die "couldn't write to file $file";
print NEWFILE $contents;
close(NEWFILE);
print "created file $file\n";
}
sub WriteToBinaryFile {
my $file = shift || return 0;
my $contents = shift || "";
open (NEWFILE, ">" . $file) or die "couldn't write to file $file";
binmode NEWFILE;
print NEWFILE $contents;
close(NEWFILE);
print "created file $file\n";
}
sub genHTMLTable {
my $array_of_arrays = shift;
my $border = shift;
my $html_table;
$html_table .= "
";
for my $array_of_items (@$array_of_arrays) {
$html_table .= "
";
$html_table .= join("
", @$array_of_items);
$html_table .= "
";
}
$html_table .= "
";
$html_table;
}
GD-2.46/demos/fills.pl 0000755 0001750 0001750 00000001124 07403777175 014114 0 ustar lstein lstein #!/usr/local/bin/perl
use GD;
$im = new GD::Image(100,50);
# allocate black -- this will be our background
$black = $im->colorAllocate(0, 0, 0);
# allocate white
$white = $im->colorAllocate(255, 255, 255);
# allocate red
$red = $im->colorAllocate(255, 0, 0);
# allocate blue
$blue = $im->colorAllocate(0,0,255);
#Inscribe an ellipse in the image
$im->arc(50, 25, 98, 48, 0, 360, $white);
# Flood-fill the ellipse. Fill color is red, and will replace the
# black interior of the ellipse
$im->fill(50, 21, $red);
binmode STDOUT;
# print the image to stdout
print $im->png;
GD-2.46/demos/ttf.pl 0000755 0001750 0001750 00000001346 07403777175 013606 0 ustar lstein lstein #!/usr/local/bin/perl
use lib '../blib/lib','../blib/arch';
use GD 1.20;
use constant font => '../t/Generic.ttf';
$im = new GD::Image(400,250);
warn $GD::VERSION;
($white,$black,$red,$blue,$yellow) =
(
$im->colorAllocate(255, 255, 255),
$im->colorAllocate(0, 0, 0),
$im->colorAllocate(255, 0, 0),
$im->colorAllocate(0,0,255),
$im->colorAllocate(255,250,205)
);
$im->interlaced(1); # cool venetian blinds effect
# Some TTFs
$im->stringTTF($black,font,12.0,0.0,20,20,"Hello world!") || die $@;
$im->stringTTF($red,font,14.0,0.0,20,80,"Hello world!")|| die $@;;
$im->stringTTF($blue,font,30.0,-0.5,60,100,"Goodbye cruel world!")|| die $@;;
binmode STDOUT;
# print the image to stdout
print $im->png;
GD-2.46/demos/font_list.png 0000644 0001750 0001750 00000053076 07403777175 015167 0 ustar lstein lstein PNG
IHDR X hXC PLTE ???___///"c IDATxے0&& Ur'/%par/+L$aC$ē'=IOzғ'=Qo]*QmeVixkV"&M7-QVבm8Cd͏Q C_Ut6jo<*/onZw=o;qڀ*y_^*">H}˛#Om/8zm
hio}"ڰ_v].R/̪থ;^Z<`S羬Kțx5'2 ZCiy! QTY\}VQ^b F+<ľ&v&H_|.+zw9%xnĔ+*'2 ĕWhmG㲬Fv hUph DnpUd^cs|/?] @"R^l9AB`ӎz`TXy } `ƵpR=
{;d@ly;s
϶)>h-rH]"bYӽp
:Y%7$QwWg{T1&Y?ߧ=1LCl` ȀoyS-u!(J$@tu;gޑtjQ6D?=Ɖ
juTEb38n3Ul*YZx~~mzS_<(#KVޅ*ϒL:z)x I'&BI >: _UAHވ ؎HzwzwE ! c} nE)ªh'c" i&mA{"aqUsi!0(c
O3a2r6h?DwuTWuPQXySj^yU i1mpwgԇml0v,=64ĐyP2;+'bG_ꭼ{aG9
u|.ͯ*&$6J×:~_J{bO͓H'fOzғ;L;# DMrV^No)b:E#[*`h0
o}.fY|Чh`HS@Zh`T\MG [
fkjGi~U|)]o̮|*sձE Qs{*Ϲ^5Kix-fMEHbES1-JkL65݁z:Vv_$~J$F!;iQ%G:fgD`xm'f`(F.*pUOܣ'@~4iX 5?֯?(`DM1,ۋ>j"}*cseMS6M`H`q uwiX'laMuQ_JHC"f~Ofwi~$ oVmH_&5A7)}j/wHct%RR
,4|hFd?M:4ñ6Ĥa:Pr(tt0lpCþ~h{.BoZRG4AMnq7Xoq^&t˪/ B@o*r6G&~Eҗ4i}))XtRk
Kѷmۗ0Tjt#'tEd @،~G=iT9vwGHj(xb,q)+1.6TS4=zrhM8VwBd! xEAh'9v
gl+KҀ?p4fW^pR.C[aۄI[j{]3 2*^P}; ğe @jwӾݵ_,=(`wR:jX#ԫu]H¬l+ sh+R=,6Ni-,xlLo:
^|ۊg,Ċz!P3/>`xBA dc8ƓN˃1u>ݥ^.#ƴv:/Y6Btŷt\w?ٕp tLxJvi.Eyr6(b&&!*;vV7LNBwt7