Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2011-04

FPVectorial and TAChart

13 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Alexander Klenin

I have tried to use FPVectorial as TAChart back-end. So far, without success.
I have a few questions/feature fequests, sorted approximately by decreasing importance:

1) How to draw a rectangle?
2) How to measure a text?
3) How to draw a polygon?
4) How to make FPVectorial output anything?
It seems crash no matter what output format I try.
5) IMHO intruducing yet another color type is a bad idea -- FPVectorial is by design sacrificing efficiency in favor of generalization/extensibility, so I do not think a few extra bytes for color will matter. Please use TFPColor.
6) How to draw a radial pie?

Alexander S. Klenin

Reply by email

Felipe Monteiro de Carvalho
to Alexander Klenin
Alexander Klenin wrote
1) How to draw a rectangle? 3) How to draw a polygon?
var
  ADoc: TvVectorialDocument;
begin
  ADoc.StartPath(0, 0);
  ADoc.AddLineToPath(100, 0);
  ADoc.AddLineToPath(100, 100);
  ADoc.AddLineToPath(0, 100);
  ADoc.AddLineToPath(0, 0);
  ADoc.EndPath();

Or do you want an entity with a brush? I can add that if necessary.

2) How to measure a text?

What exactly do you want to measure in the text? width? DXF and SVG don't specify a text width AFAIK.

TvText.FontSize + TvText.FontName is the information available.

Vectorial image formats do not specify rigid sizes for text, because it is system-independent and the text size depends on the screen/font combination. One can use freetype or LCL or fcl-image via freetype for measuring the text size in a particular screen / font combination.

4) How to make FPVectorial output anything? ?It seems crash no matter what output format I try.

The SVG and g-code outputs should work for lines and text.

The main idea was that you would also help to develop fpvectorial. The idea was that instead of writing some custom code to write vectorial files in TAChart you would make some improvements in fpvectorial, so everyone is working on the same library, code is shared, improvements are shared, etc.

5) IMHO intruducing yet another color type is a bad idea -- FPVectorial is by design sacrificing efficiency in favor of generalization/extensibility, so I do not think a few extra bytes for color will matter. Please use TFPColor.

That would waste 4 bytes per segment. And for what exactly? I don't see much use in mixing fcl-image and fpvectorial, only in the exporter which doesn't currently work with fcl-image anyway because it lacks some drawing functions. Most of the time fpvectorial will be used with the LCL, which has an incompatible color format anyway without Alpha and unavailable in the FCL.

Felipe Monteiro de Carvalho

Reply by email

Felipe Monteiro de Carvalho
to Felipe Monteiro de Carvalho
Alexander Klenin wrote
No idea if I want an entity, but I do need a brush :)

I'll add a Polygon entity, which should be a closed polyline with brush.

I need to draw a rectangle surrounding given text string.

Ummm ... some research is necessary to verify how other software solve this issue. In AutoCAD it seams that simply the rectangles are much larger then the text, to make sure it fits regardless of the font utilized.

Hm, probably the error handling should be enhanced then -- I just get SIGSEGV on doc.WriteToFile. Will try SVG.

Strange, it should throw an exception. Which output format have you tried? Maybe a test program so that I can fix the problem?

How can I visualise g-code file?
  Vec := TvVectorialDocument.Create;
  try
    Vec.ReadFromFile(FileName, vfAvisoCNCGcode);
    Drawer.Drawing.Width := CanvasSize.X;
    Drawer.Drawing.Height := CanvasSize.Y;
    Drawer.Drawing.Canvas.Brush.Color := clWhite;
    Drawer.Drawing.Canvas.Brush.Style := bsSolid;
    Drawer.Drawing.Canvas.FillRect(0, 0, Drawer.Drawing.Width,
Drawer.Drawing.Height);
    DrawFPVectorialToCanvas(
      Vec,
      Drawer.Drawing.Canvas,
      0,
      0,
      1,
      -1);
    Drawer.Invalidate;
  finally
    Vec.Free;
  end;

or else you could try: lazarus-ccr/applications/fpvviewer just change this line:

Vec.ReadFromFile(editFileName.FileName, vfDXF);

It is in my ToDo to make this program more flexible and even support selecting and editing elements.

I also wrote a specialized g-code viewer, but it is not open source.

