Inkscape.org
Creating New Extensions Any interest in convertng rectangular and square polygons into rects?
  1. #1
    Lady Aleena Lady Aleena @Lady_Aleena

    Does anyone believe there would be an interest in an extension that converts polygons that are rectangles and squares into <rect> elements? I wrote a perl script that does it on .svg files outside of Inkscape, but it might be converted into an extension with a little help. That is, if there would an interest in such an extension.

    I ran into another program that generates svg that makes rectangles polygons, and that makes things a little difficult at times. That is why I converted them to rectangles.

    I will not bog down the forum with the very basic code I wrote unless there is interest.

  2. #2
    Martin Owens Martin Owens @doctormo🌹🧀

    It's an interesting idea, it could be added firstly as a small addition to `class PathElement` as a `to_rect` which you could then add as a simple path extension, maybe with a tolerenace.

    The more interesting one is converting a rounded corner rect to a rect :-) or a circle/oval to a circle element.

  3. #3
    Lady Aleena Lady Aleena @Lady_Aleena

    @doctormo, since you've shown interest, here is the perl code I wrote for it. I know it would need to be converted to another language that is used by Inkscape.

    #!/usr/bin/perl
    use strict;
    use warnings;
    
    use List::Util qw(min max uniq);
    use Math::Round qw(nearest);
    
    sub make_rect {
      my $line = shift;
    
      my @attributes = ( $line =~ /(\w+\=".+?")/g );
    
      my @points_attribute = grep { /points/ } @attributes;
      my $points = $points_attribute[0];
         $points =~ s/.+="(.+)"/$1/;
      my $points_list = [uniq split(/ /,$points)];
    
      my $new_value;
      if (@$points_list == 4) {
        my @xs;
        my @ys;
        for my $point (@$points_list) {
          my ($x, $y) = split(/,/, $point);
          push @xs, nearest(.1, $x);
          push @ys, nearest(.1, $y);
        }
    
        my $uniqxs = uniq(@xs);
        my $uniqys = uniq(@ys);
    
        if ( $uniqxs == 2 && $uniqys == 2) {
          my $minx = min(@xs);
          my $miny = min(@ys);
          my $maxx = max(@xs);
          my $maxy = max(@ys);
    
          my $x = $minx;
          my $y = $miny;
          my $width = $maxx - $minx;
          my $height = $maxy - $miny;
    
          my @no_points_attributes = grep { !/points/ } @attributes;
          my $no_points = join(' ', @no_points_attributes);
    
          $new_value = qq(<rect x="$x" y="$y" width="$width" height="$height" $no_points />);
        }
        else {
          $new_value = $line;
        }
      }
      else {
        $new_value = $line;
      }
      return $new_value;
    }
    
    

    So, if you like, you or someone else can modify it to suit.

    LA

  4. #4
    Patrick Storz Patrick Storz @Ede_123

    As we hardly ever advertise it anymore (but I feel it should be mentioned): You can actually use perl for Inkscape extensions! See also http://wiki.inkscape.org/wiki/index.php/Extension_Interpreters.

    Only downside is that it's usually not bundled anymore, so you have to make sure to install a separate perl distribution, but other than that functionality matches the interface implemented for python.

  5. #5
    Lady Aleena Lady Aleena @Lady_Aleena

    @Ede_123 I would not know where to begin writing this as an Inkscape extension. I will need all the help I can get.

  6. #6
    Patrick Storz Patrick Storz @Ede_123

    A starting point is https://inkscape.org/develop/extensions/ and there's some more info on the Wiki. Be warned it's not complete and partially outdated, though, so looking at what bundled extensions do might be more straightforward. Hopefully we'll be able to update the documentation eventually, but obviously spare time is limited...

  7. #7
    Martin Owens Martin Owens @doctormo🌹🧀

    @Lady_Aleena Please see the attached zip file, it contains a completed extension to convert paths to rectangles. I tested it with Inkscape beta1 locally.

  8. #8
    Lady Aleena Lady Aleena @Lady_Aleena

    @doctormo Is is backward compatible with 92.4? Debian doesn't have beta 1, and alpha is in experimental (not sid).

  9. #9
    Martin Owens Martin Owens @doctormo🌹🧀

    No, extensions have completely changed API (i.e. python3, plus new inkex module etc). If you want to use the new API with 0.92, you'll need to get the extensions repository here: https://gitlab.com/inkscape/extensions

Inkscape Inkscape.org Inkscape Forum Creating New Extensions Any interest in convertng rectangular and square polygons into rects?