1
2
3=head1 NAME
4
5SDL::Font - Parrot class representing fonts in Parrot SDL
6
7=head1 SYNOPSIS
8
9    # load this library
10    load_bytecode 'SDL/Font.pir'
11
12    # create a new SDL::Font object
13    .local pmc font
14    font = new ['SDL'; 'Font']
15
16    font.'init'( 'font_file'  => 'myfont.ttf', 'point_size' => 48 )
17
18    # draw text to the screen
19    #    presuming you have an SDL::Surface, SDL::Color, and SDL::Rect here...
20    font.'draw'( 'some text', font_color, destination_surface, dest_rect )
21
22    # or render it to a surface to use later
23    font.'render_text'( 'some text', font_color )
24
25=head1 DESCRIPTION
26
27A SDL::Font object represents a TrueType font in SDL.  You can use this to draw fonts to any L<SDL::Surface>.
28
29=head1 METHODS
30
31All SDL::Font objects have the following methods:
32
33=over
34
35=cut
36
37.namespace [ 'SDL'; 'Font' ]
38
39.sub _sdl_init :load
40    .local pmc init_ttf
41    init_ttf = get_hll_global ['SDL'], '_init_ttf'
42    init_ttf()
43
44    .local pmc   font_class
45
46    newclass     font_class, ['SDL'; 'Font']
47    addattribute font_class, 'font'
48    addattribute font_class, 'size'
49
50    .return()
51.end
52
53=item init( font_args )
54
55Given a list of key-value pairs containing arguments, set the attributes of
56this font.  The valid keys are C<font_file> and C<point_size>, two strings
57containing the path to a TrueType font to load and the size of the font when
58drawn, in pixels.
59
60=cut
61
62.sub 'init' :method
63    .param string font_name :named( 'font_file'  )
64    .param int    font_size :named( 'point_size' )
65
66    .local pmc OpenFont
67    OpenFont = get_hll_global ['SDL'; 'NCI'; 'TTF'], 'OpenFont'
68
69    .local pmc font
70    font = OpenFont( font_name, font_size )
71
72    setattribute  self, 'font', font
73
74    .local pmc size_value
75    size_value = new 'Integer'
76    size_value = font_size
77    setattribute self, 'size', size_value
78
79    .return()
80.end
81
82=item draw( text_string, text_color, dest_surface, dest_rect )
83
84Given a string of text to draw, an C<SDL::Color> object representing the color
85of the text to draw, a C<SDL::Surface> to which to draw, and a C<SDL::Rect>
86representing the placement of the text within the surface, draws some text.
87
88Whew.
89
90=cut
91
92.sub draw :method
93    .param string text
94    .param pmc    color_pmc
95    .param pmc    screen
96    .param pmc    dest_rect
97
98    .local pmc font_surface
99
100    font_surface = self.'render_text'( text, color_pmc )
101
102    .local int w
103    .local int h
104    w = font_surface.'width'()
105    h = font_surface.'height'()
106
107    .local pmc rect
108    rect = new ['SDL'; 'Rect']
109
110    rect.'init'( 'x' => 0, 'y' => 0, 'height' => h, 'width' => w )
111
112    dest_rect.'height'( h )
113    dest_rect.'width'( w )
114
115    screen.'blit'( font_surface, rect, dest_rect )
116
117    .return()
118.end
119
120=item render_text( text_string, text_color )
121
122Renders a string of text of the given C<SDL::Color>.  This returns a new
123C<SDL::Surface> containing the rendered font.
124
125=cut
126
127.sub render_text :method
128    .param string text
129    .param pmc    color_pmc
130
131    .local pmc font
132    font = self.'font'()
133
134    .local pmc font_surface
135    font_surface = new ['SDL'; 'Surface']
136    font_surface.'init'( 'height' => 0, 'width' => 0 )
137
138# RNH use RenderUTF8 in preference to RenderText by default
139    .local pmc RenderUTF8_Solid
140    get_hll_global RenderUTF8_Solid, ['SDL'; 'NCI'; 'TTF'], 'RenderUTF8_Solid'
141
142    .local int color
143# RNH font routine takes color in the order rgb rather than bgr used by surface.pir hence cannot rely on color.get_integer
144    .local int component
145    .local pmc colors
146    colors = color_pmc.'color'()
147
148    component = colors['b']
149    component <<= 16
150    color = component
151
152    component = colors['g']
153    component <<= 8
154    color += component
155
156    component = colors['r']
157    color += component
158
159    .local pmc font_surface_struct
160    font_surface_struct = RenderUTF8_Solid( font, text, color )
161    font_surface.'wrap_surface'( font_surface_struct )
162
163    .return( font_surface )
164.end
165
166=item font()
167
168Returns the underlying C<SDL_Font> structure this object wraps.  You should
169never need to call this directly unless you're calling SDL functions directly,
170in which case why not send me a patch?
171
172=cut
173
174.sub font :method
175    .local pmc font
176    getattribute font, self, 'font'
177
178    .return( font )
179.end
180
181=item point_size( [ new_size ] )
182
183Gets or sets the point size associated with this font object.  The single
184argument is an integer and is optional.
185
186=cut
187
188.sub point_size :method
189    .param int size      :optional
190    .param int have_size :opt_flag
191
192    .local pmc size_value
193
194    if have_size == 0 goto getter
195
196    size_value = new 'Integer'
197    size_value = size
198    setattribute self, 'size', size_value
199
200getter:
201    getattribute size_value, self, 'size'
202    size = size_value
203
204    .return( size )
205.end
206
207=back
208
209=head1 AUTHOR
210
211Please send patches, feedback, and suggestions to the Perl 6 Internals mailing
212list.
213
214=head1 COPYRIGHT
215
216Copyright (C) 2004-2008, Parrot Foundation.
217
218=cut
219
220# Local Variables:
221#   mode: pir
222#   fill-column: 100
223# End:
224# vim: expandtab shiftwidth=4 ft=pir:
225