#!/usr/bin/perl -w # # Text Grid # A plug-in for the GIMP, written in Perl. # use strict; use Gimp qw(:auto __ N_); use Gimp::Fu; # Fonts must be specified in XLFD format # my $default_font = "-*-charter-medium-r-normal-*-*-140-*-*-p-*-iso8859-1"; register( "perl_fu_text_grid", # Name "Create a grid of text", # Blurb "=pod(HELP)", # Help "=pod(AUTHOR)", # Author "=pod(COPYRIGHT)", # Copyright "=pod(DATE)", # Date N_"/Xtns/ORA Examples/Text Grid", # Menu path "", # Images accepted # The parameters [ [ PF_INT32, "rows", "Number of rows", 8 ], [ PF_INT32, "columns", "Number of columns", 8 ], [ PF_STRING, "text", "Text", "Foo" ], [ PF_INT32, "border", "Border, in pixels", 12 ], [ PF_FONT, "font", "The font to use", $default_font ], ], # return values [], \&text_grid ); sub text_grid { # The plug-in subroutine. # First get the parameters passed to the procedure... # my ($rows, $columns, $text, $border, $font) = @_; # Initialize the progress bar # Gimp->progress_init ("Generating text grid..."); # Use the convenience function xfld_size to parse the font name # for two items; the font size, and whether the size is # represented in points or pixels # my @font_size = Gimp::xlfd_size($font); # Size each grid unit so that it is large enough to # hold the largest letter in the string. Since all letters will # have the same height (based on the font size), we just need # to find the letter with the maximum width. # my ($cell_w, $cell_h, $max_w, $size_in_pixels) = (0,0,0,0); foreach my $letter (split("", $text)) { my ($tmp_w, $tmp_h, $tmp_a, $tmp_d) = Gimp->text_get_extents_fontname($letter, @font_size, $font); if ($tmp_w > $max_w) { $max_w = $tmp_w; $cell_w = $tmp_w-2.5; # correct for text tool $cell_h = $tmp_h - $tmp_d; # subtract the descender $size_in_pixels = $tmp_h; } } # Make sure the string is long enough to fill a rows x columns grid; # if not, repeat the string an appropriate number of times. # if (length($text) < ($rows * $columns)) { $text = $text x (int(($rows * $columns)/length($text))+1); } # Create the new image # my $w = $columns * $cell_w + ($columns+1) * $border; my $h = $rows * $cell_h + ($rows+1) * $border; my $img = new Gimp::Image($w, $h, RGB); # Add a new layer, which will also be our drawable. # my $drawable = $img->layer_new($w, $h, RGBA_IMAGE, "Background", 100, NORMAL_MODE); $img->add_layer($drawable, -1); # Place at top of stack $drawable->gimp_edit_fill; # Fill with background # Now iterate over the list of characters, drawing each in a # separate grid cell with the text tool. # my @chars = split("", $text); my ($x, $y, $char); my ($x_extent, $y_extent, $ascent, $descent); my $progress = 0; for (my $i = 0; $i < $rows; $i++) { for (my $j = 0; $j < $columns; $j++) { $char = shift(@chars); ($x_extent, $y_extent, $ascent, $descent) = Gimp->text_get_extents_fontname($char, @font_size, $font); $x_extent = $x_extent-2.5; # To correct behavior of text tool $y_extent -= $descent; # Subtract descender # Calculate (x, y) coordinate of upper left corner of # text block. Center it horizontally within the cell. # $x = $border + $j * ($cell_w + $border) + ($cell_w - $x_extent)/2; $y = $border + $i * ($cell_h + $border); # Use the text tool to draw the text at the coordinate # $drawable->text_fontname($x, $y, $char, 0, 1, $size_in_pixels, 0, $font); } # Update the progress bar with each row completed. Note that this # will result in a less responsive progress bar for images that are # more horizontal than vertical. # $progress += 1/$rows; Gimp->progress_update ($progress); } # The last bit of text added will still reside in a floating # selection; anchor it. # $img->floating_selection->floating_sel_anchor; return ($img); } exit main; # Embedded POD documentation follows # =head1 NAME Text Grid =head1 HELP This script takes a string and will place each character in a separate cell of a rows x columns grid, with each cell padded by the number of pixels indicated by border. Characters will be redered in the foreground color, with the given font. If there are not enough characters to fill the grid, they will be repeated. =head1 AUTHOR Shawn P. Wallace =head1 COPYRIGHT (c) 2001 The Institute for Folk Computing =head1 DATE 2001-07-12