Browse Source

Add extra argument to Fl_SVG_File_Surface constructor.

This makes processing of the underlying FILE object consistant by classes
Fl_SVG_File_Surface, Fl_EPS_File_Surface and Fl_PostScript_File_Surface.
pull/166/head
ManoloFLTK 5 years ago
parent
commit
0d594319c1
  1. 17
      FL/Fl_SVG_File_Surface.H
  2. 12
      src/drivers/SVG/Fl_SVG_File_Surface.cxx
  3. 2
      test/device.cxx

17
FL/Fl_SVG_File_Surface.H

@ -46,17 +46,21 @@ @@ -46,17 +46,21 @@
*/
class FL_EXPORT Fl_SVG_File_Surface : public Fl_Widget_Surface {
int width_, height_;
int (*closef_)(FILE*);
public:
/**
Constructor of the SVG drawing surface.
\param width,height Width and height of the graphics area in FLTK drawing units
\param svg A writable FILE pointer where the SVG data are to be sent. The resulting SVG data are not complete until after destruction of the Fl_SVG_File_Surface object or after calling close().
*/
Fl_SVG_File_Surface(int width, int height, FILE *svg);
\param closef If not NULL, the destructor and close() will call \p closef(svg) after all
SVG data has been sent. If NULL, \p fclose(svg) is called instead. This allows to close the FILE
pointer by, e.g., \p pclose, or, using a function such as \p "int keep_open(FILE*){return 0;}", to keep it open after
completion of all output to \p svg. Function \p closef should return non zero to indicate an error.
*/
Fl_SVG_File_Surface(int width, int height, FILE *svg, int (*closef)(FILE*) = NULL);
/**
Destructor.
The underlying FILE pointer remains open after destruction of the Fl_SVG_File_Surface object
unless close() was called.
The underlying FILE pointer is processed as by close().
*/
~Fl_SVG_File_Surface();
/** Returns the underlying FILE pointer */
@ -65,9 +69,10 @@ public: @@ -65,9 +69,10 @@ public:
virtual void translate(int x, int y);
virtual void untranslate();
virtual int printable_rect(int *w, int *h);
/** Closes with function fclose() the FILE pointer where SVG data is output.
/** Closes the FILE pointer where SVG data is output.
The underlying FILE is closed by function fclose() unless another function was set at object's construction time.
The only operation possible after this on the Fl_SVG_File_Surface object is its destruction.
\return The value returned by fclose(). */
\return The value returned by the closing function call. */
int close();
};

12
src/drivers/SVG/Fl_SVG_File_Surface.cxx

@ -305,7 +305,8 @@ int Fl_SVG_Graphics_Driver::descent() { @@ -305,7 +305,8 @@ int Fl_SVG_Graphics_Driver::descent() {
return Fl_Display_Device::display_device()->driver()->descent();
}
Fl_SVG_File_Surface::Fl_SVG_File_Surface(int w, int h, FILE *f) : Fl_Widget_Surface(new Fl_SVG_Graphics_Driver(f)) {
Fl_SVG_File_Surface::Fl_SVG_File_Surface(int w, int h, FILE *f, int (*closef)(FILE*)) : Fl_Widget_Surface(new Fl_SVG_Graphics_Driver(f)) {
closef_ = closef;
Fl_Window *win = Fl::first_window();
float s = (win ? Fl::screen_scale(win->screen_num()) : 1);
int sw = w * s, sh = h * s;
@ -321,12 +322,7 @@ Fl_SVG_File_Surface::Fl_SVG_File_Surface(int w, int h, FILE *f) : Fl_Widget_Surf @@ -321,12 +322,7 @@ Fl_SVG_File_Surface::Fl_SVG_File_Surface(int w, int h, FILE *f) : Fl_Widget_Surf
}
Fl_SVG_File_Surface::~Fl_SVG_File_Surface() {
Fl_SVG_Graphics_Driver *driver = (Fl_SVG_Graphics_Driver*)this->driver();
if (driver) {
fputs("</g></g></svg>\n", driver->file());
fflush(driver->file());
delete driver;
}
if (driver()) close();
}
FILE *Fl_SVG_File_Surface::file() {
@ -337,7 +333,7 @@ FILE *Fl_SVG_File_Surface::file() { @@ -337,7 +333,7 @@ FILE *Fl_SVG_File_Surface::file() {
int Fl_SVG_File_Surface::close() {
Fl_SVG_Graphics_Driver *driver = (Fl_SVG_Graphics_Driver*)this->driver();
fputs("</g></g></svg>\n", driver->file());
int retval = fclose(driver->file());
int retval = (closef_ ? closef_(driver->file()) : fclose(driver->file()));
delete driver;
this->driver(NULL);
return retval;

2
test/device.cxx

@ -677,7 +677,7 @@ void copy(Fl_Widget *, void *data) { @@ -677,7 +677,7 @@ void copy(Fl_Widget *, void *data) {
if (surface.file()) {
if (target->as_window()) surface.draw_decorated_window(target->as_window());
else surface.draw(target);
surface.close();
if (surface.close()) fl_message("Error while writing to SVG file %s", fnfc.filename());
}
}
}

Loading…
Cancel
Save