TPCCLIB
Loading...
Searching...
No Matches
svg_file.c File Reference

File io for TPC SVG C library. More...

#include "libtpcsvg.h"

Go to the source code of this file.

Functions

FILE * svg_initiate (const char *filename, const double height, const double width, struct svg_viewports *vp, char *errmsg, int verbose)
 
int svg_close (FILE *fp, char *errmsg, int verbose)
 
FILE * svg_xhtml_initiate (const char *filename, const char *XHTML_title, char *errmsg, int verbose)
 
int svg_xhtml_close (FILE *fp, char *errmsg, int verbose)
 
int svg_xhtml_svg_open (FILE *fp, const double height, const double width, struct svg_viewports *vp, char *errmsg, int verbose)
 
int svg_xhtml_svg_close (FILE *fp, char *errmsg, int verbose)
 
int svg_write (FILE *fp, const char *svg_string, char *errmsg, int verbose)
 

Variables

int SVG_INLINE = 0
 

Detailed Description

File io for TPC SVG C library.

Author
Vesa Oikonen
Todo
If Inkscape, Gimp and Batik start to support plot data outside of viewport, then decide whether to write that data in SVG again. Embedded SVG in HTML5.

Definition in file svg_file.c.

Function Documentation

◆ svg_close()

int svg_close ( FILE * fp,
char * errmsg,
int verbose )

Close SVG graphics file.

Returns
Returns 0 if successful, <>0 in case of error.
See also
svg_initiate
Parameters
fpSVG graphics file pointer.
errmsgChar pointer to string (at least of length 128) where possible error description is copied; set to NULL if not necessary.
verboseVerbose level; if zero, then nothing is printed to stderr or stdout.

Definition at line 107 of file svg_file.c.

115 {
116 if(verbose>0) printf("%s(fp, errmsg, %d)\n", __func__, verbose);
117
118 if(svg_write(fp, "</svg>\n", errmsg, verbose-5)!=0) {fclose(fp); return(1);}
119 fflush(fp); fclose(fp);
120 return(0);
121}
int svg_write(FILE *fp, const char *svg_string, char *errmsg, int verbose)
Definition svg_file.c:304

Referenced by plot_fit_svg(), plot_fitrange_svg(), and plot_svg().

◆ svg_initiate()

FILE * svg_initiate ( const char * filename,
const double height,
const double width,
struct svg_viewports * vp,
char * errmsg,
int verbose )

Initiate a new SVG graphics file.

If file with same name exists, it is overwritten without backup.

Returns
Returns pointer to the file if successful and NULL in case of an error.
See also
svg_close, svg_xhtml_initiate
Parameters
filenameFile name for SVG graphics.
heightPlot height in cm; 0, if not predefined.
widthPlot width in cm; 0, if not predefined.
vpStruct containing the viewport sizes.
errmsgChar pointer to string (at least of length 128) where possible error description is copied; set to NULL if not necessary.
verboseVerbose level; if zero, then nothing is printed to stderr or stdout.

Definition at line 22 of file svg_file.c.

