Subject:         Re: Suggestion: DRAWINGS on newsgroups
   Date:         Fri, 27 Aug 99 20:41:43 GMT
   From:        dave@lassie.demon.co.uk (David Jordan)
     To:         office@meg-glaser.biz

Hi, Ing.

> David Jordan wrote:
> >
> >
> > It's not that difficult, it took me about an hour to come up with a simple
> > viewer written in perl. It uses the GD module so you need to invoke it as:
>
> GREAT!!!!!!!!
>
> http://geo.meg-glaser.at/txt/NGpaint.html
> has the "database" definition.
>
> The "1" is the thickness. Range: 1..10
> OR it is "DS" "DT" "DD" for dashed, dotted, dashdot.
>
> The "M" is a placeholder for later enhancement, it means "medium font
> size" in the moment, but is unused in the moment.
>
> Please send me the code, I will place it on the URL for reference.
 

Here's the code... This one works fine but I'll update it and send you
the new version soon.



PERL  NGpaint textformat reader

Version 2 from
       Sat, 28 Aug 99 22:55:40 GMT
Includes Arcs from NGpaint Ver. 6
Includes Text / Font size Small Medium Large, which is not yet implemented in NGpaint.

Version 3 from
       Thu, 2 Sep 99 00:20:53 GMT
Relative coordinates supported from NGpaint Ver.7

#!/usr/local/bin/perl

######################################################################
#                                                                    #
#  NGView V3 - Perl viewer for NGPaint files                         #
#                                                                    #
#  Requires:  GD.pm - Interface to Gd Graphics Library               #
#                                                                    #
######################################################################

use GD;

