diff options
| author | ManoloFLTK <41016272+ManoloFLTK@users.noreply.github.com> | 2020-11-03 18:05:10 +0100 |
|---|---|---|
| committer | ManoloFLTK <41016272+ManoloFLTK@users.noreply.github.com> | 2020-11-03 18:05:18 +0100 |
| commit | 0d594319c1f423679c7e7cd712003921da103c70 (patch) | |
| tree | ecdc31a39baa10de21cef5c0fcb95b5e1a1a9142 | |
| parent | 5132bb6a062b68be8f0e9e84fb5ae979ab613ea5 (diff) | |
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.
| -rw-r--r-- | FL/Fl_SVG_File_Surface.H | 17 | ||||
| -rw-r--r-- | src/drivers/SVG/Fl_SVG_File_Surface.cxx | 12 | ||||
| -rw-r--r-- | test/device.cxx | 2 |
3 files changed, 16 insertions, 15 deletions
diff --git a/FL/Fl_SVG_File_Surface.H b/FL/Fl_SVG_File_Surface.H index 74437326f..67d0aa4db 100644 --- a/FL/Fl_SVG_File_Surface.H +++ b/FL/Fl_SVG_File_Surface.H @@ -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: 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(); }; diff --git a/src/drivers/SVG/Fl_SVG_File_Surface.cxx b/src/drivers/SVG/Fl_SVG_File_Surface.cxx index ae0172fc4..2a48ac8f6 100644 --- a/src/drivers/SVG/Fl_SVG_File_Surface.cxx +++ b/src/drivers/SVG/Fl_SVG_File_Surface.cxx @@ -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 } 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() { 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; diff --git a/test/device.cxx b/test/device.cxx index 8b04e53d2..518612c61 100644 --- a/test/device.cxx +++ b/test/device.cxx @@ -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()); } } } |
