This file is indexed.

/usr/share/perl5/GD/Text.pm is in libgd-text-perl 0.86-9.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
# $Id: Text.pm,v 1.37 2003/06/19 00:13:10 mgjv Exp $

package GD::Text;

($GD::Text::prog_version) = '$Revision: 1.37 $' =~ /\s([\d.]+)/;
$GD::Text::VERSION = '0.86';

=head1 NAME

GD::Text - Text utilities for use with GD

=head1 SYNOPSIS

  use GD;
  use GD::Text;

  my $gd_text = GD::Text->new() or die GD::Text::error();
  $gd_text->set_font('funny.ttf', 12) or die $gd_text->error;
  $gd_text->set_font(gdTinyFont);
  $gd_text->set_font(GD::Font::Tiny);
  ...
  $gd_text->set_text($string);
  my ($w, $h) = $gd_text->get('width', 'height');

  if ($gd_text->is_ttf)
  {
      ...
  }

Or alternatively

  my $gd_text = GD::Text->new(
        text => 'Some text',
        font => 'funny.ttf',
        ptsize => 14,
    );

=head1 DESCRIPTION

This module provides a font-independent way of dealing with text in
GD, for use with the GD::Text::* modules and GD::Graph.

=head1 NOTES

As with all Modules for Perl: Please stick to using the interface. If
you try to fiddle too much with knowledge of the internals of this
module, you could get burned. I may change them at any time.

You can only use TrueType fonts with version of GD > 1.20, and then
only if compiled with support for this. If you attempt to do it
anyway, you will get errors.

If you want to refer to builtin GD fonts by their short name
(C<gdTinyFont>, C<gdGiantFont>), you will need to C<use> the GD module
as well as one the GD::Text modules, because it is GD that exports
those names into your name space. If you don't like that, use the
longer alternatives (C<GD::Font->Giant>) instead.

=head1 METHODS

=cut

use strict;

use GD;
use Carp;
use Cwd;

use vars qw($FONT_PATH @FONT_PATH $OS);
BEGIN
{
    $FONT_PATH = $ENV{FONT_PATH}     || 
                 $ENV{TTF_FONT_PATH} ||
                 $ENV{TT_FONT_PATH}  || '';
    unless ($OS = $^O)
    {
        require Config;
        $OS = $Config::Config{'os_name'};
    }
}

my $ERROR;

=head2 GD::Text->new( attrib => value, ... )

Create a new object. See the C<set()> method for attributes.

=cut

sub new
{
    my $proto = shift;
    my $class = ref($proto) || $proto;
    my $self = { 
            type   => 'builtin',
            font   => gdSmallFont,
            ptsize => 10,
        };
    bless $self => $class;
    $self->set(@_) or return;
    return $self
}

=head2 GD::Text::error() or $gd_text->error();

Return the last error that occured in the class. This may be
imperfect.

=cut

# XXX This sucks! fix it
sub error { $ERROR };

sub _set_error { $ERROR = shift };

=head2 $gd_text->set_font( font, size )

Set the font to use for this string. The arguments are either a GD
builtin font (like gdSmallFont or GD::Font->Small) or the name of a
TrueType font file and the size of the font to use. See also
L<"font_path">. 

If you are not using an absolute path to the font file, you can leave of
the .ttf file extension, but you have to append it for absolute paths:

  $gd_text->set_font('arial', 12);
  # but
  $gd_text->set_font('/usr/fonts/arial.ttf', 12);

The first argument can be a reference to an array of fonts. The first
font from the array that can be found will be used. This allows you to
do something like

  $gd_text->font_path( '/usr/share/fonts:/usr/fonts');
  $gd_text->set_font(
    ['verdana', 'arial', gdMediumBoldFont], 14);

if you'd prefer verdana to be used, would be satisfied with arial, but
if none of that is available just want to make sure you can fall
back on something that will be available. 

Returns true on success, false on error.

=cut

sub set_font
{
    my $self = shift;
    my $fonts = shift;
    my $size = shift;

    # Make sure we have a reference to an array
    $fonts = [$fonts] unless ref($fonts) eq 'ARRAY';

    foreach my $font (@{$fonts})
    {
        my $rc = ref($font) && $font->isa('GD::Font') ?
            $self->_set_builtin_font($font) :
            $self->_set_TTF_font($font, $size || $self->{ptsize}) ;
        return $rc if $rc;
    }

    return;
}

sub _set_builtin_font
{
    my $self = shift;
    my $font = shift;

    $self->{type}   = 'builtin';
    $self->{font}   = $font;
    $self->{ptsize} = 0;
    $self->_recalc();
    return 1;
}