$im = new GD::Image(320,200);
($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->transparent($white);   # white color is transparent
$im->interlaced(1);     # cool venetian blinds effect

# Create a rectangle paintbrush
for ($i=1; $i<=10; $i++)
  {
    $brush[$i] = new GD::Image($i,$i);
    $brush[$i]->colorAllocate(255,255,255); # white
    $brush[$i]->colorAllocate(0,0,0);   # black
    $brush[$i]->transparent($white);    # white is transparent
    $brush[$i]->filledRectangle(0,0,$i,$i,$black); # a black rectangle
  }
$im->setBrush($brush[1]);
 
 

while ($line=<STDIN>)
  {
    # Split off first character
    $type = substr($line,0,1);
    $param = substr($line,2);
    chomp $param;

    if (($type EQ 'L' or $type EQ 'R' or $type EQ 'T' or
         $type EQ 'E' or $type EQ 'A')
         and substr($line,1,1) EQ '(')
      {

        # Handle multi-line
        while (index ($param, ')') < 1)
          {
            $line=<STDIN>;
            $param = join('', $param, $line);
            chomp $param;
          }

        # Remove CRs
        $param =~ s/\r//g;

        # Remove brackets
        $param =~ s/\)$//;
        $param =~ s/^\(//;
 
 

        # Polyline (Complete)
        if ($type EQ 'L')
          {
            ($linetype) = split(/;/, $param);

            # Translate relative coordinates
            $points = substr($param, length($linetype)+1);
            @coords = parse_coords($points);

            set_line_style($linetype);

            for($i=2; $i < scalar(@coords); $i=$i+2)
              {
                $im->line($coords[$i-2], $coords[$i-1],
                          $coords[$i], $coords[$i+1], gdStyledBrushed)
              }
          }
 
 

        # Rectangle (Complete)
        if ($type EQ 'R')
          {
            ($linetype, $fill) = split(/;/, $param);

            # Translate relative coordinates
            $points=substr($param, length($linetype.$fill)+2);
            ($x1, $y1, $x2, $y2) = parse_coords($points);

            set_line_style($linetype);

            if ($fill EQ 'E')
              {
                $im->rectangle($x1, $y1, $x2, $y2, gdBrushed);
              }
            elsif ($fill EQ 'F')
              {
                $im->filledRectangle($x1, $y1, $x2, $y2, $black);
              }
          }
 
 

        # Text (Complete)
        if ($type EQ 'T')
          {
            ($size, $x, $y, $text) = split(/;/, $param);

            # Strip quote marks
            $text =~ s/\"//g;

            if ($size EQ 'S')
              {
                $im->string(gdTinyFont,$x,$y-7,$text,$black);
              }
            elsif ($size EQ 'M')
              {
                $im->string(gdSmallFont,$x,$y-11,$text,$black);
              }
            elsif ($size EQ 'L')
              {
                $im->string(gdLargeFont,$x,$y-15,$text,$black);
              }
          }
 
 

        # Ellipse (Complete)
        if ($type EQ 'E')
          {
            ($linetype, $fill) = split(/;/, $param);

            # Translate relative coordinates
            $points=substr($param, length($linetype.$fill)+2);
            ($x1, $y1, $x2, $y2) = parse_coords($points);

            $cx=((($x2-$x1)+1)/2)+$x1;
            $cy=((($y2-$y1)+1)/2)+$y1;
            $width=$x2-$x1;
            $height=$y2-$y1;

            set_line_style($linetype);

            $im->arc($cx,$cy,$width,$height,0,360,$black);

            if ($fill EQ 'F')
              {
                $im->fill($cx,$cy,$black);
              }
          }
 

        # Arc
        if ($type EQ 'A')
          {
            ($linetype, $fill) = split(/;/, $param);

            # Translate relative coordinates
            $points=substr($param, length($linetype.$fill)+2);
            ($cx, $cy, $x2, $y2) = parse_coords($points);

            if ($x2 > $cx)     # right
              {
                $width = (($x2 - $cx)+1)*2;

                if ($y2 < $cy) # up
                  {
                    $startangle = 270;
                    $endangle = 0;
                    $height = ($cy - $y2)*2;
                  }
                else           # down
                  {
                    $startangle = 0;
                    $endangle = 90;
                    $height = ($y2 - $cy)*2;
                  }
              }
            else               # left
              {
                $width = (($cx - $x2)+1)*2;

                if ($y2 < $cy) # up
                  {
                    $startangle = 180;
                    $endangle = 270;
                    $height = ($cy - $y2)*2;
                  }
                else           # down
                  {
                    $startangle = 90;
                    $endangle = 180;
                    $height = ($y2 - $cy)*2;
                  }
              }

            set_line_style($linetype);

            $im->arc($cx, $cy, $width, $height, $startangle, $endangle, $black);
          }
      }
  }
 
 

sub set_line_style
  {
    my @params = @_;
    my $type = $params[0];

    @plain=($black);

    @dotted=($black, $black, $black, gdTransparent, gdTransparent);

    @dashed=($black, $black, $black, $black, $black, $black,
         gdTransparent, gdTransparent);

    @dashdot=($black, $black, $black, $black, $black, $black,
          gdTransparent, gdTransparent, gdTransparent, $black, $black,
          $black, gdTransparent, gdTransparent, gdTransparent);

    $im->setBrush($brush[1]);

    if ($type EQ 'DS')
      {
    $im->setStyle(@dashed);
      }
    elsif ($type EQ 'DT')
      {
    $im->setStyle(@dotted);
      }
    elsif ($type EQ 'DD')
      {
    $im->setStyle(@dashdot);
      }
    else
      {
    $im->setStyle(@plain);
    $im->setBrush($brush[$type]);
      }
  }
 

sub parse_coords
  {
    my @params = @_;
    $coords = $params[0];

    # fix relative coordinates
    $coords =~ s/:/;:/g;

    @points = split (/;/, $coords);

    # Do x's
    $prevx = $points[0];
    for ($i = 2; $i < scalar(@points); $i = $i + 2)
      {
    if (substr($points[$i],0,1) EQ ':')
      {
        $points[$i] = substr($points[$i],1) + $prevx;
      }
        $prevx = $points[$i];
      }

    # Do y's
    $prevy = $points[1];
    for ($i = 3; $i < scalar(@points); $i = $i + 2)
      {
    if (substr($points[$i],0,1) EQ ':')
      {
        $points[$i] = substr($points[$i],1) + $prevy;
      }
        $prevy = $points[$i];
      }

    return @points;
  }
 

# open (OUTPUT, ">/root/image.gif");
# binmode OUTPUT;
# print OUTPUT $im->gif;
# close OUTPUT;

binmode STDOUT;
print $im->gif;
 

Dave.

--
dave@lassie.demon.co.uk                http://www.lassie.demon.co.uk
jordandc@aston.ac.uk                   http://www.aston.ac.uk/~jordandc

Amiga 4000 Cyberstorm 060/50 112MB 4GB-SCSI-II 2M-CV64 (And a Psion 3a...)

 ... The cost of feathers has risen...now even DOWN is up!



Back to NGpaint.html
This page hosted by    Get your own Free Home Page