36 {
37 FILE *fp;
38 char tmp[1024], *cptr;
39
40 if(verbose>0)
41 printf("%s(%s, %g, %g, vp, errmsg, %d)\n", __func__, filename, height, width, verbose);
42
43 /* Check input */
44 if(filename==NULL || vp==NULL) return(NULL);
45 if(vp->main_viewport.w<3 || vp->main_viewport.h<3) return(NULL);
46
47 /* Open file for write */
48 fp=fopen(filename, "w");
49 if(fp==NULL) {
50 if(errmsg!=NULL) strcpy(errmsg, "cannot open file for write");
51 return(fp);
52 }
53
54 strcpy(tmp, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
55 //strcat(tmp, "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n");
56 //strcat(tmp, " \"https://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n");
57 strcat(tmp, "<svg version=\"1.1\" baseProfile=\"full\"\n");
58 strcat(tmp, " xmlns=\"https://www.w3.org/2000/svg\"\n");
59 strcat(tmp, " xmlns:xlink=\"https://www.w3.org/1999/xlink\"\n");
60 strcat(tmp, " xmlns:ev=\"https://www.w3.org/2001/xml-events\"");
61 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {
62 fclose(fp); remove(filename); return(NULL);}
63
64 sprintf(tmp, "\n viewBox=\"0 0 %d %d\"",
66 strcat(tmp, "\n preserveAspectRatio=\"xMinYMin meet\"");
67 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {
68 fclose(fp); remove(filename); return(NULL);}
69
70 if(width>0.0) {
71 sprintf(tmp, "\n width=\"%gcm\"", width);
72 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {
73 fclose(fp); remove(filename); return(NULL);}
74 }
75 if(height>0.0) {
76 sprintf(tmp, "\n height=\"%gcm\"", height);
77 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {
78 fclose(fp); remove(filename); return(NULL);}
79 }
80
81 strcpy(tmp, ">\n");
82 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {
83 fclose(fp); remove(filename); return(NULL);}
84
85 /* Write file name as title element */
86 cptr=strrchr(filename, '/'); if(cptr==NULL) cptr=strrchr(filename, '\\');
87 if(cptr!=NULL) cptr++; else cptr=(char*)filename;
88 if(cptr!=NULL) {
89 sprintf(tmp, " <title>%s</title>\n", cptr);
90 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {
91 fclose(fp); remove(filename); return(NULL);}
92 }
93
94 /* Create plot symbols for possible later use */
95 if(svg_define_symbols(fp, errmsg, verbose)!=0) {
96 fclose(fp); remove(filename); return(NULL);}
97
98 return(fp);
99}
int svg_define_symbols(FILE *fp, char *errmsg, int verbose)
Definition svg_defs.c:83
struct svg_viewport_pos main_viewport
Definition libtpcsvg.h:104

Referenced by plot_fit_svg(), plot_fitrange_svg(), and plot_svg().

◆ svg_write()

int svg_write ( FILE * fp,
const char * svg_string,
char * errmsg,
int verbose )

Write given string into open SVG file

Returns
Returns 0 if successful, <>0 in case of an error.
See also
svg_initiate, svg_xhtml_initiate, svg_xhtml_svg_open
Parameters
fpSVG graphics file pointer.
svg_stringChar pointer to NULL terminated string to be written into file.
errmsgChar pointer to string (at least of length 128) where possible error description is copied; set to NULL if not necessary.
verboseVerbose level; if zero, then nothing is printed to stderr or stdout.

Definition at line 304 of file svg_file.c.

314 {
315 int pnr, len;
316
317 if(verbose>0) printf("%s(fp, svg_string, errmsg, %d)\n", __func__, verbose);
318 if(verbose>1) printf("svg_string := %s\n", svg_string);
319
320 len=strlen(svg_string); if(len<1) return(0);
321 if(fp==NULL) {
322 if(errmsg!=NULL) sprintf(errmsg, "file was closed too early");
323 return(1);
324 }
325 pnr=fprintf(fp, "%s", svg_string);
326 if(pnr<len) {
327 if(errmsg!=NULL) sprintf(errmsg, "cannot write into file");
328 return(2);
329 }
330 return(0);
331}

Referenced by svg_close(), svg_create_legends(), svg_create_main_title(), svg_create_xaxis_title(), svg_create_yaxis_title(), svg_define_symbols(), svg_end_coordinate_viewport(), svg_end_plot_viewport(), svg_initiate(), svg_start_coordinate_viewport(), svg_start_plot_viewport(), svg_write_axes(), svg_write_tac(), svg_write_xticks(), svg_write_yticks(), svg_xhtml_close(), svg_xhtml_initiate(), svg_xhtml_svg_close(), and svg_xhtml_svg_open().

◆ svg_xhtml_close()

int svg_xhtml_close ( FILE * fp,
char * errmsg,
int verbose )

Close XHTML file containing inline SVG.

Returns
Returns 0 if successful, <>0 in case of error.
See also
svg_xhtml_initiate
Parameters
fpSVG graphics file pointer.
errmsgChar pointer to string (at least of length 128) where possible error description is copied; set to NULL if not necessary.
verboseVerbose level; if zero, then nothing is printed to stderr or stdout.

Definition at line 206 of file svg_file.c.

214 {
215 if(verbose>0) printf("%s(fp, errmsg, %d)\n", __func__, verbose);
216
217 if(svg_write(fp, "</body>\n</html>\n", errmsg, verbose-5)!=0) {fclose(fp); return(1);}
218 fflush(fp); fclose(fp);
219
220 SVG_INLINE = 0;
221
222 return(0);
223}
int SVG_INLINE
Definition svg_file.c:12

◆ svg_xhtml_initiate()

FILE * svg_xhtml_initiate ( const char * filename,
const char * XHTML_title,
char * errmsg,
int verbose )

Initiate a new XHTML file for one or more inline SVG graphics files.

If file with same name exists, it is overwritten without backup.

Returns
Returns pointer to the file if successful and NULL in case of an error.
See also
svg_initiate, svg_xhtml_close, svg_xhtml_svg_open
Parameters
filenameFile name for SVG graphics.
XHTML_titleXHTML title; if NULL, then file name is used.
errmsgChar pointer to string (at least of length 128) where possible error description is copied; set to NULL if not necessary.
verboseVerbose level; if zero, then nothing is printed to stderr or stdout.

Definition at line 131 of file svg_file.c.

141 {
142 FILE *fp;
143 char tmp[2048], line[256];
144
145 if(verbose>0)
146 printf("%s(%s, %s, errmsg, %d)\n", __func__, filename, XHTML_title, verbose);
147
148 SVG_INLINE = 1;
149
150 /* Check input */
151 if(filename==NULL) return(NULL);
152 if(XHTML_title==NULL) XHTML_title=filename;
153
154 /* Open file for write */
155 fp=fopen(filename, "w");
156 if(fp==NULL) {
157 if(errmsg!=NULL) strcpy(errmsg, "cannot open file for write");
158 return(fp);
159 }
160
161 strcpy(tmp, "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n");
162 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {
163 fclose(fp); remove(filename); return(NULL);}
164
165 strcpy(tmp, "<!DOCTYPE html PUBLIC\n");
166 strcat(tmp, " \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n");
167 strcat(tmp, " \"https://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n");
168 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {
169 fclose(fp); remove(filename); return(NULL);}
170
171 strcpy(tmp, "<html xmlns=\"https://www.w3.org/1999/xhtml\"\n");
172 strcat(tmp, " xmlns:svg=\"https://www.w3.org/2000/svg\"\n");
173 strcat(tmp, " xmlns:xlink=\"https://www.w3.org/1999/xlink\"\n");
174 strcat(tmp, " xmlns:ev=\"https://www.w3.org/2001/xml-events\"\n");
175 strcat(tmp, " xml:lang=\"en\" lang=\"en\">\n");
176 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {
177 fclose(fp); remove(filename); return(NULL);}
178
179 strcpy(tmp, "<head>\n");
180 sprintf(line, " <title>%s</title>\n", XHTML_title); strcat(tmp, line);
181 sprintf(line, " <meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\" />\n"); strcat(tmp, line);
182 sprintf(line, " <meta http-equiv=\"content-language\" content=\"en-gb\" />\n"); strcat(tmp, line);
183 strcat(tmp, " <object id=\"AdobeSVG\" classid=\"clsid:78156a80-c6a1-4bbf-8e6a-3cd390eeb4e2\"> </object>\n");
184 strcat(tmp, " <?import namespace=\"svg\" urn=\"https://www.w3.org/2000/svg\" implementation=\"#AdobeSVG\"?>\n");
185 strcat(tmp, "</head>\n\n");
186 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {
187 fclose(fp); remove(filename); return(NULL);}
188
189 strcpy(tmp, "<body>\n\n");
190 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) {
191 fclose(fp); remove(filename); return(NULL);}
192
193 /* Create plot symbols for possible later use */
194 if(svg_define_symbols(fp, errmsg, verbose)!=0) {
195 fclose(fp); remove(filename); return(NULL);}
196
197 return(fp);
198}

◆ svg_xhtml_svg_close()

int svg_xhtml_svg_close ( FILE * fp,
char * errmsg,
int verbose )

Close SVG graphics inline in XHTML file. Leaves the file open.

Returns
Returns 0 if successful, <>0 in case of error.
See also
svg_xhtml_svg_open, svg_write
Parameters
fpSVG graphics file pointer.
errmsgChar pointer to string (at least of length 128) where possible error description is copied; set to NULL if not necessary.
verboseVerbose level; if zero, then nothing is printed to stderr or stdout.

Definition at line 283 of file svg_file.c.

291 {
292 if(verbose>0) printf("%s(fp, errmsg, %d)\n", __func__, verbose);
293
294 if(svg_write(fp, "</svg:svg>\n", errmsg, verbose-5)!=0) return(1);
295 return(0);
296}

◆ svg_xhtml_svg_open()

int svg_xhtml_svg_open ( FILE * fp,
const double height,
const double width,
struct svg_viewports * vp,
char * errmsg,
int verbose )

Open a new SVG inline XHTML file.

Returns
Returns 0 if successful, <>0 in case of error.
See also
svg_xhtml_svg_close, svg_xhtml_initiate
Parameters
fpSVG graphics file pointer.
heightPlot height in cm; 0, if not predefined.
widthPlot width in cm; 0, if not predefined.
vpStruct containing the viewport sizes.
errmsgChar pointer to string (at least of length 128) where possible error description is copied; set to NULL if not necessary.
verboseVerbose level; if zero, then nothing is printed to stderr or stdout.

Definition at line 231 of file svg_file.c.

245 {
246 char tmp[1024];
247
248 if(verbose>0)
249 printf("%s(fp, %g, %g, vp, errmsg, %d)\n", __func__, height, width, verbose);
250
251 /* Check input */
252 if(fp==NULL || vp==NULL) return(1);
253 if(vp->main_viewport.w<3 || vp->main_viewport.h<3) return(1);
254
255 strcpy(tmp, "<svg:svg version=\"1.1\" baseProfile=\"full\"");
256 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) return(2);
257
258 sprintf(tmp, "\n viewBox=\"0 0 %d %d\"", vp->main_viewport.w, vp->main_viewport.h);
259 strcat(tmp, "\n preserveAspectRatio=\"xMinYMin meet\"");
260 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) return(3);
261
262 if(width>0.0) {
263 sprintf(tmp, "\n width=\"%gcm\"", width);
264 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) return(4);
265 }
266 if(height>0.0) {
267 sprintf(tmp, "\n height=\"%gcm\"", height);
268 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) return(5);
269 }
270
271 strcpy(tmp, ">\n");
272 if(svg_write(fp, tmp, errmsg, verbose-5)!=0) return(6);
273
274 return(0);
275}

Variable Documentation

◆ SVG_INLINE