Lazarus

Programming => Packages and Libraries => FPvectorial => Topic started by: Edson on September 10, 2015, 01:09:58 am

Title: FPvectorial "Unsupported vector graphics"
Post by: Edson on September 10, 2015, 01:09:58 am
Hi.

Triying to show a simple SVG file, using:

Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
var
  Vec: TvVectorialDocument;
begin
  Vec := TvVectorialDocument.Create;
  try
    Vec.ReadFromFile('d:\triac.svg');
    Vec.GetPage(0).DrawBackground(Image1.Canvas);
    Vec.GetPage(0).Render(
      Image1.Canvas,      0,      Image1.Height,      1,      -1);
    Image1.Invalidate;
  finally
    Vec.Free;
  end;
end;

And I get the error: "Unsupported vector graphics".

How do I know what formats are supported by FPvectorial?

Thanks.
Title: Re: FPvectorial "Unsupported vector graphics"
Post by: felipemdc on September 10, 2015, 09:59:42 am
Use fpvectorial from Lazarus SVN, svg is supported: http://wiki.lazarus.freepascal.org/Getting_Lazarus#Getting_SVN
Title: Re: FPvectorial "Unsupported vector graphics"
Post by: Edson on September 10, 2015, 05:57:55 pm
Testing with the SVN version. The same error.

I have used 4 or 5 different SVG files (all of them can be correctly shown on the browser) with no success.
Title: Re: FPvectorial "Unsupported vector graphics"
Post by: wp on September 10, 2015, 06:37:46 pm
Please post these svg files here. FPVectorial does not claim to be able to read all files.

Do you have the svgvectorialreader in the uses clause?
Title: Re: FPvectorial "Unsupported vector graphics"
Post by: Edson on September 10, 2015, 06:47:51 pm
This is my sample project. Maybe I'm doing something wrong.
Title: Re: FPvectorial "Unsupported vector graphics"
Post by: wp on September 10, 2015, 07:18:57 pm
As I wrote above, add "svgvectorialreader" to the uses clause of the main form. This is because fpvectorial supports a variety of file formats but links only the needed ones into your program.

Add fpvectorialpkg as a requirement to the project (instead of adding it to the search path), i.e. open project inspector, "Add", "New requirement", type "fpvectorialpkg".

Then the error you report is gone. But the program still does not run, it does not like the parts marked in red:

<path fill="#999999" d="M1,16C1,7.72,7.71,1,16,1c8.279,0,15,6.72,15,15c0,8.29-6.721,15-15,15C7.71,31,1,24.29,1,16z"/>

Unfortunately, I do not know much about svg. What does the "-" token stand for? A mathematical operation (subtraction)? If yes, replace them by the difference values.
Title: Re: FPvectorial "Unsupported vector graphics"
Post by: wp on September 10, 2015, 07:46:13 pm
After some experimenting I see that the '-' seems to be an ordinary minus sign of a negative number. After inserting a comma before the '-' the file is read without error.

Certainly the parser of the svg reader should be clever enough to catch this case. Maybe I'll write a patch.
Title: Re: FPvectorial "Unsupported vector graphics"
Post by: Edson on September 10, 2015, 08:26:31 pm
Thanks @wp
You are right. It needs the unit "svgvectorialreader".  :)

And the negative number format, give error, but it's in some way manageable.
Sadly, All my SVG files seems to have this problem.
Title: Re: FPvectorial "Unsupported vector graphics"
Post by: wp on September 10, 2015, 08:33:23 pm
Which application did you use to create the files? Or are they downloaded from somewhere?

Since Firefox can open your files without issues I suspect that a missing separating space or comma before a minus character is within specification. Therefore, fpvectorial should be able to deal with this special case automatically.
Title: Re: FPvectorial "Unsupported vector graphics"
Post by: Edson on September 10, 2015, 08:47:40 pm
I'm triying to use the files from Fritzing: http://fritzing.org/home/ (http://fritzing.org/home/)

They represent electronic components.

