Difference between revisions of "fpvectorial"
Sekelsenmat (talk | contribs) |
Sekelsenmat (talk | contribs) |
||
Line 225: | Line 225: | ||
begin | begin | ||
Result := AHeight - AY; | Result := AHeight - AY; | ||
+ | end; | ||
+ | </delphi> | ||
+ | |||
+ | And there is also a difference regarding the text positioning. In TCanvas.TextOut the text is positioned based on the top-left coordinates of the text area. In fpvectorial it is based on the bottom-left. Again, there is a convenient routine in the unit '''fpvutils''' to convert between the coordinate systems: | ||
+ | |||
+ | <delphi> | ||
+ | {@@ | ||
+ | LCL Text is positioned based on the top-left corner of the text. | ||
+ | Besides that, one also needs to take the general coordinate change into account too. | ||
+ | |||
+ | @param ACanvasHeight Should receive TCanvas.Height | ||
+ | @param ATextHeight Should receive TFont.Size | ||
+ | } | ||
+ | function CanvasTextPosToFPVectorial(AY: Integer; ACanvasHeight, ATextHeight: Integer): Integer; | ||
+ | begin | ||
+ | Result := CanvasCoordsToFPVectorial(AY, ACanvasHeight) - ATextHeight; | ||
end; | end; | ||
</delphi> | </delphi> |
Revision as of 15:43, 16 June 2011
Introduction
FPVectorial offers support to read, modify and write vectorial images.
It's counterpart library is fcl-image, which works with raster images. FPVectorial comes with a unit which allows to draw a vectorial image to a TFPCustomCanvas, but no routines are provided to convert raster images to vectorial images.
Download
Downloading the latest code from FPC:
svn checkout http://svn.freepascal.org/svn/fpc/trunk/packages/fpvectorial fpvectorial
Downloading the fpvviewer project:
svn co https://lazarus-ccr.svn.sourceforge.net/svnroot/lazarus-ccr/applications/fpvviewer fpvviewer
Current file list
FPVectorial is located in Free Pascal 2.3.1+ in the directory fpc/packages/fpvectorial/src
The central unit is fpvectorial.
Readers for various image formats
- pdfvectorialreader - Read support for PDF files, supports compression, only reads the first page
- avisocncgcodereader - Read support for the G-Code from the Aviso CNC machine
- cdrvectorialreader - Initial work of a reader support for Corel Draw CDR files
- dxfvectorialreader - Read support for DXF, the Drawing eXchange Format utilized by the AutoCAD
- svgvectorialreader - Read support for SVG, in initial stage and optimized for reading SVG generated by fpvectorial at the moment.
- epsvectorialreader - Read support for Encapsulated PostScript, in initial stage.
Writers for various image formats
- avisocncgcodewriter - Write support for the G-Code from the Aviso CNC machine
- svgvectorialwriter - Write support for SVG. The most advanced writer at the moment. Supports lines, curves and text. Supports pen color and width.
Other units
- fpvtocanvas - Converts a vectorial document to a TFPCustomCanvas descendent (like TCanvas). Essentially converts the vectorial image to a raster image
- fpvutils - Utility functions which don't belong to fpvectorial.pas or that would bring unwanted dependencies to it. Color conversion functions.
- pdfvrlexico, pdfvrsemantico, pdfvrsintatico, avisozlib - Other units from the PDF reader, don't use directly
Formats
The PDF format is developed by Adobe (tm) and it is fully documented. Inside, PDF is simply text, so it is rather easy to parse, but parts of this text are usually can be compressed.
Link to the PDF Reference: http://www.adobe.com/devnet/pdf/pdf_reference.html
CorelDraw
Corel does not release public documentation for it's CorelDraw file format. Any information about this format depends on reverse engineering and guessing. The format is binary and consists of a tree of chunks.
See also fpvectorial#fpcorelexplorer
SVG
Scalable Vectorial Graphics (SVG) is a standard from W3C for vectorial graphics. It is the native format of Inkscape and stores it's data as a single, uncompressed XML file with the extension ".svg".
Link to the specification: http://www.w3.org/TR/SVG/
A very serious shortcoming in SVG is that it does not allow real world units to be specified in the drawing routines, only pixel values accepted, which makes it impossible to have a document with real world units. This can be worked around by simply hardcoding to a fixed DPI resolution, which is 90 DPI for Inkscape. Read more here:
- http://stackoverflow.com/questions/1346922/svg-and-dpi-absolute-units-and-user-units-inkscape-vs-firefox-vs-imagemagick
- http://www.inkscapeforum.com/viewtopic.php?f=16&t=5904
- http://www.inkscapeforum.com/viewtopic.php?f=16&t=3700&p=17179&hilit=units+path#p17179
Following Inkscape and the Opera Browser, FPVectorial also hardcodes to 90 DPI.
DXF
The Drawing eXchange Format from Autodesk AutoCAD. This format is documented, but the quality of the documentation is rather low, lacking adequate explanations.
The format itself is rather simple, composed by text. Each unit is composed of two lines, the first one being a number describing the type of data and then another line with the value itself, which might be text or a number.
The most common problem in other DXF reading libraries is that they don't support the actual DXF writen by newer versions of AutoCAD. This is not the case with fpvectorial, which was tested with AutoCAD 2000 and also the previous simpler formats.
Link to the documentation:
The software fpvviewer can be utilized to show the internal structure of a DXF file. This software can be found in the lazarus-ccr svn in applications/fpvviewer. More info here: fpvectorial#fpvviewer
PostScript
PostScript is a graphics drawing programming language and fpvectorial has an interpreter which can execute PostScript and convert it's commands into it's internal format. This is done by reader modules, for example epsvectorialreader. The interpreter is separated into a Tokenizer and an Executer. The tokens are classified into 3 types: Procedures, ExpressionTokens and Comments. Procedures are inside a {..} block in PostScript and in FPVectorial they are not tokenezed in the first pass, but instead on the first usage. The entire document is tokenized in one run, before executing the tokens, except for the procedures. The PostScript interpreter contains a Stack and a lookup Dictionary, as specified in PostScript.
PostScript can be embedded into various formats, and the interpreter currently supports the EPS format.
References:
- http://en.wikipedia.org/wiki/Encapsulated_PostScript
- http://partners.adobe.com/public/developer/en/ps/PLRM.pdf
Types of data in FPVectorial
FPVectorial holds lists of three types of objects: paths, text and entities.
Essentially paths are sequences of points, through which run lines and bezier curves. A path can be a line, a bezier, a polyline, a polybezier or any combination of these elements. The main characteristic of paths is that they can be utilized to guide a milling machine, also known as CNC machine, into physically executing the drawing. Before being able to execute a drawing which contains either text or entities in a CNC machine, these items need to be converted to paths.
Text, just like the name says are strings drawn with fonts.
Entities are things like circles, arcs, ellipses, rotated ellipses, etc.
Usage examples
Format conversion
This example loads a PDF, converts it to G-Code and writes the G-Code to a TStrings descendent
<delphi> uses
fpvectorial, pdfvectorialreader, avisocncgcodewriter;
var
Vec: TvVectorialDocument;
begin
if dialogoAbrir.Execute() then begin Vec := TvVectorialDocument.Create; try Vec.ReadFromFile(dialogoAbrir.FileName, vfPDF); Vec.WriteToStrings(synCodigo.Lines, vfGCodeAvisoCNCPrototipoV5); finally Vec.Free; end; end;
end; </delphi>
Drawing paths, bezier lines and text
This example shows how to render various elements.
<delphi> { Author: Felipe Monteiro de Carvalho
License: Public Domain } program fpvwritetest;
{$mode objfpc}{$H+}
uses
fpvectorial, svgvectorialwriter;
const
cFormat = vfSVG; cExtension = '.svg';
var
Vec: TvVectorialDocument;
begin
Vec := TvVectorialDocument.Create; try // All documents are 10cm x 10cm Vec.Width := 100; Vec.Height := 100;
// ...
// multi_test_1 Combines various elements Vec.Clear; Vec.StartPath(0, 20); Vec.AddLineToPath(30, 30); Vec.EndPath(); Vec.StartPath(0, 0); Vec.AddLineToPath(100, 0); Vec.AddLineToPath(100, 100); Vec.AddLineToPath(0, 100); Vec.AddLineToPath(0, 0); Vec.EndPath(); Vec.StartPath(0, 0); Vec.AddLineToPath(10, 10); Vec.AddBezierToPath(10, 20, 20, 20, 20, 10); Vec.AddLineToPath(30, 0); Vec.EndPath(); Vec.AddText(10, 10, 0, '10,10 Some text in english.'); Vec.AddText(20, 20, 0, '20, 20 Mówić, cześć, Włosku, Parabéns.'); Vec.AddText(30, 30, 0, '30, 30 森林,是一个高密'); Vec.WriteToFile('multi_test_1' + cExtension, cFormat); finally Vec.Free; end;
end. </delphi>
And here is the output of this example when rendered by the Opera Browser:
Drawing Aligned Coordinates
One can either draw horizontal coordinates:
<delphi>Vec.AddAlignedDimension(Make2DPoint(100, 50), Make2DPoint(200, 100), Make2DPoint(100, 150), Make2DPoint(200, 150));</delphi>
Or vertical coordinates:
<delphi>Vec.AddAlignedDimension(Make2DPoint(50, 250), Make2DPoint(100, 200), Make2DPoint(150, 250), Make2DPoint(150, 200));</delphi>
Coordinates in FPVectorial
It is important to note that the coordinates in FPVectorial are different from the ones in TCanvas. TCanvas uses the coordinates from the Windows API, with the origin in the top-left corner and with the X axis growing to the right and the Y axis growing to the bottom. FPVectorial, on the other hand, uses the coordinates which are commonly used in drawing programs, such as CAD software, Inkscape, the Mac OS X APIs (Carbon and Cocoa), etc. In FPVectorial the origin is in bottom-left part of the document. The X axis grows to the right and the Y axis to the top.
Because of these differences, one should be careful when converting coordinates between TCanvas and FPVectorial. To convert coordinates between TCanvas and FPVectorial, one needs the height of the Canvas. The following routine from the unit fpvutils can be used to execute this conversion:
<delphi> {@@ 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; </delphi>
And there is also a difference regarding the text positioning. In TCanvas.TextOut the text is positioned based on the top-left coordinates of the text area. In fpvectorial it is based on the bottom-left. Again, there is a convenient routine in the unit fpvutils to convert between the coordinate systems:
<delphi> {@@
LCL Text is positioned based on the top-left corner of the text. Besides that, one also needs to take the general coordinate change into account too.
@param ACanvasHeight Should receive TCanvas.Height @param ATextHeight Should receive TFont.Size
} function CanvasTextPosToFPVectorial(AY: Integer; ACanvasHeight, ATextHeight: Integer): Integer; begin
Result := CanvasCoordsToFPVectorial(AY, ACanvasHeight) - ATextHeight;
end; </delphi>
More examples
Please see in the FPC repository: http://svn.freepascal.org/cgi-bin/viewvc.cgi/trunk/packages/fpvectorial/examples/
fpvviewer
The Free Pascal Vectorial Viewer is a sample application which implements a simple CAD viewer using fpvectorial.
Besides being able to visualize vectorial drawings, this software can also help in exploring the internal structure of DXF file:
It's source code can be found in the Lazarus-CCR in the directory application/fpvviewer
Link to download the Lazarus-CCR from subversion:
svn co https://lazarus-ccr.svn.sourceforge.net/svnroot/lazarus-ccr lazarus-ccr
fpcorelexplorer
An application was developed for helping studying the CDR file format. At the moment this application can identify the version of CorelDraw files. It is located in fpctrunk/packages/fpvectorial/examples/fpcorelexplorer.lpi
Articles about fpvectorial
- Revista Clube Delphi Edition #131: http://www.devmedia.com.br/assgold/listmag.asp?site=3
Go to back Packages List