637e5abd427f2993dd47dedb18a1fed9b4863523
[sdlgit/SDL_perl.git] / lib / SDL / Tool / Font.pm
1 #
2 #       SDL::Tool::Font -       format agnostic font tool
3 #
4 #       Copyright (C) 2002 David J. Goehrig
5
6 package SDL::Tool::Font;
7
8 use strict;
9 use warnings;
10 use Carp;
11
12 use SDL;
13 use SDL::Font;
14 use SDL::TTFont;
15
16 sub new {
17         my $proto = shift;
18         my $class = ref($proto) || $proto;
19         my $self = {};
20         my %option = @_;
21
22         verify (%option, qw/ -sfont -ttfont -size -fg -bg -foreground -background
23                                 -normal -bold -italic -underline / ) if $SDL::DEBUG;
24
25         if ($option{-sfont}) {
26                 $$self{-font} = new SDL::Font $option{-sfont};
27         } elsif ($option{-ttfont} || $option{-t}) {
28                 $option{-size} ||= 12;
29                 $$self{-font} = new SDL::TTFont 
30                                         -name => $option{-ttfont} || $option{-t},
31                                         -size => $option{-size} || $option{-s},
32                                         -fg => $option{-foreground} || $option{-fg} ,
33                                         -bg => $option{-background} || $option{-bg};
34                 for (qw/ normal bold italic underline / ) {
35                         if ($option{"-$_"}) {
36                                 &{"SDL::TTFont::$_"}($$self{-font});
37                         }
38                 }
39         } else {
40                 croak "SDL::Tool::Font requires either a -sfont or -ttfont";    
41         }
42         bless $self,$class;
43         $self;
44 }
45
46 sub DESTROY {
47
48 }
49
50 sub print {
51         my ($self,$surface,$x,$y,@text) = @_;
52         croak "Tool::Font::print requires a SDL::Surface\n"
53                 unless ($surface->isa('SDL::Surface'));
54         if ($$self{-font}->isa('SDL::Font')) {
55                 $$self{-font}->use();
56                 SDL::SFont::PutString( $$surface, $x, $y, join('',@text));
57         } else {
58                 $$self{-font}->print($surface,$x,$y,@text);
59         }
60 }
61
62 1;
63
64 __END__;
65
66 =pod
67
68 =head1 NAME
69
70 SDL::Tool::Font - a perl extension
71
72 =head1 DESCRIPTION
73
74 L<SDL::Tool::Font> provides a unified interface for applying
75 True Type and SFont fonts to various surfaces.
76
77 =head1 METHODS
78
79 =head2 print ( surface, x, y, text ... )
80
81 C<SDL::Tool::Font::print> print the given text on the supplied surface
82 with the upper left hand corner starting at the specified coordinates.
83
84 =head1 AUTHOR
85
86 David J. Goehrig
87
88 =head1 SEE ALSO
89
90 L<perl> L<SDL::Font> L<SDL::TTFont> L<SDL::Surface>
91
92 =cut