fpvectorial
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.
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>
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