Well, from my POV you are already wasting about 24 bytes per segment -- all fields of TPathSegment class plus the overhead of the class itself.

Using a class was required in a bug report... not my invention. About the fields, well maybe I should then remove the possibility of having a different Pen for each part of the polyline. I added it because DXF supports it, but it does seams like an excess. Inkscape does not allow editing such kind of element. The list linking cannot be removed without huge changes.

  TPathSegment = class
  public
    SegmentType: TSegmentType;
    // Fields for linking the list
    Previous: TPathSegment;
    Next: TPathSegment;
    // Data fields
    PenColor: TvColor;
    PenStyle: TFPPenStyle;
    PenWidth: Integer;
  end;
Felipe Monteiro de Carvalho

Reply by email

Felipe Monteiro de Carvalho
to Alexander Klenin: I have tried to use FPVectorial as TAChart back-end. So far, without success. I have a…
Alexander Klenin wrote
1) How to draw a rectangle? 3) How to draw a polygon?

Now you can do this:

var
 ADoc: TvVectorialDocument;
begin
 ADoc.StartPath(0, 0);
 ADoc.AddLineToPath(100, 0);
 ADoc.AddLineToPath(100, 100);
 ADoc.AddLineToPath(0, 100);
 ADoc.AddLineToPath(0, 0);
 ADoc.SetBrushStyle(bsSolid);
 ADoc.SetBrushColor(something);
 ADoc.EndPath();

And you have a Polygon.

I decided to define polygons as simply paths with a Brush.Style bsClear, seams simpler then defining two separate bug very similar elements

Felipe Monteiro de Carvalho

Reply by email

Felipe Monteiro de Carvalho
to Felipe Monteiro de Carvalho
Alexander Klenin wrote 6 lines
I have committed my experimental code -- see fpvectorial demo in TAChart since r30400. Try changing output format to, say, DXF -- you will get SIGSEGV. This bug is in TvVectorialDocument.CreateVectorialWriter -- it does not check GvVectorialFormats[i].WriterClass for nil. Also, there are a few typos: "unsuported" -> "unsupported",. "seams" -> "seems".

Fixed.

It does not work on Windows due to WriteLn calls (conditional on FPVECTORIAL_TOCANVAS_DEBUG) -- should probably be disabled in SVN

Done

After fixing that, I was able to view g-code file created by TAChart back-end. However, it bears very little resemblance to the original chart :(

Can you send me the gcode file? Are you viewing with fpvviewer?

Show quoted text 5 lines
> Using a class was required in a bug report... not my invention. About > the fields, well maybe I should then remove the possibility of having > a different Pen for each part of the polyline. At least you can move it into a descendant class.

Done. Now both are possible: One pen for the entire line, or one Pen per segment, will be faster to draw too.

Felipe Monteiro de Carvalho

Reply by email

Felipe Monteiro de Carvalho
to Felipe Monteiro de Carvalho
Alexander Klenin wrote 5 lines
I added corresponging calls to fpvectorial back-end. However, it seems those calls have no effect -- all polygons are still drawn as polylines in both SVG and G-code writers. .... Ok. Note that SegmentWithPen is now not written to SVG.

Yes, of course. I only added the data structures so far. All the rest needs to be updated to use

Felipe Monteiro de Carvalho

Reply by email

Felipe Monteiro de Carvalho
to Alexander Klenin: I have tried to use FPVectorial as TAChart back-end. So far, without success. I have a…
Alexander Klenin wrote
2) How to measure a text?

I have done some research and it seams that in SVG this can only be done by using JavaScript inserted inside the image:

http://stackoverflow.com/questions/3001950/how-can-i-draw-a-box-around-text-with-svg

But that's a gross abuse .. something that one can only hope to get working in web browsers and in fact Inkscapte doesn't support Javascript inside SVG images ...

I think that the best that one could do is guessing. Take a look at the width each letter uses for example in Arial, then see which font is likely to get used in Linux if you feed Arial to the font chooser, the same for Mac, and then try to build a guess function based on empiric data.

Felipe Monteiro de Carvalho

Reply by email

Felipe Monteiro de Carvalho
to Felipe Monteiro de Carvalho
Alexander Klenin wrote 8 lines
>> 2) How to measure a text? > [skip] > I think that the best that one could do is guessing. I know. TAChart's SVG back-end already does trivial "guessing". But that is the deficiency of particular back-end (SVG) -- you do not have to repeat it in the whole fpvectorial.