sub _find_TTF
{
    my $font = shift || return;
    local $FONT_PATH = $FONT_PATH;

    # XXX MOVE A LOT OF THIS INTO THE font_path SUB, filling the
    # @FONT_PATH array
    my ($psep, $dsep);

    if ($OS =~ /^MS(DOS|Win)/i)
    {
        # Fix backslashes
        $font =~ s#\\#/#g;
        # Check for absolute path
        $font =~ m#^([A-Za-z]:|/)# and return $font;
        $FONT_PATH =~ s#\\#/#g; # XXX move to set_font_path?
        $psep = '/';
        $dsep = ';';
    }
=pod
    elsif ($OS =~ /^MacOS/i)   
    { 
        # Check for absolute path
        $font =~ /:/ and $font !~ /^:/ and return $font;
        $psep = ':';
        $dsep = ',';
    }
    elsif ($OS =~ /^AmigaOS/i) 
    { 
        # What's an absolute path here? 
        $psep = '/';
        $dsep = ':'; # XXX ?
    }
    elsif ($OS =~ /^VMS/i)
    { 
        # What's an absolute path here? 
        $psep = '/';
        $dsep = ':';
    }
=cut
    else
    {
        # Default to Unix
        # Check for absolute path
        substr($font, 0, 1) eq '/' and return $font;
        $psep = '/';
        $dsep = ':';
    }

    # If we don't have a font path set, we look in the current directory
    # only.
    if ($FONT_PATH)
    {
        # We have a font path, and a relative path to the font file.
        # Let's see if the current directory is in the font path. If
        # not, put it at the front.
        $FONT_PATH = ".$dsep$FONT_PATH"
            unless $FONT_PATH eq '.'        || $FONT_PATH =~ /^\.$dsep/ ||
                   $FONT_PATH =~ /$dsep\.$/ || $FONT_PATH =~ /$dsep\.$dsep/;
    }
    else
    {
        # XXX what about MacOS? It doesn't work like this on MacOS.
        $FONT_PATH = '.';
    }

    # Let's search for it
    # TODO Maybe truncate base name at 8 characters for dos-like
    # installations?
    for my $path (split /$dsep/, $FONT_PATH)
    {
        # XXX Can I use File::Basename for this?
        my $file = "$path$psep$font";
        -f $file and return $file;
        # See if we can find one with an extension at the end
	for my $ext (qw/ ttf TTF /)
	{
	    -f "$file.$ext" and return "$file.$ext";
	}
    }

    return;
}

sub _set_TTF_font
{
    my $self = shift;
    my $font = shift;
    my $size = shift;

    $ERROR = "TrueType fonts require a point size", return 
        unless (defined $size && $size > 0);
    
    return unless $self->can_do_ttf;

    my $font_file = _find_TTF($font) or 
        $ERROR = "Cannot find TTF font: $font", return;

    # XXX Fix for Freetype 2.0x bug, where relative paths to a font file
    # no longer work.
    if (substr($font_file, 0, 1) eq '.')
    {
        # This is a relative path. Replace ./path/file with
        # $cwd/path/file
	my $oldpath = $ENV{PATH};
	$ENV{PATH}  = "/bin:/usr/bin"; # Keep -T happy
        require Cwd;
        substr($font_file, 0, 1) = Cwd::cwd;
	$ENV{PATH} = $oldpath;
    }

    # Check that the font exists and is a real TTF font
    my @bb = GD::Image->stringTTF(0, $font_file, $size, 0, 0, 0, "foo");
    $ERROR = "$@", return unless @bb;

    $self->{type}   = 'ttf';
    $self->{font}   = $font_file;
    $self->{ptsize} = $size;
    $self->_recalc();
    return 1;
}

=head2 $gd_text->set_text('some text')

Set the text to operate on. 
Returns true on success and false on error.

=cut

sub set_text
{
    my $self = shift;
    my $text = shift;

    $ERROR = "No text set", return unless defined $text;

    $self->{text} = $text;
    $self->_recalc_width();
}

=head2 $gd_text->set( attrib => value, ... )

The set method provides a convenience replacement for the various other
C<set_xxx()> methods. Valid attributes are:

=over 4

=item text

The text to operate on, see also C<set_text()>.

=item font, ptsize

The font to use and the point size. The point size is only used for
TrueType fonts. Also see C<set_font()>.

=back

Returns true on success, false on any error, even if it was partially
successful. When an error is returned, no guarantees are given about
the correctness of the attributes.