Internet Explorer 11 can show the files correctly, too.
Title: Re: FPvectorial "Unsupported vector graphics"
Post by: wp on September 10, 2015, 09:46:30 pm
In the meanwhile I could fix the issue and uploaded a patch to bugtracker (http://bugs.freepascal.org/view.php?id=28659 (http://bugs.freepascal.org/view.php?id=28659)).

It may take some time until it is in the release version of Lazarus. If you want to test the patch please copy this procedure over the current one in unit svgvectorialreader.pas (starting at line 274) and recompile the package (of course, this is only a temporary fix, it will be lost whenever you reinstall Lazarus):

Code: [Select]
procedure TSVGPathTokenizer.TokenizePathString(AStr: string);
const
  Str_Space: Char = ' ';
  Str_Comma: Char = ',';
  Str_Plus: Char = '+';
  Str_Minus: Char = '-';
  ListOfCommandLetters: set of Char = ['a'..'d', 'f'..'z', 'A'..'D', 'F'..'Z'];
var
  i: Integer;
  lTmpStr: string = '';
  lState: Integer;
  lFirstTmpStrChar, lCurChar, lPrevChar: Char;
begin
  lState := 0;

  i := 1;
  while i <= Length(AStr) do
  begin
    case lState of
    0: // Adding to the tmp string
    begin
      if i > 0 then lPrevChar := AStr[i-1];
      lCurChar := AStr[i];
      if lCurChar = Str_Space then
      begin
        lState := 1;
        AddToken(lTmpStr);
        lTmpStr := '';
      end
      else if lCurChar = Str_Comma then
      begin
        AddToken(lTmpStr);
        lTmpStr := '';
      end
      else if (lCurChar in [Str_Plus, Str_Minus]) and (lPrevChar in ['0'..'9']) then
      begin
        AddToken(lTmpStr);
        lTmpStr := lCurChar;
      end
      else
      begin
        // Check for a break, from letter to number
        // But don't forget that we need to support 3.799e-4 !!
        // So e is not a valid letter for commands here
        if (Length(lTmpStr) >= 1) then
        begin
          lFirstTmpStrChar := lTmpStr[1];
          if ((lFirstTmpStrChar in ListOfCommandLetters) and not (lCurChar  in ListOfCommandLetters)) or
             (not (lFirstTmpStrChar in ListOfCommandLetters) and (lCurChar  in ListOfCommandLetters)) then
          begin
            AddToken(lTmpStr);
            lTmpStr := '';
            Continue;
          end;
        end;

        lTmpStr := lTmpStr + lCurChar;
      end;

      Inc(i);
    end;
    1: // Removing spaces
    begin
      if AStr[i] <> Str_Space then lState := 0
      else Inc(i);
    end;
    end;
  end;

  // If there is a token still to be added, add it now
  if (lState = 0) and (lTmpStr <> '') then AddToken(lTmpStr);
end;
Title: Re: FPvectorial "Unsupported vector graphics"
Post by: Edson on September 10, 2015, 10:17:48 pm
Good. It avoids the error messages.

Although it seems FPvectorial, doesn't draw correctly the SVG files. There is a big difference from what Firefox shows.
Title: Re: FPvectorial "Unsupported vector graphics"
Post by: wp on September 10, 2015, 10:42:20 pm
I see two differences:
Is it that what you mean?
Title: Re: FPvectorial "Unsupported vector graphics"
Post by: Edson on September 11, 2015, 05:33:06 pm
There are more differences if you see the other files. Look at this picture to compare.
Title: Re: FPvectorial "Unsupported vector graphics"
Post by: wp on January 02, 2016, 12:50:45 am
Interim report: still far from perfect, but more and more svg files are rendered correctly with the current version of fpvectorial - see screenshot.
Title: Re: FPvectorial "Unsupported vector graphics"
Post by: felipemdc on June 22, 2016, 10:15:36 pm
I'm happy you like the progress that has been made :) SVG is a priority, but it is a really, really hard format to support .... too much flexibility, too many possible ways to describe the same thing. It is the most challenging format from our support list.
Title: Re: FPvectorial "Unsupported vector graphics"
Post by: Edson on June 23, 2016, 07:11:42 am
Interim report: still far from perfect, but more and more svg files are rendered correctly with the current version of fpvectorial - see screenshot.

Good. I haven't checked recently SVG on fpvectorial, but I see there are improvements. I will return to SVG later.