I was thinking about it and I think that I could add two more properties to the text element:

Brush
BoundingRectPen

They would allow to fill a rectangular area bellow it and draw a bounding rectangle, but that's not really a solution for the generic problem of measuring text which is something that cannot be perfectly done in vectorial images.

Do you have a better idea?

Felipe Monteiro de Carvalho

Reply by email

Felipe Monteiro de Carvalho
to Felipe Monteiro de Carvalho
Alexander Klenin wrote: After fixing that, I was able to view g-code file created by TAChart back-end. However, it bears very little resemblance to the original chart :(

I have been thinking more about this and it is as expected. G-Code does not support Pen, Brush, Text, Font and also not entities. G-Code by definition only supports plain paths. I think that some machines should be able to support Pen Width by changing the tool, but that's not how the machines that I used work.

If you want to save to a file and then load again from the same file try using SVG. I have implemented a new SVG reader some days ago and it can read paths. Text and entities are not yet supported. SVG should be able to support all elements present in fpvectorial (and even some not present ones), but the writer and reader still needs to be improved.

Felipe Monteiro de Carvalho

Reply by email

Alexander Klenin

Sorry, forgot that your mail requires "reply-all" again. Forwarding to the list.

---------- Forwarded message ----------

Earlier message 20 lines
From: Alexander Klenin Date: Thu, Jun 16, 2011 at 22:14 Subject: Re: FPVectorial and TAChart To: Felipe Monteiro de Carvalho On Thu, Jun 16, 2011 at 21:18, Felipe Monteiro de Carvalho wrote: > I just took a look at your fpvectorial demo for tachart and I the svg > output of fpvectorial to support the brush color and style. But now > the output from your demo generates a black background =o > > I tryed to debug it, but I couldn't find the error. Why does your code > generate a almost black background for the drawing? Maybe that should > be something like clButton, which is not a real color. Fixed in r31246 -- Alexander S. Klenin

Reply by email

Alexander Klenin

(Quoted in full, since original mail apparently did not made it to the list)

Felipe Monteiro de Carvalho wrote 32 lines
One more thing: Your chart is upside down. It seams that you didn't think about the fact that the TCanvas and FPVectorial coordinates are different. TCanvas works like this, which is the usual for raster images: -----> X grows to the right | | y grows down, starting on the top FPVectorial uses the standard coordinates for vectorial documents instead: y grows upwards, starting on the bottom | | ----> X grows to the right I added a function that you can use in the unit fpvutils: {@@ Converts the coordinate system from a TCanvas to FPVectorial ? ?The basic difference is that the Y axis is positioned differently and ? ?points upwards in FPVectorial and downwards in TCanvas. ? ?The X axis doesn't change. The fix is trivial and requires only the Height of ? ?the Canvas as extra info. } function CanvasCoordsToFPVectorial(AY: Integer; AHeight: Integer): Integer; inline; begin ?Result := AHeight - AY; end;

You are right, I did not know it. Maybe it should be mentioned in the wiki. It is, however, not quite simple.
First, depending on interpretation, formula may be (ABottom - AY) or even (ABottom + ATop - AY) -- I am not sure which is preferable.

Also there is a question of text positioning -- do I understand correctly that FPVectorial uses given coordinates as bottom-left point of text rectangle instead of top-left?

Alexander S. Klenin

Reply by email

Felipe Monteiro de Carvalho
to Alexander Klenin
Alexander Klenin wrote 5 lines
You are right, I did not know it. Maybe it should be mentioned in the wiki. It is, however, not quite simple. First, depending on interpretation, formula may be (ABottom - AY) or even (ABottom + ATop - AY) -- I am not sure which is preferable.

There is no Bottom and Top in TCanvas, only Height, so: Height - AY

In terms of bottom and top it would be: (ABottom - ATop) - AY

I added some info here:
http://wiki.lazarus.freepascal.org/fpvectorial#Coordinates_in_FPVectorial

Also there is a question of text positioning -- do I understand correctly that FPVectorial uses given coordinates as bottom-left point of text rectangle instead of top-left?

Correct.

Felipe Monteiro de Carvalho

Reply by email