=cut

# We use this to save a few CPU cycles
my $recalc = 1;

sub set
{
    my $self = shift;
    $ERROR = "Incorrect attribute list", return if @_%2;
    my %args = @_;

    $ERROR = '';

    $recalc = 0;
    foreach (keys %args)
    {
        /^text$/i   and do {
            $self->set_text($args{$_});
            next;
        };
        /^font$/i   and do {
            $self->set_font($args{$_}, $self->{ptsize}) or return;
            next;
        };
        /^ptsize$/i and do {
            $self->{ptsize} = $args{$_};
            next;
        };
        $ERROR .= " '$_'";
    }
    $recalc = 1;
    $self->_recalc();

    if ($ERROR ne '')
    {
        $ERROR = "Illegal attribute(s):$ERROR";
        return;
    }

    return 1;
}

=head2 $gd_text->get( attrib, ... )

Get the value of an attribute.
Return a list of the attribute values in list context, and the value of
the first attribute in scalar context.

The attributes that can be retrieved are all the ones that can be set,
and:

=over 4

=item width, height

The width (height) of the string in pixels

=item space

The width of a space in pixels

=item char_up, char_down

The number of pixels that a character can stick out above and below the
baseline. Note that this is only useful for TrueType fonts. For builtins
char_up is equal to height, and char_down is always 0.

=back

Note that some of these parameters (char_up, char_down and space) are
generic font properties, and not necessarily a property of the text
that is set.

=cut

sub get
{
    my $self = shift;
    my @wanted = map $self->{$_}, @_;
    wantarray ? @wanted : $wanted[0];
}

=head2 $gd_text->width('string')

Return the length of a string in pixels, without changing the current
value of the text.  Returns the width of 'string' rendered in the
current font and size.  On failure, returns undef.

The use of this method is vaguely deprecated.

=cut

sub width
{
    my $self   = shift;
    my $string = shift;
    my $save   = $self->get('text');

    my $len = $self->set_text($string);
    return unless defined $len;
    my $w = $self->get('width');
    $self->set_text($save);

    return $w;
}

# Here we do the real work. See the documentation for the get method to
# find out which attributes need to be set and/or reset

sub _recalc_width
{
    my $self = shift;

    return unless $recalc;
    return unless (defined $self->{text} && $self->{font});

    if ($self->is_builtin)
    {
        $self->{'width'} = $self->{font}->width() * length($self->{text});
    }
    elsif ($self->is_ttf)
    {
        my @bb1 = GD::Image->stringTTF(0, 
            $self->{font}, $self->{ptsize}, 0, 0, 0, $self->{text});
        $self->{'width'} = $bb1[2] - $bb1[0];
    }
    else
    {
        confess "Impossible error in GD::Text::_recalc.";
    }
}

my ($test_string, $space_string, $n_spaces); 

BEGIN
{
    # Build a string of all characters that are printable, and that are
    # not whitespace.

    my @test_chars = map chr, 0x01 .. 0xff;

    my $isprintable_sub;
    if ($] >= 5.008)
    {
        # We have to do this at run time, otherwise 5.005_03 will
        # whinge about [[::]] syntax being reserved, and this cannot
        # be shut up with $^W
        #$^W = 0;
        eval '$isprintable_sub = sub { $_[0] =~ /^[[:graph:]]$/ }'
    }
    else
    {
        eval { local $SIG{'__WARN__'}; require POSIX };
        if ($@)
        {
            @test_chars = map chr, 0x21..0x7e, 0xa1..0xff;
            $isprintable_sub = sub { 1 }
        }
        else
        {
            $isprintable_sub = sub { POSIX::isgraph($_[0]) }
        }
    }

    $test_string = join '', grep $isprintable_sub->($_), @test_chars;

    # Put a space every 5 characters, and count how many there are
    $space_string = $test_string;
    $n_spaces = $space_string =~ s/(.{5})(.{5})/$1 $2/g;
}

