Move class Fl_SVG_File_Surface from libfltk to libfltk_images.
File examples/SVG_File_Surface.cxx is no longer useful because it was a very partial implementation of what is now class Fl_SVG_File_Surface.
This commit is contained in:
parent
26e6c3f930
commit
8d4b0c15f7
@ -56,14 +56,13 @@ class Fl_Widget;
|
||||
|
||||
Class Fl_Surface_Device can also be derived to define new kinds of graphical output
|
||||
usable with FLTK drawing functions.
|
||||
An example would be to draw to an SVG file. This would require to create a new class,
|
||||
say SVG_Surface, derived from class Fl_Surface_Device, and another new class,
|
||||
say SVG_Graphics_Driver, derived from class Fl_Graphics_Driver.
|
||||
Class SVG_Graphics_Driver should implement all virtual methods of the Fl_Graphics_Driver class
|
||||
to support all FLTK drawing functions and have them draw into SVG files. Alternatively,
|
||||
class SVG_Graphics_Driver could implement only some virtual methods, and only part of
|
||||
the FLTK drawing API would be usable when drawing to SVG files
|
||||
(see examples/SVG_File_Surface.cxx for a small, working implementation of this procedure).
|
||||
An example would be to draw to a PDF file. This would require to create a new class,
|
||||
say PDF_File_Surface, derived from class Fl_Surface_Device, and another new class,
|
||||
say PDF_Graphics_Driver, derived from class Fl_Graphics_Driver.
|
||||
Class PDF_Graphics_Driver should implement all virtual methods of the Fl_Graphics_Driver class
|
||||
to support all FLTK drawing functions and have them draw into PDF files. Alternatively,
|
||||
class PDF_Graphics_Driver could implement only some virtual methods, and only part of
|
||||
the FLTK drawing API would be usable when drawing to PDF files.
|
||||
*/
|
||||
class FL_EXPORT Fl_Surface_Device {
|
||||
/** The graphics driver in use by this surface. */
|
||||
|
@ -39,8 +39,9 @@
|
||||
fclose(svg);
|
||||
}
|
||||
\endcode
|
||||
\note FLTK uses the PNG and JPEG libraries to encode images to the SVG format. If JPEG is
|
||||
not available at application build time, PNG is enough (but produces a quite larger output).
|
||||
\note FLTK uses the PNG and JPEG libraries to encode images to the SVG format.
|
||||
For this reason, class Fl_SVG_File_Surface is placed in the fltk_images library.
|
||||
If JPEG is not available at application build time, PNG is enough (but produces a quite larger output).
|
||||
If PNG isn't available either, images don't appear in the SVG output.
|
||||
*/
|
||||
class FL_EXPORT Fl_SVG_File_Surface : public Fl_Widget_Surface {
|
||||
|
@ -24,7 +24,6 @@ ALL = browser-simple$(EXEEXT) \
|
||||
progress-simple$(EXEEXT) \
|
||||
shapedwindow$(EXEEXT) \
|
||||
simple-terminal$(EXEEXT) \
|
||||
SVG_File_Surface$(EXEEXT) \
|
||||
table-as-container$(EXEEXT) \
|
||||
table-simple$(EXEEXT) \
|
||||
table-sort$(EXEEXT) \
|
||||
|
@ -1,218 +0,0 @@
|
||||
//
|
||||
// "$Id$"
|
||||
//
|
||||
// Subclassing Fl_Surface_Device and Fl_Graphics_Driver in the Fast Light Tool Kit (FLTK).
|
||||
//
|
||||
// Copyright 2018 by Bill Spitzak and others.
|
||||
//
|
||||
// This library is free software. Distribution and use rights are outlined in
|
||||
// the file "COPYING" which should have been included with this file. If this
|
||||
// file is missing or damaged, see the license at:
|
||||
//
|
||||
// http://www.fltk.org/COPYING.php
|
||||
//
|
||||
// Please report all bugs and problems on the following page:
|
||||
//
|
||||
// http://www.fltk.org/str.php
|
||||
//
|
||||
|
||||
// Partial, working implementation of how to draw into an SVG file using the standard
|
||||
// FLTK drawing API.
|
||||
// File hello.svg is created in the current directory.
|
||||
|
||||
#include <FL/fl_draw.H>
|
||||
#include <stdio.h>
|
||||
#include <FL/Fl_Device.H>
|
||||
#include <FL/Fl_Graphics_Driver.H>
|
||||
#include <FL/Fl.H>
|
||||
#include <FL/Fl_Window.H>
|
||||
#include <FL/Fl_Box.H>
|
||||
|
||||
class SVG_Graphics_Driver : public Fl_Graphics_Driver {
|
||||
FILE *out_;
|
||||
int width_;
|
||||
const char *linecap_;
|
||||
uchar red_, green_, blue_;
|
||||
public:
|
||||
SVG_Graphics_Driver(FILE*);
|
||||
~SVG_Graphics_Driver();
|
||||
FILE* file() {return out_;}
|
||||
protected:
|
||||
const char *family_;
|
||||
const char *bold_;
|
||||
const char *style_;
|
||||
void rect(int x, int y, int w, int h);
|
||||
void rectf(int x, int y, int w, int h);
|
||||
void line_style(int style, int width, char *dashes=0);
|
||||
void line(int x1, int y1, int x2, int y2);
|
||||
void font_(int f, int s);
|
||||
void font(int f, int s);
|
||||
void draw(const char *str, int n, int x, int y);
|
||||
void draw(const char*, int, float, float) ;
|
||||
void draw(int, const char*, int, int, int) ;
|
||||
void color(uchar r, uchar g, uchar b);
|
||||
void color(Fl_Color c);
|
||||
double width(const char*, int) ;
|
||||
int height() ;
|
||||
int descent() ;
|
||||
};
|
||||
|
||||
class SVG_File_Surface : public Fl_Surface_Device {
|
||||
int width_, height_;
|
||||
public:
|
||||
SVG_File_Surface(int width, int height, FILE*);
|
||||
int width() { return width_; }
|
||||
int height() { return height_; }
|
||||
~SVG_File_Surface();
|
||||
};
|
||||
|
||||
|
||||
SVG_Graphics_Driver::SVG_Graphics_Driver(FILE *f) {
|
||||
out_ = f;
|
||||
width_ = 1;
|
||||
linecap_ = "butt";
|
||||
family_ = "";
|
||||
bold_ = "";
|
||||
style_ = "";
|
||||
red_ = green_ = blue_ = 0;
|
||||
}
|
||||
|
||||
SVG_Graphics_Driver::~SVG_Graphics_Driver()
|
||||
{
|
||||
}
|
||||
|
||||
void SVG_Graphics_Driver::rect(int x, int y, int w, int h) {
|
||||
fprintf(out_, "<rect x=\"%d\" y=\"%d\" width=\"%d\" height=\"%d\" "
|
||||
"fill=\"none\" stroke=\"rgb(%u,%u,%u)\" stroke-width=\"%d\"/>\n", x, y, w, h, red_, green_, blue_, width_);
|
||||
}
|
||||
|
||||
void SVG_Graphics_Driver::rectf(int x, int y, int w, int h) {
|
||||
fprintf(out_, "<rect x=\"%d\" y=\"%d\" width=\"%d\" height=\"%d\" "
|
||||
"fill=\"rgb(%u,%u,%u)\" />\n", x, y, w, h, red_, green_, blue_);
|
||||
}
|
||||
|
||||
void SVG_Graphics_Driver::line(int x1, int y1, int x2, int y2) {
|
||||
fprintf(out_,
|
||||
"<line x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" "
|
||||
"style=\"stroke:rgb(%u,%u,%u);stroke-width:%d;stroke-linecap:%s\" />\n",
|
||||
x1,y1,x2,y2, red_, green_, blue_, width_, linecap_);
|
||||
}
|
||||
|
||||
void SVG_Graphics_Driver::font_(int ft, int s) {
|
||||
Fl_Graphics_Driver::font(ft, s);
|
||||
int famnum = ft/4;
|
||||
if (famnum == 0) family_ = "Helvetica";
|
||||
else if (famnum == 1) family_ = "Courier";
|
||||
else family_ = "Times";
|
||||
int modulo = ft % 4;
|
||||
int use_bold = modulo == 1 || modulo == 3;
|
||||
int use_italic = modulo >= 2;
|
||||
bold_ = ( use_bold ? " font-weight=\"bold\"" : "" );
|
||||
style_ = ( use_italic ? " font-style=\"italic\"" : "" );
|
||||
if (use_italic && famnum != 2) style_ = " font-style=\"oblique\"";
|
||||
}
|
||||
|
||||
void SVG_Graphics_Driver::font(int ft, int s) {
|
||||
Fl_Display_Device::display_device()->driver()->font(ft, s);
|
||||
font_(ft, s);
|
||||
}
|
||||
|
||||
void SVG_Graphics_Driver::line_style(int style, int width, char *dashes) {
|
||||
if (width == 0) width = 1;
|
||||
width_ = width;
|
||||
if (style & FL_CAP_SQUARE) linecap_ = "square";
|
||||
if (style & FL_CAP_ROUND) linecap_ = "round";
|
||||
else linecap_ = "butt";
|
||||
}
|
||||
|
||||
void SVG_Graphics_Driver::draw(const char *str, int n, int x, int y) {
|
||||
// Caution: Internet Explorer ignores the xml:space="preserve" attribute
|
||||
// work-around: replace all spaces by no-break space = U+00A0 = 0xC2-0xA0 (UTF-8) before sending to IE
|
||||
fprintf(out_, "<text x=\"%d\" y=\"%d\" font-family=\"%s\"%s%s font-size=\"%d\" "
|
||||
"xml:space=\"preserve\" "
|
||||
" fill=\"rgb(%u,%u,%u)\" textLength=\"%d\">%.*s</text>\n",x, y, family_, bold_, style_, size(), red_, green_, blue_, (int)width(str, n), n, str);
|
||||
|
||||
}
|
||||
|
||||
void SVG_Graphics_Driver::draw(const char* str, int n, float fx, float fy) {
|
||||
return draw(str, n, (int)fx, (int)fy);
|
||||
}
|
||||
|
||||
void SVG_Graphics_Driver::draw(int angle, const char* str, int n, int x, int y) {
|
||||
fprintf(out_, "<g transform=\"translate(%d,%d) rotate(%d)\">", x, y, -angle);
|
||||
draw(str, n, 0, 0);
|
||||
fputs("</g>\n", out_);
|
||||
}
|
||||
|
||||
void SVG_Graphics_Driver::color(Fl_Color c) {
|
||||
Fl_Graphics_Driver::color(c);
|
||||
Fl::get_color(c, red_, green_, blue_);
|
||||
}
|
||||
|
||||
void SVG_Graphics_Driver::color(uchar r, uchar g, uchar b) {
|
||||
red_ = r;
|
||||
green_ = g;
|
||||
blue_ = b;
|
||||
}
|
||||
|
||||
double SVG_Graphics_Driver::width(const char* str, int l) {
|
||||
return Fl_Display_Device::display_device()->driver()->width(str, l);
|
||||
}
|
||||
|
||||
int SVG_Graphics_Driver::height() {
|
||||
return Fl_Display_Device::display_device()->driver()->height();
|
||||
}
|
||||
|
||||
int SVG_Graphics_Driver::descent() {
|
||||
return Fl_Display_Device::display_device()->driver()->descent();
|
||||
}
|
||||
|
||||
SVG_File_Surface::SVG_File_Surface(int w, int h, FILE *f) : Fl_Surface_Device(NULL) {
|
||||
fprintf(f,
|
||||
"<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n"
|
||||
"<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \n"
|
||||
"\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n"
|
||||
"<svg width=\"%dpx\" height=\"%dpx\" viewBox=\"0 0 %d %d\"\n"
|
||||
"xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">\n", w, h, w, h);
|
||||
width_ = w; height_ = h;
|
||||
driver(new SVG_Graphics_Driver(f));
|
||||
}
|
||||
|
||||
SVG_File_Surface::~SVG_File_Surface() {
|
||||
SVG_Graphics_Driver *driver = (SVG_Graphics_Driver*)this->driver();
|
||||
fputs("</svg>\n", driver->file());
|
||||
fflush(driver->file());
|
||||
delete driver;
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
Fl_Window *window = new Fl_Window(340,180);
|
||||
Fl_Box *box = new Fl_Box(20,40,300,100,"Hello, World!");
|
||||
box->box(FL_UP_BOX);
|
||||
box->labelfont(FL_BOLD+FL_ITALIC);
|
||||
box->labelsize(36);
|
||||
box->labeltype(FL_SHADOW_LABEL);
|
||||
window->end();
|
||||
window->show(argc, argv);
|
||||
|
||||
FILE *out = fl_fopen("hello.svg", "w");
|
||||
if (out) {
|
||||
SVG_File_Surface *svg = new SVG_File_Surface(box->w(), box->h(), out);
|
||||
Fl_Surface_Device::push_current(svg);
|
||||
fl_color(box->color());
|
||||
fl_rectf(0, 0, box->w(), box->h());
|
||||
fl_font(box->labelfont(), 36);
|
||||
fl_color(box->labelcolor());
|
||||
fl_draw(box->label(), 5, 50);
|
||||
Fl_Surface_Device::pop_current();
|
||||
delete svg;
|
||||
fclose(out);
|
||||
}
|
||||
|
||||
return Fl::run();
|
||||
}
|
||||
|
||||
//
|
||||
// End of "$Id$".
|
||||
//
|
@ -161,7 +161,6 @@ set (CPPFILES
|
||||
fl_utf8.cxx
|
||||
fl_encoding_latin1.cxx
|
||||
fl_encoding_mac_roman.cxx
|
||||
drivers/SVG/Fl_SVG_File_Surface.cxx
|
||||
)
|
||||
|
||||
# find all header files in source directory <FL/...>
|
||||
@ -423,6 +422,7 @@ set (IMGCPPFILES
|
||||
Fl_PNM_Image.cxx
|
||||
Fl_Image_Reader.cxx
|
||||
Fl_SVG_Image.cxx
|
||||
drivers/SVG/Fl_SVG_File_Surface.cxx
|
||||
)
|
||||
|
||||
set (CFILES
|
||||
|
@ -212,7 +212,8 @@ IMGCPPFILES = \
|
||||
Fl_PNG_Image.cxx \
|
||||
Fl_PNM_Image.cxx \
|
||||
Fl_Image_Reader.cxx \
|
||||
Fl_SVG_Image.cxx
|
||||
Fl_SVG_Image.cxx \
|
||||
drivers/SVG/Fl_SVG_File_Surface.cxx
|
||||
|
||||
CFILES = fl_call_main.c flstring.c numericsort.c vsnprintf.c
|
||||
|
||||
@ -305,8 +306,7 @@ GDICFILES = \
|
||||
|
||||
PSCPPFILES = \
|
||||
drivers/PostScript/Fl_PostScript.cxx \
|
||||
drivers/PostScript/Fl_PostScript_image.cxx \
|
||||
drivers/SVG/Fl_SVG_File_Surface.cxx
|
||||
drivers/PostScript/Fl_PostScript_image.cxx
|
||||
|
||||
################################################################
|
||||
FLTKFLAGS = -DFL_LIBRARY
|
||||
|
@ -254,6 +254,30 @@ drivers/PostScript/Fl_PostScript_image.o: ../FL/Fl_Widget.H
|
||||
drivers/PostScript/Fl_PostScript_image.o: ../FL/Fl_Widget_Surface.H
|
||||
drivers/PostScript/Fl_PostScript_image.o: ../FL/Fl_Window.H
|
||||
drivers/PostScript/Fl_PostScript_image.o: ../FL/platform_types.h
|
||||
drivers/SVG/Fl_SVG_File_Surface.o: ../config.h
|
||||
drivers/SVG/Fl_SVG_File_Surface.o: ../FL/abi-version.h
|
||||
drivers/SVG/Fl_SVG_File_Surface.o: ../FL/Enumerations.H
|
||||
drivers/SVG/Fl_SVG_File_Surface.o: ../FL/Fl.H
|
||||
drivers/SVG/Fl_SVG_File_Surface.o: ../FL/Fl_Bitmap.H
|
||||
drivers/SVG/Fl_SVG_File_Surface.o: ../FL/Fl_Cairo.H
|
||||
drivers/SVG/Fl_SVG_File_Surface.o: ../FL/Fl_Device.H
|
||||
drivers/SVG/Fl_SVG_File_Surface.o: ../FL/fl_draw.H
|
||||
drivers/SVG/Fl_SVG_File_Surface.o: ../FL/Fl_Export.H
|
||||
drivers/SVG/Fl_SVG_File_Surface.o: ../FL/Fl_Graphics_Driver.H
|
||||
drivers/SVG/Fl_SVG_File_Surface.o: ../FL/Fl_Group.H
|
||||
drivers/SVG/Fl_SVG_File_Surface.o: ../FL/Fl_Image.H
|
||||
drivers/SVG/Fl_SVG_File_Surface.o: ../FL/Fl_Pixmap.H
|
||||
drivers/SVG/Fl_SVG_File_Surface.o: ../FL/Fl_Plugin.H
|
||||
drivers/SVG/Fl_SVG_File_Surface.o: ../FL/Fl_Preferences.H
|
||||
drivers/SVG/Fl_SVG_File_Surface.o: ../FL/Fl_RGB_Image.H
|
||||
drivers/SVG/Fl_SVG_File_Surface.o: ../FL/Fl_SVG_File_Surface.H
|
||||
drivers/SVG/Fl_SVG_File_Surface.o: ../FL/fl_types.h
|
||||
drivers/SVG/Fl_SVG_File_Surface.o: ../FL/fl_utf8.h
|
||||
drivers/SVG/Fl_SVG_File_Surface.o: ../FL/Fl_Widget.H
|
||||
drivers/SVG/Fl_SVG_File_Surface.o: ../FL/Fl_Widget_Surface.H
|
||||
drivers/SVG/Fl_SVG_File_Surface.o: ../FL/Fl_Window.H
|
||||
drivers/SVG/Fl_SVG_File_Surface.o: ../FL/math.h
|
||||
drivers/SVG/Fl_SVG_File_Surface.o: ../FL/platform_types.h
|
||||
drivers/X11/Fl_X11_Screen_Driver.o: ../config.h
|
||||
drivers/X11/Fl_X11_Screen_Driver.o: ../FL/abi-version.h
|
||||
drivers/X11/Fl_X11_Screen_Driver.o: ../FL/Enumerations.H
|
||||
|
@ -370,9 +370,9 @@ demo$(EXEEXT): demo.o
|
||||
$(OSX_ONLY) mkdir -p demo.app/Contents/Resources
|
||||
$(OSX_ONLY) cp -f demo.menu demo.app/Contents/Resources/
|
||||
|
||||
device$(EXEEXT): device.o
|
||||
device$(EXEEXT): device.o $(IMGLIBNAME)
|
||||
echo Linking $@...
|
||||
$(CXX) $(ARCHFLAGS) $(CXXFLAGS) $(LDFLAGS) device.o -o $@ $(LINKFLTK) $(IMAGELIBS) $(LDLIBS)
|
||||
$(CXX) $(ARCHFLAGS) $(CXXFLAGS) $(LDFLAGS) device.o -o $@ $(LINKFLTKIMG) $(LDLIBS)
|
||||
$(OSX_ONLY) ../fltk-config --post $@
|
||||
|
||||
doublebuffer$(EXEEXT): doublebuffer.o
|
||||
|
@ -530,6 +530,7 @@ device.o: ../FL/Fl_Light_Button.H
|
||||
device.o: ../FL/Fl_Menu_.H
|
||||
device.o: ../FL/Fl_Menu_Button.H
|
||||
device.o: ../FL/Fl_Menu_Item.H
|
||||
device.o: ../FL/Fl_Native_File_Chooser.H
|
||||
device.o: ../FL/Fl_Overlay_Window.H
|
||||
device.o: ../FL/Fl_Paged_Device.H
|
||||
device.o: ../FL/Fl_Pixmap.H
|
||||
@ -544,6 +545,7 @@ device.o: ../FL/Fl_Round_Button.H
|
||||
device.o: ../FL/Fl_Scrollbar.H
|
||||
device.o: ../FL/Fl_Shared_Image.H
|
||||
device.o: ../FL/Fl_Slider.H
|
||||
device.o: ../FL/Fl_SVG_File_Surface.H
|
||||
device.o: ../FL/Fl_Tile.H
|
||||
device.o: ../FL/fl_types.h
|
||||
device.o: ../FL/fl_utf8.h
|
||||
@ -1492,6 +1494,7 @@ pixmap_browser.o: ../FL/Fl_Menu_.H
|
||||
pixmap_browser.o: ../FL/Fl_Menu_Button.H
|
||||
pixmap_browser.o: ../FL/Fl_Menu_Item.H
|
||||
pixmap_browser.o: ../FL/fl_message.H
|
||||
pixmap_browser.o: ../FL/Fl_Native_File_Chooser.H
|
||||
pixmap_browser.o: ../FL/Fl_Paged_Device.H
|
||||
pixmap_browser.o: ../FL/Fl_Plugin.H
|
||||
pixmap_browser.o: ../FL/Fl_Preferences.H
|
||||
@ -1500,6 +1503,7 @@ pixmap_browser.o: ../FL/Fl_Return_Button.H
|
||||
pixmap_browser.o: ../FL/Fl_Scrollbar.H
|
||||
pixmap_browser.o: ../FL/Fl_Shared_Image.H
|
||||
pixmap_browser.o: ../FL/Fl_Slider.H
|
||||
pixmap_browser.o: ../FL/Fl_SVG_File_Surface.H
|
||||
pixmap_browser.o: ../FL/Fl_Tile.H
|
||||
pixmap_browser.o: ../FL/fl_types.h
|
||||
pixmap_browser.o: ../FL/fl_utf8.h
|
||||
|
Loading…
Reference in New Issue
Block a user