sub _recalc
{
    my $self = shift;

    return unless $recalc;
    return unless $self->{font};

    if ($self->is_builtin)
    {
        $self->{height} =
            $self->{char_up} = $self->{font}->height();
        $self->{char_down} = 0;
            $self->{space} = $self->{font}->width();
    }
    elsif ($self->is_ttf)
    {
        my @bb1 = GD::Image->stringTTF(0, 
            $self->{font}, $self->{ptsize}, 0, 0, 0, $test_string)
                or return;
        my @bb2 = GD::Image->stringTTF(0, 
            $self->{font}, $self->{ptsize}, 0, 0, 0, $space_string);
        $self->{char_up} = -$bb1[7];
        $self->{char_down} = $bb1[1];
        $self->{height} = $self->{char_up} + $self->{char_down};
        # XXX Should we really round this?
        $self->{space} = sprintf "%.0f", 
            (($bb2[2]-$bb2[0]) - ($bb1[2]-$bb1[0]))/$n_spaces;
    }
    else
    {
        confess "Impossible error in GD::Text::_recalc.";
    }

    $self->_recalc_width() if defined $self->{text};

    return 1;
}

=head2 $gd_text->is_builtin

Returns true if the current object is based on a builtin GD font.

=cut

sub is_builtin
{
    my $self = shift; 
    return $self->{type} eq 'builtin';
}

=head2 $gd_text->is_ttf

Returns true if the current object is based on a TrueType font.

=cut

sub is_ttf
{
    my $self = shift; 
    return $self->{type} eq 'ttf';
}

=head2 $gd_text->can_do_ttf() or GD::Text->can_do_ttf()

Return true if this object can handle TTF fonts.

This depends on whether your version of GD is newer than 1.19 and
has TTF support compiled into it.

=cut

sub can_do_ttf
{
    my $proto = shift;

    # Just see whether there is a stringTTF method at all
    GD::Image->can('stringTTF') or return;

    # Let's check whether TTF support has been compiled in.  We don't
    # need to worry about providing a real font. The following will
    # always fail, but we'll check the message to see why it failed
    GD::Image->stringTTF(0, 'foo', 10, 0, 0, 0, 'foo');

    # Error message: libgd was not built with TrueType font support
    $@ =~ /not built with.*font support/i and return;

    # Well.. It all seems to be fine
    return 1;
}

=head2 $gd_text->font_path(path_spec), GD::Text->font_path(path_spec)

This sets the font path for the I<class> (i.e. not just for the object).
The C<set_font> method will search this path to find the font specified
if it is a TrueType font. It should contain a list of
paths. The current directory is always searched first, unless '.' is
present in FONT_PATH. Examples: 

  GD::Text->font_path('/usr/ttfonts'); # Unix
  GD::Text->font_path('c:/fonts');     # MS-OS

Any font name that is not an absolute path will first be looked for in
the current directory, and then in /usr/ttfonts (c:\fonts).

  GD::Text->font_path('/usr/ttfonts:.:lib/fonts'); # Unix
  GD::Text->font_path('c:/fonts;.;f:/fonts');      # MS-OS

Any font name that is not an absolute path will first be looked for in
/usr/ttfonts (c:\fonts), then in the current directory. and then in
lib/fonts (f:\fonts),
relative to the current directory.

  GD::Text->font_path(undef);

Font files are only looked for in the current directory.

FONT_PATH is initialised at module load time from the environment
variables FONT_PATH or, if that's not present, TTF_FONT_PATH, or
TT_FONT_PATH.

Returns the value the font path is set to.  If called without arguments
C<font_path> returns the current font path.

Note: This currently only works for unices, and (hopefully) for
Microsoft based OS's. If anyone feels the urge to have a look at the
code, and send me patches for their OS, I'd be most grateful)

=cut

sub font_path
{
    my $proto = shift;
    if (@_)
    {
        $FONT_PATH = shift;
        if ($FONT_PATH)
        {
            # clean up a bit
            $FONT_PATH =~ s/^:+//;
            $FONT_PATH =~ s/:+$//;
        }
    }
    $FONT_PATH;
}

=head1 BUGS

This module has only been tested with anglo-centric 'normal' fonts and
encodings.  Fonts that have other characteristics may not work well.
If that happens, please let me know how to make this work better.

The font height gets estimated by building a string with all printable
characters (with an ordinal value between 0 and 255) that pass the
POSIX::isprint() test (and not the isspace() test). If your system
doesn't have POSIX, I make an approximation that may be false. Under
Perl 5.8.0 the [[:print:]] character class is used, since the POSIX
is*() functions don't seem to work correctly.

The whole font path thing works well on Unix, but probably not very well
on other OS's. This is only a problem if you try to use a font path. If
you don't use a font path, there should never be a problem. I will try
to expand this in the future, but only if there's a demand for it.
Suggestions welcome.

=head1 COPYRIGHT

copyright 1999
Martien Verbruggen (mgjv@comdyn.com.au)

=head1 SEE ALSO

GD(3), GD::Text::Wrap(3), GD::Text::Align(3)

=cut

1;